Change the PDFViewerApplication.open method to only accept objects

If this method was added today, I really can't imagine that we'd support anything *except* objects. Unfortunately we cannot just remove this now, since the code has existed since "forever", however we can deprecate this and limit it to only the GENERIC build.

Furthermore, we can avoid a redundant `PDFViewerApplication.setTitleUsingUrl` call in the Firefox PDF Viewer since the title has already been set previously in that case.
This commit is contained in:
Jonas Jenwald 2023-01-28 13:03:03 +01:00
parent e539d2da1e
commit 6129be7b68

View File

@ -741,19 +741,16 @@ const PDFViewerApplication = {
} }
this.externalServices.initPassiveLoading({ this.externalServices.initPassiveLoading({
onOpenWithTransport: (url, length, transport) => { onOpenWithTransport: (url, length, transport) => {
this.open(url, { length, range: transport }); this.open({ url, length, range: transport });
}, },
onOpenWithData: (data, contentDispositionFilename) => { onOpenWithData: (data, contentDispositionFilename) => {
if (isPdfFile(contentDispositionFilename)) { if (isPdfFile(contentDispositionFilename)) {
this._contentDispositionFilename = contentDispositionFilename; this._contentDispositionFilename = contentDispositionFilename;
} }
this.open(data); this.open({ data });
}, },
onOpenWithURL: (url, length, originalUrl) => { onOpenWithURL: (url, length, originalUrl) => {
const file = originalUrl !== undefined ? { url, originalUrl } : url; this.open({ url, length, originalUrl });
const args = length !== undefined ? { length } : null;
this.open(file, args);
}, },
onError: err => { onError: err => {
this.l10n.get("loading_error").then(msg => { this.l10n.get("loading_error").then(msg => {
@ -890,15 +887,28 @@ const PDFViewerApplication = {
}, },
/** /**
* Opens PDF document specified by URL or array with additional arguments. * Opens a new PDF document.
* @param {string|TypedArray|ArrayBuffer} file - PDF location or binary data. * @param {Object} args - Accepts any/all of the properties from
* @param {Object} [args] - Additional arguments for the getDocument call, * {@link DocumentInitParameters}, and also a `originalUrl` string.
* e.g. HTTP headers ('httpHeaders') or alternative * @returns {Promise} - Promise that is resolved when the document is opened.
* data transport ('range').
* @returns {Promise} - Returns the promise, which is resolved when document
* is opened.
*/ */
async open(file, args) { async open(args) {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
let deprecatedArgs = false;
if (typeof args === "string") {
args = { url: args }; // URL
deprecatedArgs = true;
} else if (args?.byteLength) {
args = { data: args }; // ArrayBuffer
deprecatedArgs = true;
}
if (deprecatedArgs) {
console.error(
"The `PDFViewerApplication.open` signature was updated, please use an object instead."
);
}
}
if (this.pdfLoadingTask) { if (this.pdfLoadingTask) {
// We need to destroy already opened document. // We need to destroy already opened document.
await this.close(); await this.close();
@ -910,36 +920,36 @@ const PDFViewerApplication = {
} }
const parameters = Object.create(null); const parameters = Object.create(null);
if (typeof file === "string") { if (
// URL (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) &&
this.setTitleUsingUrl(file, /* downloadUrl = */ file); args.url
parameters.url = file; ) {
} else if (file && "byteLength" in file) { // The Firefox built-in viewer always calls `setTitleUsingUrl`, before
// ArrayBuffer // `initPassiveLoading`, and it never provides an `originalUrl` here.
parameters.data = file; if (args.originalUrl) {
} else if (file.url && file.originalUrl) { this.setTitleUsingUrl(args.originalUrl, /* downloadUrl = */ args.url);
this.setTitleUsingUrl(file.originalUrl, /* downloadUrl = */ file.url); delete args.originalUrl;
parameters.url = file.url; } else {
this.setTitleUsingUrl(args.url, /* downloadUrl = */ args.url);
}
} }
// Set the necessary API parameters, using the available options. // Set the necessary API parameters, using the available options.
const apiParameters = AppOptions.getAll(OptionKind.API); const apiParameters = AppOptions.getAll(OptionKind.API);
for (const key in apiParameters) { for (const key in apiParameters) {
let value = apiParameters[key]; let value = apiParameters[key];
if (key === "docBaseUrl" && !value) { if (key === "docBaseUrl") {
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) { if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
value = document.URL.split("#")[0]; value ||= document.URL.split("#")[0];
} else if (PDFJSDev.test("MOZCENTRAL || CHROME")) { } else if (PDFJSDev.test("MOZCENTRAL || CHROME")) {
value = this.baseUrl; value ||= this.baseUrl;
} }
} }
parameters[key] = value; parameters[key] = value;
} }
// Finally, update the API parameters with the arguments (if they exist). // Finally, update the API parameters with the arguments.
if (args) { for (const key in args) {
for (const key in args) { parameters[key] = args[key];
parameters[key] = args[key];
}
} }
const loadingTask = getDocument(parameters); const loadingTask = getDocument(parameters);
@ -2242,7 +2252,7 @@ function webViewerInitialized() {
try { try {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
if (file) { if (file) {
PDFViewerApplication.open(file); PDFViewerApplication.open({ url: file });
} else { } else {
PDFViewerApplication._hideViewBookmark(); PDFViewerApplication._hideViewBookmark();
} }
@ -2452,11 +2462,10 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
} }
const file = evt.fileInput.files[0]; const file = evt.fileInput.files[0];
let url = URL.createObjectURL(file); PDFViewerApplication.open({
if (file.name) { url: URL.createObjectURL(file),
url = { url, originalUrl: file.name }; originalUrl: file.name,
} });
PDFViewerApplication.open(url);
}; };
// eslint-disable-next-line no-var // eslint-disable-next-line no-var