0

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?

Sri
  • 233
  • 2
  • 13
  • Indeed it seems that you're running into a problem of asynchronous callback. I don't think it is possible without a lot of hacking, see discussion here: http://stackoverflow.com/questions/15124995/how-to-wait-for-an-asynchronous-methods-callback-return-value – The Wavelength Sep 10 '14 at 08:10
  • This is now a very different question. **Please do both:** 1) roll back your edit, 2) ask a different question. – Xan Sep 10 '14 at 10:06

2 Answers2

1

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.

The Wavelength
  • 2,836
  • 2
  • 25
  • 44
  • I understand what you are saying about everything in chrome being asynchronous. Although it is efficient, it poses a problem for me due to the following: – Sri Sep 10 '14 at 07:27
  • 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? – Sri Sep 10 '14 at 07:33
  • Please remember to include this sort of text in your question. I mean, saying what you want to achieve and not only how ;-) To go back to the question: Indeed it seems that you're running into a problem of asynchronous callback. I don't think it is possible without a lot of hacking, see discussion here: http://stackoverflow.com/questions/15124995/how-to-wait-for-an-asynchronous-methods-callback-return-value – The Wavelength Sep 10 '14 at 08:09
0

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.

Xan
  • 74,770
  • 16
  • 179
  • 206