Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / DOMWrapperWorld.cpp
index 8a3ef0f..7fb57ae 100644 (file)
 namespace WebCore {
 
 unsigned DOMWrapperWorld::isolatedWorldCount = 0;
-static bool initializingWindow = false;
 
-void DOMWrapperWorld::setInitializingWindow(bool initializing)
+PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::create(int worldId, int extensionGroup)
 {
-    initializingWindow = initializing;
-}
-
-PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::createMainWorld()
-{
-    return adoptRef(new DOMWrapperWorld(MainWorldId, mainWorldExtensionGroup));
+    return adoptRef(new DOMWrapperWorld(worldId, extensionGroup));
 }
 
 DOMWrapperWorld::DOMWrapperWorld(int worldId, int extensionGroup)
     : m_worldId(worldId)
     , m_extensionGroup(extensionGroup)
 {
-    if (isIsolatedWorld())
+    if (isMainWorld())
+        m_domDataStore = adoptPtr(new DOMDataStore(MainWorld));
+    else if (isIsolatedWorld())
         m_domDataStore = adoptPtr(new DOMDataStore(IsolatedWorld));
+    else if (isWorkerWorld())
+        m_domDataStore = adoptPtr(new DOMDataStore(WorkerWorld));
 }
 
-DOMWrapperWorld* DOMWrapperWorld::current()
+DOMWrapperWorld* DOMWrapperWorld::current(v8::Isolate* isolate)
 {
-    v8::Isolate* isolate = v8::Isolate::GetCurrent();
-    ASSERT(isolate->InContext());
     v8::Handle<v8::Context> context = isolate->GetCurrentContext();
-    if (!V8DOMWrapper::isWrapperOfType(toInnerGlobalObject(context), &V8Window::wrapperTypeInfo))
+    if (context.IsEmpty() || !toDOMWindow(context))
         return 0;
-    ASSERT(isMainThread());
-    if (DOMWrapperWorld* world = isolatedWorld(context))
-        return world;
-    return mainThreadNormalWorld();
-}
-
-DOMWrapperWorld* mainThreadNormalWorld()
-{
-    ASSERT(isMainThread());
-    DEFINE_STATIC_REF(DOMWrapperWorld, cachedNormalWorld, (DOMWrapperWorld::createMainWorld()));
-    return cachedNormalWorld;
+    return world(context);
 }
 
-bool DOMWrapperWorld::contextHasCorrectPrototype(v8::Handle<v8::Context> context)
+DOMWrapperWorld* DOMWrapperWorld::mainWorld()
 {
     ASSERT(isMainThread());
-    if (initializingWindow)
-        return true;
-    return V8DOMWrapper::isWrapperOfType(toInnerGlobalObject(context), &V8Window::wrapperTypeInfo);
-}
-
-void DOMWrapperWorld::setIsolatedWorldField(v8::Handle<v8::Context> context)
-{
-    V8PerContextDataHolder::from(context)->setIsolatedWorld(isMainWorld() ? 0 : this);
+    DEFINE_STATIC_REF(DOMWrapperWorld, cachedMainWorld, (DOMWrapperWorld::create(MainWorldId, mainWorldExtensionGroup)));
+    return cachedMainWorld;
 }
 
 typedef HashMap<int, DOMWrapperWorld*> WorldMap;
@@ -108,9 +88,10 @@ static WorldMap& isolatedWorldMap()
     return map;
 }
 
-void DOMWrapperWorld::getAllWorlds(Vector<RefPtr<DOMWrapperWorld> >& worlds)
+void DOMWrapperWorld::getAllWorldsInMainThread(Vector<RefPtr<DOMWrapperWorld> >& worlds)
 {
-    worlds.append(mainThreadNormalWorld());
+    ASSERT(isMainThread());
+    worlds.append(mainWorld());
     WorldMap& isolatedWorlds = isolatedWorldMap();
     for (WorldMap::iterator it = isolatedWorlds.begin(); it != isolatedWorlds.end(); ++it)
         worlds.append(it->value);
@@ -136,32 +117,34 @@ DOMWrapperWorld::~DOMWrapperWorld()
     ASSERT(map.size() == isolatedWorldCount);
 }
 
