OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/glue/plugins/pepper_var.h" | 5 #include "webkit/glue/plugins/pepper_var.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "third_party/ppapi/c/pp_var.h" | 10 #include "third_party/ppapi/c/pp_var.h" |
11 #include "third_party/ppapi/c/ppb_var.h" | 11 #include "third_party/ppapi/c/ppb_var.h" |
12 #include "third_party/ppapi/c/ppp_class.h" | 12 #include "third_party/ppapi/c/ppp_class.h" |
13 #include "third_party/WebKit/WebKit/chromium/public/WebBindings.h" | 13 #include "third_party/WebKit/WebKit/chromium/public/WebBindings.h" |
14 #include "webkit/glue/plugins/pepper_string.h" | 14 #include "webkit/glue/plugins/pepper_string.h" |
15 #include "v8/include/v8.h" | 15 #include "v8/include/v8.h" |
16 | 16 |
17 // Uncomment to enable catching JS exceptions | |
18 // #define HAVE_WEBBINDINGS_EXCEPTION_HANDLER 1 | |
19 | |
20 using WebKit::WebBindings; | 17 using WebKit::WebBindings; |
21 | 18 |
22 namespace pepper { | 19 namespace pepper { |
23 | 20 |
24 namespace { | 21 namespace { |
25 | 22 |
26 void Release(PP_Var var); | 23 void Release(PP_Var var); |
27 PP_Var VarFromUtf8(const char* data, uint32_t len); | 24 PP_Var VarFromUtf8(const char* data, uint32_t len); |
28 | 25 |
29 // --------------------------------------------------------------------------- | 26 // --------------------------------------------------------------------------- |
30 // Exceptions | 27 // Exceptions |
31 | 28 |
32 class TryCatch { | 29 class TryCatch { |
33 public: | 30 public: |
34 TryCatch(PP_Var* exception) : exception_(exception) { | 31 TryCatch(PP_Var* exception) : exception_(exception) { |
35 #ifdef HAVE_WEBBINDINGS_EXCEPTION_HANDLER | |
36 WebBindings::pushExceptionHandler(&TryCatch::Catch, this); | 32 WebBindings::pushExceptionHandler(&TryCatch::Catch, this); |
37 #endif | |
38 } | 33 } |
39 | 34 |
40 ~TryCatch() { | 35 ~TryCatch() { |
41 #ifdef HAVE_WEBBINDINGS_EXCEPTION_HANDLER | |
42 WebBindings::popExceptionHandler(); | 36 WebBindings::popExceptionHandler(); |
43 #endif | |
44 } | 37 } |
45 | 38 |
46 bool HasException() const { | 39 bool HasException() const { |
47 return exception_ && exception_->type != PP_VarType_Void; | 40 return exception_ && exception_->type != PP_VarType_Void; |
48 } | 41 } |
49 | 42 |
50 void SetException(const char* message) { | 43 void SetException(const char* message) { |
51 DCHECK(!HasException()); | 44 DCHECK(!HasException()); |
52 if (exception_) | 45 if (exception_) |
53 *exception_ = VarFromUtf8(message, strlen(message)); | 46 *exception_ = VarFromUtf8(message, strlen(message)); |
(...skipping 29 matching lines...) Expand all Loading... |
83 String* GetString(PP_Var var) { | 76 String* GetString(PP_Var var) { |
84 if (var.type != PP_VarType_String) | 77 if (var.type != PP_VarType_String) |
85 return NULL; | 78 return NULL; |
86 return GetStringUnchecked(var); | 79 return GetStringUnchecked(var); |
87 } | 80 } |
88 | 81 |
89 NPObject* GetNPObjectUnchecked(PP_Var var) { | 82 NPObject* GetNPObjectUnchecked(PP_Var var) { |
90 return reinterpret_cast<NPObject*>(var.value.as_id); | 83 return reinterpret_cast<NPObject*>(var.value.as_id); |
91 } | 84 } |
92 | 85 |
93 NPObject* GetNPObject(PP_Var var) { | |
94 if (var.type != PP_VarType_Object) | |
95 return NULL; | |
96 return GetNPObjectUnchecked(var); | |
97 } | |
98 | |
99 // Returns a PP_Var that corresponds to the given NPVariant. The contents of | 86 // Returns a PP_Var that corresponds to the given NPVariant. The contents of |
100 // the NPVariant will be copied unless the NPVariant corresponds to an object. | 87 // the NPVariant will be copied unless the NPVariant corresponds to an object. |
101 PP_Var NPVariantToPPVar(const NPVariant* variant) { | 88 PP_Var NPVariantToPPVar(const NPVariant* variant) { |
102 switch (variant->type) { | 89 switch (variant->type) { |
103 case NPVariantType_Void: | 90 case NPVariantType_Void: |
104 return PP_MakeVoid(); | 91 return PP_MakeVoid(); |
105 case NPVariantType_Null: | 92 case NPVariantType_Null: |
106 return PP_MakeNull(); | 93 return PP_MakeNull(); |
107 case NPVariantType_Bool: | 94 case NPVariantType_Bool: |
108 return PP_MakeBool(NPVARIANT_TO_BOOLEAN(*variant)); | 95 return PP_MakeBool(NPVARIANT_TO_BOOLEAN(*variant)); |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 if (exception.type != PP_VarType_Void) { | 397 if (exception.type != PP_VarType_Void) { |
411 ThrowException(object, exception); | 398 ThrowException(object, exception); |
412 Release(exception); | 399 Release(exception); |
413 return false; | 400 return false; |
414 } | 401 } |
415 return true; | 402 return true; |
416 } | 403 } |
417 | 404 |
418 bool WrapperClass_Enumerate(NPObject* object, NPIdentifier** values, | 405 bool WrapperClass_Enumerate(NPObject* object, NPIdentifier** values, |
419 uint32_t* count) { | 406 uint32_t* count) { |
420 // TODO(darin): Implement this method! | 407 WrapperObject* wrapper = ToWrapper(object); |
421 WebBindings::setException(object, kUnableToGetAllPropertiesException); | 408 |
422 return false; | 409 uint32_t property_count = 0; |
| 410 PP_Var* properties = NULL; |
| 411 PP_Var exception = PP_MakeVoid(); |
| 412 wrapper->ppp_class->GetAllPropertyNames(wrapper->ppp_class_data, |
| 413 &property_count, |
| 414 &properties, |
| 415 &exception); |
| 416 |
| 417 bool rv; |
| 418 if (exception.type == PP_VarType_Void) { |
| 419 rv = true; |
| 420 if (property_count == 0) { |
| 421 *values = NULL; |
| 422 *count = 0; |
| 423 } else { |
| 424 *values = static_cast<NPIdentifier*>( |
| 425 malloc(sizeof(NPIdentifier) * property_count)); |
| 426 *count = property_count; |
| 427 for (uint32_t i = 0; i < property_count; ++i) |
| 428 (*values)[i] = PPVarToNPIdentifier(properties[i]); |
| 429 } |
| 430 } else { |
| 431 rv = false; |
| 432 ThrowException(object, exception); |
| 433 Release(exception); |
| 434 } |
| 435 |
| 436 for (uint32_t i = 0; i < property_count; ++i) |
| 437 Release(properties[i]); |
| 438 free(properties); |
| 439 return rv; |
423 } | 440 } |
424 | 441 |
425 bool WrapperClass_Construct(NPObject* object, const NPVariant* argv, | 442 bool WrapperClass_Construct(NPObject* object, const NPVariant* argv, |
426 uint32_t argc, NPVariant* result) { | 443 uint32_t argc, NPVariant* result) { |
427 WrapperObject* wrapper = ToWrapper(object); | 444 WrapperObject* wrapper = ToWrapper(object); |
428 | 445 |
429 scoped_array<PP_Var> args; | 446 scoped_array<PP_Var> args; |
430 if (argc) { | 447 if (argc) { |
431 args.reset(new PP_Var[argc]); | 448 args.reset(new PP_Var[argc]); |
432 for (uint32_t i = 0; i < argc; ++i) | 449 for (uint32_t i = 0; i < argc; ++i) |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 | 601 |
585 PP_Var ret = NPVariantToPPVar(&result); | 602 PP_Var ret = NPVariantToPPVar(&result); |
586 WebBindings::releaseVariantValue(&result); | 603 WebBindings::releaseVariantValue(&result); |
587 return ret; | 604 return ret; |
588 } | 605 } |
589 | 606 |
590 void GetAllPropertyNames(PP_Var var, | 607 void GetAllPropertyNames(PP_Var var, |
591 uint32_t* property_count, | 608 uint32_t* property_count, |
592 PP_Var** properties, | 609 PP_Var** properties, |
593 PP_Var* exception) { | 610 PP_Var* exception) { |
| 611 *properties = NULL; |
| 612 *property_count = 0; |
| 613 |
594 TryCatch try_catch(exception); | 614 TryCatch try_catch(exception); |
595 if (try_catch.HasException()) | 615 if (try_catch.HasException()) |
596 return; | 616 return; |
597 | 617 |
598 // TODO(darin): Implement this method! | 618 NPObject* object = GetNPObject(var); |
599 try_catch.SetException(kUnableToGetAllPropertiesException); | 619 if (!object) { |
| 620 try_catch.SetException(kInvalidObjectException); |
| 621 return; |
| 622 } |
| 623 |
| 624 NPIdentifier* identifiers = NULL; |
| 625 uint32_t count = 0; |
| 626 if (!WebBindings::enumerate(NULL, object, &identifiers, &count)) { |
| 627 if (!try_catch.HasException()) |
| 628 try_catch.SetException(kUnableToGetAllPropertiesException); |
| 629 return; |
| 630 } |
| 631 |
| 632 if (count == 0) |
| 633 return; |
| 634 |
| 635 *property_count = count; |
| 636 *properties = static_cast<PP_Var*>(malloc(sizeof(PP_Var) * count)); |
| 637 for (uint32_t i = 0; i < count; ++i) |
| 638 (*properties)[i] = NPIdentifierToPPVar(identifiers[i]); |
| 639 free(identifiers); |
600 } | 640 } |
601 | 641 |
602 void SetProperty(PP_Var var, | 642 void SetProperty(PP_Var var, |
603 PP_Var name, | 643 PP_Var name, |
604 PP_Var value, | 644 PP_Var value, |
605 PP_Var* exception) { | 645 PP_Var* exception) { |
606 TryCatch try_catch(exception); | 646 TryCatch try_catch(exception); |
607 if (try_catch.HasException()) | 647 if (try_catch.HasException()) |
608 return; | 648 return; |
609 | 649 |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
792 } | 832 } |
793 | 833 |
794 PP_Var NPObjectToPPVar(NPObject* object) { | 834 PP_Var NPObjectToPPVar(NPObject* object) { |
795 PP_Var ret; | 835 PP_Var ret; |
796 ret.type = PP_VarType_Object; | 836 ret.type = PP_VarType_Object; |
797 ret.value.as_id = reinterpret_cast<intptr_t>(object); | 837 ret.value.as_id = reinterpret_cast<intptr_t>(object); |
798 WebBindings::retainObject(object); | 838 WebBindings::retainObject(object); |
799 return ret; | 839 return ret; |
800 } | 840 } |
801 | 841 |
| 842 NPObject* GetNPObject(PP_Var var) { |
| 843 if (var.type != PP_VarType_Object) |
| 844 return NULL; |
| 845 return GetNPObjectUnchecked(var); |
| 846 } |
| 847 |
802 } // namespace pepper | 848 } // namespace pepper |
OLD | NEW |