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/bad_message.h" | |
15 #include "content/browser/devtools/service_worker_devtools_manager.h" | 16 #include "content/browser/devtools/service_worker_devtools_manager.h" |
16 #include "content/browser/service_worker/embedded_worker_registry.h" | 17 #include "content/browser/service_worker/embedded_worker_registry.h" |
17 #include "content/browser/service_worker/embedded_worker_status.h" | 18 #include "content/browser/service_worker/embedded_worker_status.h" |
18 #include "content/browser/service_worker/service_worker_context_core.h" | 19 #include "content/browser/service_worker/service_worker_context_core.h" |
19 #include "content/common/content_switches_internal.h" | 20 #include "content/common/content_switches_internal.h" |
20 #include "content/common/service_worker/embedded_worker_messages.h" | 21 #include "content/common/service_worker/embedded_worker_messages.h" |
21 #include "content/common/service_worker/embedded_worker_settings.h" | 22 #include "content/common/service_worker/embedded_worker_settings.h" |
22 #include "content/common/service_worker/embedded_worker_start_params.h" | 23 #include "content/common/service_worker/embedded_worker_start_params.h" |
23 #include "content/common/service_worker/service_worker_types.h" | 24 #include "content/common/service_worker/service_worker_types.h" |
24 #include "content/common/service_worker/service_worker_utils.h" | 25 #include "content/common/service_worker/service_worker_utils.h" |
(...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
480 if (ServiceWorkerUtils::IsMojoForServiceWorkerEnabled()) { | 481 if (ServiceWorkerUtils::IsMojoForServiceWorkerEnabled()) { |
481 if (status_ == EmbeddedWorkerStatus::STARTING && | 482 if (status_ == EmbeddedWorkerStatus::STARTING && |
482 !HasSentStartWorker(starting_phase())) { | 483 !HasSentStartWorker(starting_phase())) { |
483 // Don't send the StopWorker message when the StartWorker message hasn't | 484 // Don't send the StopWorker message when the StartWorker message hasn't |
484 // been sent. | 485 // been sent. |
485 // TODO(shimazu): Invoke OnStopping/OnStopped after the legacy IPC path is | 486 // TODO(shimazu): Invoke OnStopping/OnStopped after the legacy IPC path is |
486 // removed. | 487 // removed. |
487 OnDetached(); | 488 OnDetached(); |
488 return false; | 489 return false; |
489 } | 490 } |
490 client_->StopWorker(base::Bind(&EmbeddedWorkerRegistry::OnWorkerStopped, | 491 client_->StopWorker( |
491 base::Unretained(registry_.get()), | 492 base::Bind(&EmbeddedWorkerInstance::OnStopped, base::Unretained(this))); |
492 process_id(), embedded_worker_id())); | |
493 } else { | 493 } else { |
494 ServiceWorkerStatusCode status = | 494 ServiceWorkerStatusCode status = |
495 registry_->StopWorker(process_id(), embedded_worker_id_); | 495 registry_->StopWorker(process_id(), embedded_worker_id_); |
496 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.SendStopWorker.Status", status, | 496 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.SendStopWorker.Status", status, |
497 SERVICE_WORKER_ERROR_MAX_VALUE); | 497 SERVICE_WORKER_ERROR_MAX_VALUE); |
498 // StopWorker could fail if we were starting up and don't have a process | 498 // StopWorker could fail if we were starting up and don't have a process |
499 // yet, or we can no longer communicate with the process. So just detach. | 499 // yet, or we can no longer communicate with the process. So just detach. |
500 if (status != SERVICE_WORKER_OK) { | 500 if (status != SERVICE_WORKER_OK) { |
501 OnDetached(); | 501 OnDetached(); |
502 return false; | 502 return false; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
549 EmbeddedWorkerInstance::EmbeddedWorkerInstance( | 549 EmbeddedWorkerInstance::EmbeddedWorkerInstance( |
550 base::WeakPtr<ServiceWorkerContextCore> context, | 550 base::WeakPtr<ServiceWorkerContextCore> context, |
551 int embedded_worker_id) | 551 int embedded_worker_id) |
552 : context_(context), | 552 : context_(context), |
553 registry_(context->embedded_worker_registry()), | 553 registry_(context->embedded_worker_registry()), |
554 embedded_worker_id_(embedded_worker_id), | 554 embedded_worker_id_(embedded_worker_id), |
555 status_(EmbeddedWorkerStatus::STOPPED), | 555 status_(EmbeddedWorkerStatus::STOPPED), |
556 starting_phase_(NOT_STARTING), | 556 starting_phase_(NOT_STARTING), |
557 restart_count_(0), | 557 restart_count_(0), |
558 thread_id_(kInvalidEmbeddedWorkerThreadId), | 558 thread_id_(kInvalidEmbeddedWorkerThreadId), |
559 instance_host_binding_(this), | |
559 devtools_attached_(false), | 560 devtools_attached_(false), |
560 network_accessed_for_script_(false), | 561 network_accessed_for_script_(false), |
561 weak_factory_(this) {} | 562 weak_factory_(this) {} |
562 | 563 |
563 void EmbeddedWorkerInstance::OnProcessAllocated( | 564 void EmbeddedWorkerInstance::OnProcessAllocated( |
564 std::unique_ptr<WorkerProcessHandle> handle, | 565 std::unique_ptr<WorkerProcessHandle> handle, |
565 ServiceWorkerMetrics::StartSituation start_situation) { | 566 ServiceWorkerMetrics::StartSituation start_situation) { |
566 DCHECK_EQ(EmbeddedWorkerStatus::STARTING, status_); | 567 DCHECK_EQ(EmbeddedWorkerStatus::STARTING, status_); |
567 DCHECK(!process_handle_); | 568 DCHECK(!process_handle_); |
568 | 569 |
(...skipping 19 matching lines...) Expand all Loading... | |
588 step_time_ = base::TimeTicks(); | 589 step_time_ = base::TimeTicks(); |
589 } | 590 } |
590 for (auto& observer : listener_list_) | 591 for (auto& observer : listener_list_) |
591 observer.OnRegisteredToDevToolsManager(); | 592 observer.OnRegisteredToDevToolsManager(); |
592 } | 593 } |
593 | 594 |
594 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendStartWorker( | 595 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendStartWorker( |
595 std::unique_ptr<EmbeddedWorkerStartParams> params) { | 596 std::unique_ptr<EmbeddedWorkerStartParams> params) { |
596 if (!context_) | 597 if (!context_) |
597 return SERVICE_WORKER_ERROR_ABORT; | 598 return SERVICE_WORKER_ERROR_ABORT; |
599 | |
600 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo host_ptr_info; | |
601 instance_host_binding_.Bind(&host_ptr_info); | |
602 | |
598 DCHECK(pending_dispatcher_request_.is_pending()); | 603 DCHECK(pending_dispatcher_request_.is_pending()); |
599 client_->StartWorker(*params, std::move(pending_dispatcher_request_)); | 604 client_->StartWorker(*params, std::move(pending_dispatcher_request_), |
605 std::move(host_ptr_info)); | |
600 registry_->BindWorkerToProcess(process_id(), embedded_worker_id()); | 606 registry_->BindWorkerToProcess(process_id(), embedded_worker_id()); |
601 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", "EmbeddedWorkerInstance::Start", | 607 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", "EmbeddedWorkerInstance::Start", |
602 this, "SendStartWorker"); | 608 this, "SendStartWorker"); |
603 OnStartWorkerMessageSent(); | 609 OnStartWorkerMessageSent(); |
604 return SERVICE_WORKER_OK; | 610 return SERVICE_WORKER_OK; |
605 } | 611 } |
606 | 612 |
607 void EmbeddedWorkerInstance::OnStartWorkerMessageSent() { | 613 void EmbeddedWorkerInstance::OnStartWorkerMessageSent() { |
608 if (!step_time_.is_null()) { | 614 if (!step_time_.is_null()) { |
609 base::TimeDelta duration = UpdateStepTime(); | 615 base::TimeDelta duration = UpdateStepTime(); |
610 if (inflight_start_task_->is_installed()) { | 616 if (inflight_start_task_->is_installed()) { |
611 ServiceWorkerMetrics::RecordTimeToSendStartWorker(duration, | 617 ServiceWorkerMetrics::RecordTimeToSendStartWorker(duration, |
612 start_situation_); | 618 start_situation_); |
613 } | 619 } |
614 } | 620 } |
615 | 621 |
616 starting_phase_ = SENT_START_WORKER; | 622 starting_phase_ = SENT_START_WORKER; |
617 for (auto& observer : listener_list_) | 623 for (auto& observer : listener_list_) |
618 observer.OnStartWorkerMessageSent(); | 624 observer.OnStartWorkerMessageSent(); |
619 } | 625 } |
620 | 626 |
621 void EmbeddedWorkerInstance::OnReadyForInspection() { | 627 void EmbeddedWorkerInstance::OnReadyForInspection() { |
628 TRACE_EVENT0("ServiceWorker", "EmbeddedWorkerInstance::OnReadyForInspection"); | |
622 if (devtools_proxy_) | 629 if (devtools_proxy_) |
623 devtools_proxy_->NotifyWorkerReadyForInspection(); | 630 devtools_proxy_->NotifyWorkerReadyForInspection(); |
624 } | 631 } |
625 | 632 |
626 void EmbeddedWorkerInstance::OnScriptReadStarted() { | 633 void EmbeddedWorkerInstance::OnScriptReadStarted() { |
627 starting_phase_ = SCRIPT_READ_STARTED; | 634 starting_phase_ = SCRIPT_READ_STARTED; |
628 } | 635 } |
629 | 636 |
630 void EmbeddedWorkerInstance::OnScriptReadFinished() { | 637 void EmbeddedWorkerInstance::OnScriptReadFinished() { |
631 starting_phase_ = SCRIPT_READ_FINISHED; | 638 starting_phase_ = SCRIPT_READ_FINISHED; |
632 } | 639 } |
633 | 640 |
634 void EmbeddedWorkerInstance::OnScriptLoaded() { | 641 void EmbeddedWorkerInstance::OnScriptLoaded() { |
635 using LoadSource = ServiceWorkerMetrics::LoadSource; | 642 using LoadSource = ServiceWorkerMetrics::LoadSource; |
636 | 643 |
644 TRACE_EVENT0("ServiceWorker", "EmbeddedWorkerInstance::OnScriptLoaded"); | |
645 | |
637 if (!inflight_start_task_) | 646 if (!inflight_start_task_) |
638 return; | 647 return; |
639 LoadSource source; | 648 LoadSource source; |
640 if (network_accessed_for_script_) { | 649 if (network_accessed_for_script_) { |
641 DCHECK(!inflight_start_task_->is_installed()); | 650 DCHECK(!inflight_start_task_->is_installed()); |
642 source = LoadSource::NETWORK; | 651 source = LoadSource::NETWORK; |
643 } else if (inflight_start_task_->is_installed()) { | 652 } else if (inflight_start_task_->is_installed()) { |
644 source = LoadSource::SERVICE_WORKER_STORAGE; | 653 source = LoadSource::SERVICE_WORKER_STORAGE; |
645 } else { | 654 } else { |
646 source = LoadSource::HTTP_CACHE; | 655 source = LoadSource::HTTP_CACHE; |
(...skipping 29 matching lines...) Expand all Loading... | |
676 void EmbeddedWorkerInstance::OnWorkerVersionInstalled() { | 685 void EmbeddedWorkerInstance::OnWorkerVersionInstalled() { |
677 if (devtools_proxy_) | 686 if (devtools_proxy_) |
678 devtools_proxy_->NotifyWorkerVersionInstalled(); | 687 devtools_proxy_->NotifyWorkerVersionInstalled(); |
679 } | 688 } |
680 | 689 |
681 void EmbeddedWorkerInstance::OnWorkerVersionDoomed() { | 690 void EmbeddedWorkerInstance::OnWorkerVersionDoomed() { |
682 if (devtools_proxy_) | 691 if (devtools_proxy_) |
683 devtools_proxy_->NotifyWorkerVersionDoomed(); | 692 devtools_proxy_->NotifyWorkerVersionDoomed(); |
684 } | 693 } |
685 | 694 |
686 void EmbeddedWorkerInstance::OnThreadStarted(int thread_id) { | 695 void EmbeddedWorkerInstance::OnThreadStarted(int thread_id, int provider_id) { |
696 TRACE_EVENT0("ServiceWorker", "EmbeddedWorkerInstance::OnThreadStarted"); | |
697 if (!context_) | |
698 return; | |
699 | |
700 ServiceWorkerProviderHost* provider_host = | |
701 context_->GetProviderHost(process_id(), provider_id); | |
702 if (!provider_host) { | |
703 bad_message::ReceivedBadMessage( | |
704 process_id(), bad_message::SWDH_WORKER_SCRIPT_LOAD_NO_HOST); | |
705 return; | |
706 } | |
707 | |
708 provider_host->SetReadyToSendMessagesToWorker(thread_id); | |
709 | |
687 if (!inflight_start_task_) | 710 if (!inflight_start_task_) |
688 return; | 711 return; |
689 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", "EmbeddedWorkerInstance::Start", | 712 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", "EmbeddedWorkerInstance::Start", |
690 inflight_start_task_.get(), "OnThreadStarted"); | 713 inflight_start_task_.get(), "OnThreadStarted"); |
691 | 714 |
692 starting_phase_ = THREAD_STARTED; | 715 starting_phase_ = THREAD_STARTED; |
693 if (!step_time_.is_null()) { | 716 if (!step_time_.is_null()) { |
694 base::TimeDelta duration = UpdateStepTime(); | 717 base::TimeDelta duration = UpdateStepTime(); |
695 if (inflight_start_task_->is_installed()) | 718 if (inflight_start_task_->is_installed()) |
696 ServiceWorkerMetrics::RecordTimeToStartThread(duration, start_situation_); | 719 ServiceWorkerMetrics::RecordTimeToStartThread(duration, start_situation_); |
697 } | 720 } |
698 | 721 |
699 thread_id_ = thread_id; | 722 thread_id_ = thread_id; |
700 for (auto& observer : listener_list_) | 723 for (auto& observer : listener_list_) |
701 observer.OnThreadStarted(); | 724 observer.OnThreadStarted(); |
702 } | 725 } |
703 | 726 |
704 void EmbeddedWorkerInstance::OnScriptLoadFailed() { | 727 void EmbeddedWorkerInstance::OnScriptLoadFailed() { |
728 TRACE_EVENT0("ServiceWorker", "EmbeddedWorkerInstance::OnScriptLoadFailed"); | |
705 if (!inflight_start_task_) | 729 if (!inflight_start_task_) |
706 return; | 730 return; |
707 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", "EmbeddedWorkerInstance::Start", | 731 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", "EmbeddedWorkerInstance::Start", |
708 inflight_start_task_.get(), | 732 inflight_start_task_.get(), |
709 "OnScriptLoadFailed"); | 733 "OnScriptLoadFailed"); |
710 for (auto& observer : listener_list_) | 734 for (auto& observer : listener_list_) |
711 observer.OnScriptLoadFailed(); | 735 observer.OnScriptLoadFailed(); |
712 } | 736 } |
713 | 737 |
714 void EmbeddedWorkerInstance::OnScriptEvaluated(bool success) { | 738 void EmbeddedWorkerInstance::OnScriptEvaluated(bool success) { |
739 TRACE_EVENT0("ServiceWorker", "EmbeddedWorkerInstance::OnScriptEvaluated"); | |
715 if (!inflight_start_task_) | 740 if (!inflight_start_task_) |
716 return; | 741 return; |
717 DCHECK_EQ(EmbeddedWorkerStatus::STARTING, status_); | 742 DCHECK_EQ(EmbeddedWorkerStatus::STARTING, status_); |
718 | 743 |
719 TRACE_EVENT_ASYNC_STEP_PAST1("ServiceWorker", "EmbeddedWorkerInstance::Start", | 744 TRACE_EVENT_ASYNC_STEP_PAST1("ServiceWorker", "EmbeddedWorkerInstance::Start", |
720 inflight_start_task_.get(), "OnScriptEvaluated", | 745 inflight_start_task_.get(), "OnScriptEvaluated", |
721 "Success", success); | 746 "Success", success); |
722 starting_phase_ = SCRIPT_EVALUATED; | 747 starting_phase_ = SCRIPT_EVALUATED; |
723 if (!step_time_.is_null()) { | 748 if (!step_time_.is_null()) { |
724 base::TimeDelta duration = UpdateStepTime(); | 749 base::TimeDelta duration = UpdateStepTime(); |
725 if (success && inflight_start_task_->is_installed()) | 750 if (success && inflight_start_task_->is_installed()) |
726 ServiceWorkerMetrics::RecordTimeToEvaluateScript(duration, | 751 ServiceWorkerMetrics::RecordTimeToEvaluateScript(duration, |
727 start_situation_); | 752 start_situation_); |
728 } | 753 } |
729 | 754 |
730 base::WeakPtr<EmbeddedWorkerInstance> weak_this = weak_factory_.GetWeakPtr(); | 755 base::WeakPtr<EmbeddedWorkerInstance> weak_this = weak_factory_.GetWeakPtr(); |
731 StartTask::RunStartCallback( | 756 StartTask::RunStartCallback( |
732 inflight_start_task_.get(), | 757 inflight_start_task_.get(), |
733 success ? SERVICE_WORKER_OK | 758 success ? SERVICE_WORKER_OK |
734 : SERVICE_WORKER_ERROR_SCRIPT_EVALUATE_FAILED); | 759 : SERVICE_WORKER_ERROR_SCRIPT_EVALUATE_FAILED); |
735 // |this| may be destroyed by the callback. | 760 // |this| may be destroyed by the callback. |
736 } | 761 } |
737 | 762 |
738 void EmbeddedWorkerInstance::OnStarted() { | 763 void EmbeddedWorkerInstance::OnStarted() { |
764 TRACE_EVENT0("ServiceWorker", "EmbeddedWorkerInstance::OnStarted"); | |
765 if (!registry_->OnWorkerStarted(process_id(), embedded_worker_id_)) | |
leonhsl(Using Gerrit)
2017/04/04 07:14:04
Need to do some necessary work in registry.
| |
766 return; | |
739 // Stop is requested before OnStarted is sent back from the worker. | 767 // Stop is requested before OnStarted is sent back from the worker. |
740 if (status_ == EmbeddedWorkerStatus::STOPPING) | 768 if (status_ == EmbeddedWorkerStatus::STOPPING) |
741 return; | 769 return; |
742 DCHECK(status_ == EmbeddedWorkerStatus::STARTING); | 770 DCHECK(status_ == EmbeddedWorkerStatus::STARTING); |
743 status_ = EmbeddedWorkerStatus::RUNNING; | 771 status_ = EmbeddedWorkerStatus::RUNNING; |
744 inflight_start_task_.reset(); | 772 inflight_start_task_.reset(); |
745 for (auto& observer : listener_list_) | 773 for (auto& observer : listener_list_) |
746 observer.OnStarted(); | 774 observer.OnStarted(); |
747 } | 775 } |
748 | 776 |
749 void EmbeddedWorkerInstance::OnStopped() { | 777 void EmbeddedWorkerInstance::OnStopped() { |
778 registry_->OnWorkerStoppedd(process_id(), embedded_worker_id_); | |
779 | |
750 EmbeddedWorkerStatus old_status = status_; | 780 EmbeddedWorkerStatus old_status = status_; |
751 ReleaseProcess(); | 781 ReleaseProcess(); |
752 for (auto& observer : listener_list_) | 782 for (auto& observer : listener_list_) |
753 observer.OnStopped(old_status); | 783 observer.OnStopped(old_status); |
754 } | 784 } |
755 | 785 |
756 void EmbeddedWorkerInstance::OnDetached() { | 786 void EmbeddedWorkerInstance::OnDetached() { |
757 EmbeddedWorkerStatus old_status = status_; | 787 EmbeddedWorkerStatus old_status = status_; |
758 ReleaseProcess(); | 788 ReleaseProcess(); |
759 for (auto& observer : listener_list_) | 789 for (auto& observer : listener_list_) |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
919 case SCRIPT_READ_FINISHED: | 949 case SCRIPT_READ_FINISHED: |
920 return "Script read finished"; | 950 return "Script read finished"; |
921 case STARTING_PHASE_MAX_VALUE: | 951 case STARTING_PHASE_MAX_VALUE: |
922 NOTREACHED(); | 952 NOTREACHED(); |
923 } | 953 } |
924 NOTREACHED() << phase; | 954 NOTREACHED() << phase; |
925 return std::string(); | 955 return std::string(); |
926 } | 956 } |
927 | 957 |
928 } // namespace content | 958 } // namespace content |
OLD | NEW |