OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/service_worker/embedded_worker_instance.h" | 5 #include "content/browser/service_worker/embedded_worker_instance.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/threading/non_thread_safe.h" | 12 #include "base/threading/non_thread_safe.h" |
13 #include "base/trace_event/trace_event.h" | 13 #include "base/trace_event/trace_event.h" |
14 #include "content/browser/devtools/service_worker_devtools_manager.h" | 14 #include "content/browser/devtools/service_worker_devtools_manager.h" |
15 #include "content/browser/service_worker/embedded_worker_registry.h" | 15 #include "content/browser/service_worker/embedded_worker_registry.h" |
16 #include "content/browser/service_worker/service_worker_context_core.h" | 16 #include "content/browser/service_worker/service_worker_context_core.h" |
17 #include "content/common/content_switches_internal.h" | 17 #include "content/common/content_switches_internal.h" |
18 #include "content/common/mojo/service_registry_impl.h" | |
18 #include "content/common/service_worker/embedded_worker_messages.h" | 19 #include "content/common/service_worker/embedded_worker_messages.h" |
20 #include "content/common/service_worker/embedded_worker_setup.mojom.h" | |
19 #include "content/common/service_worker/service_worker_types.h" | 21 #include "content/common/service_worker/service_worker_types.h" |
20 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
22 #include "ipc/ipc_message.h" | 24 #include "ipc/ipc_message.h" |
23 #include "url/gurl.h" | 25 #include "url/gurl.h" |
24 | 26 |
25 namespace content { | 27 namespace content { |
26 | 28 |
27 namespace { | 29 namespace { |
28 | 30 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 service_worker_context_weak, | 78 service_worker_context_weak, |
77 service_worker_version_id, | 79 service_worker_version_id, |
78 url)); | 80 url)); |
79 } | 81 } |
80 BrowserThread::PostTask( | 82 BrowserThread::PostTask( |
81 BrowserThread::IO, | 83 BrowserThread::IO, |
82 FROM_HERE, | 84 FROM_HERE, |
83 base::Bind(callback, worker_devtools_agent_route_id, wait_for_debugger)); | 85 base::Bind(callback, worker_devtools_agent_route_id, wait_for_debugger)); |
84 } | 86 } |
85 | 87 |
88 void SetupMojoOnUIThread(int process_id, | |
89 int thread_id, | |
90 mojo::InterfaceRequest<mojo::ServiceProvider> services, | |
91 mojo::ServiceProviderPtr exposed_services) { | |
92 RenderProcessHost* rph = RenderProcessHost::FromID(process_id); | |
93 // |rph| may be NULL in unit tests. | |
94 if (!rph) | |
95 return; | |
96 EmbeddedWorkerSetupPtr setup; | |
97 rph->GetServiceRegistry()->ConnectToRemoteService(mojo::GetProxy(&setup)); | |
98 setup->ExchangeServiceProviders(thread_id, services.Pass(), | |
99 exposed_services.Pass()); | |
100 } | |
101 | |
86 } // namespace | 102 } // namespace |
87 | 103 |
88 // Lives on IO thread, proxies notifications to DevToolsManager that lives on | 104 // Lives on IO thread, proxies notifications to DevToolsManager that lives on |
89 // UI thread. Owned by EmbeddedWorkerInstance. | 105 // UI thread. Owned by EmbeddedWorkerInstance. |
90 class EmbeddedWorkerInstance::DevToolsProxy : public base::NonThreadSafe { | 106 class EmbeddedWorkerInstance::DevToolsProxy : public base::NonThreadSafe { |
91 public: | 107 public: |
92 DevToolsProxy(int process_id, int agent_route_id) | 108 DevToolsProxy(int process_id, int agent_route_id) |
93 : process_id_(process_id), | 109 : process_id_(process_id), |
94 agent_route_id_(agent_route_id) {} | 110 agent_route_id_(agent_route_id) {} |
95 | 111 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendMessage( | 211 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendMessage( |
196 const IPC::Message& message) { | 212 const IPC::Message& message) { |
197 DCHECK_NE(kInvalidEmbeddedWorkerThreadId, thread_id_); | 213 DCHECK_NE(kInvalidEmbeddedWorkerThreadId, thread_id_); |
198 if (status_ != RUNNING && status_ != STARTING) | 214 if (status_ != RUNNING && status_ != STARTING) |
199 return SERVICE_WORKER_ERROR_IPC_FAILED; | 215 return SERVICE_WORKER_ERROR_IPC_FAILED; |
200 return registry_->Send(process_id_, | 216 return registry_->Send(process_id_, |
201 new EmbeddedWorkerContextMsg_MessageToWorker( | 217 new EmbeddedWorkerContextMsg_MessageToWorker( |
202 thread_id_, embedded_worker_id_, message)); | 218 thread_id_, embedded_worker_id_, message)); |
203 } | 219 } |
204 | 220 |
221 ServiceRegistry* EmbeddedWorkerInstance::GetServiceRegistry() { | |
michaeln
2015/07/15 02:34:22
Maybe DCHECK its either STARTING or RUNNING and se
Marijn Kruisselbrink
2015/07/15 18:13:28
I added the DCHECK and moved the creating of the s
| |
222 return service_registry_.get(); | |
223 } | |
224 | |
205 EmbeddedWorkerInstance::EmbeddedWorkerInstance( | 225 EmbeddedWorkerInstance::EmbeddedWorkerInstance( |
206 base::WeakPtr<ServiceWorkerContextCore> context, | 226 base::WeakPtr<ServiceWorkerContextCore> context, |
207 int embedded_worker_id) | 227 int embedded_worker_id) |
208 : context_(context), | 228 : context_(context), |
209 registry_(context->embedded_worker_registry()), | 229 registry_(context->embedded_worker_registry()), |
210 embedded_worker_id_(embedded_worker_id), | 230 embedded_worker_id_(embedded_worker_id), |
211 status_(STOPPED), | 231 status_(STOPPED), |
212 starting_phase_(NOT_STARTING), | 232 starting_phase_(NOT_STARTING), |
213 process_id_(-1), | 233 process_id_(-1), |
214 thread_id_(kInvalidEmbeddedWorkerThreadId), | 234 thread_id_(kInvalidEmbeddedWorkerThreadId), |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
337 UMA_HISTOGRAM_TIMES( | 357 UMA_HISTOGRAM_TIMES( |
338 "EmbeddedWorkerInstance.ScriptLoadWithoutNetworkAccess", | 358 "EmbeddedWorkerInstance.ScriptLoadWithoutNetworkAccess", |
339 base::TimeTicks::Now() - start_timing_); | 359 base::TimeTicks::Now() - start_timing_); |
340 } | 360 } |
341 // Reset |start_timing_| to measure the time excluding the process | 361 // Reset |start_timing_| to measure the time excluding the process |
342 // allocation time and the script loading time. | 362 // allocation time and the script loading time. |
343 start_timing_ = base::TimeTicks::Now(); | 363 start_timing_ = base::TimeTicks::Now(); |
344 } | 364 } |
345 thread_id_ = thread_id; | 365 thread_id_ = thread_id; |
346 FOR_EACH_OBSERVER(Listener, listener_list_, OnScriptLoaded()); | 366 FOR_EACH_OBSERVER(Listener, listener_list_, OnScriptLoaded()); |
367 | |
368 service_registry_.reset(new ServiceRegistryImpl()); | |
369 mojo::ServiceProviderPtr exposed_services; | |
370 service_registry_->Bind(GetProxy(&exposed_services)); | |
371 mojo::ServiceProviderPtr services; | |
372 mojo::InterfaceRequest<mojo::ServiceProvider> services_request = | |
373 GetProxy(&services); | |
374 BrowserThread::PostTask( | |
375 BrowserThread::UI, FROM_HERE, | |
376 base::Bind(SetupMojoOnUIThread, process_id_, thread_id_, | |
377 base::Passed(&services_request), | |
378 base::Passed(&exposed_services))); | |
379 service_registry_->BindRemoteServiceProvider(services.Pass()); | |
347 } | 380 } |
348 | 381 |
349 void EmbeddedWorkerInstance::OnScriptLoadFailed() { | 382 void EmbeddedWorkerInstance::OnScriptLoadFailed() { |
350 } | 383 } |
351 | 384 |
352 void EmbeddedWorkerInstance::OnScriptEvaluated(bool success) { | 385 void EmbeddedWorkerInstance::OnScriptEvaluated(bool success) { |
353 starting_phase_ = SCRIPT_EVALUATED; | 386 starting_phase_ = SCRIPT_EVALUATED; |
354 if (start_callback_.is_null()) { | 387 if (start_callback_.is_null()) { |
355 DVLOG(1) << "Received unexpected OnScriptEvaluated message."; | 388 DVLOG(1) << "Received unexpected OnScriptEvaluated message."; |
356 return; | 389 return; |
(...skipping 12 matching lines...) Expand all Loading... | |
369 if (status_ == STOPPING) | 402 if (status_ == STOPPING) |
370 return; | 403 return; |
371 DCHECK(status_ == STARTING); | 404 DCHECK(status_ == STARTING); |
372 status_ = RUNNING; | 405 status_ = RUNNING; |
373 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarted()); | 406 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarted()); |
374 } | 407 } |
375 | 408 |
376 void EmbeddedWorkerInstance::OnStopped() { | 409 void EmbeddedWorkerInstance::OnStopped() { |
377 Status old_status = status_; | 410 Status old_status = status_; |
378 ReleaseProcess(); | 411 ReleaseProcess(); |
412 service_registry_.reset(); | |
379 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopped(old_status)); | 413 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopped(old_status)); |
380 } | 414 } |
381 | 415 |
382 void EmbeddedWorkerInstance::OnDetached() { | 416 void EmbeddedWorkerInstance::OnDetached() { |
383 Status old_status = status_; | 417 Status old_status = status_; |
384 ReleaseProcess(); | 418 ReleaseProcess(); |
michaeln
2015/07/15 02:34:22
probably should reset the registry here too, looks
Marijn Kruisselbrink
2015/07/15 18:13:28
Yes, moved the resetting of the registry to Releas
| |
385 FOR_EACH_OBSERVER(Listener, listener_list_, OnDetached(old_status)); | 419 FOR_EACH_OBSERVER(Listener, listener_list_, OnDetached(old_status)); |
386 } | 420 } |
387 | 421 |
388 bool EmbeddedWorkerInstance::OnMessageReceived(const IPC::Message& message) { | 422 bool EmbeddedWorkerInstance::OnMessageReceived(const IPC::Message& message) { |
389 ListenerList::Iterator it(&listener_list_); | 423 ListenerList::Iterator it(&listener_list_); |
390 while (Listener* listener = it.GetNext()) { | 424 while (Listener* listener = it.GetNext()) { |
391 if (listener->OnMessageReceived(message)) | 425 if (listener->OnMessageReceived(message)) |
392 return true; | 426 return true; |
393 } | 427 } |
394 return false; | 428 return false; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
486 case SCRIPT_EVALUATED: | 520 case SCRIPT_EVALUATED: |
487 return "Script evaluated"; | 521 return "Script evaluated"; |
488 case STARTING_PHASE_MAX_VALUE: | 522 case STARTING_PHASE_MAX_VALUE: |
489 NOTREACHED(); | 523 NOTREACHED(); |
490 } | 524 } |
491 NOTREACHED() << phase; | 525 NOTREACHED() << phase; |
492 return std::string(); | 526 return std::string(); |
493 } | 527 } |
494 | 528 |
495 } // namespace content | 529 } // namespace content |
OLD | NEW |