Native messaging - Escape sequences

65 views
Skip to first unread message

malja

unread,
Aug 24, 2015, 9:54:49 AM8/24/15
to Chromium-extensions
Hi all,

I have been trying to get working the Native messaging from my chrome extension to native C++ application for last few days. It works quite good, unless the message contains special escape sequences. It seems they are completely ignored and sent as a common ones. For example '\n' (newline) is split into two characters - backslash and n.

Event page:

var nativeHost = chrome.runtime.connectNative( "myapp" );
nativeHost
.postMessage( "One\nTwo\\nThree\tFour" );

My native application contains just few lines to get and print the message to cerr. 

/ Get message length
unsigned int size = 0;

std::cin.read(reinterpret_cast<char*>(&size) ,4);

char *buffer = (char*)malloc( sizeof(char) * (size + 1));
std::cin.read( buffer, size);
std::cerr << buffer << std::endl;
free( buffer );

The output looks exactly the same as the message sent - "One\nTwo\\nThree\tFour". I would rather expect something like:

One
Two
Three    Four

Could you please point me to the solution?
thank you.

Tony BenBrahim

unread,
Aug 24, 2015, 12:16:51 PM8/24/15
to malja, Chromium-extensions
What you get on the native application is exactly what you sent. You need a JSON parser in your native app to make the conversion. For C++11, https://github.com/nlohmann/json is a very good choice. Also note that you may find it more useful to send actual Javascript objects, rather than trying to come up with a multiline protocol. For example;

{
   "command";  "doSomething",
   "arg1"; 42,
   "arg2'; true
}

rather than "doSomething\n42\ntrue".

With a good JSON parser like the one mentioned above, you will get a string for request['"command'], a number for request["arg1"] and a bool for request["arg2"']

Tony

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/ff512f45-bc38-4773-93c1-9d15313b74c1%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

Reply all
Reply to author
Forward
0 new messages