OLD | NEW |
| (Empty) |
1 /** @file $safeprojectname$.c | |
2 * This example demonstrates loading, running and scripting a very simple | |
3 * NaCl module. | |
4 */ | |
5 #include <stdlib.h> | |
6 #include <string.h> | |
7 #include "ppapi/c/pp_errors.h" | |
8 #include "ppapi/c/pp_module.h" | |
9 #include "ppapi/c/pp_var.h" | |
10 #include "ppapi/c/ppb.h" | |
11 #include "ppapi/c/ppb_instance.h" | |
12 #include "ppapi/c/ppb_messaging.h" | |
13 #include "ppapi/c/ppb_var.h" | |
14 #include "ppapi/c/ppp.h" | |
15 #include "ppapi/c/ppp_instance.h" | |
16 #include "ppapi/c/ppp_messaging.h" | |
17 | |
18 static PP_Module module_id = 0; | |
19 static struct PPB_Messaging* messaging_interface = NULL; | |
20 static struct PPB_Var* var_interface = NULL; | |
21 | |
22 /** | |
23 * Returns a mutable C string contained in the @a var or NULL if @a var is not | |
24 * string. This makes a copy of the string in the @a var and adds a NULL | |
25 * terminator. Note that VarToUtf8() does not guarantee the NULL terminator on | |
26 * the returned string. See the comments for VarToUtf8() in ppapi/c/ppb_var.h | |
27 * for more info. The caller is responsible for freeing the returned memory. | |
28 * @param[in] var PP_Var containing string. | |
29 * @return a mutable C string representation of @a var. | |
30 * @note The caller is responsible for freeing the returned string. | |
31 */ | |
32 /* TODO(sdk_user): 2. Uncomment this when you need it. It is commented out so | |
33 * that the compiler doesn't complain about unused functions. | |
34 */ | |
35 #if 0 | |
36 static char* AllocateCStrFromVar(struct PP_Var var) { | |
37 uint32_t len = 0; | |
38 if (var_interface != NULL) { | |
39 const char* var_c_str = var_interface->VarToUtf8(var, &len); | |
40 if (len > 0) { | |
41 char* c_str = (char*)malloc(len + 1); | |
42 memcpy(c_str, var_c_str, len); | |
43 c_str[len] = '\0'; | |
44 return c_str; | |
45 } | |
46 } | |
47 return NULL; | |
48 } | |
49 #endif | |
50 | |
51 /** | |
52 * Creates a new string PP_Var from C string. The resulting object will be a | |
53 * refcounted string object. It will be AddRef()ed for the caller. When the | |
54 * caller is done with it, it should be Release()d. | |
55 * @param[in] str C string to be converted to PP_Var | |
56 * @return PP_Var containing string. | |
57 */ | |
58 /* TODO(sdk_user): 3. Uncomment this when you need it. It is commented out so | |
59 * that the compiler doesn't complain about unused functions. | |
60 */ | |
61 #if 0 | |
62 static struct PP_Var AllocateVarFromCStr(const char* str) { | |
63 if (var_interface != NULL) | |
64 return var_interface->VarFromUtf8(module_id, str, strlen(str)); | |
65 return PP_MakeUndefined(); | |
66 } | |
67 #endif | |
68 | |
69 /** | |
70 * Called when the NaCl module is instantiated on the web page. The identifier | |
71 * of the new instance will be passed in as the first argument (this value is | |
72 * generated by the browser and is an opaque handle). This is called for each | |
73 * instantiation of the NaCl module, which is each time the <embed> tag for | |
74 * this module is encountered. | |
75 * | |
76 * If this function reports a failure (by returning @a PP_FALSE), the NaCl | |
77 * module will be deleted and DidDestroy will be called. | |
78 * @param[in] instance The identifier of the new instance representing this | |
79 * NaCl module. | |
80 * @param[in] argc The number of arguments contained in @a argn and @a argv. | |
81 * @param[in] argn An array of argument names. These argument names are | |
82 * supplied in the <embed> tag, for example: | |
83 * <embed id="nacl_module" dimensions="2"> | |
84 * will produce two arguments, one named "id" and one named "dimensions". | |
85 * @param[in] argv An array of argument values. These are the values of the | |
86 * arguments listed in the <embed> tag. In the above example, there will | |
87 * be two elements in this array, "nacl_module" and "2". The indices of | |
88 * these values match the indices of the corresponding names in @a argn. | |
89 * @return @a PP_TRUE on success. | |
90 */ | |
91 static PP_Bool Instance_DidCreate(PP_Instance instance, | |
92 uint32_t argc, | |
93 const char* argn[], | |
94 const char* argv[]) { | |
95 return PP_TRUE; | |
96 } | |
97 | |
98 /** | |
99 * Called when the NaCl module is destroyed. This will always be called, | |
100 * even if DidCreate returned failure. This routine should deallocate any data | |
101 * associated with the instance. | |
102 * @param[in] instance The identifier of the instance representing this NaCl | |
103 * module. | |
104 */ | |
105 static void Instance_DidDestroy(PP_Instance instance) { | |
106 } | |
107 | |
108 /** | |
109 * Called when the position, the size, or the clip rect of the element in the | |
110 * browser that corresponds to this NaCl module has changed. | |
111 * @param[in] instance The identifier of the instance representing this NaCl | |
112 * module. | |
113 * @param[in] position The location on the page of this NaCl module. This is | |
114 * relative to the top left corner of the viewport, which changes as the | |
115 * page is scrolled. | |
116 * @param[in] clip The visible region of the NaCl module. This is relative to | |
117 * the top left of the plugin's coordinate system (not the page). If the | |
118 * plugin is invisible, @a clip will be (0, 0, 0, 0). | |
119 */ | |
120 static void Instance_DidChangeView(PP_Instance instance, | |
121 const struct PP_Rect* position, | |
122 const struct PP_Rect* clip) { | |
123 } | |
124 | |
125 /** | |
126 * Notification that the given NaCl module has gained or lost focus. | |
127 * Having focus means that keyboard events will be sent to the NaCl module | |
128 * represented by @a instance. A NaCl module's default condition is that it | |
129 * will not have focus. | |
130 * | |
131 * Note: clicks on NaCl modules will give focus only if you handle the | |
132 * click event. You signal if you handled it by returning @a true from | |
133 * HandleInputEvent. Otherwise the browser will bubble the event and give | |
134 * focus to the element on the page that actually did end up consuming it. | |
135 * If you're not getting focus, check to make sure you're returning true from | |
136 * the mouse click in HandleInputEvent. | |
137 * @param[in] instance The identifier of the instance representing this NaCl | |
138 * module. | |
139 * @param[in] has_focus Indicates whether this NaCl module gained or lost | |
140 * event focus. | |
141 */ | |
142 static void Instance_DidChangeFocus(PP_Instance instance, | |
143 PP_Bool has_focus) { | |
144 } | |
145 | |
146 /** | |
147 * Handler that gets called after a full-frame module is instantiated based on | |
148 * registered MIME types. This function is not called on NaCl modules. This | |
149 * function is essentially a place-holder for the required function pointer in | |
150 * the PPP_Instance structure. | |
151 * @param[in] instance The identifier of the instance representing this NaCl | |
152 * module. | |
153 * @param[in] url_loader A PP_Resource an open PPB_URLLoader instance. | |
154 * @return PP_FALSE. | |
155 */ | |
156 static PP_Bool Instance_HandleDocumentLoad(PP_Instance instance, | |
157 PP_Resource url_loader) { | |
158 /* NaCl modules do not need to handle the document load function. */ | |
159 return PP_FALSE; | |
160 } | |
161 | |
162 | |
163 /** | |
164 * Handler for messages coming in from the browser via postMessage. The | |
165 * @a var_message can contain anything: a JSON string; a string that encodes | |
166 * method names and arguments; etc. For example, you could use JSON.stringify | |
167 * in the browser to create a message that contains a method name and some | |
168 * parameters, something like this: | |
169 * var json_message = JSON.stringify({ "myMethod" : "3.14159" }); | |
170 * nacl_module.postMessage(json_message); | |
171 * On receipt of this message in @a var_message, you could parse the JSON to | |
172 * retrieve the method name, match it to a function call, and then call it with | |
173 * the parameter. | |
174 * @param[in] instance The instance ID. | |
175 * @param[in] message The contents, copied by value, of the message sent from | |
176 * browser via postMessage. | |
177 */ | |
178 void Messaging_HandleMessage(PP_Instance instance, struct PP_Var var_message) { | |
179 /* TODO(sdk_user): 1. Make this function handle the incoming message. */ | |
180 } | |
181 | |
182 /** | |
183 * Entry points for the module. | |
184 * Initialize instance interface and scriptable object class. | |
185 * @param[in] a_module_id Module ID | |
186 * @param[in] get_browser_interface Pointer to PPB_GetInterface | |
187 * @return PP_OK on success, any other value on failure. | |
188 */ | |
189 PP_EXPORT int32_t PPP_InitializeModule(PP_Module a_module_id, | |
190 PPB_GetInterface get_browser_interface) { | |
191 module_id = a_module_id; | |
192 var_interface = (struct PPB_Var*)(get_browser_interface(PPB_VAR_INTERFACE)); | |
193 messaging_interface = | |
194 (struct PPB_Messaging*)(get_browser_interface(PPB_MESSAGING_INTERFACE)); | |
195 return PP_OK; | |
196 } | |
197 | |
198 /** | |
199 * Returns an interface pointer for the interface of the given name, or NULL | |
200 * if the interface is not supported. | |
201 * @param[in] interface_name name of the interface | |
202 * @return pointer to the interface | |
203 */ | |
204 PP_EXPORT const void* PPP_GetInterface(const char* interface_name) { | |
205 if (strcmp(interface_name, PPP_INSTANCE_INTERFACE) == 0) { | |
206 static struct PPP_Instance instance_interface = { | |
207 &Instance_DidCreate, | |
208 &Instance_DidDestroy, | |
209 &Instance_DidChangeView, | |
210 &Instance_DidChangeFocus, | |
211 &Instance_HandleDocumentLoad | |
212 }; | |
213 return &instance_interface; | |
214 } else if (strcmp(interface_name, PPP_MESSAGING_INTERFACE) == 0) { | |
215 static struct PPP_Messaging messaging_interface = { | |
216 &Messaging_HandleMessage | |
217 }; | |
218 return &messaging_interface; | |
219 } | |
220 return NULL; | |
221 } | |
222 | |
223 /** | |
224 * Called before the plugin module is unloaded. | |
225 */ | |
226 PP_EXPORT void PPP_ShutdownModule() { | |
227 } | |
OLD | NEW |