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/core/v8/DOMWrapperMap.h"
35 #include "bindings/core/v8/DOMWrapperWorld.h"
36 #include "bindings/core/v8/ScriptWrappable.h"
37 #include "bindings/core/v8/WrapperTypeInfo.h"
38 #include "wtf/Noncopyable.h"
39 #include "wtf/StdLibExtras.h"
47 WTF_MAKE_NONCOPYABLE(DOMDataStore);
49 explicit DOMDataStore(bool isMainWorld)
50 : m_isMainWorld(isMainWorld)
51 , m_wrapperMap(v8::Isolate::GetCurrent()) { }
54 // We never actually destruct the main world's DOMDataStore.
55 ASSERT(!m_isMainWorld);
59 static DOMDataStore& current(v8::Isolate* isolate)
61 return DOMWrapperWorld::current(isolate).domDataStore();
64 static bool setReturnValueFast(v8::ReturnValue<v8::Value> returnValue, ScriptWrappable* object, v8::Local<v8::Object> holder, const ScriptWrappable* wrappable)
66 // The second fastest way to check if we're in the main world is to check if
67 // the wrappable's wrapper is the same as the holder.
68 // FIXME: Investigate if it's worth having this check for performance.
69 if (holderContainsWrapper(holder, wrappable))
70 return object->setReturnValue(returnValue);
71 return current(returnValue.GetIsolate()).setReturnValueFrom(returnValue, object);
74 static bool setReturnValueFast(v8::ReturnValue<v8::Value> returnValue, Node* node, v8::Local<v8::Object> holder, const ScriptWrappable* wrappable)
76 if (canUseScriptWrappable(node)
77 // The second fastest way to check if we're in the main world is to
78 // check if the wrappable's wrapper is the same as the holder.
79 // FIXME: Investigate if it's worth having this check for performance.
80 || holderContainsWrapper(holder, wrappable))
81 return ScriptWrappable::fromNode(node)->setReturnValue(returnValue);
82 return current(returnValue.GetIsolate()).setReturnValueFrom(returnValue, ScriptWrappable::fromNode(node));
85 static bool setReturnValue(v8::ReturnValue<v8::Value> returnValue, ScriptWrappableBase* object)
87 return current(returnValue.GetIsolate()).setReturnValueFrom(returnValue, object);
90 static bool setReturnValue(v8::ReturnValue<v8::Value> returnValue, ScriptWrappable* object)
92 return current(returnValue.GetIsolate()).setReturnValueFrom(returnValue, object);
95 static bool setReturnValue(v8::ReturnValue<v8::Value> returnValue, Node* object)
97 if (canUseScriptWrappable(object))
98 return ScriptWrappable::fromNode(object)->setReturnValue(returnValue);
99 return current(returnValue.GetIsolate()).setReturnValueFrom(returnValue, ScriptWrappable::fromNode(object));
102 static bool setReturnValueForMainWorld(v8::ReturnValue<v8::Value> returnValue, ScriptWrappable* object)
104 return object->setReturnValue(returnValue);
107 static v8::Handle<v8::Object> getWrapper(ScriptWrappableBase* object, v8::Isolate* isolate)
109 return current(isolate).get(object, isolate);
112 static v8::Handle<v8::Object> getWrapper(ScriptWrappable* object, v8::Isolate* isolate)
114 return current(isolate).get(object, isolate);
117 static v8::Handle<v8::Object> getWrapper(Node* node, v8::Isolate* isolate)
119 if (canUseScriptWrappable(node)) {
120 v8::Handle<v8::Object> result = ScriptWrappable::fromNode(node)->newLocalWrapper(isolate);
121 // Security: always guard against malicious tampering.
122 ScriptWrappable::fromNode(node)->assertWrapperSanity(result);
125 return current(isolate).get(ScriptWrappable::fromNode(node), isolate);
128 static void setWrapperReference(const v8::Persistent<v8::Object>& parent, ScriptWrappableBase* child, v8::Isolate* isolate)
130 current(isolate).setReference(parent, child, isolate);
133 static void setWrapperReference(const v8::Persistent<v8::Object>& parent, ScriptWrappable* child, v8::Isolate* isolate)
135 current(isolate).setReference(parent, child, isolate);
138 static void setWrapperReference(const v8::Persistent<v8::Object>& parent, Node* child, v8::Isolate* isolate)
140 if (canUseScriptWrappable(child)) {
141 ScriptWrappable::fromNode(child)->setReference(parent, isolate);
144 current(isolate).setReference(parent, ScriptWrappable::fromNode(child), isolate);
147 static void setWrapper(ScriptWrappableBase* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperTypeInfo* wrapperTypeInfo)
149 return current(isolate).set(object, wrapper, isolate, wrapperTypeInfo);
152 static void setWrapper(ScriptWrappable* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperTypeInfo* wrapperTypeInfo)
154 return current(isolate).set(object, wrapper, isolate, wrapperTypeInfo);
157 static void setWrapper(Node* node, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperTypeInfo* wrapperTypeInfo)
159 if (canUseScriptWrappable(node)) {
160 ScriptWrappable::fromNode(node)->setWrapper(wrapper, isolate, wrapperTypeInfo);
163 return current(isolate).set(ScriptWrappable::fromNode(node), wrapper, isolate, wrapperTypeInfo);
166 static bool containsWrapper(ScriptWrappableBase* object, v8::Isolate* isolate)
168 return current(isolate).containsWrapper(object);
171 static bool containsWrapper(ScriptWrappable* object, v8::Isolate* isolate)
173 return current(isolate).containsWrapper(object);
176 v8::Handle<v8::Object> get(ScriptWrappableBase* object, v8::Isolate* isolate)
178 return m_wrapperMap.newLocal(object->toScriptWrappableBase(), isolate);
181 v8::Handle<v8::Object> get(ScriptWrappable* object, v8::Isolate* isolate)
184 return object->newLocalWrapper(isolate);
185 return m_wrapperMap.newLocal(object->toScriptWrappableBase(), isolate);
188 void setReference(const v8::Persistent<v8::Object>& parent, ScriptWrappableBase* child, v8::Isolate* isolate)
190 m_wrapperMap.setReference(parent, child, isolate);
193 void setReference(const v8::Persistent<v8::Object>& parent, ScriptWrappable* child, v8::Isolate* isolate)
196 child->setReference(parent, isolate);
199 m_wrapperMap.setReference(parent, child->toScriptWrappableBase(), isolate);
202 bool setReturnValueFrom(v8::ReturnValue<v8::Value> returnValue, ScriptWrappableBase* object)
204 return m_wrapperMap.setReturnValueFrom(returnValue, object);
207 bool setReturnValueFrom(v8::ReturnValue<v8::Value> returnValue, ScriptWrappable* object)
210 return object->setReturnValue(returnValue);
211 return m_wrapperMap.setReturnValueFrom(returnValue, object->toScriptWrappableBase());
214 bool containsWrapper(ScriptWrappableBase* object)
216 return m_wrapperMap.containsKey(object->toScriptWrappableBase());
219 bool containsWrapper(ScriptWrappable* object)
222 return object->containsWrapper();
223 return m_wrapperMap.containsKey(object->toScriptWrappableBase());
227 void set(ScriptWrappableBase* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperTypeInfo* wrapperTypeInfo)
230 ASSERT(!wrapper.IsEmpty());
231 m_wrapperMap.set(object->toScriptWrappableBase(), wrapper, wrapperTypeInfo);
234 void set(ScriptWrappable* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperTypeInfo* wrapperTypeInfo)
237 ASSERT(!wrapper.IsEmpty());
239 object->setWrapper(wrapper, isolate, wrapperTypeInfo);
242 m_wrapperMap.set(object->toScriptWrappableBase(), wrapper, wrapperTypeInfo);
245 // We can use a wrapper stored in a ScriptWrappable when we're in the main world.
246 // This method does the fast check if we're in the main world. If this method returns true,
247 // it is guaranteed that we're in the main world. On the other hand, if this method returns
248 // false, nothing is guaranteed (we might be in the main world).
249 static bool canUseScriptWrappable(Node*)
251 // This helper function itself doesn't use the argument, but we have to
252 // make sure that the argument is type of Node* because Node and its
253 // subclasses satisfy the following two conditions.
254 // 1. Nodes never exist in worker.
255 // 2. Node inherits from ScriptWrappable.
256 // and if there exists no isolated world, we're sure that we're in the
257 // main world and we can use ScriptWrappable's wrapper.
258 return !DOMWrapperWorld::isolatedWorldsExist();
261 static bool holderContainsWrapper(v8::Local<v8::Object> holder, const ScriptWrappable* wrappable)
263 // Verify our assumptions about the main world.
265 ASSERT(!wrappable->containsWrapper() || !wrappable->isEqualTo(holder) || current(v8::Isolate::GetCurrent()).m_isMainWorld);
266 return wrappable->isEqualTo(holder);
270 DOMWrapperMap<ScriptWrappableBase> m_wrapperMap;
274 inline void DOMWrapperMap<ScriptWrappableBase>::PersistentValueMapTraits::Dispose(
275 v8::Isolate* isolate,
276 v8::UniquePersistent<v8::Object> value,
277 ScriptWrappableBase* key)
279 RELEASE_ASSERT(!value.IsEmpty()); // See crbug.com/368095.
280 releaseObject(v8::Local<v8::Object>::New(isolate, value));
285 #endif // DOMDataStore_h