OLD | NEW |
| (Empty) |
1 /* Copyright (c) 2012 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 | |
6 /** @file hello_world.c | |
7 * This example demonstrates loading, running and scripting a very simple | |
8 * NaCl module. | |
9 */ | |
10 #include <stdio.h> | |
11 #include <stdlib.h> | |
12 #include <string.h> | |
13 | |
14 #include "ppapi/c/pp_errors.h" | |
15 #include "ppapi/c/pp_module.h" | |
16 #include "ppapi/c/pp_var.h" | |
17 #include "ppapi/c/ppb.h" | |
18 #include "ppapi/c/ppb_instance.h" | |
19 #include "ppapi/c/ppb_messaging.h" | |
20 #include "ppapi/c/ppb_var.h" | |
21 #include "ppapi/c/ppp.h" | |
22 #include "ppapi/c/ppp_instance.h" | |
23 #include "ppapi/c/ppp_messaging.h" | |
24 | |
25 | |
26 static PPB_Messaging* ppb_messaging_interface = NULL; | |
27 static PPB_Var* ppb_var_interface = NULL; | |
28 | |
29 | |
30 /** | |
31 * Creates new string PP_Var from C string. The resulting object will be a | |
32 * refcounted string object. It will be AddRef()ed for the caller. When the | |
33 * caller is done with it, it should be Release()d. | |
34 * @param[in] str C string to be converted to PP_Var | |
35 * @return PP_Var containing string. | |
36 */ | |
37 static struct PP_Var CStrToVar(const char* str) { | |
38 if (ppb_var_interface != NULL) { | |
39 return ppb_var_interface->VarFromUtf8(str, strlen(str)); | |
40 } | |
41 return PP_MakeUndefined(); | |
42 } | |
43 | |
44 | |
45 /** | |
46 * Called when the NaCl module is instantiated on the web page. The identifier | |
47 * of the new instance will be passed in as the first argument (this value is | |
48 * generated by the browser and is an opaque handle). This is called for each | |
49 * instantiation of the NaCl module, which is each time the <embed> tag for | |
50 * this module is encountered. | |
51 * | |
52 * If this function reports a failure (by returning @a PP_FALSE), the NaCl | |
53 * module will be deleted and DidDestroy will be called. | |
54 * @param[in] instance The identifier of the new instance representing this | |
55 * NaCl module. | |
56 * @param[in] argc The number of arguments contained in @a argn and @a argv. | |
57 * @param[in] argn An array of argument names. These argument names are | |
58 * supplied in the <embed> tag, for example: | |
59 * <embed id="nacl_module" dimensions="2"> | |
60 * will produce two arguments, one named "id" and one named "dimensions". | |
61 * @param[in] argv An array of argument values. These are the values of the | |
62 * arguments listed in the <embed> tag. In the above example, there will | |
63 * be two elements in this array, "nacl_module" and "2". The indices of | |
64 * these values match the indices of the corresponding names in @a argn. | |
65 * @return @a PP_TRUE on success. | |
66 */ | |
67 static PP_Bool Instance_DidCreate(PP_Instance instance, | |
68 uint32_t argc, | |
69 const char* argn[], | |
70 const char* argv[]) { | |
71 ppb_messaging_interface->PostMessage(instance, | |
72 CStrToVar("Hello World: " TCNAME)); | |
73 return PP_TRUE; | |
74 } | |
75 | |
76 | |
77 /** | |
78 * Called when the NaCl module is destroyed. This will always be called, | |
79 * even if DidCreate returned failure. This routine should deallocate any data | |
80 * associated with the instance. | |
81 * @param[in] instance The identifier of the instance representing this NaCl | |
82 * module. | |
83 */ | |
84 static void Instance_DidDestroy(PP_Instance instance) { | |
85 } | |
86 | |
87 /** | |
88 * Called when the position, the size, or the clip rect of the element in the | |
89 * browser that corresponds to this NaCl module has changed. | |
90 * @param[in] instance The identifier of the instance representing this NaCl | |
91 * module. | |
92 * @param[in] position The location on the page of this NaCl module. This is | |
93 * relative to the top left corner of the viewport, which changes as the | |
94 * page is scrolled. | |
95 * @param[in] clip The visible region of the NaCl module. This is relative to | |
96 * the top left of the plugin's coordinate system (not the page). If the | |
97 * plugin is invisible, @a clip will be (0, 0, 0, 0). | |
98 */ | |
99 static void Instance_DidChangeView(PP_Instance instance, | |
100 PP_Resource view_resource) { | |
101 } | |
102 | |
103 /** | |
104 * Notification that the given NaCl module has gained or lost focus. | |
105 * Having focus means that keyboard events will be sent to the NaCl module | |
106 * represented by @a instance. A NaCl module's default condition is that it | |
107 * will not have focus. | |
108 * | |
109 * Note: clicks on NaCl modules will give focus only if you handle the | |
110 * click event. You signal if you handled it by returning @a true from | |
111 * HandleInputEvent. Otherwise the browser will bubble the event and give | |
112 * focus to the element on the page that actually did end up consuming it. | |
113 * If you're not getting focus, check to make sure you're returning true from | |
114 * the mouse click in HandleInputEvent. | |
115 * @param[in] instance The identifier of the instance representing this NaCl | |
116 * module. | |
117 * @param[in] has_focus Indicates whether this NaCl module gained or lost | |
118 * event focus. | |
119 */ | |
120 static void Instance_DidChangeFocus(PP_Instance instance, | |
121 PP_Bool has_focus) { | |
122 } | |
123 | |
124 /** | |
125 * Handler that gets called after a full-frame module is instantiated based on | |
126 * registered MIME types. This function is not called on NaCl modules. This | |
127 * function is essentially a place-holder for the required function pointer in | |
128 * the PPP_Instance structure. | |
129 * @param[in] instance The identifier of the instance representing this NaCl | |
130 * module. | |
131 * @param[in] url_loader A PP_Resource an open PPB_URLLoader instance. | |
132 * @return PP_FALSE. | |
133 */ | |
134 static PP_Bool Instance_HandleDocumentLoad(PP_Instance instance, | |
135 PP_Resource url_loader) { | |
136 /* NaCl modules do not need to handle the document load function. */ | |
137 return PP_FALSE; | |
138 } | |
139 | |
140 | |
141 | |
142 /** | |
143 * Entry points for the module. | |
144 * Initialize needed interfaces: PPB_Core, PPB_Messaging and PPB_Var. | |
145 * @param[in] a_module_id module ID | |
146 * @param[in] get_browser pointer to PPB_GetInterface | |
147 * @return PP_OK on success, any other value on failure. | |
148 */ | |
149 PP_EXPORT int32_t PPP_InitializeModule(PP_Module a_module_id, | |
150 PPB_GetInterface get_browser) { | |
151 ppb_messaging_interface = | |
152 (PPB_Messaging*)(get_browser(PPB_MESSAGING_INTERFACE)); | |
153 ppb_var_interface = (PPB_Var*)(get_browser(PPB_VAR_INTERFACE)); | |
154 return PP_OK; | |
155 } | |
156 | |
157 | |
158 /** | |
159 * Returns an interface pointer for the interface of the given name, or NULL | |
160 * if the interface is not supported. | |
161 * @param[in] interface_name name of the interface | |
162 * @return pointer to the interface | |
163 */ | |
164 PP_EXPORT const void* PPP_GetInterface(const char* interface_name) { | |
165 if (strcmp(interface_name, PPP_INSTANCE_INTERFACE) == 0) { | |
166 static PPP_Instance instance_interface = { | |
167 &Instance_DidCreate, | |
168 &Instance_DidDestroy, | |
169 &Instance_DidChangeView, | |
170 &Instance_DidChangeFocus, | |
171 &Instance_HandleDocumentLoad, | |
172 }; | |
173 return &instance_interface; | |
174 } | |
175 return NULL; | |
176 } | |
177 | |
178 | |
179 /** | |
180 * Called before the plugin module is unloaded. | |
181 */ | |
182 PP_EXPORT void PPP_ShutdownModule() { | |
183 } | |
OLD | NEW |