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 | #pragma once |
23 | |
24 | #include "JSDOMGlobalObject.h" |
25 | #include <wtf/Forward.h> |
26 | |
27 | namespace WebCore { |
28 | |
29 | class WindowProxy; |
30 | |
31 | typedef HashMap<void*, JSC::Weak<JSC::JSObject>> DOMObjectWrapperMap; |
32 | |
33 | class DOMWrapperWorld : public RefCounted<DOMWrapperWorld> { |
34 | public: |
35 | static Ref<DOMWrapperWorld> create(JSC::VM& vm, bool isNormal = false) |
36 | { |
37 | return adoptRef(*new DOMWrapperWorld(vm, isNormal)); |
38 | } |
39 | WEBCORE_EXPORT ~DOMWrapperWorld(); |
40 | |
41 | // Free as much memory held onto by this world as possible. |
42 | WEBCORE_EXPORT void clearWrappers(); |
43 | |
44 | void didCreateWindowProxy(WindowProxy* controller) { m_jsWindowProxies.add(controller); } |
45 | void didDestroyWindowProxy(WindowProxy* controller) { m_jsWindowProxies.remove(controller); } |
46 | |
47 | void setShadowRootIsAlwaysOpen() { m_shadowRootIsAlwaysOpen = true; } |
48 | bool shadowRootIsAlwaysOpen() const { return m_shadowRootIsAlwaysOpen; } |
49 | |
50 | void disableOverrideBuiltinsBehavior() { m_shouldDisableOverrideBuiltinsBehavior = true; } |
51 | bool shouldDisableOverrideBuiltinsBehavior() const { return m_shouldDisableOverrideBuiltinsBehavior; } |
52 | |
53 | DOMObjectWrapperMap& wrappers() { return m_wrappers; } |
54 | |
55 | bool isNormal() const { return m_isNormal; } |
56 | |
57 | JSC::VM& vm() const { return m_vm; } |
58 | |
59 | protected: |
60 | DOMWrapperWorld(JSC::VM&, bool isNormal); |
61 | |
62 | private: |
63 | JSC::VM& m_vm; |
64 | HashSet<WindowProxy*> m_jsWindowProxies; |
65 | DOMObjectWrapperMap m_wrappers; |
66 | |
67 | bool m_isNormal; |
68 | bool m_shadowRootIsAlwaysOpen { false }; |
69 | bool m_shouldDisableOverrideBuiltinsBehavior { false }; |
70 | }; |
71 | |
72 | DOMWrapperWorld& normalWorld(JSC::VM&); |
73 | WEBCORE_EXPORT DOMWrapperWorld& mainThreadNormalWorld(); |
74 | |
75 | inline DOMWrapperWorld& debuggerWorld() { return mainThreadNormalWorld(); } |
76 | inline DOMWrapperWorld& pluginWorld() { return mainThreadNormalWorld(); } |
77 | |
78 | DOMWrapperWorld& currentWorld(JSC::ExecState&); |
79 | DOMWrapperWorld& worldForDOMObject(JSC::JSObject&); |
80 | |
81 | // Helper function for code paths that must not share objects across isolated DOM worlds. |
82 | bool isWorldCompatible(JSC::ExecState&, JSC::JSValue); |
83 | |
84 | inline DOMWrapperWorld& currentWorld(JSC::ExecState& state) |
85 | { |
86 | return JSC::jsCast<JSDOMGlobalObject*>(state.lexicalGlobalObject())->world(); |
87 | } |
88 | |
89 | inline DOMWrapperWorld& worldForDOMObject(JSC::JSObject& object) |
90 | { |
91 | return JSC::jsCast<JSDOMGlobalObject*>(object.globalObject())->world(); |
92 | } |
93 | |
94 | inline bool isWorldCompatible(JSC::ExecState& state, JSC::JSValue value) |
95 | { |
96 | return !value.isObject() || &worldForDOMObject(*value.getObject()) == ¤tWorld(state); |
97 | } |
98 | |
99 | } // namespace WebCore |
100 | |