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 template<typename V8T, typename T, typename Wrappable>
55 static bool setReturnValueFromWrapperFast(v8::ReturnValue<v8::Value> returnValue, T* object, v8::Local<v8::Object> holder, Wrappable* wrappable)
57 // What we'd really like to check here is whether we're in the
58 // main world or in an isolated world. The fastest way to do that
59 // is to check that there is no isolated world and the 'object'
60 // is an object that can exist in the main world. The second fastest
61 // way is to check whether the wrappable's wrapper is the same as
63 if ((!DOMWrapperWorld::isolatedWorldsExist() && !canExistInWorker(object)) || holderContainsWrapper(holder, wrappable)) {
64 if (ScriptWrappable::wrapperCanBeStoredInObject(object))
65 return ScriptWrappable::setReturnValueWithSecurityCheck<V8T>(returnValue, object);
66 return mainWorldStore().m_wrapperMap.setReturnValueFrom(returnValue, V8T::toInternalPointer(object));
68 return current(returnValue.GetIsolate()).template setReturnValueFrom<V8T>(returnValue, object);
71 template<typename V8T, typename T>
72 static bool setReturnValueFromWrapper(v8::ReturnValue<v8::Value> returnValue, T* object)
74 if (ScriptWrappable::wrapperCanBeStoredInObject(object) && !canExistInWorker(object)) {
75 if (LIKELY(!DOMWrapperWorld::isolatedWorldsExist()))
76 return ScriptWrappable::setReturnValueWithSecurityCheck<V8T>(returnValue, object);
78 return current(returnValue.GetIsolate()).template setReturnValueFrom<V8T>(returnValue, object);
81 template<typename V8T, typename T>
82 static bool setReturnValueFromWrapperForMainWorld(v8::ReturnValue<v8::Value> returnValue, T* object)
84 if (ScriptWrappable::wrapperCanBeStoredInObject(object))
85 return ScriptWrappable::setReturnValue(returnValue, object);
86 return mainWorldStore().m_wrapperMap.setReturnValueFrom(returnValue, V8T::toInternalPointer(object));
89 template<typename V8T, typename T>
90 static v8::Handle<v8::Object> getWrapper(T* object, v8::Isolate* isolate)
92 if (ScriptWrappable::wrapperCanBeStoredInObject(object) && !canExistInWorker(object)) {
93 if (LIKELY(!DOMWrapperWorld::isolatedWorldsExist())) {
94 v8::Handle<v8::Object> result = ScriptWrappable::getUnsafeWrapperFromObject(object).newLocal(isolate);
95 // Security: always guard against malicious tampering.
96 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(result.IsEmpty() || result->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex) == V8T::toInternalPointer(object));
100 return current(isolate).template get<V8T>(object, isolate);
103 template<typename V8T, typename T>
104 static void setWrapperReference(const v8::Persistent<v8::Object>& parent, T* child, v8::Isolate* isolate)
106 if (ScriptWrappable::wrapperCanBeStoredInObject(child) && !canExistInWorker(child)) {
107 if (LIKELY(!DOMWrapperWorld::isolatedWorldsExist())) {
108 UnsafePersistent<v8::Object> unsafePersistent = ScriptWrappable::getUnsafeWrapperFromObject(child);
109 // Security: always guard against malicious tampering.
110 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(unsafePersistent.isEmpty() || unsafePersistent.value()->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex) == V8T::toInternalPointer(child));
111 unsafePersistent.setReferenceFrom(parent, isolate);
114 current(isolate).template setReference<V8T>(parent, child, isolate);
117 template<typename V8T, typename T>
118 static void setWrapper(T* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperConfiguration& configuration)
120 if (ScriptWrappable::wrapperCanBeStoredInObject(object) && !canExistInWorker(object)) {
121 if (LIKELY(!DOMWrapperWorld::isolatedWorldsExist())) {
122 ScriptWrappable::setWrapperInObject(object, wrapper, isolate, configuration);
126 return current(isolate).template set<V8T>(object, wrapper, isolate, configuration);
129 template<typename V8T, typename T>
130 static bool containsWrapper(T* object, v8::Isolate* isolate)
132 return current(isolate).template containsWrapper<V8T>(object);
135 template<typename V8T, typename T>
136 inline v8::Handle<v8::Object> get(T* object, v8::Isolate* isolate)
138 if (ScriptWrappable::wrapperCanBeStoredInObject(object) && m_type == MainWorld)
139 return ScriptWrappable::getUnsafeWrapperFromObject(object).newLocal(isolate);
140 return m_wrapperMap.newLocal(V8T::toInternalPointer(object), isolate);
143 template<typename V8T, typename T>
144 inline void setReference(const v8::Persistent<v8::Object>& parent, T* child, v8::Isolate* isolate)
146 if (ScriptWrappable::wrapperCanBeStoredInObject(child) && m_type == MainWorld) {
147 ScriptWrappable::getUnsafeWrapperFromObject(child).setReferenceFrom(parent, isolate);
150 m_wrapperMap.setReference(parent, V8T::toInternalPointer(child), isolate);
153 template<typename V8T, typename T>
154 inline bool setReturnValueFrom(v8::ReturnValue<v8::Value> returnValue, T* object)
156 if (ScriptWrappable::wrapperCanBeStoredInObject(object) && m_type == MainWorld)
157 return ScriptWrappable::setReturnValue(returnValue, object);
158 return m_wrapperMap.setReturnValueFrom(returnValue, V8T::toInternalPointer(object));
161 template<typename V8T, typename T>
162 inline bool containsWrapper(T* object)
164 if (ScriptWrappable::wrapperCanBeStoredInObject(object) && m_type == MainWorld)
165 return !ScriptWrappable::getUnsafeWrapperFromObject(object).isEmpty();
166 return m_wrapperMap.containsKey(V8T::toInternalPointer(object));
170 template<typename V8T, typename T>
171 inline void set(T* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperConfiguration& configuration)
174 ASSERT(!wrapper.IsEmpty());
175 if (ScriptWrappable::wrapperCanBeStoredInObject(object) && m_type == MainWorld) {
176 ScriptWrappable::setWrapperInObject(object, wrapper, isolate, configuration);
179 m_wrapperMap.set(V8T::toInternalPointer(object), wrapper, configuration);
182 static DOMDataStore& mainWorldStore();
184 static bool canExistInWorker(void*) { return true; }
185 static bool canExistInWorker(Node*) { return false; }
187 static bool holderContainsWrapper(v8::Local<v8::Object>, void*)
192 static bool holderContainsWrapper(v8::Local<v8::Object> holder, ScriptWrappable* wrappable)
194 // Verify our assumptions about the main world.
195 UnsafePersistent<v8::Object> unsafePersistent = wrappable->unsafePersistent();
196 ASSERT(unsafePersistent.isEmpty() || !(holder == *unsafePersistent.persistent()) || current(v8::Isolate::GetCurrent()).m_type == MainWorld);
197 return holder == *unsafePersistent.persistent();
200 WrapperWorldType m_type;
201 DOMWrapperMap<void> m_wrapperMap;
204 } // namespace WebCore
206 #endif // DOMDataStore_h