From 001b34b01c939e2010a5d98ccd60b76850881936 Mon Sep 17 00:00:00 2001 From: Gregory Pappas Date: Tue, 3 Oct 2023 21:48:34 +0000 Subject: [PATCH] Bug 1854298 - Implement PrintingEnabled policy r=emilio,mkaply,Gijs,fluent-reviewers Differential Revision: https://phabricator.services.mozilla.com/D188792 --- browser/base/content/browser.js | 25 +++++++++++ .../enterprisepolicies/Policies.sys.mjs | 6 +++ .../schemas/policies-schema.json | 4 ++ .../policies/policies-descriptions.ftl | 2 + dom/base/nsGlobalWindowOuter.cpp | 5 +++ modules/libpref/init/StaticPrefList.yaml | 6 +++ .../components/printing/tests/browser.toml | 2 + .../printing/tests/browser_print_disabled.js | 43 +++++++++++++++++++ 8 files changed, 93 insertions(+) create mode 100644 toolkit/components/printing/tests/browser_print_disabled.js diff --git a/browser/base/content/browser.js b/browser/base/content/browser.js index 01bcd17750a6..42599f30007a 100644 --- a/browser/base/content/browser.js +++ b/browser/base/content/browser.js @@ -586,6 +586,16 @@ XPCOMUtils.defineLazyPreferenceGetter( } ); +XPCOMUtils.defineLazyPreferenceGetter( + this, + "gPrintEnabled", + "print.enabled", + false, + (aPref, aOldVal, aNewVal) => { + updatePrintCommands(aNewVal); + } +); + XPCOMUtils.defineLazyPreferenceGetter( this, "gScreenshotsComponentEnabled", @@ -847,6 +857,19 @@ function UpdateBackForwardCommands(aWebNavigation) { } } +function updatePrintCommands(enabled) { + var printCommand = document.getElementById("cmd_print"); + var printPreviewCommand = document.getElementById("cmd_printPreviewToggle"); + + if (enabled) { + printCommand.removeAttribute("disabled"); + printPreviewCommand.removeAttribute("disabled"); + } else { + printCommand.setAttribute("disabled", "true"); + printPreviewCommand.setAttribute("disabled", "true"); + } +} + /** * Click-and-Hold implementation for the Back and Forward buttons * XXXmano: should this live in toolbarbutton.js? @@ -1643,6 +1666,8 @@ var gBrowserInit = { updateFxaToolbarMenu(gFxaToolbarEnabled, true); + updatePrintCommands(gPrintEnabled); + gUnifiedExtensions.init(); // Setting the focus will cause a style flush, it's preferable to call anything diff --git a/browser/components/enterprisepolicies/Policies.sys.mjs b/browser/components/enterprisepolicies/Policies.sys.mjs index bad3c1f6963d..9f8139cb742b 100644 --- a/browser/components/enterprisepolicies/Policies.sys.mjs +++ b/browser/components/enterprisepolicies/Policies.sys.mjs @@ -1877,6 +1877,12 @@ export var Policies = { }, }, + PrintingEnabled: { + onBeforeUIStartup(manager, param) { + setAndLockPref("print.enabled", param); + }, + }, + PromptForDownloadLocation: { onBeforeAddons(manager, param) { setAndLockPref("browser.download.useDownloadDir", !param); diff --git a/browser/components/enterprisepolicies/schemas/policies-schema.json b/browser/components/enterprisepolicies/schemas/policies-schema.json index e5ddcbf73c79..f160343d0c84 100644 --- a/browser/components/enterprisepolicies/schemas/policies-schema.json +++ b/browser/components/enterprisepolicies/schemas/policies-schema.json @@ -1145,6 +1145,10 @@ "type": "boolean" }, + "PrintingEnabled": { + "type": "boolean" + }, + "PromptForDownloadLocation": { "type": "boolean" }, diff --git a/browser/locales/en-US/browser/policies/policies-descriptions.ftl b/browser/locales/en-US/browser/policies/policies-descriptions.ftl index 0f33f3cbf236..ac20df15fcc8 100644 --- a/browser/locales/en-US/browser/policies/policies-descriptions.ftl +++ b/browser/locales/en-US/browser/policies/policies-descriptions.ftl @@ -155,6 +155,8 @@ policy-ManualAppUpdateOnly = Allow manual updates only and do not notify the use policy-PrimaryPassword = Require or prevent using a Primary Password. +policy-PrintingEnabled = Enable or disable printing. + policy-NetworkPrediction = Enable or disable network prediction (DNS prefetching). policy-NewTabPage = Enable or disable the New Tab page. diff --git a/dom/base/nsGlobalWindowOuter.cpp b/dom/base/nsGlobalWindowOuter.cpp index 0da454ad536a..cdace15eb881 100644 --- a/dom/base/nsGlobalWindowOuter.cpp +++ b/dom/base/nsGlobalWindowOuter.cpp @@ -4960,6 +4960,11 @@ void nsGlobalWindowOuter::PrintOuter(ErrorResult& aError) { return; } + // Printing is disabled, silently return. + if (!StaticPrefs::print_enabled()) { + return; + } + // If we're loading, queue the print for later. This is a special-case that // only applies to the window.print() call, for compat with other engines and // pre-existing behavior. diff --git a/modules/libpref/init/StaticPrefList.yaml b/modules/libpref/init/StaticPrefList.yaml index 9845516bcb6e..835df610bc13 100644 --- a/modules/libpref/init/StaticPrefList.yaml +++ b/modules/libpref/init/StaticPrefList.yaml @@ -12813,6 +12813,12 @@ value: true mirror: always +# Disabling this will no-op window.print() +- name: print.enabled + type: RelaxedAtomicBool + value: true + mirror: always + #--------------------------------------------------------------------------- # Prefs starting with "privacy." #--------------------------------------------------------------------------- diff --git a/toolkit/components/printing/tests/browser.toml b/toolkit/components/printing/tests/browser.toml index 5b2f07b5bf24..683caba9a231 100644 --- a/toolkit/components/printing/tests/browser.toml +++ b/toolkit/components/printing/tests/browser.toml @@ -46,6 +46,8 @@ support-files = ["file_link_modulepreload.html"] ["browser_print_context_menu.js"] +["browser_print_disabled.js"] + ["browser_print_copies.js"] ["browser_print_duplex.js"] diff --git a/toolkit/components/printing/tests/browser_print_disabled.js b/toolkit/components/printing/tests/browser_print_disabled.js new file mode 100644 index 000000000000..87315d5952e3 --- /dev/null +++ b/toolkit/components/printing/tests/browser_print_disabled.js @@ -0,0 +1,43 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +add_setup(async function setup() { + await SpecialPowers.pushPrefEnv({ + set: [["print.enabled", false]], + }); +}); + +add_task(async function test_print_commands_disabled() { + let printCommand = document.getElementById(`cmd_print`); + let printPreview = document.getElementById(`cmd_printPreviewToggle`); + + is( + printCommand.getAttribute("disabled"), + "true", + "print command is disabled when print.enabled=false" + ); + is( + printPreview.getAttribute("disabled"), + "true", + "print preview command is disabled when print.enabled=false" + ); +}); + +add_task(async function test_window_print_disabled() { + await BrowserTestUtils.withNewTab("https://example.org/", async browser => { + await SpecialPowers.spawn(browser, [], function () { + content.window.print(); + }); + + // When the print dialog is open, the main thread of our tab is blocked, so + // the failure mode of this test is a timeout caused by the print dialog never + // closing. + + Assert.ok( + true, + "window.print doesn't do anything when print.enabled=false" + ); + }); +});