| 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<typename T, bool = IsGarbageCollectedType<T>::value> |
| 38 class InspectorWrapperTypeTrait { |
| 39 public: |
| 40 using PassType = PassRefPtr<T>; |
| 41 using Type = RefPtr<T>; |
| 42 }; |
| 43 |
| 44 template<typename T> |
| 45 class InspectorWrapperTypeTrait<T, true> { |
| 46 public: |
| 47 using PassType = PassRefPtrWillBeRawPtr<T>; |
| 48 using Type = RefPtrWillBeRawPtr<T>; |
| 49 }; |
| 50 |
| 51 template<class T, char* const hiddenPropertyName, char* const className> |
| 52 class InspectorWrapper final : public InspectorWrapperBase { |
| 53 public: |
| 54 class WeakCallbackData final { |
| 55 public: |
| 56 WeakCallbackData(v8::Isolate* isolate, typename InspectorWrapperTypeTrai
t<T>::PassType impl, v8::Local<v8::Object> wrapper) |
| 57 : m_impl(impl) |
| 58 , m_persistent(isolate, wrapper) |
| 59 { |
| 60 m_persistent.SetWeak(this, &WeakCallbackData::weakCallback, v8::Weak
CallbackType::kParameter); |
| 61 } |
| 62 |
| 63 typename InspectorWrapperTypeTrait<T>::Type m_impl; |
| 64 |
| 65 private: |
| 66 static void weakCallback(const v8::WeakCallbackInfo<WeakCallbackData>& i
nfo) |
| 67 { |
| 68 delete info.GetParameter(); |
| 69 } |
| 70 |
| 71 v8::Global<v8::Object> m_persistent; |
| 72 }; |
| 73 |
| 74 static v8::Local<v8::FunctionTemplate> createWrapperTemplate(v8::Isolate* is
olate, const Vector<V8MethodConfiguration>& methods, const Vector<V8AttributeCon
figuration>& attributes) |
| 75 { |
| 76 return InspectorWrapperBase::createWrapperTemplate(isolate, className, m
ethods, attributes); |
| 77 } |
| 78 |
| 79 static v8::Local<v8::Object> wrap(v8::Local<v8::FunctionTemplate> constructo
rTemplate, v8::Local<v8::Context> context, typename blink::InspectorWrapperTypeT
rait<T>::PassType object) |
| 80 { |
| 81 v8::Context::Scope contextScope(context); |
| 82 v8::Local<v8::Object> result = InspectorWrapperBase::createWrapper(const
ructorTemplate, context); |
| 83 if (result.IsEmpty()) |
| 84 return v8::Local<v8::Object>(); |
| 85 typename blink::InspectorWrapperTypeTrait<T>::Type impl(object); |
| 86 v8::Isolate* isolate = context->GetIsolate(); |
| 87 v8::Local<v8::External> objectReference = v8::External::New(isolate, new
WeakCallbackData(isolate, impl, result)); |
| 88 result->SetHiddenValue(v8InternalizedString(isolate, hiddenPropertyName)
, objectReference); |
| 89 return result; |
| 90 } |
| 91 static T* unwrap(v8::Local<v8::Object> object) |
| 92 { |
| 93 void* data = InspectorWrapperBase::unwrap(object, hiddenPropertyName); |
| 94 if (!data) |
| 95 return nullptr; |
| 96 return reinterpret_cast<WeakCallbackData*>(data)->m_impl.get(); |
| 97 } |
| 98 }; |
| 99 |
| 100 } // namespace blink |
| 101 |
| 102 #endif // InspectorWrapper_h |
| OLD | NEW |