Bug 1854298 - Implement PrintingEnabled policy r=emilio,mkaply,Gijs,fluent-reviewers

Differential Revision: https://phabricator.services.mozilla.com/D188792
This commit is contained in:
Gregory Pappas 2023-10-03 21:48:34 +00:00
Родитель eb088cca88
Коммит 001b34b01c
8 изменённых файлов: 93 добавлений и 0 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -1877,6 +1877,12 @@ export var Policies = {
},
},
PrintingEnabled: {
onBeforeUIStartup(manager, param) {
setAndLockPref("print.enabled", param);
},
},
PromptForDownloadLocation: {
onBeforeAddons(manager, param) {
setAndLockPref("browser.download.useDownloadDir", !param);

Просмотреть файл

@ -1145,6 +1145,10 @@
"type": "boolean"
},
"PrintingEnabled": {
"type": "boolean"
},
"PromptForDownloadLocation": {
"type": "boolean"
},

Просмотреть файл

@ -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.

Просмотреть файл

@ -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.

Просмотреть файл

@ -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."
#---------------------------------------------------------------------------

Просмотреть файл

@ -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"]

Просмотреть файл

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