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 CSSValue;
33 class JSDOMWrapper;
34 class ScriptController;
35 
36 typedef HashMap<void*, JSC::Weak<JSDOMWrapper> > DOMObjectWrapperMap;
37 typedef HashMap<StringImpl*, JSC::Weak<JSC::JSString> > JSStringCache;
38 
39 class JSDOMWrapperOwner : public JSC::WeakHandleOwner {
40 public:
41     JSDOMWrapperOwner(DOMWrapperWorld*);
42     virtual void finalize(JSC::Handle<JSC::Unknown>, void* context);
43 
44 private:
45     DOMWrapperWorld* m_world;
46 };
47 
JSDOMWrapperOwner(DOMWrapperWorld * world)48 inline JSDOMWrapperOwner::JSDOMWrapperOwner(DOMWrapperWorld* world)
49     : m_world(world)
50 {
51 }
52 
53 class JSStringOwner : public JSC::WeakHandleOwner {
54 public:
55     JSStringOwner(DOMWrapperWorld*);
56     virtual void finalize(JSC::Handle<JSC::Unknown>, void* context);
57 
58 private:
59     DOMWrapperWorld* m_world;
60 };
61 
JSStringOwner(DOMWrapperWorld * world)62 inline JSStringOwner::JSStringOwner(DOMWrapperWorld* world)
63     : m_world(world)
64 {
65 }
66 
67 class DOMWrapperWorld : public RefCounted<DOMWrapperWorld> {
68 public:
69     static PassRefPtr<DOMWrapperWorld> create(JSC::JSGlobalData* globalData, bool isNormal = false)
70     {
71         return adoptRef(new DOMWrapperWorld(globalData, isNormal));
72     }
73     ~DOMWrapperWorld();
74 
75     // Free as much memory held onto by this world as possible.
76     void clearWrappers();
77 
didCreateWindowShell(ScriptController * scriptController)78     void didCreateWindowShell(ScriptController* scriptController) { m_scriptControllersWithWindowShells.add(scriptController); }
didDestroyWindowShell(ScriptController * scriptController)79     void didDestroyWindowShell(ScriptController* scriptController) { m_scriptControllersWithWindowShells.remove(scriptController); }
80 
81     // FIXME: can we make this private?
82     DOMObjectWrapperMap m_wrappers;
83     JSStringCache m_stringCache;
84     HashMap<CSSValue*, void*> m_cssValueRoots;
85 
isNormal()86     bool isNormal() const { return m_isNormal; }
87 
globalData()88     JSC::JSGlobalData* globalData() const { return m_globalData; }
defaultWrapperOwner()89     JSDOMWrapperOwner* defaultWrapperOwner() { return &m_defaultWrapperOwner; }
stringWrapperOwner()90     JSStringOwner* stringWrapperOwner() { return &m_stringWrapperOwner; }
91 
92 protected:
93     DOMWrapperWorld(JSC::JSGlobalData*, bool isNormal);
94 
95 private:
96     JSC::JSGlobalData* m_globalData;
97     HashSet<ScriptController*> m_scriptControllersWithWindowShells;
98     bool m_isNormal;
99     JSDOMWrapperOwner m_defaultWrapperOwner;
100     JSStringOwner m_stringWrapperOwner;
101 };
102 
103 DOMWrapperWorld* normalWorld(JSC::JSGlobalData&);
104 DOMWrapperWorld* mainThreadNormalWorld();
debuggerWorld()105 inline DOMWrapperWorld* debuggerWorld() { return mainThreadNormalWorld(); }
pluginWorld()106 inline DOMWrapperWorld* pluginWorld() { return mainThreadNormalWorld(); }
107 
currentWorld(JSC::ExecState * exec)108 inline DOMWrapperWorld* currentWorld(JSC::ExecState* exec)
109 {
110     return static_cast<JSDOMGlobalObject*>(exec->lexicalGlobalObject())->world();
111 }
112 
113 } // namespace WebCore
114 
115 #endif // DOMWrapperWorld_h
116