| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef InspectorWrapper_h | |
| 6 #define InspectorWrapper_h | |
| 7 | |
| 8 #include "platform/inspector_protocol/Collections.h" | |
| 9 #include <v8.h> | |
| 10 | |
| 11 namespace blink { | |
| 12 | |
| 13 class InspectorWrapperBase { | |
| 14 public: | |
| 15 struct V8MethodConfiguration { | |
| 16 const char* name; | |
| 17 v8::FunctionCallback callback; | |
| 18 }; | |
| 19 | |
| 20 struct V8AttributeConfiguration { | |
| 21 const char* name; | |
| 22 v8::AccessorNameGetterCallback callback; | |
| 23 }; | |
| 24 | |
| 25 static v8::Local<v8::FunctionTemplate> createWrapperTemplate(v8::Isolate*, c
onst char* className, const protocol::Vector<V8MethodConfiguration>& methods, co
nst protocol::Vector<V8AttributeConfiguration>& attributes); | |
| 26 | |
| 27 protected: | |
| 28 static v8::Local<v8::Object> createWrapper(v8::Local<v8::FunctionTemplate>,
v8::Local<v8::Context>); | |
| 29 static void* unwrap(v8::Local<v8::Context>, v8::Local<v8::Object>, const cha
r* name); | |
| 30 | |
| 31 static v8::Local<v8::String> v8InternalizedString(v8::Isolate*, const char*
name); | |
| 32 }; | |
| 33 | |
| 34 template<class T, char* const hiddenPropertyName, char* const className> | |
| 35 class InspectorWrapper final : public InspectorWrapperBase { | |
| 36 public: | |
| 37 class WeakCallbackData final { | |
| 38 public: | |
| 39 WeakCallbackData(v8::Isolate* isolate, T* impl, v8::Local<v8::Object> wr
apper) | |
| 40 : m_impl(impl) | |
| 41 , m_persistent(isolate, wrapper) | |
| 42 { | |
| 43 m_persistent.SetWeak(this, &WeakCallbackData::weakCallback, v8::Weak
CallbackType::kParameter); | |
| 44 } | |
| 45 | |
| 46 T* m_impl; | |
| 47 OwnPtr<T> m_implOwn; | |
| 48 | |
| 49 private: | |
| 50 static void weakCallback(const v8::WeakCallbackInfo<WeakCallbackData>& i
nfo) | |
| 51 { | |
| 52 delete info.GetParameter(); | |
| 53 } | |
| 54 | |
| 55 v8::Global<v8::Object> m_persistent; | |
| 56 }; | |
| 57 | |
| 58 static v8::Local<v8::FunctionTemplate> createWrapperTemplate(v8::Isolate* is
olate, const protocol::Vector<V8MethodConfiguration>& methods, const protocol::V
ector<V8AttributeConfiguration>& attributes) | |
| 59 { | |
| 60 return InspectorWrapperBase::createWrapperTemplate(isolate, className, m
ethods, attributes); | |
| 61 } | |
| 62 | |
| 63 static v8::Local<v8::Object> wrap(v8::Local<v8::FunctionTemplate> constructo
rTemplate, v8::Local<v8::Context> context, T* object) | |
| 64 { | |
| 65 v8::Context::Scope contextScope(context); | |
| 66 v8::Local<v8::Object> result = InspectorWrapperBase::createWrapper(const
ructorTemplate, context); | |
| 67 if (result.IsEmpty()) | |
| 68 return v8::Local<v8::Object>(); | |
| 69 v8::Isolate* isolate = context->GetIsolate(); | |
| 70 v8::Local<v8::External> objectReference = v8::External::New(isolate, new
WeakCallbackData(isolate, object, result)); | |
| 71 | |
| 72 v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, v8::Str
ing::NewFromUtf8(isolate, hiddenPropertyName, v8::NewStringType::kInternalized).
ToLocalChecked()); | |
| 73 result->SetPrivate(context, privateKey, objectReference); | |
| 74 return result; | |
| 75 } | |
| 76 | |
| 77 static T* unwrap(v8::Local<v8::Context> context, v8::Local<v8::Object> objec
t) | |
| 78 { | |
| 79 void* data = InspectorWrapperBase::unwrap(context, object, hiddenPropert
yName); | |
| 80 if (!data) | |
| 81 return nullptr; | |
| 82 return reinterpret_cast<WeakCallbackData*>(data)->m_impl; | |
| 83 } | |
| 84 }; | |
| 85 | |
| 86 } // namespace blink | |
| 87 | |
| 88 #endif // InspectorWrapper_h | |
| OLD | NEW |