Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Side by Side Diff: gin/wrappable.h

Issue 105743007: Gin: Make it easier to implement Wrappable (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cleanup Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « gin/object_template_builder.h ('k') | gin/wrappable.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 11
12 namespace gin { 12 namespace gin {
13 13
14 // Wrappable is an abstract base class for C++ objects that have cooresponding 14 namespace internal {
15 // v8 wrapper objects. To retain a Wrappable object on the stack, use a 15
16 // gin::Handle. 16 void* FromV8Impl(v8::Isolate* isolate, v8::Handle<v8::Value> val,
17 class Wrappable { 17 WrapperInfo* info);
18
19 } // namespace internal
20
21
22 // Wrappable is a base class for C++ objects that have corresponding v8 wrapper
23 // objects. To retain a Wrappable object on the stack, use a gin::Handle.
24 //
25 // USAGE:
26 // // my_class.h
27 // class MyClass : Wrappable<MyClass> {
28 // ...
29 // };
30 //
31 // // my_class.cc
32 // INIT_WRAPABLE(MyClass);
33 //
34 // Subclasses should also typically have private constructors and expose a
35 // static Create function that returns a gin::Handle. Forcing creators through
36 // this static Create function will enforce that clients actually create a
37 // wrapper for the object. If clients fail to create a wrapper for a wrappable
38 // object, the object will leak because we use the weak callback from the
39 // wrapper as the signal to delete the wrapped object.
40 template<typename T>
41 class Wrappable;
42
43
44 // Non-template base class to share code between templates instances.
45 class WrappableBase {
46 protected:
47 WrappableBase();
48 ~WrappableBase();
49 v8::Handle<v8::Object> GetWrapperImpl(v8::Isolate* isolate,
50 WrapperInfo* wrapper_info);
51 v8::Handle<v8::Object> CreateWrapper(v8::Isolate* isolate,
52 WrapperInfo* wrapper_info);
53 v8::Persistent<v8::Object> wrapper_; // Weak
54
55 private:
56 static void WeakCallback(
57 const v8::WeakCallbackData<v8::Object, WrappableBase>& data);
58
59 DISALLOW_COPY_AND_ASSIGN(WrappableBase);
60 };
61
62
63 template<typename T>
64 class Wrappable : public WrappableBase {
18 public: 65 public:
19 // Subclasses must return the WrapperInfo object associated with the 66 static WrapperInfo kWrapperInfo;
20 // v8::ObjectTemplate for their subclass. When creating a v8 wrapper for
21 // this object, we'll look up the appropriate v8::ObjectTemplate in the
22 // PerIsolateData using this WrapperInfo pointer.
23 virtual WrapperInfo* GetWrapperInfo() = 0;
24
25 // Subclasses much also contain a static member variable named |kWrapperInfo|
26 // of type WrapperInfo:
27 //
28 // static WrapperInfo kWrapperInfo;
29 //
30 // If |obj| is a concrete instance of the subclass, then obj->GetWrapperInfo()
31 // must return &kWrapperInfo.
32 //
33 // We use both the dynamic |GetWrapperInfo| function and the static
34 // |kWrapperInfo| member variable during wrapping and the unwrapping. During
35 // wrapping, we use GetWrapperInfo() to make sure we use the correct
36 // v8::ObjectTemplate for the object regardless of the declared C++ type.
37 // During unwrapping, we use the static member variable to prevent type errors
38 // during the downcast from Wrappable to the subclass.
39 67
40 // Retrieve (or create) the v8 wrapper object cooresponding to this object. 68 // Retrieve (or create) the v8 wrapper object cooresponding to this object.
41 // To customize the wrapper created for a subclass, override GetWrapperInfo() 69 // To customize the wrapper created for a subclass, override GetWrapperInfo()
42 // instead of overriding this function. 70 // instead of overriding this function.
43 v8::Handle<v8::Object> GetWrapper(v8::Isolate* isolate); 71 v8::Handle<v8::Object> GetWrapper(v8::Isolate* isolate) {
44 72 return GetWrapperImpl(isolate, &kWrapperInfo);
45 // Subclasses should have private constructors and expose a static Create 73 }
46 // function that returns a gin::Handle. Forcing creators through this static
47 // Create function will enforce that clients actually create a wrapper for
48 // the object. If clients fail to create a wrapper for a wrappable object,
49 // the object will leak because we use the weak callback from the wrapper
50 // as the signal to delete the wrapped object.
51 74
52 protected: 75 protected:
53 Wrappable(); 76 Wrappable() {}
54 virtual ~Wrappable(); 77 ~Wrappable() {}
55
56 private:
57 static void WeakCallback(
58 const v8::WeakCallbackData<v8::Object, Wrappable>& data);
59 v8::Handle<v8::Object> CreateWrapper(v8::Isolate* isolate);
60
61 v8::Persistent<v8::Object> wrapper_; // Weak
62
63 DISALLOW_COPY_AND_ASSIGN(Wrappable); 78 DISALLOW_COPY_AND_ASSIGN(Wrappable);
64 }; 79 };
65 80
81
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
66 // This converter handles any subclass of Wrappable. 89 // This converter handles any subclass of Wrappable.
67 template<typename T> 90 template<typename T>
68 struct Converter<T*, typename base::enable_if< 91 struct Converter<T*, typename base::enable_if<
69 base::is_convertible<T*, Wrappable*>::value>::type> { 92 base::is_convertible<T*, Wrappable<T>*>::value>::type> {
70 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, T* val) { 93 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, T* val) {
71 return val->GetWrapper(isolate); 94 return val->GetWrapper(isolate);
72 } 95 }
73 96
74 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, T** out) { 97 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, T** out) {
75 if (!val->IsObject()) 98 *out = static_cast<T*>(internal::FromV8Impl(isolate, val,
76 return false; 99 &T::kWrapperInfo));
77 v8::Handle<v8::Object> obj = v8::Handle<v8::Object>::Cast(val); 100 return *out != NULL;
78 WrapperInfo* info = WrapperInfo::From(obj);
79
80 // If this fails, the object is not managed by Gin. It is either a normal JS
81 // object that's not wrapping any external C++ object, or it is wrapping
82 // some C++ object, but that object isn't managed by Gin (maybe Blink).
83 if (!info)
84 return false;
85
86 // If this fails, the object is managed by Gin, but it's not wrapping an
87 // instance of T.
88 if (info != &T::kWrapperInfo)
89 return false;
90
91 void* pointer = obj->GetAlignedPointerFromInternalField(kEncodedValueIndex);
92 T* result = static_cast<T*>(pointer);
93
94 // If this fails, something fishy is going on. |info| should have come from
95 // T::GetWrapperInfo(), but somehow is now different than it. So panic.
96 CHECK(result->GetWrapperInfo() == info);
97 *out = result;
98 return true;
99 } 101 }
100 }; 102 };
101 103
102 } // namespace gin 104 } // namespace gin
103 105
104 #endif // GIN_WRAPPABLE_H_ 106 #endif // GIN_WRAPPABLE_H_
OLDNEW
« no previous file with comments | « gin/object_template_builder.h ('k') | gin/wrappable.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698