From 567f585defce102d9dd4aaded5635f358757bf14 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Wed, 20 May 2026 18:37:26 +0200 Subject: [PATCH] Add a pref-controlled method to notify embedders that mozPrintCallback has completed (bug 2036265) Adds a `postMessageAfterPrintCallback` browser option (off by default). When enabled by Firefox, the print service posts "ready" or "error" to the window after each page's mozPrintCallback resolves, so embedders (e.g. print-preview test harnesses) can observe when rendering is done. Upstreams the viewer-side portion of Phabricator D297837. Co-Authored-By: Claude Opus 4.7 (1M context) Co-authored-by: Daniel Holbert --- web/app_options.js | 5 +++++ web/firefox_print_service.js | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/web/app_options.js b/web/app_options.js index 27f24f2c3..ffdf4eb3e 100644 --- a/web/app_options.js +++ b/web/app_options.js @@ -369,6 +369,11 @@ const defaultOptions = { value: typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING"), kind: OptionKind.VIEWER + OptionKind.PREFERENCE, }, + postMessageAfterPrintCallback: { + /** @type {boolean} */ + value: false, + kind: OptionKind.VIEWER + OptionKind.PREFERENCE, + }, printResolution: { /** @type {number} */ value: 150, diff --git a/web/firefox_print_service.js b/web/firefox_print_service.js index 04b8921e4..57e99fc7a 100644 --- a/web/firefox_print_service.js +++ b/web/firefox_print_service.js @@ -23,6 +23,7 @@ import { BasePrintServiceFactory, getXfaHtmlForPrinting, } from "./print_utils.js"; +import { AppOptions } from "./app_options.js"; // Creates a placeholder with div and canvas with right size for the page. function composePage( @@ -31,6 +32,7 @@ function composePage( size, printContainer, printResolution, + shouldPostMessageAfterPrintCallback, optionalContentConfigPromise, printAnnotationStoragePromise ) { @@ -94,6 +96,9 @@ function composePage( currentRenderTask = null; } obj.done(); + if (shouldPostMessageAfterPrintCallback) { + window.postMessage("ready", "*"); + } }, function (reason) { if (!(reason instanceof RenderingCancelledException)) { @@ -112,6 +117,9 @@ function composePage( } else { obj.done(); } + if (shouldPostMessageAfterPrintCallback) { + window.postMessage("error", "*"); + } } ); }; @@ -171,6 +179,9 @@ class FirefoxPrintService { return; } + const shouldPostMessageAfterPrintCallback = AppOptions.get( + "postMessageAfterPrintCallback" + ); for (let i = 0, ii = pagesOverview.length; i < ii; ++i) { composePage( pdfDocument, @@ -178,6 +189,7 @@ class FirefoxPrintService { pagesOverview[i], printContainer, _printResolution, + shouldPostMessageAfterPrintCallback, _optionalContentConfigPromise, _printAnnotationStoragePromise );