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
This commit is contained in:
Calixte Denizet
2025-08-29 21:34:54 +00:00
committed by rvandermeulen@mozilla.com
parent 13cf8c918d
commit 90bc3d36c3
2 changed files with 84 additions and 10 deletions

View File

@@ -7040,6 +7040,7 @@ class PDFViewer {
#eventAbortController = null; #eventAbortController = null;
#minDurationToUpdateCanvas = 0; #minDurationToUpdateCanvas = 0;
#mlManager = null; #mlManager = null;
#printingAllowed = true;
#scrollTimeoutId = null; #scrollTimeoutId = null;
#switchAnnotationEditorModeAC = null; #switchAnnotationEditorModeAC = null;
#switchAnnotationEditorModeTimeoutId = null; #switchAnnotationEditorModeTimeoutId = null;
@@ -7118,6 +7119,9 @@ class PDFViewer {
} }
}); });
} }
get printingAllowed() {
return this.#printingAllowed;
}
get pagesCount() { get pagesCount() {
return this._pages.length; return this._pages.length;
} }
@@ -7297,8 +7301,18 @@ class PDFViewer {
textLayerMode: this.#textLayerMode textLayerMode: this.#textLayerMode
}; };
if (!permissions) { if (!permissions) {
this.#printingAllowed = true;
this.eventBus.dispatch("printingallowed", {
source: this,
isAllowed: this.#printingAllowed
});
return params; 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) { if (!permissions.includes(PermissionFlag.COPY) && this.#textLayerMode === TextLayerMode.ENABLE) {
params.textLayerMode = TextLayerMode.ENABLE_PERMISSIONS; params.textLayerMode = TextLayerMode.ENABLE_PERMISSIONS;
} }
@@ -7396,6 +7410,7 @@ class PDFViewer {
this._scriptingManager?.setDocument(null); this._scriptingManager?.setDocument(null);
this.#annotationEditorUIManager?.destroy(); this.#annotationEditorUIManager?.destroy();
this.#annotationEditorUIManager = null; this.#annotationEditorUIManager = null;
this.#printingAllowed = true;
} }
this.pdfDocument = pdfDocument; this.pdfDocument = pdfDocument;
if (!pdfDocument) { if (!pdfDocument) {
@@ -8798,6 +8813,7 @@ const PDFViewerApplication = {
_caretBrowsing: null, _caretBrowsing: null,
_isScrolling: false, _isScrolling: false,
editorUndoBar: null, editorUndoBar: null,
_printPermissionPromise: null,
async initialize(appConfig) { async initialize(appConfig) {
this.appConfig = appConfig; this.appConfig = appConfig;
try { try {
@@ -9138,9 +9154,16 @@ const PDFViewerApplication = {
console.warn(msg); console.warn(msg);
}); });
} }
const togglePrintingButtons = visible => {
appConfig.toolbar?.print?.classList.toggle("hidden", !visible);
appConfig.secondaryToolbar?.printButton.classList.toggle("hidden", !visible);
};
if (!this.supportsPrinting) { if (!this.supportsPrinting) {
appConfig.toolbar?.print?.classList.add("hidden"); togglePrintingButtons(false);
appConfig.secondaryToolbar?.printButton.classList.add("hidden"); } else {
eventBus.on("printingallowed", ({
isAllowed
}) => togglePrintingButtons(isAllowed));
} }
if (!this.supportsFullscreen) { if (!this.supportsFullscreen) {
appConfig.secondaryToolbar?.presentationModeButton.classList.add("hidden"); appConfig.secondaryToolbar?.presentationModeButton.classList.add("hidden");
@@ -9445,6 +9468,20 @@ const PDFViewerApplication = {
}, },
load(pdfDocument) { load(pdfDocument) {
this.pdfDocument = 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(({ pdfDocument.getDownloadInfo().then(({
length length
}) => { }) => {
@@ -9798,7 +9835,7 @@ const PDFViewerApplication = {
if (this.printService) { if (this.printService) {
return; return;
} }
if (!this.supportsPrinting) { if (!this.supportsPrinting || !this.pdfViewer.printingAllowed) {
this._otherError("pdfjs-printing-not-supported"); this._otherError("pdfjs-printing-not-supported");
return; return;
} }
@@ -9849,8 +9886,8 @@ const PDFViewerApplication = {
requestPresentationMode() { requestPresentationMode() {
this.pdfPresentationMode?.request(); this.pdfPresentationMode?.request();
}, },
triggerPrinting() { async triggerPrinting() {
if (this.supportsPrinting) { if (this.supportsPrinting && (await this._printPermissionPromise)) {
window.print(); window.print();
} }
}, },

View File

@@ -10228,6 +10228,7 @@ class PDFViewer {
#eventAbortController = null; #eventAbortController = null;
#minDurationToUpdateCanvas = 0; #minDurationToUpdateCanvas = 0;
#mlManager = null; #mlManager = null;
#printingAllowed = true;
#scrollTimeoutId = null; #scrollTimeoutId = null;
#switchAnnotationEditorModeAC = null; #switchAnnotationEditorModeAC = null;
#switchAnnotationEditorModeTimeoutId = null; #switchAnnotationEditorModeTimeoutId = null;
@@ -10306,6 +10307,9 @@ class PDFViewer {
} }
}); });
} }
get printingAllowed() {
return this.#printingAllowed;
}
get pagesCount() { get pagesCount() {
return this._pages.length; return this._pages.length;
} }
@@ -10485,8 +10489,18 @@ class PDFViewer {
textLayerMode: this.#textLayerMode textLayerMode: this.#textLayerMode
}; };
if (!permissions) { if (!permissions) {
this.#printingAllowed = true;
this.eventBus.dispatch("printingallowed", {
source: this,
isAllowed: this.#printingAllowed,
});
return params; 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) { if (!permissions.includes(PermissionFlag.COPY) && this.#textLayerMode === TextLayerMode.ENABLE) {
params.textLayerMode = TextLayerMode.ENABLE_PERMISSIONS; params.textLayerMode = TextLayerMode.ENABLE_PERMISSIONS;
} }
@@ -10584,6 +10598,7 @@ class PDFViewer {
this._scriptingManager?.setDocument(null); this._scriptingManager?.setDocument(null);
this.#annotationEditorUIManager?.destroy(); this.#annotationEditorUIManager?.destroy();
this.#annotationEditorUIManager = null; this.#annotationEditorUIManager = null;
this.#printingAllowed = true;
} }
this.pdfDocument = pdfDocument; this.pdfDocument = pdfDocument;
if (!pdfDocument) { if (!pdfDocument) {
@@ -13406,6 +13421,7 @@ const PDFViewerApplication = {
_caretBrowsing: null, _caretBrowsing: null,
_isScrolling: false, _isScrolling: false,
editorUndoBar: null, editorUndoBar: null,
_printPermissionPromise: null,
async initialize(appConfig) { async initialize(appConfig) {
this.appConfig = appConfig; this.appConfig = appConfig;
try { try {
@@ -13745,9 +13761,16 @@ const PDFViewerApplication = {
console.warn(msg); console.warn(msg);
}); });
} }
const togglePrintingButtons = visible => {
appConfig.toolbar?.print?.classList.toggle("hidden", !visible);
appConfig.secondaryToolbar?.printButton.classList.toggle("hidden", !visible);
};
if (!this.supportsPrinting) { if (!this.supportsPrinting) {
appConfig.toolbar?.print?.classList.add("hidden"); togglePrintingButtons(false);
appConfig.secondaryToolbar?.printButton.classList.add("hidden"); } else {
eventBus.on("printingallowed", ({
isAllowed
}) => togglePrintingButtons(isAllowed));
} }
if (!this.supportsFullscreen) { if (!this.supportsFullscreen) {
appConfig.secondaryToolbar?.presentationModeButton.classList.add("hidden"); appConfig.secondaryToolbar?.presentationModeButton.classList.add("hidden");
@@ -14052,6 +14075,20 @@ const PDFViewerApplication = {
}, },
load(pdfDocument) { load(pdfDocument) {
this.pdfDocument = 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(({ pdfDocument.getDownloadInfo().then(({
length length
}) => { }) => {
@@ -14438,7 +14475,7 @@ const PDFViewerApplication = {
if (this.printService) { if (this.printService) {
return; return;
} }
if (!this.supportsPrinting) { if (!this.supportsPrinting || !this.pdfViewer.printingAllowed) {
this._otherError("pdfjs-printing-not-supported"); this._otherError("pdfjs-printing-not-supported");
return; return;
} }
@@ -14489,8 +14526,8 @@ const PDFViewerApplication = {
requestPresentationMode() { requestPresentationMode() {
this.pdfPresentationMode?.request(); this.pdfPresentationMode?.request();
}, },
triggerPrinting() { async triggerPrinting() {
if (this.supportsPrinting) { if (this.supportsPrinting && (await this._printPermissionPromise)) {
window.print(); window.print();
} }
}, },