-
Notifications
You must be signed in to change notification settings - Fork 10k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In order to simplify m-c code, move some in pdf.js
* move set/clear|Timeout/Interval and crackURL code in pdf.js * remove the "backdoor" in the proxy (used to dispatch event) and so return the dispatch function in the initializer * remove listeners if an error occured during sandbox initialization * add support for alert and prompt in the sandbox * add a function to eval in the global scope
- Loading branch information
1 parent
00b4f86
commit 68764a7
Showing
15 changed files
with
372 additions
and
247 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* 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. | ||
*/ | ||
|
||
// In mozilla-central, this file is loaded as non-module script, | ||
// so it mustn't have any dependencies. | ||
|
||
function buildExternals(win, sandbox) { | ||
const timeoutIds = new Map(); | ||
return { | ||
setTimeout(callbackId, nMilliseconds) { | ||
if (typeof callbackId !== "number" || typeof nMilliseconds !== "number") { | ||
return; | ||
} | ||
const id = win.setTimeout(() => { | ||
timeoutIds.delete(callbackId); | ||
sandbox.callFunction("timeoutCb", { callbackId, interval: false }); | ||
}, nMilliseconds); | ||
timeoutIds.set(callbackId, id); | ||
}, | ||
clearTimeout(id) { | ||
win.clearTimeout(timeoutIds.get(id)); | ||
timeoutIds.delete(id); | ||
}, | ||
setInterval(callbackId, nMilliseconds) { | ||
if (typeof callbackId !== "number" || typeof nMilliseconds !== "number") { | ||
return; | ||
} | ||
const id = win.setInterval(() => { | ||
sandbox.callFunction("timeoutCb", { callbackId, interval: true }); | ||
}, nMilliseconds); | ||
timeoutIds.set(callbackId, id); | ||
}, | ||
clearInterval(id) { | ||
win.clearInterval(timeoutIds.get(id)); | ||
timeoutIds.delete(id); | ||
}, | ||
cleanTimeouts() { | ||
timeoutIds.forEach(([_, id]) => win.clearTimeout(id)); | ||
timeoutIds.clear(); | ||
}, | ||
alert(cMsg) { | ||
if (typeof cMsg !== "string") { | ||
return; | ||
} | ||
win.alert(cMsg); | ||
}, | ||
prompt(cQuestion, cDefault) { | ||
if (typeof cQuestion !== "string" || typeof cDefault !== "string") { | ||
return null; | ||
} | ||
return win.prompt(cQuestion, cDefault); | ||
}, | ||
parseURL(cUrl) { | ||
let ret; | ||
try { | ||
const url = new win.URL(cUrl); | ||
const props = [ | ||
"hash", | ||
"host", | ||
"hostname", | ||
"href", | ||
"origin", | ||
"password", | ||
"pathname", | ||
"port", | ||
"protocol", | ||
"search", | ||
"searchParams", | ||
"username", | ||
]; | ||
|
||
ret = Object.fromEntries( | ||
props.map(name => [name, url[name].toString()]) | ||
); | ||
} catch (err) { | ||
ret = { error: err.message }; | ||
} | ||
return sandbox.cloneIn(ret); | ||
}, | ||
send(data) { | ||
try { | ||
if (!data) { | ||
return; | ||
} | ||
const detail = sandbox.cloneOut(data, win); | ||
const event = new win.CustomEvent("updateFromSandbox", { detail }); | ||
win.dispatchEvent(event); | ||
} catch (_) {} | ||
}, | ||
}; | ||
} | ||
|
||
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) { | ||
exports.buildExternals = buildExternals; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.