1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
8 ChromeUtils.defineESModuleGetters(lazy, {
9 ObjectUtils: "resource://gre/modules/ObjectUtils.sys.mjs",
13 * Default implementation of the helper class to assist in deleting the firefox protocols.
14 * See maybeDeleteBridgeProtocolRegistryEntries for more info.
16 class DeleteBridgeProtocolRegistryEntryHelperImplementation {
17 getApplicationPath() {
18 return Services.dirsvc.get("XREExeF", Ci.nsIFile).path;
22 const wrk = Cc["@mozilla.org/windows-registry-key;1"].createInstance(
26 wrk.open(wrk.ROOT_KEY_CURRENT_USER, "Software\\Classes", wrk.ACCESS_ALL);
31 deleteChildren(start) {
32 // Recursively delete all of the children of the children
33 // Go through the list in reverse order, so that shrinking
34 // the list doesn't rearrange things while iterating
35 for (let i = start.childCount; i > 0; i--) {
36 const childName = start.getChildName(i - 1);
37 const child = start.openChild(childName, start.ACCESS_ALL);
38 this.deleteChildren(child);
41 start.removeChild(childName);
45 deleteRegistryTree(root, toDeletePath) {
46 var start = root.openChild(toDeletePath, root.ACCESS_ALL);
47 this.deleteChildren(start);
50 root.removeChild(toDeletePath);
54 export const FirefoxBridgeExtensionUtils = {
56 * In Firefox 122, we enabled the firefox and firefox-private protocols.
57 * We switched over to using firefox-bridge and firefox-private-bridge,
59 * In Firefox 126, we deleted the above firefox-bridge and
60 * firefox-private-bridge protocols in favor of using native
61 * messaging so we are only keeping the deletion code.
63 * but we want to clean up the use of the other protocols.
65 * deleteBridgeProtocolRegistryEntryHelper handles everything outside of the logic needed for
66 * this method so that the logic in maybeDeleteBridgeProtocolRegistryEntries can be unit tested
68 * We only delete the entries for the firefox and firefox-private protocols if
69 * they were set up to use this install and in the format that Firefox installed
70 * them with. If the entries are changed in any way, it is assumed that the user
71 * mucked with them manually and knows what they are doing.
74 PUBLIC_PROTOCOL: "firefox-bridge",
75 PRIVATE_PROTOCOL: "firefox-private-bridge",
76 OLD_PUBLIC_PROTOCOL: "firefox",
77 OLD_PRIVATE_PROTOCOL: "firefox-private",
79 maybeDeleteBridgeProtocolRegistryEntries(
80 publicProtocol = this.PUBLIC_PROTOCOL,
81 privateProtocol = this.PRIVATE_PROTOCOL,
82 deleteBridgeProtocolRegistryEntryHelper = new DeleteBridgeProtocolRegistryEntryHelperImplementation()
85 var wrk = deleteBridgeProtocolRegistryEntryHelper.openRegistryRoot();
86 const path = deleteBridgeProtocolRegistryEntryHelper.getApplicationPath();
88 const maybeDeleteRegistryKey = (protocol, protocolCommand) => {
89 const openCommandPath = protocol + "\\shell\\open\\command";
90 if (wrk.hasChild(openCommandPath)) {
91 let deleteProtocolEntry = false;
94 var openCommandKey = wrk.openChild(
98 if (openCommandKey.valueCount == 1) {
99 const defaultKeyName = "";
100 if (openCommandKey.getValueName(0) == defaultKeyName) {
102 openCommandKey.getValueType(defaultKeyName) ==
103 Ci.nsIWindowsRegKey.TYPE_STRING
105 const val = openCommandKey.readStringValue(defaultKeyName);
106 if (val == protocolCommand) {
107 deleteProtocolEntry = true;
113 openCommandKey.close();
116 if (deleteProtocolEntry) {
117 deleteBridgeProtocolRegistryEntryHelper.deleteRegistryTree(
125 maybeDeleteRegistryKey(publicProtocol, `\"${path}\" -osint -url \"%1\"`);
126 maybeDeleteRegistryKey(
128 `\"${path}\" -osint -private-window \"%1\"`
137 getNativeMessagingHostId() {
138 let nativeMessagingHostId = "org.mozilla.firefox_bridge_nmh";
139 if (AppConstants.NIGHTLY_BUILD) {
140 nativeMessagingHostId += "_nightly";
141 } else if (AppConstants.MOZ_DEV_EDITION) {
142 nativeMessagingHostId += "_dev";
143 } else if (AppConstants.IS_ESR) {
144 nativeMessagingHostId += "_esr";
146 return nativeMessagingHostId;
149 getExtensionOrigins() {
150 return Services.prefs
151 .getStringPref("browser.firefoxbridge.extensionOrigins", "")
155 async maybeWriteManifestFiles(
157 nativeMessagingHostId,
158 dualBrowserExtensionOrigins
161 let binFile = Services.dirsvc.get("XREExeF", Ci.nsIFile).parent;
162 if (AppConstants.platform == "win") {
163 binFile.append("nmhproxy.exe");
164 } else if (AppConstants.platform == "macosx") {
165 binFile.append("nmhproxy");
167 throw new Error("Unsupported platform");
171 name: nativeMessagingHostId,
172 description: "Firefox Native Messaging Host",
175 allowed_origins: dualBrowserExtensionOrigins,
177 let nmhManifestFile = await IOUtils.getFile(
179 `${nativeMessagingHostId}.json`
182 // This throws an error if the JSON file doesn't exist
183 // or if it's corrupt.
184 let correctFileExists = true;
186 correctFileExists = lazy.ObjectUtils.deepEqual(
187 await IOUtils.readJSON(nmhManifestFile.path),
191 correctFileExists = false;
193 if (!correctFileExists) {
194 await IOUtils.writeJSON(nmhManifestFile.path, jsonContent);
201 async ensureRegistered() {
202 let nmhManifestFolder = null;
203 if (AppConstants.platform == "win") {
204 // We don't have permission to write to the application install directory
205 // so instead write to %AppData%\Mozilla\Firefox.
206 nmhManifestFolder = PathUtils.join(
207 Services.dirsvc.get("AppData", Ci.nsIFile).path,
211 } else if (AppConstants.platform == "macosx") {
213 "~/Library/Application Support/Google/Chrome/NativeMessagingHosts/";
215 throw new Error("Unsupported platform");
217 await this.maybeWriteManifestFiles(
219 this.getNativeMessagingHostId(),
220 this.getExtensionOrigins()
222 if (AppConstants.platform == "win") {
223 this.maybeWriteNativeMessagingRegKeys(
224 "Software\\Google\\Chrome\\NativeMessagingHosts",
226 this.getNativeMessagingHostId()
231 maybeWriteNativeMessagingRegKeys(
234 NATIVE_MESSAGING_HOST_ID
236 let wrk = Cc["@mozilla.org/windows-registry-key;1"].createInstance(
240 let expectedValue = PathUtils.join(
242 `${NATIVE_MESSAGING_HOST_ID}.json`
245 // If the key already exists it will just be opened
247 wrk.ROOT_KEY_CURRENT_USER,
248 regPath + `\\${NATIVE_MESSAGING_HOST_ID}`,
251 if (wrk.readStringValue("") == expectedValue) {
255 // The key either doesn't have a value or doesn't exist
256 // In either case we need to write it.
258 wrk.writeStringValue("", expectedValue);
260 // The method fails if we can't access the key
261 // which means it doesn't exist. That's a normal situation.
262 // We don't need to do anything here.