Merge pull request #12703 from Snuffleupagus/GenericScripting-rm-scriptElement

Ensure that the `pdf.sandbox.js` is removed from the DOM on destroy, and unbreak the Chromium-extension (PR 12695 follow-up)
This commit is contained in:
Tim van der Meij 2020-12-09 23:09:06 +01:00 committed by GitHub
commit 0629a8f553
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 110 additions and 51 deletions

View File

@ -530,14 +530,20 @@ function isValidFetchUrl(url, baseUrl) {
/** /**
* @param {string} src * @param {string} src
* @param {boolean} [removeScriptElement]
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
function loadScript(src) { function loadScript(src, removeScriptElement = false) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const script = document.createElement("script"); const script = document.createElement("script");
script.src = src; script.src = src;
script.onload = resolve; script.onload = function (evt) {
if (removeScriptElement) {
script.remove();
}
resolve(evt);
};
script.onerror = function () { script.onerror = function () {
reject(new Error(`Cannot load script at: ${script.src}`)); reject(new Error(`Cannot load script at: ${script.src}`));
}; };

View File

@ -168,6 +168,10 @@ class DefaultExternalServices {
throw new Error("Not implemented: createL10n"); throw new Error("Not implemented: createL10n");
} }
static createScripting() {
throw new Error("Not implemented: createScripting");
}
static get supportsIntegratedFind() { static get supportsIntegratedFind() {
return shadow(this, "supportsIntegratedFind", false); return shadow(this, "supportsIntegratedFind", false);
} }
@ -186,10 +190,6 @@ class DefaultExternalServices {
static get isInAutomation() { static get isInAutomation() {
return shadow(this, "isInAutomation", false); return shadow(this, "isInAutomation", false);
} }
static get scripting() {
throw new Error("Not implemented: scripting");
}
} }
const PDFViewerApplication = { const PDFViewerApplication = {
@ -763,6 +763,39 @@ const PDFViewerApplication = {
document.title = title; document.title = title;
}, },
/**
* @private
*/
_cancelIdleCallbacks() {
if (!this._idleCallbacks.size) {
return;
}
for (const callback of this._idleCallbacks) {
window.cancelIdleCallback(callback);
}
this._idleCallbacks.clear();
},
/**
* @private
*/
async _destroyScriptingInstance() {
if (!this._scriptingInstance) {
return;
}
const { scripting, events } = this._scriptingInstance;
try {
await scripting.destroySandbox();
} catch (ex) {}
for (const [name, listener] of events) {
window.removeEventListener(name, listener);
}
events.clear();
this._scriptingInstance = null;
},
/** /**
* Closes opened PDF document. * Closes opened PDF document.
* @returns {Promise} - Returns the promise, which is resolved when all * @returns {Promise} - Returns the promise, which is resolved when all
@ -775,8 +808,9 @@ const PDFViewerApplication = {
if (!this.pdfLoadingTask) { if (!this.pdfLoadingTask) {
return undefined; return undefined;
} }
const promises = [];
const promise = this.pdfLoadingTask.destroy(); promises.push(this.pdfLoadingTask.destroy());
this.pdfLoadingTask = null; this.pdfLoadingTask = null;
if (this.pdfDocument) { if (this.pdfDocument) {
@ -799,22 +833,9 @@ const PDFViewerApplication = {
this._contentLength = null; this._contentLength = null;
this.triggerDelayedFallback = null; this.triggerDelayedFallback = null;
this._saveInProgress = false; this._saveInProgress = false;
for (const callback of this._idleCallbacks) {
window.cancelIdleCallback(callback);
}
this._idleCallbacks.clear();
if (this._scriptingInstance) { this._cancelIdleCallbacks();
const { scripting, events } = this._scriptingInstance; promises.push(this._destroyScriptingInstance());
try {
scripting.destroySandbox();
} catch (ex) {}
for (const [name, listener] of events) {
window.removeEventListener(name, listener);
}
this._scriptingInstance = null;
}
this.pdfSidebar.reset(); this.pdfSidebar.reset();
this.pdfOutlineViewer.reset(); this.pdfOutlineViewer.reset();
@ -833,7 +854,7 @@ const PDFViewerApplication = {
if (typeof PDFBug !== "undefined") { if (typeof PDFBug !== "undefined") {
PDFBug.cleanup(); PDFBug.cleanup();
} }
return promise; return Promise.all(promises);
}, },
/** /**
@ -1435,7 +1456,7 @@ const PDFViewerApplication = {
// or the document was closed while the data resolved. // or the document was closed while the data resolved.
return; return;
} }
const { scripting } = this.externalServices; const scripting = this.externalServices.createScripting();
// Store a reference to the current scripting-instance, to allow destruction // Store a reference to the current scripting-instance, to allow destruction
// of the sandbox and removal of the event listeners at document closing. // of the sandbox and removal of the event listeners at document closing.
this._scriptingInstance = { scripting, events: new Map() }; this._scriptingInstance = { scripting, events: new Map() };

View File

@ -265,6 +265,12 @@ if (
: "../build/pdf.sandbox.js", : "../build/pdf.sandbox.js",
kind: OptionKind.VIEWER, kind: OptionKind.VIEWER,
}; };
} else if (PDFJSDev.test("CHROME")) {
defaultOptions.sandboxBundleSrc = {
/** @type {string} */
value: "../build/pdf.sandbox.js",
kind: OptionKind.VIEWER,
};
} }
const userOptions = Object.create(null); const userOptions = Object.create(null);

