From 87cc6d58be20e9a0c304ec52c80aca9e266fb256 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 6 Dec 2020 14:11:06 +0100 Subject: [PATCH] Re-factor the `scripting` getter in GENERIC-builds, since using the same 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. --- web/genericcom.js | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/web/genericcom.js b/web/genericcom.js index 73f78b045ac433..2b83f0dfec1075 100644 --- a/web/genericcom.js +++ b/web/genericcom.js @@ -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( @@ -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(); @@ -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;