[email protected] | 93f9f360 | 2013-11-21 18:38:51 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef GIN_WRAPPABLE_H_ |
| 6 | #define GIN_WRAPPABLE_H_ |
| 7 | |
[email protected] | cf76c84a | 2013-12-06 14:07:07 | [diff] [blame] | 8 | #include "base/template_util.h" |
[email protected] | 93f9f360 | 2013-11-21 18:38:51 | [diff] [blame] | 9 | #include "gin/converter.h" |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 10 | #include "gin/gin_export.h" |
[email protected] | 93f9f360 | 2013-11-21 18:38:51 | [diff] [blame] | 11 | #include "gin/public/wrapper_info.h" |
| 12 | |
| 13 | namespace gin { |
| 14 | |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 15 | // Wrappable is a base class for C++ objects that have corresponding v8 wrapper |
| 16 | // objects. To retain a Wrappable object on the stack, use a gin::Handle. |
| 17 | // |
| 18 | // USAGE: |
| 19 | // // my_class.h |
| 20 | // class MyClass : Wrappable<MyClass> { |
[email protected] | cb6c2bb | 2013-12-17 21:47:04 | [diff] [blame] | 21 | // public: |
| 22 | // static WrapperInfo kWrapperInfo; |
| 23 | // |
| 24 | // // Optional, only required if non-empty template should be used. |
[email protected] | 75a2090 | 2013-12-19 15:18:04 | [diff] [blame] | 25 | // virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder( |
[email protected] | cb6c2bb | 2013-12-17 21:47:04 | [diff] [blame] | 26 | // v8::Isolate* isolate); |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 27 | // ... |
| 28 | // }; |
| 29 | // |
| 30 | // // my_class.cc |
[email protected] | 75a2090 | 2013-12-19 15:18:04 | [diff] [blame] | 31 | // WrapperInfo MyClass::kWrapperInfo = {kEmbedderNativeGin}; |
[email protected] | cb6c2bb | 2013-12-17 21:47:04 | [diff] [blame] | 32 | // |
[email protected] | 75a2090 | 2013-12-19 15:18:04 | [diff] [blame] | 33 | // gin::ObjectTemplateBuilder MyClass::GetObjectTemplateBuilder( |
[email protected] | cb6c2bb | 2013-12-17 21:47:04 | [diff] [blame] | 34 | // v8::Isolate* isolate) { |
[email protected] | 75a2090 | 2013-12-19 15:18:04 | [diff] [blame] | 35 | // return Wrappable<MyClass>::GetObjectTemplateBuilder(isolate) |
| 36 | // .SetValue("foobar", 42); |
[email protected] | cb6c2bb | 2013-12-17 21:47:04 | [diff] [blame] | 37 | // } |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 38 | // |
| 39 | // Subclasses should also typically have private constructors and expose a |
| 40 | // static Create function that returns a gin::Handle. Forcing creators through |
| 41 | // this static Create function will enforce that clients actually create a |
| 42 | // wrapper for the object. If clients fail to create a wrapper for a wrappable |
| 43 | // object, the object will leak because we use the weak callback from the |
| 44 | // wrapper as the signal to delete the wrapped object. |
tommycli | 425d6965 | 2015-06-11 22:20:14 | [diff] [blame] | 45 | // |
| 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 |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 59 | |
[email protected] | 13c977e6 | 2013-12-19 00:52:44 | [diff] [blame] | 60 | class ObjectTemplateBuilder; |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 61 | |
| 62 | // Non-template base class to share code between templates instances. |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 63 | class GIN_EXPORT WrappableBase { |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 64 | protected: |
| 65 | WrappableBase(); |
[email protected] | 695af7f5 | 2013-12-11 19:41:01 | [diff] [blame] | 66 | virtual ~WrappableBase(); |
[email protected] | cb6c2bb | 2013-12-17 21:47:04 | [diff] [blame] | 67 | |
tommycli | 425d6965 | 2015-06-11 22:20:14 | [diff] [blame] | 68 | // Overrides of this method should be declared final and not overridden again. |
[email protected] | 13c977e6 | 2013-12-19 00:52:44 | [diff] [blame] | 69 | virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate); |
[email protected] | cb6c2bb | 2013-12-17 21:47:04 | [diff] [blame] | 70 | |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 71 | v8::Local<v8::Object> GetWrapperImpl(v8::Isolate* isolate, |
[email protected] | 13c977e6 | 2013-12-19 00:52:44 | [diff] [blame] | 72 | WrapperInfo* wrapper_info); |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 73 | |
| 74 | private: |
dcarney | 99ade908 | 2015-04-22 09:55:42 | [diff] [blame] | 75 | static void FirstWeakCallback( |
| 76 | const v8::WeakCallbackInfo<WrappableBase>& data); |
| 77 | static void SecondWeakCallback( |
| 78 | const v8::WeakCallbackInfo<WrappableBase>& data); |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 79 | |
dcarney | 99ade908 | 2015-04-22 09:55:42 | [diff] [blame] | 80 | v8::Global<v8::Object> wrapper_; // Weak |
[email protected] | cb6c2bb | 2013-12-17 21:47:04 | [diff] [blame] | 81 | |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 82 | DISALLOW_COPY_AND_ASSIGN(WrappableBase); |
| 83 | }; |
| 84 | |
| 85 | |
| 86 | template<typename T> |
| 87 | class Wrappable : public WrappableBase { |
| 88 | public: |
[email protected] | 60531d5 | 2013-11-27 02:10:15 | [diff] [blame] | 89 | // Retrieve (or create) the v8 wrapper object cooresponding to this object. |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 90 | v8::Local<v8::Object> GetWrapper(v8::Isolate* isolate) { |
[email protected] | 13c977e6 | 2013-12-19 00:52:44 | [diff] [blame] | 91 | return GetWrapperImpl(isolate, &T::kWrapperInfo); |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 92 | } |
[email protected] | d379b0c | 2013-12-05 05:48:01 | [diff] [blame] | 93 | |
[email protected] | 93f9f360 | 2013-11-21 18:38:51 | [diff] [blame] | 94 | protected: |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 95 | Wrappable() {} |
[email protected] | 695af7f5 | 2013-12-11 19:41:01 | [diff] [blame] | 96 | virtual ~Wrappable() {} |
| 97 | |
| 98 | private: |
[email protected] | 93f9f360 | 2013-11-21 18:38:51 | [diff] [blame] | 99 | DISALLOW_COPY_AND_ASSIGN(Wrappable); |
| 100 | }; |
| 101 | |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 102 | |
[email protected] | cf76c84a | 2013-12-06 14:07:07 | [diff] [blame] | 103 | // This converter handles any subclass of Wrappable. |
[email protected] | 93f9f360 | 2013-11-21 18:38:51 | [diff] [blame] | 104 | template<typename T> |
[email protected] | cf76c84a | 2013-12-06 14:07:07 | [diff] [blame] | 105 | struct Converter<T*, typename base::enable_if< |
[email protected] | 13c977e6 | 2013-12-19 00:52:44 | [diff] [blame] | 106 | base::is_convertible<T*, WrappableBase*>::value>::type> { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 107 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, T* val) { |
[email protected] | cf76c84a | 2013-12-06 14:07:07 | [diff] [blame] | 108 | return val->GetWrapper(isolate); |
[email protected] | 93f9f360 | 2013-11-21 18:38:51 | [diff] [blame] | 109 | } |
[email protected] | cf76c84a | 2013-12-06 14:07:07 | [diff] [blame] | 110 | |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 111 | static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val, T** out) { |
[email protected] | 75a2090 | 2013-12-19 15:18:04 | [diff] [blame] | 112 | *out = static_cast<T*>(static_cast<WrappableBase*>( |
| 113 | internal::FromV8Impl(isolate, val, &T::kWrapperInfo))); |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 114 | return *out != NULL; |
[email protected] | 93f9f360 | 2013-11-21 18:38:51 | [diff] [blame] | 115 | } |
| 116 | }; |
| 117 | |
| 118 | } // namespace gin |
| 119 | |
| 120 | #endif // GIN_WRAPPABLE_H_ |