Avi Drissman | 468e51b6 | 2022-09-13 20:47:01 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | d379b0c | 2013-12-05 05:48:01 | [diff] [blame] | 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_HANDLE_H_ |
| 6 | #define GIN_HANDLE_H_ |
| 7 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 8 | #include "base/memory/raw_ptr.h" |
[email protected] | d379b0c | 2013-12-05 05:48:01 | [diff] [blame] | 9 | #include "gin/converter.h" |
| 10 | |
| 11 | namespace gin { |
| 12 | |
| 13 | // You can use gin::Handle on the stack to retain a gin::Wrappable object. |
| 14 | // Currently we don't have a mechanism for retaining a gin::Wrappable object |
| 15 | // in the C++ heap because strong references from C++ to V8 can cause memory |
| 16 | // leaks. |
| 17 | template<typename T> |
| 18 | class Handle { |
| 19 | public: |
Lukasz Anforowicz | c695e53 | 2020-06-09 02:09:45 | [diff] [blame] | 20 | Handle() : object_(nullptr) {} |
[email protected] | d379b0c | 2013-12-05 05:48:01 | [diff] [blame] | 21 | |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 22 | Handle(v8::Local<v8::Value> wrapper, T* object) |
[email protected] | d379b0c | 2013-12-05 05:48:01 | [diff] [blame] | 23 | : wrapper_(wrapper), |
| 24 | object_(object) { |
| 25 | } |
| 26 | |
| 27 | bool IsEmpty() const { return !object_; } |
| 28 | |
| 29 | void Clear() { |
| 30 | wrapper_.Clear(); |
| 31 | object_ = NULL; |
| 32 | } |
| 33 | |
| 34 | T* operator->() const { return object_; } |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 35 | v8::Local<v8::Value> ToV8() const { return wrapper_; } |
[email protected] | d379b0c | 2013-12-05 05:48:01 | [diff] [blame] | 36 | T* get() const { return object_; } |
| 37 | |
| 38 | private: |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 39 | v8::Local<v8::Value> wrapper_; |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 40 | raw_ptr<T> object_; |
[email protected] | d379b0c | 2013-12-05 05:48:01 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | template<typename T> |
| 44 | struct Converter<gin::Handle<T> > { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 45 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
[email protected] | d379b0c | 2013-12-05 05:48:01 | [diff] [blame] | 46 | const gin::Handle<T>& val) { |
| 47 | return val.ToV8(); |
| 48 | } |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 49 | static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val, |
[email protected] | d379b0c | 2013-12-05 05:48:01 | [diff] [blame] | 50 | gin::Handle<T>* out) { |
| 51 | T* object = NULL; |
[email protected] | 481d249 | 2013-12-13 23:55:25 | [diff] [blame] | 52 | if (!Converter<T*>::FromV8(isolate, val, &object)) { |
| 53 | return false; |
| 54 | } |
| 55 | *out = gin::Handle<T>(val, object); |
| 56 | return true; |
[email protected] | d379b0c | 2013-12-05 05:48:01 | [diff] [blame] | 57 | } |
| 58 | }; |
| 59 | |
| 60 | // This function is a convenient way to create a handle from a raw pointer |
| 61 | // without having to write out the type of the object explicitly. |
| 62 | template<typename T> |
| 63 | gin::Handle<T> CreateHandle(v8::Isolate* isolate, T* object) { |
Ken Rockot | 2b0f0765 | 2017-04-12 19:10:49 | [diff] [blame] | 64 | v8::Local<v8::Object> wrapper; |
| 65 | if (!object->GetWrapper(isolate).ToLocal(&wrapper) || wrapper.IsEmpty()) |
[email protected] | e0719edc | 2014-02-28 14:48:25 | [diff] [blame] | 66 | return gin::Handle<T>(); |
| 67 | return gin::Handle<T>(wrapper, object); |
[email protected] | d379b0c | 2013-12-05 05:48:01 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | } // namespace gin |
| 71 | |
| 72 | #endif // GIN_HANDLE_H_ |