0

I've written a Chrome extension and companion native messaging host. I don't have any issues with it failing to start or crashing, but I would like to be able to restart it for updates of the extension. I can't find anything in the documentation or elsewhere regarding this. Is it even possible, or does the browser need to be restarted? Due to the nature of the extension, I'd like to avoid restarting the browser if possible.

Documentation can be found here, but it's not exactly robust. https://developer.chrome.com/extensions/nativeMessaging

spike.barnett
  • 170
  • 1
  • 8
  • [What is the best way to auto update a windows application?](http://stackoverflow.com/q/7395609) - similar methods should be available for other platforms - I didn't try it but the obvious procedure would be to send a message from the extension to trigger the self-update and then simply send another message (or reconnect the port) in a few seconds which will start the updated application. – wOxxOm Dec 05 '15 at 05:35
  • @wOxxOm I'm not sure this is applicable. The native host needs to be started by Chrome as it takes over its stdin/stdout. It doesn't listen on the network. – spike.barnett Dec 05 '15 at 05:54
  • Chrome starts the host when you establish a connection so when the host terminates the connection is closed (at least this is how I would implement it), so my idea is that you can re-establish it by reconnecting. – wOxxOm Dec 05 '15 at 06:32
  • `Restart it for updates..??`, If I am getting you right then you can just use the `chrome.runtime.onInstalled.addListener(callback)` and connect to the native host application in the callback function using `chrome.runtime.connectNative()`. – Nikhil Sharma Dec 05 '15 at 06:40
  • @wOxxOm I think you're on to something here. In my case the host application is started when chrome is started and then sits and waits for messages, but I may just be able to run the host app before every message and close after, rather than leave it open. I'll have to look into this further. – spike.barnett Dec 05 '15 at 08:32
  • @NikhilSharma That seems like it should handle things. My only concern would be the old host app being left running. Chrome may close it on the chrome.runtime.connectNative call though. I'll have to look into this as well. – spike.barnett Dec 05 '15 at 08:37
  • 1
    Restart what for updates of what? The host for extensions' updates? The other way around? Please clarify that _in the question itself_. – Xan Dec 05 '15 at 09:33

1 Answers1

0

Upon further investigation I have found that restarting the native host application manually is not required. Chrome does this itself on update of the extension. However, that breaks the ability to send messages to the native host application from content scripts that have already been loaded, which was causing the issue I was seeing. Pages can be reloaded to fix messaging.

spike.barnett
  • 170
  • 1
  • 8
  • Please note, content scripts can detect that they are orphaned. [This question](http://stackoverflow.com/q/22516195/934239) might be of value. – Xan Dec 05 '15 at 11:22