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