From 90bc3d36c3d0465648bc7ce1092756b6965b24a0 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Fri, 29 Aug 2025 21:34:54 +0000 Subject: [PATCH] Bug 1978985 - Disable printing when enablePermission is true and the pdf isn't allowed to be printed a=RyanVM Differential Revision: https://phabricator.services.mozilla.com/D263103 --- .../pdfjs/content/web/viewer-geckoview.mjs | 47 +++++++++++++++++-- .../components/pdfjs/content/web/viewer.mjs | 47 +++++++++++++++++-- 2 files changed, 84 insertions(+), 10 deletions(-) diff --git a/toolkit/components/pdfjs/content/web/viewer-geckoview.mjs b/toolkit/components/pdfjs/content/web/viewer-geckoview.mjs index 981e4adc7c99..d8390aca87c6 100644 --- a/toolkit/components/pdfjs/content/web/viewer-geckoview.mjs +++ b/toolkit/components/pdfjs/content/web/viewer-geckoview.mjs @@ -7040,6 +7040,7 @@ class PDFViewer { #eventAbortController = null; #minDurationToUpdateCanvas = 0; #mlManager = null; + #printingAllowed = true; #scrollTimeoutId = null; #switchAnnotationEditorModeAC = null; #switchAnnotationEditorModeTimeoutId = null; @@ -7118,6 +7119,9 @@ class PDFViewer { } }); } + get printingAllowed() { + return this.#printingAllowed; + } get pagesCount() { return this._pages.length; } @@ -7297,8 +7301,18 @@ class PDFViewer { textLayerMode: this.#textLayerMode }; if (!permissions) { + this.#printingAllowed = true; + this.eventBus.dispatch("printingallowed", { + source: this, + isAllowed: this.#printingAllowed + }); return params; } + this.#printingAllowed = permissions.includes(PermissionFlag.PRINT_HIGH_QUALITY) || permissions.includes(PermissionFlag.PRINT); + this.eventBus.dispatch("printingallowed", { + source: this, + isAllowed: this.#printingAllowed + }); if (!permissions.includes(PermissionFlag.COPY) && this.#textLayerMode === TextLayerMode.ENABLE) { params.textLayerMode = TextLayerMode.ENABLE_PERMISSIONS; } @@ -7396,6 +7410,7 @@ class PDFViewer { this._scriptingManager?.setDocument(null); this.#annotationEditorUIManager?.destroy(); this.#annotationEditorUIManager = null; + this.#printingAllowed = true; } this.pdfDocument = pdfDocument; if (!pdfDocument) { @@ -8798,6 +8813,7 @@ const PDFViewerApplication = { _caretBrowsing: null, _isScrolling: false, editorUndoBar: null, + _printPermissionPromise: null, async initialize(appConfig) { this.appConfig = appConfig; try { @@ -9138,9 +9154,16 @@ const PDFViewerApplication = { console.warn(msg); }); } + const togglePrintingButtons = visible => { + appConfig.toolbar?.print?.classList.toggle("hidden", !visible); + appConfig.secondaryToolbar?.printButton.classList.toggle("hidden", !visible); + }; if (!this.supportsPrinting) { - appConfig.toolbar?.print?.classList.add("hidden"); - appConfig.secondaryToolbar?.printButton.classList.add("hidden"); + togglePrintingButtons(false); + } else { + eventBus.on("printingallowed", ({ + isAllowed + }) => togglePrintingButtons(isAllowed)); } if (!this.supportsFullscreen) { appConfig.secondaryToolbar?.presentationModeButton.classList.add("hidden"); @@ -9445,6 +9468,20 @@ const PDFViewerApplication = { }, load(pdfDocument) { this.pdfDocument = pdfDocument; + this._printPermissionPromise = new Promise(resolve => { + this.eventBus.on("printingallowed", ({ + isAllowed + }) => { + if (!isAllowed) { + window.print = () => { + console.warn("Printing is not allowed."); + }; + } + resolve(isAllowed); + }, { + once: true + }); + }); pdfDocument.getDownloadInfo().then(({ length }) => { @@ -9798,7 +9835,7 @@ const PDFViewerApplication = { if (this.printService) { return; } - if (!this.supportsPrinting) { + if (!this.supportsPrinting || !this.pdfViewer.printingAllowed) { this._otherError("pdfjs-printing-not-supported"); return; } @@ -9849,8 +9886,8 @@ const PDFViewerApplication = { requestPresentationMode() { this.pdfPresentationMode?.request(); }, - triggerPrinting() { - if (this.supportsPrinting) { + async triggerPrinting() { + if (this.supportsPrinting && (await this._printPermissionPromise)) { window.print(); } }, diff --git a/toolkit/components/pdfjs/content/web/viewer.mjs b/toolkit/components/pdfjs/content/web/viewer.mjs index 91805d8c3d5b..0957a41993c3 100644 --- a/toolkit/components/pdfjs/content/web/viewer.mjs +++ b/toolkit/components/pdfjs/content/web/viewer.mjs @@ -10228,6 +10228,7 @@ class PDFViewer { #eventAbortController = null; #minDurationToUpdateCanvas = 0; #mlManager = null; + #printingAllowed = true; #scrollTimeoutId = null; #switchAnnotationEditorModeAC = null; #switchAnnotationEditorModeTimeoutId = null; @@ -10306,6 +10307,9 @@ class PDFViewer { } }); } + get printingAllowed() { + return this.#printingAllowed; + } get pagesCount() { return this._pages.length; } @@ -10485,8 +10489,18 @@ class PDFViewer { textLayerMode: this.#textLayerMode }; if (!permissions) { + this.#printingAllowed = true; + this.eventBus.dispatch("printingallowed", { + source: this, + isAllowed: this.#printingAllowed, + }); return params; } + this.#printingAllowed = permissions.includes(PermissionFlag.PRINT_HIGH_QUALITY) || permissions.includes(PermissionFlag.PRINT); + this.eventBus.dispatch("printingallowed", { + source: this, + isAllowed: this.#printingAllowed + }); if (!permissions.includes(PermissionFlag.COPY) && this.#textLayerMode === TextLayerMode.ENABLE) { params.textLayerMode = TextLayerMode.ENABLE_PERMISSIONS; } @@ -10584,6 +10598,7 @@ class PDFViewer { this._scriptingManager?.setDocument(null); this.#annotationEditorUIManager?.destroy(); this.#annotationEditorUIManager = null; + this.#printingAllowed = true; } this.pdfDocument = pdfDocument; if (!pdfDocument) { @@ -13406,6 +13421,7 @@ const PDFViewerApplication = { _caretBrowsing: null, _isScrolling: false, editorUndoBar: null, + _printPermissionPromise: null, async initialize(appConfig) { this.appConfig = appConfig; try { @@ -13745,9 +13761,16 @@ const PDFViewerApplication = { console.warn(msg); }); } + const togglePrintingButtons = visible => { + appConfig.toolbar?.print?.classList.toggle("hidden", !visible); + appConfig.secondaryToolbar?.printButton.classList.toggle("hidden", !visible); + }; if (!this.supportsPrinting) { - appConfig.toolbar?.print?.classList.add("hidden"); - appConfig.secondaryToolbar?.printButton.classList.add("hidden"); + togglePrintingButtons(false); + } else { + eventBus.on("printingallowed", ({ + isAllowed + }) => togglePrintingButtons(isAllowed)); } if (!this.supportsFullscreen) { appConfig.secondaryToolbar?.presentationModeButton.classList.add("hidden"); @@ -14052,6 +14075,20 @@ const PDFViewerApplication = { }, load(pdfDocument) { this.pdfDocument = pdfDocument; + this._printPermissionPromise = new Promise(resolve => { + this.eventBus.on("printingallowed", ({ + isAllowed + }) => { + if (!isAllowed) { + window.print = () => { + console.warn("Printing is not allowed."); + }; + } + resolve(isAllowed); + }, { + once: true + }); + }); pdfDocument.getDownloadInfo().then(({ length }) => { @@ -14438,7 +14475,7 @@ const PDFViewerApplication = { if (this.printService) { return; } - if (!this.supportsPrinting) { + if (!this.supportsPrinting || !this.pdfViewer.printingAllowed) { this._otherError("pdfjs-printing-not-supported"); return; } @@ -14489,8 +14526,8 @@ const PDFViewerApplication = { requestPresentationMode() { this.pdfPresentationMode?.request(); }, - triggerPrinting() { - if (this.supportsPrinting) { + async triggerPrinting() { + if (this.supportsPrinting && (await this._printPermissionPromise)) { window.print(); } },