OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef GIN_WRAPPABLE_H_ | 5 #ifndef GIN_WRAPPABLE_H_ |
6 #define GIN_WRAPPABLE_H_ | 6 #define GIN_WRAPPABLE_H_ |
7 | 7 |
8 #include "base/template_util.h" | 8 #include "base/template_util.h" |
9 #include "gin/converter.h" | 9 #include "gin/converter.h" |
10 #include "gin/public/wrapper_info.h" | 10 #include "gin/public/wrapper_info.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 static void WeakCallback( | 56 static void WeakCallback( |
57 const v8::WeakCallbackData<v8::Object, WrappableBase>& data); | 57 const v8::WeakCallbackData<v8::Object, WrappableBase>& data); |
58 | 58 |
59 DISALLOW_COPY_AND_ASSIGN(WrappableBase); | 59 DISALLOW_COPY_AND_ASSIGN(WrappableBase); |
60 }; | 60 }; |
61 | 61 |
62 | 62 |
63 template<typename T> | 63 template<typename T> |
64 class Wrappable : public WrappableBase { | 64 class Wrappable : public WrappableBase { |
65 public: | 65 public: |
66 static WrapperInfo kWrapperInfo; | |
67 | |
68 // Retrieve (or create) the v8 wrapper object cooresponding to this object. | 66 // Retrieve (or create) the v8 wrapper object cooresponding to this object. |
69 // To customize the wrapper created for a subclass, override GetWrapperInfo() | 67 // To customize the wrapper created for a subclass, override GetWrapperInfo() |
70 // instead of overriding this function. | 68 // instead of overriding this function. |
71 v8::Handle<v8::Object> GetWrapper(v8::Isolate* isolate) { | 69 v8::Handle<v8::Object> GetWrapper(v8::Isolate* isolate) { |
72 return GetWrapperImpl(isolate, &kWrapperInfo); | 70 return GetWrapperImpl(isolate, &T::kWrapperInfo); |
73 } | 71 } |
74 | 72 |
75 protected: | 73 protected: |
76 Wrappable() {} | 74 Wrappable() {} |
77 ~Wrappable() {} | 75 ~Wrappable() {} |
78 DISALLOW_COPY_AND_ASSIGN(Wrappable); | 76 DISALLOW_COPY_AND_ASSIGN(Wrappable); |
79 }; | 77 }; |
80 | 78 |
81 | 79 |
82 // Subclasses of Wrappable must call this within a cc file to initialize their | |
83 // WrapperInfo. | |
84 #define INIT_WRAPPABLE(TYPE) \ | |
85 template<> \ | |
86 gin::WrapperInfo gin::Wrappable<TYPE>::kWrapperInfo = { kEmbedderNativeGin }; | |
87 | |
88 | |
89 // This converter handles any subclass of Wrappable. | 80 // This converter handles any subclass of Wrappable. |
90 template<typename T> | 81 template<typename T> |
91 struct Converter<T*, typename base::enable_if< | 82 struct Converter<T*, typename base::enable_if< |
92 base::is_convertible<T*, Wrappable<T>*>::value>::type> { | 83 base::is_convertible<T*, Wrappable<T>*>::value>::type> { |
93 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, T* val) { | 84 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, T* val) { |
94 return val->GetWrapper(isolate); | 85 return val->GetWrapper(isolate); |
95 } | 86 } |
96 | 87 |
97 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, T** out) { | 88 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, T** out) { |
98 *out = static_cast<T*>(internal::FromV8Impl(isolate, val, | 89 *out = static_cast<T*>(internal::FromV8Impl(isolate, val, |
99 &T::kWrapperInfo)); | 90 &T::kWrapperInfo)); |
100 return *out != NULL; | 91 return *out != NULL; |
101 } | 92 } |
102 }; | 93 }; |
103 | 94 |
104 } // namespace gin | 95 } // namespace gin |
105 | 96 |
106 #endif // GIN_WRAPPABLE_H_ | 97 #endif // GIN_WRAPPABLE_H_ |
OLD | NEW |