2 * Copyright (C) 2009 Apple 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
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "WebKitDLL.h"
27 #include "WebScriptWorld.h"
29 #include <JavaScriptCore/APICast.h>
30 #include <WebCore/JSDOMBinding.h>
31 #include <WebCore/ScriptController.h>
33 using namespace WebCore;
35 typedef HashMap<DOMWrapperWorld*, WebScriptWorld*> WorldMap;
36 static WorldMap& allWorlds()
38 static WorldMap& map = *new WorldMap;
42 inline WebScriptWorld::WebScriptWorld(PassRefPtr<DOMWrapperWorld> world)
46 ASSERT_ARG(world, m_world);
48 ASSERT_ARG(world, !allWorlds().contains(m_world.get()));
49 allWorlds().add(m_world.get(), this);
52 gClassNameCount.add("WebScriptWorld");
55 WebScriptWorld::~WebScriptWorld()
57 ASSERT(allWorlds().contains(m_world.get()));
58 allWorlds().remove(m_world.get());
61 gClassNameCount.remove("WebScriptWorld");
64 WebScriptWorld* WebScriptWorld::standardWorld()
66 static WebScriptWorld* standardWorld = createInstance(mainThreadNormalWorld()).leakRef();
70 COMPtr<WebScriptWorld> WebScriptWorld::createInstance()
72 return createInstance(ScriptController::createWorld());
75 COMPtr<WebScriptWorld> WebScriptWorld::createInstance(PassRefPtr<DOMWrapperWorld> world)
77 return new WebScriptWorld(world);
80 COMPtr<WebScriptWorld> WebScriptWorld::findOrCreateWorld(DOMWrapperWorld* world)
82 if (world == mainThreadNormalWorld())
83 return standardWorld();
85 if (WebScriptWorld* existingWorld = allWorlds().get(world))
88 return createInstance(world);
91 ULONG WebScriptWorld::AddRef()
96 ULONG WebScriptWorld::Release()
98 ULONG newRefCount = --m_refCount;
104 HRESULT WebScriptWorld::QueryInterface(REFIID riid, void** ppvObject)
110 if (IsEqualIID(riid, __uuidof(WebScriptWorld)))
112 else if (IsEqualIID(riid, __uuidof(IWebScriptWorld)))
113 *ppvObject = static_cast<IWebScriptWorld*>(this);
114 else if (IsEqualIID(riid, IID_IUnknown))
115 *ppvObject = static_cast<IUnknown*>(this);
117 return E_NOINTERFACE;
123 HRESULT WebScriptWorld::standardWorld(IWebScriptWorld** outWorld)
128 *outWorld = standardWorld();
129 (*outWorld)->AddRef();
133 HRESULT WebScriptWorld::scriptWorldForGlobalContext(JSGlobalContextRef context, IWebScriptWorld** outWorld)
137 return findOrCreateWorld(currentWorld(toJS(context))).copyRefTo(outWorld);
140 HRESULT WebScriptWorld::unregisterWorld()
142 m_world->clearWrappers();