Merge pull request #17813 from Snuffleupagus/initPassiveLoading-simplify

Remove `PDFViewerApplication.initPassiveLoading` and directly invoke the `open`-method from the extension-specific code
This commit is contained in:
Tim van der Meij 2024-03-22 10:02:51 +01:00 committed by GitHub
commit e7203f558f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 43 additions and 71 deletions

View File

@ -685,7 +685,9 @@ const PDFViewerApplication = {
this._hideViewBookmark(); this._hideViewBookmark();
} }
} else if (PDFJSDev.test("MOZCENTRAL || CHROME")) { } else if (PDFJSDev.test("MOZCENTRAL || CHROME")) {
this.initPassiveLoading(file); this.setTitleUsingUrl(file, /* downloadUrl = */ file);
this.externalServices.initPassiveLoading();
} else { } else {
throw new Error("Not implemented: run"); throw new Error("Not implemented: run");
} }
@ -811,39 +813,6 @@ const PDFViewerApplication = {
this._caretBrowsing.moveCaret(isUp, select); 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.l10n.get("pdfjs-loading-error").then(msg => {
this._documentError(msg, err);
});
},
onProgress: (loaded, total) => {
this.progress(loaded / total);
},
});
},
setTitleUsingUrl(url = "", downloadUrl = null) { setTitleUsingUrl(url = "", downloadUrl = null) {
this.url = url; this.url = url;
this.baseUrl = url.split("#", 1)[0]; this.baseUrl = url.split("#", 1)[0];
@ -985,10 +954,11 @@ const PDFViewerApplication = {
const workerParams = AppOptions.getAll(OptionKind.WORKER); const workerParams = AppOptions.getAll(OptionKind.WORKER);
Object.assign(GlobalWorkerOptions, workerParams); Object.assign(GlobalWorkerOptions, workerParams);
if ( if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
(typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) && if (args.data && isPdfFile(args.filename)) {
args.url this._contentDispositionFilename = args.filename;
) { }
} else if (args.url) {
// The Firefox built-in viewer always calls `setTitleUsingUrl`, before // The Firefox built-in viewer always calls `setTitleUsingUrl`, before
// `initPassiveLoading`, and it never provides an `originalUrl` here. // `initPassiveLoading`, and it never provides an `originalUrl` here.
this.setTitleUsingUrl( this.setTitleUsingUrl(
@ -1046,10 +1016,11 @@ const PDFViewerApplication = {
} else if (reason instanceof UnexpectedResponseException) { } else if (reason instanceof UnexpectedResponseException) {
key = "pdfjs-unexpected-response-error"; key = "pdfjs-unexpected-response-error";
} }
return this.l10n.get(key).then(msg => { return this._documentError(key, { message: reason.message }).then(
this._documentError(msg, { message: reason?.message }); () => {
throw reason; throw reason;
}); }
);
} }
); );
}, },
@ -1130,10 +1101,13 @@ const PDFViewerApplication = {
* Report the error; used for errors affecting loading and/or parsing of * Report the error; used for errors affecting loading and/or parsing of
* the entire PDF document. * the entire PDF document.
*/ */
_documentError(message, moreInfo = null) { async _documentError(key, moreInfo = null) {
this._unblockDocumentLoadEvent(); this._unblockDocumentLoadEvent();
this._otherError(message, moreInfo); const message = await this._otherError(
key || "pdfjs-loading-error",
moreInfo
);
this.eventBus.dispatch("documenterror", { this.eventBus.dispatch("documenterror", {
source: this, source: this,
@ -1144,12 +1118,15 @@ const PDFViewerApplication = {
/** /**
* Report the error; used for errors affecting e.g. only a single page. * Report the error; used for errors affecting e.g. only a single page.
* @param {string} message - A message that is human readable. * @param {string} key - The localization key for the error.
* @param {Object} [moreInfo] - Further information about the error that is * @param {Object} [moreInfo] - Further information about the error that is
* more technical. Should have a 'message' and * more technical. Should have a 'message' and
* optionally a 'stack' property. * optionally a 'stack' property.
* @returns {string} A (localized) error message that is human readable.
*/ */
_otherError(message, moreInfo = null) { async _otherError(key, moreInfo = null) {
const message = await this.l10n.get(key);
const moreInfoText = [`PDF.js v${version || "?"} (build: ${build || "?"})`]; const moreInfoText = [`PDF.js v${version || "?"} (build: ${build || "?"})`];
if (moreInfo) { if (moreInfo) {
moreInfoText.push(`Message: ${moreInfo.message}`); moreInfoText.push(`Message: ${moreInfo.message}`);
@ -1167,6 +1144,7 @@ const PDFViewerApplication = {
} }
console.error(`${message}\n\n${moreInfoText.join("\n")}`); console.error(`${message}\n\n${moreInfoText.join("\n")}`);
return message;
}, },
progress(level) { progress(level) {
@ -1383,9 +1361,7 @@ const PDFViewerApplication = {
this._initializeAutoPrint(pdfDocument, openActionPromise); this._initializeAutoPrint(pdfDocument, openActionPromise);
}, },
reason => { reason => {
this.l10n.get("pdfjs-loading-error").then(msg => { this._documentError("pdfjs-loading-error", { message: reason.message });
this._documentError(msg, { message: reason?.message });
});
} }
); );
@ -1774,9 +1750,7 @@ const PDFViewerApplication = {
} }
if (!this.supportsPrinting) { if (!this.supportsPrinting) {
this.l10n.get("pdfjs-printing-not-supported").then(msg => { this._otherError("pdfjs-printing-not-supported");
this._otherError(msg);
});
return; return;
} }
@ -2238,8 +2212,8 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
throw new Error("file origin does not match viewer's"); throw new Error("file origin does not match viewer's");
} }
} catch (ex) { } catch (ex) {
PDFViewerApplication.l10n.get("pdfjs-loading-error").then(msg => { PDFViewerApplication._documentError("pdfjs-loading-error", {
PDFViewerApplication._documentError(msg, { message: ex?.message }); message: ex.message,
}); });
throw ex; throw ex;
} }
@ -2304,9 +2278,7 @@ function webViewerPageRendered({ pageNumber, error }) {
} }
if (error) { if (error) {
PDFViewerApplication.l10n.get("pdfjs-rendering-error").then(msg => { PDFViewerApplication._otherError("pdfjs-rendering-error", error);
PDFViewerApplication._otherError(msg, error);
});
} }
} }

