| 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/heap/Handle.h" |
| 9 #include "wtf/PassRefPtr.h" |
| 10 #include "wtf/RefPtr.h" |
| 11 #include "wtf/Vector.h" |
| 12 #include <v8.h> |
| 13 |
| 14 namespace blink { |
| 15 |
| 16 class InspectorWrapperBase { |
| 17 public: |
| 18 struct V8MethodConfiguration { |
| 19 const char* name; |
| 20 v8::FunctionCallback callback; |
| 21 }; |
| 22 |
| 23 struct V8AttributeConfiguration { |
| 24 const char* name; |
| 25 v8::AccessorNameGetterCallback callback; |
| 26 }; |
| 27 |
| 28 static v8::Local<v8::FunctionTemplate> createWrapperTemplate(v8::Isolate*, c
onst char* className, const Vector<V8MethodConfiguration>& methods, const Vector
<V8AttributeConfiguration>& attributes); |
| 29 |
| 30 protected: |
| 31 static v8::Local<v8::Object> createWrapper(v8::Local<v8::FunctionTemplate>,
v8::Local<v8::Context>); |
| 32 static void* unwrap(v8::Local<v8::Object>, const char* name); |
| 33 |
| 34 static v8::Local<v8::String> v8InternalizedString(v8::Isolate*, const char*
name); |
| 35 }; |
| 36 |
| 37 template<class T, char* const hiddenPropertyName, char* const className> |
| 38 class InspectorWrapper final : public InspectorWrapperBase { |
| 39 public: |
| 40 class WeakCallbackData final { |
| 41 public: |
| 42 WeakCallbackData(v8::Isolate* isolate, PassRefPtr<T> impl, v8::Local<v8:
:Object> wrapper) |
| 43 : m_impl(impl) |
| 44 , m_persistent(isolate, wrapper) |
| 45 { |
| 46 m_persistent.SetWeak(this, &WeakCallbackData::weakCallback, v8::Weak
CallbackType::kParameter); |
| 47 } |
| 48 |
| 49 RefPtr<T> m_impl; |
| 50 |
| 51 private: |
| 52 static void weakCallback(const v8::WeakCallbackInfo<WeakCallbackData>& i
nfo) |
| 53 { |
| 54 delete info.GetParameter(); |
| 55 } |
| 56 |
| 57 v8::Global<v8::Object> m_persistent; |
| 58 }; |
| 59 |
| 60 static v8::Local<v8::FunctionTemplate> createWrapperTemplate(v8::Isolate* is
olate, const Vector<V8MethodConfiguration>& methods, const Vector<V8AttributeCon
figuration>& attributes) |
| 61 { |
| 62 return InspectorWrapperBase::createWrapperTemplate(isolate, className, m
ethods, attributes); |
| 63 } |
| 64 |
| 65 static v8::Local<v8::Object> wrap(v8::Local<v8::FunctionTemplate> constructo
rTemplate, v8::Local<v8::Context> context, PassRefPtr<T> object) |
| 66 { |
| 67 v8::Context::Scope contextScope(context); |
| 68 v8::Local<v8::Object> result = InspectorWrapperBase::createWrapper(const
ructorTemplate, context); |
| 69 if (result.IsEmpty()) |
| 70 return v8::Local<v8::Object>(); |
| 71 RefPtrWillBeRawPtr<T> impl(object); |
| 72 v8::Isolate* isolate = context->GetIsolate(); |
| 73 v8::Local<v8::External> objectReference = v8::External::New(isolate, new
WeakCallbackData(isolate, impl, result)); |
| 74 result->SetHiddenValue(v8InternalizedString(isolate, hiddenPropertyName)
, objectReference); |
| 75 return result; |
| 76 } |
| 77 static T* unwrap(v8::Local<v8::Object> object) |
| 78 { |
| 79 void* data = InspectorWrapperBase::unwrap(object, hiddenPropertyName); |
| 80 if (!data) |
| 81 return nullptr; |
| 82 return reinterpret_cast<WeakCallbackData*>(data)->m_impl.get(); |
| 83 } |
| 84 }; |
| 85 |
| 86 } // namespace blink |
| 87 |
| 88 #endif // InspectorWrapper_h |
| OLD | NEW |