OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
30 | |
31 #include "config.h" | |
32 #include "bindings/v8/DOMWrapperWorld.h" | |
33 | |
34 #include "bindings/core/v8/V8Window.h" | |
35 #include "bindings/v8/DOMDataStore.h" | |
36 #include "bindings/v8/ScriptController.h" | |
37 #include "bindings/v8/V8Binding.h" | |
38 #include "bindings/v8/V8DOMActivityLogger.h" | |
39 #include "bindings/v8/V8DOMWrapper.h" | |
40 #include "bindings/v8/V8WindowShell.h" | |
41 #include "bindings/v8/WrapperTypeInfo.h" | |
42 #include "core/dom/ExecutionContext.h" | |
43 #include "wtf/HashTraits.h" | |
44 #include "wtf/StdLibExtras.h" | |
45 | |
46 namespace WebCore { | |
47 | |
48 unsigned DOMWrapperWorld::isolatedWorldCount = 0; | |
49 DOMWrapperWorld* DOMWrapperWorld::worldOfInitializingWindow = 0; | |
50 | |
51 PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::create(int worldId, int extensionGr
oup) | |
52 { | |
53 return adoptRef(new DOMWrapperWorld(worldId, extensionGroup)); | |
54 } | |
55 | |
56 DOMWrapperWorld::DOMWrapperWorld(int worldId, int extensionGroup) | |
57 : m_worldId(worldId) | |
58 , m_extensionGroup(extensionGroup) | |
59 , m_domDataStore(adoptPtr(new DOMDataStore(isMainWorld()))) | |
60 { | |
61 } | |
62 | |
63 DOMWrapperWorld& DOMWrapperWorld::mainWorld() | |
64 { | |
65 ASSERT(isMainThread()); | |
66 DEFINE_STATIC_REF(DOMWrapperWorld, cachedMainWorld, (DOMWrapperWorld::create
(MainWorldId, mainWorldExtensionGroup))); | |
67 return *cachedMainWorld; | |
68 } | |
69 | |
70 DOMWrapperWorld& DOMWrapperWorld::privateScriptIsolatedWorld() | |
71 { | |
72 ASSERT(isMainThread()); | |
73 DEFINE_STATIC_LOCAL(RefPtr<DOMWrapperWorld>, cachedPrivateScriptIsolatedWorl
d, ()); | |
74 if (!cachedPrivateScriptIsolatedWorld) { | |
75 cachedPrivateScriptIsolatedWorld = DOMWrapperWorld::create(PrivateScript
IsolatedWorldId, privateScriptIsolatedWorldExtensionGroup); | |
76 isolatedWorldCount++; | |
77 } | |
78 return *cachedPrivateScriptIsolatedWorld; | |
79 } | |
80 | |
81 typedef HashMap<int, DOMWrapperWorld*> WorldMap; | |
82 static WorldMap& isolatedWorldMap() | |
83 { | |
84 ASSERT(isMainThread()); | |
85 DEFINE_STATIC_LOCAL(WorldMap, map, ()); | |
86 return map; | |
87 } | |
88 | |
89 void DOMWrapperWorld::allWorldsInMainThread(Vector<RefPtr<DOMWrapperWorld> >& wo
rlds) | |
90 { | |
91 ASSERT(isMainThread()); | |
92 worlds.append(&mainWorld()); | |
93 WorldMap& isolatedWorlds = isolatedWorldMap(); | |
94 for (WorldMap::iterator it = isolatedWorlds.begin(); it != isolatedWorlds.en
d(); ++it) | |
95 worlds.append(it->value); | |
96 } | |
97 | |
98 DOMWrapperWorld::~DOMWrapperWorld() | |
99 { | |
100 ASSERT(!isMainWorld()); | |
101 | |
102 dispose(); | |
103 | |
104 if (!isIsolatedWorld()) | |
105 return; | |
106 | |
107 WorldMap& map = isolatedWorldMap(); | |
108 WorldMap::iterator it = map.find(m_worldId); | |
109 if (it == map.end()) { | |
110 ASSERT_NOT_REACHED(); | |
111 return; | |
112 } | |
113 ASSERT(it->value == this); | |
114 | |
115 map.remove(it); | |
116 isolatedWorldCount--; | |
117 ASSERT(map.size() == isolatedWorldCount); | |
118 } | |
119 | |
120 void DOMWrapperWorld::dispose() | |
121 { | |
122 m_domDataStore.clear(); | |
123 } | |
124 | |
125 #ifndef NDEBUG | |
126 static bool isIsolatedWorldId(int worldId) | |
127 { | |
128 return MainWorldId < worldId && worldId < IsolatedWorldIdLimit; | |
129 } | |
130 #endif | |
131 | |
132 PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::ensureIsolatedWorld(int worldId, in
t extensionGroup) | |
133 { | |
134 ASSERT(isIsolatedWorldId(worldId)); | |
135 | |
136 WorldMap& map = isolatedWorldMap(); | |
137 WorldMap::AddResult result = map.add(worldId, 0); | |
138 RefPtr<DOMWrapperWorld> world = result.storedValue->value; | |
139 if (world) { | |
140 ASSERT(world->worldId() == worldId); | |
141 ASSERT(world->extensionGroup() == extensionGroup); | |
142 return world.release(); | |
143 } | |
144 | |
145 world = DOMWrapperWorld::create(worldId, extensionGroup); | |
146 result.storedValue->value = world.get(); | |
147 isolatedWorldCount++; | |
148 ASSERT(map.size() == isolatedWorldCount); | |
149 | |
150 return world.release(); | |
151 } | |
152 | |
153 typedef HashMap<int, RefPtr<SecurityOrigin> > IsolatedWorldSecurityOriginMap; | |
154 static IsolatedWorldSecurityOriginMap& isolatedWorldSecurityOrigins() | |
155 { | |
156 ASSERT(isMainThread()); | |
157 DEFINE_STATIC_LOCAL(IsolatedWorldSecurityOriginMap, map, ()); | |
158 return map; | |
159 } | |
160 | |
161 SecurityOrigin* DOMWrapperWorld::isolatedWorldSecurityOrigin() | |
162 { | |
163 ASSERT(this->isIsolatedWorld()); | |
164 IsolatedWorldSecurityOriginMap& origins = isolatedWorldSecurityOrigins(); | |
165 IsolatedWorldSecurityOriginMap::iterator it = origins.find(worldId()); | |
166 return it == origins.end() ? 0 : it->value.get(); | |
167 } | |
168 | |
169 void DOMWrapperWorld::setIsolatedWorldSecurityOrigin(int worldId, PassRefPtr<Sec
urityOrigin> securityOrigin) | |
170 { | |
171 ASSERT(isIsolatedWorldId(worldId)); | |
172 if (securityOrigin) | |
173 isolatedWorldSecurityOrigins().set(worldId, securityOrigin); | |
174 else | |
175 isolatedWorldSecurityOrigins().remove(worldId); | |
176 } | |
177 | |
178 typedef HashMap<int, bool> IsolatedWorldContentSecurityPolicyMap; | |
179 static IsolatedWorldContentSecurityPolicyMap& isolatedWorldContentSecurityPolici
es() | |
180 { | |
181 ASSERT(isMainThread()); | |
182 DEFINE_STATIC_LOCAL(IsolatedWorldContentSecurityPolicyMap, map, ()); | |
183 return map; | |
184 } | |
185 | |
186 bool DOMWrapperWorld::isolatedWorldHasContentSecurityPolicy() | |
187 { | |
188 ASSERT(this->isIsolatedWorld()); | |
189 IsolatedWorldContentSecurityPolicyMap& policies = isolatedWorldContentSecuri
tyPolicies(); | |
190 IsolatedWorldContentSecurityPolicyMap::iterator it = policies.find(worldId()
); | |
191 return it == policies.end() ? false : it->value; | |
192 } | |
193 | |
194 void DOMWrapperWorld::setIsolatedWorldContentSecurityPolicy(int worldId, const S
tring& policy) | |
195 { | |
196 ASSERT(isIsolatedWorldId(worldId)); | |
197 if (!policy.isEmpty()) | |
198 isolatedWorldContentSecurityPolicies().set(worldId, true); | |
199 else | |
200 isolatedWorldContentSecurityPolicies().remove(worldId); | |
201 } | |
202 | |
203 } // namespace WebCore | |
OLD | NEW |