shacharsa
shacharsa

Reputation: 381

Chrome Native Messaging not closed when chrome closed

I've made chrome host for passing native messages between my extension and my process, my process starts when chrome starts but not closed when i close chrome, should i add parameter to the manifest of the host or should i add my process handling to close the process when the chrome closed ?

thanks.

Upvotes: 10

Views: 2141

Answers (2)

myuce
myuce

Reputation: 1391

Building upon bkdc's answer;

if you are using C library function - getchar(), this should work;

unsigned int c = getchar();
if (c == 0xFFFFFFFF){//this means native app finished its work
        return 0;
}

Upvotes: 0

bkdc
bkdc

Reputation: 524

You don't offer a lot of details but I can answer some of your questions: - no need to add a "parameter" to the manifest; there is no such parameter - no need to detect, from your process, when Chrome closes

Chrome starts your native messaging host whenever you send a message to it or, if you're using long lived connection, when you open the connection. The application should close when the STDIN stream closes (simply put, you can't read anymore from stdin).

For a single threaded app, the flow looks like this: 1. Read request from stdin 2. process request 3. write response to stdout; Repeat 1-3 as long as you can read from stdin; if you can't read from stdin, break the loop and exit.

    std::string req;
while(!(req=read_request()).empty())
{
       //process request and send response
}

read_request is up to you to implement: the first 4 BYTES of the request contain the message length so read those 4 first then read len bytes that contain the actual JSON request. If you can't read anything (empty), then the while loop will break and you exit the app.

Upvotes: 7

Related Questions