+#ifndef NDEBUG
+static bool isIsolatedWorldId(int worldId)
+{
+    return worldId != MainWorldId && worldId != WorkerWorldId;
+}
+#endif
+
 PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::ensureIsolatedWorld(int worldId, int extensionGroup)
 {
-    ASSERT(worldId > MainWorldId);
+    ASSERT(isIsolatedWorldId(worldId));
 
     WorldMap& map = isolatedWorldMap();
     WorldMap::AddResult result = map.add(worldId, 0);
-    RefPtr<DOMWrapperWorld> world = result.iterator->value;
+    RefPtr<DOMWrapperWorld> world = result.storedValue->value;
     if (world) {
         ASSERT(world->worldId() == worldId);
         ASSERT(world->extensionGroup() == extensionGroup);
         return world.release();
     }
 
-    world = adoptRef(new DOMWrapperWorld(worldId, extensionGroup));
-    result.iterator->value = world.get();
+    world = DOMWrapperWorld::create(worldId, extensionGroup);
+    result.storedValue->value = world.get();
     isolatedWorldCount++;
     ASSERT(map.size() == isolatedWorldCount);
 
     return world.release();
 }
 
-v8::Handle<v8::Context> DOMWrapperWorld::context(ScriptController& controller)
-{
-    return controller.windowShell(this)->context();
-}
-
 typedef HashMap<int, RefPtr<SecurityOrigin> > IsolatedWorldSecurityOriginMap;
 static IsolatedWorldSecurityOriginMap& isolatedWorldSecurityOrigins()
 {
@@ -178,19 +161,19 @@ SecurityOrigin* DOMWrapperWorld::isolatedWorldSecurityOrigin()
     return it == origins.end() ? 0 : it->value.get();
 }
 
-void DOMWrapperWorld::setIsolatedWorldSecurityOrigin(int worldID, PassRefPtr<SecurityOrigin> securityOrigin)
+void DOMWrapperWorld::setIsolatedWorldSecurityOrigin(int worldId, PassRefPtr<SecurityOrigin> securityOrigin)
 {
-    ASSERT(DOMWrapperWorld::isIsolatedWorldId(worldID));
+    ASSERT(isIsolatedWorldId(worldId));
     if (securityOrigin)
-        isolatedWorldSecurityOrigins().set(worldID, securityOrigin);
+        isolatedWorldSecurityOrigins().set(worldId, securityOrigin);
     else
-        isolatedWorldSecurityOrigins().remove(worldID);
+        isolatedWorldSecurityOrigins().remove(worldId);
 }
 
-void DOMWrapperWorld::clearIsolatedWorldSecurityOrigin(int worldID)
+void DOMWrapperWorld::clearIsolatedWorldSecurityOrigin(int worldId)
 {
-    ASSERT(DOMWrapperWorld::isIsolatedWorldId(worldID));
-    isolatedWorldSecurityOrigins().remove(worldID);
+    ASSERT(isIsolatedWorldId(worldId));
+    isolatedWorldSecurityOrigins().remove(worldId);
 }
 
 typedef HashMap<int, bool> IsolatedWorldContentSecurityPolicyMap;
@@ -209,19 +192,19 @@ bool DOMWrapperWorld::isolatedWorldHasContentSecurityPolicy()
     return it == policies.end() ? false : it->value;
 }
 
-void DOMWrapperWorld::setIsolatedWorldContentSecurityPolicy(int worldID, const String& policy)
+void DOMWrapperWorld::setIsolatedWorldContentSecurityPolicy(int worldId, const String& policy)
 {
-    ASSERT(DOMWrapperWorld::isIsolatedWorldId(worldID));
+    ASSERT(isIsolatedWorldId(worldId));
     if (!policy.isEmpty())
-        isolatedWorldContentSecurityPolicies().set(worldID, true);
+        isolatedWorldContentSecurityPolicies().set(worldId, true);
     else
-        isolatedWorldContentSecurityPolicies().remove(worldID);
+        isolatedWorldContentSecurityPolicies().remove(worldId);
 }
 
-void DOMWrapperWorld::clearIsolatedWorldContentSecurityPolicy(int worldID)
+void DOMWrapperWorld::clearIsolatedWorldContentSecurityPolicy(int worldId)
 {
-    ASSERT(DOMWrapperWorld::isIsolatedWorldId(worldID));
-    isolatedWorldContentSecurityPolicies().remove(worldID);
+    ASSERT(isIsolatedWorldId(worldId));
+    isolatedWorldContentSecurityPolicies().remove(worldId);
 }
 
 typedef HashMap<int, OwnPtr<V8DOMActivityLogger>, WTF::IntHash<int>, WTF::UnsignedWithZeroKeyHashTraits<int> > DOMActivityLoggerMap;
@@ -244,4 +227,9 @@ V8DOMActivityLogger* DOMWrapperWorld::activityLogger(int worldId)
     return it == loggers.end() ? 0 : it->value.get();
 }
 
+bool DOMWrapperWorld::contextHasCorrectPrototype(v8::Handle<v8::Context> context)
+{
+    return V8WindowShell::contextHasCorrectPrototype(context);
+}
+
 } // namespace WebCore