| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /// @file | |
| 6 /// This example demonstrates loading, running and scripting a very simple NaCl | |
| 7 /// module. To load the NaCl module, the browser first looks for the | |
| 8 /// CreateModule() factory method (at the end of this file). It calls | |
| 9 /// CreateModule() once to load the module code from your .nexe. After the | |
| 10 /// .nexe code is loaded, CreateModule() is not called again. | |
| 11 /// | |
| 12 /// Once the .nexe code is loaded, the browser then calls the | |
| 13 /// HelloWorldModule::CreateInstance() | |
| 14 /// method on the object returned by CreateModule(). It calls CreateInstance() | |
| 15 /// each time it encounters an <embed> tag that references your NaCl module. | |
| 16 | |
| 17 #include <cstdio> | |
| 18 #include <cstring> | |
| 19 #include <string> | |
| 20 #include "ppapi/cpp/instance.h" | |
| 21 #include "ppapi/cpp/module.h" | |
| 22 #include "ppapi/cpp/var.h" | |
| 23 | |
| 24 #include "helper_functions.h" | |
| 25 | |
| 26 namespace hello_world { | |
| 27 /// Method name for ReverseText, as seen by JavaScript code. | |
| 28 const char* const kReverseTextMethodId = "reverseText"; | |
| 29 | |
| 30 /// Method name for FortyTwo, as seen by Javascript code. @see FortyTwo() | |
| 31 const char* const kFortyTwoMethodId = "fortyTwo"; | |
| 32 | |
| 33 /// Separator character for the reverseText method. | |
| 34 static const char kMessageArgumentSeparator = ':'; | |
| 35 | |
| 36 /// This is the module's function that invokes FortyTwo and converts the return | |
| 37 /// value from an int32_t to a pp::Var for return. | |
| 38 pp::Var MarshallFortyTwo() { | |
| 39 return pp::Var(FortyTwo()); | |
| 40 } | |
| 41 | |
| 42 /// This function is passed the arg list from the JavaScript call to | |
| 43 /// @a reverseText. | |
| 44 /// It makes sure that there is one argument and that it is a string, returning | |
| 45 /// an error message if it is not. | |
| 46 /// On good input, it calls ReverseText and returns the result. The result is | |
| 47 /// then sent back via a call to PostMessage. | |
| 48 pp::Var MarshallReverseText(const std::string& text) { | |
| 49 return pp::Var(ReverseText(text)); | |
| 50 } | |
| 51 | |
| 52 /// The Instance class. One of these exists for each instance of your NaCl | |
| 53 /// module on the web page. The browser will ask the Module object to create | |
| 54 /// a new Instance for each occurrence of the <embed> tag that has these | |
| 55 /// attributes: | |
| 56 /// <pre> | |
| 57 /// type="application/x-nacl" | |
| 58 /// nacl="hello_world.nmf" | |
| 59 /// </pre> | |
| 60 class HelloWorldInstance : public pp::Instance { | |
| 61 public: | |
| 62 explicit HelloWorldInstance(PP_Instance instance) : pp::Instance(instance) { | |
| 63 printf("HelloWorldInstance.\n"); | |
| 64 } | |
| 65 virtual ~HelloWorldInstance() {} | |
| 66 | |
| 67 /// Called by the browser to handle the postMessage() call in Javascript. | |
| 68 /// Detects which method is being called from the message contents, and | |
| 69 /// calls the appropriate function. Posts the result back to the browser | |
| 70 /// asynchronously. | |
| 71 /// @param[in] var_message The message posted by the browser. The possible | |
| 72 /// messages are 'fortyTwo' and 'reverseText:Hello World'. Note that | |
| 73 /// the 'reverseText' form contains the string to reverse following a ':' | |
| 74 /// separator. | |
| 75 virtual void HandleMessage(const pp::Var& var_message); | |
| 76 }; | |
| 77 | |
| 78 void HelloWorldInstance::HandleMessage(const pp::Var& var_message) { | |
| 79 if (!var_message.is_string()) { | |
| 80 return; | |
| 81 } | |
| 82 std::string message = var_message.AsString(); | |
| 83 pp::Var return_var; | |
| 84 if (message == kFortyTwoMethodId) { | |
| 85 // Note that no arguments are passed in to FortyTwo. | |
| 86 return_var = MarshallFortyTwo(); | |
| 87 } else if (message.find(kReverseTextMethodId) == 0) { | |
| 88 // The argument to reverseText is everything after the first ':'. | |
| 89 size_t sep_pos = message.find_first_of(kMessageArgumentSeparator); | |
| 90 if (sep_pos != std::string::npos) { | |
| 91 std::string string_arg = message.substr(sep_pos + 1); | |
| 92 return_var = MarshallReverseText(string_arg); | |
| 93 } | |
| 94 } | |
| 95 // Post the return result back to the browser. Note that HandleMessage() is | |
| 96 // always called on the main thread, so it's OK to post the return message | |
| 97 // directly from here. The return post is asynhronous: PostMessage returns | |
| 98 // immediately. | |
| 99 PostMessage(return_var); | |
| 100 } | |
| 101 | |
| 102 /// The Module class. The browser calls the CreateInstance() method to create | |
| 103 /// an instance of your NaCl module on the web page. The browser creates a new | |
| 104 /// instance for each <embed> tag with | |
| 105 /// <code>type="application/x-nacl"</code>. | |
| 106 class HelloWorldModule : public pp::Module { | |
| 107 public: | |
| 108 HelloWorldModule() : pp::Module() { | |
| 109 printf("Got here.\n"); | |
| 110 } | |
| 111 virtual ~HelloWorldModule() {} | |
| 112 | |
| 113 /// Create and return a HelloWorldInstance object. | |
| 114 /// @param[in] instance a handle to a plug-in instance. | |
| 115 /// @return a newly created HelloWorldInstance. | |
| 116 /// @note The browser is responsible for calling @a delete when done. | |
| 117 virtual pp::Instance* CreateInstance(PP_Instance instance) { | |
| 118 return new HelloWorldInstance(instance); | |
| 119 } | |
| 120 }; | |
| 121 } // namespace hello_world | |
| 122 | |
| 123 | |
| 124 namespace pp { | |
| 125 /// Factory function called by the browser when the module is first loaded. | |
| 126 /// The browser keeps a singleton of this module. It calls the | |
| 127 /// CreateInstance() method on the object you return to make instances. There | |
| 128 /// is one instance per <embed> tag on the page. This is the main binding | |
| 129 /// point for your NaCl module with the browser. | |
| 130 /// @return new HelloWorldModule. | |
| 131 /// @note The browser is responsible for deleting returned @a Module. | |
| 132 Module* CreateModule() { | |
| 133 return new hello_world::HelloWorldModule(); | |
| 134 } | |
| 135 } // namespace pp | |
| OLD | NEW |