3

According to the Chrome Native Messaging docs a successful call to connectNative() returns a port, with which you can post messages to a native app (a Mac app). In my case nativeConnect() does return a valid port in my case, but a call to onDisconnected() listener is fired almost immediately. Whenever the listener is fired it prints the "lastError" property to the browser's console, and this gives:

Specified native messaging host not found.

Why is it doing this? The listener that produces the msg looks like this:

function onDisconnected() {
  console.log("Inside onDisconnected(): " + chrome.runtime.lastError.message);
  port = null;
}

There's an entire section on this particular error toward the bottom of the docs (Native Messaging), and the proposed remedies say that either the manifest file is named, placed or defined (JSON) incorrectly, or else the host app is not named or located where the manifest says it should be. The doc says connectNative() will "start the host in a separate process", but Activity Monitor gives no evidence that the native host app was launched.

I call connectNative() as follows:

chrome.runtime.onMessageExternal.addListener(

  function(request, sender, sendResponse) {
    //var imgdata = JSON.stringify(request.imgdata);
    //process it somehow here

    port = chrome.runtime.connectNative("com.allinlearning.nmhforbrowserextension");

    if (port)
    {
       console.log("connectNative() returned a non-null port");

       port.onMessage.addListener(onNativeMessage);
       port.onDisconnect.addListener(onDisconnected);
    }
});

My native host manifest file is located in the correct folder as per docs, parses fine as JSON, and looks like:

{
  "name": "com.allinlearning.nmhforbrowserextension",
  "description": "Manifest for native messaging host for Google browser extension",
  "path": "/Users/mycomputer1/Documents/nmhost.app",
  "type": "stdio",
  "allowed_origins": ["chrome-extension://gldheanjpgopipommeingjlnoiamdfol/"]
}

The Chrome extension requires a manifest also, and until I got the permissions section right I was unable to get a non-null port back from connectNative(), so I'm pretty sure this is now correct:

"permissions": [
               "nativeMessaging",
                "tabs",
                "activeTab",
                "background",
                "http://*/", "https://*/"
                ]

UPDATE:

Figured out how to launch the Chrome browser from Mac's Terminal with flags enabling viewing of more "verbose" logging. Then when I ran things I noticed this output:

[21285:38915:1231/164417:ERROR:native_process_launcher.cc(131)] Can't find manifest for native messaging host com.allinlearning.nmhforbrowserextension

Pretty clear it cannot find the host manifest, but why??

Alyoshak
  • 2,696
  • 10
  • 43
  • 70
  • Well, obviously what matters is where you put the manifest. All you say is "in the correct folder as per docs". Please include the full path to the file. – Xan Jan 02 '15 at 16:59
  • 1
    @Xan, thx. 2 things: 1) I finally got it to find the manifest, but only by adding the missing directories needed to attempt the system-wide solution: /Library/Google/Chrome/NativeMessagingHosts. 2) I still cannot get it to work located here (user-specific location): ~/Library/Application Support/Google/Chrome/Default/NativeMessagingHosts. Since when we deploy we may only be allowed to install at user-specific locations, I do need to find a solution and so am leaving the question open. Besides it may benefit others down the road. – Alyoshak Jan 02 '15 at 19:45
  • @Alyoshak You were probably using Chrome Canary which means you have to `Google/Chrome\ Canary`... – vaughan Feb 25 '21 at 00:25

2 Answers2

2

For Google Chrome, the system-wide directory for the manifest file is:

~/Library/Application Support/Google/Chrome/NativeMessagingHosts/

The user-specific install path is at:

~/Library/Application Support/Chromium/NativeMessagingHosts/

(the currently documented path for Mac is incorrect (patch). The paths in install.sh (from the example in the documentation) are correct though).

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • To be complete, if chrome is launched with --user-data-dir=, replace ~/Library/Application Support/ by that custom directory. It's obvious but I just lost ten minutes just because I paste stuff without checking what it's doing :) – vincent Sep 16 '15 at 20:58
  • I got the same error message in windows. How can I solve it? – George Varghese Feb 09 '16 at 11:40
  • @GeorgeVarghese Read the documentation: https://developer.chrome.com/extensions/nativeMessaging#native-messaging-host-location – Rob W Feb 09 '16 at 11:40
  • Thank you for sharing the details. I added the registry details also. Now I got another error. "Invalid port!onMessage @ VM285:1397". – George Varghese Feb 10 '16 at 06:15
  • @GeorgeVarghese Could you create a new question and post all necessary details? "Invalid port" is not an error message from Chrome AFAIK. – Rob W Feb 10 '16 at 09:55
0

Just want to mention that if you are using a different Chrome release channel such as Canary, which is common during development, you will have to adjust the path accordingly.

~/Library/Application Support/Google/Chrome Canary/NativeMessagingHosts/
vaughan
  • 6,982
  • 6
  • 47
  • 63