8

I have a native app written in c++ and a chrome-extension.

I am communicating between them using 'chrome native messaging'.

Native-App code:

int main(int argc, char* argv[]) {
 unsigned int a, c, i, t=0;
 std::string inp;  do {
 inp="";
 t=0;
 // Sum the first 4 chars from stdin (the length of the message passed).
  for (i = 0; i <= 3; i++) {
    t += getchar();
  }

  // Loop getchar to pull in the message until we reach the total
  //  length provided.
  for (i=0; i < t; i++) {
    c = getchar();
    inp += c;
  }

// Collect the length of the message
unsigned int len = inp.length();
//// We need to send the 4 btyes of length information
std::cout << char(((len>>0) & 0xFF))
          << char(((len>>8) & 0xFF))
          << char(((len>>16) & 0xFF))
          << char(((len>>24) & 0xFF));
//// Now we can output our message
std::cout << inp <<std::endl;
flushall();
}while(cnt < 2 );
return 0;  }

Here I'm reading message sent by chrome-extension on stdin. and sending the same message back by writing it on stdout.

Extension is using PostMessage()

This is working... BUT ..

When I put my program under continuous while loop, the flow executes only once!

i.e port.postMessage({'text':'hello_1'}) gets echoed back as expected but if I do

port.postMessage({'text':'hello_2'}) it doesn't get echoed back.

I'm unable to understand what the problem is. Does it require threading?

Please help!

Thanks!

FuzzyAmi
  • 7,543
  • 6
  • 45
  • 79
rohitvk
  • 277
  • 3
  • 13

3 Answers3

10

Marc's answer contains some errors (inherited from the question) and will not work for messages with lengths that do not fit in one byte.

Chrome's protocol, when communicating with native apps is:

  • requests to native app are received through stdin
  • responses to Chrome are sent through stdout
  • Chrome doesn't deal well with Windows style \r\n so avoid that in the messages and set stdin mode to binary (so you can correctly read the request len and \n doesn't 'turn' into \r\n):

    _setmode(_fileno(stdin),_O_BINARY);
    

The request and response messages are JSON with a 4 byte header (uint32) containing the length of the message: [length 4 byte header][message]

Reading the request header:

uint32_t reqLen = 0;
cin.read(reinterpret_cast<char*>(&reqLen) ,4);

Writing the response header:

cout.write(reinterpret_cast<char*>(&responseLen),4); 
bkdc
  • 524
  • 3
  • 6
  • Dear @bkdc, I have a question, What is the type of Native Messaging? Is it a chrome app or an extension? I mean, is it possible to have a native messaging sample in an extension? – Hosein Aqajani Nov 22 '15 at 07:43
  • @H.Aqjn In this case, it's an extension! – rohitvk Dec 01 '15 at 10:12
  • @H.Aqjn Native messaging fills a somewhat similar role npapi/npruntime plugins used to - which is to provide a way to "go outside" the limits of what the browser can do and implement other pieces of functionality. However, native messaging is NOT a direct replacement for npapi/npruntime as it only provides a subset of that kind of functionality. Native Messaging (as an API) can be used by both Chrome extensions and Chrome applications to send and receive data from an external application that implements the communication protocol. – bkdc Mar 11 '16 at 13:04
2

This works for me:

 int main(int argc, char* argv[])
 {

 std::cout.setf( std::ios_base::unitbuf ); //instead of "<< eof" and "flushall"
 unsigned int a, c, i, t=0;
 std::string inp;  

 do {

 inp="";
 t=0;
 // Sum the first 4 chars from stdin (the length of the message passed).
  for (i = 0; i <= 3; i++) {
    t += getchar();
  }

  // Loop getchar to pull in the message until we reach the total
  //  length provided.
  for (i=0; i < t; i++) {
    c = getchar();
    inp += c;
  }

//Collect the length of the message
unsigned int len = inp.length();
//// We need to send the 4 btyes of length information
std::cout << char(((len>>0) & 0xFF))
          << char(((len>>8) & 0xFF))
          << char(((len>>16) & 0xFF))
          << char(((len>>24) & 0xFF));
//// Now we can output our message
std::cout << inp;
}

...

Marc
  • 95
  • 5
  • thnx man! It worked! by adding std::cout.setf( std::ios_base::unitbuf ); [Correct me if I'm wrong] it needed an EOF after std::cout right ? – rohitvk Nov 27 '13 at 07:17
  • Great! No, you dont need a "eof". "std::ios_base::unitbuf": flush output after each inserting operation. – Marc Nov 27 '13 at 07:29
  • 5
    @Marc your code for reading the message length is wrong: adding byte values together doesn't return the correct length. Please consider editing your answer with the solution provided in my answer or in [Chromium-extensions forum](https://groups.google.com/a/chromium.org/forum/#!msg/chromium-extensions/Y5RckbPVnr8/xe5w9RC6O5sJ) for more details. – bkdc Mar 10 '14 at 11:15
  • Dear @Marc I have a question: What is the type of Native Messaging? it is a chrome app or an extension? – Hosein Aqajani Nov 22 '15 at 07:44
0

The string length decode algorithm is incorrect. This is the correction:

for (i = 0; i <= 3; i++) {
    c = getchar();
    l |= (c << 8*i);
}
Keith
  • 1