-->

What is causing an “Error connecting to native app

2019-05-13 20:20发布

问题:

I am implementing communication between a web page and a Mac application using Chrome's Native Messaging feature. All seems to go well until I make the javascript call to chrome.runtime.connectNative("my_native_host_app_name"), which produces the following error in the console:

Error in event handler for runtime.onMessageExternal: Error connecting to native app: com.allinlearning.nmhforbrowserextension
Stack trace: Error: Error connecting to native app: com.allinlearning.nmhforbrowserextension
    at Object.<anonymous> (extensions::runtime:189:11)
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at Object.handleRequest (extensions::binding:55:27)
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at Object.<anonymous> (extensions::binding:318:32)
    at chrome-extension://gldheanjpgopipommeingjlnoiamdfol/background.js:19:27
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at EventImpl.dispatchToListener (extensions::event_bindings:395:22)
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at Event.$Array.forEach.publicClass.(anonymous function) [as dispatchToListener] (extensions::utils:65:26) extensions::event_bindings:383
EventImpl.dispatch_extensions::event_bindings:383
EventImpl.dispatchextensions::event_bindings:401
$Array.forEach.publicClass.(anonymous function)extensions::utils:65
messageListenerextensions::messaging:190
EventImpl.dispatchToListenerextensions::event_bindings:395
$Array.forEach.publicClass.(anonymous function)extensions::utils:65
EventImpl.dispatch_extensions::event_bindings:378
EventImpl.dispatchextensions::event_bindings:401
$Array.forEach.publicClass.(anonymous function)extensions::utils:65
dispatchOnMessageextensions::messaging:304

The actual call that seems to cause this error (line 19 in the ref to background.js in the stack trace) is:

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

To give more context, it is invoked from a listener:

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");
    else
       console.log("connectNative() returned null for the port");
});

It never gets to the if statement. The doc I am primarily using is Chrome Native Messaging. At the bottom of the doc it has a section to provide help with common errors Debugging native messaging. I can't seem to relate the "Error connecting to native app" to any of the specifically mentioned errors.

The complete contents of my extension manifest file ("manifest.json") are:

{
  "manifest_version": 2,

    "name": "AIL Extension",
    "version": "1.0",
    "description": "New Reader",

    "background": {
        "scripts": ["background.js"]
     },

    "externally_connectable": {
    // Extension and app IDs. If this field is not specified, no
    // extensions or apps can connect.
    "ids": [
      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
      "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
    ],
    // Match patterns for web pages. Does not affect content scripts.
    // If this field is not specified, no webpages can connect.
    "matches": [
      "http://localhost/charles/qrexttest/*"
    ],

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

    // Indicates that the extension would like to make use of the TLS
    // channel ID of the web page connecting to it. The web page must
    // also opt to send the TLS channel ID to the extension via setting
    // includeTlsChannelId to true in runtime.connect's connectInfo
    // or runtime.sendMessage's options.
    "accepts_tls_channel_id": false
  }
}

回答1:

One of the possible causes for the "Error connecting to native app: [native messaging host id]" is the absence of the required nativeMessaging permission.

Declare the "nativeMessaging" permission in the manifest file, reload the extension and try again.

(the fact that chrome.runtime.connectNative and sendNativeMessage are available without this permission is a bug.)