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

Side by Side Diff: native_client_sdk/src/getting_started/part1/hello_tutorial.cc

Issue 23661005: [NaCl SDK] Add a very simple getting_started example at top-level. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add hello_tutorial.nmf 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 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. After the code is loaded,
10 /// CreateModule() is not called again.
11 ///
12 /// Once the 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 "ppapi/cpp/instance.h"
28 #include "ppapi/cpp/module.h"
29 #include "ppapi/cpp/var.h"
30
31 /// The Instance class. One of these exists for each instance of your NaCl
32 /// module on the web page. The browser will ask the Module object to create
33 /// a new Instance for each occurrence of the <embed> tag that has these
34 /// attributes:
35 /// type="application/x-pnacl"
36 /// src="hello_tutorial.nmf"
37 /// To communicate with the browser, you must override HandleMessage() to
38 /// receive messages from the browser, and use PostMessage() to send messages
39 /// back to the browser. Note that this interface is asynchronous.
40 class HelloTutorialInstance : public pp::Instance {
41 public:
42 /// The constructor creates the plugin-side instance.
43 /// @param[in] instance the handle to the browser-side plugin instance.
44 explicit HelloTutorialInstance(PP_Instance instance) : pp::Instance(instance)
45 {}
46 virtual ~HelloTutorialInstance() {}
47
48 /// Handler for messages coming in from the browser via postMessage(). The
49 /// @a var_message can contain anything: a JSON string; a string that encodes
50 /// method names and arguments; etc. For example, you could use
51 /// JSON.stringify in the browser to create a message that contains a method
52 /// name and some parameters, something like this:
53 /// var json_message = JSON.stringify({ "myMethod" : "3.14159" });
54 /// nacl_module.postMessage(json_message);
55 /// On receipt of this message in @a var_message, you could parse the JSON to
56 /// retrieve the method name, match it to a function call, and then call it
57 /// with the parameter.
58 /// @param[in] var_message The message posted by the browser.
59 virtual void HandleMessage(const pp::Var& var_message) {
60 // TODO(sdk_user): 1. Make this function handle the incoming message.
61 }
62 };
63
64 /// The Module class. The browser calls the CreateInstance() method to create
65 /// an instance of your NaCl module on the web page. The browser creates a new
66 /// instance for each <embed> tag with type="application/x-nacl".
67 class HelloTutorialModule : public pp::Module {
68 public:
69 HelloTutorialModule() : pp::Module() {}
70 virtual ~HelloTutorialModule() {}
71
72 /// Create and return a HelloTutorialInstance object.
73 /// @param[in] instance The browser-side instance.
74 /// @return the plugin-side instance.
75 virtual pp::Instance* CreateInstance(PP_Instance instance) {
76 return new HelloTutorialInstance(instance);
77 }
78 };
79
80 namespace pp {
81 /// Factory function called by the browser when the module is first loaded.
82 /// The browser keeps a singleton of this module. It calls the
83 /// CreateInstance() method on the object you return to make instances. There
84 /// is one instance per <embed> tag on the page. This is the main binding
85 /// point for your NaCl module with the browser.
86 Module* CreateModule() {
87 return new HelloTutorialModule();
88 }
89 } // namespace pp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698