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 "DOMWrapperMap.h"
35 #include "DOMWrapperWorld.h"
37 #include "V8GCController.h"
39 #include <wtf/HashMap.h>
40 #include <wtf/MainThread.h>
41 #include <wtf/Noncopyable.h>
42 #include <wtf/OwnPtr.h>
43 #include <wtf/StdLibExtras.h>
44 #include <wtf/Threading.h>
45 #include <wtf/ThreadSpecific.h>
46 #include <wtf/Vector.h>
51 WTF_MAKE_NONCOPYABLE(DOMDataStore);
59 explicit DOMDataStore(Type);
62 static DOMDataStore* current(v8::Isolate*);
64 template<typename T, typename HolderContainer, typename Wrappable>
65 static v8::Handle<v8::Object> getWrapperFast(T* object, const HolderContainer& container, Wrappable* holder)
67 // What we'd really like to check here is whether we're in the
68 // main world or in an isolated world. The fastest way to do that
69 // is to check that there is no isolated world and the 'object'
70 // is an object that can exist in the main world. The second fastest
71 // way is to check whether the wrappable's wrapper is the same as
73 if ((!DOMWrapperWorld::isolatedWorldsExist() && isMainWorldObject(object)) || holderContainsWrapper(container, holder)) {
74 if (mainWorldWrapperIsStoredInObject(object))
75 return getWrapperFromObject(object);
76 return mainWorldStore()->m_wrapperMap.get(object);
78 return current(container.GetIsolate())->get(object);
82 static v8::Handle<v8::Object> getWrapper(T* object, v8::Isolate* isolate)
84 if (mainWorldWrapperIsStoredInObject(object) && isMainWorldObject(object)) {
85 if (LIKELY(!DOMWrapperWorld::isolatedWorldsExist()))
86 return getWrapperFromObject(object);
88 return current(isolate)->get(object);
92 static void setWrapper(T* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperConfiguration& configuration)
94 if (mainWorldWrapperIsStoredInObject(object) && isMainWorldObject(object)) {
95 if (LIKELY(!DOMWrapperWorld::isolatedWorldsExist())) {
96 setWrapperInObject(object, wrapper, isolate, configuration);
100 return current(isolate)->set(object, wrapper, isolate, configuration);
104 inline v8::Handle<v8::Object> get(T* object)
106 if (mainWorldWrapperIsStoredInObject(object) && m_type == MainWorld)
107 return getWrapperFromObject(object);
108 return m_wrapperMap.get(object);
111 void reportMemoryUsage(MemoryObjectInfo*) const;
115 inline void set(T* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperConfiguration& configuration)
118 ASSERT(!wrapper.IsEmpty());
119 if (mainWorldWrapperIsStoredInObject(object) && m_type == MainWorld) {
120 setWrapperInObject(object, wrapper, isolate, configuration);
123 m_wrapperMap.set(object, wrapper, configuration);
126 static DOMDataStore* mainWorldStore();
128 static bool mainWorldWrapperIsStoredInObject(void*) { return false; }
129 static bool mainWorldWrapperIsStoredInObject(ScriptWrappable*) { return true; }
131 static bool isMainWorldObject(void*) { return false; }
132 static bool isMainWorldObject(Node*) { return true; }
134 template<typename HolderContainer>
135 static bool holderContainsWrapper(const HolderContainer&, void*)
139 template<typename HolderContainer>
140 static bool holderContainsWrapper(const HolderContainer& container, ScriptWrappable* wrappable)
142 // Verify our assumptions about the main world.
143 ASSERT(wrappable->wrapper().IsEmpty() || container.Holder() != wrappable->wrapper() || current(v8::Isolate::GetCurrent())->m_type == MainWorld);
144 return container.Holder() == wrappable->wrapper();
147 static v8::Handle<v8::Object> getWrapperFromObject(void*)
149 ASSERT_NOT_REACHED();
150 return v8::Handle<v8::Object>();
152 static v8::Handle<v8::Object> getWrapperFromObject(ScriptWrappable* object)
154 return object->wrapper();
157 static void setWrapperInObject(void*, v8::Handle<v8::Object>, v8::Isolate*, const WrapperConfiguration&)
159 ASSERT_NOT_REACHED();
161 static void setWrapperInObject(ScriptWrappable* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperConfiguration& configuration)
163 object->setWrapper(wrapper, isolate, configuration);
167 DOMWrapperMap<void> m_wrapperMap;
170 } // namespace WebCore
172 #endif // DOMDataStore_h