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