View File

@ -19,6 +19,7 @@ import { AppOptions } from "./app_options.js";
import { BasePreferences } from "./preferences.js"; import { BasePreferences } from "./preferences.js";
import { DownloadManager } from "./download_manager.js"; import { DownloadManager } from "./download_manager.js";
import { GenericL10n } from "./genericl10n.js"; import { GenericL10n } from "./genericl10n.js";
import { GenericScripting } from "./generic_scripting.js";
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("CHROME")) { if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("CHROME")) {
throw new Error( throw new Error(
@ -428,6 +429,10 @@ class ChromeExternalServices extends DefaultExternalServices {
static createL10n(options) { static createL10n(options) {
return new GenericL10n(navigator.language); return new GenericL10n(navigator.language);
} }
static createScripting() {
return new GenericScripting();
}
} }
PDFViewerApplication.externalServices = ChromeExternalServices; PDFViewerApplication.externalServices = ChromeExternalServices;

View File

@ -360,7 +360,7 @@ class FirefoxExternalServices extends DefaultExternalServices {
return new MozL10n(mozL10n); return new MozL10n(mozL10n);
} }
static get scripting() { static createScripting() {
return FirefoxScripting; return FirefoxScripting;
} }

45
web/generic_scripting.js Normal file
View File

@ -0,0 +1,45 @@
/* Copyright 2020 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AppOptions } from "./app_options.js";
import { loadScript } from "pdfjs-lib";
class GenericScripting {
constructor() {
this._ready = loadScript(
AppOptions.get("sandboxBundleSrc"),
/* removeScriptElement = */ true
).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();
}
}
export { GenericScripting };

View File

@ -14,11 +14,10 @@
*/ */
import { DefaultExternalServices, PDFViewerApplication } from "./app.js"; import { DefaultExternalServices, PDFViewerApplication } from "./app.js";
import { AppOptions } from "./app_options.js";
import { BasePreferences } from "./preferences.js"; import { BasePreferences } from "./preferences.js";
import { DownloadManager } from "./download_manager.js"; import { DownloadManager } from "./download_manager.js";
import { GenericL10n } from "./genericl10n.js"; import { GenericL10n } from "./genericl10n.js";
import { loadScript } from "pdfjs-lib"; import { GenericScripting } from "./generic_scripting.js";
if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("GENERIC")) { if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("GENERIC")) {
throw new Error( throw new Error(
@ -39,29 +38,6 @@ 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 { class GenericExternalServices extends DefaultExternalServices {
static createDownloadManager(options) { static createDownloadManager(options) {
return new DownloadManager(); return new DownloadManager();
@ -75,7 +51,7 @@ class GenericExternalServices extends DefaultExternalServices {
return new GenericL10n(locale); return new GenericL10n(locale);
} }
static get scripting() { static createScripting() {
return new GenericScripting(); return new GenericScripting();
} }
} }