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 // private: | |
34 // friend class Wrappable<MyClass>; | |
35 // static v8::Local<v8::ObjectTemplate> GetObjectTemplate( | |
Aaron Boodman
2013/12/16 16:44:26
It's ok for this to be public now (I think before
Aaron Boodman
2013/12/16 16:50:35
Some classes don't want to have any methods or pro
jochen (gone - plz use gerrit)
2013/12/17 14:32:31
Done.
| |
36 // v8::Isolate* isolate); | |
30 // ... | 37 // ... |
31 // }; | 38 // }; |
32 // | 39 // |
33 // // my_class.cc | 40 // // my_class.cc |
34 // INIT_WRAPABLE(MyClass); | 41 // WrapperInfo MyClass::kWrapperInfo = { kEmbedderNativeGin }; |
42 // | |
43 // v8::Local<v8::ObjectTemplate> MyClass::GetObjectTemplate( | |
44 // v8::Isolate* isolate) { | |
45 // return ObjectTemplateBuilder(isolate, &kWrapperInfo).Build(); | |
46 // } | |
35 // | 47 // |
36 // Subclasses should also typically have private constructors and expose a | 48 // Subclasses should also typically have private constructors and expose a |
37 // static Create function that returns a gin::Handle. Forcing creators through | 49 // static Create function that returns a gin::Handle. Forcing creators through |
38 // this static Create function will enforce that clients actually create a | 50 // 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 | 51 // 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 | 52 // object, the object will leak because we use the weak callback from the |
41 // wrapper as the signal to delete the wrapped object. | 53 // wrapper as the signal to delete the wrapped object. |
42 template<typename T> | 54 template<typename T> |
43 class Wrappable; | 55 class Wrappable; |
44 | 56 |
45 | 57 |
46 // Non-template base class to share code between templates instances. | 58 // Non-template base class to share code between templates instances. |
47 class GIN_EXPORT WrappableBase { | 59 class GIN_EXPORT WrappableBase { |
48 protected: | 60 protected: |
61 typedef v8::Local<v8::ObjectTemplate> (*TemplateBuilder)(v8::Isolate*); | |
Aaron Boodman
2013/12/16 16:44:26
Confusing name since we also have ObjectTemplateBu
jochen (gone - plz use gerrit)
2013/12/17 14:32:31
Done.
| |
62 | |
49 WrappableBase(); | 63 WrappableBase(); |
50 virtual ~WrappableBase(); | 64 virtual ~WrappableBase(); |
65 | |
51 v8::Handle<v8::Object> GetWrapperImpl(v8::Isolate* isolate, | 66 v8::Handle<v8::Object> GetWrapperImpl(v8::Isolate* isolate, |
52 WrapperInfo* wrapper_info); | 67 WrapperInfo* wrapper_info, |
53 v8::Handle<v8::Object> CreateWrapper(v8::Isolate* isolate, | 68 TemplateBuilder builder); |
54 WrapperInfo* wrapper_info); | |
55 v8::Persistent<v8::Object> wrapper_; // Weak | |
56 | 69 |
57 private: | 70 private: |
58 static void WeakCallback( | 71 static void WeakCallback( |
59 const v8::WeakCallbackData<v8::Object, WrappableBase>& data); | 72 const v8::WeakCallbackData<v8::Object, WrappableBase>& data); |
60 | 73 |
74 v8::Handle<v8::Object> CreateWrapper(v8::Isolate* isolate, | |
75 WrapperInfo* wrapper_info, | |
76 TemplateBuilder builder); | |
77 | |
78 v8::Persistent<v8::Object> wrapper_; // Weak | |
79 | |
61 DISALLOW_COPY_AND_ASSIGN(WrappableBase); | 80 DISALLOW_COPY_AND_ASSIGN(WrappableBase); |
62 }; | 81 }; |
63 | 82 |
64 | 83 |
65 template<typename T> | 84 template<typename T> |
66 class Wrappable : public WrappableBase { | 85 class Wrappable : public WrappableBase { |
67 public: | 86 public: |
68 // Retrieve (or create) the v8 wrapper object cooresponding to this object. | 87 // Retrieve (or create) the v8 wrapper object cooresponding to this object. |
69 // To customize the wrapper created for a subclass, override GetWrapperInfo() | 88 // To customize the wrapper created for a subclass, override GetWrapperInfo() |
70 // instead of overriding this function. | 89 // instead of overriding this function. |
71 v8::Handle<v8::Object> GetWrapper(v8::Isolate* isolate) { | 90 v8::Handle<v8::Object> GetWrapper(v8::Isolate* isolate) { |
72 return GetWrapperImpl(isolate, &T::kWrapperInfo); | 91 return GetWrapperImpl(isolate, &T::kWrapperInfo, &T::GetObjectTemplate); |
73 } | 92 } |
74 | 93 |
75 protected: | 94 protected: |
76 Wrappable() {} | 95 Wrappable() {} |
77 virtual ~Wrappable() {} | 96 virtual ~Wrappable() {} |
78 | 97 |
79 private: | 98 private: |
80 DISALLOW_COPY_AND_ASSIGN(Wrappable); | 99 DISALLOW_COPY_AND_ASSIGN(Wrappable); |
81 }; | 100 }; |
82 | 101 |
83 | 102 |
84 // This converter handles any subclass of Wrappable. | 103 // This converter handles any subclass of Wrappable. |
85 template<typename T> | 104 template<typename T> |
86 struct Converter<T*, typename base::enable_if< | 105 struct Converter<T*, typename base::enable_if< |
87 base::is_convertible<T*, Wrappable<T>*>::value>::type> { | 106 base::is_convertible<T*, Wrappable<T>*>::value>::type> { |
88 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, T* val) { | 107 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, T* val) { |
89 return val->GetWrapper(isolate); | 108 return val->GetWrapper(isolate); |
90 } | 109 } |
91 | 110 |
92 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, T** out) { | 111 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, T** out) { |
93 *out = static_cast<T*>(internal::FromV8Impl(isolate, val, | 112 *out = static_cast<T*>(internal::FromV8Impl(isolate, val, |
94 &T::kWrapperInfo)); | 113 &T::kWrapperInfo)); |
95 return *out != NULL; | 114 return *out != NULL; |
96 } | 115 } |
97 }; | 116 }; |
98 | 117 |
99 } // namespace gin | 118 } // namespace gin |
100 | 119 |
101 #endif // GIN_WRAPPABLE_H_ | 120 #endif // GIN_WRAPPABLE_H_ |
OLD | NEW |