initial import
[vuplus_webkit] / Source / WebCore / bindings / js / DOMWrapperWorld.cpp
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 #include <wtf/MainThread.h>
28
29 using namespace JSC;
30
31 namespace WebCore {
32
33 void JSDOMWrapperOwner::finalize(JSC::Handle<JSC::Unknown> handle, void* context)
34 {
35     JSDOMWrapper* wrapper = static_cast<JSDOMWrapper*>(handle.get().asCell());
36     void* domObject = context;
37     uncacheWrapper(m_world, domObject, wrapper);
38 }
39
40 void JSStringOwner::finalize(JSC::Handle<JSC::Unknown> handle, void* context)
41 {
42     JSString* jsString = static_cast<JSString*>(handle.get().asCell());
43     StringImpl* stringImpl = static_cast<StringImpl*>(context);
44     ASSERT_UNUSED(jsString, m_world->m_stringCache.find(stringImpl)->second.get() == jsString);
45     m_world->m_stringCache.remove(stringImpl);
46 }
47
48 DOMWrapperWorld::DOMWrapperWorld(JSC::JSGlobalData* globalData, bool isNormal)
49     : m_globalData(globalData)
50     , m_isNormal(isNormal)
51     , m_defaultWrapperOwner(this)
52     , m_stringWrapperOwner(this)
53 {
54     JSGlobalData::ClientData* clientData = m_globalData->clientData;
55     ASSERT(clientData);
56     static_cast<WebCoreJSClientData*>(clientData)->rememberWorld(this);
57 }
58
59 DOMWrapperWorld::~DOMWrapperWorld()
60 {
61     JSGlobalData::ClientData* clientData = m_globalData->clientData;
62     ASSERT(clientData);
63     static_cast<WebCoreJSClientData*>(clientData)->forgetWorld(this);
64
65     // These items are created lazily.
66     while (!m_scriptControllersWithWindowShells.isEmpty())
67         (*m_scriptControllersWithWindowShells.begin())->destroyWindowShell(this);
68 }
69
70 void DOMWrapperWorld::clearWrappers()
71 {
72     m_wrappers.clear();
73     m_stringCache.clear();
74
75     // These items are created lazily.
76     while (!m_scriptControllersWithWindowShells.isEmpty())
77         (*m_scriptControllersWithWindowShells.begin())->destroyWindowShell(this);
78 }
79
80 DOMWrapperWorld* normalWorld(JSC::JSGlobalData& globalData)
81 {
82     JSGlobalData::ClientData* clientData = globalData.clientData;
83     ASSERT(clientData);
84     return static_cast<WebCoreJSClientData*>(clientData)->normalWorld();
85 }
86
87 DOMWrapperWorld* mainThreadNormalWorld()
88 {
89     ASSERT(isMainThread());
90     static DOMWrapperWorld* cachedNormalWorld = normalWorld(*JSDOMWindow::commonJSGlobalData());
91     return cachedNormalWorld;
92 }
93
94 } // namespace WebCore