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 * but we want to clean up the use of the other protocols.
61 * deleteBridgeProtocolRegistryEntryHelper handles everything outside of the logic needed for
62 * this method so that the logic in maybeDeleteBridgeProtocolRegistryEntries can be unit tested
64 * We only delete the entries for the firefox and firefox-private protocols if
65 * they were set up to use this install and in the format that Firefox installed
66 * them with. If the entries are changed in any way, it is assumed that the user
67 * mucked with them manually and knows what they are doing.
69 maybeDeleteBridgeProtocolRegistryEntries(
70 deleteBridgeProtocolRegistryEntryHelper = new DeleteBridgeProtocolRegistryEntryHelperImplementation()
73 var wrk = deleteBridgeProtocolRegistryEntryHelper.openRegistryRoot();
74 const path = deleteBridgeProtocolRegistryEntryHelper.getApplicationPath();
76 const maybeDeleteRegistryKey = (protocol, protocolCommand) => {
77 const openCommandPath = protocol + "\\shell\\open\\command";
78 if (wrk.hasChild(openCommandPath)) {
79 let deleteProtocolEntry = false;
82 var openCommandKey = wrk.openChild(
86 if (openCommandKey.valueCount == 1) {
87 const defaultKeyName = "";
88 if (openCommandKey.getValueName(0) == defaultKeyName) {
90 openCommandKey.getValueType(defaultKeyName) ==
91 Ci.nsIWindowsRegKey.TYPE_STRING
93 const val = openCommandKey.readStringValue(defaultKeyName);
94 if (val == protocolCommand) {
95 deleteProtocolEntry = true;
101 openCommandKey.close();
104 if (deleteProtocolEntry) {
105 deleteBridgeProtocolRegistryEntryHelper.deleteRegistryTree(
113 maybeDeleteRegistryKey("firefox", `\"${path}\" -osint -url \"%1\"`);
114 maybeDeleteRegistryKey(
116 `\"${path}\" -osint -private-window \"%1\"`
126 * Registers the firefox-bridge and firefox-private-bridge protocols
127 * on the Windows platform.
129 maybeRegisterFirefoxBridgeProtocols() {
130 const FIREFOX_BRIDGE_HANDLER_NAME = "firefox-bridge";
131 const FIREFOX_PRIVATE_BRIDGE_HANDLER_NAME = "firefox-private-bridge";
132 const path = Services.dirsvc.get("XREExeF", Ci.nsIFile).path;
133 let wrk = Cc["@mozilla.org/windows-registry-key;1"].createInstance(
137 wrk.open(wrk.ROOT_KEY_CLASSES_ROOT, "", wrk.ACCESS_READ);
138 let FxSet = wrk.hasChild(FIREFOX_BRIDGE_HANDLER_NAME);
139 let FxPrivateSet = wrk.hasChild(FIREFOX_PRIVATE_BRIDGE_HANDLER_NAME);
141 if (FxSet && FxPrivateSet) {
144 wrk.open(wrk.ROOT_KEY_CURRENT_USER, "Software\\Classes", wrk.ACCESS_ALL);
145 const maybeUpdateRegistry = (isSetAlready, handler, protocolName) => {
149 let FxKey = wrk.createChild(handler, wrk.ACCESS_ALL);
151 // Write URL protocol key
152 FxKey.writeStringValue("", protocolName);
153 FxKey.writeStringValue("URL Protocol", "");
155 // Write defaultIcon key
157 FxKey.ROOT_KEY_CURRENT_USER,
158 "Software\\Classes\\" + handler + "\\DefaultIcon",
162 FxKey.ROOT_KEY_CURRENT_USER,
163 "Software\\Classes\\" + handler + "\\DefaultIcon",
166 FxKey.writeStringValue("", `\"${path}\",1`);
168 // Write shell\\open\\command key
170 FxKey.ROOT_KEY_CURRENT_USER,
171 "Software\\Classes\\" + handler + "\\shell",
175 FxKey.ROOT_KEY_CURRENT_USER,
176 "Software\\Classes\\" + handler + "\\shell\\open",
180 FxKey.ROOT_KEY_CURRENT_USER,
181 "Software\\Classes\\" + handler + "\\shell\\open\\command",
185 FxKey.ROOT_KEY_CURRENT_USER,
186 "Software\\Classes\\" + handler + "\\shell\\open\\command",
189 if (handler == FIREFOX_PRIVATE_BRIDGE_HANDLER_NAME) {
190 FxKey.writeStringValue(
192 `\"${path}\" -osint -private-window \"%1\"`
195 FxKey.writeStringValue("", `\"${path}\" -osint -url \"%1\"`);
207 FIREFOX_BRIDGE_HANDLER_NAME,
208 "URL:Firefox Bridge Protocol"
217 FIREFOX_PRIVATE_BRIDGE_HANDLER_NAME,
218 "URL:Firefox Private Bridge Protocol"
230 getNativeMessagingHostId() {
231 let nativeMessagingHostId = "org.mozilla.firefox_bridge_nmh";
232 if (AppConstants.NIGHTLY_BUILD) {
233 nativeMessagingHostId += "_nightly";
234 } else if (AppConstants.MOZ_DEV_EDITION) {
235 nativeMessagingHostId += "_dev";
236 } else if (AppConstants.IS_ESR) {
237 nativeMessagingHostId += "_esr";
239 return nativeMessagingHostId;
242 getExtensionOrigins() {
243 return Services.prefs
244 .getStringPref("browser.firefoxbridge.extensionOrigins", "")
248 async maybeWriteManifestFiles(
250 nativeMessagingHostId,
251 dualBrowserExtensionOrigins
254 let binFile = Services.dirsvc.get("XREExeF", Ci.nsIFile).parent;
255 if (AppConstants.platform == "win") {
256 binFile.append("nmhproxy.exe");
257 } else if (AppConstants.platform == "macosx") {
258 binFile.append("nmhproxy");
260 throw new Error("Unsupported platform");
264 name: nativeMessagingHostId,
265 description: "Firefox Native Messaging Host",
268 allowed_origins: dualBrowserExtensionOrigins,
270 let nmhManifestFile = await IOUtils.getFile(
272 `${nativeMessagingHostId}.json`
275 // This throws an error if the JSON file doesn't exist
276 // or if it's corrupt.
277 let correctFileExists = true;
279 correctFileExists = lazy.ObjectUtils.deepEqual(
280 await IOUtils.readJSON(nmhManifestFile.path),
284 correctFileExists = false;
286 if (!correctFileExists) {
287 await IOUtils.writeJSON(nmhManifestFile.path, jsonContent);
294 async ensureRegistered() {
295 let nmhManifestFolder = null;
296 if (AppConstants.platform == "win") {
297 // We don't have permission to write to the application install directory
298 // so instead write to %AppData%\Mozilla\Firefox.
299 nmhManifestFolder = PathUtils.join(
300 Services.dirsvc.get("AppData", Ci.nsIFile).path,
304 } else if (AppConstants.platform == "macosx") {
306 "~/Library/Application Support/Google/Chrome/NativeMessagingHosts/";
308 throw new Error("Unsupported platform");
310 await this.maybeWriteManifestFiles(
312 this.getNativeMessagingHostId(),
313 this.getExtensionOrigins()
315 if (AppConstants.platform == "win") {
316 this.maybeWriteNativeMessagingRegKeys(
317 "Software\\Google\\Chrome\\NativeMessagingHosts",
319 this.getNativeMessagingHostId()
324 maybeWriteNativeMessagingRegKeys(
327 NATIVE_MESSAGING_HOST_ID
329 let wrk = Cc["@mozilla.org/windows-registry-key;1"].createInstance(
333 let expectedValue = PathUtils.join(
335 `${NATIVE_MESSAGING_HOST_ID}.json`
338 // If the key already exists it will just be opened
340 wrk.ROOT_KEY_CURRENT_USER,
341 regPath + `\\${NATIVE_MESSAGING_HOST_ID}`,
344 if (wrk.readStringValue("") == expectedValue) {
348 // The key either doesn't have a value or doesn't exist
349 // In either case we need to write it.
351 wrk.writeStringValue("", expectedValue);
353 // The method fails if we can't access the key
354 // which means it doesn't exist. That's a normal situation.
355 // We don't need to do anything here.