tizen beta release
[profile/ivi/webkit-efl.git] / 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 JSStringOwner::finalize(JSC::Handle<JSC::Unknown> handle, void* context)
34 {
35     JSString* jsString = static_cast<JSString*>(handle.get().asCell());
36     StringImpl* stringImpl = static_cast<StringImpl*>(context);
37     ASSERT_UNUSED(jsString, m_world->m_stringCache.find(stringImpl)->second.get() == jsString);
38     m_world->m_stringCache.remove(stringImpl);
39 }
40
41 DOMWrapperWorld::DOMWrapperWorld(JSC::JSGlobalData* globalData, bool isNormal)
42     : m_globalData(globalData)
43     , m_isNormal(isNormal)
44     , m_stringWrapperOwner(this)
45 {
46     JSGlobalData::ClientData* clientData = m_globalData->clientData;
47     ASSERT(clientData);
48     static_cast<WebCoreJSClientData*>(clientData)->rememberWorld(this);
49 }
50
51 DOMWrapperWorld::~DOMWrapperWorld()
52 {
53     JSGlobalData::ClientData* clientData = m_globalData->clientData;
54     ASSERT(clientData);
55     static_cast<WebCoreJSClientData*>(clientData)->forgetWorld(this);
56
57     // These items are created lazily.
58     while (!m_scriptControllersWithWindowShells.isEmpty())
59         (*m_scriptControllersWithWindowShells.begin())->destroyWindowShell(this);
60 }
61
62 void DOMWrapperWorld::clearWrappers()
63 {
64     m_wrappers.clear();
65     m_stringCache.clear();
66
67     // These items are created lazily.
68     while (!m_scriptControllersWithWindowShells.isEmpty())
69         (*m_scriptControllersWithWindowShells.begin())->destroyWindowShell(this);
70 }
71
72 DOMWrapperWorld* normalWorld(JSC::JSGlobalData& globalData)
73 {
74     JSGlobalData::ClientData* clientData = globalData.clientData;
75     ASSERT(clientData);
76     return static_cast<WebCoreJSClientData*>(clientData)->normalWorld();
77 }
78
79 DOMWrapperWorld* mainThreadNormalWorld()
80 {
81     ASSERT(isMainThread());
82     static DOMWrapperWorld* cachedNormalWorld = normalWorld(*JSDOMWindow::commonJSGlobalData());
83     return cachedNormalWorld;
84 }
85
86 } // namespace WebCore