Sri
Sri

Reputation: 233

Initiate message from host app to chrome extension

I have managed to send message from a chrome extension to a native (C++) app thanks to the good folks of stack overflow. Once the native host app receives the message, it can return any response data. However, this is in response to the native app receiving data from the extension. Also, the message sent by the extension is not synchronous and therefore the extension doesn't wait for a response from the host app.

My requirement is to initiate the comms connection from the native app i.e. my native app will periodically send updates to the chrome extension. Is there any mechanism to initiate the message sending from the native app and pass it to the appropriate chrome extension?

I basically want to achieve the following: I want to post a message on events like webRequest.OnBeforeSendHeaders and I want the response to the message to decide if I should block the url request. If the receiving of the response is going to be on a callback, this will mostly be on a different thread and that will not allow me to wait for the response on the webRequest.OnBeforeSendHeaders thread. Is there a way I can pause the webRequest.OnBeforeSendHeaders while I wait for the native app to respond to my request? Or can webRequest.OnBeforeSendHeaders peek into the message buffer (in a loop) to see if the native app has responded?

Or is there an alternative way to achieve this?

Upvotes: 0

Views: 359

Answers (2)

Xan
Xan

Reputation: 77502

By design, you cannot initiate the connection from the native app.

For every open port, Chrome starts an instance of your app. If you want the app to send updates, you need to open a port on the Chrome side and keep it open.

Upvotes: 0

The Wavelength
The Wavelength

Reputation: 2914

Nothing in chrome is synchronous. That's one of the reasons why chrome isn't likely to freeze. But that is not the issue. You can achieve what you want easily even with asynchronous code, it's just another pattern you must follow.

The description on how to proceed with native apps is here: https://developer.chrome.com/extensions/messaging#native-messaging. There is described that you can pass for example stdio to the type-parameter. Then, whenever you send data to the app, Chrome writes to stdin. You can send messages back (i. e. to response to something) using stdout (in case of C++: print it through cout). In the native app, you don't have to know that your native app is actually a Chrome native app. It just needs to know that it received data from stdin and sends data through stdout.

Upvotes: 1

Related Questions