Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8361)

Unified Diff: chrome/common/extensions/docs/examples/extensions/native_messaging/echo.rb

Issue 10818013: Native Messaging! (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Added Example Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/common/extensions/docs/examples/extensions/native_messaging/echo.rb
diff --git a/chrome/common/extensions/docs/examples/extensions/native_messaging/echo.rb b/chrome/common/extensions/docs/examples/extensions/native_messaging/echo.rb
new file mode 100755
index 0000000000000000000000000000000000000000..079b73a5a8b31d452fc7e2d819a176330cea2894
--- /dev/null
+++ b/chrome/common/extensions/docs/examples/extensions/native_messaging/echo.rb
@@ -0,0 +1,40 @@
+#!/usr/bin/ruby
+
+require 'iconv'
+
+# A simple native client (works with Ruby 1.8 and 1.9).
+# All this client does is echo the text it recieves back at the extension.
+
+MESSAGE_TYPE_SEND_MESSAGE_REQUEST = 0
+MESSAGE_TYPE_SEND_MESSAGE_RESPONSE = 1
+MESSAGE_TYPE_CONNECT = 2
+MESSAGE_TYPE_CONNECT_MESSAGE = 3
+
+messageNumber = 0
+
+while (!STDIN.eof?)
+ # Read the message type (first 4 bytes).
+ messageType = STDIN.read(4).unpack('L')[0]
+
+ # Read the message length (4 bytes).
+ textLength = STDIN.read(4).unpack('L')[0]
+
+ # Read the text (JSON object) of the message.
+ text = Iconv.conv('UTF-8//IGNORE', 'UTF-8',
+ STDIN.read(textLength) + ' ')[0..-2]
+
+ messageNumber += 1
+
+ response = Iconv.conv(
+ 'UTF-8//IGNORE', 'UTF-8',
+ "{\"id\": #{messageNumber}, \"echo\": #{text}}" + ' ')[0..-2]
+
+ # Choose the correct message type for the response.
+ responseType = messageType == MESSAGE_TYPE_SEND_MESSAGE_REQUEST ?
+ MESSAGE_TYPE_SEND_MESSAGE_RESPONSE :
+ MESSAGE_TYPE_CONNECT_MESSAGE
+
+ STDOUT.write([responseType, response.length].pack('LL'))
+ STDOUT.write(response)
+ STDOUT.flush()
+end

Powered by Google App Engine
This is Rietveld 408576698