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/core/v8/DOMDataStore.h"
35 #include "wtf/PassRefPtr.h"
36 #include "wtf/RawPtr.h"
37 #include "wtf/text/AtomicString.h"
42 class ScriptWrappableBase;
43 struct WrapperTypeInfo;
47 static v8::Local<v8::Object> createWrapper(v8::Handle<v8::Object> creationContext, const WrapperTypeInfo*, ScriptWrappableBase* internalPointer, v8::Isolate*);
49 template<typename V8T, typename T>
50 static inline v8::Handle<v8::Object> associateObjectWithWrapper(PassRefPtr<T>, const WrapperTypeInfo*, v8::Handle<v8::Object>, v8::Isolate*, WrapperConfiguration::Lifetime);
51 template<typename V8T, typename T>
52 static inline v8::Handle<v8::Object> associateObjectWithWrapper(RawPtr<T> object, const WrapperTypeInfo* wrapperTypeInfo, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, WrapperConfiguration::Lifetime lifetime)
54 return associateObjectWithWrapper<V8T, T>(object.get(), wrapperTypeInfo, wrapper, isolate, lifetime);
56 template<typename V8T, typename T>
57 static inline v8::Handle<v8::Object> associateObjectWithWrapper(T*, const WrapperTypeInfo*, v8::Handle<v8::Object>, v8::Isolate*, WrapperConfiguration::Lifetime);
58 static inline void setNativeInfo(v8::Handle<v8::Object>, const WrapperTypeInfo*, ScriptWrappableBase* internalPointer);
59 static inline void setNativeInfoForHiddenWrapper(v8::Handle<v8::Object>, const WrapperTypeInfo*, ScriptWrappableBase* internalPointer);
60 static inline void setNativeInfoWithPersistentHandle(v8::Handle<v8::Object>, const WrapperTypeInfo*, ScriptWrappableBase* internalPointer, PersistentNode*);
61 static inline void clearNativeInfo(v8::Handle<v8::Object>, const WrapperTypeInfo*);
63 static bool isDOMWrapper(v8::Handle<v8::Value>);
66 inline void V8DOMWrapper::setNativeInfo(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* wrapperTypeInfo, ScriptWrappableBase* internalPointer)
68 ASSERT(wrapper->InternalFieldCount() >= 2);
69 ASSERT(internalPointer);
70 ASSERT(wrapperTypeInfo);
72 ASSERT(wrapperTypeInfo->gcType == RefCountedObject);
74 ASSERT(wrapperTypeInfo->gcType == RefCountedObject || wrapperTypeInfo->gcType == WillBeGarbageCollectedObject);
76 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, internalPointer);
77 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(wrapperTypeInfo));
80 inline void V8DOMWrapper::setNativeInfoForHiddenWrapper(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* wrapperTypeInfo, ScriptWrappableBase* internalPointer)
82 // see WindowProxy::installDOMWindow() comment for why this version is needed and safe.
83 ASSERT(wrapper->InternalFieldCount() >= 2);
84 ASSERT(internalPointer);
85 ASSERT(wrapperTypeInfo);
87 ASSERT(wrapperTypeInfo->gcType != RefCountedObject);
89 ASSERT(wrapperTypeInfo->gcType == RefCountedObject || wrapperTypeInfo->gcType == WillBeGarbageCollectedObject);
92 // Clear out the last internal field, which is assumed to contain a valid persistent pointer value.
93 if (wrapperTypeInfo->gcType == GarbageCollectedObject) {
94 wrapper->SetAlignedPointerInInternalField(wrapper->InternalFieldCount() - 1, 0);
95 } else if (wrapperTypeInfo->gcType == WillBeGarbageCollectedObject) {
97 wrapper->SetAlignedPointerInInternalField(wrapper->InternalFieldCount() - 1, 0);
100 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, internalPointer);
101 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(wrapperTypeInfo));
104 inline void V8DOMWrapper::setNativeInfoWithPersistentHandle(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* wrapperTypeInfo, ScriptWrappableBase* internalPointer, PersistentNode* handle)
106 ASSERT(wrapper->InternalFieldCount() >= 3);
107 ASSERT(internalPointer);
108 ASSERT(wrapperTypeInfo);
109 ASSERT(wrapperTypeInfo->gcType != RefCountedObject);
110 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, internalPointer);
111 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(wrapperTypeInfo));
112 // Persistent handle is stored in the last internal field.
113 wrapper->SetAlignedPointerInInternalField(wrapper->InternalFieldCount() - 1, handle);
116 inline void V8DOMWrapper::clearNativeInfo(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* wrapperTypeInfo)
118 ASSERT(wrapper->InternalFieldCount() >= 2);
119 ASSERT(wrapperTypeInfo);
120 // clearNativeInfo() is used only by NP objects, which are not garbage collected.
121 ASSERT(wrapperTypeInfo->gcType == RefCountedObject);
122 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(wrapperTypeInfo));
123 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, 0);
126 template<typename V8T, typename T>
127 inline v8::Handle<v8::Object> V8DOMWrapper::associateObjectWithWrapper(PassRefPtr<T> object, const WrapperTypeInfo* wrapperTypeInfo, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, WrapperConfiguration::Lifetime lifetime)
129 setNativeInfo(wrapper, wrapperTypeInfo, V8T::toInternalPointer(object.get()));
130 ASSERT(isDOMWrapper(wrapper));
131 WrapperConfiguration configuration = buildWrapperConfiguration(object.get(), lifetime);
132 DOMDataStore::setWrapper<V8T>(object.leakRef(), wrapper, isolate, configuration);
136 template<typename V8T, typename T>
137 inline v8::Handle<v8::Object> V8DOMWrapper::associateObjectWithWrapper(T* object, const WrapperTypeInfo* wrapperTypeInfo, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, WrapperConfiguration::Lifetime lifetime)
139 setNativeInfoWithPersistentHandle(wrapper, wrapperTypeInfo, V8T::toInternalPointer(object), new Persistent<T>(object));
140 ASSERT(isDOMWrapper(wrapper));
141 WrapperConfiguration configuration = buildWrapperConfiguration(object, lifetime);
142 DOMDataStore::setWrapper<V8T>(object, wrapper, isolate, configuration);
146 class V8WrapperInstantiationScope {
148 V8WrapperInstantiationScope(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
149 : m_didEnterContext(false)
150 , m_context(isolate->GetCurrentContext())
152 // creationContext should not be empty. Because if we have an
153 // empty creationContext, we will end up creating
154 // a new object in the context currently entered. This is wrong.
155 RELEASE_ASSERT(!creationContext.IsEmpty());
156 v8::Handle<v8::Context> contextForWrapper = creationContext->CreationContext();
157 // For performance, we enter the context only if the currently running context
158 // is different from the context that we are about to enter.
159 if (contextForWrapper == m_context)
161 m_context = v8::Local<v8::Context>::New(isolate, contextForWrapper);
162 m_didEnterContext = true;
166 ~V8WrapperInstantiationScope()
168 if (!m_didEnterContext)
173 v8::Handle<v8::Context> context() const { return m_context; }
176 bool m_didEnterContext;
177 v8::Handle<v8::Context> m_context;
182 #endif // V8DOMWrapper_h