用户关闭Chrome时,请关闭本机主机 [英] Close Native Host when Chrome is closed by the user

查看:222
本文介绍了用户关闭Chrome时,请关闭本机主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究使用C ++本机主机的Chrome扩展程序.在background.js脚本(永久设置为false)中,使用chrome.onstartup事件,我创建了与C ++主机的连接.

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.

我希望我的主机在用户一直在使用Chrome的情况下一直运行.

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

如果我关闭所有当前的Chrome选项卡,则独立的Chrome进程仍会显示在任务管理器的背景"部分(包括必须明确终止的Host进程).

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).

我了解用户可以将Chrome配置为不运行后台进程,但是我可以设计扩展程序以在用户关闭所有Chrome选项卡时杀死主机进程(断开端口)吗?

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?

此外,如果禁用扩展程序,问题将变得更加严重.主机进程在后台变为独立的进程.如果再次启用该扩展程序,则杀死所有已处理的Chrome并重新启动Chrome(因为我的扩展程序在启动Chrome时连接到Host),我将有多个Host进程.

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.

推荐答案

Chrome终止或您的扩展程序卸载后,它将向您的本地消息传递主机发送-1消息.假设您的本机消息传递主机是用C ++编写的,那么您将必须检查该值,这是您应该执行的操作:

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...
}

这篇关于用户关闭Chrome时,请关闭本机主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