refactor: waterfox utils

This commit is contained in:
Alex Kontos
2025-08-05 14:29:35 +01:00
parent 1bb5a07822
commit 740a64f68f
6 changed files with 61 additions and 68 deletions

View File

@@ -16,10 +16,10 @@ const lazy = {};
XPCOMUtils.defineLazyModuleGetters(lazy, { XPCOMUtils.defineLazyModuleGetters(lazy, {
AddonManager: "resource://gre/modules/AddonManager.sys.mjs", AddonManager: "resource://gre/modules/AddonManager.sys.mjs",
BrowserUtils: "resource:///modules/BrowserUtils.jsm", BrowserUtils: "resource:///modules/BrowserUtils.sys.mjs",
ChromeManifest: "resource:///modules/ChromeManifest.sys.mjs", ChromeManifest: "resource:///modules/ChromeManifest.sys.mjs",
Overlays: "resource:///modules/Overlays.sys.mjs", Overlays: "resource:///modules/Overlays.sys.mjs",
PrefUtils: "resource:///modules/PrefUtils.jsm", PrefUtils: "resource:///modules/PrefUtils.sys.mjs",
PrivateTab: "resource:///modules/PrivateTab.sys.mjs", PrivateTab: "resource:///modules/PrivateTab.sys.mjs",
StatusBar: "resource:///modules/StatusBar.sys.mjs", StatusBar: "resource:///modules/StatusBar.sys.mjs",
TabFeatures: "resource:///modules/TabFeatures.sys.mjs", TabFeatures: "resource:///modules/TabFeatures.sys.mjs",

View File

@@ -2,32 +2,20 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* global */ import { CustomizableUI } from "resource:///modules/CustomizableUI.sys.mjs";
import { PanelMultiView } from "resource:///modules/PanelMultiView.sys.mjs";
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
const EXPORTED_SYMBOLS = ["BrowserUtils"]; const lazy = {};
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { CustomizableUI } = ChromeUtils.import(
"resource:///modules/CustomizableUI.jsm"
);
const { PanelMultiView } = ChromeUtils.import(
"resource:///modules/PanelMultiView.jsm"
);
XPCOMUtils.defineLazyServiceGetter( XPCOMUtils.defineLazyServiceGetter(
this, lazy,
"styleSheetService", "styleSheetService",
"@mozilla.org/content/style-sheet-service;1", "@mozilla.org/content/style-sheet-service;1",
"nsIStyleSheetService" "nsIStyleSheetService"
); );
const BrowserUtils = { export const BrowserUtils = {
// internal functions/props // internal functions/props
get mostRecentWindow() { get mostRecentWindow() {
return Services.wm.getMostRecentWindow("navigator:browser"); return Services.wm.getMostRecentWindow("navigator:browser");
@@ -38,8 +26,8 @@ const BrowserUtils = {
}, },
createElement(aDoc, aTag, aAttrs) { createElement(aDoc, aTag, aAttrs) {
// Create element // Create element
let el = aDoc.createXULElement(aTag); const el = aDoc.createXULElement(aTag);
for (let att in aAttrs) { for (const att in aAttrs) {
// don't set null attrs // don't set null attrs
if (aAttrs[att]) { if (aAttrs[att]) {
el.setAttribute(att, aAttrs[att]); el.setAttribute(att, aAttrs[att]);
@@ -50,9 +38,9 @@ const BrowserUtils = {
// api endpoints // api endpoints
createAndPositionElement(aWindow, aTag, aAttrs, aAdjacentTo, aPosition) { createAndPositionElement(aWindow, aTag, aAttrs, aAdjacentTo, aPosition) {
let doc = aWindow.document; const doc = aWindow.document;
// Create element // Create element
let el = this.createElement(doc, aTag, aAttrs); const el = this.createElement(doc, aTag, aAttrs);
// Place it in certain location // Place it in certain location
let pos = doc.getElementById(aAdjacentTo); let pos = doc.getElementById(aAdjacentTo);
if (aPosition) { if (aPosition) {
@@ -65,7 +53,7 @@ const BrowserUtils = {
if (pos) { if (pos) {
pos.insertAdjacentElement(aPosition, el); pos.insertAdjacentElement(aPosition, el);
} }
} else if (aAdjacentTo == "gNavToolbox") { } else if (aAdjacentTo === "gNavToolbox") {
aWindow.gNavToolbox.appendChild(el); aWindow.gNavToolbox.appendChild(el);
} else { } else {
pos.appendChild(el); pos.appendChild(el);
@@ -76,21 +64,23 @@ const BrowserUtils = {
* Helper function to execute a given function with some args in every open browser window. * Helper function to execute a given function with some args in every open browser window.
* Window must be the functions first arg, subsequent args are passed in the same manner * Window must be the functions first arg, subsequent args are passed in the same manner
* as to executeInAllWindows(). * as to executeInAllWindows().
*
* @param func - The function to be called in each open browser window. * @param func - The function to be called in each open browser window.
* @param args - The arguments to supply to the function. * @param args - The arguments to supply to the function.
* Example: * Example:
* BrowserUtils.executeInAllWindows(Urlbar.addDynamicStylesheet, "chrome://browser/skin/waterfox.css") * BrowserUtils.executeInAllWindows(Urlbar.addDynamicStylesheet, "chrome://browser/skin/waterfox.css")
*/ */
executeInAllWindows(func, ...args) { executeInAllWindows(func, ...args) {
let windows = Services.wm.getEnumerator("navigator:browser"); const windows = Services.wm.getEnumerator("navigator:browser");
while (windows.hasMoreElements()) { while (windows.hasMoreElements()) {
let window = windows.getNext(); const window = windows.getNext();
func(window, ...args); func(window, ...args);
} }
}, },
/** /**
* Helper method to register or unregister a given stylesheet depending on the bool arg passed. * Helper method to register or unregister a given stylesheet depending on the bool arg passed.
*
* @param uri - The URI of the stylesheet to register or unregister. * @param uri - The URI of the stylesheet to register or unregister.
* @param enabled - A boolean indicating whether to register or unregister the sheet. * @param enabled - A boolean indicating whether to register or unregister the sheet.
*/ */
@@ -103,26 +93,36 @@ const BrowserUtils = {
}, },
registerStylesheet(uri) { registerStylesheet(uri) {
let url = Services.io.newURI(uri); if (!this.sheetRegistered(uri)) {
let type = styleSheetService.USER_SHEET; const url = Services.io.newURI(uri);
styleSheetService.loadAndRegisterSheet(url, type); const type = lazy.styleSheetService.USER_SHEET;
lazy.styleSheetService.loadAndRegisterSheet(url, type);
}
}, },
unregisterStylesheet(uri) { unregisterStylesheet(uri) {
let url = Services.io.newURI(uri); if (this.sheetRegistered(uri)) {
let type = styleSheetService.USER_SHEET; const url = Services.io.newURI(uri);
styleSheetService.unregisterSheet(url, type); const type = lazy.styleSheetService.USER_SHEET;
lazy.styleSheetService.unregisterSheet(url, type);
}
},
sheetRegistered(uri) {
const url = Services.io.newURI(uri);
const type = lazy.styleSheetService.USER_SHEET;
return lazy.styleSheetService.sheetRegistered(url, type);
}, },
setStyle(aStyleSheet) { setStyle(aStyleSheet) {
let styleSheetService = Cc[ const styleSheetService = Cc[
"@mozilla.org/content/style-sheet-service;1" "@mozilla.org/content/style-sheet-service;1"
].getService(Ci.nsIStyleSheetService); ].getService(Ci.nsIStyleSheetService);
let url = Services.io.newURI( const url = Services.io.newURI(
"data:text/css;charset=UTF-8," + encodeURIComponent(aStyleSheet) `data:text/css;charset=UTF-8,${encodeURIComponent(aStyleSheet)}`
); );
let type = styleSheetService.USER_SHEET; const type = styleSheetService.USER_SHEET;
styleSheetService.loadAndRegisterSheet(url, type); styleSheetService.loadAndRegisterSheet(url, type);
}, },

View File

@@ -2,18 +2,14 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const EXPORTED_SYMBOLS = ["PrefUtils"]; export const PrefUtils = {
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
var PrefUtils = {
get(prefPath, valueIfUndefined, def = false, setDefault = true) { get(prefPath, valueIfUndefined, def = false, setDefault = true) {
let sPrefs = def ? Services.prefs.getDefaultBranch(null) : Services.prefs; const sPrefs = def ? Services.prefs.getDefaultBranch(null) : Services.prefs;
try { try {
switch (sPrefs.getPrefType(prefPath)) { switch (sPrefs.getPrefType(prefPath)) {
case 0: case 0:
if (valueIfUndefined != undefined) { if (valueIfUndefined !== undefined) {
return this.set(prefPath, valueIfUndefined, setDefault); return this.set(prefPath, valueIfUndefined, setDefault);
} }
return undefined; return undefined;
@@ -24,12 +20,12 @@ var PrefUtils = {
case 128: case 128:
return sPrefs.getBoolPref(prefPath); return sPrefs.getBoolPref(prefPath);
} }
} catch (ex) {} } catch (_ex) {}
return undefined; return undefined;
}, },
set(prefPath, value, def = false) { set(prefPath, value, def = false) {
let sPrefs = def ? Services.prefs.getDefaultBranch(null) : Services.prefs; const sPrefs = def ? Services.prefs.getDefaultBranch(null) : Services.prefs;
switch (typeof value) { switch (typeof value) {
case "string": case "string":
@@ -43,7 +39,7 @@ var PrefUtils = {
}, },
lock(prefPath, value) { lock(prefPath, value) {
let sPrefs = Services.prefs; const sPrefs = Services.prefs;
this.lockedBackupDef[prefPath] = this.get(prefPath, true); this.lockedBackupDef[prefPath] = this.get(prefPath, true);
if (sPrefs.prefIsLocked(prefPath)) { if (sPrefs.prefIsLocked(prefPath)) {
sPrefs.unlockPref(prefPath); sPrefs.unlockPref(prefPath);
@@ -57,8 +53,8 @@ var PrefUtils = {
unlock(prefPath) { unlock(prefPath) {
Services.prefs.unlockPref(prefPath); Services.prefs.unlockPref(prefPath);
let bkp = this.lockedBackupDef[prefPath]; const bkp = this.lockedBackupDef[prefPath];
if (bkp == undefined) { if (bkp === undefined) {
Services.prefs.deleteBranch(prefPath); Services.prefs.deleteBranch(prefPath);
} else { } else {
this.set(prefPath, bkp, true); this.set(prefPath, bkp, true);
@@ -68,9 +64,8 @@ var PrefUtils = {
clear: Services.prefs.clearUserPref, clear: Services.prefs.clearUserPref,
addObserver(aPrefPath, aCallback) { addObserver(aPrefPath, aCallback) {
this.observer = function(aSubject, aTopic, prefPath) { this.observer = (_aSubject, _aTopic, prefPath) =>
return aCallback(PrefUtils.get(prefPath), prefPath); aCallback(PrefUtils.get(prefPath), prefPath);
};
Services.prefs.addObserver(aPrefPath, this.observer); Services.prefs.addObserver(aPrefPath, this.observer);
return { return {

View File

@@ -5,8 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
EXTRA_JS_MODULES += [ EXTRA_JS_MODULES += [
"BrowserUtils.jsm", "BrowserUtils.sys.mjs",
"PrefUtils.jsm", "PrefUtils.sys.mjs",
] ]
BROWSER_CHROME_MANIFESTS += [ BROWSER_CHROME_MANIFESTS += [

View File

@@ -1,22 +1,20 @@
"use strict";
// Test setting and getting different types of pref // Test setting and getting different types of pref
add_task(async function testGetSetPrefs() { add_task(async function testGetSetPrefs() {
// String pref // String pref
PrefUtils.set(STRING_PREF, "some string"); PrefUtils.set(STRING_PREF, "some string");
let strPref = PrefUtils.get(STRING_PREF); const strPref = PrefUtils.get(STRING_PREF);
is(typeof strPref, "string", "String pref is string"); is(typeof strPref, "string", "String pref is string");
is(strPref, "some string", "String pref is set"); is(strPref, "some string", "String pref is set");
// Int pref // Int pref
PrefUtils.set(INT_PREF, 999); PrefUtils.set(INT_PREF, 999);
let intPref = PrefUtils.get(INT_PREF); const intPref = PrefUtils.get(INT_PREF);
is(typeof intPref, "number", "Int pref is int"); is(typeof intPref, "number", "Int pref is int");
is(intPref, 999, "Int pref is set"); is(intPref, 999, "Int pref is set");
// Bool pref // Bool pref
PrefUtils.set(BOOL_PREF, false); PrefUtils.set(BOOL_PREF, false);
let boolPref = PrefUtils.get(BOOL_PREF); const boolPref = PrefUtils.get(BOOL_PREF);
is(typeof boolPref, "boolean", "Bool pref is bool"); is(typeof boolPref, "boolean", "Bool pref is bool");
is(boolPref, false, "Bool pref is set"); is(boolPref, false, "Bool pref is set");
@@ -28,13 +26,13 @@ add_task(async function testGetSetPrefs() {
// Test observing a pref // Test observing a pref
add_task(async function testObservePref() { add_task(async function testObservePref() {
let msg = "Callback succeeded"; const msg = "Callback succeeded";
// Set up the observer // Set up the observer
async function callback(pref, path) { async function callback(_pref, _path) {
Services.prefs.setCharPref(STRING_PREF, msg); Services.prefs.setCharPref(STRING_PREF, msg);
} }
let obs = PrefUtils.addObserver(BOOL_PREF, callback); const obs = PrefUtils.addObserver(BOOL_PREF, callback);
// Trigger the obs callback // Trigger the obs callback
is( is(

View File

@@ -1,7 +1,7 @@
"use strict"; const { PrefUtils } = ChromeUtils.importESModule(
"resource:///modules/PrefUtils.sys.mjs"
);
const { PrefUtils } = ChromeUtils.import("resource:///modules/PrefUtils.jsm"); const _STRING_PREF = "browser.test.stringPref";
const _INT_PREF = "browser.test.intPref";
const STRING_PREF = "browser.test.stringPref"; const _BOOL_PREF = "browser.test.boolPref";
const INT_PREF = "browser.test.intPref";
const BOOL_PREF = "browser.test.boolPref";