Bug 1948364 - part 3: Adjust BrowserGlue _monitorScreenshotsPref. r=sfoster

Differential Revision: https://phabricator.services.mozilla.com/D238315
This commit is contained in:
Luca Greco
2025-03-06 16:24:02 +00:00
parent 75ee2794b9
commit 9b62e7c434
3 changed files with 37 additions and 152 deletions

View File

@@ -2221,7 +2221,6 @@ BrowserGlue.prototype = {
_monitorScreenshotsPref() {
const SCREENSHOTS_PREF = "extensions.screenshots.disabled";
const COMPONENT_PREF = "screenshots.browser.component.enabled";
const ID = "screenshots@mozilla.org";
const _checkScreenshotsPref = async () => {
let screenshotsDisabled = Services.prefs.getBoolPref(
SCREENSHOTS_PREF,
@@ -2229,25 +2228,16 @@ BrowserGlue.prototype = {
);
let componentEnabled = Services.prefs.getBoolPref(COMPONENT_PREF, true);
let screenshotsAddon = await lazy.AddonManager.getAddonByID(ID);
if (screenshotsDisabled) {
if (componentEnabled) {
// TODO(Bug 1948366): simplify this logic further once we have migrated
// all users of the legacy `extensions.screenshots.disabled` to the new
// `screenshots.browser.component.enabled` pref (e.g. enterprise policies
// `DisableFirefoxScreenshots` setting and users that may have been directly
// using the legacy pref to disable the screenshot feature).
if (screenshotsDisabled && componentEnabled) {
lazy.ScreenshotsUtils.uninitialize();
} else if (screenshotsAddon?.isActive) {
await screenshotsAddon.disable({ allowSystemAddons: true });
}
} else if (componentEnabled) {
lazy.ScreenshotsUtils.initialize();
if (screenshotsAddon?.isActive) {
await screenshotsAddon.disable({ allowSystemAddons: true });
}
} else {
try {
await screenshotsAddon.enable({ allowSystemAddons: true });
} catch (ex) {
console.error(`Error trying to enable screenshots extension: ${ex}`);
}
lazy.ScreenshotsUtils.uninitialize();
}
};

View File

@@ -188,6 +188,12 @@ export var ScreenshotsUtils = {
if (this.initialized) {
Services.obs.removeObserver(this, "menuitem-screenshot");
this.initialized = false;
if (Cu.isInAutomation) {
Services.obs.notifyObservers(
null,
"screenshots-component-uninitialized"
);
}
}
},

View File

@@ -9,18 +9,10 @@ const { sinon } = ChromeUtils.importESModule(
ChromeUtils.defineESModuleGetters(this, {
ScreenshotsUtils: "resource:///modules/ScreenshotsUtils.sys.mjs",
AddonManager: "resource://gre/modules/AddonManager.sys.mjs",
});
ChromeUtils.defineLazyGetter(this, "ExtensionManagement", () => {
const { Management } = ChromeUtils.importESModule(
"resource://gre/modules/Extension.sys.mjs"
);
return Management;
});
const COMPONENT_PREF = "screenshots.browser.component.enabled";
const SCREENSHOTS_PREF = "extensions.screenshots.disabled";
const SCREENSHOT_EXTENSION = "screenshots@mozilla.org";
add_task(async function test_toggling_screenshots_pref() {
let observerSpy = sinon.spy();
@@ -41,40 +33,13 @@ add_task(async function test_toggling_screenshots_pref() {
ok(Services.prefs.getBoolPref(COMPONENT_PREF), "Component enabled");
ok(!Services.prefs.getBoolPref(SCREENSHOTS_PREF), "Screenshots enabled");
let addon = await AddonManager.getAddonByID(SCREENSHOT_EXTENSION);
await BrowserTestUtils.waitForCondition(
() => !addon.isActive,
"The extension is not active when the component is prefd on"
);
await BrowserTestUtils.withNewTab(
{
gBrowser,
url: SHORT_TEST_PAGE,
},
async browser => {
function extensionEventPromise(eventName, id) {
return new Promise(resolve => {
let listener = (_eventName, ...args) => {
let extension = args[0];
if (_eventName === eventName && extension.id == id) {
ExtensionManagement.off(eventName, listener);
resolve();
}
};
ExtensionManagement.on(eventName, listener);
});
}
let helper = new ScreenshotsHelper(browser);
ok(
addon.userDisabled,
"The extension is disabled when the component is prefd on"
);
ok(
!addon.isActive,
"The extension is not initially active when the component is prefd on"
);
await BrowserTestUtils.waitForCondition(
() => ScreenshotsUtils.initialized,
"The component is initialized"
@@ -108,21 +73,24 @@ add_task(async function test_toggling_screenshots_pref() {
Assert.equal(observerSpy.callCount, 3, "Observer function called thrice");
let extensionReadyPromise = extensionEventPromise(
"ready",
SCREENSHOT_EXTENSION
let componentUnitialized = TestUtils.topicObserved(
"screenshots-component-uninitialized"
);
Services.prefs.setBoolPref(COMPONENT_PREF, false);
ok(!Services.prefs.getBoolPref(COMPONENT_PREF), "Extension enabled");
info("Waiting for the extension ready event");
await extensionReadyPromise;
await BrowserTestUtils.waitForCondition(
() => !addon.userDisabled,
"The extension gets un-disabled when the component is prefd off"
ok(
!Services.prefs.getBoolPref(COMPONENT_PREF),
"Component should be disabled"
);
ok(addon.isActive, "Extension is active");
info("Wait for the Screenshot component to be uninitialized");
await componentUnitialized;
ok(
!ScreenshotsUtils.initialized,
"Screenshot component should be uninitialized"
);
info("Triggering the screenshot again should be a no-op");
helper.triggerUIFromToolbar();
Assert.equal(
observerSpy.callCount,
@@ -130,51 +98,7 @@ add_task(async function test_toggling_screenshots_pref() {
"Observer function still called thrice"
);
info("Waiting for the extensions overlay");
await SpecialPowers.spawn(
browser,
["#firefox-screenshots-preselection-iframe"],
async function (iframeSelector) {
info(
`in waitForUIContent content function, iframeSelector: ${iframeSelector}`
);
let iframe;
await ContentTaskUtils.waitForCondition(() => {
iframe = content.document.querySelector(iframeSelector);
if (!iframe || !ContentTaskUtils.isVisible(iframe)) {
info("in waitForUIContent, no visible iframe yet");
return false;
}
return true;
});
// wait a frame for the screenshots UI to finish any init
await new content.Promise(res => content.requestAnimationFrame(res));
}
);
info("Waiting for the extensions overlay");
helper.triggerUIFromToolbar();
await SpecialPowers.spawn(
browser,
["#firefox-screenshots-preselection-iframe"],
async function (iframeSelector) {
info(
`in waitForUIContent content function, iframeSelector: ${iframeSelector}`
);
let iframe;
await ContentTaskUtils.waitForCondition(() => {
iframe = content.document.querySelector(iframeSelector);
if (!iframe || !ContentTaskUtils.isVisible(iframe)) {
info("in waitForUIContent, no visible iframe yet");
return true;
}
return false;
});
// wait a frame for the screenshots UI to finish any init
await new content.Promise(res => content.requestAnimationFrame(res));
}
);
info("Triggering the screenshot from the contextmenu should be a no-op");
popupshown = BrowserTestUtils.waitForPopupEvent(menu, "shown");
EventUtils.synthesizeMouseAtCenter(document.body, {
type: "contextmenu",
@@ -192,54 +116,12 @@ add_task(async function test_toggling_screenshots_pref() {
"Observer function still called thrice"
);
await SpecialPowers.spawn(
browser,
["#firefox-screenshots-preselection-iframe"],
async function (iframeSelector) {
info(
`in waitForUIContent content function, iframeSelector: ${iframeSelector}`
);
let iframe;
await ContentTaskUtils.waitForCondition(() => {
iframe = content.document.querySelector(iframeSelector);
if (!iframe || !ContentTaskUtils.isVisible(iframe)) {
info("in waitForUIContent, no visible iframe yet");
return false;
}
return true;
});
// wait a frame for the screenshots UI to finish any init
await new content.Promise(res => content.requestAnimationFrame(res));
}
);
helper.triggerUIFromToolbar();
await SpecialPowers.spawn(
browser,
["#firefox-screenshots-preselection-iframe"],
async function (iframeSelector) {
info(
`in waitForUIContent content function, iframeSelector: ${iframeSelector}`
);
let iframe;
await ContentTaskUtils.waitForCondition(() => {
iframe = content.document.querySelector(iframeSelector);
if (!iframe || !ContentTaskUtils.isVisible(iframe)) {
return true;
}
info("in waitForUIContent, iframe still visible");
info(iframe);
return false;
});
// wait a frame for the screenshots UI to finish any init
await new content.Promise(res => content.requestAnimationFrame(res));
}
);
let componentReady = TestUtils.topicObserved(
"screenshots-component-initialized"
);
info("Re-enabling the Screenshot component should re-initialize it");
Services.prefs.setBoolPref(COMPONENT_PREF, true);
ok(Services.prefs.getBoolPref(COMPONENT_PREF), "Component enabled");
// Needed for component to initialize
@@ -260,11 +142,18 @@ add_task(async function test_toggling_screenshots_pref() {
url: SHORT_TEST_PAGE,
},
async browser => {
let componentUnitialized = TestUtils.topicObserved(
"screenshots-component-uninitialized"
);
Services.prefs.setBoolPref(SCREENSHOTS_PREF, true);
Services.prefs.setBoolPref(COMPONENT_PREF, true);
ok(Services.prefs.getBoolPref(SCREENSHOTS_PREF), "Screenshots disabled");
info("Wait for the screenshot component to be uninitialized");
await componentUnitialized;
ok(
document.getElementById("screenshot-button").disabled,
"Toolbar button disabled"