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" | |
13 #include "content/browser/shared_worker/shared_worker_host.h" | 7 #include "content/browser/shared_worker/shared_worker_host.h" |
14 #include "content/browser/shared_worker/shared_worker_instance.h" | 8 #include "content/browser/shared_worker/shared_worker_instance.h" |
15 #include "content/browser/shared_worker/shared_worker_message_filter.h" | 9 #include "content/browser/shared_worker/shared_worker_message_filter.h" |
16 #include "content/browser/worker_host/worker_document_set.h" | 10 #include "content/browser/worker_host/worker_document_set.h" |
17 #include "content/common/view_messages.h" | 11 #include "content/common/view_messages.h" |
18 #include "content/common/worker_messages.h" | 12 #include "content/common/worker_messages.h" |
19 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/worker_service_observer.h" | 14 #include "content/public/browser/worker_service_observer.h" |
21 | 15 |
22 namespace content { | 16 namespace content { |
23 namespace { | |
24 | |
25 class ScopedWorkerDependencyChecker { | |
26 public: | |
27 explicit ScopedWorkerDependencyChecker(SharedWorkerServiceImpl* service) | |
28 : service_(service) {} | |
29 ~ScopedWorkerDependencyChecker() { service_->CheckWorkerDependency(); } | |
30 | |
31 private: | |
32 SharedWorkerServiceImpl* service_; | |
33 DISALLOW_COPY_AND_ASSIGN(ScopedWorkerDependencyChecker); | |
34 }; | |
35 | |
36 void UpdateWorkerDependencyOnUI(const std::vector<int>& added_ids, | |
37 const std::vector<int>& removed_ids) { | |
38 for (size_t i = 0; i < added_ids.size(); ++i) { | |
39 RenderProcessHostImpl* render_process_host_impl = | |
40 static_cast<RenderProcessHostImpl*>( | |
41 RenderProcessHost::FromID(added_ids[i])); | |
42 if (!render_process_host_impl) | |
43 continue; | |
44 render_process_host_impl->IncrementWorkerRefCount(); | |
45 } | |
46 for (size_t i = 0; i < removed_ids.size(); ++i) { | |
47 RenderProcessHostImpl* render_process_host_impl = | |
48 static_cast<RenderProcessHostImpl*>( | |
49 RenderProcessHost::FromID(removed_ids[i])); | |
50 if (!render_process_host_impl) | |
51 continue; | |
52 render_process_host_impl->DecrementWorkerRefCount(); | |
53 } | |
54 } | |
55 | |
56 void UpdateWorkerDependency(const std::vector<int>& added_ids, | |
57 const std::vector<int>& removed_ids) { | |
58 BrowserThread::PostTask( | |
59 BrowserThread::UI, | |
60 FROM_HERE, | |
61 base::Bind(&UpdateWorkerDependencyOnUI, added_ids, removed_ids)); | |
62 } | |
63 | |
64 } // namespace | |
65 | 17 |
66 SharedWorkerServiceImpl* SharedWorkerServiceImpl::GetInstance() { | 18 SharedWorkerServiceImpl* SharedWorkerServiceImpl::GetInstance() { |
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 19 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
68 return Singleton<SharedWorkerServiceImpl>::get(); | 20 return Singleton<SharedWorkerServiceImpl>::get(); |
69 } | 21 } |
70 | 22 |
71 SharedWorkerServiceImpl::SharedWorkerServiceImpl() | 23 SharedWorkerServiceImpl::SharedWorkerServiceImpl() { |
72 : update_worker_dependency_(UpdateWorkerDependency) {} | 24 } |
73 | 25 |
74 SharedWorkerServiceImpl::~SharedWorkerServiceImpl() {} | 26 SharedWorkerServiceImpl::~SharedWorkerServiceImpl() { |
75 | |
76 void SharedWorkerServiceImpl::ResetForTesting() { | |
77 last_worker_depended_renderers_.clear(); | |
78 worker_hosts_.clear(); | |
79 observers_.Clear(); | |
80 update_worker_dependency_ = UpdateWorkerDependency; | |
81 } | 27 } |
82 | 28 |
83 bool SharedWorkerServiceImpl::TerminateWorker(int process_id, int route_id) { | 29 bool SharedWorkerServiceImpl::TerminateWorker(int process_id, int route_id) { |
84 SharedWorkerHost* host = | 30 SharedWorkerHost* host = |
85 worker_hosts_.get(std::make_pair(process_id, route_id)); | 31 worker_hosts_.get(std::make_pair(process_id, route_id)); |
86 if (!host || !host->instance()) | 32 if (!host || !host->instance()) |
87 return false; | 33 return false; |
88 host->TerminateWorker(); | 34 host->TerminateWorker(); |
89 return true; | 35 return true; |
90 } | 36 } |
(...skipping 29 matching lines...) Expand all Loading... |
120 } | 66 } |
121 | 67 |
122 void SharedWorkerServiceImpl::CreateWorker( | 68 void SharedWorkerServiceImpl::CreateWorker( |
123 const ViewHostMsg_CreateWorker_Params& params, | 69 const ViewHostMsg_CreateWorker_Params& params, |
124 int route_id, | 70 int route_id, |
125 SharedWorkerMessageFilter* filter, | 71 SharedWorkerMessageFilter* filter, |
126 ResourceContext* resource_context, | 72 ResourceContext* resource_context, |
127 const WorkerStoragePartition& partition, | 73 const WorkerStoragePartition& partition, |
128 bool* url_mismatch) { | 74 bool* url_mismatch) { |
129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
130 ScopedWorkerDependencyChecker checker(this); | |
131 *url_mismatch = false; | 76 *url_mismatch = false; |
132 SharedWorkerInstance* existing_instance = | 77 SharedWorkerInstance* existing_instance = |
133 FindSharedWorkerInstance( | 78 FindSharedWorkerInstance( |
134 params.url, params.name, partition, resource_context); | 79 params.url, params.name, partition, resource_context); |
135 if (existing_instance) { | 80 if (existing_instance) { |
136 if (params.url != existing_instance->url()) { | 81 if (params.url != existing_instance->url()) { |
137 *url_mismatch = true; | 82 *url_mismatch = true; |
138 return; | 83 return; |
139 } | 84 } |
140 if (existing_instance->load_failed()) { | 85 if (existing_instance->load_failed()) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 iter != worker_hosts_.end(); | 128 iter != worker_hosts_.end(); |
184 ++iter) { | 129 ++iter) { |
185 if (iter->second->FilterMessage(message, filter)) | 130 if (iter->second->FilterMessage(message, filter)) |
186 return; | 131 return; |
187 } | 132 } |
188 } | 133 } |
189 | 134 |
190 void SharedWorkerServiceImpl::DocumentDetached( | 135 void SharedWorkerServiceImpl::DocumentDetached( |
191 unsigned long long document_id, | 136 unsigned long long document_id, |
192 SharedWorkerMessageFilter* filter) { | 137 SharedWorkerMessageFilter* filter) { |
193 ScopedWorkerDependencyChecker checker(this); | |
194 for (WorkerHostMap::const_iterator iter = worker_hosts_.begin(); | 138 for (WorkerHostMap::const_iterator iter = worker_hosts_.begin(); |
195 iter != worker_hosts_.end(); | 139 iter != worker_hosts_.end(); |
196 ++iter) { | 140 ++iter) { |
197 iter->second->DocumentDetached(filter, document_id); | 141 iter->second->DocumentDetached(filter, document_id); |
198 } | 142 } |
199 } | 143 } |
200 | 144 |
201 void SharedWorkerServiceImpl::WorkerContextClosed( | 145 void SharedWorkerServiceImpl::WorkerContextClosed( |
202 int worker_route_id, | 146 int worker_route_id, |
203 SharedWorkerMessageFilter* filter) { | 147 SharedWorkerMessageFilter* filter) { |
204 ScopedWorkerDependencyChecker checker(this); | |
205 if (SharedWorkerHost* host = FindSharedWorkerHost(filter, worker_route_id)) | 148 if (SharedWorkerHost* host = FindSharedWorkerHost(filter, worker_route_id)) |
206 host->WorkerContextClosed(); | 149 host->WorkerContextClosed(); |
207 } | 150 } |
208 | 151 |
209 void SharedWorkerServiceImpl::WorkerContextDestroyed( | 152 void SharedWorkerServiceImpl::WorkerContextDestroyed( |
210 int worker_route_id, | 153 int worker_route_id, |
211 SharedWorkerMessageFilter* filter) { | 154 SharedWorkerMessageFilter* filter) { |
212 ScopedWorkerDependencyChecker checker(this); | |
213 scoped_ptr<SharedWorkerHost> host = | 155 scoped_ptr<SharedWorkerHost> host = |
214 worker_hosts_.take_and_erase(std::make_pair(filter->render_process_id(), | 156 worker_hosts_.take_and_erase(std::make_pair(filter->render_process_id(), |
215 worker_route_id)); | 157 worker_route_id)); |
216 if (!host) | 158 if (!host) |
217 return; | 159 return; |
218 host->WorkerContextDestroyed(); | 160 host->WorkerContextDestroyed(); |
219 } | 161 } |
220 | 162 |
221 void SharedWorkerServiceImpl::WorkerScriptLoaded( | 163 void SharedWorkerServiceImpl::WorkerScriptLoaded( |
222 int worker_route_id, | 164 int worker_route_id, |
223 SharedWorkerMessageFilter* filter) { | 165 SharedWorkerMessageFilter* filter) { |
224 if (SharedWorkerHost* host = FindSharedWorkerHost(filter, worker_route_id)) | 166 if (SharedWorkerHost* host = FindSharedWorkerHost(filter, worker_route_id)) |
225 host->WorkerScriptLoaded(); | 167 host->WorkerScriptLoaded(); |
226 } | 168 } |
227 | 169 |
228 void SharedWorkerServiceImpl::WorkerScriptLoadFailed( | 170 void SharedWorkerServiceImpl::WorkerScriptLoadFailed( |
229 int worker_route_id, | 171 int worker_route_id, |
230 SharedWorkerMessageFilter* filter) { | 172 SharedWorkerMessageFilter* filter) { |
231 ScopedWorkerDependencyChecker checker(this); | |
232 scoped_ptr<SharedWorkerHost> host = | 173 scoped_ptr<SharedWorkerHost> host = |
233 worker_hosts_.take_and_erase(std::make_pair(filter->render_process_id(), | 174 worker_hosts_.take_and_erase(std::make_pair(filter->render_process_id(), |
234 worker_route_id)); | 175 worker_route_id)); |
235 if (!host) | 176 if (!host) |
236 return; | 177 return; |
237 host->WorkerScriptLoadFailed(); | 178 host->WorkerScriptLoadFailed(); |
238 } | 179 } |
239 | 180 |
240 void SharedWorkerServiceImpl::WorkerConnected( | 181 void SharedWorkerServiceImpl::WorkerConnected( |
241 int message_port_id, | 182 int message_port_id, |
(...skipping 29 matching lines...) Expand all Loading... |
271 const GURL& url, | 212 const GURL& url, |
272 const base::string16& name, | 213 const base::string16& name, |
273 bool* result, | 214 bool* result, |
274 SharedWorkerMessageFilter* filter) { | 215 SharedWorkerMessageFilter* filter) { |
275 if (SharedWorkerHost* host = FindSharedWorkerHost(filter, worker_route_id)) | 216 if (SharedWorkerHost* host = FindSharedWorkerHost(filter, worker_route_id)) |
276 host->AllowIndexedDB(url, name, result); | 217 host->AllowIndexedDB(url, name, result); |
277 } | 218 } |
278 | 219 |
279 void SharedWorkerServiceImpl::OnSharedWorkerMessageFilterClosing( | 220 void SharedWorkerServiceImpl::OnSharedWorkerMessageFilterClosing( |
280 SharedWorkerMessageFilter* filter) { | 221 SharedWorkerMessageFilter* filter) { |
281 ScopedWorkerDependencyChecker checker(this); | |
282 std::vector<ProcessRouteIdPair> remove_list; | 222 std::vector<ProcessRouteIdPair> remove_list; |
283 for (WorkerHostMap::iterator iter = worker_hosts_.begin(); | 223 for (WorkerHostMap::iterator iter = worker_hosts_.begin(); |
284 iter != worker_hosts_.end(); | 224 iter != worker_hosts_.end(); |
285 ++iter) { | 225 ++iter) { |
286 iter->second->FilterShutdown(filter); | 226 iter->second->FilterShutdown(filter); |
287 if (iter->first.first == filter->render_process_id()) | 227 if (iter->first.first == filter->render_process_id()) |
288 remove_list.push_back(iter->first); | 228 remove_list.push_back(iter->first); |
289 } | 229 } |
290 for (size_t i = 0; i < remove_list.size(); ++i) | 230 for (size_t i = 0; i < remove_list.size(); ++i) |
291 worker_hosts_.erase(remove_list[i]); | 231 worker_hosts_.erase(remove_list[i]); |
(...skipping 14 matching lines...) Expand all Loading... |
306 for (WorkerHostMap::const_iterator iter = worker_hosts_.begin(); | 246 for (WorkerHostMap::const_iterator iter = worker_hosts_.begin(); |
307 iter != worker_hosts_.end(); | 247 iter != worker_hosts_.end(); |
308 ++iter) { | 248 ++iter) { |
309 SharedWorkerInstance* instance = iter->second->instance(); | 249 SharedWorkerInstance* instance = iter->second->instance(); |
310 if (instance && instance->Matches(url, name, partition, resource_context)) | 250 if (instance && instance->Matches(url, name, partition, resource_context)) |
311 return instance; | 251 return instance; |
312 } | 252 } |
313 return NULL; | 253 return NULL; |
314 } | 254 } |
315 | 255 |
316 const std::set<int> | |
317 SharedWorkerServiceImpl::GetRenderersWithWorkerDependency() { | |
318 std::set<int> dependent_renderers; | |
319 for (WorkerHostMap::iterator host_iter = worker_hosts_.begin(); | |
320 host_iter != worker_hosts_.end(); | |
321 ++host_iter) { | |
322 const int process_id = host_iter->first.first; | |
323 if (dependent_renderers.count(process_id)) | |
324 continue; | |
325 SharedWorkerInstance* instance = host_iter->second->instance(); | |
326 if (instance && | |
327 instance->worker_document_set()->ContainsExternalRenderer(process_id)) { | |
328 dependent_renderers.insert(process_id); | |
329 } | |
330 } | |
331 return dependent_renderers; | |
332 } | |
333 | |
334 void SharedWorkerServiceImpl::CheckWorkerDependency() { | |
335 const std::set<int> current_worker_depended_renderers = | |
336 GetRenderersWithWorkerDependency(); | |
337 std::vector<int> added_items; | |
338 std::vector<int> removed_items; | |
339 std::set_difference(current_worker_depended_renderers.begin(), | |
340 current_worker_depended_renderers.end(), | |
341 last_worker_depended_renderers_.begin(), | |
342 last_worker_depended_renderers_.end(), | |
343 std::back_inserter(added_items)); | |
344 std::set_difference(last_worker_depended_renderers_.begin(), | |
345 last_worker_depended_renderers_.end(), | |
346 current_worker_depended_renderers.begin(), | |
347 current_worker_depended_renderers.end(), | |
348 std::back_inserter(removed_items)); | |
349 if (!added_items.empty() || !removed_items.empty()) { | |
350 last_worker_depended_renderers_ = current_worker_depended_renderers; | |
351 update_worker_dependency_(added_items, removed_items); | |
352 } | |
353 } | |
354 | |
355 void SharedWorkerServiceImpl::ChangeUpdateWorkerDependencyFuncForTesting( | |
356 UpdateWorkerDependencyFunc new_func) { | |
357 update_worker_dependency_ = new_func; | |
358 } | |
359 | |
360 } // namespace content | 256 } // namespace content |
OLD | NEW |