From c6ed7347eefc393cfebc51cbf422571687addd2c Mon Sep 17 00:00:00 2001 From: Jonathan Kew Date: Wed, 2 Jul 2025 12:25:32 +0000 Subject: [PATCH] Bug 1959052 - Ensure JS execution is blocked during window.print() when we're using the system UI (rather than printing via the Firefox preview panel). a=pascalc The patch in bug 1898184 made reasonable sense, inasmuch as when the print.prefer_system_dialog option is set, we don't show our window-modal preview panel. However, as noted in comment 6 here, we do still need to block JS while window.print() is presenting the system dialog, otherwise the document may be changed or even closed by script behind the dialog's back. Prior to bug 1898184, the IsPreview::Yes flag caused us to block, but the issue was that without the preview panel taking care of cleaning up the cloned document, the block got cleared. It looks like we can solve this by setting the closeWindowAfterPrint flag in this case. In local testing, this seems to work for the various examples here, and does not regress bug 1898184. Original Revision: https://phabricator.services.mozilla.com/D253655 Differential Revision: https://phabricator.services.mozilla.com/D253944 --- dom/base/nsGlobalWindowOuter.cpp | 15 ++++++++++----- modules/libpref/init/StaticPrefList.yaml | 8 ++++++++ modules/libpref/init/all.js | 5 ----- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/dom/base/nsGlobalWindowOuter.cpp b/dom/base/nsGlobalWindowOuter.cpp index 42ee50b53a66..b5cf9524c493 100644 --- a/dom/base/nsGlobalWindowOuter.cpp +++ b/dom/base/nsGlobalWindowOuter.cpp @@ -4937,9 +4937,8 @@ void nsGlobalWindowOuter::PrintOuter(ErrorResult& aError) { } }); - const bool forPreview = - !StaticPrefs::print_always_print_silent() && - !Preferences::GetBool("print.prefer_system_dialog", false); + const bool forPreview = !StaticPrefs::print_always_print_silent() && + !StaticPrefs::print_prefer_system_dialog(); Print(nullptr, nullptr, nullptr, nullptr, IsPreview(forPreview), IsForWindowDotPrint::Yes, nullptr, nullptr, aError); #endif @@ -5136,8 +5135,11 @@ Nullable nsGlobalWindowOuter::Print( // The exception is if we're using the passed-in aCachedBrowsingContext, in // which case this is the second print with this static document clone that // we created the first time through, and we are responsible for cleaning it - // up. - closeWindowAfterPrint = usingCachedBrowsingContext; + // up. There's also an exception if we're directly using the system print + // dialog rather than our preview panel, because in this case the preview + // will not take care of cleaning up the cloned doc. + closeWindowAfterPrint = + usingCachedBrowsingContext || StaticPrefs::print_prefer_system_dialog(); } else { // In this case the document was not a static clone, so we made a static // clone for printing purposes and must clean it up after the print is done. @@ -5179,6 +5181,9 @@ Nullable nsGlobalWindowOuter::Print( if (aIsPreview == IsPreview::Yes) { return !hasPrintCallbacks; } + if (StaticPrefs::print_prefer_system_dialog()) { + return true; + } return StaticPrefs::dom_window_print_fuzzing_block_while_printing(); }(); diff --git a/modules/libpref/init/StaticPrefList.yaml b/modules/libpref/init/StaticPrefList.yaml index acadd3a2cca1..283167eb4d32 100644 --- a/modules/libpref/init/StaticPrefList.yaml +++ b/modules/libpref/init/StaticPrefList.yaml @@ -15589,6 +15589,14 @@ value: false mirror: always +# Whether we directly use the system print dialog to collect the user's print +# settings rather than using the tab-modal print preview dialog. +# Note: `print.always_print_silent` overrides this. +- name: print.prefer_system_dialog + type: RelaxedAtomicBool + value: false + mirror: always + # Whether we attempt to generate links in Save As PDF output. - name: print.save_as_pdf.links.enabled type: RelaxedAtomicBool diff --git a/modules/libpref/init/all.js b/modules/libpref/init/all.js index 95bcd7179309..c175e7707cf3 100644 --- a/modules/libpref/init/all.js +++ b/modules/libpref/init/all.js @@ -725,11 +725,6 @@ pref("browser.fixup.fallback-to-https", true); // used in this case. See nsPrintSettingsService::InitPrintSettingsFromPrefs // for the restrictions on which prefs can act as defaults. -// Whether we directly use the system print dialog to collect the user's print -// settings rather than using the tab-modal print preview dialog. -// Note: `print.always_print_silent` overrides this. -pref("print.prefer_system_dialog", false); - // Print/Preview Shrink-To-Fit won't shrink below 20% for text-ish documents. pref("print.shrink-to-fit.scale-limit-percent", 20);