OLD | NEW |
| (Empty) |
1 /// @file $safeprojectname$.cc | |
2 /// This example demonstrates loading, running and scripting a very simple NaCl | |
3 /// module. To load the NaCl module, the browser first looks for the | |
4 /// CreateModule() factory method (at the end of this file). It calls | |
5 /// CreateModule() once to load the module code from your .nexe. After the | |
6 /// .nexe code is loaded, CreateModule() is not called again. | |
7 /// | |
8 /// Once the .nexe code is loaded, the browser than calls the CreateInstance() | |
9 /// method on the object returned by CreateModule(). It calls CreateInstance() | |
10 /// each time it encounters an <embed> tag that references your NaCl module. | |
11 /// | |
12 /// The browser can talk to your NaCl module via the postMessage() Javascript | |
13 /// function. When you call postMessage() on your NaCl module from the browser, | |
14 /// this becomes a call to the HandleMessage() method of your pp::Instance | |
15 /// subclass. You can send messages back to the browser by calling the | |
16 /// PostMessage() method on your pp::Instance. Note that these two methods | |
17 /// (postMessage() in Javascript and PostMessage() in C++) are asynchronous. | |
18 /// This means they return immediately - there is no waiting for the message | |
19 /// to be handled. This has implications in your program design, particularly | |
20 /// when mutating property values that are exposed to both the browser and the | |
21 /// NaCl module. | |
22 | |
23 #include <cstdio> | |
24 #include <string> | |
25 #include "ppapi/cpp/instance.h" | |
26 #include "ppapi/cpp/module.h" | |
27 #include "ppapi/cpp/var.h" | |
28 | |
29 // Note to the user: This glue code reflects the current state of affairs. It | |
30 // may change. In particular, interface elements marked as deprecated will | |
31 // disappear sometime in the near future and replaced with more elegant | |
32 // interfaces. As of the time of this writing, the new interfaces are not | |
33 // available so we have to provide this code as it is written below. | |
34 | |
35 /// The Instance class. One of these exists for each instance of your NaCl | |
36 /// module on the web page. The browser will ask the Module object to create | |
37 /// a new Instance for each occurence of the <embed> tag that has these | |
38 /// attributes: | |
39 /// type="application/x-nacl" | |
40 /// src="$safeprojectname$.nmf" | |
41 /// To communicate with the browser, you must override HandleMessage() for | |
42 /// receiving messages from the borwser, and use PostMessage() to send messages | |
43 /// back to the browser. Note that this interface is entirely asynchronous. | |
44 class $safeprojectname$Instance : public pp::Instance { | |
45 public: | |
46 /// The constructor creates the plugin-side instance. | |
47 /// @param[in] instance the handle to the browser-side plugin instance. | |
48 explicit $safeprojectname$Instance(PP_Instance instance) : pp::Instance(instan
ce) | |
49 {} | |
50 virtual ~$safeprojectname$Instance() {} | |
51 | |
52 /// Handler for messages coming in from the browser via postMessage(). The | |
53 /// @a var_message can contain anything: a JSON string; a string that encodes | |
54 /// method names and arguments; etc. For example, you could use | |
55 /// JSON.stringify in the browser to create a message that contains a method | |
56 /// name and some parameters, something like this: | |
57 /// var json_message = JSON.stringify({ "myMethod" : "3.14159" }); | |
58 /// nacl_module.postMessage(json_message); | |
59 /// On receipt of this message in @a var_message, you could parse the JSON to | |
60 /// retrieve the method name, match it to a function call, and then call it | |
61 /// with the parameter. | |
62 /// @param[in] var_message The message posted by the browser. | |
63 virtual void HandleMessage(const pp::Var& var_message) { | |
64 // TODO(sdk_user): 1. Make this function handle the incoming message. | |
65 } | |
66 }; | |
67 | |
68 /// The Module class. The browser calls the CreateInstance() method to create | |
69 /// an instance of your NaCl module on the web page. The browser creates a new | |
70 /// instance for each <embed> tag with type="application/x-nacl". | |
71 class $safeprojectname$Module : public pp::Module { | |
72 public: | |
73 $safeprojectname$Module() : pp::Module() {} | |
74 virtual ~$safeprojectname$Module() {} | |
75 | |
76 /// Create and return a $safeprojectname$Instance object. | |
77 /// @param[in] instance The browser-side instance. | |
78 /// @return the plugin-side instance. | |
79 virtual pp::Instance* CreateInstance(PP_Instance instance) { | |
80 return new $safeprojectname$Instance(instance); | |
81 } | |
82 }; | |
83 | |
84 namespace pp { | |
85 /// Factory function called by the browser when the module is first loaded. | |
86 /// The browser keeps a singleton of this module. It calls the | |
87 /// CreateInstance() method on the object you return to make instances. There | |
88 /// is one instance per <embed> tag on the page. This is the main binding | |
89 /// point for your NaCl module with the browser. | |
90 Module* CreateModule() { | |
91 return new $safeprojectname$Module(); | |
92 } | |
93 } // namespace pp | |
OLD | NEW |