Native Messaging : Is working in macOS yet?

To support my macOS bookmarks syncing application, I have an old Firefox extension (whose XPCOM binary I’ve lost the recipe to build!), and Google Chrome and Opera extensions. I am therefore delighted about WebExtensions. But I’m having trouble with Native Messaging.

What I did

  1. Copy the files in my Google Chrome extension to a folder named myextension@mycompany.com.

  2. Copy my Google Chrome Native Messaging Manifest file to
    ~/Library/Application Support/Mozilla/NativeMessagingHosts/com.mycompany.chromessenger.json

  3. In that Native Messaging Manifest, delete the allowed_origins key and insert instead
    "allowed_extensions" : [ "myextension@mycompany.com" ]

  4. Test in Firefox Developer Edition 51.0a2 using Load Temporary Add-On.

Result

The extension seems to load OK – its button appears in the toolbar. And if I put a syntax error in the Native Messaging Manifest, I get a complaint in the Debug Console – so I know that Firefox is reading my Native Messaging Manifest. But when I click my extension’s button, and it attempts to open its Native Messaging connection, the Debug Console says:

This extension does not have permission to use native application com.mycompany.chromessenger [NativeMessaging.jsm:192]

I could not find in the documentation a clear statement of how WebExtensions are identified, so as you can see I presumed that the extension identifier is only the name of the parent folder, e.g. myextension.mycompany.com. Is that correct? If so, what might be wrong here? Has anyone successfully used Native Messaging in their macOS extension yet? Is there a demo?

Hi there,
I’m new to native messaging but this demo worked for me - https://github.com/mdn/webextensions-examples/tree/master/native-messaging/app

As I’m new I can’t really help much, but I have questions if you can help me! Haha

Can you please share how you are copying the exe and the manifest onto the users computer? I was hoping to try out cross browser extension but did not know how to write an installer. How do you distribute that installer? Do the opera and chrome markets allow you to upload an installer?

Also do you know where I can find all the error messages returned by the native messaging startup for Chrome and Opera?

Thank you, @noitidart.

I was not aware of that demo. I looked through it and didn’t find anything different than what I was doing, except for the differences which are necessary because it is running a Python script on Microsoft Windows instead of a binary on macOS. With Native Messaging there are several moving parts which need to be set up, so unfortunately you can’t just “download, click and test”.

Regarding your questions, I will try to help, but privately because your questions are off topic. Look for my private message.

1 Like

Thanks!

I was able to get it working in macOS, try using NAME.json where NAME is the value of “name” in the manifest.json file.

Actually forget that, the problem is this:

https://dxr.mozilla.org/mozilla-central/source/toolkit/components/extensions/NativeMessaging.jsm#191

    if (!hostInfo.manifest.allowed_extensions.includes(extension.id)) {
      throw new Error(`This extension does not have permission to use native application ${application}`);
    }

So you need to place your ID into the allowed_extensions array in the manifest.json

And the way you set your id in the manifest.json is like this:

"applications": {
	"gecko": {
		"id": "Profilist@jetpack",
		"strict_min_version": "51.0"
	}
},

Very good, @noitidart! When I read (too quickly) the documentation regarding extension manifest files, regarding that applications key, I mis-interpreted the phrase mandatory in Firefox before Firefox 48 to mean not required in Firefox 48 or later. But it looks like it is still required if you are using Native Messaging. At least, after I added applications and re-tested, my Native Messaging connection worked :slight_smile:

I cannot find that requirement in the documentation, though. I would appreciate if someone who knows the details of this requirement would please comment.

If you use native messaging, the native app must explicitly grant access to the add-on by listing the add-on’s ID in the “allowed_extensions” key in the app manifest.

“You should probably specify your add-on ID explicitly, using the applications
manifest key (This is because the app’s manifest will identify the set
of WebExtensions that are allowed to connect to it by listing their
IDs).”

https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Native_messaging#Add-on_manifest