tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit 786d6decf42e27bda2cd35437262c014a3d9fe8c
parent d4dcd51ddaf307ab44f6df052691e01a955c3c3f
Author: Gijs Kruitbosch <gijskruitbosch@gmail.com>
Date:   Tue,  4 Nov 2025 00:27:01 +0000

Bug 1996027 - create convenient helper for moz-src conversion for new tab, r=mconley,home-newtab-reviewers

Differential Revision: https://phabricator.services.mozilla.com/D269965

Diffstat:
Abrowser/extensions/newtab/lib/ImportHelper.sys.mjs | 25+++++++++++++++++++++++++
Mbrowser/extensions/newtab/lib/TrendingSearchFeed.sys.mjs | 31++++++++++++++++---------------
Mtools/use-moz-src/mach_commands.py | 22++++++++++++++++++++++
3 files changed, 63 insertions(+), 15 deletions(-)

diff --git a/browser/extensions/newtab/lib/ImportHelper.sys.mjs b/browser/extensions/newtab/lib/ImportHelper.sys.mjs @@ -0,0 +1,25 @@ +/* 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/. */ + +export let ImportHelper = { + /** + * Helper so the new tab extension can easily trainhop while we convert + * modules from resource:// to moz-src:// URIs. + * + * @param {string} module + * The full moz-src URI to import. + * @param {string} [fallbackResourcePath ="resource://gre/modules/"] + * The resource url prefix to use for fallback import if moz-src fails. + * The helper will suffix _only_ the module filename to this path. + */ + import(module, fallbackResourcePath = "resource://gre/modules/") { + try { + return ChromeUtils.importESModule(module); + } catch { + let baseName = module.split("/").pop(); + // Fallback to a resource URI if moz-src fails. + return ChromeUtils.importESModule(fallbackResourcePath + baseName); + } + }, +}; diff --git a/browser/extensions/newtab/lib/TrendingSearchFeed.sys.mjs b/browser/extensions/newtab/lib/TrendingSearchFeed.sys.mjs @@ -2,6 +2,13 @@ * 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/. */ +import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs"; +import { + actionTypes as at, + actionCreators as ac, +} from "resource://newtab/common/Actions.mjs"; +import { ImportHelper } from "resource://newtab/lib/ImportHelper.sys.mjs"; + const lazy = {}; ChromeUtils.defineESModuleGetters(lazy, { BrowserSearchTelemetry: @@ -11,24 +18,18 @@ ChromeUtils.defineESModuleGetters(lazy, { "moz-src:///toolkit/components/search/SearchSuggestionController.sys.mjs", }); +/** + * @backward-compat { version 145 } + * + * UrlbarUtils.sys.mjs was moved to moz-src in 145. + */ ChromeUtils.defineLazyGetter(lazy, "UrlbarUtils", () => { - try { - return ChromeUtils.importESModule( - "moz-src:///browser/components/urlbar/UrlbarUtils.sys.mjs" - ).UrlbarUtils; - } catch { - // Fallback to URI format prior to FF 144. - return ChromeUtils.importESModule("resource:///modules/UrlbarUtils.sys.mjs") - .UrlbarUtils; - } + return ImportHelper.import( + "moz-src:///browser/components/urlbar/UrlbarUtils.sys.mjs", + "resource:///modules/" + ).UrlbarUtils; }); -import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs"; -import { - actionTypes as at, - actionCreators as ac, -} from "resource://newtab/common/Actions.mjs"; - const PREF_SHOW_TRENDING_SEARCH = "trendingSearch.enabled"; const PREF_SHOW_TRENDING_SEARCH_SYSTEM = "system.trendingSearch.enabled"; const PREF_TRENDING_SEARCH_DEFAULT = "trendingSearch.defaultSearchEngine"; diff --git a/tools/use-moz-src/mach_commands.py b/tools/use-moz-src/mach_commands.py @@ -43,6 +43,14 @@ and should have a comment explaining why it cannot be automatically moved to moz-src:///. """.strip() +NEWTAB_WARNING = """ +WARNING: Some files in browser/extensions/newtab/ cannot be automatically +updated for moz-src. You should manually replace the relevant imports +with the ImportHelper.import() method as needed. + +The affected files are: +""".strip() + excluded_from_convert_re = list( map( re.compile, @@ -72,6 +80,14 @@ def is_excluded_from_convert(path): return False +NEWTAB_NORM = os.path.normpath("browser/extensions/newtab/") + + +def is_path_newtab_extension(path): + """Returns true if the path is in browser/extensions/newtab/""" + return os.path.normpath(path).startswith(NEWTAB_NORM) + + def extract_info_from_mozbuild(command_context, paths): mozbuilds_for_fixing = set() urlmap = dict() @@ -273,6 +289,12 @@ def use_moz_src(command_context, paths): paths=updated_files, fix=True, ) + newtab_files = list(filter(is_path_newtab_extension, updated_files)) + if len(newtab_files) > 0: + _log.log( + logging.ERROR, + NEWTAB_WARNING + "\n {}".format("\n ".join(newtab_files)), + ) _log.log( logging.INFO, "Done. Make sure to test the result before submitting patches."