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.
32 #include "bindings/core/v8/DOMWrapperWorld.h"
34 #include "bindings/core/v8/DOMDataStore.h"
35 #include "bindings/core/v8/ScriptController.h"
36 #include "bindings/core/v8/V8Binding.h"
37 #include "bindings/core/v8/V8DOMActivityLogger.h"
38 #include "bindings/core/v8/V8DOMWrapper.h"
39 #include "bindings/core/v8/V8Window.h"
40 #include "bindings/core/v8/WindowProxy.h"
41 #include "bindings/core/v8/WrapperTypeInfo.h"
42 #include "core/dom/ExecutionContext.h"
43 #include "wtf/HashTraits.h"
44 #include "wtf/StdLibExtras.h"
48 unsigned DOMWrapperWorld::isolatedWorldCount = 0;
49 DOMWrapperWorld* DOMWrapperWorld::worldOfInitializingWindow = 0;
51 PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::create(int worldId, int extensionGroup)
53 return adoptRef(new DOMWrapperWorld(worldId, extensionGroup));
56 DOMWrapperWorld::DOMWrapperWorld(int worldId, int extensionGroup)
58 , m_extensionGroup(extensionGroup)
59 , m_domDataStore(adoptPtr(new DOMDataStore(isMainWorld())))
63 DOMWrapperWorld& DOMWrapperWorld::mainWorld()
65 ASSERT(isMainThread());
66 DEFINE_STATIC_REF(DOMWrapperWorld, cachedMainWorld, (DOMWrapperWorld::create(MainWorldId, mainWorldExtensionGroup)));
67 return *cachedMainWorld;
70 DOMWrapperWorld& DOMWrapperWorld::privateScriptIsolatedWorld()
72 ASSERT(isMainThread());
73 DEFINE_STATIC_LOCAL(RefPtr<DOMWrapperWorld>, cachedPrivateScriptIsolatedWorld, ());
74 if (!cachedPrivateScriptIsolatedWorld) {
75 cachedPrivateScriptIsolatedWorld = DOMWrapperWorld::create(PrivateScriptIsolatedWorldId, privateScriptIsolatedWorldExtensionGroup);
78 return *cachedPrivateScriptIsolatedWorld;
81 typedef HashMap<int, DOMWrapperWorld*> WorldMap;
82 static WorldMap& isolatedWorldMap()
84 ASSERT(isMainThread());
85 DEFINE_STATIC_LOCAL(WorldMap, map, ());
89 void DOMWrapperWorld::allWorldsInMainThread(Vector<RefPtr<DOMWrapperWorld> >& worlds)
91 ASSERT(isMainThread());
92 worlds.append(&mainWorld());
93 WorldMap& isolatedWorlds = isolatedWorldMap();
94 for (WorldMap::iterator it = isolatedWorlds.begin(); it != isolatedWorlds.end(); ++it)
95 worlds.append(it->value);
98 DOMWrapperWorld::~DOMWrapperWorld()
100 ASSERT(!isMainWorld());
104 if (!isIsolatedWorld())
107 WorldMap& map = isolatedWorldMap();
108 WorldMap::iterator it = map.find(m_worldId);
109 if (it == map.end()) {
110 ASSERT_NOT_REACHED();
113 ASSERT(it->value == this);
116 isolatedWorldCount--;
119 void DOMWrapperWorld::dispose()
121 m_domObjectHolders.clear();
122 m_domDataStore.clear();
126 static bool isIsolatedWorldId(int worldId)
128 return MainWorldId < worldId && worldId < IsolatedWorldIdLimit;
132 PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::ensureIsolatedWorld(int worldId, int extensionGroup)
134 ASSERT(isIsolatedWorldId(worldId));
136 WorldMap& map = isolatedWorldMap();
137 WorldMap::AddResult result = map.add(worldId, 0);
138 RefPtr<DOMWrapperWorld> world = result.storedValue->value;
140 ASSERT(world->worldId() == worldId);
141 ASSERT(world->extensionGroup() == extensionGroup);
142 return world.release();
145 world = DOMWrapperWorld::create(worldId, extensionGroup);
146 result.storedValue->value = world.get();
147 isolatedWorldCount++;
148 return world.release();
151 typedef HashMap<int, RefPtr<SecurityOrigin> > IsolatedWorldSecurityOriginMap;
152 static IsolatedWorldSecurityOriginMap& isolatedWorldSecurityOrigins()
154 ASSERT(isMainThread());
155 DEFINE_STATIC_LOCAL(IsolatedWorldSecurityOriginMap, map, ());
159 SecurityOrigin* DOMWrapperWorld::isolatedWorldSecurityOrigin()
161 ASSERT(this->isIsolatedWorld());
162 IsolatedWorldSecurityOriginMap& origins = isolatedWorldSecurityOrigins();
163 IsolatedWorldSecurityOriginMap::iterator it = origins.find(worldId());
164 return it == origins.end() ? 0 : it->value.get();
167 void DOMWrapperWorld::setIsolatedWorldSecurityOrigin(int worldId, PassRefPtr<SecurityOrigin> securityOrigin)
169 ASSERT(isIsolatedWorldId(worldId));
171 isolatedWorldSecurityOrigins().set(worldId, securityOrigin);
173 isolatedWorldSecurityOrigins().remove(worldId);
176 typedef HashMap<int, String > IsolatedWorldHumanReadableNameMap;
177 static IsolatedWorldHumanReadableNameMap& isolatedWorldHumanReadableNames()
179 ASSERT(isMainThread());
180 DEFINE_STATIC_LOCAL(IsolatedWorldHumanReadableNameMap, map, ());
184 String DOMWrapperWorld::isolatedWorldHumanReadableName()
186 ASSERT(this->isIsolatedWorld());
187 return isolatedWorldHumanReadableNames().get(worldId());
190 void DOMWrapperWorld::setIsolatedWorldHumanReadableName(int worldId, const String& humanReadableName)
192 ASSERT(isIsolatedWorldId(worldId));
193 isolatedWorldHumanReadableNames().set(worldId, humanReadableName);
196 typedef HashMap<int, bool> IsolatedWorldContentSecurityPolicyMap;
197 static IsolatedWorldContentSecurityPolicyMap& isolatedWorldContentSecurityPolicies()
199 ASSERT(isMainThread());
200 DEFINE_STATIC_LOCAL(IsolatedWorldContentSecurityPolicyMap, map, ());
204 bool DOMWrapperWorld::isolatedWorldHasContentSecurityPolicy()
206 ASSERT(this->isIsolatedWorld());
207 IsolatedWorldContentSecurityPolicyMap& policies = isolatedWorldContentSecurityPolicies();
208 IsolatedWorldContentSecurityPolicyMap::iterator it = policies.find(worldId());
209 return it == policies.end() ? false : it->value;
212 void DOMWrapperWorld::setIsolatedWorldContentSecurityPolicy(int worldId, const String& policy)
214 ASSERT(isIsolatedWorldId(worldId));
215 if (!policy.isEmpty())
216 isolatedWorldContentSecurityPolicies().set(worldId, true);
218 isolatedWorldContentSecurityPolicies().remove(worldId);
221 void DOMWrapperWorld::registerDOMObjectHolderInternal(PassOwnPtr<DOMObjectHolderBase> holderBase)
223 ASSERT(!m_domObjectHolders.contains(holderBase.get()));
224 holderBase->setWorld(this);
225 holderBase->setWeak(&DOMWrapperWorld::weakCallbackForDOMObjectHolder);
226 m_domObjectHolders.add(holderBase);
229 void DOMWrapperWorld::unregisterDOMObjectHolder(DOMObjectHolderBase* holderBase)
231 ASSERT(m_domObjectHolders.contains(holderBase));
232 m_domObjectHolders.remove(holderBase);
235 void DOMWrapperWorld::weakCallbackForDOMObjectHolder(const v8::WeakCallbackData<v8::Value, DOMObjectHolderBase>& data)
237 DOMObjectHolderBase* holderBase = data.GetParameter();
238 holderBase->world()->unregisterDOMObjectHolder(holderBase);