Skip to content

Re-factor the PDFScriptingManager._destroyScripting method (PR 13042 follow-up) #13074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions web/base_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,7 @@ class BaseViewer {
this.findController.setDocument(null);
}
if (this._scriptingManager) {
// Defer this slightly, to allow the "PageClose" event to be handled.
Promise.resolve().then(() => {
this._scriptingManager.setDocument(null);
});
this._scriptingManager.setDocument(null);
}
}

Expand Down
29 changes: 24 additions & 5 deletions web/pdf_scripting_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { RenderingStates } from "./pdf_rendering_queue.js";

class PDFScriptingManager {
/**
* @param {PDFScriptingManager} options
* @param {PDFScriptingManagerOptions} options
*/
constructor({
eventBus,
Expand All @@ -41,6 +41,7 @@ class PDFScriptingManager {
}) {
this._pdfDocument = null;
this._pdfViewer = null;
this._closeCapability = null;
this._destroyCapability = null;

this._scripting = null;
Expand Down Expand Up @@ -124,8 +125,10 @@ class PDFScriptingManager {
}
this._dispatchPageOpen(pageNumber);
});
this._internalEvents.set("pagesdestroy", event => {
this._dispatchPageClose(this._pdfViewer.currentPageNumber);
this._internalEvents.set("pagesdestroy", async event => {
await this._dispatchPageClose(this._pdfViewer.currentPageNumber);

this._closeCapability?.resolve();
});

this._domEvents.set("mousedown", event => {
Expand Down Expand Up @@ -307,6 +310,8 @@ class PDFScriptingManager {
visitedPages = this._visitedPages;

if (initialize) {
this._closeCapability = createPromiseCapability();

this._pageEventsReady = true;
}
if (!this._pageEventsReady) {
Expand Down Expand Up @@ -417,12 +422,26 @@ class PDFScriptingManager {
* @private
*/
async _destroyScripting() {
this._pdfDocument = null; // Ensure that it's *always* reset synchronously.

if (!this._scripting) {
this._pdfDocument = null;

this._destroyCapability?.resolve();
return;
}
if (this._closeCapability) {
await Promise.race([
this._closeCapability.promise,
new Promise(resolve => {
// Avoid the scripting/sandbox-destruction hanging indefinitely.
setTimeout(resolve, 1000);
}),
]).catch(reason => {
// Ignore any errors, to ensure that the sandbox is always destroyed.
});
this._closeCapability = null;
}
this._pdfDocument = null;

try {
await this._scripting.destroySandbox();
} catch (ex) {}
Expand Down