| 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 { | |
| 16 | |
| 17 GIN_EXPORT void* FromV8Impl(v8::Isolate* isolate, | |
| 18 v8::Local<v8::Value> val, | |
| 19 WrapperInfo* info); | |
| 20 | |
| 21 } // namespace internal | |
| 22 | |
| 23 | |
| 24 // Wrappable is a base class for C++ objects that have corresponding v8 wrapper | 15 // 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. | 16 // objects. To retain a Wrappable object on the stack, use a gin::Handle. |
| 26 // | 17 // |
| 27 // USAGE: | 18 // USAGE: |
| 28 // // my_class.h | 19 // // my_class.h |
| 29 // class MyClass : Wrappable<MyClass> { | 20 // class MyClass : Wrappable<MyClass> { |
| 30 // public: | 21 // public: |
| 31 // static WrapperInfo kWrapperInfo; | 22 // static WrapperInfo kWrapperInfo; |
| 32 // | 23 // |
| 33 // // Optional, only required if non-empty template should be used. | 24 // // Optional, only required if non-empty template should be used. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 44 // return Wrappable<MyClass>::GetObjectTemplateBuilder(isolate) | 35 // return Wrappable<MyClass>::GetObjectTemplateBuilder(isolate) |
| 45 // .SetValue("foobar", 42); | 36 // .SetValue("foobar", 42); |
| 46 // } | 37 // } |
| 47 // | 38 // |
| 48 // Subclasses should also typically have private constructors and expose a | 39 // Subclasses should also typically have private constructors and expose a |
| 49 // static Create function that returns a gin::Handle. Forcing creators through | 40 // static Create function that returns a gin::Handle. Forcing creators through |
| 50 // this static Create function will enforce that clients actually create a | 41 // this static Create function will enforce that clients actually create a |
| 51 // wrapper for the object. If clients fail to create a wrapper for a wrappable | 42 // wrapper for the object. If clients fail to create a wrapper for a wrappable |
| 52 // object, the object will leak because we use the weak callback from the | 43 // object, the object will leak because we use the weak callback from the |
| 53 // wrapper as the signal to delete the wrapped object. | 44 // wrapper as the signal to delete the wrapped object. |
| 54 template<typename T> | 45 // |
| 55 class Wrappable; | 46 // Wrappable<T> explicitly does not support further subclassing of T. |
| 47 // Subclasses of Wrappable<T> should be declared final. Because Wrappable<T> |
| 48 // caches the object template using &T::kWrapperInfo as the key, all subclasses |
| 49 // would share a single object template. This will lead to hard to debug crashes |
| 50 // that look like use-after-free errors. |
| 51 |
| 52 namespace internal { |
| 53 |
| 54 GIN_EXPORT void* FromV8Impl(v8::Isolate* isolate, |
| 55 v8::Local<v8::Value> val, |
| 56 WrapperInfo* info); |
| 57 |
| 58 } // namespace internal |
| 56 | 59 |
| 57 class ObjectTemplateBuilder; | 60 class ObjectTemplateBuilder; |
| 58 | 61 |
| 59 // Non-template base class to share code between templates instances. | 62 // Non-template base class to share code between templates instances. |
| 60 class GIN_EXPORT WrappableBase { | 63 class GIN_EXPORT WrappableBase { |
| 61 protected: | 64 protected: |
| 62 WrappableBase(); | 65 WrappableBase(); |
| 63 virtual ~WrappableBase(); | 66 virtual ~WrappableBase(); |
| 64 | 67 |
| 68 // Overrides of this method should be declared final and not overridden again. |
| 65 virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate); | 69 virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate); |
| 66 | 70 |
| 67 v8::Local<v8::Object> GetWrapperImpl(v8::Isolate* isolate, | 71 v8::Local<v8::Object> GetWrapperImpl(v8::Isolate* isolate, |
| 68 WrapperInfo* wrapper_info); | 72 WrapperInfo* wrapper_info); |
| 69 | 73 |
| 70 private: | 74 private: |
| 71 static void FirstWeakCallback( | 75 static void FirstWeakCallback( |
| 72 const v8::WeakCallbackInfo<WrappableBase>& data); | 76 const v8::WeakCallbackInfo<WrappableBase>& data); |
| 73 static void SecondWeakCallback( | 77 static void SecondWeakCallback( |
| 74 const v8::WeakCallbackInfo<WrappableBase>& data); | 78 const v8::WeakCallbackInfo<WrappableBase>& data); |
| 75 | 79 |
| 76 v8::Global<v8::Object> wrapper_; // Weak | 80 v8::Global<v8::Object> wrapper_; // Weak |
| 77 | 81 |
| 78 DISALLOW_COPY_AND_ASSIGN(WrappableBase); | 82 DISALLOW_COPY_AND_ASSIGN(WrappableBase); |
| 79 }; | 83 }; |
| 80 | 84 |
| 81 | 85 |
| 82 template<typename T> | 86 template<typename T> |
| 83 class Wrappable : public WrappableBase { | 87 class Wrappable : public WrappableBase { |
| 84 public: | 88 public: |
| 85 // Retrieve (or create) the v8 wrapper object cooresponding to this object. | 89 // Retrieve (or create) the v8 wrapper object cooresponding to this object. |
| 86 // To customize the wrapper created for a subclass, override GetWrapperInfo() | |
| 87 // instead of overriding this function. | |
| 88 v8::Local<v8::Object> GetWrapper(v8::Isolate* isolate) { | 90 v8::Local<v8::Object> GetWrapper(v8::Isolate* isolate) { |
| 89 return GetWrapperImpl(isolate, &T::kWrapperInfo); | 91 return GetWrapperImpl(isolate, &T::kWrapperInfo); |
| 90 } | 92 } |
| 91 | 93 |
| 92 protected: | 94 protected: |
| 93 Wrappable() {} | 95 Wrappable() {} |
| 94 virtual ~Wrappable() {} | 96 virtual ~Wrappable() {} |
| 95 | 97 |
| 96 private: | 98 private: |
| 97 DISALLOW_COPY_AND_ASSIGN(Wrappable); | 99 DISALLOW_COPY_AND_ASSIGN(Wrappable); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 109 static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val, T** out) { | 111 static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val, T** out) { |
| 110 *out = static_cast<T*>(static_cast<WrappableBase*>( | 112 *out = static_cast<T*>(static_cast<WrappableBase*>( |
| 111 internal::FromV8Impl(isolate, val, &T::kWrapperInfo))); | 113 internal::FromV8Impl(isolate, val, &T::kWrapperInfo))); |
| 112 return *out != NULL; | 114 return *out != NULL; |
| 113 } | 115 } |
| 114 }; | 116 }; |
| 115 | 117 |
| 116 } // namespace gin | 118 } // namespace gin |
| 117 | 119 |
| 118 #endif // GIN_WRAPPABLE_H_ | 120 #endif // GIN_WRAPPABLE_H_ |
| OLD | NEW |