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 DOMDataStore_h
32 #define DOMDataStore_h
34 #include "bindings/v8/DOMWrapperMap.h"
35 #include "bindings/v8/DOMWrapperWorld.h"
36 #include "bindings/v8/ScriptWrappable.h"
37 #include "bindings/v8/WrapperTypeInfo.h"
39 #include "wtf/Noncopyable.h"
40 #include "wtf/StdLibExtras.h"
47 WTF_MAKE_NONCOPYABLE(DOMDataStore);
49 explicit DOMDataStore(WrapperWorldType);
52 static DOMDataStore& current(v8::Isolate*);
54 // We can use a wrapper stored in a ScriptWrappable when we're in the main world.
55 // This method does the fast check if we're in the main world. If this method returns true,
56 // it is guaranteed that we're in the main world. On the other hand, if this method returns
57 // false, nothing is guaranteed (we might be in the main world).
59 static bool canUseScriptWrappable(T* object)
61 return !DOMWrapperWorld::isolatedWorldsExist()
62 && !canExistInWorker(object)
63 && ScriptWrappable::wrapperCanBeStoredInObject(object);
66 template<typename V8T, typename T, typename Wrappable>
67 static bool setReturnValueFromWrapperFast(v8::ReturnValue<v8::Value> returnValue, T* object, v8::Local<v8::Object> holder, Wrappable* wrappable)
69 if (canUseScriptWrappable(object))
70 return ScriptWrappable::setReturnValueWithSecurityCheck<V8T>(returnValue, object);
71 // The second fastest way to check if we're in the main world is to check if
72 // the wrappable's wrapper is the same as the holder.
73 // FIXME: Investigate if it's worth having this check for performance.
74 if (holderContainsWrapper(holder, wrappable)) {
75 if (ScriptWrappable::wrapperCanBeStoredInObject(object))
76 return ScriptWrappable::setReturnValueWithSecurityCheck<V8T>(returnValue, object);
77 return DOMWrapperWorld::mainWorld()->domDataStore().m_wrapperMap.setReturnValueFrom(returnValue, V8T::toInternalPointer(object));
79 return current(returnValue.GetIsolate()).template setReturnValueFrom<V8T>(returnValue, object);
82 template<typename V8T, typename T>
83 static bool setReturnValueFromWrapper(v8::ReturnValue<v8::Value> returnValue, T* object)
85 if (canUseScriptWrappable(object))
86 return ScriptWrappable::setReturnValueWithSecurityCheck<V8T>(returnValue, object);
87 return current(returnValue.GetIsolate()).template setReturnValueFrom<V8T>(returnValue, object);
90 template<typename V8T, typename T>
91 static bool setReturnValueFromWrapperForMainWorld(v8::ReturnValue<v8::Value> returnValue, T* object)
93 if (ScriptWrappable::wrapperCanBeStoredInObject(object))
94 return ScriptWrappable::setReturnValue(returnValue, object);
95 return DOMWrapperWorld::mainWorld()->domDataStore().m_wrapperMap.setReturnValueFrom(returnValue, V8T::toInternalPointer(object));
98 template<typename V8T, typename T>
99 static v8::Handle<v8::Object> getWrapper(T* object, v8::Isolate* isolate)
101 if (canUseScriptWrappable(object)) {
102 v8::Handle<v8::Object> result = ScriptWrappable::getUnsafeWrapperFromObject(object).newLocal(isolate);
103 // Security: always guard against malicious tampering.
104 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(result.IsEmpty() || result->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex) == V8T::toInternalPointer(object));
107 return current(isolate).template get<V8T>(object, isolate);
110 template<typename V8T, typename T>
111 static void setWrapperReference(const v8::Persistent<v8::Object>& parent, T* child, v8::Isolate* isolate)
113 if (canUseScriptWrappable(child)) {
114 UnsafePersistent<v8::Object> unsafePersistent = ScriptWrappable::getUnsafeWrapperFromObject(child);
115 // Security: always guard against malicious tampering.
116 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(unsafePersistent.isEmpty() || unsafePersistent.value()->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex) == V8T::toInternalPointer(child));
117 unsafePersistent.setReferenceFrom(parent, isolate);
120 current(isolate).template setReference<V8T>(parent, child, isolate);
123 template<typename V8T, typename T>
124 static void setWrapper(T* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperConfiguration& configuration)
126 if (canUseScriptWrappable(object)) {
127 ScriptWrappable::setWrapperInObject(object, wrapper, isolate, configuration);
130 return current(isolate).template set<V8T>(object, wrapper, isolate, configuration);
133 template<typename V8T, typename T>
134 static bool containsWrapper(T* object, v8::Isolate* isolate)
136 return current(isolate).template containsWrapper<V8T>(object);
139 template<typename V8T, typename T>
140 inline v8::Handle<v8::Object> get(T* object, v8::Isolate* isolate)
142 if (ScriptWrappable::wrapperCanBeStoredInObject(object) && m_type == MainWorld)
143 return ScriptWrappable::getUnsafeWrapperFromObject(object).newLocal(isolate);
144 return m_wrapperMap.newLocal(V8T::toInternalPointer(object), isolate);
147 template<typename V8T, typename T>
148 inline void setReference(const v8::Persistent<v8::Object>& parent, T* child, v8::Isolate* isolate)
150 if (ScriptWrappable::wrapperCanBeStoredInObject(child) && m_type == MainWorld) {
151 ScriptWrappable::getUnsafeWrapperFromObject(child).setReferenceFrom(parent, isolate);
154 m_wrapperMap.setReference(parent, V8T::toInternalPointer(child), isolate);
157 template<typename V8T, typename T>
158 inline bool setReturnValueFrom(v8::ReturnValue<v8::Value> returnValue, T* object)
160 if (ScriptWrappable::wrapperCanBeStoredInObject(object) && m_type == MainWorld)
161 return ScriptWrappable::setReturnValue(returnValue, object);
162 return m_wrapperMap.setReturnValueFrom(returnValue, V8T::toInternalPointer(object));
165 template<typename V8T, typename T>
166 inline bool containsWrapper(T* object)
168 if (ScriptWrappable::wrapperCanBeStoredInObject(object) && m_type == MainWorld)
169 return !ScriptWrappable::getUnsafeWrapperFromObject(object).isEmpty();
170 return m_wrapperMap.containsKey(V8T::toInternalPointer(object));
174 template<typename V8T, typename T>
175 inline void set(T* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperConfiguration& configuration)
178 ASSERT(!wrapper.IsEmpty());
179 if (ScriptWrappable::wrapperCanBeStoredInObject(object) && m_type == MainWorld) {
180 ScriptWrappable::setWrapperInObject(object, wrapper, isolate, configuration);
183 m_wrapperMap.set(V8T::toInternalPointer(object), wrapper, configuration);
186 static bool canExistInWorker(void*) { return true; }
187 static bool canExistInWorker(Node*) { return false; }
189 static bool holderContainsWrapper(v8::Local<v8::Object>, void*)
194 static bool holderContainsWrapper(v8::Local<v8::Object> holder, ScriptWrappable* wrappable)
196 // Verify our assumptions about the main world.
197 UnsafePersistent<v8::Object> unsafePersistent = wrappable->unsafePersistent();
198 ASSERT(unsafePersistent.isEmpty() || !(holder == *unsafePersistent.persistent()) || current(v8::Isolate::GetCurrent()).m_type == MainWorld);
199 return holder == *unsafePersistent.persistent();
202 WrapperWorldType m_type;
203 DOMWrapperMap<void> m_wrapperMap;
206 } // namespace WebCore
208 #endif // DOMDataStore_h