OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/shared_worker/shared_worker_service_impl.h" | 5 #include "content/browser/shared_worker/shared_worker_service_impl.h" |
6 | 6 |
7 #include <algorithm> | |
8 #include <iterator> | |
9 #include <set> | |
10 #include <vector> | |
11 | |
12 #include "content/browser/renderer_host/render_process_host_impl.h" | |
7 #include "content/browser/shared_worker/shared_worker_host.h" | 13 #include "content/browser/shared_worker/shared_worker_host.h" |
8 #include "content/browser/shared_worker/shared_worker_instance.h" | 14 #include "content/browser/shared_worker/shared_worker_instance.h" |
9 #include "content/browser/shared_worker/shared_worker_message_filter.h" | 15 #include "content/browser/shared_worker/shared_worker_message_filter.h" |
10 #include "content/browser/worker_host/worker_document_set.h" | 16 #include "content/browser/worker_host/worker_document_set.h" |
11 #include "content/common/view_messages.h" | 17 #include "content/common/view_messages.h" |
12 #include "content/common/worker_messages.h" | 18 #include "content/common/worker_messages.h" |
13 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/worker_service_observer.h" | 20 #include "content/public/browser/worker_service_observer.h" |
15 | 21 |
16 namespace content { | 22 namespace content { |
23 namespace { | |
24 | |
25 void UpdateWorkerDependencyOnUI(const std::vector<int> added_ids, | |
26 const std::vector<int> removed_ids) { | |
kinuko
2014/03/10 04:25:43
nit: const -> const&
horo
2014/03/10 11:48:31
Done.
| |
27 for (size_t i = 0; i < added_ids.size(); ++i) { | |
28 RenderProcessHostImpl* render_process_host_impl = | |
29 static_cast<RenderProcessHostImpl*>( | |
30 RenderProcessHost::FromID(added_ids[i])); | |
31 if (!render_process_host_impl) | |
32 continue; | |
33 render_process_host_impl->IncrementWorkerRefCount(); | |
34 } | |
35 for (size_t i = 0; i < removed_ids.size(); ++i) { | |
36 RenderProcessHostImpl* render_process_host_impl = | |
37 static_cast<RenderProcessHostImpl*>( | |
38 RenderProcessHost::FromID(removed_ids[i])); | |
39 if (!render_process_host_impl) | |
40 continue; | |
41 render_process_host_impl->DecrementWorkerRefCount(); | |
42 } | |
43 } | |
44 | |
45 void UpdateWorkerDependency(const std::vector<int> added_ids, | |
46 const std::vector<int> removed_ids) { | |
47 BrowserThread::PostTask( | |
48 BrowserThread::UI, | |
49 FROM_HERE, | |
50 base::Bind(&UpdateWorkerDependencyOnUI, added_ids, removed_ids)); | |
51 } | |
52 | |
53 } // namespace | |
17 | 54 |
18 SharedWorkerServiceImpl* SharedWorkerServiceImpl::GetInstance() { | 55 SharedWorkerServiceImpl* SharedWorkerServiceImpl::GetInstance() { |
19 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
20 return Singleton<SharedWorkerServiceImpl>::get(); | 57 return Singleton<SharedWorkerServiceImpl>::get(); |
21 } | 58 } |
22 | 59 |
23 SharedWorkerServiceImpl::SharedWorkerServiceImpl() { | 60 SharedWorkerServiceImpl::SharedWorkerServiceImpl() |
24 } | 61 : update_worker_dependency_(UpdateWorkerDependency) {} |
25 | 62 |
26 SharedWorkerServiceImpl::~SharedWorkerServiceImpl() { | 63 SharedWorkerServiceImpl::~SharedWorkerServiceImpl() {} |
64 | |
65 void SharedWorkerServiceImpl::ResetForTesting() { | |
66 last_worker_depended_renderers_.clear(); | |
67 worker_hosts_.clear(); | |
68 observers_.Clear(); | |
69 update_worker_dependency_ = UpdateWorkerDependency; | |
27 } | 70 } |
28 | 71 |
29 bool SharedWorkerServiceImpl::TerminateWorker(int process_id, int route_id) { | 72 bool SharedWorkerServiceImpl::TerminateWorker(int process_id, int route_id) { |
30 SharedWorkerHost* host = | 73 SharedWorkerHost* host = |
31 worker_hosts_.get(std::make_pair(process_id, route_id)); | 74 worker_hosts_.get(std::make_pair(process_id, route_id)); |
32 if (!host || !host->instance()) | 75 if (!host || !host->instance()) |
33 return false; | 76 return false; |
34 host->TerminateWorker(); | 77 host->TerminateWorker(); |
35 return true; | 78 return true; |
36 } | 79 } |
(...skipping 29 matching lines...) Expand all Loading... | |
66 } | 109 } |
67 | 110 |
68 void SharedWorkerServiceImpl::CreateWorker( | 111 void SharedWorkerServiceImpl::CreateWorker( |
69 const ViewHostMsg_CreateWorker_Params& params, | 112 const ViewHostMsg_CreateWorker_Params& params, |
70 int route_id, | 113 int route_id, |
71 SharedWorkerMessageFilter* filter, | 114 SharedWorkerMessageFilter* filter, |
72 ResourceContext* resource_context, | 115 ResourceContext* resource_context, |
73 const WorkerStoragePartition& partition, | 116 const WorkerStoragePartition& partition, |
74 bool* url_mismatch) { | 117 bool* url_mismatch) { |
75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
119 ScopedWorkerDependencyChecker checker(this); | |
76 *url_mismatch = false; | 120 *url_mismatch = false; |
77 SharedWorkerInstance* existing_instance = | 121 SharedWorkerInstance* existing_instance = |
78 FindSharedWorkerInstance( | 122 FindSharedWorkerInstance( |
79 params.url, params.name, partition, resource_context); | 123 params.url, params.name, partition, resource_context); |
80 if (existing_instance) { | 124 if (existing_instance) { |
81 if (params.url != existing_instance->url()) { | 125 if (params.url != existing_instance->url()) { |
82 *url_mismatch = true; | 126 *url_mismatch = true; |
83 return; | 127 return; |
84 } | 128 } |
85 if (existing_instance->load_failed()) { | 129 if (existing_instance->load_failed()) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 iter != worker_hosts_.end(); | 172 iter != worker_hosts_.end(); |
129 ++iter) { | 173 ++iter) { |
130 if (iter->second->FilterMessage(message, filter)) | 174 if (iter->second->FilterMessage(message, filter)) |
131 return; | 175 return; |
132 } | 176 } |
133 } | 177 } |
134 | 178 |
135 void SharedWorkerServiceImpl::DocumentDetached( | 179 void SharedWorkerServiceImpl::DocumentDetached( |
136 unsigned long long document_id, | 180 unsigned long long document_id, |
137 SharedWorkerMessageFilter* filter) { | 181 SharedWorkerMessageFilter* filter) { |
182 ScopedWorkerDependencyChecker checker(this); | |
138 for (WorkerHostMap::const_iterator iter = worker_hosts_.begin(); | 183 for (WorkerHostMap::const_iterator iter = worker_hosts_.begin(); |
139 iter != worker_hosts_.end(); | 184 iter != worker_hosts_.end(); |
140 ++iter) { | 185 ++iter) { |
141 iter->second->DocumentDetached(filter, document_id); | 186 iter->second->DocumentDetached(filter, document_id); |
142 } | 187 } |
143 } | 188 } |
144 | 189 |
145 void SharedWorkerServiceImpl::WorkerContextClosed( | 190 void SharedWorkerServiceImpl::WorkerContextClosed( |
146 int worker_route_id, | 191 int worker_route_id, |
147 SharedWorkerMessageFilter* filter) { | 192 SharedWorkerMessageFilter* filter) { |
193 ScopedWorkerDependencyChecker checker(this); | |
148 if (SharedWorkerHost* host = FindSharedWorkerHost(filter, worker_route_id)) | 194 if (SharedWorkerHost* host = FindSharedWorkerHost(filter, worker_route_id)) |
149 host->WorkerContextClosed(); | 195 host->WorkerContextClosed(); |
150 } | 196 } |
151 | 197 |
152 void SharedWorkerServiceImpl::WorkerContextDestroyed( | 198 void SharedWorkerServiceImpl::WorkerContextDestroyed( |
153 int worker_route_id, | 199 int worker_route_id, |
154 SharedWorkerMessageFilter* filter) { | 200 SharedWorkerMessageFilter* filter) { |
201 ScopedWorkerDependencyChecker checker(this); | |
155 scoped_ptr<SharedWorkerHost> host = | 202 scoped_ptr<SharedWorkerHost> host = |
156 worker_hosts_.take_and_erase(std::make_pair(filter->render_process_id(), | 203 worker_hosts_.take_and_erase(std::make_pair(filter->render_process_id(), |
157 worker_route_id)); | 204 worker_route_id)); |
158 if (!host) | 205 if (!host) |
159 return; | 206 return; |
160 host->WorkerContextDestroyed(); | 207 host->WorkerContextDestroyed(); |
161 } | 208 } |
162 | 209 |
163 void SharedWorkerServiceImpl::WorkerScriptLoaded( | 210 void SharedWorkerServiceImpl::WorkerScriptLoaded( |
164 int worker_route_id, | 211 int worker_route_id, |
165 SharedWorkerMessageFilter* filter) { | 212 SharedWorkerMessageFilter* filter) { |
166 if (SharedWorkerHost* host = FindSharedWorkerHost(filter, worker_route_id)) | 213 if (SharedWorkerHost* host = FindSharedWorkerHost(filter, worker_route_id)) |
167 host->WorkerScriptLoaded(); | 214 host->WorkerScriptLoaded(); |
168 } | 215 } |
169 | 216 |
170 void SharedWorkerServiceImpl::WorkerScriptLoadFailed( | 217 void SharedWorkerServiceImpl::WorkerScriptLoadFailed( |
171 int worker_route_id, | 218 int worker_route_id, |
172 SharedWorkerMessageFilter* filter) { | 219 SharedWorkerMessageFilter* filter) { |
220 ScopedWorkerDependencyChecker checker(this); | |
173 scoped_ptr<SharedWorkerHost> host = | 221 scoped_ptr<SharedWorkerHost> host = |
174 worker_hosts_.take_and_erase(std::make_pair(filter->render_process_id(), | 222 worker_hosts_.take_and_erase(std::make_pair(filter->render_process_id(), |
175 worker_route_id)); | 223 worker_route_id)); |
176 if (!host) | 224 if (!host) |
177 return; | 225 return; |
178 host->WorkerScriptLoadFailed(); | 226 host->WorkerScriptLoadFailed(); |
179 } | 227 } |
180 | 228 |
181 void SharedWorkerServiceImpl::WorkerConnected( | 229 void SharedWorkerServiceImpl::WorkerConnected( |
182 int message_port_id, | 230 int message_port_id, |
(...skipping 29 matching lines...) Expand all Loading... | |
212 const GURL& url, | 260 const GURL& url, |
213 const base::string16& name, | 261 const base::string16& name, |
214 bool* result, | 262 bool* result, |
215 SharedWorkerMessageFilter* filter) { | 263 SharedWorkerMessageFilter* filter) { |
216 if (SharedWorkerHost* host = FindSharedWorkerHost(filter, worker_route_id)) | 264 if (SharedWorkerHost* host = FindSharedWorkerHost(filter, worker_route_id)) |
217 host->AllowIndexedDB(url, name, result); | 265 host->AllowIndexedDB(url, name, result); |
218 } | 266 } |
219 | 267 |
220 void SharedWorkerServiceImpl::OnSharedWorkerMessageFilterClosing( | 268 void SharedWorkerServiceImpl::OnSharedWorkerMessageFilterClosing( |
221 SharedWorkerMessageFilter* filter) { | 269 SharedWorkerMessageFilter* filter) { |
270 ScopedWorkerDependencyChecker checker(this); | |
222 std::vector<ProcessRouteIdPair> remove_list; | 271 std::vector<ProcessRouteIdPair> remove_list; |
223 for (WorkerHostMap::iterator iter = worker_hosts_.begin(); | 272 for (WorkerHostMap::iterator iter = worker_hosts_.begin(); |
224 iter != worker_hosts_.end(); | 273 iter != worker_hosts_.end(); |
225 ++iter) { | 274 ++iter) { |
226 iter->second->FilterShutdown(filter); | 275 iter->second->FilterShutdown(filter); |
227 if (iter->first.first == filter->render_process_id()) | 276 if (iter->first.first == filter->render_process_id()) |
228 remove_list.push_back(iter->first); | 277 remove_list.push_back(iter->first); |
229 } | 278 } |
230 for (size_t i = 0; i < remove_list.size(); ++i) | 279 for (size_t i = 0; i < remove_list.size(); ++i) |
231 worker_hosts_.erase(remove_list[i]); | 280 worker_hosts_.erase(remove_list[i]); |
(...skipping 14 matching lines...) Expand all Loading... | |
246 for (WorkerHostMap::const_iterator iter = worker_hosts_.begin(); | 295 for (WorkerHostMap::const_iterator iter = worker_hosts_.begin(); |
247 iter != worker_hosts_.end(); | 296 iter != worker_hosts_.end(); |
248 ++iter) { | 297 ++iter) { |
249 SharedWorkerInstance* instance = iter->second->instance(); | 298 SharedWorkerInstance* instance = iter->second->instance(); |
250 if (instance && instance->Matches(url, name, partition, resource_context)) | 299 if (instance && instance->Matches(url, name, partition, resource_context)) |
251 return instance; | 300 return instance; |
252 } | 301 } |
253 return NULL; | 302 return NULL; |
254 } | 303 } |
255 | 304 |
305 const std::set<int> | |
306 SharedWorkerServiceImpl::GetRenderersWithWorkerDependency() { | |
307 std::set<int> dependend_renderers; | |
308 for (WorkerHostMap::iterator host_iter = worker_hosts_.begin(); | |
309 host_iter != worker_hosts_.end(); | |
310 ++host_iter) { | |
311 const int process_id = host_iter->first.first; | |
312 if (dependend_renderers.count(process_id)) | |
313 continue; | |
314 SharedWorkerInstance* instance = host_iter->second->instance(); | |
315 if (instance && | |
316 instance->worker_document_set()->ContainsExternalRenderer(process_id)) { | |
317 dependend_renderers.insert(process_id); | |
318 } | |
319 } | |
320 return dependend_renderers; | |
kinuko
2014/03/10 04:25:43
nit: probably you meant depended_ or dependent_ ?
horo
2014/03/10 11:48:31
Done.
| |
321 } | |
322 | |
323 void SharedWorkerServiceImpl::CheckWorkerDependency() { | |
324 const std::set<int> current_worker_depended_renderers = | |
325 GetRenderersWithWorkerDependency(); | |
326 std::vector<int> added_items; | |
327 std::vector<int> removed_items; | |
328 std::set_difference(current_worker_depended_renderers.begin(), | |
329 current_worker_depended_renderers.end(), | |
330 last_worker_depended_renderers_.begin(), | |
331 last_worker_depended_renderers_.end(), | |
332 std::back_inserter(added_items)); | |
333 std::set_difference(last_worker_depended_renderers_.begin(), | |
334 last_worker_depended_renderers_.end(), | |
335 current_worker_depended_renderers.begin(), | |
336 current_worker_depended_renderers.end(), | |
337 std::back_inserter(removed_items)); | |
338 if (!added_items.empty() || !removed_items.empty()) { | |
339 last_worker_depended_renderers_ = current_worker_depended_renderers; | |
340 update_worker_dependency_(added_items, removed_items); | |
341 } | |
342 } | |
343 | |
344 void SharedWorkerServiceImpl::ChangeUpdateWorkerDependencyFuncForTesting( | |
345 UpdateWorkerDependencyFunc new_func) { | |
346 update_worker_dependency_ = new_func; | |
347 } | |
348 | |
256 } // namespace content | 349 } // namespace content |
OLD | NEW |