Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the sourceEventType from the viewer (bug 1757771 follow-up) #14926

Merged
merged 2 commits into from
May 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ const PDFViewerApplication = {
) {
try {
// Trigger saving, to prevent data loss in forms; see issue 12257.
await this.save({ sourceEventType: "save" });
await this.save();
} catch (reason) {
// Ignoring errors, to ensure that document closing won't break.
}
Expand Down Expand Up @@ -964,7 +964,7 @@ const PDFViewerApplication = {
throw new Error("PDF document not downloaded.");
},

async download({ sourceEventType = "download" } = {}) {
async download() {
const url = this._downloadUrl,
filename = this._docFilename;
try {
Expand All @@ -973,15 +973,15 @@ const PDFViewerApplication = {
const data = await this.pdfDocument.getData();
const blob = new Blob([data], { type: "application/pdf" });

await this.downloadManager.download(blob, url, filename, sourceEventType);
await this.downloadManager.download(blob, url, filename);
} catch (reason) {
// When the PDF document isn't ready, or the PDF file is still
// downloading, simply download using the URL.
await this.downloadManager.downloadUrl(url, filename);
}
},

async save({ sourceEventType = "download" } = {}) {
async save() {
if (this._saveInProgress) {
return;
}
Expand All @@ -996,23 +996,23 @@ const PDFViewerApplication = {
const data = await this.pdfDocument.saveDocument();
const blob = new Blob([data], { type: "application/pdf" });

await this.downloadManager.download(blob, url, filename, sourceEventType);
await this.downloadManager.download(blob, url, filename);
} catch (reason) {
// When the PDF document isn't ready, or the PDF file is still
// downloading, simply fallback to a "regular" download.
console.error(`Error when saving the document: ${reason.message}`);
await this.download({ sourceEventType });
await this.download();
} finally {
await this.pdfScriptingManager.dispatchDidSave();
this._saveInProgress = false;
}
},

downloadOrSave(options) {
downloadOrSave() {
if (this.pdfDocument?.annotationStorage.size > 0) {
this.save(options);
this.save();
} else {
this.download(options);
this.download();
}
},

Expand Down Expand Up @@ -1875,7 +1875,6 @@ const PDFViewerApplication = {
eventBus._on("presentationmode", webViewerPresentationMode);
eventBus._on("print", webViewerPrint);
eventBus._on("download", webViewerDownload);
eventBus._on("save", webViewerSave);
eventBus._on("firstpage", webViewerFirstPage);
eventBus._on("lastpage", webViewerLastPage);
eventBus._on("nextpage", webViewerNextPage);
Expand Down Expand Up @@ -1973,7 +1972,6 @@ const PDFViewerApplication = {
eventBus._off("presentationmode", webViewerPresentationMode);
eventBus._off("print", webViewerPrint);
eventBus._off("download", webViewerDownload);
eventBus._off("save", webViewerSave);
eventBus._off("firstpage", webViewerFirstPage);
eventBus._off("lastpage", webViewerLastPage);
eventBus._off("nextpage", webViewerNextPage);
Expand Down Expand Up @@ -2330,7 +2328,7 @@ function webViewerNamedAction(evt) {
break;

case "SaveAs":
webViewerSave();
PDFViewerApplication.downloadOrSave();
break;
}
}
Expand Down Expand Up @@ -2461,10 +2459,7 @@ function webViewerPrint() {
PDFViewerApplication.triggerPrinting();
}
function webViewerDownload() {
PDFViewerApplication.downloadOrSave({ sourceEventType: "download" });
}
function webViewerSave() {
PDFViewerApplication.downloadOrSave({ sourceEventType: "save" });
PDFViewerApplication.downloadOrSave();
}
function webViewerFirstPage() {
if (PDFViewerApplication.pdfDocument) {
Expand Down
8 changes: 1 addition & 7 deletions web/download_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,7 @@ class DownloadManager {
return false;
}

/**
* @param sourceEventType {string} Used to signal what triggered the download.
* The version of PDF.js integrated with Firefox uses this to to determine
* which dialog to show. "save" triggers "save as" and "download" triggers
* the "open with" dialog.
*/
download(blob, url, filename, sourceEventType = "download") {
download(blob, url, filename) {
const blobUrl = URL.createObjectURL(blob);
download(blobUrl, filename);
}
Expand Down
18 changes: 4 additions & 14 deletions web/firefoxcom.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,11 @@ class DownloadManager {
new Blob([data], { type: contentType })
);

FirefoxCom.requestAsync("download", {
FirefoxCom.request("download", {
blobUrl,
originalUrl: blobUrl,
filename,
isAttachment: true,
}).then(error => {
URL.revokeObjectURL(blobUrl);
});
}

Expand Down Expand Up @@ -158,21 +156,13 @@ class DownloadManager {
return false;
}

download(blob, url, filename, sourceEventType = "download") {
download(blob, url, filename) {
const blobUrl = URL.createObjectURL(blob);

FirefoxCom.requestAsync("download", {
FirefoxCom.request("download", {
blobUrl,
originalUrl: url,
filename,
sourceEventType,
}).then(error => {
if (error) {
// If downloading failed in `PdfStreamConverter.jsm` it's very unlikely
// that attempting to fallback and re-download would be helpful here.
console.error("`ChromeActions.download` failed.");
}
URL.revokeObjectURL(blobUrl);
});
}
}
Expand Down Expand Up @@ -275,7 +265,7 @@ class MozL10n {
if (!PDFViewerApplication.initialized) {
return;
}
PDFViewerApplication.eventBus.dispatch(type, { source: window });
PDFViewerApplication.eventBus.dispatch("download", { source: window });
};

window.addEventListener("save", handleEvent);
Expand Down
3 changes: 1 addition & 2 deletions web/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,8 @@ class IDownloadManager {
* @param {Blob} blob
* @param {string} url
* @param {string} filename
* @param {string} [sourceEventType]
*/
download(blob, url, filename, sourceEventType = "download") {}
download(blob, url, filename) {}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion web/pdf_scripting_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ class PDFScriptingManager {
this._pdfViewer.currentScaleValue = value;
break;
case "SaveAs":
this._eventBus.dispatch("save", { source: this });
this._eventBus.dispatch("download", { source: this });
break;
case "FirstPage":
this._pdfViewer.currentPageNumber = 1;
Expand Down