OLD | NEW |
---|---|
(Empty) | |
1 /// Copyright (c) 2012 The Native Client Authors. All rights reserved. | |
eliben
2013/09/09 16:48:31
2013
binji
2013/09/09 18:43:46
Done.
| |
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 hello_tutorial.cc | |
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 | |
eliben
2013/09/09 16:48:31
Should we be talking about a .exe here? Maybe .pex
binji
2013/09/09 18:43:46
Done.
| |
10 /// .nexe code is loaded, CreateModule() is not called again. | |
11 /// | |
12 /// Once the .nexe code is loaded, the browser than calls the CreateInstance() | |
13 /// method on the object returned by CreateModule(). It calls CreateInstance() | |
14 /// each time it encounters an <embed> tag that references your NaCl module. | |
15 /// | |
16 /// The browser can talk to your NaCl module via the postMessage() Javascript | |
17 /// function. When you call postMessage() on your NaCl module from the browser, | |
18 /// this becomes a call to the HandleMessage() method of your pp::Instance | |
19 /// subclass. You can send messages back to the browser by calling the | |
20 /// PostMessage() method on your pp::Instance. Note that these two methods | |
21 /// (postMessage() in Javascript and PostMessage() in C++) are asynchronous. | |
22 /// This means they return immediately - there is no waiting for the message | |
23 /// to be handled. This has implications in your program design, particularly | |
24 /// when mutating property values that are exposed to both the browser and the | |
25 /// NaCl module. | |
26 | |
27 #include <cstdio> | |
28 #include <string> | |
eliben
2013/09/09 16:48:31
i don't think it's used?
maybe cstdio too?
binji
2013/09/09 18:43:46
Done.
| |
29 #include "ppapi/cpp/instance.h" | |
30 #include "ppapi/cpp/module.h" | |
31 #include "ppapi/cpp/var.h" | |
32 | |
33 /// The Instance class. One of these exists for each instance of your NaCl | |
34 /// module on the web page. The browser will ask the Module object to create | |
35 /// a new Instance for each occurence of the <embed> tag that has these | |
eliben
2013/09/09 16:48:31
occurrence
binji
2013/09/09 18:43:46
Done.
| |
36 /// attributes: | |
37 /// type="application/x-pnacl" | |
38 /// src="hello_tutorial.nmf" | |
39 /// To communicate with the browser, you must override HandleMessage() for | |
40 /// receiving messages from the browser, and use PostMessage() to send messages | |
noelallen1
2013/09/09 04:32:00
for receiving... for sending...
binji
2013/09/09 18:43:46
Done.
| |
41 /// back to the browser. Note that this interface is asynchronous. | |
42 class HelloTutorialInstance : public pp::Instance { | |
43 public: | |
44 /// The constructor creates the plugin-side instance. | |
45 /// @param[in] instance the handle to the browser-side plugin instance. | |
46 explicit HelloTutorialInstance(PP_Instance instance) : pp::Instance(instance) | |
47 {} | |
48 virtual ~HelloTutorialInstance() {} | |
49 | |
50 /// Handler for messages coming in from the browser via postMessage(). The | |
51 /// @a var_message can contain anything: a JSON string; a string that encodes | |
52 /// method names and arguments; etc. For example, you could use | |
53 /// JSON.stringify in the browser to create a message that contains a method | |
54 /// name and some parameters, something like this: | |
55 /// var json_message = JSON.stringify({ "myMethod" : "3.14159" }); | |
56 /// nacl_module.postMessage(json_message); | |
57 /// On receipt of this message in @a var_message, you could parse the JSON to | |
58 /// retrieve the method name, match it to a function call, and then call it | |
59 /// with the parameter. | |
60 /// @param[in] var_message The message posted by the browser. | |
61 virtual void HandleMessage(const pp::Var& var_message) { | |
62 // TODO(sdk_user): 1. Make this function handle the incoming message. | |
63 } | |
64 }; | |
65 | |
66 /// The Module class. The browser calls the CreateInstance() method to create | |
67 /// an instance of your NaCl module on the web page. The browser creates a new | |
68 /// instance for each <embed> tag with type="application/x-nacl". | |
69 class HelloTutorialModule : public pp::Module { | |
70 public: | |
71 HelloTutorialModule() : pp::Module() {} | |
72 virtual ~HelloTutorialModule() {} | |
73 | |
74 /// Create and return a HelloTutorialInstance object. | |
75 /// @param[in] instance The browser-side instance. | |
76 /// @return the plugin-side instance. | |
77 virtual pp::Instance* CreateInstance(PP_Instance instance) { | |
78 return new HelloTutorialInstance(instance); | |
79 } | |
80 }; | |
81 | |
82 namespace pp { | |
83 /// Factory function called by the browser when the module is first loaded. | |
84 /// The browser keeps a singleton of this module. It calls the | |
85 /// CreateInstance() method on the object you return to make instances. There | |
86 /// is one instance per <embed> tag on the page. This is the main binding | |
87 /// point for your NaCl module with the browser. | |
88 Module* CreateModule() { | |
89 return new HelloTutorialModule(); | |
90 } | |
91 } // namespace pp | |
OLD | NEW |