View File

@ -108,10 +108,11 @@ const ChromeCom = {
// Even without this check, the file load in frames is still blocked, // Even without this check, the file load in frames is still blocked,
// but this may change in the future (https://crbug.com/550151). // but this may change in the future (https://crbug.com/550151).
if (origin && !/^file:|^chrome-extension:/.test(origin)) { if (origin && !/^file:|^chrome-extension:/.test(origin)) {
viewerApp._documentError( viewerApp._documentError(null, {
`Blocked ${origin} from loading ${file}. Refused to load ` + message:
"a local file in a non-local page for security reasons." `Blocked ${origin} from loading ${file}. Refused to load ` +
); "a local file in a non-local page for security reasons.",
});
return; return;
} }
isAllowedFileSchemeAccess(function (isAllowedAccess) { isAllowedFileSchemeAccess(function (isAllowedAccess) {
@ -416,12 +417,11 @@ class Preferences extends BasePreferences {
} }
class ExternalServices extends BaseExternalServices { class ExternalServices extends BaseExternalServices {
initPassiveLoading(callbacks) { initPassiveLoading() {
// defaultUrl is set in viewer.js
ChromeCom.resolvePDFFile( ChromeCom.resolvePDFFile(
AppOptions.get("defaultUrl"), AppOptions.get("defaultUrl"),
function (url, length, originalUrl) { function (url, length, originalUrl) {
callbacks.onOpenWithURL(url, length, originalUrl); viewerApp.open({ url, length, originalUrl });
} }
); );
} }

View File

@ -24,7 +24,7 @@ class BaseExternalServices {
updateFindMatchesCount(data) {} updateFindMatchesCount(data) {}
initPassiveLoading(callbacks) {} initPassiveLoading() {}
reportTelemetry(data) {} reportTelemetry(data) {}

View File

@ -323,7 +323,7 @@ class ExternalServices extends BaseExternalServices {
FirefoxCom.request("updateFindMatchesCount", data); FirefoxCom.request("updateFindMatchesCount", data);
} }
initPassiveLoading(callbacks) { initPassiveLoading() {
let pdfDataRangeTransport; let pdfDataRangeTransport;
window.addEventListener("message", function windowMessage(e) { window.addEventListener("message", function windowMessage(e) {
@ -340,7 +340,7 @@ class ExternalServices extends BaseExternalServices {
switch (args.pdfjsLoadAction) { switch (args.pdfjsLoadAction) {
case "supportsRangedLoading": case "supportsRangedLoading":
if (args.done && !args.data) { if (args.done && !args.data) {
callbacks.onError(); viewerApp._documentError(null);
break; break;
} }
pdfDataRangeTransport = new FirefoxComDataRangeTransport( pdfDataRangeTransport = new FirefoxComDataRangeTransport(
@ -350,7 +350,7 @@ class ExternalServices extends BaseExternalServices {
args.filename args.filename
); );
callbacks.onOpenWithTransport(pdfDataRangeTransport); viewerApp.open({ range: pdfDataRangeTransport });
break; break;
case "range": case "range":
pdfDataRangeTransport.onDataRange(args.begin, args.chunk); pdfDataRangeTransport.onDataRange(args.begin, args.chunk);
@ -369,14 +369,14 @@ class ExternalServices extends BaseExternalServices {
pdfDataRangeTransport?.onDataProgressiveDone(); pdfDataRangeTransport?.onDataProgressiveDone();
break; break;
case "progress": case "progress":
callbacks.onProgress(args.loaded, args.total); viewerApp.progress(args.loaded / args.total);
break; break;
case "complete": case "complete":
if (!args.data) { if (!args.data) {
callbacks.onError(args.errorCode); viewerApp._documentError(null, { message: args.errorCode });
break; break;
} }
callbacks.onOpenWithData(args.data, args.filename); viewerApp.open({ data: args.data, filename: args.filename });
break; break;
} }
}); });