Skip to content

Commit

Permalink
Remove PDFViewerApplication.initPassiveLoading and directly invoke …
Browse files Browse the repository at this point in the history
…the `open`-method from the extension-specific code

This old method is essentially just adding, a small amount of, unnecessary indirection and we can easily invoke `PDFViewerApplication.open` directly from the extension-specific code instead.
  • Loading branch information
Snuffleupagus committed Mar 20, 2024
1 parent b5e00e1 commit ea1c910
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 46 deletions.
44 changes: 8 additions & 36 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,9 @@ const PDFViewerApplication = {
this._hideViewBookmark();
}
} else if (PDFJSDev.test("MOZCENTRAL || CHROME")) {
this.initPassiveLoading(file);
this.setTitleUsingUrl(file, /* downloadUrl = */ file);

this.externalServices.initPassiveLoading();
} else {
throw new Error("Not implemented: run");
}
Expand Down Expand Up @@ -815,37 +817,6 @@ const PDFViewerApplication = {
this._caretBrowsing.moveCaret(isUp, select);
},

initPassiveLoading(file) {
if (
typeof PDFJSDev === "undefined" ||
!PDFJSDev.test("MOZCENTRAL || CHROME")
) {
throw new Error("Not implemented: initPassiveLoading");
}
this.setTitleUsingUrl(file, /* downloadUrl = */ file);

this.externalServices.initPassiveLoading({
onOpenWithTransport: range => {
this.open({ range });
},
onOpenWithData: (data, contentDispositionFilename) => {
if (isPdfFile(contentDispositionFilename)) {
this._contentDispositionFilename = contentDispositionFilename;
}
this.open({ data });
},
onOpenWithURL: (url, length, originalUrl) => {
this.open({ url, length, originalUrl });
},
onError: err => {
this._documentError("pdfjs-loading-error", err);
},
onProgress: (loaded, total) => {
this.progress(loaded / total);
},
});
},

setTitleUsingUrl(url = "", downloadUrl = null) {
this.url = url;
this.baseUrl = url.split("#", 1)[0];
Expand Down Expand Up @@ -987,10 +958,11 @@ const PDFViewerApplication = {
const workerParams = AppOptions.getAll(OptionKind.WORKER);
Object.assign(GlobalWorkerOptions, workerParams);

if (
(typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) &&
args.url
) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
if (args.data && isPdfFile(args.filename)) {
this._contentDispositionFilename = args.filename;
}
} else if (args.url) {
// The Firefox built-in viewer always calls `setTitleUsingUrl`, before
// `initPassiveLoading`, and it never provides an `originalUrl` here.
this.setTitleUsingUrl(
Expand Down
5 changes: 2 additions & 3 deletions web/chromecom.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,11 @@ class Preferences extends BasePreferences {
}

class ExternalServices extends BaseExternalServices {
initPassiveLoading(callbacks) {
// defaultUrl is set in viewer.js
initPassiveLoading() {
ChromeCom.resolvePDFFile(
AppOptions.get("defaultUrl"),
function (url, length, originalUrl) {
callbacks.onOpenWithURL(url, length, originalUrl);
viewerApp.open({ url, length, originalUrl });
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion web/external_services.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BaseExternalServices {

updateFindMatchesCount(data) {}

initPassiveLoading(callbacks) {}
initPassiveLoading() {}

reportTelemetry(data) {}

Expand Down
12 changes: 6 additions & 6 deletions web/firefoxcom.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ class ExternalServices extends BaseExternalServices {
FirefoxCom.request("updateFindMatchesCount", data);
}

initPassiveLoading(callbacks) {
initPassiveLoading() {
let pdfDataRangeTransport;

window.addEventListener("message", function windowMessage(e) {
Expand All @@ -340,7 +340,7 @@ class ExternalServices extends BaseExternalServices {
switch (args.pdfjsLoadAction) {
case "supportsRangedLoading":
if (args.done && !args.data) {
callbacks.onError();
viewerApp._documentError(null);
break;
}
pdfDataRangeTransport = new FirefoxComDataRangeTransport(
Expand All @@ -350,7 +350,7 @@ class ExternalServices extends BaseExternalServices {
args.filename
);

callbacks.onOpenWithTransport(pdfDataRangeTransport);
viewerApp.open({ range: pdfDataRangeTransport });
break;
case "range":
pdfDataRangeTransport.onDataRange(args.begin, args.chunk);
Expand All @@ -369,14 +369,14 @@ class ExternalServices extends BaseExternalServices {
pdfDataRangeTransport?.onDataProgressiveDone();
break;
case "progress":
callbacks.onProgress(args.loaded, args.total);
viewerApp.progress(args.loaded / args.total);
break;
case "complete":
if (!args.data) {
callbacks.onError(args.errorCode);
viewerApp._documentError(null, { message: args.errorCode });
break;
}
callbacks.onOpenWithData(args.data, args.filename);
viewerApp.open({ data: args.data, filename: args.filename });
break;
}
});
Expand Down

0 comments on commit ea1c910

Please sign in to comment.