Skip to content

Commit 502b94e

Browse files
committed
Bug 1876342 - Added Windows native messaging host registration for Chromium r=mhughes,robwu,nalexander
Differential Revision: https://phabricator.services.mozilla.com/D199587
1 parent 5896a31 commit 502b94e

File tree

4 files changed

+76
-4
lines changed

4 files changed

+76
-4
lines changed

browser/components/BrowserGlue.sys.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2657,7 +2657,8 @@ BrowserGlue.prototype = {
26572657
{
26582658
name: "firefoxBridgeNativeMessaging",
26592659
condition:
2660-
AppConstants.platform == "macosx" &&
2660+
(AppConstants.platform == "macosx" ||
2661+
AppConstants.platform == "win") &&
26612662
Services.prefs.getBoolPref("browser.firefoxbridge.enabled", false),
26622663
task: async () => {
26632664
await lazy.FirefoxBridgeExtensionUtils.ensureRegistered();

browser/modules/FirefoxBridgeExtensionUtils.sys.mjs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,9 @@ export const FirefoxBridgeExtensionUtils = {
252252
) {
253253
try {
254254
let binFile = Services.dirsvc.get("XREExeF", Ci.nsIFile).parent;
255-
if (AppConstants.platform == "macosx") {
255+
if (AppConstants.platform == "win") {
256+
binFile.append("nmhproxy.exe");
257+
} else if (AppConstants.platform == "macosx") {
256258
binFile.append("nmhproxy");
257259
} else {
258260
throw new Error("Unsupported platform");
@@ -291,7 +293,15 @@ export const FirefoxBridgeExtensionUtils = {
291293

292294
async ensureRegistered() {
293295
let nmhManifestFolder = null;
294-
if (AppConstants.platform == "macosx") {
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,
301+
"Mozilla",
302+
"Firefox"
303+
);
304+
} else if (AppConstants.platform == "macosx") {
295305
nmhManifestFolder =
296306
"~/Library/Application Support/Google/Chrome/NativeMessagingHosts/";
297307
} else {
@@ -302,5 +312,49 @@ export const FirefoxBridgeExtensionUtils = {
302312
this.getNativeMessagingHostId(),
303313
this.getExtensionOrigins()
304314
);
315+
if (AppConstants.platform == "win") {
316+
this.maybeWriteNativeMessagingRegKeys(
317+
"Software\\Google\\Chrome\\NativeMessagingHosts",
318+
nmhManifestFolder,
319+
this.getNativeMessagingHostId()
320+
);
321+
}
322+
},
323+
324+
maybeWriteNativeMessagingRegKeys(
325+
regPath,
326+
nmhManifestFolder,
327+
NATIVE_MESSAGING_HOST_ID
328+
) {
329+
let wrk = Cc["@mozilla.org/windows-registry-key;1"].createInstance(
330+
Ci.nsIWindowsRegKey
331+
);
332+
try {
333+
let expectedValue = PathUtils.join(
334+
nmhManifestFolder,
335+
`${NATIVE_MESSAGING_HOST_ID}.json`
336+
);
337+
try {
338+
// If the key already exists it will just be opened
339+
wrk.create(
340+
wrk.ROOT_KEY_CURRENT_USER,
341+
regPath + `\\${NATIVE_MESSAGING_HOST_ID}`,
342+
wrk.ACCESS_ALL
343+
);
344+
if (wrk.readStringValue("") == expectedValue) {
345+
return;
346+
}
347+
} catch (e) {
348+
// The key either doesn't have a value or doesn't exist
349+
// In either case we need to write it.
350+
}
351+
wrk.writeStringValue("", expectedValue);
352+
} catch (e) {
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.
356+
} finally {
357+
wrk.close();
358+
}
305359
},
306360
};

browser/modules/test/unit/test_FirefoxBridgeExtensionUtilsNativeManifest.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ let userDir = dir.clone();
2323
userDir.append("user");
2424
userDir.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
2525

26+
let appDir = dir.clone();
27+
appDir.append("app");
28+
appDir.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
29+
2630
let dirProvider = {
2731
getFile(property) {
2832
if (property == "Home") {
2933
return userDir.clone();
34+
} else if (property == "AppData") {
35+
return appDir.clone();
3036
}
3137
return null;
3238
},
@@ -35,6 +41,9 @@ let dirProvider = {
3541
try {
3642
Services.dirsvc.undefine("Home");
3743
} catch (e) {}
44+
try {
45+
Services.dirsvc.undefine("AppData");
46+
} catch (e) {}
3847
Services.dirsvc.registerProvider(dirProvider);
3948

4049
registerCleanupFunction(() => {
@@ -47,7 +56,9 @@ const USER_TEST_PATH = PathUtils.join(userDir.path, "manifestDir");
4756
let binFile = null;
4857
add_setup(async function () {
4958
binFile = Services.dirsvc.get("XREExeF", Ci.nsIFile).parent.clone();
50-
if (AppConstants.platform == "macosx") {
59+
if (AppConstants.platform == "win") {
60+
binFile.append("nmhproxy.exe");
61+
} else if (AppConstants.platform == "macosx") {
5162
binFile.append("nmhproxy");
5263
} else {
5364
throw new Error("Unsupported platform");
@@ -180,6 +191,11 @@ add_task(async function test_ensureRegistered() {
180191
userDir.path,
181192
"Library/Application Support/Google/Chrome/NativeMessagingHosts/"
182193
);
194+
} else if (AppConstants.platform == "win") {
195+
expectedJSONDirPath = PathUtils.joinRelative(
196+
appDir.path,
197+
"Mozilla\\Firefox"
198+
);
183199
} else {
184200
throw new Error("Unsupported platform");
185201
}

browser/modules/test/unit/xpcshell.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ run-if = ["os == 'win'"] # Test of a Windows-specific feature
1010

1111
["test_FirefoxBridgeExtensionUtilsNativeManifest.js"]
1212
run-if = [
13+
"os == 'win'",
1314
"os == 'mac'",
1415
]
1516

0 commit comments

Comments
 (0)