[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 | namespace internal { |
[email protected] | 93f9f360 | 2013-11-21 18:38:51 | [diff] [blame] | 16 | |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 17 | GIN_EXPORT void* FromV8Impl(v8::Isolate* isolate, |
| 18 | v8::Handle<v8::Value> val, |
| 19 | WrapperInfo* info); |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 20 | |
| 21 | } // namespace internal |
| 22 | |
| 23 | |
| 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. |
| 26 | // |
| 27 | // USAGE: |
| 28 | // // my_class.h |
| 29 | // class MyClass : Wrappable<MyClass> { |
[email protected] | cb6c2bb | 2013-12-17 21:47:04 | [diff] [blame] | 30 | // public: |
| 31 | // static WrapperInfo kWrapperInfo; |
| 32 | // |
| 33 | // // Optional, only required if non-empty template should be used. |
[email protected] | 75a2090 | 2013-12-19 15:18:04 | [diff] [blame] | 34 | // virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder( |
[email protected] | cb6c2bb | 2013-12-17 21:47:04 | [diff] [blame] | 35 | // v8::Isolate* isolate); |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 36 | // ... |
| 37 | // }; |
| 38 | // |
| 39 | // // my_class.cc |
[email protected] | 75a2090 | 2013-12-19 15:18:04 | [diff] [blame] | 40 | // WrapperInfo MyClass::kWrapperInfo = {kEmbedderNativeGin}; |
[email protected] | cb6c2bb | 2013-12-17 21:47:04 | [diff] [blame] | 41 | // |
[email protected] | 75a2090 | 2013-12-19 15:18:04 | [diff] [blame] | 42 | // gin::ObjectTemplateBuilder MyClass::GetObjectTemplateBuilder( |
[email protected] | cb6c2bb | 2013-12-17 21:47:04 | [diff] [blame] | 43 | // v8::Isolate* isolate) { |
[email protected] | 75a2090 | 2013-12-19 15:18:04 | [diff] [blame] | 44 | // return Wrappable<MyClass>::GetObjectTemplateBuilder(isolate) |
| 45 | // .SetValue("foobar", 42); |
[email protected] | cb6c2bb | 2013-12-17 21:47:04 | [diff] [blame] | 46 | // } |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 47 | // |
| 48 | // Subclasses should also typically have private constructors and expose a |
| 49 | // static Create function that returns a gin::Handle. Forcing creators through |
| 50 | // 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 |
| 52 | // object, the object will leak because we use the weak callback from the |
| 53 | // wrapper as the signal to delete the wrapped object. |
| 54 | template<typename T> |
| 55 | class Wrappable; |
| 56 | |
[email protected] | 13c977e6 | 2013-12-19 00:52:44 | [diff] [blame] | 57 | class ObjectTemplateBuilder; |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 58 | |
| 59 | // Non-template base class to share code between templates instances. |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 60 | class GIN_EXPORT WrappableBase { |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 61 | protected: |
| 62 | WrappableBase(); |
[email protected] | 695af7f5 | 2013-12-11 19:41:01 | [diff] [blame] | 63 | virtual ~WrappableBase(); |
[email protected] | cb6c2bb | 2013-12-17 21:47:04 | [diff] [blame] | 64 | |
[email protected] | 13c977e6 | 2013-12-19 00:52:44 | [diff] [blame] | 65 | virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate); |
[email protected] | cb6c2bb | 2013-12-17 21:47:04 | [diff] [blame] | 66 | |
[email protected] | 13c977e6 | 2013-12-19 00:52:44 | [diff] [blame] | 67 | v8::Handle<v8::Object> GetWrapperImpl(v8::Isolate* isolate, |
| 68 | WrapperInfo* wrapper_info); |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 69 | |
| 70 | private: |
| 71 | static void WeakCallback( |
| 72 | const v8::WeakCallbackData<v8::Object, WrappableBase>& data); |
| 73 | |
[email protected] | cb6c2bb | 2013-12-17 21:47:04 | [diff] [blame] | 74 | v8::Persistent<v8::Object> wrapper_; // Weak |
| 75 | |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 76 | DISALLOW_COPY_AND_ASSIGN(WrappableBase); |
| 77 | }; |
| 78 | |
| 79 | |
| 80 | template<typename T> |
| 81 | class Wrappable : public WrappableBase { |
| 82 | public: |
[email protected] | 60531d5 | 2013-11-27 02:10:15 | [diff] [blame] | 83 | // Retrieve (or create) the v8 wrapper object cooresponding to this object. |
| 84 | // To customize the wrapper created for a subclass, override GetWrapperInfo() |
| 85 | // instead of overriding this function. |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 86 | v8::Handle<v8::Object> GetWrapper(v8::Isolate* isolate) { |
[email protected] | 13c977e6 | 2013-12-19 00:52:44 | [diff] [blame] | 87 | return GetWrapperImpl(isolate, &T::kWrapperInfo); |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 88 | } |
[email protected] | d379b0c | 2013-12-05 05:48:01 | [diff] [blame] | 89 | |
[email protected] | 93f9f360 | 2013-11-21 18:38:51 | [diff] [blame] | 90 | protected: |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 91 | Wrappable() {} |
[email protected] | 695af7f5 | 2013-12-11 19:41:01 | [diff] [blame] | 92 | virtual ~Wrappable() {} |
| 93 | |
| 94 | private: |
[email protected] | 93f9f360 | 2013-11-21 18:38:51 | [diff] [blame] | 95 | DISALLOW_COPY_AND_ASSIGN(Wrappable); |
| 96 | }; |
| 97 | |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 98 | |
[email protected] | cf76c84a | 2013-12-06 14:07:07 | [diff] [blame] | 99 | // This converter handles any subclass of Wrappable. |
[email protected] | 93f9f360 | 2013-11-21 18:38:51 | [diff] [blame] | 100 | template<typename T> |
[email protected] | cf76c84a | 2013-12-06 14:07:07 | [diff] [blame] | 101 | struct Converter<T*, typename base::enable_if< |
[email protected] | 13c977e6 | 2013-12-19 00:52:44 | [diff] [blame] | 102 | base::is_convertible<T*, WrappableBase*>::value>::type> { |
[email protected] | 93f9f360 | 2013-11-21 18:38:51 | [diff] [blame] | 103 | static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, T* val) { |
[email protected] | cf76c84a | 2013-12-06 14:07:07 | [diff] [blame] | 104 | return val->GetWrapper(isolate); |
[email protected] | 93f9f360 | 2013-11-21 18:38:51 | [diff] [blame] | 105 | } |
[email protected] | cf76c84a | 2013-12-06 14:07:07 | [diff] [blame] | 106 | |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 107 | static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, T** out) { |
[email protected] | 75a2090 | 2013-12-19 15:18:04 | [diff] [blame] | 108 | *out = static_cast<T*>(static_cast<WrappableBase*>( |
| 109 | internal::FromV8Impl(isolate, val, &T::kWrapperInfo))); |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 110 | return *out != NULL; |
[email protected] | 93f9f360 | 2013-11-21 18:38:51 | [diff] [blame] | 111 | } |
| 112 | }; |
| 113 | |
| 114 | } // namespace gin |
| 115 | |
| 116 | #endif // GIN_WRAPPABLE_H_ |