From f979615867fd70dae4875311ebca0323a29727fc Mon Sep 17 00:00:00 2001 From: Alex Kontos Date: Wed, 30 Oct 2024 11:44:35 +0000 Subject: [PATCH] Extrapolate and create AboutPages handler. --- .../browser/components/WaterfoxGlue.sys.mjs | 4 +- .../components/aboutcfg/AboutCfg.sys.mjs | 57 --------------- .../browser/components/aboutcfg/moz.build | 4 -- .../components/utils/AboutPages.sys.mjs | 70 +++++++++++++++++++ waterfox/browser/components/utils/moz.build | 1 + 5 files changed, 73 insertions(+), 63 deletions(-) delete mode 100644 waterfox/browser/components/aboutcfg/AboutCfg.sys.mjs create mode 100644 waterfox/browser/components/utils/AboutPages.sys.mjs diff --git a/waterfox/browser/components/WaterfoxGlue.sys.mjs b/waterfox/browser/components/WaterfoxGlue.sys.mjs index d6c4ad8557308..bb919cc3ea06b 100644 --- a/waterfox/browser/components/WaterfoxGlue.sys.mjs +++ b/waterfox/browser/components/WaterfoxGlue.sys.mjs @@ -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 = { this.addAddonListener(); // Register about:cfg - lazy.AboutCfg.init(); + lazy.AboutPages.init(); }, async _setPrefObservers() { diff --git a/waterfox/browser/components/aboutcfg/AboutCfg.sys.mjs b/waterfox/browser/components/aboutcfg/AboutCfg.sys.mjs deleted file mode 100644 index a29acdd5b9c53..0000000000000 --- a/waterfox/browser/components/aboutcfg/AboutCfg.sys.mjs +++ /dev/null @@ -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 - ); - } -} diff --git a/waterfox/browser/components/aboutcfg/moz.build b/waterfox/browser/components/aboutcfg/moz.build index 7d1c0486581ec..d988c0ff9b162 100644 --- a/waterfox/browser/components/aboutcfg/moz.build +++ b/waterfox/browser/components/aboutcfg/moz.build @@ -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"] diff --git a/waterfox/browser/components/utils/AboutPages.sys.mjs b/waterfox/browser/components/utils/AboutPages.sys.mjs new file mode 100644 index 0000000000000..712e6c9700162 --- /dev/null +++ b/waterfox/browser/components/utils/AboutPages.sys.mjs @@ -0,0 +1,70 @@ +/* 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 + ); + } + } +}; diff --git a/waterfox/browser/components/utils/moz.build b/waterfox/browser/components/utils/moz.build index d56ef175a7db1..148cb41f32681 100644 --- a/waterfox/browser/components/utils/moz.build +++ b/waterfox/browser/components/utils/moz.build @@ -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", ]