Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 .. _message-system: | 1 .. _message-system: |
| 2 | 2 |
| 3 ################ | 3 ################ |
| 4 Messaging System | 4 Messaging System |
| 5 ################ | 5 ################ |
| 6 | 6 |
| 7 .. contents:: | 7 .. contents:: |
| 8 :local: | 8 :local: |
| 9 :backlinks: none | 9 :backlinks: none |
| 10 :depth: 2 | 10 :depth: 2 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 The "Hello, World" example for getting started with NaCl is used here to | 23 The "Hello, World" example for getting started with NaCl is used here to |
| 24 illustrate basic programming techniques. You can find this code in | 24 illustrate basic programming techniques. You can find this code in |
| 25 the ``/getting_started/part2`` directory in the Native Client SDK download. | 25 the ``/getting_started/part2`` directory in the Native Client SDK download. |
| 26 | 26 |
| 27 Reference information | 27 Reference information |
| 28 ===================== | 28 ===================== |
| 29 | 29 |
| 30 For reference information related to the Pepper messaging API, see the | 30 For reference information related to the Pepper messaging API, see the |
| 31 following documentation: | 31 following documentation: |
| 32 | 32 |
| 33 * `pp::Instance class <https://developers.google.com/native-client/peppercpp/cla sspp_1_1_instance>`_ HandleMessage(), PostMessage()) | 33 * `pp::Instance class </native-client/pepper_stable/cpp/classpp_1_1_instance>`_ |
| 34 * `pp::Module class <https://developers.google.com/native-client/peppercpp/class pp_1_1_module>`_ | 34 HandleMessage(), PostMessage()) |
| 35 * `pp::Var class <https://developers.google.com/native-client/peppercpp/classpp_ 1_1_var>`_ | 35 * `pp::Module class </native-client/pepper_stable/cpp/classpp_1_1_module>`_ |
| 36 * `pp::Var class </native-client/pepper_stable/cpp/classpp_1_1_var>`_ | |
| 36 | 37 |
| 37 Introduction to the messaging system | 38 Introduction to the messaging system |
| 38 ==================================== | 39 ==================================== |
| 39 | 40 |
| 40 Native Client modules and JavaScript communicate by sending messages | 41 Native Client modules and JavaScript communicate by sending messages to each |
| 41 to each other. The most basic form of a message is a string. Messages | 42 other. The most basic form of a message is a string. Messages support many |
| 42 support many JavaScript types, including ints, arrays, array buffers, | 43 JavaScript types, including ints, arrays, array buffers, and dictionaries (see |
| 43 and dictionaries (see `pp::Var | 44 `pp::Var <h/native-client/pepper_stable/cpp/classpp_1_1_var>`_, |
|
JF
2014/04/29 20:49:25
extra h?
binji
2014/04/30 17:31:31
Done.
| |
| 44 <https://developers.google.com/native-client/peppercpp/classpp_1_1_var>`_, | |
| 45 `pp:VarArrayBuffer | 45 `pp:VarArrayBuffer |
| 46 <https://developers.google.com/native-client/peppercpp/classpp_1_1_var_array_buf fer>`_, | 46 </native-client/pepper_stable/cpp/classpp_1_1_var_array_buffer>`_, and the |
| 47 and the general `messaging system documentation | 47 general `messaging system documentation |
| 48 <https://developers.google.com/native-client/pepperc/struct_p_p_b___messaging__1 __0>`_). | 48 </native-client/pepper_stable/c/struct_p_p_b___messaging__1__0>`_). It's up to |
| 49 It's up to you to decide on the type of message and define how to | 49 you to decide on the type of message and define how to process the messages on |
| 50 process the messages on both the JavaScript and Native Client | 50 both the JavaScript and Native Client side. For the "Hello, World" example, we |
| 51 side. For the "Hello, World" example, we will work with string-typed | 51 will work with string-typed messages only. |
| 52 messages only. | |
| 53 | 52 |
| 54 When JavaScript posts a message to the Native Client module, the | 53 When JavaScript posts a message to the Native Client module, the |
| 55 Pepper ``HandleMessage()`` function is invoked on the module | 54 Pepper ``HandleMessage()`` function is invoked on the module |
| 56 side. Similarly, the Native Client module can post a message to | 55 side. Similarly, the Native Client module can post a message to |
| 57 JavaScript, and this message triggers a JavaScript event listener for | 56 JavaScript, and this message triggers a JavaScript event listener for |
| 58 ``message`` events in the DOM. (See the W3C specification on | 57 ``message`` events in the DOM. (See the W3C specification on |
| 59 `Document Object Model Events | 58 `Document Object Model Events |
| 60 <http://www.w3.org/TR/DOM-Level-2-Events/events.html>`_ for more | 59 <http://www.w3.org/TR/DOM-Level-2-Events/events.html>`_ for more |
| 61 information.) In the "Hello, World" example, the JavaScript functions for | 60 information.) In the "Hello, World" example, the JavaScript functions for |
| 62 posting and handling messages are named ``postMessage()`` and | 61 posting and handling messages are named ``postMessage()`` and |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 | 168 |
| 170 Native Client module | 169 Native Client module |
| 171 -------------------- | 170 -------------------- |
| 172 | 171 |
| 173 The C++ code in the Native Client module of the "Hello, World" example: | 172 The C++ code in the Native Client module of the "Hello, World" example: |
| 174 | 173 |
| 175 #. Implements ``pp::Instance::HandleMessage()`` to handle messages sent | 174 #. Implements ``pp::Instance::HandleMessage()`` to handle messages sent |
| 176 by the JavaScript. | 175 by the JavaScript. |
| 177 #. Processes incoming messages. This example simply checks that JavaScript | 176 #. Processes incoming messages. This example simply checks that JavaScript |
| 178 has sent a "hello" message and not some other message. | 177 has sent a "hello" message and not some other message. |
| 179 #. Calls ``PostMessage()`` to send an acknowledgement back to the | 178 #. Calls ``PostMessage()`` to send an acknowledgement back to the JavaScript |
| 180 JavaScript code. The acknowledgement is a string in the form of a ``Var`` | 179 code. The acknowledgement is a string in the form of a ``Var`` that the |
| 181 that the JavaScript code can process. In general, a ``pp::Var`` can be | 180 JavaScript code can process. In general, a ``pp::Var`` can be several |
| 182 several JavaScript types, see the | 181 JavaScript types, see the `messaging system documentation |
| 183 `messaging system documentation | 182 </native-client/pepper_stable/c/struct_p_p_b___messaging__1__0>`_. |
| 184 <https://developers.google.com/native-client/pepperc/struct_p_p_b___messaging __1__0>`_. | |
| 185 | 183 |
| 186 | 184 |
| 187 .. naclcode:: | 185 .. naclcode:: |
| 188 | 186 |
| 189 class HelloTutorialInstance : public pp::Instance { | 187 class HelloTutorialInstance : public pp::Instance { |
| 190 public: | 188 public: |
| 191 // ... | 189 // ... |
| 192 | 190 |
| 193 // === Step 1: Implement the HandleMessage function. === | 191 // === Step 1: Implement the HandleMessage function. === |
| 194 virtual void HandleMessage(const pp::Var& var_message) { | 192 virtual void HandleMessage(const pp::Var& var_message) { |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 406 virtual void HandleMessage(const pp::Var& var) { | 404 virtual void HandleMessage(const pp::Var& var) { |
| 407 if (var.is_dictionary()) { | 405 if (var.is_dictionary()) { |
| 408 pp::VarDictionary dictionary(var); | 406 pp::VarDictionary dictionary(var); |
| 409 // Use the dictionary | 407 // Use the dictionary |
| 410 pp::VarArray keys = dictionary.GetKeys(); | 408 pp::VarArray keys = dictionary.GetKeys(); |
| 411 // ... | 409 // ... |
| 412 } else { | 410 } else { |
| 413 // ... | 411 // ... |
| 414 } | 412 } |
| 415 } | 413 } |
| OLD | NEW |