| 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 |
| 27 const char kInvalidObjectException[] = "Error: Invalid object"; | 59 const char kInvalidObjectException[] = "Error: Invalid object"; |
| 28 const char kInvalidPropertyException[] = "Error: Invalid property"; | 60 const char kInvalidPropertyException[] = "Error: Invalid property"; |
| 29 const char kUnableToGetPropertyException[] = "Error: Unable to get property"; | 61 const char kUnableToGetPropertyException[] = "Error: Unable to get property"; |
| 30 const char kUnableToSetPropertyException[] = "Error: Unable to set property"; | 62 const char kUnableToSetPropertyException[] = "Error: Unable to set property"; |
| 31 const char kUnableToRemovePropertyException[] = | 63 const char kUnableToRemovePropertyException[] = |
| 32 "Error: Unable to remove property"; | 64 "Error: Unable to remove property"; |
| 33 const char kUnableToGetAllPropertiesException[] = | 65 const char kUnableToGetAllPropertiesException[] = |
| 34 "Error: Unable to get all properties"; | 66 "Error: Unable to get all properties"; |
| 35 const char kUnableToCallMethodException[] = "Error: Unable to call method"; | 67 const char kUnableToCallMethodException[] = "Error: Unable to call method"; |
| 36 const char kUnableToConstructException[] = "Error: Unable to construct"; | 68 const char kUnableToConstructException[] = "Error: Unable to construct"; |
| 37 | 69 |
| 38 // --------------------------------------------------------------------------- | 70 // --------------------------------------------------------------------------- |
| 39 // Utilities | 71 // Utilities |
| 40 | 72 |
| 41 String* GetStringUnchecked(PP_Var var) { | 73 String* GetStringUnchecked(PP_Var var) { |
| 42 return reinterpret_cast<String*>(var.value.as_id); | 74 return reinterpret_cast<String*>(var.value.as_id); |
| 43 } | 75 } |
| 44 | 76 |
| 45 NPObject* GetNPObjectUnchecked(PP_Var var) { | 77 NPObject* GetNPObjectUnchecked(PP_Var var) { |
| 46 return reinterpret_cast<NPObject*>(var.value.as_id); | 78 return reinterpret_cast<NPObject*>(var.value.as_id); |
| 47 } | 79 } |
| 48 | 80 |
| 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 |
| 49 // Returns a NPVariant that corresponds to the given PP_Var. The contents of | 105 // Returns a NPVariant that corresponds to the given PP_Var. The contents of |
| 50 // the PP_Var will be copied unless the PP_Var corresponds to an object. | 106 // the PP_Var will be copied unless the PP_Var corresponds to an object. |
| 51 NPVariant PPVarToNPVariant(PP_Var var) { | 107 NPVariant PPVarToNPVariant(PP_Var var) { |
| 52 NPVariant ret; | 108 NPVariant ret; |
| 53 switch (var.type) { | 109 switch (var.type) { |
| 54 case PP_VARTYPE_VOID: | 110 case PP_VARTYPE_VOID: |
| 55 VOID_TO_NPVARIANT(ret); | 111 VOID_TO_NPVARIANT(ret); |
| 56 break; | 112 break; |
| 57 case PP_VARTYPE_NULL: | 113 case PP_VARTYPE_NULL: |
| 58 NULL_TO_NPVARIANT(ret); | 114 NULL_TO_NPVARIANT(ret); |
| (...skipping 723 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 782 } | 838 } |
| 783 | 839 |
| 784 PP_Var NPObjectToPPVar(NPObject* object) { | 840 PP_Var NPObjectToPPVar(NPObject* object) { |
| 785 PP_Var ret; | 841 PP_Var ret; |
| 786 ret.type = PP_VARTYPE_OBJECT; | 842 ret.type = PP_VARTYPE_OBJECT; |
| 787 ret.value.as_id = reinterpret_cast<intptr_t>(object); | 843 ret.value.as_id = reinterpret_cast<intptr_t>(object); |
| 788 WebBindings::retainObject(object); | 844 WebBindings::retainObject(object); |
| 789 return ret; | 845 return ret; |
| 790 } | 846 } |
| 791 | 847 |
| 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 | |
| 815 NPObject* GetNPObject(PP_Var var) { | 848 NPObject* GetNPObject(PP_Var var) { |
| 816 if (var.type != PP_VARTYPE_OBJECT) | 849 if (var.type != PP_VARTYPE_OBJECT) |
| 817 return NULL; | 850 return NULL; |
| 818 return GetNPObjectUnchecked(var); | 851 return GetNPObjectUnchecked(var); |
| 819 } | 852 } |
| 820 | 853 |
| 821 PP_Var StringToPPVar(const std::string& str) { | 854 PP_Var StringToPPVar(const std::string& str) { |
| 822 DCHECK(IsStringUTF8(str)); | 855 DCHECK(IsStringUTF8(str)); |
| 823 return VarFromUtf8(str.data(), str.size()); | 856 return VarFromUtf8(str.data(), str.size()); |
| 824 } | 857 } |
| 825 | 858 |
| 826 String* GetString(PP_Var var) { | 859 String* GetString(PP_Var var) { |
| 827 if (var.type != PP_VARTYPE_STRING) | 860 if (var.type != PP_VARTYPE_STRING) |
| 828 return NULL; | 861 return NULL; |
| 829 return GetStringUnchecked(var); | 862 return GetStringUnchecked(var); |
| 830 } | 863 } |
| 831 | 864 |
| 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 | |
| 856 } // namespace pepper | 865 } // namespace pepper |
| OLD | NEW |