From 0baabf69db7c83dec424b5004348f46529467ea8 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 24 Apr 2020 11:29:33 +0200 Subject: [PATCH 1/3] Use the native `URL.createObjectURL` method more in `web/firefoxcom.js` Given that `URL.createObjectURL` is assumed to always be available in MOZCENTRAL builds, note the existing usage in the file, there's no reason to depend on the PDF.js helper function `createObjectURL` at all here. Furthermore this patch also changes `DownloadManager.downloadData` to actually revoke the `blobUrl` after downloading has completed, which is similar to the existing code in `DownloadManager.download`. --- web/firefoxcom.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/web/firefoxcom.js b/web/firefoxcom.js index 64514c9eb..e14cdcfc2 100644 --- a/web/firefoxcom.js +++ b/web/firefoxcom.js @@ -14,8 +14,8 @@ */ import "../extensions/firefox/tools/l10n.js"; -import { createObjectURL, PDFDataRangeTransport, shadow } from "pdfjs-lib"; import { DefaultExternalServices, PDFViewerApplication } from "./app.js"; +import { PDFDataRangeTransport, shadow } from "pdfjs-lib"; import { BasePreferences } from "./preferences.js"; import { DEFAULT_SCALE_VALUE } from "./ui_utils.js"; @@ -100,14 +100,23 @@ class DownloadManager { } downloadData(data, filename, contentType) { - const blobUrl = createObjectURL(data, contentType); + const blobUrl = URL.createObjectURL( + new Blob([data], { type: contentType }) + ); + const onResponse = err => { + URL.revokeObjectURL(blobUrl); + }; - FirefoxCom.request("download", { - blobUrl, - originalUrl: blobUrl, - filename, - isAttachment: true, - }); + FirefoxCom.request( + "download", + { + blobUrl, + originalUrl: blobUrl, + filename, + isAttachment: true, + }, + onResponse + ); } download(blob, url, filename) { From cd666e3a370fc666bd68369d26556b68a30fc37d Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 24 Apr 2020 11:50:58 +0200 Subject: [PATCH 2/3] Use the native `URL.createObjectURL` method in `web/pdf_attachment_viewer.js` There's no particular reason for using the PDF.js helper function `createObjectURL` here, given that the relevant code-path is already guarded by multiple "disableCreateObjectURL" option checks. --- web/pdf_attachment_viewer.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/web/pdf_attachment_viewer.js b/web/pdf_attachment_viewer.js index d3ab20216..a8c2dbefa 100644 --- a/web/pdf_attachment_viewer.js +++ b/web/pdf_attachment_viewer.js @@ -14,7 +14,6 @@ */ import { - createObjectURL, createPromiseCapability, getFilenameFromUrl, removeNullCharacters, @@ -86,7 +85,9 @@ class PDFAttachmentViewer { let blobUrl; button.onclick = function () { if (!blobUrl) { - blobUrl = createObjectURL(content, "application/pdf"); + blobUrl = URL.createObjectURL( + new Blob([content], { type: "application/pdf" }) + ); } let viewerUrl; if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { From fd9f3d7d5eb3928dea692b2ee757750335f9e53d Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 24 Apr 2020 13:17:04 +0200 Subject: [PATCH 3/3] Let `PDFAttachmentViewer._bindPdfLink` fallback to downloading the PDF file, when opening the `blobUrl` fails This is a simple work-around for https://bugzilla.mozilla.org/show_bug.cgi?id=1632644 which was caused by platform changes in Firefox. Ideally the Firefox bug should still be fixed, but these PDF.js changes seem generally useful to prevent both current and future issues here. --- web/pdf_attachment_viewer.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/web/pdf_attachment_viewer.js b/web/pdf_attachment_viewer.js index a8c2dbefa..de1a63df6 100644 --- a/web/pdf_attachment_viewer.js +++ b/web/pdf_attachment_viewer.js @@ -83,7 +83,7 @@ class PDFAttachmentViewer { ); } let blobUrl; - button.onclick = function () { + button.onclick = () => { if (!blobUrl) { blobUrl = URL.createObjectURL( new Blob([content], { type: "application/pdf" }) @@ -93,6 +93,9 @@ class PDFAttachmentViewer { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { // The current URL is the viewer, let's use it and append the file. viewerUrl = "?file=" + encodeURIComponent(blobUrl + "#" + filename); + } else if (PDFJSDev.test("MOZCENTRAL")) { + // Let Firefox's content handler catch the URL and display the PDF. + viewerUrl = blobUrl + "?" + encodeURIComponent(filename); } else if (PDFJSDev.test("CHROME")) { // In the Chrome extension, the URL is rewritten using the history API // in viewer.js, so an absolute URL must be generated. @@ -101,11 +104,17 @@ class PDFAttachmentViewer { chrome.runtime.getURL("/content/web/viewer.html") + "?file=" + encodeURIComponent(blobUrl + "#" + filename); - } else if (PDFJSDev.test("MOZCENTRAL")) { - // Let Firefox's content handler catch the URL and display the PDF. - viewerUrl = blobUrl + "?" + encodeURIComponent(filename); } - window.open(viewerUrl); + try { + window.open(viewerUrl); + } catch (ex) { + console.error(`_bindPdfLink: ${ex}`); + // Release the `blobUrl`, since opening it failed... + URL.revokeObjectURL(blobUrl); + blobUrl = null; + // ... and fallback to downloading the PDF file. + this.downloadManager.downloadData(content, filename, "application/pdf"); + } return false; }; }