Close Native Host when Chrome is closed by the user

旧时模样 提交于 2019-12-11 17:57:47

问题


I am working on a Chrome extension that uses a C++ Native Host. In a background.js script (persistent set to false), using chrome.onstartup event, I create the connection to the C++ Host.

I want my Host to be running for as long as the user is actively using Chrome.

If I close all my current Chrome tabs, independent Chrome processes still appear in the "Background section" of the Task Manager (including my Host process that must be explicitly killed).

I understood that the user can configure the Chrome not to run background processes, but can I design my extension to kill the Host process(disconnect the port) when the user closes all Chrome tabs?

Moreover, the problem becomes more serious if I disable the extension. The Host process becomes a detached process in the background. If enable the extension again, kill all Chrome processed and restart Chrome (as my extension connects to Host on startup of Chrome), I will have multiple Host processes.


回答1:


When Chrome terminates or your extension is unloaded, it will send -1 message to your native messaging host. You will have to check for that value, assuming your native messaging host is written in C++ then this is what you should do:

int read_char = ::getchar();
if (read_char == -1) {
// Do termination work here...
}


来源:https://stackoverflow.com/questions/55079220/close-native-host-when-chrome-is-closed-by-the-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!