| 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 SKY_ENGINE_BINDINGS_CORE_V8_V8DOMWRAPPER_H_ | |
| 32 #define SKY_ENGINE_BINDINGS_CORE_V8_V8DOMWRAPPER_H_ | |
| 33 | |
| 34 #include "sky/engine/bindings/core/v8/DOMDataStore.h" | |
| 35 #include "sky/engine/bindings/core/v8/ScriptWrappable.h" | |
| 36 #include "sky/engine/wtf/PassRefPtr.h" | |
| 37 #include "sky/engine/wtf/RawPtr.h" | |
| 38 #include "sky/engine/wtf/text/AtomicString.h" | |
| 39 #include "v8/include/v8.h" | |
| 40 | |
| 41 namespace blink { | |
| 42 | |
| 43 class Node; | |
| 44 struct WrapperTypeInfo; | |
| 45 | |
| 46 class V8DOMWrapper { | |
| 47 public: | |
| 48 static v8::Local<v8::Object> createWrapper(v8::Handle<v8::Object> creationCo
ntext, const WrapperTypeInfo*, ScriptWrappableBase* internalPointer, v8::Isolate
*); | |
| 49 | |
| 50 template<typename V8T, typename T> | |
| 51 static v8::Handle<v8::Object> associateObjectWithWrapper(PassRefPtr<T>, cons
t WrapperTypeInfo*, v8::Handle<v8::Object>, v8::Isolate*); | |
| 52 template<typename V8T, typename T> | |
| 53 static v8::Handle<v8::Object> associateObjectWithWrapper(RawPtr<T> object, c
onst WrapperTypeInfo* wrapperTypeInfo, v8::Handle<v8::Object> wrapper, v8::Isola
te* isolate) | |
| 54 { | |
| 55 return associateObjectWithWrapper<V8T, T>(object.get(), wrapperTypeInfo,
wrapper, isolate); | |
| 56 } | |
| 57 template<typename V8T, typename T> | |
| 58 static v8::Handle<v8::Object> associateObjectWithWrapper(T*, const WrapperTy
peInfo*, v8::Handle<v8::Object>, v8::Isolate*); | |
| 59 static v8::Handle<v8::Object> associateObjectWithWrapperNonTemplate(ScriptWr
appable*, const WrapperTypeInfo*, v8::Handle<v8::Object>, v8::Isolate*); | |
| 60 static v8::Handle<v8::Object> associateObjectWithWrapperNonTemplate(Node*, c
onst WrapperTypeInfo*, v8::Handle<v8::Object>, v8::Isolate*); | |
| 61 static void setNativeInfo(v8::Handle<v8::Object>, const WrapperTypeInfo*, Sc
riptWrappableBase* internalPointer); | |
| 62 static void setNativeInfoForHiddenWrapper(v8::Handle<v8::Object>, const Wrap
perTypeInfo*, ScriptWrappableBase* internalPointer); | |
| 63 static void clearNativeInfo(v8::Handle<v8::Object>, const WrapperTypeInfo*); | |
| 64 | |
| 65 static bool isDOMWrapper(v8::Handle<v8::Value>); | |
| 66 }; | |
| 67 | |
| 68 inline void V8DOMWrapper::setNativeInfo(v8::Handle<v8::Object> wrapper, const Wr
apperTypeInfo* wrapperTypeInfo, ScriptWrappableBase* internalPointer) | |
| 69 { | |
| 70 ASSERT(wrapper->InternalFieldCount() >= 2); | |
| 71 ASSERT(internalPointer); | |
| 72 ASSERT(wrapperTypeInfo); | |
| 73 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, internalP
ointer); | |
| 74 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<
WrapperTypeInfo*>(wrapperTypeInfo)); | |
| 75 } | |
| 76 | |
| 77 inline void V8DOMWrapper::setNativeInfoForHiddenWrapper(v8::Handle<v8::Object> w
rapper, const WrapperTypeInfo* wrapperTypeInfo, ScriptWrappableBase* internalPoi
nter) | |
| 78 { | |
| 79 // see WindowProxy::installDOMWindow() comment for why this version is neede
d and safe. | |
| 80 ASSERT(wrapper->InternalFieldCount() >= 2); | |
| 81 ASSERT(internalPointer); | |
| 82 ASSERT(wrapperTypeInfo); | |
| 83 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, internalP
ointer); | |
| 84 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<
WrapperTypeInfo*>(wrapperTypeInfo)); | |
| 85 } | |
| 86 | |
| 87 inline void V8DOMWrapper::clearNativeInfo(v8::Handle<v8::Object> wrapper, const
WrapperTypeInfo* wrapperTypeInfo) | |
| 88 { | |
| 89 ASSERT(wrapper->InternalFieldCount() >= 2); | |
| 90 ASSERT(wrapperTypeInfo); | |
| 91 // clearNativeInfo() is used only by NP objects, which are not garbage colle
cted. | |
| 92 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<
WrapperTypeInfo*>(wrapperTypeInfo)); | |
| 93 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, 0); | |
| 94 } | |
| 95 | |
| 96 template<typename V8T, typename T> | |
| 97 inline v8::Handle<v8::Object> V8DOMWrapper::associateObjectWithWrapper(PassRefPt
r<T> object, const WrapperTypeInfo* wrapperTypeInfo, v8::Handle<v8::Object> wrap
per, v8::Isolate* isolate) | |
| 98 { | |
| 99 setNativeInfo(wrapper, wrapperTypeInfo, V8T::toScriptWrappableBase(object.ge
t())); | |
| 100 ASSERT(isDOMWrapper(wrapper)); | |
| 101 DOMDataStore::setWrapper<V8T>(object.leakRef(), wrapper, isolate, wrapperTyp
eInfo); | |
| 102 return wrapper; | |
| 103 } | |
| 104 | |
| 105 inline v8::Handle<v8::Object> V8DOMWrapper::associateObjectWithWrapperNonTemplat
e(ScriptWrappable* impl, const WrapperTypeInfo* wrapperTypeInfo, v8::Handle<v8::
Object> wrapper, v8::Isolate* isolate) | |
| 106 { | |
| 107 wrapperTypeInfo->refObject(impl->toScriptWrappableBase()); | |
| 108 setNativeInfo(wrapper, wrapperTypeInfo, impl->toScriptWrappableBase()); | |
| 109 ASSERT(isDOMWrapper(wrapper)); | |
| 110 DOMDataStore::setWrapperNonTemplate(impl, wrapper, isolate, wrapperTypeInfo)
; | |
| 111 return wrapper; | |
| 112 } | |
| 113 | |
| 114 inline v8::Handle<v8::Object> V8DOMWrapper::associateObjectWithWrapperNonTemplat
e(Node* node, const WrapperTypeInfo* wrapperTypeInfo, v8::Handle<v8::Object> wra
pper, v8::Isolate* isolate) | |
| 115 { | |
| 116 wrapperTypeInfo->refObject(ScriptWrappable::fromObject(node)->toScriptWrappa
bleBase()); | |
| 117 setNativeInfo(wrapper, wrapperTypeInfo, ScriptWrappable::fromObject(node)->t
oScriptWrappableBase()); | |
| 118 ASSERT(isDOMWrapper(wrapper)); | |
| 119 DOMDataStore::setWrapperNonTemplate(node, wrapper, isolate, wrapperTypeInfo)
; | |
| 120 return wrapper; | |
| 121 } | |
| 122 | |
| 123 class V8WrapperInstantiationScope { | |
| 124 public: | |
| 125 V8WrapperInstantiationScope(v8::Handle<v8::Object> creationContext, v8::Isol
ate* isolate) | |
| 126 : m_didEnterContext(false) | |
| 127 , m_context(isolate->GetCurrentContext()) | |
| 128 { | |
| 129 // creationContext should not be empty. Because if we have an | |
| 130 // empty creationContext, we will end up creating | |
| 131 // a new object in the context currently entered. This is wrong. | |
| 132 RELEASE_ASSERT(!creationContext.IsEmpty()); | |
| 133 v8::Handle<v8::Context> contextForWrapper = creationContext->CreationCon
text(); | |
| 134 // For performance, we enter the context only if the currently running c
ontext | |
| 135 // is different from the context that we are about to enter. | |
| 136 if (contextForWrapper == m_context) | |
| 137 return; | |
| 138 m_context = v8::Local<v8::Context>::New(isolate, contextForWrapper); | |
| 139 m_didEnterContext = true; | |
| 140 m_context->Enter(); | |
| 141 } | |
| 142 | |
| 143 ~V8WrapperInstantiationScope() | |
| 144 { | |
| 145 if (!m_didEnterContext) | |
| 146 return; | |
| 147 m_context->Exit(); | |
| 148 } | |
| 149 | |
| 150 v8::Handle<v8::Context> context() const { return m_context; } | |
| 151 | |
| 152 private: | |
| 153 bool m_didEnterContext; | |
| 154 v8::Handle<v8::Context> m_context; | |
| 155 }; | |
| 156 | |
| 157 } // namespace blink | |
| 158 | |
| 159 #endif // SKY_ENGINE_BINDINGS_CORE_V8_V8DOMWRAPPER_H_ | |
| OLD | NEW |