[GeckoView] Add a button to download and open the file in an external app (bug 1829367)

This commit is contained in:
Calixte Denizet 2023-05-05 15:18:01 +02:00
parent f151a39d14
commit a652dc85e4
10 changed files with 54 additions and 16 deletions

View file

@ -1012,7 +1012,7 @@ const PDFViewerApplication = {
throw new Error("PDF document not downloaded.");
},
async download() {
async download(options = {}) {
const url = this._downloadUrl,
filename = this._docFilename;
try {
@ -1021,15 +1021,15 @@ const PDFViewerApplication = {
const data = await this.pdfDocument.getData();
const blob = new Blob([data], { type: "application/pdf" });
await this.downloadManager.download(blob, url, filename);
await this.downloadManager.download(blob, url, filename, options);
} 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);
await this.downloadManager.downloadUrl(url, filename, options);
}
},
async save() {
async save(options = {}) {
if (this._saveInProgress) {
return;
}
@ -1044,12 +1044,12 @@ const PDFViewerApplication = {
const data = await this.pdfDocument.saveDocument();
const blob = new Blob([data], { type: "application/pdf" });
await this.downloadManager.download(blob, url, filename);
await this.downloadManager.download(blob, url, filename, options);
} 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();
await this.download(options);
} finally {
await this.pdfScriptingManager.dispatchDidSave();
this._saveInProgress = false;
@ -1063,14 +1063,18 @@ const PDFViewerApplication = {
}
},
downloadOrSave() {
downloadOrSave(options = {}) {
if (this.pdfDocument?.annotationStorage.size > 0) {
this.save();
this.save(options);
} else {
this.download();
this.download(options);
}
},
openInExternalApp() {
this.downloadOrSave({ openInExternalApp: true });
},
/**
* Report the error; used for errors affecting loading and/or parsing of
* the entire PDF document.
@ -1852,6 +1856,7 @@ const PDFViewerApplication = {
);
eventBus._on("print", webViewerPrint);
eventBus._on("download", webViewerDownload);
eventBus._on("openinexternalapp", webViewerOpenInExternalApp);
eventBus._on("firstpage", webViewerFirstPage);
eventBus._on("lastpage", webViewerLastPage);
eventBus._on("nextpage", webViewerNextPage);
@ -1984,6 +1989,7 @@ const PDFViewerApplication = {
eventBus._off("presentationmode", webViewerPresentationMode);
eventBus._off("print", webViewerPrint);
eventBus._off("download", webViewerDownload);
eventBus._off("openinexternalapp", webViewerOpenInExternalApp);
eventBus._off("firstpage", webViewerFirstPage);
eventBus._off("lastpage", webViewerLastPage);
eventBus._off("nextpage", webViewerNextPage);
@ -2500,6 +2506,9 @@ function webViewerPrint() {
function webViewerDownload() {
PDFViewerApplication.downloadOrSave();
}
function webViewerOpenInExternalApp() {
PDFViewerApplication.openInExternalApp();
}
function webViewerFirstPage() {
PDFViewerApplication.page = 1;
}