feat: create AboutPages handler

(cherry picked from commit 88fb86b62c00835e406d2f3292a6c8fd96bf7a2e)
This commit is contained in:
Alex Kontos
2025-08-05 09:17:59 +01:00
parent f00aef9770
commit d04deb376a
5 changed files with 77 additions and 63 deletions

View File

@@ -5,6 +5,7 @@
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
AboutPages: "resource:///modules/AboutPages.sys.mjs",
AddonManager: "resource://gre/modules/AddonManager.sys.mjs",
AttributionCode: "resource:///modules/AttributionCode.sys.mjs",
BrowserUtils: "resource:///modules/BrowserUtils.sys.mjs",
@@ -16,7 +17,6 @@ ChromeUtils.defineESModuleGetters(lazy, {
TabFeatures: "resource:///modules/TabFeatures.sys.mjs",
setTimeout: "resource://gre/modules/Timer.sys.mjs",
UICustomizations: "resource:///modules/UICustomizations.sys.mjs",
AboutCfg: "resource:///modules/AboutCfg.sys.mjs",
});
const WATERFOX_CUSTOMIZATIONS_PREF =
@@ -74,7 +74,7 @@ export const WaterfoxGlue = {
// Listen for addon events
this.addAddonListener();
// Register about:cfg
lazy.AboutCfg.init();
lazy.AboutPages.init();
},
async _setPrefObservers() {

View File

@@ -1,57 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
// https://github.com/earthlng/aboutconfig/blob/main/aboutcfg.jsm
const registrar = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar);
// generate a unique ID on every app launch. protection against the very unlikely possibility that a
// future update adds a component with the same class ID, which would break the script.
function generateFreeCID() {
let uuid = Components.ID(Services.uuid.generateUUID().toString());
// I can't tell whether generateUUID is guaranteed to produce a unique ID, or just a random ID.
// so I add this loop to regenerate it in the extremely unlikely (or potentially impossible)
// event that the UUID is already registered as a CID.
while (registrar.isCIDRegistered(uuid)) {
uuid = Components.ID(Services.uuid.generateUUID().toString());
}
return uuid;
}
function VintageAboutConfig() {}
VintageAboutConfig.prototype = {
get uri() {
const urlString = 'chrome://browser/content/aboutcfg/aboutcfg.xhtml';
return this._uri || (this._uri = Services.io.newURI(urlString));
},
newChannel: function (_uri, loadInfo) {
const ch = Services.io.newChannelFromURIWithLoadInfo(this.uri, loadInfo);
ch.owner = Services.scriptSecurityManager.getSystemPrincipal();
return ch;
},
getURIFlags: function (_uri) {
return Components.interfaces.nsIAboutModule.ALLOW_SCRIPT | Components.interfaces.nsIAboutModule.IS_SECURE_CHROME_UI;
},
getChromeURI: function (_uri) {
return this.uri;
},
QueryInterface: ChromeUtils.generateQI(['nsIAboutModule']),
};
export const AboutCfg = {
init() {
const AboutModuleFactory = {
createInstance(aIID) {
return new VintageAboutConfig().QueryInterface(aIID);
},
QueryInterface: ChromeUtils.generateQI(['nsIFactory']),
};
registrar.registerFactory(
generateFreeCID(),
'about:cfg',
'@mozilla.org/network/protocol/about;1?what=cfg',
AboutModuleFactory
);
}
}

View File

@@ -4,8 +4,4 @@
# 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/.
EXTRA_JS_MODULES += [
"AboutCfg.sys.mjs",
]
JAR_MANIFESTS += ["jar.mn"]

View File

@@ -0,0 +1,74 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
const registrar = Components.manager.QueryInterface(
Components.interfaces.nsIComponentRegistrar
);
function generateFreeCID() {
let uuid = Components.ID(Services.uuid.generateUUID().toString());
while (registrar.isCIDRegistered(uuid)) {
uuid = Components.ID(Services.uuid.generateUUID().toString());
}
return uuid;
}
function AboutPage(pageInfo) {
this.pageInfo = pageInfo;
}
AboutPage.prototype = {
get uri() {
if (!this._uri) {
this._uri = Services.io.newURI(this.pageInfo.chrome);
}
return this._uri;
},
newChannel: function (_uri, loadInfo) {
const ch = Services.io.newChannelFromURIWithLoadInfo(this.uri, loadInfo);
ch.owner = Services.scriptSecurityManager.getSystemPrincipal();
return ch;
},
getURIFlags: (_uri) =>
Components.interfaces.nsIAboutModule.ALLOW_SCRIPT |
Components.interfaces.nsIAboutModule.IS_SECURE_CHROME_UI,
getChromeURI: function (_uri) {
return this.uri;
},
QueryInterface: ChromeUtils.generateQI(["nsIAboutModule"]),
};
// Define the pages to register
const ABOUT_PAGES = [
{
about: "cfg",
chrome: "chrome://browser/content/aboutcfg/aboutcfg.xhtml",
contract: "@mozilla.org/network/protocol/about;1?what=cfg",
},
{
about: "passwords",
chrome: "chrome://browser/content/passwordManager.xhtml",
contract: "@mozilla.org/network/protocol/about;1?what=passwords",
},
];
export const AboutPages = {
init() {
for (const pageInfo of ABOUT_PAGES) {
const AboutModuleFactory = {
createInstance(aIID) {
return new AboutPage(pageInfo).QueryInterface(aIID);
},
QueryInterface: ChromeUtils.generateQI(["nsIFactory"]),
};
registrar.registerFactory(
generateFreeCID(),
`about:${pageInfo.about}`,
pageInfo.contract,
AboutModuleFactory
);
}
},
};

View File

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