OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be found | |
3 // in the LICENSE file. | |
4 | |
5 /// @fileoverview This file contains code for a simple prototype of the webgtt | |
6 /// project. it demonstrates loading, running and scripting a simple nacl | |
7 /// module, which, when given the adjacency matrix of a graph by the browser, | |
8 /// returns a valid vertex coloring of the graph. to load the nacl module, the | |
9 /// browser first looks for the createmodule() factory method. it calls | |
10 /// createmodule() once to load the module code from the .nexe. after the .nexe | |
11 /// code is loaded, createmodule() is not called again. once the .nexe code is | |
12 /// loaded, the browser then calls the createinstance() method on the object | |
13 /// returned by createmodule(). it calls createinstance() each time it | |
14 /// encounters an <embed> tag that references the nacl module. | |
15 /// | |
16 /// @author ragad@google.com (Raga Gopalakrishnan) | |
17 | |
18 #include <cmath> | |
19 #include <string> | |
20 | |
21 #include "webgtt/parser.h" | |
22 #include "ppapi/cpp/instance.h" | |
23 #include "ppapi/cpp/module.h" | |
24 #include "ppapi/cpp/var.h" | |
25 | |
26 namespace webgtt { | |
27 | |
28 /// The Instance class. One of these exists for each instance of the NaCl module | |
29 /// on the web page. The browser will ask the Module object to create a new | |
30 /// Instance for each occurence of the <embed> tag that has these attributes: | |
31 /// type="application/x-nacl" | |
32 /// src="webgtt.nmf" | |
33 /// To communicate with the browser, the HandleMessage() method is overridden | |
34 /// for receiving messages from the browser. The PostMessage() method is used to | |
35 /// send messages back to the browser. This interface is entirely asynchronous. | |
36 class WebgttInstance : public pp::Instance { | |
37 public: | |
38 /// The constructor creates the plugin-side instance. | |
39 /// | |
40 /// @param[in] instance The handle to the browser-side plugin instance. | |
41 /// @constructor | |
42 explicit WebgttInstance(PP_Instance instance) : pp::Instance(instance) {} | |
43 virtual ~WebgttInstance() {} | |
44 | |
45 /// This function handles messages coming in from the browser via | |
46 /// postMessage(). | |
47 /// | |
48 /// The @a var_message can contain anything: a JSON string, a string that | |
49 /// encodes method names and arguments, etc. | |
50 /// | |
51 /// @param[in] var_message The message posted by the browser. | |
52 virtual void HandleMessage(const pp::Var& var_message) { | |
53 if (!var_message.is_string()) { | |
54 return; | |
55 } | |
56 std::string message = var_message.AsString(); | |
57 | |
58 Parser parse_message(message); | |
59 pp::Var var_reply; | |
60 if (parse_message.DecodeMessage()) { | |
61 var_reply = pp::Var(parse_message.GetResponse()); | |
62 } else { | |
63 var_reply = pp::Var("Error encountered while parsing the message!"); | |
64 } | |
65 PostMessage(var_reply); | |
66 } | |
67 | |
68 private: | |
69 /// This disallows usage of copy and assignment constructors. | |
70 WebgttInstance(const WebgttInstance&); | |
71 void operator=(const WebgttInstance&); | |
72 }; | |
73 | |
74 /// The Module class. The browser calls the CreateInstance() method to create | |
75 /// an instance of the NaCl module on the web page. The browser creates a new | |
76 /// instance for each <embed> tag with type="application/x-nacl". | |
77 class WebgttModule : public pp::Module { | |
78 public: | |
79 WebgttModule() : pp::Module() {} | |
80 virtual ~WebgttModule() {} | |
81 | |
82 /// This function creates and returns a WebgttInstance object. | |
83 /// | |
84 /// @param[in] instance The browser-side instance. | |
85 /// @return The plugin-side instance. | |
86 virtual pp::Instance* CreateInstance(PP_Instance instance) { | |
87 return new WebgttInstance(instance); | |
88 } | |
89 | |
90 private: | |
91 /// This disallows usage of copy and assignment constructors. | |
92 WebgttModule(const WebgttModule&); | |
93 void operator=(const WebgttModule&); | |
94 }; | |
95 | |
96 } // namespace webgtt | |
97 | |
98 namespace pp { | |
99 | |
100 /// This is the factory function called by the browser when the module is | |
101 /// first loaded. The browser keeps a singleton of this module. It calls the | |
102 /// CreateInstance() method on the object that is returned to make instances. | |
103 /// There is one instance per <embed> tag on the page. This is the main | |
104 /// binding point for the NaCl module with the browser. | |
105 Module* CreateModule() { | |
106 return new webgtt::WebgttModule(); | |
107 } | |
108 | |
109 } // namespace pp | |
OLD | NEW |