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" |
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 status_ == EmbeddedWorkerStatus::STOPPED) | 435 status_ == EmbeddedWorkerStatus::STOPPED) |
436 << static_cast<int>(status_); | 436 << static_cast<int>(status_); |
437 devtools_proxy_.reset(); | 437 devtools_proxy_.reset(); |
438 if (registry_->GetWorker(embedded_worker_id_)) | 438 if (registry_->GetWorker(embedded_worker_id_)) |
439 registry_->RemoveWorker(process_id(), embedded_worker_id_); | 439 registry_->RemoveWorker(process_id(), embedded_worker_id_); |
440 process_handle_.reset(); | 440 process_handle_.reset(); |
441 } | 441 } |
442 | 442 |
443 void EmbeddedWorkerInstance::Start( | 443 void EmbeddedWorkerInstance::Start( |
444 std::unique_ptr<EmbeddedWorkerStartParams> params, | 444 std::unique_ptr<EmbeddedWorkerStartParams> params, |
| 445 ProviderInfoGetter provider_info_getter, |
445 mojom::ServiceWorkerEventDispatcherRequest dispatcher_request, | 446 mojom::ServiceWorkerEventDispatcherRequest dispatcher_request, |
446 const StatusCallback& callback) { | 447 const StatusCallback& callback) { |
447 restart_count_++; | 448 restart_count_++; |
448 if (!context_) { | 449 if (!context_) { |
449 callback.Run(SERVICE_WORKER_ERROR_ABORT); | 450 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
450 // |this| may be destroyed by the callback. | 451 // |this| may be destroyed by the callback. |
451 return; | 452 return; |
452 } | 453 } |
453 DCHECK(status_ == EmbeddedWorkerStatus::STOPPED); | 454 DCHECK(status_ == EmbeddedWorkerStatus::STOPPED); |
454 | 455 |
455 DCHECK(!params->pause_after_download || !params->is_installed); | 456 DCHECK(!params->pause_after_download || !params->is_installed); |
456 DCHECK_NE(kInvalidServiceWorkerVersionId, params->service_worker_version_id); | 457 DCHECK_NE(kInvalidServiceWorkerVersionId, params->service_worker_version_id); |
457 step_time_ = base::TimeTicks::Now(); | 458 step_time_ = base::TimeTicks::Now(); |
458 status_ = EmbeddedWorkerStatus::STARTING; | 459 status_ = EmbeddedWorkerStatus::STARTING; |
459 starting_phase_ = ALLOCATING_PROCESS; | 460 starting_phase_ = ALLOCATING_PROCESS; |
460 network_accessed_for_script_ = false; | 461 network_accessed_for_script_ = false; |
| 462 provider_info_getter_ = std::move(provider_info_getter); |
| 463 |
461 for (auto& observer : listener_list_) | 464 for (auto& observer : listener_list_) |
462 observer.OnStarting(); | 465 observer.OnStarting(); |
463 | 466 |
464 params->embedded_worker_id = embedded_worker_id_; | 467 params->embedded_worker_id = embedded_worker_id_; |
465 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE; | 468 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE; |
466 params->wait_for_debugger = false; | 469 params->wait_for_debugger = false; |
467 params->settings.v8_cache_options = GetV8CacheOptions(); | 470 params->settings.v8_cache_options = GetV8CacheOptions(); |
468 | 471 |
469 mojom::EmbeddedWorkerInstanceClientRequest request = | 472 mojom::EmbeddedWorkerInstanceClientRequest request = |
470 mojo::MakeRequest(&client_); | 473 mojo::MakeRequest(&client_); |
471 client_.set_connection_error_handler( | 474 client_.set_connection_error_handler( |
472 base::Bind(&CallDetach, base::Unretained(this))); | 475 base::Bind(&CallDetach, base::Unretained(this))); |
473 | |
474 pending_dispatcher_request_ = std::move(dispatcher_request); | 476 pending_dispatcher_request_ = std::move(dispatcher_request); |
475 | 477 |
476 inflight_start_task_.reset( | 478 inflight_start_task_.reset( |
477 new StartTask(this, params->script_url, std::move(request))); | 479 new StartTask(this, params->script_url, std::move(request))); |
478 inflight_start_task_->Start(std::move(params), callback); | 480 inflight_start_task_->Start(std::move(params), callback); |
479 } | 481 } |
480 | 482 |
481 bool EmbeddedWorkerInstance::Stop() { | 483 bool EmbeddedWorkerInstance::Stop() { |
482 DCHECK(status_ == EmbeddedWorkerStatus::STARTING || | 484 DCHECK(status_ == EmbeddedWorkerStatus::STARTING || |
483 status_ == EmbeddedWorkerStatus::RUNNING) | 485 status_ == EmbeddedWorkerStatus::RUNNING) |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
582 step_time_ = base::TimeTicks(); | 584 step_time_ = base::TimeTicks(); |
583 } | 585 } |
584 for (auto& observer : listener_list_) | 586 for (auto& observer : listener_list_) |
585 observer.OnRegisteredToDevToolsManager(); | 587 observer.OnRegisteredToDevToolsManager(); |
586 } | 588 } |
587 | 589 |
588 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendStartWorker( | 590 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendStartWorker( |
589 std::unique_ptr<EmbeddedWorkerStartParams> params) { | 591 std::unique_ptr<EmbeddedWorkerStartParams> params) { |
590 if (!context_) | 592 if (!context_) |
591 return SERVICE_WORKER_ERROR_ABORT; | 593 return SERVICE_WORKER_ERROR_ABORT; |
| 594 DCHECK(pending_dispatcher_request_.is_pending()); |
592 | 595 |
593 DCHECK(!instance_host_binding_.is_bound()); | 596 DCHECK(!instance_host_binding_.is_bound()); |
594 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo host_ptr_info; | 597 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo host_ptr_info; |
595 instance_host_binding_.Bind(mojo::MakeRequest(&host_ptr_info)); | 598 instance_host_binding_.Bind(mojo::MakeRequest(&host_ptr_info)); |
596 | 599 |
597 DCHECK(pending_dispatcher_request_.is_pending()); | 600 mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info = |
| 601 std::move(provider_info_getter_).Run(process_id()); |
| 602 |
598 client_->StartWorker(*params, std::move(pending_dispatcher_request_), | 603 client_->StartWorker(*params, std::move(pending_dispatcher_request_), |
599 std::move(host_ptr_info)); | 604 std::move(host_ptr_info), std::move(provider_info)); |
600 registry_->BindWorkerToProcess(process_id(), embedded_worker_id()); | 605 registry_->BindWorkerToProcess(process_id(), embedded_worker_id()); |
| 606 |
601 OnStartWorkerMessageSent(); | 607 OnStartWorkerMessageSent(); |
602 // Once the start worker message is received, renderer side will prepare a | 608 // Once the start worker message is received, renderer side will prepare a |
603 // shadow page for getting worker script. | 609 // shadow page for getting worker script. |
604 TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("ServiceWorker", "PREPARING_SCRIPT_LOAD", | 610 TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("ServiceWorker", "PREPARING_SCRIPT_LOAD", |
605 this); | 611 this); |
606 return SERVICE_WORKER_OK; | 612 return SERVICE_WORKER_OK; |
607 } | 613 } |
608 | 614 |
609 void EmbeddedWorkerInstance::OnStartWorkerMessageSent() { | 615 void EmbeddedWorkerInstance::OnStartWorkerMessageSent() { |
610 if (!step_time_.is_null()) { | 616 if (!step_time_.is_null()) { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
694 void EmbeddedWorkerInstance::OnWorkerVersionInstalled() { | 700 void EmbeddedWorkerInstance::OnWorkerVersionInstalled() { |
695 if (devtools_proxy_) | 701 if (devtools_proxy_) |
696 devtools_proxy_->NotifyWorkerVersionInstalled(); | 702 devtools_proxy_->NotifyWorkerVersionInstalled(); |
697 } | 703 } |
698 | 704 |
699 void EmbeddedWorkerInstance::OnWorkerVersionDoomed() { | 705 void EmbeddedWorkerInstance::OnWorkerVersionDoomed() { |
700 if (devtools_proxy_) | 706 if (devtools_proxy_) |
701 devtools_proxy_->NotifyWorkerVersionDoomed(); | 707 devtools_proxy_->NotifyWorkerVersionDoomed(); |
702 } | 708 } |
703 | 709 |
704 void EmbeddedWorkerInstance::OnThreadStarted(int thread_id, int provider_id) { | 710 void EmbeddedWorkerInstance::OnThreadStarted(int thread_id) { |
705 if (!context_ || !inflight_start_task_) | 711 if (!context_ || !inflight_start_task_) |
706 return; | 712 return; |
707 | 713 |
708 ServiceWorkerProviderHost* provider_host = | |
709 context_->GetProviderHost(process_id(), provider_id); | |
710 if (!provider_host) { | |
711 bad_message::ReceivedBadMessage( | |
712 process_id(), bad_message::SWDH_WORKER_SCRIPT_LOAD_NO_HOST); | |
713 return; | |
714 } | |
715 | |
716 provider_host->SetReadyToSendMessagesToWorker(thread_id); | |
717 | |
718 TRACE_EVENT_NESTABLE_ASYNC_END0("ServiceWorker", "LAUNCHING_WORKER_THREAD", | 714 TRACE_EVENT_NESTABLE_ASYNC_END0("ServiceWorker", "LAUNCHING_WORKER_THREAD", |
719 this); | 715 this); |
720 // Renderer side has started to evaluate the loaded worker script. | 716 // Renderer side has started to evaluate the loaded worker script. |
721 TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("ServiceWorker", "EVALUATING_SCRIPT", this); | 717 TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("ServiceWorker", "EVALUATING_SCRIPT", this); |
722 starting_phase_ = THREAD_STARTED; | 718 starting_phase_ = THREAD_STARTED; |
723 if (!step_time_.is_null()) { | 719 if (!step_time_.is_null()) { |
724 base::TimeDelta duration = UpdateStepTime(); | 720 base::TimeDelta duration = UpdateStepTime(); |
725 if (inflight_start_task_->is_installed()) | 721 if (inflight_start_task_->is_installed()) |
726 ServiceWorkerMetrics::RecordTimeToStartThread(duration, start_situation_); | 722 ServiceWorkerMetrics::RecordTimeToStartThread(duration, start_situation_); |
727 } | 723 } |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
804 } | 800 } |
805 | 801 |
806 void EmbeddedWorkerInstance::OnDetached() { | 802 void EmbeddedWorkerInstance::OnDetached() { |
807 EmbeddedWorkerStatus old_status = status_; | 803 EmbeddedWorkerStatus old_status = status_; |
808 ReleaseProcess(); | 804 ReleaseProcess(); |
809 for (auto& observer : listener_list_) | 805 for (auto& observer : listener_list_) |
810 observer.OnDetached(old_status); | 806 observer.OnDetached(old_status); |
811 } | 807 } |
812 | 808 |
813 void EmbeddedWorkerInstance::Detach() { | 809 void EmbeddedWorkerInstance::Detach() { |
| 810 if (status() == EmbeddedWorkerStatus::STOPPED) |
| 811 return; |
814 registry_->DetachWorker(process_id(), embedded_worker_id()); | 812 registry_->DetachWorker(process_id(), embedded_worker_id()); |
815 OnDetached(); | 813 OnDetached(); |
816 } | 814 } |
817 | 815 |
818 base::WeakPtr<EmbeddedWorkerInstance> EmbeddedWorkerInstance::AsWeakPtr() { | 816 base::WeakPtr<EmbeddedWorkerInstance> EmbeddedWorkerInstance::AsWeakPtr() { |
819 return weak_factory_.GetWeakPtr(); | 817 return weak_factory_.GetWeakPtr(); |
820 } | 818 } |
821 | 819 |
822 bool EmbeddedWorkerInstance::OnMessageReceived(const IPC::Message& message) { | 820 bool EmbeddedWorkerInstance::OnMessageReceived(const IPC::Message& message) { |
823 for (auto& listener : listener_list_) { | 821 for (auto& listener : listener_list_) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
885 TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("ServiceWorker", "SCRIPT_DOWNLOADING", | 883 TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("ServiceWorker", "SCRIPT_DOWNLOADING", |
886 this); | 884 this); |
887 starting_phase_ = SCRIPT_DOWNLOADING; | 885 starting_phase_ = SCRIPT_DOWNLOADING; |
888 network_accessed_for_script_ = true; | 886 network_accessed_for_script_ = true; |
889 } | 887 } |
890 | 888 |
891 void EmbeddedWorkerInstance::ReleaseProcess() { | 889 void EmbeddedWorkerInstance::ReleaseProcess() { |
892 // Abort an inflight start task. | 890 // Abort an inflight start task. |
893 inflight_start_task_.reset(); | 891 inflight_start_task_.reset(); |
894 | 892 |
895 client_.reset(); | |
896 instance_host_binding_.Close(); | 893 instance_host_binding_.Close(); |
897 devtools_proxy_.reset(); | 894 devtools_proxy_.reset(); |
898 process_handle_.reset(); | 895 process_handle_.reset(); |
899 status_ = EmbeddedWorkerStatus::STOPPED; | 896 status_ = EmbeddedWorkerStatus::STOPPED; |
900 starting_phase_ = NOT_STARTING; | 897 starting_phase_ = NOT_STARTING; |
901 thread_id_ = kInvalidEmbeddedWorkerThreadId; | 898 thread_id_ = kInvalidEmbeddedWorkerThreadId; |
902 } | 899 } |
903 | 900 |
904 void EmbeddedWorkerInstance::OnStartFailed(const StatusCallback& callback, | 901 void EmbeddedWorkerInstance::OnStartFailed(const StatusCallback& callback, |
905 ServiceWorkerStatusCode status) { | 902 ServiceWorkerStatusCode status) { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
972 case SCRIPT_READ_FINISHED: | 969 case SCRIPT_READ_FINISHED: |
973 return "Script read finished"; | 970 return "Script read finished"; |
974 case STARTING_PHASE_MAX_VALUE: | 971 case STARTING_PHASE_MAX_VALUE: |
975 NOTREACHED(); | 972 NOTREACHED(); |
976 } | 973 } |
977 NOTREACHED() << phase; | 974 NOTREACHED() << phase; |
978 return std::string(); | 975 return std::string(); |
979 } | 976 } |
980 | 977 |
981 } // namespace content | 978 } // namespace content |
OLD | NEW |