[GeckoView] Allow to query pdf.js to know if we can avoid to print a pdf (bug 1846296)

This commit is contained in:
Calixte Denizet 2023-07-31 18:45:42 +02:00
parent ce9f94848c
commit 8bd4a18190
3 changed files with 46 additions and 2 deletions

View File

@ -3021,7 +3021,7 @@ class WorkerTransport {
}
getDocJSActions() {
return this.messageHandler.sendWithPromise("GetDocJSActions", null);
return this.#cacheSimpleMethod("GetDocJSActions");
}
getPageJSActions(pageIndex) {

View File

@ -208,7 +208,7 @@ class OptionalContentConfig {
}
get hasInitialVisibility() {
return this.getHash() === this.#initialHash;
return this.#initialHash === null || this.getHash() === this.#initialHash;
}
getOrder() {

View File

@ -285,6 +285,50 @@ class MozL10n {
window.addEventListener("editingaction", handleEvent);
})();
if (PDFJSDev.test("GECKOVIEW")) {
(function listenQueryEvents() {
window.addEventListener("pdf.js.query", async ({ detail: { queryId } }) => {
let result = null;
if (queryId === "canDownloadInsteadOfPrint") {
result = false;
const { pdfDocument, pdfViewer } = PDFViewerApplication;
if (pdfDocument) {
try {
const hasUnchangedAnnotations =
pdfDocument.annotationStorage.size === 0;
// WillPrint is called just before printing the document and could
// lead to have modified annotations.
const hasWillPrint =
pdfViewer.enableScripting &&
!!(await pdfDocument.getJSActions())?.WillPrint;
const hasUnchangedOptionalContent = (
await pdfViewer.optionalContentConfigPromise
).hasInitialVisibility;
result =
hasUnchangedAnnotations &&
!hasWillPrint &&
hasUnchangedOptionalContent;
} catch {
console.warn("Unable to check if the document can be downloaded.");
}
}
}
window.dispatchEvent(
new CustomEvent("pdf.js.query.answer", {
bubbles: true,
cancelable: false,
detail: {
queryId,
value: result,
},
})
);
});
})();
}
class FirefoxComDataRangeTransport extends PDFDataRangeTransport {
requestDataRange(begin, end) {
FirefoxCom.request("requestDataRange", { begin, end });