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
29using namespace JSC;
30
31namespace WebCore {
32
33DOMWrapperWorld::DOMWrapperWorld(JSC::VM& vm, bool isNormal)
34    : m_vm(vm)
35    , m_isNormal(isNormal)
36{
37    VM::ClientData* clientData = m_vm.clientData;
38    ASSERT(clientData);
39    static_cast<WebCoreJSClientData*>(clientData)->rememberWorld(*this);
40}
41
42DOMWrapperWorld::~DOMWrapperWorld()
43{
44    VM::ClientData* clientData = m_vm.clientData;
45    ASSERT(clientData);
46    static_cast<WebCoreJSClientData*>(clientData)->forgetWorld(*this);
47
48    // These items are created lazily.
49    while (!m_scriptControllersWithWindowShells.isEmpty())
50        (*m_scriptControllersWithWindowShells.begin())->destroyWindowShell(*this);
51}
52
53void DOMWrapperWorld::clearWrappers()
54{
55    m_wrappers.clear();
56
57    // These items are created lazily.
58    while (!m_scriptControllersWithWindowShells.isEmpty())
59        (*m_scriptControllersWithWindowShells.begin())->destroyWindowShell(*this);
60}
61
62DOMWrapperWorld& normalWorld(JSC::VM& vm)
63{
64    VM::ClientData* clientData = vm.clientData;
65    ASSERT(clientData);
66    return static_cast<WebCoreJSClientData*>(clientData)->normalWorld();
67}
68
69DOMWrapperWorld& mainThreadNormalWorld()
70{
71    ASSERT(isMainThread());
72    static DOMWrapperWorld& cachedNormalWorld = normalWorld(JSDOMWindow::commonVM());
73    return cachedNormalWorld;
74}
75
76} // namespace WebCore
77