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AI,EAAE;MACzB,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;MACtB,IAAI,CAAC,OAAO,GAAG,IAAI;MACnB,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,aAAa,EAAE,IAAI,EAAE,EAAE,CAAC;MAC3C,IAAI,CAAC,OAAO,GAAG,IAAI;IACrB;EACF;EAEA,cAAc,CAAC,IAAI,EAAE,GAAG,EAAE;IAgBtB,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC;EAEtD;AACF;AAEA,eAAe,cAAc,CAAC,OAAO,GAAG,cAAc,EAAE;EACtD,MAAM;IAAE,OAAO,EAAE;EAAa,CAAC,GAAG;EAAA;EAAA;EAChC,GAAG,OAAO,iBAAiB,CAC5B;EACD,MAAM,MAAM,GAAG,MAAM,YAAY,CAAC,CAAC;EACnC,OAAO,IAAI,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC;AACpC;AAEA,UAAU,CAAC,YAAY,GAAG;EACxB;AACF,CAAC","sources":["webpack://pdf.js/./src/pdf.sandbox.external.js","webpack://pdf.js/./src/pdf.sandbox.js"],"sourcesContent":["/* Copyright 2020 Mozilla Foundation\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n// In mozilla-central, this file is loaded as non-module script,\n// so it mustn't have any dependencies.\n\n// eslint-disable-next-line no-restricted-syntax\nexport class SandboxSupportBase {\n /**\n * @param {DOMWindow} - win\n */\n constructor(win) {\n this.win = win;\n this.timeoutIds = new Map();\n\n // Will be assigned after the sandbox is initialized\n this.commFun = null;\n }\n\n destroy() {\n this.commFun = null;\n for (const id of this.timeoutIds.values()) {\n this.win.clearTimeout(id);\n }\n this.timeoutIds = null;\n }\n\n /**\n * @param {Object} val - Export a value in the sandbox.\n */\n exportValueToSandbox(val) {\n throw new Error(\"Not implemented\");\n }\n\n /**\n * @param {Object} val - Import a value from the sandbox.\n */\n importValueFromSandbox(val) {\n throw new Error(\"Not implemented\");\n }\n\n /**\n * @param {String} errorMessage - Create an error in the sandbox.\n */\n createErrorForSandbox(errorMessage) {\n throw new Error(\"Not implemented\");\n }\n\n /**\n * @param {String} name - Name of the function to call in the sandbox\n * @param {Array<Object>} args - Arguments of the function.\n */\n callSandboxFunction(name, args) {\n if (!this.commFun) {\n return;\n }\n try {\n args = this.exportValueToSandbox(args);\n this.commFun(name, args);\n } catch (e) {\n this.win.console.error(e);\n }\n }\n\n createSandboxExternals() {\n // All the functions in externals object are called\n // from the sandbox.\n const externals = {\n setTimeout: (callbackId, nMilliseconds) => {\n if (\n typeof callbackId !== \"number\" ||\n typeof nMilliseconds !== \"number\"\n ) {\n return;\n }\n\n if (callbackId === 0) {\n // This callbackId corresponds to the one used for userActivation.\n // So here, we cancel the last userActivation.\n this.win.clearTimeout(this.timeoutIds.get(callbackId));\n }\n\n const id = this.win.setTimeout(() => {\n this.timeoutIds.delete(callbackId);\n this.callSandboxFunction(\"timeoutCb\", {\n callbackId,\n interval: false,\n });\n }, nMilliseconds);\n this.timeoutIds.set(callbackId, id);\n },\n clearTimeout: callbackId => {\n this.win.clearTimeout(this.timeoutIds.get(callbackId));\n this.timeoutIds.delete(callbackId);\n },\n setInterval: (callbackId, nMilliseconds) => {\n if (\n typeof callbackId !== \"number\" ||\n typeof nMilliseconds !== \"number\"\n ) {\n return;\n }\n const id = this.win.setInterval(() => {\n this.callSandboxFunction(\"timeoutCb\", {\n callbackId,\n interval: true,\n });\n }, nMilliseconds);\n this.timeoutIds.set(callbackId, id);\n },\n clearInterval: callbackId => {\n this.win.clearInterval(this.timeoutIds.get(callbackId));\n this.timeoutIds.delete(callbackId);\n },\n alert: cMsg => {\n if (typeof cMsg !== \"string\") {\n return;\n }\n this.win.alert(cMsg);\n },\n confirm: cMsg => {\n if (typeof cMsg !== \"string\") {\n return false;\n }\n return this.win.confirm(cMsg);\n },\n prompt: (cQuestion, cDefault) => {\n if (typeof cQuestion !== \"string\" || typeof cDefault !== \"string\") {\n return null;\n }\n return this.win.prompt(cQuestion, cDefault);\n },\n parseURL: cUrl => {\n const url = new this.win.URL(cUrl);\n const props = [\n \"hash\",\n \"host\",\n \"hostname\",\n \"href\",\n \"origin\",\n \"password\",\n \"pathname\",\n \"port\",\n \"protocol\",\n \"search\",\n \"searchParams\",\n \"username\",\n ];\n\n return Object.fromEntries(\n props.map(name => [name, url[name].toString()])\n );\n },\n send: data => {\n if (!data) {\n return;\n }\n const event = new this.win.CustomEvent(\"updatefromsandbox\", {\n detail: this.importValueFromSandbox(data),\n });\n this.win.dispatchEvent(event);\n },\n };\n Object.setPrototypeOf(externals, null);\n\n return (name, args) => {\n try {\n const result = externals[name](...args);\n return this.exportValueToSandbox(result);\n } catch (error) {\n throw this.createErrorForSandbox(error?.toString() ?? \"\");\n }\n };\n }\n}\n","/* Copyright 2020 Mozilla Foundation\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { SandboxSupportBase } from \"./pdf.sandbox.external.js\";\n\nclass SandboxSupport extends SandboxSupportBase {\n exportValueToSandbox(val) {\n // The communication with the Quickjs sandbox is based on strings\n // So we use JSON.stringfy to serialize\n return JSON.stringify(val);\n }\n\n importValueFromSandbox(val) {\n return val;\n }\n\n createErrorForSandbox(errorMessage) {\n return new Error(errorMessage);\n }\n}\n\nclass Sandbox {\n constructor(win, module) {\n this.support = new SandboxSupport(win, this);\n\n // The \"external\" functions created in pdf.sandbox.external.js\n // are finally used here:\n // https://github.com/mozilla/pdf.js.quickjs/blob/main/src/myjs.js\n // They're called from the sandbox only.\n module.externalCall = this.support.createSandboxExternals();\n\n this._module = module;\n\n // 0 to display error using console.error\n // else display error using window.alert\n this._alertOnError = 0;\n }\n\n create(data) {\n if (PDFJSDev.test(\"TESTING\")) {\n this._module.ccall(\"nukeSandbox\", null, []);\n }\n const code = [PDFJSDev.eval(\"PDF_SCRIPTING_JS_SOURCE\")];\n\n if (PDFJSDev.test(\"TESTING\")) {\n code.push(\n `globalThis.sendResultForTesting = callExternalFunction.bind(null, \"send\");`\n );\n } else {\n code.push(\"delete dump;\");\n }\n\n let success = false;\n let buf = 0;\n try {\n const sandboxData = JSON.stringify(data);\n // \"pdfjsScripting.initSandbox...\" MUST be the last line to be evaluated\n // since the returned value is used for the communication.\n code.push(`pdfjsScripting.initSandbox({ data: ${sandboxData} })`);\n buf = this._module.stringToNewUTF8(code.join(\"\\n\"));\n\n success = !!this._module.ccall(\n \"init\",\n \"number\",\n [\"number\", \"number\"],\n [buf, this._alertOnError]\n );\n } catch (error) {\n // eslint-disable-next-line no-console\n console.error(error);\n } finally {\n if (buf) {\n this._module.ccall(\"free\", \"number\", [\"number\"], [buf]);\n }\n }\n\n if (success) {\n this.support.commFun = this._module.cwrap(\"commFun\", null, [\n \"string\",\n \"string\",\n ]);\n } else {\n this.nukeSandbox();\n throw new Error(\"Cannot start sandbox\");\n }\n }\n\n dispatchEvent(event) {\n this.support?.callSandboxFunction(\"dispatchEvent\", event);\n }\n\n dumpMemoryUse() {\n this._module?.ccall(\"dumpMemoryUse\", null, []);\n }\n\n nukeSandbox() {\n if (this._module !== null) {\n this.support.destroy();\n this.support = null;\n this._module.ccall(\"nukeSandbox\", null, []);\n this._module = null;\n }\n }\n\n evalForTesting(code, key) {\n if (PDFJSDev.test(\"TESTING\")) {\n this._module.ccall(\n \"evalInSandbox\",\n null,\n [\"string\", \"int\"],\n [\n `try {\n sendResultForTesting([{ id: \"${key}\", result: ${code} }]);\n } catch (error) {\n sendResultForTesting([{ id: \"${key}\", result: error.message }]);\n }`,\n this._alertOnError,\n ]\n );\n } else {\n throw new Error(\"Not implemented: evalForTesting\");\n }\n }\n}\n\nasync function QuickJSSandbox(wasmUrl = \"../web/wasm/\") {\n const { default: ModuleLoader } = await __raw_import__(\n `${wasmUrl}quickjs-eval.js`\n );\n const module = await ModuleLoader();\n return new Sandbox(window, module);\n}\n\nglobalThis.pdfjsSandbox = {\n QuickJSSandbox,\n};\n\nexport { QuickJSSandbox };\n"],"names":[],"sourceRoot":""}