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 "DOMWrapperWorld.h"
34 #include "DOMDataStore.h"
35 #include "V8Binding.h"
36 #include "V8DOMActivityLogger.h"
37 #include "V8DOMWindow.h"
38 #include "V8DOMWrapper.h"
39 #include "WrapperTypeInfo.h"
40 #include <wtf/HashTraits.h>
41 #include <wtf/MainThread.h>
42 #include <wtf/StdLibExtras.h>
46 int DOMWrapperWorld::isolatedWorldCount = 0;
47 static bool initializingWindow = false;
49 PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::createUninitializedWorld()
51 return adoptRef(new DOMWrapperWorld(uninitializedWorldId, uninitializedExtensionGroup));
54 void DOMWrapperWorld::setInitializingWindow(bool initializing)
56 initializingWindow = initializing;
59 PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::createMainWorld()
61 return adoptRef(new DOMWrapperWorld(mainWorldId, mainWorldExtensionGroup));
64 DOMWrapperWorld::DOMWrapperWorld(int worldId, int extensionGroup)
66 , m_extensionGroup(extensionGroup)
68 if (isIsolatedWorld())
69 m_domDataStore = adoptPtr(new DOMDataStore(IsolatedWorld));
72 DOMWrapperWorld* mainThreadNormalWorld()
74 ASSERT(isMainThread());
75 DEFINE_STATIC_LOCAL(RefPtr<DOMWrapperWorld>, cachedNormalWorld, (DOMWrapperWorld::createMainWorld()));
76 return cachedNormalWorld.get();
79 bool DOMWrapperWorld::contextHasCorrectPrototype(v8::Handle<v8::Context> context)
81 ASSERT(isMainThread());
82 if (initializingWindow)
84 return V8DOMWrapper::isWrapperOfType(toInnerGlobalObject(context), &V8DOMWindow::info);
87 static void isolatedWorldWeakCallback(v8::Isolate* isolate, v8::Persistent<v8::Value> object, void* parameter)
89 object.Dispose(isolate);
91 static_cast<DOMWrapperWorld*>(parameter)->deref();
94 void DOMWrapperWorld::makeContextWeak(v8::Handle<v8::Context> context)
96 ASSERT(isIsolatedWorld());
97 ASSERT(isolatedWorld(context) == this);
98 v8::Isolate* isolate = context->GetIsolate();
99 v8::Persistent<v8::Context>::New(isolate, context).MakeWeak(isolate, this, isolatedWorldWeakCallback);
100 // Matching deref is in weak callback.
104 void DOMWrapperWorld::setIsolatedWorldField(v8::Handle<v8::Context> context)
106 context->SetAlignedPointerInEmbedderData(v8ContextIsolatedWorld, isMainWorld() ? 0 : this);
109 typedef HashMap<int, DOMWrapperWorld*> WorldMap;
110 static WorldMap& isolatedWorldMap()
112 ASSERT(isMainThread());
113 DEFINE_STATIC_LOCAL(WorldMap, map, ());
117 void DOMWrapperWorld::getAllWorlds(Vector<RefPtr<DOMWrapperWorld> >& worlds)
119 worlds.append(mainThreadNormalWorld());
120 WorldMap& isolatedWorlds = isolatedWorldMap();
121 for (WorldMap::iterator it = isolatedWorlds.begin(); it != isolatedWorlds.end(); ++it)
122 worlds.append(it->value);
125 DOMWrapperWorld::~DOMWrapperWorld()
127 ASSERT(!isMainWorld());
129 if (!isIsolatedWorld())
132 WorldMap& map = isolatedWorldMap();
133 WorldMap::iterator i = map.find(m_worldId);
134 if (i == map.end()) {
135 ASSERT_NOT_REACHED();
138 ASSERT(i->value == this);
141 isolatedWorldCount--;
142 ASSERT(map.size() == isolatedWorldCount);
145 static int temporaryWorldId = DOMWrapperWorld::uninitializedWorldId-1;
147 PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::ensureIsolatedWorld(int worldId, int extensionGroup)
149 ASSERT(worldId != mainWorldId);
150 ASSERT(worldId >= uninitializedWorldId);
152 WorldMap& map = isolatedWorldMap();
153 if (worldId == uninitializedWorldId)
154 worldId = temporaryWorldId--;
156 WorldMap::iterator i = map.find(worldId);
157 if (i != map.end()) {
158 ASSERT(i->value->worldId() == worldId);
159 ASSERT(i->value->extensionGroup() == extensionGroup);
164 RefPtr<DOMWrapperWorld> world = adoptRef(new DOMWrapperWorld(worldId, extensionGroup));
165 map.add(worldId, world.get());
166 isolatedWorldCount++;
167 ASSERT(map.size() == isolatedWorldCount);
169 return world.release();
172 typedef HashMap<int, RefPtr<SecurityOrigin> > IsolatedWorldSecurityOriginMap;
173 static IsolatedWorldSecurityOriginMap& isolatedWorldSecurityOrigins()
175 ASSERT(isMainThread());
176 DEFINE_STATIC_LOCAL(IsolatedWorldSecurityOriginMap, map, ());
180 SecurityOrigin* DOMWrapperWorld::isolatedWorldSecurityOrigin()
182 ASSERT(this->isIsolatedWorld());
183 IsolatedWorldSecurityOriginMap& origins = isolatedWorldSecurityOrigins();
184 IsolatedWorldSecurityOriginMap::iterator it = origins.find(worldId());
185 return it == origins.end() ? 0 : it->value.get();
188 void DOMWrapperWorld::setIsolatedWorldSecurityOrigin(int worldID, PassRefPtr<SecurityOrigin> securityOrigin)
190 ASSERT(DOMWrapperWorld::isIsolatedWorldId(worldID));
192 isolatedWorldSecurityOrigins().set(worldID, securityOrigin);
194 isolatedWorldSecurityOrigins().remove(worldID);
197 void DOMWrapperWorld::clearIsolatedWorldSecurityOrigin(int worldID)
199 ASSERT(DOMWrapperWorld::isIsolatedWorldId(worldID));
200 isolatedWorldSecurityOrigins().remove(worldID);
203 typedef HashMap<int, bool> IsolatedWorldContentSecurityPolicyMap;
204 static IsolatedWorldContentSecurityPolicyMap& isolatedWorldContentSecurityPolicies()
206 ASSERT(isMainThread());
207 DEFINE_STATIC_LOCAL(IsolatedWorldContentSecurityPolicyMap, map, ());
211 bool DOMWrapperWorld::isolatedWorldHasContentSecurityPolicy()
213 ASSERT(this->isIsolatedWorld());
214 IsolatedWorldContentSecurityPolicyMap& policies = isolatedWorldContentSecurityPolicies();
215 IsolatedWorldContentSecurityPolicyMap::iterator it = policies.find(worldId());
216 return it == policies.end() ? false : it->value;
219 void DOMWrapperWorld::setIsolatedWorldContentSecurityPolicy(int worldID, const String& policy)
221 ASSERT(DOMWrapperWorld::isIsolatedWorldId(worldID));
222 if (!policy.isEmpty())
223 isolatedWorldContentSecurityPolicies().set(worldID, true);
225 isolatedWorldContentSecurityPolicies().remove(worldID);
228 void DOMWrapperWorld::clearIsolatedWorldContentSecurityPolicy(int worldID)
230 ASSERT(DOMWrapperWorld::isIsolatedWorldId(worldID));
231 isolatedWorldContentSecurityPolicies().remove(worldID);
234 typedef HashMap<int, OwnPtr<V8DOMActivityLogger>, WTF::IntHash<int>, WTF::UnsignedWithZeroKeyHashTraits<int> > DOMActivityLoggerMap;
235 static DOMActivityLoggerMap& domActivityLoggers()
237 ASSERT(isMainThread());
238 DEFINE_STATIC_LOCAL(DOMActivityLoggerMap, map, ());
242 void DOMWrapperWorld::setActivityLogger(int worldId, PassOwnPtr<V8DOMActivityLogger> logger)
244 domActivityLoggers().set(worldId, logger);
247 V8DOMActivityLogger* DOMWrapperWorld::activityLogger(int worldId)
249 DOMActivityLoggerMap& loggers = domActivityLoggers();
250 DOMActivityLoggerMap::iterator it = loggers.find(worldId);
251 return it == loggers.end() ? 0 : it->value.get();
254 } // namespace WebCore