2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 #ifndef V8DOMWrapper_h
32 #define V8DOMWrapper_h
34 #include "bindings/v8/DOMDataStore.h"
36 #include "wtf/PassRefPtr.h"
37 #include "wtf/RawPtr.h"
38 #include "wtf/text/AtomicString.h"
42 struct WrapperTypeInfo;
46 static v8::Local<v8::Object> createWrapper(v8::Handle<v8::Object> creationContext, const WrapperTypeInfo*, void*, v8::Isolate*);
48 template<typename V8T, typename T>
49 static inline v8::Handle<v8::Object> associateObjectWithWrapper(PassRefPtr<T>, const WrapperTypeInfo*, v8::Handle<v8::Object>, v8::Isolate*, WrapperConfiguration::Lifetime);
50 template<typename V8T, typename T>
51 static inline v8::Handle<v8::Object> associateObjectWithWrapper(RawPtr<T> object, const WrapperTypeInfo* wrapperTypeInfo, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, WrapperConfiguration::Lifetime lifetime)
53 return associateObjectWithWrapper<V8T, T>(object.get(), wrapperTypeInfo, wrapper, isolate, lifetime);
55 template<typename V8T, typename T>
56 static inline v8::Handle<v8::Object> associateObjectWithWrapper(T*, const WrapperTypeInfo*, v8::Handle<v8::Object>, v8::Isolate*, WrapperConfiguration::Lifetime);
57 static inline void setNativeInfo(v8::Handle<v8::Object>, const WrapperTypeInfo*, void*);
58 static inline void setNativeInfoForHiddenWrapper(v8::Handle<v8::Object>, const 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 WrapperTypeInfo*);
62 static bool isDOMWrapper(v8::Handle<v8::Value>);
65 inline void V8DOMWrapper::setNativeInfo(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* wrapperTypeInfo, void* object)
67 ASSERT(wrapper->InternalFieldCount() >= 2);
69 ASSERT(wrapperTypeInfo);
71 ASSERT(wrapperTypeInfo->gcType == RefCountedObject);
73 ASSERT(wrapperTypeInfo->gcType == RefCountedObject || wrapperTypeInfo->gcType == WillBeGarbageCollectedObject);
75 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, object);
76 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(wrapperTypeInfo));
79 inline void V8DOMWrapper::setNativeInfoForHiddenWrapper(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* wrapperTypeInfo, void* object)
81 // see V8WindowShell::installDOMWindow() comment for why this version is needed and safe.
82 ASSERT(wrapper->InternalFieldCount() >= 2);
84 ASSERT(wrapperTypeInfo);
86 ASSERT(wrapperTypeInfo->gcType != RefCountedObject);
88 ASSERT(wrapperTypeInfo->gcType == RefCountedObject || wrapperTypeInfo->gcType == WillBeGarbageCollectedObject);
91 // Clear out the last internal field, which is assumed to contain a valid persistent pointer value.
92 if (wrapperTypeInfo->gcType == GarbageCollectedObject) {
93 wrapper->SetAlignedPointerInInternalField(wrapper->InternalFieldCount() - 1, 0);
94 } else if (wrapperTypeInfo->gcType == WillBeGarbageCollectedObject) {
96 wrapper->SetAlignedPointerInInternalField(wrapper->InternalFieldCount() - 1, 0);
99 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, object);
100 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(wrapperTypeInfo));
103 inline void V8DOMWrapper::setNativeInfoWithPersistentHandle(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* wrapperTypeInfo, void* object, PersistentNode* handle)
105 ASSERT(wrapper->InternalFieldCount() >= 3);
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);
115 inline void V8DOMWrapper::clearNativeInfo(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* wrapperTypeInfo)
117 ASSERT(wrapper->InternalFieldCount() >= 2);
118 ASSERT(wrapperTypeInfo);
119 // clearNativeInfo() is used only by NP objects, which are not garbage collected.
120 ASSERT(wrapperTypeInfo->gcType == RefCountedObject);
121 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(wrapperTypeInfo));
122 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, 0);
125 template<typename V8T, typename T>
126 inline v8::Handle<v8::Object> V8DOMWrapper::associateObjectWithWrapper(PassRefPtr<T> object, const WrapperTypeInfo* wrapperTypeInfo, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, WrapperConfiguration::Lifetime lifetime)
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, configuration);
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::Isolate* isolate, WrapperConfiguration::Lifetime lifetime)
138 setNativeInfoWithPersistentHandle(wrapper, wrapperTypeInfo, V8T::toInternalPointer(object), new Persistent<T>(object));
139 ASSERT(isDOMWrapper(wrapper));
140 WrapperConfiguration configuration = buildWrapperConfiguration(object, lifetime);
141 DOMDataStore::setWrapper<V8T>(object, wrapper, isolate, configuration);
145 class V8WrapperInstantiationScope {
147 V8WrapperInstantiationScope(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
148 : m_didEnterContext(false)
149 , m_context(isolate->GetCurrentContext())
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->CreationContext();
156 // For performance, we enter the context only if the currently running context
157 // is different from the context that we are about to enter.
158 if (contextForWrapper == m_context)
160 m_context = v8::Local<v8::Context>::New(isolate, contextForWrapper);
161 m_didEnterContext = true;
165 ~V8WrapperInstantiationScope()
167 if (!m_didEnterContext)
172 v8::Handle<v8::Context> context() const { return m_context; }
175 bool m_didEnterContext;
176 v8::Handle<v8::Context> m_context;
181 #endif // V8DOMWrapper_h