3
votes

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??

2
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
@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
@Alyoshak You were probably using Chrome Canary which means you have to Google/Chrome\ Canary... - vaughan

2 Answers

2
votes

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).

0
votes

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/