summaryrefslogtreecommitdiff
path: root/legacy/LegacyFoxUtils.sys.mjs
diff options
context:
space:
mode:
authorgirst <girst@users.noreply.github.com>2025-08-05 19:21:23 +0200
committergirst <girst@users.noreply.github.com>2025-08-05 20:25:42 +0200
commitf6ad068eefefd2f081423278d8b0bbb763a6656f (patch)
tree5e8f650097cb3bb17034c06dd77da2a8258dc0a7 /legacy/LegacyFoxUtils.sys.mjs
parentfd3ca18334ca895abef89c0386cbe4328dd88b8b (diff)
downloadlegacywolf-f6ad068eefefd2f081423278d8b0bbb763a6656f.tar.gz
legacywolf-f6ad068eefefd2f081423278d8b0bbb763a6656f.zip
make compatible with mozilla142 and above
the general idea came from onemen and 117649 in the discussion in https://github.com/xiaoxiaoflood/firefox-scripts/issues/363 but the code has been written from scratch (as I wasn't convinced of some of their design decisions). in the end, the overall approach taken looks somewhat similar, because there seems to be only one way to do that from JS code. we only rewrite a subset of specified manifest instructions, that are needed for VimFx. others are passed-through and might make other addons work. the full list of manifest instructions is documented here: https://www.devdoc.net/web/developer.mozilla.org/en-US/Chrome_Registration.html Further notes on the implementation: use of constants from (and hence import of) FileUtils has been avoided by 1) specifying unix file/dir permissions directly instead of using FileUtils.PERMS_FILE and 2) by relying on default open(2) modes in FileOutputStream, which are the one we want anyways. its documented at https://searchfox.org/mozilla-central/rev/4fd0d5e4669bfa2d0888b730684d8adea061fd30/netwerk/base/nsIFileStreams.idl#96-97 during uninstall(), it does not suffice to re-call .autoRegister() to remove the chrome.manifest. .checkForNewChrome() has precedence in mozilla-central: https://searchfox.org/mozilla-central/rev/4fd0d5e4669bfa2d0888b730684d8adea061fd30/toolkit/mozapps/extensions/AddonManagerStartup.cpp#770 the file doesn't need to exist in uninstall() at all, and only the parent directory must exist for install(), but creating it in both cases simplified the logic a bit. an empty directory will stay behind in the profile directory after uninstallation - oh, well; too bad. nsIFile.remove() takes a boolean argument; it determines recursion. https://searchfox.org/mozilla-central/rev/820596a140570007ce22a6c137ce2520676cfffe/xpcom/io/nsIFile.idl#272 AddonManagerStartup exposes a method .registerChrome(), which takes a parsed manifest instead of a file path. This one doesn't support 'skin' entries (workaround-able with overrides) and does not expose 'content' entries to the content processes - the latter kills this idea. https://searchfox.org/mozilla-central/rev/820596a140570007ce22a6c137ce2520676cfffe/toolkit/mozapps/extensions/AddonManagerStartup.cpp#782 https://searchfox.org/mozilla-central/rev/820596a140570007ce22a6c137ce2520676cfffe/browser/extensions/formautofill/api.js#124 zipReader code was cribbed from here, with Cc[].createInstance() replaced by the nicer Components.Constructor call: https://searchfox.org/mozilla-central/rev/820596a140570007ce22a6c137ce2520676cfffe/modules/libjar/zipwriter/test/unit/test_alignment.js#73 regressed-by: https://bugzilla.mozilla.org/show_bug.cgi?id=1953136
Diffstat (limited to 'legacy/LegacyFoxUtils.sys.mjs')
-rw-r--r--legacy/LegacyFoxUtils.sys.mjs85
1 files changed, 85 insertions, 0 deletions
diff --git a/legacy/LegacyFoxUtils.sys.mjs b/legacy/LegacyFoxUtils.sys.mjs
new file mode 100644
index 0000000..099f996
--- /dev/null
+++ b/legacy/LegacyFoxUtils.sys.mjs
@@ -0,0 +1,85 @@
+/**
+ * Replacements for Components.manager.addBootstrappedManifestLocation and
+ * Components.manager.removeBootstrappedManifestLocation, which have been nixed
+ * in mozilla142. Copyright 2025 Tobias Girstmair <https://gir.st/>, MPLv2.
+ */
+
+const ZipReader = Components.Constructor(
+ "@mozilla.org/libjar/zip-reader;1",
+ "nsIZipReader",
+ "open"
+);
+
+const ScriptableInputStream = Components.Constructor(
+ "@mozilla.org/scriptableinputstream;1",
+ "nsIScriptableInputStream",
+ "init"
+);
+
+const FileOutputStream = Components.Constructor(
+ "@mozilla.org/network/file-output-stream;1",
+ "nsIFileOutputStream",
+ "init"
+);
+
+export class LegacyFoxUtils {
+ static addBootstrappedManifestLocation(file, addon, uriMaker) {
+ // read chrome.manifest from .jar (or unpacked addon)
+ let zipReader = new ZipReader(file);
+ let istream = new ScriptableInputStream(zipReader.getInputStream("chrome.manifest"));
+ let manifestContents = istream.read(zipReader.getEntry("chrome.manifest").realSize);
+
+ // replace chrome:// and resource:// URIs with absolute paths to JAR
+ manifestContents = manifestContents
+ .split("\n")
+ .map(absolutizePaths.bind(null, uriMaker, file))
+ .join("\n");
+
+ // write modified chrome.manifest to profile directory
+ let manifest = constructManifestPath(addon);
+ let ostream = new FileOutputStream(manifest, -1, -1, 0);
+ ostream.write(manifestContents, manifestContents.length);
+ ostream.close();
+
+ // let Firefox read and parse it
+ Components.manager.QueryInterface(Ci.nsIComponentRegistrar).autoRegister(manifest);
+ }
+
+ static removeBootstrappedManifestLocation(addon) {
+ let manifest = constructManifestPath(addon);
+ manifest.fileSize = 0; // truncate the manifest
+ Cc['@mozilla.org/chrome/chrome-registry;1']
+ .getService(Ci.nsIXULChromeRegistry).checkForNewChrome();
+ manifest.remove(false);
+ }
+}
+
+function absolutizePaths(uriMaker, file, line) {
+ const manifestMethodPathLocation = {
+ content: 2, // content packagename uri/to/files/ [flags]
+ locale: 3, // locale packagename localename uri/to/files/ [flags]
+ skin: 3, // skin packagename skinname uri/to/files/ [flags]
+ resource: 2 // resource aliasname uri/to/files/ [flags]
+ };
+
+ let words = line.trim().split(/\s+/);
+ const index = manifestMethodPathLocation[words[0]];
+
+ if (index) {
+ words[index] = uriMaker(file, words[index]).spec;
+ line = words.join(" ");
+ }
+
+ return line;
+}
+
+function constructManifestPath(addon) {
+ let manifest = Services.dirsvc.get('ProfD', Ci.nsIFile)
+ manifest.append('legacy-extension-data');
+ manifest.append(addon.id);
+ manifest.append('chrome.manifest');
+
+ manifest.exists() || manifest.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o644);
+
+ return manifest;
+}