| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 InspectorWrapper_h | 5 #ifndef InspectorWrapper_h |
| 6 #define InspectorWrapper_h | 6 #define InspectorWrapper_h |
| 7 | 7 |
| 8 #include "platform/inspector_protocol/Collections.h" | 8 #include "platform/inspector_protocol/Collections.h" |
| 9 #include "wtf/PassOwnPtr.h" | |
| 10 #include <v8.h> | 9 #include <v8.h> |
| 11 | 10 |
| 12 namespace blink { | 11 namespace blink { |
| 13 | 12 |
| 14 class InspectorWrapperBase { | 13 class InspectorWrapperBase { |
| 15 public: | 14 public: |
| 16 struct V8MethodConfiguration { | 15 struct V8MethodConfiguration { |
| 17 const char* name; | 16 const char* name; |
| 18 v8::FunctionCallback callback; | 17 v8::FunctionCallback callback; |
| 19 }; | 18 }; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 37 public: | 36 public: |
| 38 class WeakCallbackData final { | 37 class WeakCallbackData final { |
| 39 public: | 38 public: |
| 40 WeakCallbackData(v8::Isolate* isolate, T* impl, v8::Local<v8::Object> wr
apper) | 39 WeakCallbackData(v8::Isolate* isolate, T* impl, v8::Local<v8::Object> wr
apper) |
| 41 : m_impl(impl) | 40 : m_impl(impl) |
| 42 , m_persistent(isolate, wrapper) | 41 , m_persistent(isolate, wrapper) |
| 43 { | 42 { |
| 44 m_persistent.SetWeak(this, &WeakCallbackData::weakCallback, v8::Weak
CallbackType::kParameter); | 43 m_persistent.SetWeak(this, &WeakCallbackData::weakCallback, v8::Weak
CallbackType::kParameter); |
| 45 } | 44 } |
| 46 | 45 |
| 47 WeakCallbackData(v8::Isolate* isolate, PassOwnPtr<T> impl, v8::Local<v8:
:Object> wrapper) | |
| 48 : m_impl(impl.get()) | |
| 49 , m_implOwn(impl) | |
| 50 , m_persistent(isolate, wrapper) | |
| 51 { | |
| 52 m_persistent.SetWeak(this, &WeakCallbackData::weakCallback, v8::Weak
CallbackType::kParameter); | |
| 53 } | |
| 54 | |
| 55 T* m_impl; | 46 T* m_impl; |
| 56 OwnPtr<T> m_implOwn; | 47 OwnPtr<T> m_implOwn; |
| 57 | 48 |
| 58 private: | 49 private: |
| 59 static void weakCallback(const v8::WeakCallbackInfo<WeakCallbackData>& i
nfo) | 50 static void weakCallback(const v8::WeakCallbackInfo<WeakCallbackData>& i
nfo) |
| 60 { | 51 { |
| 61 delete info.GetParameter(); | 52 delete info.GetParameter(); |
| 62 } | 53 } |
| 63 | 54 |
| 64 v8::Global<v8::Object> m_persistent; | 55 v8::Global<v8::Object> m_persistent; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 76 if (result.IsEmpty()) | 67 if (result.IsEmpty()) |
| 77 return v8::Local<v8::Object>(); | 68 return v8::Local<v8::Object>(); |
| 78 v8::Isolate* isolate = context->GetIsolate(); | 69 v8::Isolate* isolate = context->GetIsolate(); |
| 79 v8::Local<v8::External> objectReference = v8::External::New(isolate, new
WeakCallbackData(isolate, object, result)); | 70 v8::Local<v8::External> objectReference = v8::External::New(isolate, new
WeakCallbackData(isolate, object, result)); |
| 80 | 71 |
| 81 v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, v8::Str
ing::NewFromUtf8(isolate, hiddenPropertyName, v8::NewStringType::kInternalized).
ToLocalChecked()); | 72 v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, v8::Str
ing::NewFromUtf8(isolate, hiddenPropertyName, v8::NewStringType::kInternalized).
ToLocalChecked()); |
| 82 result->SetPrivate(context, privateKey, objectReference); | 73 result->SetPrivate(context, privateKey, objectReference); |
| 83 return result; | 74 return result; |
| 84 } | 75 } |
| 85 | 76 |
| 86 static v8::Local<v8::Object> wrap(v8::Local<v8::FunctionTemplate> constructo
rTemplate, v8::Local<v8::Context> context, PassOwnPtr<T> object) | |
| 87 { | |
| 88 v8::Context::Scope contextScope(context); | |
| 89 v8::Local<v8::Object> result = InspectorWrapperBase::createWrapper(const
ructorTemplate, context); | |
| 90 if (result.IsEmpty()) | |
| 91 return v8::Local<v8::Object>(); | |
| 92 v8::Isolate* isolate = context->GetIsolate(); | |
| 93 v8::Local<v8::External> objectReference = v8::External::New(isolate, new
WeakCallbackData(isolate, object, result)); | |
| 94 | |
| 95 v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, v8::Str
ing::NewFromUtf8(isolate, hiddenPropertyName, v8::NewStringType::kInternalized).
ToLocalChecked()); | |
| 96 result->SetPrivate(context, privateKey, objectReference); | |
| 97 return result; | |
| 98 } | |
| 99 | |
| 100 static T* unwrap(v8::Local<v8::Context> context, v8::Local<v8::Object> objec
t) | 77 static T* unwrap(v8::Local<v8::Context> context, v8::Local<v8::Object> objec
t) |
| 101 { | 78 { |
| 102 void* data = InspectorWrapperBase::unwrap(context, object, hiddenPropert
yName); | 79 void* data = InspectorWrapperBase::unwrap(context, object, hiddenPropert
yName); |
| 103 if (!data) | 80 if (!data) |
| 104 return nullptr; | 81 return nullptr; |
| 105 return reinterpret_cast<WeakCallbackData*>(data)->m_impl; | 82 return reinterpret_cast<WeakCallbackData*>(data)->m_impl; |
| 106 } | 83 } |
| 107 }; | 84 }; |
| 108 | 85 |
| 109 } // namespace blink | 86 } // namespace blink |
| 110 | 87 |
| 111 #endif // InspectorWrapper_h | 88 #endif // InspectorWrapper_h |
| OLD | NEW |