1 /*
2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Samuel Weinig <sam@webkit.org>
5 * Copyright (C) 2009 Google, Inc. All rights reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #ifndef DOMWrapperWorld_h
23 #define DOMWrapperWorld_h
24
25 #include "JSDOMGlobalObject.h"
26 #include <heap/Weak.h>
27 #include <runtime/WeakGCMap.h>
28 #include <wtf/Forward.h>
29
30 namespace WebCore {
31
32 class JSDOMWrapper;
33 class ScriptController;
34
35 typedef HashMap<void*, JSC::Weak<JSDOMWrapper> > DOMObjectWrapperMap;
36 typedef JSC::WeakGCMap<StringImpl*, JSC::JSString> JSStringCache;
37
38 class JSDOMWrapperOwner : public JSC::WeakHandleOwner {
39 public:
40 JSDOMWrapperOwner(DOMWrapperWorld*);
41 virtual void finalize(JSC::Handle<JSC::Unknown>, void* context);
42
43 private:
44 DOMWrapperWorld* m_world;
45 };
46
JSDOMWrapperOwner(DOMWrapperWorld * world)47 inline JSDOMWrapperOwner::JSDOMWrapperOwner(DOMWrapperWorld* world)
48 : m_world(world)
49 {
50 }
51
52 class DOMWrapperWorld : public RefCounted<DOMWrapperWorld> {
53 public:
54 static PassRefPtr<DOMWrapperWorld> create(JSC::JSGlobalData* globalData, bool isNormal = false)
55 {
56 return adoptRef(new DOMWrapperWorld(globalData, isNormal));
57 }
58 ~DOMWrapperWorld();
59
60 // Free as much memory held onto by this world as possible.
61 void clearWrappers();
62
didCreateWindowShell(ScriptController * scriptController)63 void didCreateWindowShell(ScriptController* scriptController) { m_scriptControllersWithWindowShells.add(scriptController); }
didDestroyWindowShell(ScriptController * scriptController)64 void didDestroyWindowShell(ScriptController* scriptController) { m_scriptControllersWithWindowShells.remove(scriptController); }
65
66 // FIXME: can we make this private?
67 DOMObjectWrapperMap m_wrappers;
68 JSStringCache m_stringCache;
69
isNormal()70 bool isNormal() const { return m_isNormal; }
71
globalData()72 JSC::JSGlobalData* globalData() const { return m_globalData; }
defaultWrapperOwner()73 JSDOMWrapperOwner* defaultWrapperOwner() { return &m_defaultWrapperOwner; }
74
75 protected:
76 DOMWrapperWorld(JSC::JSGlobalData*, bool isNormal);
77
78 private:
79 JSC::JSGlobalData* m_globalData;
80 HashSet<ScriptController*> m_scriptControllersWithWindowShells;
81 bool m_isNormal;
82 JSDOMWrapperOwner m_defaultWrapperOwner;
83 };
84
85 DOMWrapperWorld* normalWorld(JSC::JSGlobalData&);
86 DOMWrapperWorld* mainThreadNormalWorld();
debuggerWorld()87 inline DOMWrapperWorld* debuggerWorld() { return mainThreadNormalWorld(); }
pluginWorld()88 inline DOMWrapperWorld* pluginWorld() { return mainThreadNormalWorld(); }
89
currentWorld(JSC::ExecState * exec)90 inline DOMWrapperWorld* currentWorld(JSC::ExecState* exec)
91 {
92 return static_cast<JSDOMGlobalObject*>(exec->lexicalGlobalObject())->world();
93 }
94
95 } // namespace WebCore
96
97 #endif // DOMWrapperWorld_h
98