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

Unified Diff: native_client_sdk/src/doc/devguide/coding/message-system.rst

Issue 23688008: [NaCl Docs] Add some messaging system docs for how to use pp::VarDictionaries. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: spacing Created 7 years, 3 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: native_client_sdk/src/doc/devguide/coding/message-system.rst
diff --git a/native_client_sdk/src/doc/devguide/coding/message-system.rst b/native_client_sdk/src/doc/devguide/coding/message-system.rst
index e72168745b75f90398cfe7d8db92c8c8f5a1e84b..ca7bd4d6377736db7182e977a37eb229973eb526 100644
--- a/native_client_sdk/src/doc/devguide/coding/message-system.rst
+++ b/native_client_sdk/src/doc/devguide/coding/message-system.rst
@@ -358,3 +358,58 @@ end of the Native Client module's ``HandleMessage()`` function:
.. naclcode::
PostMessage(var_reply);
+
+
+Sending and receiving other ``pp::Var`` types
+---------------------------------------------
+
+Besides strings, ``pp::Var`` can represent other types of JavaScript
+objects. For example, messages can be JavaScript objects. These
+richer types can make it easier to implement an application's
+messaging protocol.
+
+To send a dictionary from the NaCl module to JavaScript simply create
+a ``pp::VarDictionary`` and then call ``PostMessage`` with the
+dictionary.
+
+.. naclcode::
+
+ pp::VarDictionary dictionary;
+ dictionary.Set(pp::Var("command"), pp::Var(next_command));
+ dictionary.Set(pp::Var("param_int"), pp::Var(123));
+ pp::VarArray an_array;
+ an_array.Set(0, pp::Var("string0"));
+ an_array.Set(1, pp::Var("string1"))
+ dictionary.Set(pp::Var("param_array"), an_array);
+ PostMessage(dictionary);
+
+
+Here is how to create a similar object in JavaScript and send it to
+the NaCl module:
+
+.. naclcode::
+
+ var dictionary = {
+ command: next_command,
+ param_int: 123,
+ param_array: ['string0', 'string1']
+ }
+ nacl_module.postMessage(dictionary);
+
+
+To receive a dictionary-typed message in the NaCl module, test that
+the message is truly a dictionary type, then convert the message
+with the ``pp::VarDictionary`` class.
+
+.. naclcode::
+
+ virtual void HandleMessage(const pp::Var& var) {
+ if (var.is_dictionary()) {
+ pp::VarDictionary dictionary(var);
+ // Use the dictionary
+ pp::VarArray keys = dictionary.GetKeys();
+ // ...
+ } else {
+ // ...
+ }
+ }
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698