OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #ifndef V8DOMWrapper_h |
| 32 #define V8DOMWrapper_h |
| 33 |
| 34 #include "bindings/core/v8/DOMDataStore.h" |
| 35 #include "wtf/PassRefPtr.h" |
| 36 #include "wtf/RawPtr.h" |
| 37 #include "wtf/text/AtomicString.h" |
| 38 #include <v8.h> |
| 39 |
| 40 namespace WebCore { |
| 41 |
| 42 struct WrapperTypeInfo; |
| 43 |
| 44 class V8DOMWrapper { |
| 45 public: |
| 46 static v8::Local<v8::Object> createWrapper(v8::Handle<v8::Object> creationCo
ntext, const WrapperTypeInfo*, void*, v8::Isolate*); |
| 47 |
| 48 template<typename V8T, typename T> |
| 49 static inline v8::Handle<v8::Object> associateObjectWithWrapper(PassRefPtr<T
>, const WrapperTypeInfo*, v8::Handle<v8::Object>, v8::Isolate*, WrapperConfigur
ation::Lifetime); |
| 50 template<typename V8T, typename T> |
| 51 static inline v8::Handle<v8::Object> associateObjectWithWrapper(RawPtr<T> ob
ject, const WrapperTypeInfo* wrapperTypeInfo, v8::Handle<v8::Object> wrapper, v8
::Isolate* isolate, WrapperConfiguration::Lifetime lifetime) |
| 52 { |
| 53 return associateObjectWithWrapper<V8T, T>(object.get(), wrapperTypeInfo,
wrapper, isolate, lifetime); |
| 54 } |
| 55 template<typename V8T, typename T> |
| 56 static inline v8::Handle<v8::Object> associateObjectWithWrapper(T*, const Wr
apperTypeInfo*, v8::Handle<v8::Object>, v8::Isolate*, WrapperConfiguration::Life
time); |
| 57 static inline void setNativeInfo(v8::Handle<v8::Object>, const WrapperTypeIn
fo*, void*); |
| 58 static inline void setNativeInfoForHiddenWrapper(v8::Handle<v8::Object>, con
st WrapperTypeInfo*, void*); |
| 59 static inline void setNativeInfoWithPersistentHandle(v8::Handle<v8::Object>,
const WrapperTypeInfo*, void*, PersistentNode*); |
| 60 static inline void clearNativeInfo(v8::Handle<v8::Object>, const WrapperType
Info*); |
| 61 |
| 62 static bool isDOMWrapper(v8::Handle<v8::Value>); |
| 63 }; |
| 64 |
| 65 inline void V8DOMWrapper::setNativeInfo(v8::Handle<v8::Object> wrapper, const Wr
apperTypeInfo* wrapperTypeInfo, void* object) |
| 66 { |
| 67 ASSERT(wrapper->InternalFieldCount() >= 2); |
| 68 ASSERT(object); |
| 69 ASSERT(wrapperTypeInfo); |
| 70 #if ENABLE(OILPAN) |
| 71 ASSERT(wrapperTypeInfo->gcType == RefCountedObject); |
| 72 #else |
| 73 ASSERT(wrapperTypeInfo->gcType == RefCountedObject || wrapperTypeInfo->gcTyp
e == WillBeGarbageCollectedObject); |
| 74 #endif |
| 75 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, object); |
| 76 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<
WrapperTypeInfo*>(wrapperTypeInfo)); |
| 77 } |
| 78 |
| 79 inline void V8DOMWrapper::setNativeInfoForHiddenWrapper(v8::Handle<v8::Object> w
rapper, const WrapperTypeInfo* wrapperTypeInfo, void* object) |
| 80 { |
| 81 // see V8WindowShell::installDOMWindow() comment for why this version is nee
ded and safe. |
| 82 ASSERT(wrapper->InternalFieldCount() >= 2); |
| 83 ASSERT(object); |
| 84 ASSERT(wrapperTypeInfo); |
| 85 #if ENABLE(OILPAN) |
| 86 ASSERT(wrapperTypeInfo->gcType != RefCountedObject); |
| 87 #else |
| 88 ASSERT(wrapperTypeInfo->gcType == RefCountedObject || wrapperTypeInfo->gcTyp
e == WillBeGarbageCollectedObject); |
| 89 #endif |
| 90 |
| 91 // Clear out the last internal field, which is assumed to contain a valid pe
rsistent pointer value. |
| 92 if (wrapperTypeInfo->gcType == GarbageCollectedObject) { |
| 93 wrapper->SetAlignedPointerInInternalField(wrapper->InternalFieldCount()
- 1, 0); |
| 94 } else if (wrapperTypeInfo->gcType == WillBeGarbageCollectedObject) { |
| 95 #if ENABLE(OILPAN) |
| 96 wrapper->SetAlignedPointerInInternalField(wrapper->InternalFieldCount()
- 1, 0); |
| 97 #endif |
| 98 } |
| 99 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, object); |
| 100 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<
WrapperTypeInfo*>(wrapperTypeInfo)); |
| 101 } |
| 102 |
| 103 inline void V8DOMWrapper::setNativeInfoWithPersistentHandle(v8::Handle<v8::Objec
t> wrapper, const WrapperTypeInfo* wrapperTypeInfo, void* object, PersistentNode
* handle) |
| 104 { |
| 105 ASSERT(wrapper->InternalFieldCount() >= 3); |
| 106 ASSERT(object); |
| 107 ASSERT(wrapperTypeInfo); |
| 108 ASSERT(wrapperTypeInfo->gcType != RefCountedObject); |
| 109 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, object); |
| 110 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<
WrapperTypeInfo*>(wrapperTypeInfo)); |
| 111 // Persistent handle is stored in the last internal field. |
| 112 wrapper->SetAlignedPointerInInternalField(wrapper->InternalFieldCount() - 1,
handle); |
| 113 } |
| 114 |
| 115 inline void V8DOMWrapper::clearNativeInfo(v8::Handle<v8::Object> wrapper, const
WrapperTypeInfo* wrapperTypeInfo) |
| 116 { |
| 117 ASSERT(wrapper->InternalFieldCount() >= 2); |
| 118 ASSERT(wrapperTypeInfo); |
| 119 // clearNativeInfo() is used only by NP objects, which are not garbage colle
cted. |
| 120 ASSERT(wrapperTypeInfo->gcType == RefCountedObject); |
| 121 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<
WrapperTypeInfo*>(wrapperTypeInfo)); |
| 122 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, 0); |
| 123 } |
| 124 |
| 125 template<typename V8T, typename T> |
| 126 inline v8::Handle<v8::Object> V8DOMWrapper::associateObjectWithWrapper(PassRefPt
r<T> object, const WrapperTypeInfo* wrapperTypeInfo, v8::Handle<v8::Object> wrap
per, v8::Isolate* isolate, WrapperConfiguration::Lifetime lifetime) |
| 127 { |
| 128 setNativeInfo(wrapper, wrapperTypeInfo, V8T::toInternalPointer(object.get())
); |
| 129 ASSERT(isDOMWrapper(wrapper)); |
| 130 WrapperConfiguration configuration = buildWrapperConfiguration(object.get(),
lifetime); |
| 131 DOMDataStore::setWrapper<V8T>(object.leakRef(), wrapper, isolate, configurat
ion); |
| 132 return wrapper; |
| 133 } |
| 134 |
| 135 template<typename V8T, typename T> |
| 136 inline v8::Handle<v8::Object> V8DOMWrapper::associateObjectWithWrapper(T* object
, const WrapperTypeInfo* wrapperTypeInfo, v8::Handle<v8::Object> wrapper, v8::Is
olate* isolate, WrapperConfiguration::Lifetime lifetime) |
| 137 { |
| 138 setNativeInfoWithPersistentHandle(wrapper, wrapperTypeInfo, V8T::toInternalP
ointer(object), new Persistent<T>(object)); |
| 139 ASSERT(isDOMWrapper(wrapper)); |
| 140 WrapperConfiguration configuration = buildWrapperConfiguration(object, lifet
ime); |
| 141 DOMDataStore::setWrapper<V8T>(object, wrapper, isolate, configuration); |
| 142 return wrapper; |
| 143 } |
| 144 |
| 145 class V8WrapperInstantiationScope { |
| 146 public: |
| 147 V8WrapperInstantiationScope(v8::Handle<v8::Object> creationContext, v8::Isol
ate* isolate) |
| 148 : m_didEnterContext(false) |
| 149 , m_context(isolate->GetCurrentContext()) |
| 150 { |
| 151 // creationContext should not be empty. Because if we have an |
| 152 // empty creationContext, we will end up creating |
| 153 // a new object in the context currently entered. This is wrong. |
| 154 RELEASE_ASSERT(!creationContext.IsEmpty()); |
| 155 v8::Handle<v8::Context> contextForWrapper = creationContext->CreationCon
text(); |
| 156 // For performance, we enter the context only if the currently running c
ontext |
| 157 // is different from the context that we are about to enter. |
| 158 if (contextForWrapper == m_context) |
| 159 return; |
| 160 m_context = v8::Local<v8::Context>::New(isolate, contextForWrapper); |
| 161 m_didEnterContext = true; |
| 162 m_context->Enter(); |
| 163 } |
| 164 |
| 165 ~V8WrapperInstantiationScope() |
| 166 { |
| 167 if (!m_didEnterContext) |
| 168 return; |
| 169 m_context->Exit(); |
| 170 } |
| 171 |
| 172 v8::Handle<v8::Context> context() const { return m_context; } |
| 173 |
| 174 private: |
| 175 bool m_didEnterContext; |
| 176 v8::Handle<v8::Context> m_context; |
| 177 }; |
| 178 |
| 179 } // namespace WebCore |
| 180 |
| 181 #endif // V8DOMWrapper_h |
OLD | NEW |