pdf.js/src/pdf.sandbox.js
Jonas Jenwald c78f153bda Remove the ENABLE_SCRIPTING build-target, since it's not necessary
There's no really compelling reason, as far as I can tell, to introduce the `ENABLE_SCRIPTING` build-target, instead of simply re-using the existing `TESTING` build-target for the new `gulp integrationtest` task.

In general there should be no problem with just always enable scripting in TESTING-builds, and if I were to *guess* the reason that this didn't seem to work was most likely because the Preferences ended up over-writing the `AppOptions`.
As it turns out the GENERIC-viewer has already has built-in support for disabling of Preferences, via the `AppOptions`, and this can be utilized in TESTING-builds as well to ensure that whatever `AppOptions` are set they're always respected.
2020-12-18 22:10:36 +01:00

145 lines
3.7 KiB
JavaScript

/* 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 ModuleLoader from "../external/quickjs/quickjs-eval.js";
import { SandboxSupportBase } from "./pdf.sandbox.external.js";
/* eslint-disable-next-line no-unused-vars */
const pdfjsVersion = PDFJSDev.eval("BUNDLE_VERSION");
/* eslint-disable-next-line no-unused-vars */
const pdfjsBuild = PDFJSDev.eval("BUNDLE_BUILD");
const TESTING =
typeof PDFJSDev === "undefined" || PDFJSDev.test("!PRODUCTION || TESTING");
class SandboxSupport extends SandboxSupportBase {
exportValueToSandbox(val) {
// The communication with the Quickjs sandbox is based on strings
// So we use JSON.stringfy to serialize
return JSON.stringify(val);
}
importValueFromSandbox(val) {
return val;
}
createErrorForSandbox(errorMessage) {
return new Error(errorMessage);
}
}
class Sandbox {
constructor(win, module) {
this.support = new SandboxSupport(win, this);
// The "external" functions created in pdf.sandbox.external.js
// are finally used here:
// https://github.com/mozilla/pdf.js.quickjs/blob/main/src/myjs.js
// They're called from the sandbox only.
module.externalCall = this.support.createSandboxExternals();
this._module = module;
// 0 to display error using console.error
// else display error using window.alert
this._alertOnError = 0;
}
create(data) {
if (TESTING) {
this._module.ccall("nukeSandbox", null, []);
}
const sandboxData = JSON.stringify(data);
const code = [
PDFJSDev.eval("PDF_SCRIPTING_JS_SOURCE"),
`pdfjsScripting.initSandbox({ data: ${sandboxData} })`,
];
if (!TESTING) {
code.push("delete dump;");
} else {
code.unshift(
`globalThis.sendResultForTesting = callExternalFunction.bind(null, "send");`
);
}
let success = false;
try {
success = !!this._module.ccall(
"init",
"number",
["string", "number"],
[code.join("\n"), this._alertOnError]
);
} catch (error) {
console.error(error);
}
if (success) {
this.support.commFun = this._module.cwrap("commFun", null, [
"string",
"string",
]);
} else {
this.nukeSandbox();
throw new Error("Cannot start sandbox");
}
}
dispatchEvent(event) {
this.support.callSandboxFunction("dispatchEvent", event);
}
dumpMemoryUse() {
if (this._module) {
this._module.ccall("dumpMemoryUse", null, []);
}
}
nukeSandbox() {
if (this._module !== null) {
this.support.destroy();
this.support = null;
this._module.ccall("nukeSandbox", null, []);
this._module = null;
}
}
evalForTesting(code, key) {
if (TESTING) {
this._module.ccall(
"evalInSandbox",
null,
["string", "int"],
[
`try {
sendResultForTesting([{ id: "${key}", result: ${code} }]);
} catch (error) {
sendResultForTesting([{ id: "${key}", result: error.message }]);
}`,
this._alertOnError,
]
);
}
}
}
function QuickJSSandbox() {
return ModuleLoader().then(module => {
return new Sandbox(window, module);
});
}
export { QuickJSSandbox };