1 /*
2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Samuel Weinig <sam@webkit.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "config.h"
22 #include "DOMWrapperWorld.h"
23
24 #include "JSDOMWindow.h"
25 #include "ScriptController.h"
26 #include "WebCoreJSClientData.h"
27
28 using namespace JSC;
29
30 namespace WebCore {
31
finalize(JSC::Handle<JSC::Unknown> handle,void * context)32 void JSDOMWrapperOwner::finalize(JSC::Handle<JSC::Unknown> handle, void* context)
33 {
34 JSDOMWrapper* wrapper = static_cast<JSDOMWrapper*>(handle.get().asCell());
35 void* domObject = context;
36 uncacheWrapper(m_world, domObject, wrapper);
37 }
38
finalize(JSC::Handle<JSC::Unknown> handle,void * context)39 void JSStringOwner::finalize(JSC::Handle<JSC::Unknown> handle, void* context)
40 {
41 JSString* jsString = static_cast<JSString*>(handle.get().asCell());
42 StringImpl* stringImpl = static_cast<StringImpl*>(context);
43 ASSERT_UNUSED(jsString, m_world->m_stringCache.find(stringImpl)->second.get() == jsString);
44 m_world->m_stringCache.remove(stringImpl);
45 }
46
DOMWrapperWorld(JSC::JSGlobalData * globalData,bool isNormal)47 DOMWrapperWorld::DOMWrapperWorld(JSC::JSGlobalData* globalData, bool isNormal)
48 : m_globalData(globalData)
49 , m_isNormal(isNormal)
50 , m_defaultWrapperOwner(this)
51 , m_stringWrapperOwner(this)
52 {
53 JSGlobalData::ClientData* clientData = m_globalData->clientData;
54 ASSERT(clientData);
55 static_cast<WebCoreJSClientData*>(clientData)->rememberWorld(this);
56 }
57
~DOMWrapperWorld()58 DOMWrapperWorld::~DOMWrapperWorld()
59 {
60 JSGlobalData::ClientData* clientData = m_globalData->clientData;
61 ASSERT(clientData);
62 static_cast<WebCoreJSClientData*>(clientData)->forgetWorld(this);
63
64 // These items are created lazily.
65 while (!m_scriptControllersWithWindowShells.isEmpty())
66 (*m_scriptControllersWithWindowShells.begin())->destroyWindowShell(this);
67 }
68
clearWrappers()69 void DOMWrapperWorld::clearWrappers()
70 {
71 m_wrappers.clear();
72 m_stringCache.clear();
73
74 // These items are created lazily.
75 while (!m_scriptControllersWithWindowShells.isEmpty())
76 (*m_scriptControllersWithWindowShells.begin())->destroyWindowShell(this);
77 }
78
normalWorld(JSC::JSGlobalData & globalData)79 DOMWrapperWorld* normalWorld(JSC::JSGlobalData& globalData)
80 {
81 JSGlobalData::ClientData* clientData = globalData.clientData;
82 ASSERT(clientData);
83 return static_cast<WebCoreJSClientData*>(clientData)->normalWorld();
84 }
85
mainThreadNormalWorld()86 DOMWrapperWorld* mainThreadNormalWorld()
87 {
88 ASSERT(isMainThread());
89 static DOMWrapperWorld* cachedNormalWorld = normalWorld(*JSDOMWindow::commonJSGlobalData());
90 return cachedNormalWorld;
91 }
92
93 } // namespace WebCore
94