doesntgolf
doesntgolf

Reputation: 52

How to invoke Chrome native message host without arguments?

I'm using Chrome's native messaging API to connect to a native host I'm developing in Go with the Cobra library. The native application has a standalone CLI (implemented with Cobra), and the bare command (without any arguments) starts listening for JSON via stdin, which is meant to be an API for Chrome to interact with.

However, it fails every time the extension makes requests to the native messaging host (the client just immediately disconnects from the process). When I start Chrome with the --enable-logging flag I can see that the native host is erroring with unknown command "chrome-extension://cnjopnegooahjdngnkhiokognkdjiioc/" for "--native-app-name--". This is Cobra's error message that means "chrome-extension://cnjopnegooahjdngnkhiokognkdjiioc/" is being used as an argument, which seems to mean that Chrome is invoking the native host with app-name chrome-extension://cnjopnegooahjdngnkhiokognkdjiioc/ instead of just app-name.

Here's the code I'm using from the extension to call the native host:

var port = chrome.runtime.connectNative('app-name');
port.onMessage.addListener(function(msg) {
  console.log(msg);
});
port.onDisconnect.addListener(function() {
  console.log("disconnected");
});
port.postMessage({cmd:"ping"});

I can't find any documentation that suggests that Chrome sends the extension address as an argument, or whether it can be prevented.

Upvotes: 2

Views: 2792

Answers (1)

woxxom
woxxom

Reputation: 73526

It's part of the protocol and can't be disabled. The command line on Windows is something like this:

C:\Windows\system32\cmd.exe /c YOURHOSTAPP.exe chrome-extension://.................../  
--parent-window=6752474 < \\.\pipe\chrome.nativeMessaging.in.e11ed8be274e1a85 
> \\.\pipe\chrome.nativeMessaging.out.e11ed8be274e1a85

The first argument to the native messaging host is the origin of the caller, usually chrome-extension://[ID of whitelisted extension]. This allows native messaging hosts to identify the source of the message when multiple extensions are specified in the allowed_origins key in the native messaging host manifest.

On Windows, the native messaging host is also passed a command line argument with a handle to the calling chrome native window: --parent-window=<decimal handle value>. This lets the native messaging host create native UI windows that are correctly focused.


Warning: In Windows, in Chrome 54 and earlier, the origin was passed as the second parameter instead of the first parameter.

Upvotes: 4

Related Questions