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> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "content/browser/devtools/shared_worker_devtools_manager.h" | |
13 #include "content/browser/renderer_host/render_process_host_impl.h" | 12 #include "content/browser/renderer_host/render_process_host_impl.h" |
14 #include "content/browser/shared_worker/shared_worker_host.h" | 13 #include "content/browser/shared_worker/shared_worker_host.h" |
15 #include "content/browser/shared_worker/shared_worker_instance.h" | 14 #include "content/browser/shared_worker/shared_worker_instance.h" |
16 #include "content/browser/shared_worker/shared_worker_message_filter.h" | 15 #include "content/browser/shared_worker/shared_worker_message_filter.h" |
17 #include "content/browser/worker_host/worker_document_set.h" | 16 #include "content/browser/worker_host/worker_document_set.h" |
18 #include "content/common/view_messages.h" | 17 #include "content/common/view_messages.h" |
19 #include "content/common/worker_messages.h" | 18 #include "content/common/worker_messages.h" |
20 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/worker_service_observer.h" | 20 #include "content/public/browser/worker_service_observer.h" |
22 | 21 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 } | 54 } |
56 | 55 |
57 void UpdateWorkerDependency(const std::vector<int>& added_ids, | 56 void UpdateWorkerDependency(const std::vector<int>& added_ids, |
58 const std::vector<int>& removed_ids) { | 57 const std::vector<int>& removed_ids) { |
59 BrowserThread::PostTask( | 58 BrowserThread::PostTask( |
60 BrowserThread::UI, | 59 BrowserThread::UI, |
61 FROM_HERE, | 60 FROM_HERE, |
62 base::Bind(&UpdateWorkerDependencyOnUI, added_ids, removed_ids)); | 61 base::Bind(&UpdateWorkerDependencyOnUI, added_ids, removed_ids)); |
63 } | 62 } |
64 | 63 |
65 void WorkerCreatedResultCallbackOnIO(int worker_process_id, | |
66 int worker_route_id, | |
67 bool pause_on_start) { | |
68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
69 SharedWorkerServiceImpl::GetInstance()->WorkerCreatedResultCallback( | |
70 worker_process_id, worker_route_id, pause_on_start); | |
71 } | |
72 | |
73 void NotifyWorkerCreatedOnUI(int worker_process_id, | |
74 int worker_route_id, | |
75 const SharedWorkerInstance& instance) { | |
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
77 bool pause_on_start = | |
78 SharedWorkerDevToolsManager::GetInstance()->WorkerCreated( | |
79 worker_process_id, worker_route_id, instance); | |
80 BrowserThread::PostTask(BrowserThread::IO, | |
81 FROM_HERE, | |
82 base::Bind(&WorkerCreatedResultCallbackOnIO, | |
83 worker_process_id, | |
84 worker_route_id, | |
85 pause_on_start)); | |
86 } | |
87 | |
88 } // namespace | 64 } // namespace |
89 | 65 |
90 SharedWorkerServiceImpl* SharedWorkerServiceImpl::GetInstance() { | 66 SharedWorkerServiceImpl* SharedWorkerServiceImpl::GetInstance() { |
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
92 return Singleton<SharedWorkerServiceImpl>::get(); | 68 return Singleton<SharedWorkerServiceImpl>::get(); |
93 } | 69 } |
94 | 70 |
95 SharedWorkerServiceImpl::SharedWorkerServiceImpl() | 71 SharedWorkerServiceImpl::SharedWorkerServiceImpl() |
96 : update_worker_dependency_(UpdateWorkerDependency) {} | 72 : update_worker_dependency_(UpdateWorkerDependency) {} |
97 | 73 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 const ViewHostMsg_CreateWorker_Params& params, | 123 const ViewHostMsg_CreateWorker_Params& params, |
148 int route_id, | 124 int route_id, |
149 SharedWorkerMessageFilter* filter, | 125 SharedWorkerMessageFilter* filter, |
150 ResourceContext* resource_context, | 126 ResourceContext* resource_context, |
151 const WorkerStoragePartition& partition, | 127 const WorkerStoragePartition& partition, |
152 bool* url_mismatch) { | 128 bool* url_mismatch) { |
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
154 ScopedWorkerDependencyChecker checker(this); | 130 ScopedWorkerDependencyChecker checker(this); |
155 *url_mismatch = false; | 131 *url_mismatch = false; |
156 SharedWorkerHost* existing_host = FindSharedWorkerHost( | 132 SharedWorkerHost* existing_host = FindSharedWorkerHost( |
157 worker_hosts_, params.url, params.name, partition, resource_context); | 133 params.url, params.name, partition, resource_context); |
158 if (existing_host) { | 134 if (existing_host) { |
159 if (params.url != existing_host->instance()->url()) { | 135 if (params.url != existing_host->instance()->url()) { |
160 *url_mismatch = true; | 136 *url_mismatch = true; |
161 return; | 137 return; |
162 } | 138 } |
163 if (existing_host->load_failed()) { | 139 if (existing_host->load_failed()) { |
164 filter->Send(new ViewMsg_WorkerScriptLoadFailed(route_id)); | 140 filter->Send(new ViewMsg_WorkerScriptLoadFailed(route_id)); |
165 return; | 141 return; |
166 } | 142 } |
167 existing_host->AddFilter(filter, route_id); | 143 existing_host->AddFilter(filter, route_id); |
168 existing_host->worker_document_set()->Add(filter, | 144 existing_host->worker_document_set()->Add(filter, |
169 params.document_id, | 145 params.document_id, |
170 filter->render_process_id(), | 146 filter->render_process_id(), |
171 params.render_frame_route_id); | 147 params.render_frame_route_id); |
172 filter->Send(new ViewMsg_WorkerCreated(route_id)); | 148 filter->Send(new ViewMsg_WorkerCreated(route_id)); |
173 return; | 149 return; |
174 } | 150 } |
175 SharedWorkerHost* pending_host = FindSharedWorkerHost(pending_worker_hosts_, | |
176 params.url, | |
177 params.name, | |
178 partition, | |
179 resource_context); | |
180 if (pending_host) { | |
181 if (params.url != pending_host->instance()->url()) { | |
182 *url_mismatch = true; | |
183 return; | |
184 } | |
185 pending_host->AddFilter(filter, route_id); | |
186 pending_host->worker_document_set()->Add(filter, | |
187 params.document_id, | |
188 filter->render_process_id(), | |
189 params.render_frame_route_id); | |
190 return; | |
191 } | |
192 | 151 |
193 scoped_ptr<SharedWorkerInstance> instance(new SharedWorkerInstance( | 152 scoped_ptr<SharedWorkerInstance> instance(new SharedWorkerInstance( |
194 params.url, | 153 params.url, |
195 params.name, | 154 params.name, |
196 params.content_security_policy, | 155 params.content_security_policy, |
197 params.security_policy_type, | 156 params.security_policy_type, |
198 resource_context, | 157 resource_context, |
199 partition)); | 158 partition)); |
200 scoped_ptr<SharedWorkerHost> host( | 159 scoped_ptr<SharedWorkerHost> host(new SharedWorkerHost(instance.release())); |
201 new SharedWorkerHost(instance.release(), filter)); | |
202 host->AddFilter(filter, route_id); | 160 host->AddFilter(filter, route_id); |
203 host->worker_document_set()->Add(filter, | 161 host->worker_document_set()->Add(filter, |
204 params.document_id, | 162 params.document_id, |
205 filter->render_process_id(), | 163 filter->render_process_id(), |
206 params.render_frame_route_id); | 164 params.render_frame_route_id); |
| 165 |
| 166 host->Init(filter); |
207 const int worker_route_id = host->worker_route_id(); | 167 const int worker_route_id = host->worker_route_id(); |
208 // We need to call SharedWorkerDevToolsManager::WorkerCreated() on UI thread | 168 worker_hosts_.set(std::make_pair(filter->render_process_id(), |
209 // to know whether the worker should be paused on start or not. | 169 worker_route_id), |
210 // WorkerCreatedResultCallback() will be called with the result. | 170 host.Pass()); |
211 BrowserThread::PostTask(BrowserThread::UI, | |
212 FROM_HERE, | |
213 base::Bind(&NotifyWorkerCreatedOnUI, | |
214 filter->render_process_id(), | |
215 worker_route_id, | |
216 *host->instance())); | |
217 pending_worker_hosts_.set( | |
218 std::make_pair(filter->render_process_id(), worker_route_id), | |
219 host.Pass()); | |
220 } | |
221 | 171 |
222 void SharedWorkerServiceImpl::WorkerCreatedResultCallback(int worker_process_id, | |
223 int worker_route_id, | |
224 bool pause_on_start) { | |
225 scoped_ptr<SharedWorkerHost> host = pending_worker_hosts_.take_and_erase( | |
226 std::make_pair(worker_process_id, worker_route_id)); | |
227 const GURL url = host->instance()->url(); | |
228 const base::string16 name = host->instance()->name(); | |
229 host->Start(pause_on_start); | |
230 worker_hosts_.set(std::make_pair(worker_process_id, worker_route_id), | |
231 host.Pass()); | |
232 FOR_EACH_OBSERVER( | 172 FOR_EACH_OBSERVER( |
233 WorkerServiceObserver, | 173 WorkerServiceObserver, observers_, |
234 observers_, | 174 WorkerCreated(params.url, |
235 WorkerCreated(url, name, worker_process_id, worker_route_id)); | 175 params.name, |
| 176 filter->render_process_id(), |
| 177 worker_route_id)); |
236 } | 178 } |
237 | 179 |
238 void SharedWorkerServiceImpl::ForwardToWorker( | 180 void SharedWorkerServiceImpl::ForwardToWorker( |
239 const IPC::Message& message, | 181 const IPC::Message& message, |
240 SharedWorkerMessageFilter* filter) { | 182 SharedWorkerMessageFilter* filter) { |
241 for (WorkerHostMap::const_iterator iter = worker_hosts_.begin(); | 183 for (WorkerHostMap::const_iterator iter = worker_hosts_.begin(); |
242 iter != worker_hosts_.end(); | 184 iter != worker_hosts_.end(); |
243 ++iter) { | 185 ++iter) { |
244 if (iter->second->FilterMessage(message, filter)) | 186 if (iter->second->FilterMessage(message, filter)) |
245 return; | 187 return; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 SharedWorkerMessageFilter* filter) { | 281 SharedWorkerMessageFilter* filter) { |
340 ScopedWorkerDependencyChecker checker(this); | 282 ScopedWorkerDependencyChecker checker(this); |
341 std::vector<ProcessRouteIdPair> remove_list; | 283 std::vector<ProcessRouteIdPair> remove_list; |
342 for (WorkerHostMap::iterator iter = worker_hosts_.begin(); | 284 for (WorkerHostMap::iterator iter = worker_hosts_.begin(); |
343 iter != worker_hosts_.end(); | 285 iter != worker_hosts_.end(); |
344 ++iter) { | 286 ++iter) { |
345 iter->second->FilterShutdown(filter); | 287 iter->second->FilterShutdown(filter); |
346 if (iter->first.first == filter->render_process_id()) | 288 if (iter->first.first == filter->render_process_id()) |
347 remove_list.push_back(iter->first); | 289 remove_list.push_back(iter->first); |
348 } | 290 } |
349 for (size_t i = 0; i < remove_list.size(); ++i) { | 291 for (size_t i = 0; i < remove_list.size(); ++i) |
350 scoped_ptr<SharedWorkerHost> host = | 292 worker_hosts_.erase(remove_list[i]); |
351 worker_hosts_.take_and_erase(remove_list[i]); | |
352 } | |
353 } | |
354 | |
355 void SharedWorkerServiceImpl::NotifyWorkerDestroyed(int worker_process_id, | |
356 int worker_route_id) { | |
357 FOR_EACH_OBSERVER(WorkerServiceObserver, | |
358 observers_, | |
359 WorkerDestroyed(worker_process_id, worker_route_id)); | |
360 } | 293 } |
361 | 294 |
362 SharedWorkerHost* SharedWorkerServiceImpl::FindSharedWorkerHost( | 295 SharedWorkerHost* SharedWorkerServiceImpl::FindSharedWorkerHost( |
363 SharedWorkerMessageFilter* filter, | 296 SharedWorkerMessageFilter* filter, |
364 int worker_route_id) { | 297 int worker_route_id) { |
365 return worker_hosts_.get(std::make_pair(filter->render_process_id(), | 298 return worker_hosts_.get(std::make_pair(filter->render_process_id(), |
366 worker_route_id)); | 299 worker_route_id)); |
367 } | 300 } |
368 | 301 |
369 // static | |
370 SharedWorkerHost* SharedWorkerServiceImpl::FindSharedWorkerHost( | 302 SharedWorkerHost* SharedWorkerServiceImpl::FindSharedWorkerHost( |
371 const WorkerHostMap& hosts, | |
372 const GURL& url, | 303 const GURL& url, |
373 const base::string16& name, | 304 const base::string16& name, |
374 const WorkerStoragePartition& partition, | 305 const WorkerStoragePartition& partition, |
375 ResourceContext* resource_context) { | 306 ResourceContext* resource_context) { |
376 for (WorkerHostMap::const_iterator iter = hosts.begin(); iter != hosts.end(); | 307 for (WorkerHostMap::const_iterator iter = worker_hosts_.begin(); |
| 308 iter != worker_hosts_.end(); |
377 ++iter) { | 309 ++iter) { |
378 SharedWorkerInstance* instance = iter->second->instance(); | 310 SharedWorkerInstance* instance = iter->second->instance(); |
379 if (instance && !iter->second->closed() && | 311 if (instance && !iter->second->closed() && |
380 instance->Matches(url, name, partition, resource_context)) | 312 instance->Matches(url, name, partition, resource_context)) |
381 return iter->second; | 313 return iter->second; |
382 } | 314 } |
383 return NULL; | 315 return NULL; |
384 } | 316 } |
385 | 317 |
386 const std::set<int> | 318 const std::set<int> |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 update_worker_dependency_(added_items, removed_items); | 353 update_worker_dependency_(added_items, removed_items); |
422 } | 354 } |
423 } | 355 } |
424 | 356 |
425 void SharedWorkerServiceImpl::ChangeUpdateWorkerDependencyFuncForTesting( | 357 void SharedWorkerServiceImpl::ChangeUpdateWorkerDependencyFuncForTesting( |
426 UpdateWorkerDependencyFunc new_func) { | 358 UpdateWorkerDependencyFunc new_func) { |
427 update_worker_dependency_ = new_func; | 359 update_worker_dependency_ = new_func; |
428 } | 360 } |
429 | 361 |
430 } // namespace content | 362 } // namespace content |
OLD | NEW |