Chromium Code Reviews| 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" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 // | 46 // |
| 47 // Subclasses should also typically have private constructors and expose a | 47 // Subclasses should also typically have private constructors and expose a |
| 48 // static Create function that returns a gin::Handle. Forcing creators through | 48 // static Create function that returns a gin::Handle. Forcing creators through |
| 49 // this static Create function will enforce that clients actually create a | 49 // this static Create function will enforce that clients actually create a |
| 50 // 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 |
| 51 // 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 |
| 52 // wrapper as the signal to delete the wrapped object. | 52 // wrapper as the signal to delete the wrapped object. |
| 53 template<typename T> | 53 template<typename T> |
| 54 class Wrappable; | 54 class Wrappable; |
| 55 | 55 |
| 56 class ObjectTemplateBuilder; | |
| 56 | 57 |
| 57 // Non-template base class to share code between templates instances. | 58 // Non-template base class to share code between templates instances. |
| 58 class GIN_EXPORT WrappableBase { | 59 class GIN_EXPORT WrappableBase { |
| 59 protected: | 60 protected: |
| 60 typedef v8::Local<v8::ObjectTemplate>(*GetObjectTemplateFunction)( | |
| 61 v8::Isolate*); | |
| 62 | |
| 63 WrappableBase(); | 61 WrappableBase(); |
| 64 virtual ~WrappableBase(); | 62 virtual ~WrappableBase(); |
| 65 | 63 |
| 66 v8::Handle<v8::Object> GetWrapperImpl( | 64 virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate); |
| 67 v8::Isolate* isolate, | |
| 68 WrapperInfo* wrapper_info, | |
| 69 GetObjectTemplateFunction template_getter); | |
| 70 | 65 |
| 71 static v8::Local<v8::ObjectTemplate> GetObjectTemplate(v8::Isolate* isolate); | 66 v8::Handle<v8::Object> GetWrapperImpl(v8::Isolate* isolate, |
| 67 WrapperInfo* wrapper_info); | |
| 72 | 68 |
| 73 private: | 69 private: |
| 74 static void WeakCallback( | 70 static void WeakCallback( |
| 75 const v8::WeakCallbackData<v8::Object, WrappableBase>& data); | 71 const v8::WeakCallbackData<v8::Object, WrappableBase>& data); |
| 76 | 72 |
| 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 | 73 v8::Persistent<v8::Object> wrapper_; // Weak |
| 83 | 74 |
| 84 DISALLOW_COPY_AND_ASSIGN(WrappableBase); | 75 DISALLOW_COPY_AND_ASSIGN(WrappableBase); |
| 85 }; | 76 }; |
| 86 | 77 |
| 87 | 78 |
| 88 template<typename T> | 79 template<typename T> |
| 89 class Wrappable : public WrappableBase { | 80 class Wrappable : public WrappableBase { |
| 90 public: | 81 public: |
| 91 // Retrieve (or create) the v8 wrapper object cooresponding to this object. | 82 // Retrieve (or create) the v8 wrapper object cooresponding to this object. |
| 92 // To customize the wrapper created for a subclass, override GetWrapperInfo() | 83 // To customize the wrapper created for a subclass, override GetWrapperInfo() |
| 93 // instead of overriding this function. | 84 // instead of overriding this function. |
| 94 v8::Handle<v8::Object> GetWrapper(v8::Isolate* isolate) { | 85 v8::Handle<v8::Object> GetWrapper(v8::Isolate* isolate) { |
| 95 return GetWrapperImpl(isolate, &T::kWrapperInfo, &T::GetObjectTemplate); | 86 return GetWrapperImpl(isolate, &T::kWrapperInfo); |
| 96 } | 87 } |
| 97 | 88 |
| 98 protected: | 89 protected: |
| 99 Wrappable() {} | 90 Wrappable() {} |
| 100 virtual ~Wrappable() {} | 91 virtual ~Wrappable() {} |
| 101 | 92 |
| 102 private: | 93 private: |
| 103 DISALLOW_COPY_AND_ASSIGN(Wrappable); | 94 DISALLOW_COPY_AND_ASSIGN(Wrappable); |
| 104 }; | 95 }; |
| 105 | 96 |
| 106 | 97 |
| 107 // This converter handles any subclass of Wrappable. | 98 // This converter handles any subclass of Wrappable. |
| 108 template<typename T> | 99 template<typename T> |
| 109 struct Converter<T*, typename base::enable_if< | 100 struct Converter<T*, typename base::enable_if< |
| 110 base::is_convertible<T*, Wrappable<T>*>::value>::type> { | 101 base::is_convertible<T*, WrappableBase*>::value>::type> { |
|
Aaron Boodman
2013/12/18 19:12:18
This was making it so that only direct descendants
| |
| 111 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, T* val) { | 102 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, T* val) { |
| 112 return val->GetWrapper(isolate); | 103 return val->GetWrapper(isolate); |
| 113 } | 104 } |
| 114 | 105 |
| 115 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, T** out) { | 106 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, T** out) { |
| 116 *out = static_cast<T*>(internal::FromV8Impl(isolate, val, | 107 *out = static_cast<T*>(internal::FromV8Impl(isolate, val, |
| 117 &T::kWrapperInfo)); | 108 &T::kWrapperInfo)); |
| 118 return *out != NULL; | 109 return *out != NULL; |
| 119 } | 110 } |
| 120 }; | 111 }; |
| 121 | 112 |
| 122 } // namespace gin | 113 } // namespace gin |
| 123 | 114 |
| 124 #endif // GIN_WRAPPABLE_H_ | 115 #endif // GIN_WRAPPABLE_H_ |
| OLD | NEW |