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 | |
3 // found in the LICENSE file. | |
4 | |
5 // Copy of the stub test from ppapi/examples/stub/stub.c | |
6 // This is the simplest possible C Pepper plugin that does nothing. If you're | |
7 // using C++, you will want to look at stub.cc which uses the more convenient | |
8 // C++ wrappers. | |
9 | |
10 #include <stdio.h> | |
11 #include <string.h> | |
12 #include <inttypes.h> | |
13 #include <nacl/nacl_inttypes.h> | |
14 #include <map> | |
15 #include <string> | |
16 #include <utility> | |
17 #include "ppapi/c/dev/ppb_var_deprecated.h" | |
18 #include "ppapi/c/dev/ppp_class_deprecated.h" | |
19 #include "ppapi/c/pp_errors.h" | |
20 #include "ppapi/c/pp_module.h" | |
21 #include "ppapi/c/pp_resource.h" | |
22 #include "ppapi/c/ppb.h" | |
23 #include "ppapi/c/ppp.h" | |
24 #include "ppapi/c/ppp_instance.h" | |
25 | |
26 | |
27 namespace { | |
28 | |
29 PP_Module g_module_id; | |
30 PPB_GetInterface g_get_browser_interface = NULL; | |
31 const PPB_Var_Deprecated* g_var_interface = NULL; | |
32 | |
33 const PP_Var kGetFailed = PP_MakeNull(); | |
34 const PP_Var kSetFailed = PP_MakeBool(PP_FALSE); | |
35 const PP_Var kCallFailed = PP_MakeBool(PP_FALSE); | |
36 | |
37 const char* const kPropUndefined = "propUndefined"; | |
38 const char* const kPropNull = "propNull"; | |
39 const char* const kPropBool = "propBool"; | |
40 const char* const kPropInt32 = "propInt32"; | |
41 const char* const kPropDouble = "propDouble"; | |
42 const char* const kPropString = "propString"; | |
43 const char* const kPropObject = "propObject"; | |
44 const char* const kPropWindow = "propWindow"; | |
45 | |
46 // Each of these methods takes 1 parameter, which has to be the same type as | |
47 // the type named in the method. E.g. "methodInt32" takes a pp_Var that | |
48 // must be int32 type. | |
49 const char* const kMethodUndefined = "methodUndefined"; | |
50 const char* const kMethodNull = "methodNull"; | |
51 const char* const kMethodBool = "methodBool"; | |
52 const char* const kMethodInt32 = "methodInt32"; | |
53 const char* const kMethodDouble = "methodDouble"; | |
54 const char* const kMethodString = "methodString"; | |
55 const char* const kMethodObject = "methodObject"; | |
56 | |
57 // Each of these methods takes 2 parameters, each has to be the same type as | |
58 // the type named in the method. E.g. "methodInt32With2Args" takes two pp_Vars | |
59 // that must both be int32 type. | |
60 const char* const kMethodUndefinedWith2Args = "methodUndefinedWith2Args"; | |
61 const char* const kMethodNullWith2Args = "methodNullWith2Args"; | |
62 const char* const kMethodBoolWith2Args = "methodBoolWith2Args"; | |
63 const char* const kMethodInt32With2Args = "methodInt32With2Args"; | |
64 const char* const kMethodDoubleWith2Args = "methodDoubleWith2Args"; | |
65 const char* const kMethodStringWith2Args = "methodStringWith2Args"; | |
66 const char* const kMethodObjectWith2Args = "methodObjectWith2Args"; | |
67 | |
68 // This method takes an object as its single argument (should be the |window| | |
69 // object in the browser), and returns the value of the |location| property | |
70 // on that object. In the test, this should produce the same object as | |
71 // the JavaScript window.location. | |
72 const char* const kMethodWindow = "windowLocation"; | |
73 | |
74 // This method takes no parameters and returns the string declared by | |
75 // |kHelloWorld|, below. | |
76 const char* const kMethodHelloWorld = "helloWorld"; | |
77 | |
78 const char* const kHelloWorld = "hello, world"; | |
79 | |
80 bool IsIntegral(PP_Var var) { | |
81 // JavaScript sometimes passes doubles for integers. | |
82 return (var.type == PP_VARTYPE_INT32) || | |
83 ((var.type == PP_VARTYPE_DOUBLE) && | |
84 (var.value.as_double == (int) var.value.as_double)); | |
85 } | |
86 | |
87 // A printer for PP_Vars. | |
88 void PrintPpVar(PP_Var var) { | |
89 printf("PP_Var("); | |
90 switch (var.type) { | |
91 case PP_VARTYPE_UNDEFINED: | |
92 printf("undefined"); | |
93 break; | |
94 case PP_VARTYPE_NULL: | |
95 printf("null"); | |
96 break; | |
97 case PP_VARTYPE_BOOL: | |
98 printf("bool(%s)", var.value.as_bool ? "true" : "false"); | |
99 break; | |
100 case PP_VARTYPE_INT32: | |
101 printf("int32(%d)", static_cast<int>(var.value.as_int)); | |
102 break; | |
103 case PP_VARTYPE_DOUBLE: | |
104 printf("double(%f)", var.value.as_double); | |
105 break; | |
106 case PP_VARTYPE_STRING: | |
107 /* SCOPE */ { | |
108 uint32_t len; | |
109 const char* str = g_var_interface->VarToUtf8(var, &len); | |
110 printf("string(\"%*s\")", static_cast<int>(len), str); | |
111 } | |
112 break; | |
113 case PP_VARTYPE_OBJECT: | |
114 printf("object(%d)", static_cast<int>(var.value.as_id)); | |
115 break; | |
116 default: | |
117 printf("bad_type(%d)", var.type); | |
118 break; | |
119 } | |
120 printf(")"); | |
121 } | |
122 | |
123 class TestObject { | |
124 public: | |
125 TestObject(); | |
126 | |
127 bool HasProperty(PP_Var name); | |
128 bool HasMethod(PP_Var name); | |
129 PP_Var GetProperty(PP_Var name); | |
130 void RemoveProperty(PP_Var name); | |
131 bool SetProperty(PP_Var name, PP_Var value); | |
132 PP_Var Call(PP_Var name, uint32_t argc, PP_Var* argv, PP_Var* exception); | |
133 | |
134 private: | |
135 // Getter/setter/function for PP_VARTYPE_UNDEFINED. | |
136 PP_Var prop_undefined() const { return prop_undefined_; } | |
137 bool set_prop_undefined(PP_Var prop_undefined); | |
138 PP_Var method_undefined(uint32_t argc, PP_Var* argv, PP_Var* exception); | |
139 PP_Var method_undefined_2_args(uint32_t argc, | |
140 PP_Var* argv, | |
141 PP_Var* exception); | |
142 // Getter/setter/function for PP_VARTYPE_NULL. | |
143 PP_Var prop_null() const { return prop_null_; } | |
144 bool set_prop_null(PP_Var prop_null); | |
145 PP_Var method_null(uint32_t argc, PP_Var* argv, PP_Var* exception); | |
146 PP_Var method_null_2_args(uint32_t argc, PP_Var* argv, PP_Var* exception); | |
147 // Getter/setter/function for PP_VARTYPE_BOOL. | |
148 PP_Var prop_bool() const { return prop_bool_; } | |
149 bool set_prop_bool(PP_Var prop_bool); | |
150 PP_Var method_bool(uint32_t argc, PP_Var* argv, PP_Var* exception); | |
151 PP_Var method_bool_2_args(uint32_t argc, PP_Var* argv, PP_Var* exception); | |
152 // Getter/setter/function for PP_VARTYPE_INT32. | |
153 PP_Var prop_int32() const { return prop_int32_; } | |
154 bool set_prop_int32(PP_Var prop_int32); | |
155 PP_Var method_int32(uint32_t argc, PP_Var* argv, PP_Var* exception); | |
156 PP_Var method_int32_2_args(uint32_t argc, PP_Var* argv, PP_Var* exception); | |
157 // Getter/setter/function for PP_VARTYPE_DOUBLE. | |
158 PP_Var prop_double() const { return prop_double_; } | |
159 bool set_prop_double(PP_Var prop_double); | |
160 PP_Var method_double(uint32_t argc, PP_Var* argv, PP_Var* exception); | |
161 PP_Var method_double_2_args(uint32_t argc, PP_Var* argv, PP_Var* exception); | |
162 // Getter/setter/function for PP_VARTYPE_STRING. | |
163 PP_Var prop_string() const { return prop_string_; } | |
164 bool set_prop_string(PP_Var prop_string); | |
165 PP_Var method_string(uint32_t argc, PP_Var* argv, PP_Var* exception); | |
166 PP_Var method_string_2_args(uint32_t argc, PP_Var* argv, PP_Var* exception); | |
167 // Getter/setter/function for PP_VARTYPE_OBJECT. | |
168 PP_Var prop_object() const { return prop_object_; } | |
169 bool set_prop_object(PP_Var prop_object); | |
170 PP_Var method_object(uint32_t argc, PP_Var* argv, PP_Var* exception); | |
171 PP_Var method_object_2_args(uint32_t argc, PP_Var* argv, PP_Var* exception); | |
172 // Test of scripting the window object. | |
173 PP_Var window_location(uint32_t argc, PP_Var* argv, PP_Var* exception); | |
174 // Test that calls a method on a JS object which is exported from the NaCl | |
175 // module. | |
176 PP_Var hello_world(uint32_t argc, PP_Var* argv, PP_Var* exception); | |
177 | |
178 PP_Var module_ready() const { return prop_module_ready_; } | |
179 bool set_module_ready(PP_Var module_ready); | |
180 | |
181 // Properties. | |
182 typedef PP_Var (TestObject::*Getter)() const; | |
183 typedef bool (TestObject::*Setter)(PP_Var val); | |
184 typedef std::pair<Getter, Setter> Property; | |
185 typedef std::map<std::string, Property> PropertyMap; | |
186 // Methods. | |
187 typedef PP_Var (TestObject::*Method)(uint32_t argc, | |
188 PP_Var* argv, | |
189 PP_Var* exception); | |
190 typedef std::map<std::string, Method> MethodMap; | |
191 | |
192 PP_Var prop_undefined_; | |
193 PP_Var prop_null_; | |
194 PP_Var prop_bool_; | |
195 PP_Var prop_int32_; | |
196 PP_Var prop_double_; | |
197 PP_Var prop_string_; | |
198 PP_Var prop_object_; | |
199 PP_Var prop_module_ready_; | |
200 | |
201 static PropertyMap property_map; | |
202 static MethodMap method_map; | |
203 static bool class_is_initialized; | |
204 }; | |
205 | |
206 TestObject::PropertyMap TestObject::property_map; | |
207 TestObject::MethodMap TestObject::method_map; | |
208 bool TestObject::class_is_initialized = false; | |
209 | |
210 TestObject::TestObject() | |
211 : prop_undefined_(PP_MakeNull()), | |
212 prop_null_(PP_MakeNull()), | |
213 prop_int32_(PP_MakeNull()), | |
214 prop_double_(PP_MakeNull()), | |
215 prop_string_(PP_MakeNull()), | |
216 prop_object_(PP_MakeNull()), | |
217 prop_module_ready_(PP_MakeBool(PP_TRUE)) { | |
218 if (class_is_initialized) { | |
219 return; | |
220 } | |
221 // Property map initialization. | |
222 property_map[kPropUndefined] = | |
223 Property(&TestObject::prop_undefined, &TestObject::set_prop_undefined); | |
224 property_map[kPropNull] = | |
225 Property(&TestObject::prop_null, &TestObject::set_prop_null); | |
226 property_map[kPropBool] = | |
227 Property(&TestObject::prop_bool, &TestObject::set_prop_bool); | |
228 property_map[kPropInt32] = | |
229 Property(&TestObject::prop_int32, &TestObject::set_prop_int32); | |
230 property_map[kPropDouble] = | |
231 Property(&TestObject::prop_double, &TestObject::set_prop_double); | |
232 property_map[kPropString] = | |
233 Property(&TestObject::prop_string, &TestObject::set_prop_string); | |
234 property_map[kPropObject] = | |
235 Property(&TestObject::prop_object, &TestObject::set_prop_object); | |
236 // Method map initialization. | |
237 method_map[kMethodUndefined] = &TestObject::method_undefined; | |
238 method_map[kMethodNull] = &TestObject::method_null; | |
239 method_map[kMethodBool] = &TestObject::method_bool; | |
240 method_map[kMethodInt32] = &TestObject::method_int32; | |
241 method_map[kMethodDouble] = &TestObject::method_double; | |
242 method_map[kMethodString] = &TestObject::method_string; | |
243 method_map[kMethodObject] = &TestObject::method_object; | |
244 | |
245 method_map[kMethodUndefinedWith2Args] = &TestObject::method_undefined_2_args; | |
246 method_map[kMethodNullWith2Args] = &TestObject::method_null_2_args; | |
247 method_map[kMethodBoolWith2Args] = &TestObject::method_bool_2_args; | |
248 method_map[kMethodInt32With2Args] = &TestObject::method_int32_2_args; | |
249 method_map[kMethodDoubleWith2Args] = &TestObject::method_double_2_args; | |
250 method_map[kMethodStringWith2Args] = &TestObject::method_string_2_args; | |
251 method_map[kMethodObjectWith2Args] = &TestObject::method_object_2_args; | |
252 method_map[kMethodWindow] = &TestObject::window_location; | |
253 method_map[kMethodHelloWorld] = &TestObject::hello_world; | |
254 class_is_initialized = true; | |
255 } | |
256 | |
257 bool TestObject::HasProperty(PP_Var name) { | |
258 printf("basic_object: HasProperty(%p, ", reinterpret_cast<void*>(this)); | |
259 PrintPpVar(name); | |
260 printf(")\n"); | |
261 uint32_t len; | |
262 const char* str = g_var_interface->VarToUtf8(name, &len); | |
263 if (str == NULL) return false; | |
264 std::string name_str(str, len); | |
265 bool retval = property_map.find(name_str) != property_map.end(); | |
266 printf(" HasProperty done\n"); | |
267 return retval; | |
268 } | |
269 | |
270 PP_Var TestObject::GetProperty(PP_Var name) { | |
271 printf("basic_object: GetProperty(%p, ", reinterpret_cast<void*>(this)); | |
272 PrintPpVar(name); | |
273 printf(")\n"); | |
274 uint32_t len; | |
275 const char* str = g_var_interface->VarToUtf8(name, &len); | |
276 if (str == NULL) return kGetFailed; | |
277 std::string name_str(str, len); | |
278 if (property_map.find(name_str) == property_map.end()) { | |
279 return kGetFailed; | |
280 } | |
281 PP_Var retval = (this->*property_map[name_str].first)(); | |
282 printf(" GetProperty done\n"); | |
283 return retval; | |
284 } | |
285 | |
286 bool TestObject::SetProperty(PP_Var name, PP_Var value) { | |
287 printf("basic_object: SetProperty(%p, ", reinterpret_cast<void*>(this)); | |
288 PrintPpVar(name); | |
289 printf(", "); | |
290 PrintPpVar(value); | |
291 printf(")\n"); | |
292 uint32_t len; | |
293 const char* str = g_var_interface->VarToUtf8(name, &len); | |
294 if (str == NULL) return false; | |
295 std::string name_str(str, len); | |
296 if (property_map.find(name_str) == property_map.end()) { | |
297 return false; | |
298 } | |
299 return (this->*property_map[name_str].second)(value); | |
300 } | |
301 | |
302 void TestObject::RemoveProperty(PP_Var name) { | |
303 printf("basic_object: RemoveProperty(%p, ", reinterpret_cast<void*>(this)); | |
304 PrintPpVar(name); | |
305 printf(")\n"); | |
306 uint32_t len; | |
307 const char* str = g_var_interface->VarToUtf8(name, &len); | |
308 if (str == NULL) return; | |
309 std::string name_str(str, len); | |
310 PropertyMap::iterator iter = property_map.find(name_str); | |
311 if (iter != property_map.end()) { | |
312 property_map.erase(iter); | |
313 } | |
314 } | |
315 | |
316 bool TestObject::HasMethod(PP_Var name) { | |
317 printf("basic_object: HasMethod(%p, ", reinterpret_cast<void*>(this)); | |
318 PrintPpVar(name); | |
319 printf(")\n"); | |
320 uint32_t len; | |
321 const char* str = g_var_interface->VarToUtf8(name, &len); | |
322 if (str == NULL) return false; | |
323 std::string name_str(str, len); | |
324 return method_map.find(name_str) != method_map.end(); | |
325 } | |
326 | |
327 PP_Var TestObject::Call(PP_Var name, | |
328 uint32_t argc, | |
329 PP_Var* argv, | |
330 PP_Var* exception) { | |
331 printf("basic_object: Call(%p, ", reinterpret_cast<void*>(this)); | |
332 PrintPpVar(name); | |
333 printf(", argc=%d, [", static_cast<int>(argc)); | |
334 for (uint32_t i = 0; i < argc; ++i) { | |
335 if (i != 0) printf(", "); | |
336 PrintPpVar(argv[i]); | |
337 } | |
338 printf("])\n"); | |
339 uint32_t len; | |
340 const char* str = g_var_interface->VarToUtf8(name, &len); | |
341 if (str == NULL) { | |
342 *exception = kCallFailed; | |
343 return kCallFailed; | |
344 } | |
345 std::string name_str(str, len); | |
346 return (this->*method_map[name_str])(argc, argv, exception); | |
347 } | |
348 | |
349 bool TestObject::set_prop_undefined(PP_Var prop_undefined) { | |
350 if (prop_undefined.type != PP_VARTYPE_UNDEFINED) return false; | |
351 prop_undefined_ = prop_undefined; | |
352 return true; | |
353 } | |
354 | |
355 bool TestObject::set_prop_null(PP_Var prop_null) { | |
356 if (prop_null.type != PP_VARTYPE_NULL) return false; | |
357 prop_null_ = prop_null; | |
358 return true; | |
359 } | |
360 | |
361 bool TestObject::set_prop_bool(PP_Var prop_bool) { | |
362 if (prop_bool.type != PP_VARTYPE_BOOL) return false; | |
363 prop_bool_ = prop_bool; | |
364 return true; | |
365 } | |
366 | |
367 bool TestObject::set_prop_int32(PP_Var prop_int32) { | |
368 if (!IsIntegral(prop_int32)) return false; | |
369 prop_int32_ = prop_int32; | |
370 return true; | |
371 } | |
372 | |
373 bool TestObject::set_prop_double(PP_Var prop_double) { | |
374 if (prop_double.type != PP_VARTYPE_DOUBLE) return false; | |
375 prop_double_ = prop_double; | |
376 return true; | |
377 } | |
378 | |
379 bool TestObject::set_prop_string(PP_Var prop_string) { | |
380 if (prop_string.type != PP_VARTYPE_STRING) return false; | |
381 prop_string_ = prop_string; | |
382 return true; | |
383 } | |
384 | |
385 bool TestObject::set_prop_object(PP_Var prop_object) { | |
386 if (prop_object.type != PP_VARTYPE_OBJECT) return false; | |
387 prop_object_ = prop_object; | |
388 return true; | |
389 } | |
390 | |
391 bool TestObject::set_module_ready(PP_Var prop_string) { | |
392 // This is a read-only property. | |
393 return false; | |
394 } | |
395 | |
396 PP_Var TestObject::method_undefined(uint32_t argc, | |
397 PP_Var* argv, | |
398 PP_Var* exception) { | |
399 if (argc != 1 || argv[0].type != PP_VARTYPE_UNDEFINED) { | |
400 *exception = kCallFailed; | |
401 } | |
402 return argv[0]; | |
403 } | |
404 | |
405 PP_Var TestObject::method_null(uint32_t argc, | |
406 PP_Var* argv, | |
407 PP_Var* exception) { | |
408 if (argc != 1 || argv[0].type != PP_VARTYPE_NULL) { | |
409 *exception = kCallFailed; | |
410 } | |
411 return argv[0]; | |
412 } | |
413 | |
414 PP_Var TestObject::method_bool(uint32_t argc, | |
415 PP_Var* argv, | |
416 PP_Var* exception) { | |
417 if (argc != 1 || argv[0].type != PP_VARTYPE_BOOL) { | |
418 *exception = kCallFailed; | |
419 } | |
420 return argv[0]; | |
421 } | |
422 | |
423 PP_Var TestObject::method_int32(uint32_t argc, | |
424 PP_Var* argv, | |
425 PP_Var* exception) { | |
426 if (argc != 1 || !IsIntegral(argv[0])) { | |
427 *exception = kCallFailed; | |
428 } | |
429 return argv[0]; | |
430 } | |
431 | |
432 PP_Var TestObject::method_double(uint32_t argc, | |
433 PP_Var* argv, | |
434 PP_Var* exception) { | |
435 if (argc != 1 || argv[0].type != PP_VARTYPE_DOUBLE) { | |
436 *exception = kCallFailed; | |
437 } | |
438 return argv[0]; | |
439 } | |
440 | |
441 PP_Var TestObject::method_string(uint32_t argc, | |
442 PP_Var* argv, | |
443 PP_Var* exception) { | |
444 if (argc != 1 || argv[0].type != PP_VARTYPE_STRING) { | |
445 *exception = kCallFailed; | |
446 } | |
447 return argv[0]; | |
448 } | |
449 | |
450 PP_Var TestObject::method_object(uint32_t argc, | |
451 PP_Var* argv, | |
452 PP_Var* exception) { | |
453 if (argc != 1 || argv[0].type != PP_VARTYPE_OBJECT) { | |
454 *exception = kCallFailed; | |
455 return kCallFailed; | |
456 } | |
457 return argv[0]; | |
458 } | |
459 | |
460 PP_Var TestObject::method_undefined_2_args(uint32_t argc, | |
461 PP_Var* argv, | |
462 PP_Var* exception) { | |
463 if (argc != 2 || | |
464 argv[0].type != PP_VARTYPE_UNDEFINED || | |
465 argv[1].type != PP_VARTYPE_UNDEFINED) { | |
466 *exception = kCallFailed; | |
467 } | |
468 return argv[0]; | |
469 } | |
470 | |
471 PP_Var TestObject::method_null_2_args(uint32_t argc, | |
472 PP_Var* argv, | |
473 PP_Var* exception) { | |
474 if (argc != 2 || | |
475 argv[0].type != PP_VARTYPE_NULL || | |
476 argv[1].type != PP_VARTYPE_NULL) { | |
477 *exception = kCallFailed; | |
478 } | |
479 return argv[0]; | |
480 } | |
481 | |
482 PP_Var TestObject::method_bool_2_args(uint32_t argc, | |
483 PP_Var* argv, | |
484 PP_Var* exception) { | |
485 if (argc != 2 || | |
486 argv[0].type != PP_VARTYPE_BOOL || | |
487 argv[1].type != PP_VARTYPE_BOOL) { | |
488 *exception = kCallFailed; | |
489 } | |
490 return argv[0]; | |
491 } | |
492 | |
493 PP_Var TestObject::method_int32_2_args(uint32_t argc, | |
494 PP_Var* argv, | |
495 PP_Var* exception) { | |
496 if (argc != 2 || !IsIntegral(argv[0]) || !IsIntegral(argv[1])) { | |
497 *exception = kCallFailed; | |
498 } | |
499 return argv[0]; | |
500 } | |
501 | |
502 PP_Var TestObject::method_double_2_args(uint32_t argc, | |
503 PP_Var* argv, | |
504 PP_Var* exception) { | |
505 if (argc != 2 || | |
506 argv[0].type != PP_VARTYPE_DOUBLE || | |
507 argv[1].type != PP_VARTYPE_DOUBLE) { | |
508 *exception = kCallFailed; | |
509 } | |
510 return argv[0]; | |
511 } | |
512 | |
513 PP_Var TestObject::method_string_2_args(uint32_t argc, | |
514 PP_Var* argv, | |
515 PP_Var* exception) { | |
516 if (argc != 2 || | |
517 argv[0].type != PP_VARTYPE_STRING || | |
518 argv[1].type != PP_VARTYPE_STRING) { | |
519 *exception = kCallFailed; | |
520 } | |
521 return argv[0]; | |
522 } | |
523 | |
524 PP_Var TestObject::method_object_2_args(uint32_t argc, | |
525 PP_Var* argv, | |
526 PP_Var* exception) { | |
527 if (argc != 2 || | |
528 argv[0].type != PP_VARTYPE_OBJECT || | |
529 argv[1].type != PP_VARTYPE_OBJECT) { | |
530 *exception = kCallFailed; | |
531 return kCallFailed; | |
532 } | |
533 return argv[0]; | |
534 } | |
535 | |
536 // A function to be called with the JavaScript |window| object. | |
537 // This test basically checks that we can access browser object proxies. | |
538 PP_Var TestObject::window_location(uint32_t argc, | |
539 PP_Var* argv, | |
540 PP_Var* exception) { | |
541 if (argc != 1 || | |
542 argv[0].type != PP_VARTYPE_OBJECT) { | |
543 *exception = kCallFailed; | |
544 return kCallFailed; | |
545 } | |
546 // Get the location property from the passed in object. | |
547 const char kLocation[] = "location"; | |
548 const uint32_t kLocationLength = static_cast<uint32_t>(strlen(kLocation)); | |
549 PP_Var location_name = | |
550 g_var_interface->VarFromUtf8(g_module_id, kLocation, kLocationLength); | |
551 PP_Var location = | |
552 g_var_interface->GetProperty(argv[0], location_name, exception); | |
553 if (location.type != PP_VARTYPE_OBJECT || | |
554 exception->type != PP_VARTYPE_UNDEFINED) { | |
555 *exception = kCallFailed; | |
556 return kCallFailed; | |
557 } | |
558 // Get the href property on the returned object. | |
559 const char kHref[] = "href"; | |
560 const uint32_t kHrefLength = static_cast<uint32_t>(strlen(kHref)); | |
561 PP_Var href_name = | |
562 g_var_interface->VarFromUtf8(g_module_id, kHref, kHrefLength); | |
563 PP_Var href = g_var_interface->GetProperty(location, href_name, exception); | |
564 printf("window.location.href = "); | |
565 PrintPpVar(href); | |
566 printf("\n"); | |
567 if (href.type != PP_VARTYPE_STRING || | |
568 exception->type != PP_VARTYPE_UNDEFINED) { | |
569 *exception = kCallFailed; | |
570 return kCallFailed; | |
571 } | |
572 return PP_MakeBool(PP_TRUE); | |
573 } | |
574 | |
575 PP_Var TestObject::hello_world(uint32_t argc, PP_Var* argv, PP_Var* exception) { | |
576 return g_var_interface->VarFromUtf8(g_module_id, | |
577 kHelloWorld, | |
578 strlen(kHelloWorld)); | |
579 } | |
580 | |
581 // PPP_Class_Deprecated | |
582 | |
583 bool HasProperty(void* object, | |
584 PP_Var name, | |
585 PP_Var* exception) { | |
586 printf("Global HasProperty\n"); | |
587 TestObject* test_object = static_cast<TestObject*>(object); | |
588 return test_object->HasProperty(name); | |
589 } | |
590 | |
591 bool HasMethod(void* object, | |
592 PP_Var name, | |
593 PP_Var* exception) { | |
594 TestObject* test_object = static_cast<TestObject*>(object); | |
595 return test_object->HasMethod(name); | |
596 } | |
597 | |
598 PP_Var GetProperty(void* object, | |
599 PP_Var name, | |
600 PP_Var* exception) { | |
601 printf("Global GetProperty\n"); | |
602 TestObject* test_object = static_cast<TestObject*>(object); | |
603 return test_object->GetProperty(name); | |
604 } | |
605 | |
606 void SetProperty(void* object, | |
607 PP_Var name, | |
608 PP_Var value, | |
609 PP_Var* exception) { | |
610 printf("Global SetProperty\n"); | |
611 TestObject* test_object = static_cast<TestObject*>(object); | |
612 if (!test_object->SetProperty(name, value)) { | |
613 *exception = kSetFailed; | |
614 } | |
615 } | |
616 | |
617 void RemoveProperty(void* object, | |
618 PP_Var name, | |
619 PP_Var* exception) { | |
620 TestObject* test_object = static_cast<TestObject*>(object); | |
621 test_object->RemoveProperty(name); | |
622 } | |
623 | |
624 PP_Var Call(void* object, | |
625 PP_Var name, | |
626 uint32_t argc, | |
627 PP_Var* argv, | |
628 PP_Var* exception) { | |
629 TestObject* test_object = static_cast<TestObject*>(object); | |
630 return test_object->Call(name, argc, argv, exception); | |
631 } | |
632 | |
633 static const PPP_Class_Deprecated object_class = { | |
634 HasProperty, | |
635 HasMethod, | |
636 GetProperty, | |
637 NULL, | |
638 SetProperty, | |
639 RemoveProperty, | |
640 Call, | |
641 NULL, | |
642 NULL, | |
643 }; | |
644 | |
645 // PPP_Instance functions. | |
646 | |
647 PP_Bool DidCreate(PP_Instance instance, | |
648 uint32_t argc, | |
649 const char* argn[], | |
650 const char* argv[]) { | |
651 printf("basic_object: DidCreate(%"NACL_PRIu32")\n", instance); | |
652 for (uint32_t i = 0; i < argc; ++i) { | |
653 printf(" arg[%"NACL_PRIu32"]: '%s' = '%s'\n", i, argn[i], argv[i]); | |
654 } | |
655 return PP_TRUE; | |
656 } | |
657 | |
658 void DidDestroy(PP_Instance instance) { | |
659 printf("basic_object: DidDestroy(%"NACL_PRIu32")\n", instance); | |
660 } | |
661 | |
662 PP_Var GetInstanceObject(PP_Instance instance) { | |
663 printf("basic_object: GetInstanceObject(%"NACL_PRIu32")\n", instance); | |
664 printf(" g_var_interface = %p\n", | |
665 reinterpret_cast<const void*>(g_var_interface)); | |
666 PP_Var retval = | |
667 g_var_interface->CreateObject(g_module_id, | |
668 &object_class, | |
669 static_cast<void*>(new TestObject)); | |
670 return retval; | |
671 } | |
672 | |
673 static const void* GetInstanceInterface() { | |
674 static const PPP_Instance instance_class = { | |
675 DidCreate, | |
676 DidDestroy, | |
677 NULL, // DidChangeView | |
678 NULL, // DidChangeFocus | |
679 NULL, // HandleInputEvent | |
680 NULL // HandleDocumentLoad | |
681 // TODO(dmichael): This test should probably just be removed. | |
682 #ifndef PPAPI_INSTANCE_REMOVE_SCRIPTING | |
683 , GetInstanceObject | |
684 #endif | |
685 }; | |
686 | |
687 return reinterpret_cast<const void*>(&instance_class); | |
688 } | |
689 | |
690 } // namespace | |
691 | |
692 PP_EXPORT int32_t PPP_InitializeModule(PP_Module module_id, | |
693 PPB_GetInterface get_browser_interface) { | |
694 // Save the global module information for later. | |
695 g_module_id = module_id; | |
696 g_get_browser_interface = get_browser_interface; | |
697 printf("basic_object: PPP_InitializeModule(%"NACL_PRId32", %p)\n", | |
698 module_id, | |
699 get_browser_interface); | |
700 | |
701 g_var_interface = | |
702 reinterpret_cast<const PPB_Var_Deprecated*>( | |
703 get_browser_interface(PPB_VAR_DEPRECATED_INTERFACE)); | |
704 | |
705 return PP_OK; | |
706 } | |
707 | |
708 PP_EXPORT void PPP_ShutdownModule() { | |
709 printf("basic_object: PPP_ShutdownModule()\n"); | |
710 } | |
711 | |
712 PP_EXPORT const void* PPP_GetInterface(const char* interface_name) { | |
713 printf("basic_object: PPP_GetInterface('%s')\n", interface_name); | |
714 if (strcmp(interface_name, PPP_INSTANCE_INTERFACE) == 0) { | |
715 return GetInstanceInterface(); | |
716 } | |
717 return NULL; | |
718 } | |
OLD | NEW |