Skip to content

Commit

Permalink
Re-factor the scripting getter in GENERIC-builds, since using the s…
Browse files Browse the repository at this point in the history
…ame sandbox for *multiple* PDF documents seems highly questionable

Similar to the previous patch, the GENERIC default viewer capable of opening more than *one* PDF document and we should ensure that we handle that case correctly.
  • Loading branch information
Snuffleupagus committed Dec 6, 2020
1 parent 1e007f9 commit 87cc6d5
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions web/genericcom.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
*/

import { DefaultExternalServices, PDFViewerApplication } from "./app.js";
import { loadScript, shadow } from "pdfjs-lib";
import { AppOptions } from "./app_options.js";
import { BasePreferences } from "./preferences.js";
import { DownloadManager } from "./download_manager.js";
import { GenericL10n } from "./genericl10n.js";
import { loadScript } from "pdfjs-lib";

if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("GENERIC")) {
throw new Error(
Expand All @@ -39,6 +39,29 @@ class GenericPreferences extends BasePreferences {
}
}

class GenericScripting {
constructor() {
this._ready = loadScript(AppOptions.get("sandboxBundleSrc")).then(() => {
return window.pdfjsSandbox.QuickJSSandbox();
});
}

async createSandbox(data) {
const sandbox = await this._ready;
sandbox.create(data);
}

async dispatchEventInSandbox(event) {
const sandbox = await this._ready;
sandbox.dispatchEvent(event);
}

async destroySandbox() {
const sandbox = await this._ready;
sandbox.nukeSandbox();
}
}

class GenericExternalServices extends DefaultExternalServices {
static createDownloadManager(options) {
return new DownloadManager();
Expand All @@ -53,22 +76,7 @@ class GenericExternalServices extends DefaultExternalServices {
}

static get scripting() {
const promise = loadScript(AppOptions.get("sandboxBundleSrc")).then(() => {
return window.pdfjsSandbox.QuickJSSandbox();
});
const sandbox = {
createSandbox(data) {
promise.then(sbx => sbx.create(data));
},
dispatchEventInSandbox(event) {
promise.then(sbx => sbx.dispatchEvent(event));
},
destroySandbox() {
promise.then(sbx => sbx.nukeSandbox());
},
};

return shadow(this, "scripting", sandbox);
return new GenericScripting();
}
}
PDFViewerApplication.externalServices = GenericExternalServices;
Expand Down

0 comments on commit 87cc6d5

Please sign in to comment.