Skip to content

Commit

Permalink
In order to simplify m-c code, move some in pdf.js
Browse files Browse the repository at this point in the history
 * 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
 * inject doc properties in the global scope
  • Loading branch information
calixteman committed Dec 6, 2020
1 parent 54ca67d commit 9d8fb6b
Show file tree
Hide file tree
Showing 18 changed files with 461 additions and 220 deletions.
50 changes: 24 additions & 26 deletions external/quickjs/quickjs-eval.js

Large diffs are not rendered by default.

15 changes: 14 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,19 @@ function createScriptingBundle(defines) {
.src("./src/pdf.scripting.js")
.pipe(webpack2Stream(scriptingFileConfig))
.pipe(replaceWebpackRequire())
.pipe(replaceJSRootName(scriptingAMDName, "pdfjsScripting"));
.pipe(
replace(
'root["' + scriptingAMDName + '"] = factory()',
"root.pdfjsScripting = factory()"
)
);
}

function copyScriptingExtra() {
return gulp
.src("./src/scripting_api/extra.js")
.pipe(rename("pdf.scripting.extra.js"))
.pipe(replace("export { buildExtra };", ""));
}

function createSandboxBundle(defines, code) {
Expand Down Expand Up @@ -1195,6 +1207,7 @@ gulp.task(
createScriptingBundle(defines).pipe(
gulp.dest(MOZCENTRAL_CONTENT_DIR + "build")
),
copyScriptingExtra().pipe(gulp.dest(MOZCENTRAL_CONTENT_DIR + "build")),
createWorkerBundle(defines).pipe(
gulp.dest(MOZCENTRAL_CONTENT_DIR + "build")
),
Expand Down
4 changes: 1 addition & 3 deletions src/scripting_api/aform.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
*/

class AForm {
constructor(document, app, util) {
this._document = document;
constructor(app, util) {
this._app = app;
this._util = util;
}
Expand All @@ -28,7 +27,6 @@ class AForm {
strCurrency,
bCurrencyPrepend
) {
const event = this._document._event;
if (!event.value) {
return;
}
Expand Down
32 changes: 12 additions & 20 deletions src/scripting_api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,9 @@ class App extends PDFObject {
data.calculationOrder,
this._objects
);
this._setTimeout = data.setTimeout;
this._clearTimeout = data.clearTimeout;
this._setInterval = data.setInterval;
this._clearInterval = data.clearInterval;
this._timeoutIds = null;
this._timeoutIdsRegistry = null;

// used in proxy.js to check that this is the object with the backdoor
this._isApp = true;
this._extra = data.extra;
}

// This function is called thanks to the proxy
Expand Down Expand Up @@ -90,7 +84,7 @@ class App extends PDFObject {
);
}
}
this._timeoutIds.set(timeout, [id, interval]);
this._timeoutIds.set(timeout, id);
if (this._timeoutIdsRegistry) {
this._timeoutIdsRegistry.register(timeout, [id, interval]);
}
Expand All @@ -100,16 +94,16 @@ class App extends PDFObject {
if (!this._timeoutIds || !this._timeoutIds.has(timeout)) {
return;
}
const [id, interval] = this._timeoutIds.get(timeout);
const { id, interval } = this._timeoutIds.get(timeout);
if (this._timeoutIdsRegistry) {
this._timeoutIdsRegistry.unregister(timeout);
}
this._timeoutIds.delete(timeout);

if (interval) {
this._clearInterval(id);
this._extra.clearInterval(id);
} else {
this._clearTimeout(id);
this._extra.clearTimeout(id);
}
}

Expand Down Expand Up @@ -409,7 +403,7 @@ class App extends PDFObject {
oDoc = null,
oCheckbox = null
) {
this._send({ command: "alert", value: cMsg });
this._extra.alert(cMsg);
}

beep() {
Expand All @@ -425,11 +419,11 @@ class App extends PDFObject {
}

clearInterval(oInterval) {
this.unregisterTimeout(oInterval);
this._unregisterTimeout(oInterval);
}

clearTimeOut(oTime) {
this.unregisterTimeout(oTime);
this._unregisterTimeout(oTime);
}

endPriv() {
Expand Down Expand Up @@ -524,8 +518,8 @@ class App extends PDFObject {
/* Not implemented */
}

response() {
/* TODO or not */
response(cQuestion, cTitle = "", cDefault = "", bPassword = "", cLabel = "") {
return this._extra.prompt(cQuestion, cDefault);
}

setInterval(cExpr, nMilliseconds) {
Expand All @@ -537,8 +531,7 @@ class App extends PDFObject {
"Second argument of app.setInterval must be a number"
);
}

const id = this._setInterval(cExpr, nMilliseconds);
const id = this._extra.setInterval(cExpr, nMilliseconds);
const timeout = Object.create(null);
this._registerTimeout(timeout, id, true);
return timeout;
Expand All @@ -551,8 +544,7 @@ class App extends PDFObject {
if (typeof nMilliseconds !== "number") {
throw new TypeError("Second argument of app.setTimeOut must be a number");
}

const id = this._setTimeout(cExpr, nMilliseconds);
const id = this._extra.setTimeout(cExpr, nMilliseconds);
const timeout = Object.create(null);
this._registerTimeout(timeout, id, false);
return timeout;
Expand Down
113 changes: 100 additions & 13 deletions src/scripting_api/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,18 @@ class InfoProxyHandler {
class Doc extends PDFObject {
constructor(data) {
super(data);
this.calculate = true;

this.baseURL = data.baseURL || "";
this.calculate = true;
this.delay = false;
this.dirty = false;
this.disclosed = false;
this.media = undefined;
this.metadata = data.metadata;
this.noautocomplete = undefined;
this.nocache = undefined;
this.spellDictionaryOrder = [];
this.spellLanguageOrder = [];

this._baseURL = data.baseURL || "";
this._calculate = true;
this._delay = false;
this._dirty = false;
this._disclosed = false;
this._media = undefined;
this._metadata = data.metadata;
this._noautocomplete = undefined;
this._nocache = undefined;
this._spellDictionaryOrder = [];
this._spellLanguageOrder = [];

this._printParams = null;
this._fields = new Map();
Expand Down Expand Up @@ -128,6 +127,14 @@ class Doc extends PDFObject {
throw new Error("doc.author is read-only");
}

get baseURL() {
return this._baseURL;
}

set baseURL(baseURL) {
this._baseURL = baseURL;
}

get bookmarkRoot() {
return undefined;
}
Expand All @@ -136,6 +143,14 @@ class Doc extends PDFObject {
throw new Error("doc.bookmarkRoot is read-only");
}

get calculate() {
return this._calculate;
}

set calculate(calculate) {
this._calculate = calculate;
}

get creator() {
return this._creator;
}
Expand All @@ -152,6 +167,30 @@ class Doc extends PDFObject {
throw new Error("doc.dataObjects is read-only");
}

get delay() {
return this._delay;
}

set delay(delay) {
this._delay = delay;
}

get dirty() {
return this._dirty;
}

set dirty(dirty) {
this._dirty = dirty;
}

get disclosed() {
return this._disclosed;
}

set disclosed(disclosed) {
this._disclosed = disclosed;
}

get docID() {
return this._docID;
}
Expand Down Expand Up @@ -279,6 +318,22 @@ class Doc extends PDFObject {
this._layout = value;
}

get media() {
return this._media;
}

set media(media) {
this._media = media;
}

get metadata() {
return this._metadata;
}

set metadata(metadata) {
this._metadata = metadata;
}

get modDate() {
return this._modDate;
}
Expand All @@ -303,6 +358,22 @@ class Doc extends PDFObject {
throw new Error("doc.mouseY is read-only");
}

get noautocomplete() {
return this._noautocomplete;
}

set noautocomplete(noautocomplete) {
this._noautocomplete = noautocomplete;
}

get nocache() {
return this._nocache;
}

set nocache(nocache) {
this._nocache = nocache;
}

get numFields() {
return this._numFields;
}
Expand Down Expand Up @@ -419,6 +490,22 @@ class Doc extends PDFObject {
throw new Error("doc.sounds is read-only");
}

get spellDictionaryOrder() {
return this._spellDictionaryOrder;
}

set spellDictionaryOrder(spellDictionaryOrder) {
this._spellDictionaryOrder = spellDictionaryOrder;
}

get spellLanguageOrder() {
return this._spellLanguageOrder;
}

set spellLanguageOrder(spellLanguageOrder) {
this._spellLanguageOrder = spellLanguageOrder;
}

get subject() {
return this._subject;
}
Expand Down
4 changes: 2 additions & 2 deletions src/scripting_api/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class EventDispatcher {

const name = baseEvent.name.replace(" ", "");
const source = this._objects[id];
const event = (this._document.obj._event = new Event(baseEvent));
globalThis.event = new Event(baseEvent);
let savedChange;

if (source.obj._isButton()) {
Expand Down Expand Up @@ -155,7 +155,7 @@ class EventDispatcher {
}
const first = this._calculationOrder[0];
const source = this._objects[first];
const event = (this._document.obj._event = new Event({}));
globalThis.event = new Event({});
this.runCalculate(source, event);
}

Expand Down
Loading

0 comments on commit 9d8fb6b

Please sign in to comment.