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;
#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();
}
},

View File

@@ -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();
}
},