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_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "third_party/ppapi/c/pp_var.h" | 11 #include "third_party/ppapi/c/pp_var.h" |
12 #include "third_party/ppapi/c/ppb_var.h" | 12 #include "third_party/ppapi/c/ppb_var.h" |
13 #include "third_party/ppapi/c/ppp_class.h" | 13 #include "third_party/ppapi/c/ppp_class.h" |
14 #include "third_party/WebKit/WebKit/chromium/public/WebBindings.h" | 14 #include "third_party/WebKit/WebKit/chromium/public/WebBindings.h" |
15 #include "webkit/glue/plugins/pepper_string.h" | 15 #include "webkit/glue/plugins/pepper_string.h" |
16 #include "v8/include/v8.h" | 16 #include "v8/include/v8.h" |
17 | 17 |
18 using WebKit::WebBindings; | 18 using WebKit::WebBindings; |
19 | 19 |
20 namespace pepper { | 20 namespace pepper { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 void Release(PP_Var var); | 24 void Release(PP_Var var); |
25 PP_Var VarFromUtf8(const char* data, uint32_t len); | 25 PP_Var VarFromUtf8(const char* data, uint32_t len); |
26 | 26 |
27 // --------------------------------------------------------------------------- | |
28 // Exceptions | |
29 | |
30 class TryCatch { | |
31 public: | |
32 TryCatch(PP_Var* exception) : exception_(exception) { | |
33 WebBindings::pushExceptionHandler(&TryCatch::Catch, this); | |
34 } | |
35 | |
36 ~TryCatch() { | |
37 WebBindings::popExceptionHandler(); | |
38 } | |
39 | |
40 bool HasException() const { | |
41 return exception_ && exception_->type != PP_VARTYPE_VOID; | |
42 } | |
43 | |
44 void SetException(const char* message) { | |
45 DCHECK(!HasException()); | |
46 if (exception_) | |
47 *exception_ = VarFromUtf8(message, strlen(message)); | |
48 } | |
49 | |
50 private: | |
51 static void Catch(void* self, const NPUTF8* message) { | |
52 static_cast<TryCatch*>(self)->SetException(message); | |
53 } | |
54 | |
55 // May be null if the consumer isn't interesting in catching exceptions. | |
56 PP_Var* exception_; | |
57 }; | |
58 | |
59 const char kInvalidObjectException[] = "Error: Invalid object"; | 27 const char kInvalidObjectException[] = "Error: Invalid object"; |
60 const char kInvalidPropertyException[] = "Error: Invalid property"; | 28 const char kInvalidPropertyException[] = "Error: Invalid property"; |
61 const char kUnableToGetPropertyException[] = "Error: Unable to get property"; | 29 const char kUnableToGetPropertyException[] = "Error: Unable to get property"; |
62 const char kUnableToSetPropertyException[] = "Error: Unable to set property"; | 30 const char kUnableToSetPropertyException[] = "Error: Unable to set property"; |
63 const char kUnableToRemovePropertyException[] = | 31 const char kUnableToRemovePropertyException[] = |
64 "Error: Unable to remove property"; | 32 "Error: Unable to remove property"; |
65 const char kUnableToGetAllPropertiesException[] = | 33 const char kUnableToGetAllPropertiesException[] = |
66 "Error: Unable to get all properties"; | 34 "Error: Unable to get all properties"; |
67 const char kUnableToCallMethodException[] = "Error: Unable to call method"; | 35 const char kUnableToCallMethodException[] = "Error: Unable to call method"; |
68 const char kUnableToConstructException[] = "Error: Unable to construct"; | 36 const char kUnableToConstructException[] = "Error: Unable to construct"; |
69 | 37 |
70 // --------------------------------------------------------------------------- | 38 // --------------------------------------------------------------------------- |
71 // Utilities | 39 // Utilities |
72 | 40 |
73 String* GetStringUnchecked(PP_Var var) { | 41 String* GetStringUnchecked(PP_Var var) { |
74 return reinterpret_cast<String*>(var.value.as_id); | 42 return reinterpret_cast<String*>(var.value.as_id); |
75 } | 43 } |
76 | 44 |
77 NPObject* GetNPObjectUnchecked(PP_Var var) { | 45 NPObject* GetNPObjectUnchecked(PP_Var var) { |
78 return reinterpret_cast<NPObject*>(var.value.as_id); | 46 return reinterpret_cast<NPObject*>(var.value.as_id); |
79 } | 47 } |
80 | 48 |
81 // Returns a PP_Var that corresponds to the given NPVariant. The contents of | |
82 // the NPVariant will be copied unless the NPVariant corresponds to an object. | |
83 PP_Var NPVariantToPPVar(const NPVariant* variant) { | |
84 switch (variant->type) { | |
85 case NPVariantType_Void: | |
86 return PP_MakeVoid(); | |
87 case NPVariantType_Null: | |
88 return PP_MakeNull(); | |
89 case NPVariantType_Bool: | |
90 return PP_MakeBool(NPVARIANT_TO_BOOLEAN(*variant)); | |
91 case NPVariantType_Int32: | |
92 return PP_MakeInt32(NPVARIANT_TO_INT32(*variant)); | |
93 case NPVariantType_Double: | |
94 return PP_MakeDouble(NPVARIANT_TO_DOUBLE(*variant)); | |
95 case NPVariantType_String: | |
96 return VarFromUtf8(NPVARIANT_TO_STRING(*variant).UTF8Characters, | |
97 NPVARIANT_TO_STRING(*variant).UTF8Length); | |
98 case NPVariantType_Object: | |
99 return NPObjectToPPVar(NPVARIANT_TO_OBJECT(*variant)); | |
100 } | |
101 NOTREACHED(); | |
102 return PP_MakeVoid(); | |
103 } | |
104 | |
105 // Returns a NPVariant that corresponds to the given PP_Var. The contents of | 49 // Returns a NPVariant that corresponds to the given PP_Var. The contents of |
106 // the PP_Var will be copied unless the PP_Var corresponds to an object. | 50 // the PP_Var will be copied unless the PP_Var corresponds to an object. |
107 NPVariant PPVarToNPVariant(PP_Var var) { | 51 NPVariant PPVarToNPVariant(PP_Var var) { |
108 NPVariant ret; | 52 NPVariant ret; |
109 switch (var.type) { | 53 switch (var.type) { |
110 case PP_VARTYPE_VOID: | 54 case PP_VARTYPE_VOID: |
111 VOID_TO_NPVARIANT(ret); | 55 VOID_TO_NPVARIANT(ret); |
112 break; | 56 break; |
113 case PP_VARTYPE_NULL: | 57 case PP_VARTYPE_NULL: |
114 NULL_TO_NPVARIANT(ret); | 58 NULL_TO_NPVARIANT(ret); |
(...skipping 723 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
838 } | 782 } |
839 | 783 |
840 PP_Var NPObjectToPPVar(NPObject* object) { | 784 PP_Var NPObjectToPPVar(NPObject* object) { |
841 PP_Var ret; | 785 PP_Var ret; |
842 ret.type = PP_VARTYPE_OBJECT; | 786 ret.type = PP_VARTYPE_OBJECT; |
843 ret.value.as_id = reinterpret_cast<intptr_t>(object); | 787 ret.value.as_id = reinterpret_cast<intptr_t>(object); |
844 WebBindings::retainObject(object); | 788 WebBindings::retainObject(object); |
845 return ret; | 789 return ret; |
846 } | 790 } |
847 | 791 |
| 792 PP_Var NPVariantToPPVar(const NPVariant* variant) { |
| 793 switch (variant->type) { |
| 794 case NPVariantType_Void: |
| 795 return PP_MakeVoid(); |
| 796 case NPVariantType_Null: |
| 797 return PP_MakeNull(); |
| 798 case NPVariantType_Bool: |
| 799 return PP_MakeBool(NPVARIANT_TO_BOOLEAN(*variant)); |
| 800 case NPVariantType_Int32: |
| 801 return PP_MakeInt32(NPVARIANT_TO_INT32(*variant)); |
| 802 case NPVariantType_Double: |
| 803 return PP_MakeDouble(NPVARIANT_TO_DOUBLE(*variant)); |
| 804 case NPVariantType_String: |
| 805 return VarFromUtf8(NPVARIANT_TO_STRING(*variant).UTF8Characters, |
| 806 NPVARIANT_TO_STRING(*variant).UTF8Length); |
| 807 case NPVariantType_Object: |
| 808 return NPObjectToPPVar(NPVARIANT_TO_OBJECT(*variant)); |
| 809 } |
| 810 NOTREACHED(); |
| 811 return PP_MakeVoid(); |
| 812 } |
| 813 |
| 814 |
848 NPObject* GetNPObject(PP_Var var) { | 815 NPObject* GetNPObject(PP_Var var) { |
849 if (var.type != PP_VARTYPE_OBJECT) | 816 if (var.type != PP_VARTYPE_OBJECT) |
850 return NULL; | 817 return NULL; |
851 return GetNPObjectUnchecked(var); | 818 return GetNPObjectUnchecked(var); |
852 } | 819 } |
853 | 820 |
854 PP_Var StringToPPVar(const std::string& str) { | 821 PP_Var StringToPPVar(const std::string& str) { |
855 DCHECK(IsStringUTF8(str)); | 822 DCHECK(IsStringUTF8(str)); |
856 return VarFromUtf8(str.data(), str.size()); | 823 return VarFromUtf8(str.data(), str.size()); |
857 } | 824 } |
858 | 825 |
859 String* GetString(PP_Var var) { | 826 String* GetString(PP_Var var) { |
860 if (var.type != PP_VARTYPE_STRING) | 827 if (var.type != PP_VARTYPE_STRING) |
861 return NULL; | 828 return NULL; |
862 return GetStringUnchecked(var); | 829 return GetStringUnchecked(var); |
863 } | 830 } |
864 | 831 |
| 832 TryCatch::TryCatch(PP_Var* exception) : exception_(exception) { |
| 833 WebBindings::pushExceptionHandler(&TryCatch::Catch, this); |
| 834 } |
| 835 |
| 836 TryCatch::~TryCatch() { |
| 837 WebBindings::popExceptionHandler(); |
| 838 } |
| 839 |
| 840 bool TryCatch::HasException() const { |
| 841 return exception_ && exception_->type != PP_VARTYPE_VOID; |
| 842 } |
| 843 |
| 844 void TryCatch::SetException(const char* message) { |
| 845 DCHECK(!HasException()); |
| 846 if (exception_) |
| 847 *exception_ = VarFromUtf8(message, strlen(message)); |
| 848 } |
| 849 |
| 850 // static |
| 851 void TryCatch::Catch(void* self, const char* message) { |
| 852 static_cast<TryCatch*>(self)->SetException(message); |
| 853 } |
| 854 |
| 855 |
865 } // namespace pepper | 856 } // namespace pepper |
OLD | NEW |