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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
13 #include "base/threading/non_thread_safe.h" | 13 #include "base/threading/non_thread_safe.h" |
14 #include "base/trace_event/trace_event.h" | 14 #include "base/trace_event/trace_event.h" |
15 #include "content/browser/devtools/service_worker_devtools_manager.h" | 15 #include "content/browser/devtools/service_worker_devtools_manager.h" |
16 #include "content/browser/service_worker/embedded_worker_registry.h" | 16 #include "content/browser/service_worker/embedded_worker_registry.h" |
17 #include "content/browser/service_worker/embedded_worker_status.h" | 17 #include "content/browser/service_worker/embedded_worker_status.h" |
18 #include "content/browser/service_worker/service_worker_context_core.h" | 18 #include "content/browser/service_worker/service_worker_context_core.h" |
19 #include "content/common/content_switches_internal.h" | 19 #include "content/common/content_switches_internal.h" |
20 #include "content/common/service_worker/embedded_worker_messages.h" | 20 #include "content/common/service_worker/embedded_worker_messages.h" |
21 #include "content/common/service_worker/embedded_worker_settings.h" | 21 #include "content/common/service_worker/embedded_worker_settings.h" |
22 #include "content/common/service_worker/embedded_worker_setup.mojom.h" | 22 #include "content/common/service_worker/embedded_worker_setup.mojom.h" |
horo
2017/01/16 04:56:31
remove
shimazu
2017/01/16 05:49:53
Done.
| |
23 #include "content/common/service_worker/embedded_worker_start_params.h" | 23 #include "content/common/service_worker/embedded_worker_start_params.h" |
24 #include "content/common/service_worker/service_worker_types.h" | 24 #include "content/common/service_worker/service_worker_types.h" |
25 #include "content/common/service_worker/service_worker_utils.h" | 25 #include "content/common/service_worker/service_worker_utils.h" |
26 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
27 #include "content/public/browser/content_browser_client.h" | 27 #include "content/public/browser/content_browser_client.h" |
28 #include "content/public/browser/render_process_host.h" | 28 #include "content/public/browser/render_process_host.h" |
29 #include "content/public/common/child_process_host.h" | 29 #include "content/public/common/child_process_host.h" |
30 #include "content/public/common/content_switches.h" | 30 #include "content/public/common/content_switches.h" |
31 #include "ipc/ipc_message.h" | 31 #include "ipc/ipc_message.h" |
32 #include "url/gurl.h" | 32 #include "url/gurl.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
96 is_installed); | 96 is_installed); |
97 if (request.is_pending()) | 97 if (request.is_pending()) |
98 rph->GetRemoteInterfaces()->GetInterface(std::move(request)); | 98 rph->GetRemoteInterfaces()->GetInterface(std::move(request)); |
99 } | 99 } |
100 BrowserThread::PostTask( | 100 BrowserThread::PostTask( |
101 BrowserThread::IO, | 101 BrowserThread::IO, |
102 FROM_HERE, | 102 FROM_HERE, |
103 base::Bind(callback, worker_devtools_agent_route_id, wait_for_debugger)); | 103 base::Bind(callback, worker_devtools_agent_route_id, wait_for_debugger)); |
104 } | 104 } |
105 | 105 |
106 void SetupEventDispatcherOnUIThread( | |
107 int process_id, | |
108 int thread_id, | |
109 mojom::ServiceWorkerEventDispatcherRequest request) { | |
110 DCHECK(!ServiceWorkerUtils::IsMojoForServiceWorkerEnabled()); | |
111 RenderProcessHost* rph = RenderProcessHost::FromID(process_id); | |
112 // |rph| or its InterfaceProvider may be NULL in unit tests. | |
113 if (!rph || !rph->GetRemoteInterfaces()) | |
114 return; | |
115 mojom::EmbeddedWorkerSetupPtr setup; | |
116 rph->GetRemoteInterfaces()->GetInterface(&setup); | |
117 setup->AttachServiceWorkerEventDispatcher(thread_id, std::move(request)); | |
118 } | |
119 | |
120 void CallDetach(EmbeddedWorkerInstance* instance) { | 106 void CallDetach(EmbeddedWorkerInstance* instance) { |
121 // This could be called on the UI thread if |client_| still be valid when the | 107 // This could be called on the UI thread if |client_| still be valid when the |
122 // message loop on the UI thread gets destructed. | 108 // message loop on the UI thread gets destructed. |
123 // TODO(shimazu): Remove this after https://crbug.com/604762 is fixed | 109 // TODO(shimazu): Remove this after https://crbug.com/604762 is fixed |
124 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 110 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
125 DCHECK(ServiceWorkerUtils::IsMojoForServiceWorkerEnabled()); | 111 DCHECK(ServiceWorkerUtils::IsMojoForServiceWorkerEnabled()); |
126 return; | 112 return; |
127 } | 113 } |
128 instance->Detach(); | 114 instance->Detach(); |
129 } | 115 } |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
394 "EmbeddedWorkerInstance::Start", this, | 380 "EmbeddedWorkerInstance::Start", this, |
395 "OnSetupOnUICompleted"); | 381 "OnSetupOnUICompleted"); |
396 | 382 |
397 // Notify the instance that it is registered to the devtools manager. | 383 // Notify the instance that it is registered to the devtools manager. |
398 instance_->OnRegisteredToDevToolsManager( | 384 instance_->OnRegisteredToDevToolsManager( |
399 is_new_process, worker_devtools_agent_route_id, wait_for_debugger); | 385 is_new_process, worker_devtools_agent_route_id, wait_for_debugger); |
400 | 386 |
401 params->worker_devtools_agent_route_id = worker_devtools_agent_route_id; | 387 params->worker_devtools_agent_route_id = worker_devtools_agent_route_id; |
402 params->wait_for_debugger = wait_for_debugger; | 388 params->wait_for_debugger = wait_for_debugger; |
403 | 389 |
404 if (ServiceWorkerUtils::IsMojoForServiceWorkerEnabled()) | 390 SendStartWorker(std::move(params)); |
falken
2017/01/16 05:07:45
Can we collapse SendStartWorker into this function
shimazu
2017/01/16 05:49:53
Done.
| |
405 SendMojoStartWorker(std::move(params)); | |
406 else | |
407 SendStartWorker(std::move(params)); | |
408 } | 391 } |
409 | 392 |
410 void SendStartWorker(std::unique_ptr<EmbeddedWorkerStartParams> params) { | 393 void SendStartWorker(std::unique_ptr<EmbeddedWorkerStartParams> params) { |
411 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 394 ServiceWorkerStatusCode status = |
412 ServiceWorkerStatusCode status = instance_->registry_->SendStartWorker( | 395 instance_->SendStartWorker(std::move(params)); |
413 std::move(params), instance_->process_id()); | |
414 TRACE_EVENT_ASYNC_STEP_PAST1( | |
415 "ServiceWorker", "EmbeddedWorkerInstance::Start", this, | |
416 "SendStartWorker", "Status", ServiceWorkerStatusToString(status)); | |
417 if (status != SERVICE_WORKER_OK) { | 396 if (status != SERVICE_WORKER_OK) { |
418 StatusCallback callback = start_callback_; | 397 StatusCallback callback = start_callback_; |
419 start_callback_.Reset(); | 398 start_callback_.Reset(); |
420 instance_->OnStartFailed(callback, status); | |
421 // |this| may be destroyed. | |
422 return; | |
423 } | |
424 instance_->OnStartWorkerMessageSent(); | |
425 | |
426 // |start_callback_| will be called via RunStartCallback() when the script | |
427 // is evaluated. | |
428 } | |
429 | |
430 void SendMojoStartWorker(std::unique_ptr<EmbeddedWorkerStartParams> params) { | |
431 ServiceWorkerStatusCode status = | |
432 instance_->SendMojoStartWorker(std::move(params)); | |
433 if (status != SERVICE_WORKER_OK) { | |
434 StatusCallback callback = start_callback_; | |
435 start_callback_.Reset(); | |
436 instance_->OnStartFailed(callback, status); | 399 instance_->OnStartFailed(callback, status); |
437 // |this| may be destroyed. | 400 // |this| may be destroyed. |
438 return; | 401 return; |
falken
2017/01/16 05:07:45
This return seems unneeded.
shimazu
2017/01/16 05:49:53
Done.
| |
439 } | 402 } |
440 } | 403 } |
441 | 404 |
442 // |instance_| must outlive |this|. | 405 // |instance_| must outlive |this|. |
443 EmbeddedWorkerInstance* instance_; | 406 EmbeddedWorkerInstance* instance_; |
444 | 407 |
445 // Ownership is transferred by base::Passed() to a task after process | 408 // Ownership is transferred by base::Passed() to a task after process |
446 // allocation. | 409 // allocation. |
447 mojom::EmbeddedWorkerInstanceClientRequest request_; | 410 mojom::EmbeddedWorkerInstanceClientRequest request_; |
448 | 411 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
493 network_accessed_for_script_ = false; | 456 network_accessed_for_script_ = false; |
494 for (auto& observer : listener_list_) | 457 for (auto& observer : listener_list_) |
495 observer.OnStarting(); | 458 observer.OnStarting(); |
496 | 459 |
497 params->embedded_worker_id = embedded_worker_id_; | 460 params->embedded_worker_id = embedded_worker_id_; |
498 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE; | 461 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE; |
499 params->wait_for_debugger = false; | 462 params->wait_for_debugger = false; |
500 params->settings.v8_cache_options = GetV8CacheOptions(); | 463 params->settings.v8_cache_options = GetV8CacheOptions(); |
501 | 464 |
502 mojom::EmbeddedWorkerInstanceClientRequest request; | 465 mojom::EmbeddedWorkerInstanceClientRequest request; |
503 if (ServiceWorkerUtils::IsMojoForServiceWorkerEnabled()) { | 466 request = mojo::MakeRequest(&client_); |
falken
2017/01/16 05:07:45
Can we initialize request in line 465?
shimazu
2017/01/16 05:49:53
Yes, that's right. Done.
| |
504 request = mojo::MakeRequest(&client_); | 467 client_.set_connection_error_handler( |
505 client_.set_connection_error_handler( | 468 base::Bind(&CallDetach, base::Unretained(this))); |
506 base::Bind(&CallDetach, base::Unretained(this))); | |
507 } | |
508 | 469 |
509 pending_dispatcher_request_ = std::move(dispatcher_request); | 470 pending_dispatcher_request_ = std::move(dispatcher_request); |
510 | 471 |
511 inflight_start_task_.reset( | 472 inflight_start_task_.reset( |
512 new StartTask(this, params->script_url, std::move(request))); | 473 new StartTask(this, params->script_url, std::move(request))); |
513 inflight_start_task_->Start(std::move(params), callback); | 474 inflight_start_task_->Start(std::move(params), callback); |
514 } | 475 } |
515 | 476 |
516 bool EmbeddedWorkerInstance::Stop() { | 477 bool EmbeddedWorkerInstance::Stop() { |
517 DCHECK(status_ == EmbeddedWorkerStatus::STARTING || | 478 DCHECK(status_ == EmbeddedWorkerStatus::STARTING || |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
628 } | 589 } |
629 if (wait_for_debugger) { | 590 if (wait_for_debugger) { |
630 // We don't measure the start time when wait_for_debugger flag is set. So | 591 // We don't measure the start time when wait_for_debugger flag is set. So |
631 // we set the NULL time here. | 592 // we set the NULL time here. |
632 step_time_ = base::TimeTicks(); | 593 step_time_ = base::TimeTicks(); |
633 } | 594 } |
634 for (auto& observer : listener_list_) | 595 for (auto& observer : listener_list_) |
635 observer.OnRegisteredToDevToolsManager(); | 596 observer.OnRegisteredToDevToolsManager(); |
636 } | 597 } |
637 | 598 |
638 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendMojoStartWorker( | 599 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendStartWorker( |
639 std::unique_ptr<EmbeddedWorkerStartParams> params) { | 600 std::unique_ptr<EmbeddedWorkerStartParams> params) { |
640 if (!context_) | 601 if (!context_) |
641 return SERVICE_WORKER_ERROR_ABORT; | 602 return SERVICE_WORKER_ERROR_ABORT; |
642 DCHECK(pending_dispatcher_request_.is_pending()); | 603 DCHECK(pending_dispatcher_request_.is_pending()); |
643 client_->StartWorker(*params, std::move(pending_dispatcher_request_)); | 604 client_->StartWorker(*params, std::move(pending_dispatcher_request_)); |
644 registry_->BindWorkerToProcess(process_id(), embedded_worker_id()); | 605 registry_->BindWorkerToProcess(process_id(), embedded_worker_id()); |
645 TRACE_EVENT_ASYNC_STEP_PAST1("ServiceWorker", "EmbeddedWorkerInstance::Start", | 606 TRACE_EVENT_ASYNC_STEP_PAST1("ServiceWorker", "EmbeddedWorkerInstance::Start", |
646 this, "SendStartWorker", "Status", "mojo"); | 607 this, "SendStartWorker", "Status", "mojo"); |
647 OnStartWorkerMessageSent(); | 608 OnStartWorkerMessageSent(); |
648 return SERVICE_WORKER_OK; | 609 return SERVICE_WORKER_OK; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
736 starting_phase_ = THREAD_STARTED; | 697 starting_phase_ = THREAD_STARTED; |
737 if (!step_time_.is_null()) { | 698 if (!step_time_.is_null()) { |
738 base::TimeDelta duration = UpdateStepTime(); | 699 base::TimeDelta duration = UpdateStepTime(); |
739 if (inflight_start_task_->is_installed()) | 700 if (inflight_start_task_->is_installed()) |
740 ServiceWorkerMetrics::RecordTimeToStartThread(duration, start_situation_); | 701 ServiceWorkerMetrics::RecordTimeToStartThread(duration, start_situation_); |
741 } | 702 } |
742 | 703 |
743 thread_id_ = thread_id; | 704 thread_id_ = thread_id; |
744 for (auto& observer : listener_list_) | 705 for (auto& observer : listener_list_) |
745 observer.OnThreadStarted(); | 706 observer.OnThreadStarted(); |
746 | |
747 // The pending request is sent at StartWorker if mojo for the service worker | |
748 // is enabled. | |
749 if (!ServiceWorkerUtils::IsMojoForServiceWorkerEnabled()) { | |
750 DCHECK(pending_dispatcher_request_.is_pending()); | |
751 BrowserThread::PostTask( | |
752 BrowserThread::UI, FROM_HERE, | |
753 base::Bind(SetupEventDispatcherOnUIThread, process_id(), thread_id_, | |
754 base::Passed(&pending_dispatcher_request_))); | |
755 } | |
756 } | 707 } |
757 | 708 |
758 void EmbeddedWorkerInstance::OnScriptLoadFailed() { | 709 void EmbeddedWorkerInstance::OnScriptLoadFailed() { |
759 if (!inflight_start_task_) | 710 if (!inflight_start_task_) |
760 return; | 711 return; |
761 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", "EmbeddedWorkerInstance::Start", | 712 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", "EmbeddedWorkerInstance::Start", |
762 inflight_start_task_.get(), | 713 inflight_start_task_.get(), |
763 "OnScriptLoadFailed"); | 714 "OnScriptLoadFailed"); |
764 for (auto& observer : listener_list_) | 715 for (auto& observer : listener_list_) |
765 observer.OnScriptLoadFailed(); | 716 observer.OnScriptLoadFailed(); |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
973 case SCRIPT_READ_FINISHED: | 924 case SCRIPT_READ_FINISHED: |
974 return "Script read finished"; | 925 return "Script read finished"; |
975 case STARTING_PHASE_MAX_VALUE: | 926 case STARTING_PHASE_MAX_VALUE: |
976 NOTREACHED(); | 927 NOTREACHED(); |
977 } | 928 } |
978 NOTREACHED() << phase; | 929 NOTREACHED() << phase; |
979 return std::string(); | 930 return std::string(); |
980 } | 931 } |
981 | 932 |
982 } // namespace content | 933 } // namespace content |
OLD | NEW |