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/gin_export.h" | 10 #include "gin/gin_export.h" |
11 #include "gin/public/wrapper_info.h" | 11 #include "gin/public/wrapper_info.h" |
12 | 12 |
13 namespace gin { | 13 namespace gin { |
14 | 14 |
15 namespace internal { | 15 namespace internal { |
16 | 16 |
17 GIN_EXPORT void* FromV8Impl(v8::Isolate* isolate, | 17 GIN_EXPORT void* FromV8Impl(v8::Isolate* isolate, |
18 v8::Handle<v8::Value> val, | 18 v8::Handle<v8::Value> val, |
19 WrapperInfo* info); | 19 WrapperInfo* info); |
20 | 20 |
21 } // namespace internal | 21 } // namespace internal |
22 | 22 |
23 | 23 |
24 // Wrappable is a base class for C++ objects that have corresponding v8 wrapper | 24 // Wrappable is a base class for C++ objects that have corresponding v8 wrapper |
25 // objects. To retain a Wrappable object on the stack, use a gin::Handle. | 25 // objects. To retain a Wrappable object on the stack, use a gin::Handle. |
26 // | 26 // |
27 // USAGE: | 27 // USAGE: |
28 // // my_class.h | 28 // // my_class.h |
29 // class MyClass : Wrappable<MyClass> { | 29 // class MyClass : Wrappable<MyClass> { |
| 30 // public: |
| 31 // static WrapperInfo kWrapperInfo; |
| 32 // |
| 33 // // Optional, only required if non-empty template should be used. |
| 34 // static v8::Local<v8::ObjectTemplate> GetObjectTemplate( |
| 35 // v8::Isolate* isolate); |
30 // ... | 36 // ... |
31 // }; | 37 // }; |
32 // | 38 // |
33 // // my_class.cc | 39 // // my_class.cc |
34 // INIT_WRAPABLE(MyClass); | 40 // WrapperInfo MyClass::kWrapperInfo = { kEmbedderNativeGin }; |
| 41 // |
| 42 // v8::Local<v8::ObjectTemplate> MyClass::GetObjectTemplate( |
| 43 // v8::Isolate* isolate) { |
| 44 // return ObjectTemplateBuilder(isolate).SetValue("foobar", 42).Build(); |
| 45 // } |
35 // | 46 // |
36 // Subclasses should also typically have private constructors and expose a | 47 // Subclasses should also typically have private constructors and expose a |
37 // static Create function that returns a gin::Handle. Forcing creators through | 48 // static Create function that returns a gin::Handle. Forcing creators through |
38 // this static Create function will enforce that clients actually create a | 49 // this static Create function will enforce that clients actually create a |
39 // wrapper for the object. If clients fail to create a wrapper for a wrappable | 50 // wrapper for the object. If clients fail to create a wrapper for a wrappable |
40 // object, the object will leak because we use the weak callback from the | 51 // object, the object will leak because we use the weak callback from the |
41 // wrapper as the signal to delete the wrapped object. | 52 // wrapper as the signal to delete the wrapped object. |
42 template<typename T> | 53 template<typename T> |
43 class Wrappable; | 54 class Wrappable; |
44 | 55 |
45 | 56 |
46 // Non-template base class to share code between templates instances. | 57 // Non-template base class to share code between templates instances. |
47 class GIN_EXPORT WrappableBase { | 58 class GIN_EXPORT WrappableBase { |
48 protected: | 59 protected: |
| 60 typedef v8::Local<v8::ObjectTemplate>(*GetObjectTemplateFunction)( |
| 61 v8::Isolate*); |
| 62 |
49 WrappableBase(); | 63 WrappableBase(); |
50 virtual ~WrappableBase(); | 64 virtual ~WrappableBase(); |
51 v8::Handle<v8::Object> GetWrapperImpl(v8::Isolate* isolate, | 65 |
52 WrapperInfo* wrapper_info); | 66 v8::Handle<v8::Object> GetWrapperImpl( |
53 v8::Handle<v8::Object> CreateWrapper(v8::Isolate* isolate, | 67 v8::Isolate* isolate, |
54 WrapperInfo* wrapper_info); | 68 WrapperInfo* wrapper_info, |
55 v8::Persistent<v8::Object> wrapper_; // Weak | 69 GetObjectTemplateFunction template_getter); |
| 70 |
| 71 static v8::Local<v8::ObjectTemplate> GetObjectTemplate(v8::Isolate* isolate); |
56 | 72 |
57 private: | 73 private: |
58 static void WeakCallback( | 74 static void WeakCallback( |
59 const v8::WeakCallbackData<v8::Object, WrappableBase>& data); | 75 const v8::WeakCallbackData<v8::Object, WrappableBase>& data); |
60 | 76 |
| 77 v8::Handle<v8::Object> CreateWrapper( |
| 78 v8::Isolate* isolate, |
| 79 WrapperInfo* wrapper_info, |
| 80 GetObjectTemplateFunction template_getter); |
| 81 |
| 82 v8::Persistent<v8::Object> wrapper_; // Weak |
| 83 |
61 DISALLOW_COPY_AND_ASSIGN(WrappableBase); | 84 DISALLOW_COPY_AND_ASSIGN(WrappableBase); |
62 }; | 85 }; |
63 | 86 |
64 | 87 |
65 template<typename T> | 88 template<typename T> |
66 class Wrappable : public WrappableBase { | 89 class Wrappable : public WrappableBase { |
67 public: | 90 public: |
68 // Retrieve (or create) the v8 wrapper object cooresponding to this object. | 91 // Retrieve (or create) the v8 wrapper object cooresponding to this object. |
69 // To customize the wrapper created for a subclass, override GetWrapperInfo() | 92 // To customize the wrapper created for a subclass, override GetWrapperInfo() |
70 // instead of overriding this function. | 93 // instead of overriding this function. |
71 v8::Handle<v8::Object> GetWrapper(v8::Isolate* isolate) { | 94 v8::Handle<v8::Object> GetWrapper(v8::Isolate* isolate) { |
72 return GetWrapperImpl(isolate, &T::kWrapperInfo); | 95 return GetWrapperImpl(isolate, &T::kWrapperInfo, &T::GetObjectTemplate); |
73 } | 96 } |
74 | 97 |
75 protected: | 98 protected: |
76 Wrappable() {} | 99 Wrappable() {} |
77 virtual ~Wrappable() {} | 100 virtual ~Wrappable() {} |
78 | 101 |
79 private: | 102 private: |
80 DISALLOW_COPY_AND_ASSIGN(Wrappable); | 103 DISALLOW_COPY_AND_ASSIGN(Wrappable); |
81 }; | 104 }; |
82 | 105 |
83 | 106 |
84 // This converter handles any subclass of Wrappable. | 107 // This converter handles any subclass of Wrappable. |
85 template<typename T> | 108 template<typename T> |
86 struct Converter<T*, typename base::enable_if< | 109 struct Converter<T*, typename base::enable_if< |
87 base::is_convertible<T*, Wrappable<T>*>::value>::type> { | 110 base::is_convertible<T*, Wrappable<T>*>::value>::type> { |
88 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, T* val) { | 111 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, T* val) { |
89 return val->GetWrapper(isolate); | 112 return val->GetWrapper(isolate); |
90 } | 113 } |
91 | 114 |
92 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, T** out) { | 115 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, T** out) { |
93 *out = static_cast<T*>(internal::FromV8Impl(isolate, val, | 116 *out = static_cast<T*>(internal::FromV8Impl(isolate, val, |
94 &T::kWrapperInfo)); | 117 &T::kWrapperInfo)); |
95 return *out != NULL; | 118 return *out != NULL; |
96 } | 119 } |
97 }; | 120 }; |
98 | 121 |
99 } // namespace gin | 122 } // namespace gin |
100 | 123 |
101 #endif // GIN_WRAPPABLE_H_ | 124 #endif // GIN_WRAPPABLE_H_ |
OLD | NEW |