Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(314)

Side by Side Diff: content/browser/service_worker/embedded_worker_instance.cc

Issue 2418373002: Reduce FOR_EACH_OBSERVER usage in content/browser/service_worker (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 434 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 DCHECK(status_ == EmbeddedWorkerStatus::STOPPED); 445 DCHECK(status_ == EmbeddedWorkerStatus::STOPPED);
446 446
447 DCHECK(!params->pause_after_download || !params->is_installed); 447 DCHECK(!params->pause_after_download || !params->is_installed);
448 DCHECK_NE(kInvalidServiceWorkerVersionId, params->service_worker_version_id); 448 DCHECK_NE(kInvalidServiceWorkerVersionId, params->service_worker_version_id);
449 step_time_ = base::TimeTicks::Now(); 449 step_time_ = base::TimeTicks::Now();
450 status_ = EmbeddedWorkerStatus::STARTING; 450 status_ = EmbeddedWorkerStatus::STARTING;
451 starting_phase_ = ALLOCATING_PROCESS; 451 starting_phase_ = ALLOCATING_PROCESS;
452 network_accessed_for_script_ = false; 452 network_accessed_for_script_ = false;
453 interface_registry_.reset(new shell::InterfaceRegistry); 453 interface_registry_.reset(new shell::InterfaceRegistry);
454 remote_interfaces_.reset(new shell::InterfaceProvider); 454 remote_interfaces_.reset(new shell::InterfaceProvider);
455 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarting()); 455 for (auto& observer : listener_list_)
456 observer.OnStarting();
456 457
457 params->embedded_worker_id = embedded_worker_id_; 458 params->embedded_worker_id = embedded_worker_id_;
458 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE; 459 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE;
459 params->wait_for_debugger = false; 460 params->wait_for_debugger = false;
460 params->settings.v8_cache_options = GetV8CacheOptions(); 461 params->settings.v8_cache_options = GetV8CacheOptions();
461 462
462 mojom::EmbeddedWorkerInstanceClientRequest request; 463 mojom::EmbeddedWorkerInstanceClientRequest request;
463 if (ServiceWorkerUtils::IsMojoForServiceWorkerEnabled()) { 464 if (ServiceWorkerUtils::IsMojoForServiceWorkerEnabled()) {
464 request = mojo::GetProxy(&client_); 465 request = mojo::GetProxy(&client_);
465 client_.set_connection_error_handler( 466 client_.set_connection_error_handler(
(...skipping 25 matching lines...) Expand all
491 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.SendStopWorker.Status", status, 492 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.SendStopWorker.Status", status,
492 SERVICE_WORKER_ERROR_MAX_VALUE); 493 SERVICE_WORKER_ERROR_MAX_VALUE);
493 // StopWorker could fail if we were starting up and don't have a process yet, 494 // StopWorker could fail if we were starting up and don't have a process yet,
494 // or we can no longer communicate with the process. So just detach. 495 // or we can no longer communicate with the process. So just detach.
495 if (status != SERVICE_WORKER_OK) { 496 if (status != SERVICE_WORKER_OK) {
496 OnDetached(); 497 OnDetached();
497 return status; 498 return status;
498 } 499 }
499 500
500 status_ = EmbeddedWorkerStatus::STOPPING; 501 status_ = EmbeddedWorkerStatus::STOPPING;
501 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopping()); 502 for (auto& observer : listener_list_)
503 observer.OnStopping();
502 return status; 504 return status;
503 } 505 }
504 506
505 void EmbeddedWorkerInstance::StopIfIdle() { 507 void EmbeddedWorkerInstance::StopIfIdle() {
506 if (devtools_attached_) { 508 if (devtools_attached_) {
507 if (devtools_proxy_) { 509 if (devtools_proxy_) {
508 // Check ShouldNotifyWorkerStopIgnored not to show the same message 510 // Check ShouldNotifyWorkerStopIgnored not to show the same message
509 // multiple times in DevTools. 511 // multiple times in DevTools.
510 if (devtools_proxy_->ShouldNotifyWorkerStopIgnored()) { 512 if (devtools_proxy_->ShouldNotifyWorkerStopIgnored()) {
511 AddMessageToConsole(CONSOLE_MESSAGE_LEVEL_DEBUG, 513 AddMessageToConsole(CONSOLE_MESSAGE_LEVEL_DEBUG,
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 570
569 void EmbeddedWorkerInstance::OnProcessAllocated( 571 void EmbeddedWorkerInstance::OnProcessAllocated(
570 std::unique_ptr<WorkerProcessHandle> handle, 572 std::unique_ptr<WorkerProcessHandle> handle,
571 ServiceWorkerMetrics::StartSituation start_situation) { 573 ServiceWorkerMetrics::StartSituation start_situation) {
572 DCHECK_EQ(EmbeddedWorkerStatus::STARTING, status_); 574 DCHECK_EQ(EmbeddedWorkerStatus::STARTING, status_);
573 DCHECK(!process_handle_); 575 DCHECK(!process_handle_);
574 576
575 process_handle_ = std::move(handle); 577 process_handle_ = std::move(handle);
576 starting_phase_ = REGISTERING_TO_DEVTOOLS; 578 starting_phase_ = REGISTERING_TO_DEVTOOLS;
577 start_situation_ = start_situation; 579 start_situation_ = start_situation;
578 FOR_EACH_OBSERVER(Listener, listener_list_, OnProcessAllocated()); 580 for (auto& observer : listener_list_)
581 observer.OnProcessAllocated();
579 } 582 }
580 583
581 void EmbeddedWorkerInstance::OnRegisteredToDevToolsManager( 584 void EmbeddedWorkerInstance::OnRegisteredToDevToolsManager(
582 bool is_new_process, 585 bool is_new_process,
583 int worker_devtools_agent_route_id, 586 int worker_devtools_agent_route_id,
584 bool wait_for_debugger) { 587 bool wait_for_debugger) {
585 if (worker_devtools_agent_route_id != MSG_ROUTING_NONE) { 588 if (worker_devtools_agent_route_id != MSG_ROUTING_NONE) {
586 DCHECK(!devtools_proxy_); 589 DCHECK(!devtools_proxy_);
587 devtools_proxy_.reset( 590 devtools_proxy_.reset(
588 new DevToolsProxy(process_id(), worker_devtools_agent_route_id)); 591 new DevToolsProxy(process_id(), worker_devtools_agent_route_id));
589 } 592 }
590 if (wait_for_debugger) { 593 if (wait_for_debugger) {
591 // We don't measure the start time when wait_for_debugger flag is set. So 594 // We don't measure the start time when wait_for_debugger flag is set. So
592 // we set the NULL time here. 595 // we set the NULL time here.
593 step_time_ = base::TimeTicks(); 596 step_time_ = base::TimeTicks();
594 } 597 }
595 FOR_EACH_OBSERVER(Listener, listener_list_, OnRegisteredToDevToolsManager()); 598 for (auto& observer : listener_list_)
599 observer.OnRegisteredToDevToolsManager();
596 } 600 }
597 601
598 void EmbeddedWorkerInstance::SendMojoStartWorker( 602 void EmbeddedWorkerInstance::SendMojoStartWorker(
599 std::unique_ptr<EmbeddedWorkerStartParams> params) { 603 std::unique_ptr<EmbeddedWorkerStartParams> params) {
600 client_->StartWorker(*params); 604 client_->StartWorker(*params);
601 registry_->BindWorkerToProcess(process_id(), embedded_worker_id()); 605 registry_->BindWorkerToProcess(process_id(), embedded_worker_id());
602 TRACE_EVENT_ASYNC_STEP_PAST1("ServiceWorker", "EmbeddedWorkerInstance::Start", 606 TRACE_EVENT_ASYNC_STEP_PAST1("ServiceWorker", "EmbeddedWorkerInstance::Start",
603 this, "SendStartWorker", "Status", "mojo"); 607 this, "SendStartWorker", "Status", "mojo");
604 OnStartWorkerMessageSent(); 608 OnStartWorkerMessageSent();
605 } 609 }
606 610
607 void EmbeddedWorkerInstance::OnStartWorkerMessageSent() { 611 void EmbeddedWorkerInstance::OnStartWorkerMessageSent() {
608 if (!step_time_.is_null()) { 612 if (!step_time_.is_null()) {
609 base::TimeDelta duration = UpdateStepTime(); 613 base::TimeDelta duration = UpdateStepTime();
610 if (inflight_start_task_->is_installed()) { 614 if (inflight_start_task_->is_installed()) {
611 ServiceWorkerMetrics::RecordTimeToSendStartWorker(duration, 615 ServiceWorkerMetrics::RecordTimeToSendStartWorker(duration,
612 start_situation_); 616 start_situation_);
613 } 617 }
614 } 618 }
615 619
616 starting_phase_ = SENT_START_WORKER; 620 starting_phase_ = SENT_START_WORKER;
617 FOR_EACH_OBSERVER(Listener, listener_list_, OnStartWorkerMessageSent()); 621 for (auto& observer : listener_list_)
622 observer.OnStartWorkerMessageSent();
618 } 623 }
619 624
620 void EmbeddedWorkerInstance::OnReadyForInspection() { 625 void EmbeddedWorkerInstance::OnReadyForInspection() {
621 if (devtools_proxy_) 626 if (devtools_proxy_)
622 devtools_proxy_->NotifyWorkerReadyForInspection(); 627 devtools_proxy_->NotifyWorkerReadyForInspection();
623 } 628 }
624 629
625 void EmbeddedWorkerInstance::OnScriptReadStarted() { 630 void EmbeddedWorkerInstance::OnScriptReadStarted() {
626 starting_phase_ = SCRIPT_READ_STARTED; 631 starting_phase_ = SCRIPT_READ_STARTED;
627 } 632 }
(...skipping 20 matching lines...) Expand all
648 "ServiceWorker", "EmbeddedWorkerInstance::Start", 653 "ServiceWorker", "EmbeddedWorkerInstance::Start",
649 inflight_start_task_.get(), "OnScriptLoaded", "Source", 654 inflight_start_task_.get(), "OnScriptLoaded", "Source",
650 ServiceWorkerMetrics::LoadSourceToString(source)); 655 ServiceWorkerMetrics::LoadSourceToString(source));
651 656
652 if (!step_time_.is_null()) { 657 if (!step_time_.is_null()) {
653 base::TimeDelta duration = UpdateStepTime(); 658 base::TimeDelta duration = UpdateStepTime();
654 ServiceWorkerMetrics::RecordTimeToLoad(duration, source, start_situation_); 659 ServiceWorkerMetrics::RecordTimeToLoad(duration, source, start_situation_);
655 } 660 }
656 661
657 starting_phase_ = SCRIPT_LOADED; 662 starting_phase_ = SCRIPT_LOADED;
658 FOR_EACH_OBSERVER(Listener, listener_list_, OnScriptLoaded()); 663 for (auto& observer : listener_list_)
664 observer.OnScriptLoaded();
659 // |this| may be destroyed by the callback. 665 // |this| may be destroyed by the callback.
660 } 666 }
661 667
662 void EmbeddedWorkerInstance::OnURLJobCreatedForMainScript() { 668 void EmbeddedWorkerInstance::OnURLJobCreatedForMainScript() {
663 if (!inflight_start_task_) 669 if (!inflight_start_task_)
664 return; 670 return;
665 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", "EmbeddedWorkerInstance::Start", 671 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", "EmbeddedWorkerInstance::Start",
666 inflight_start_task_.get(), "OnURLJobCreated"); 672 inflight_start_task_.get(), "OnURLJobCreated");
667 if (!step_time_.is_null()) { 673 if (!step_time_.is_null()) {
668 base::TimeDelta duration = UpdateStepTime(); 674 base::TimeDelta duration = UpdateStepTime();
(...skipping 19 matching lines...) Expand all
688 inflight_start_task_.get(), "OnThreadStarted"); 694 inflight_start_task_.get(), "OnThreadStarted");
689 695
690 starting_phase_ = THREAD_STARTED; 696 starting_phase_ = THREAD_STARTED;
691 if (!step_time_.is_null()) { 697 if (!step_time_.is_null()) {
692 base::TimeDelta duration = UpdateStepTime(); 698 base::TimeDelta duration = UpdateStepTime();
693 if (inflight_start_task_->is_installed()) 699 if (inflight_start_task_->is_installed())
694 ServiceWorkerMetrics::RecordTimeToStartThread(duration, start_situation_); 700 ServiceWorkerMetrics::RecordTimeToStartThread(duration, start_situation_);
695 } 701 }
696 702
697 thread_id_ = thread_id; 703 thread_id_ = thread_id;
698 FOR_EACH_OBSERVER(Listener, listener_list_, OnThreadStarted()); 704 for (auto& observer : listener_list_)
705 observer.OnThreadStarted();
699 706
700 shell::mojom::InterfaceProviderPtr exposed_interfaces; 707 shell::mojom::InterfaceProviderPtr exposed_interfaces;
701 interface_registry_->Bind(mojo::GetProxy(&exposed_interfaces)); 708 interface_registry_->Bind(mojo::GetProxy(&exposed_interfaces));
702 shell::mojom::InterfaceProviderPtr remote_interfaces; 709 shell::mojom::InterfaceProviderPtr remote_interfaces;
703 shell::mojom::InterfaceProviderRequest request = 710 shell::mojom::InterfaceProviderRequest request =
704 mojo::GetProxy(&remote_interfaces); 711 mojo::GetProxy(&remote_interfaces);
705 remote_interfaces_->Bind(std::move(remote_interfaces)); 712 remote_interfaces_->Bind(std::move(remote_interfaces));
706 BrowserThread::PostTask( 713 BrowserThread::PostTask(
707 BrowserThread::UI, FROM_HERE, 714 BrowserThread::UI, FROM_HERE,
708 base::Bind(SetupMojoOnUIThread, process_id(), thread_id_, 715 base::Bind(SetupMojoOnUIThread, process_id(), thread_id_,
709 base::Passed(&request), 716 base::Passed(&request),
710 base::Passed(exposed_interfaces.PassInterface()))); 717 base::Passed(exposed_interfaces.PassInterface())));
711 } 718 }
712 719
713 void EmbeddedWorkerInstance::OnScriptLoadFailed() { 720 void EmbeddedWorkerInstance::OnScriptLoadFailed() {
714 if (!inflight_start_task_) 721 if (!inflight_start_task_)
715 return; 722 return;
716 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", "EmbeddedWorkerInstance::Start", 723 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", "EmbeddedWorkerInstance::Start",
717 inflight_start_task_.get(), 724 inflight_start_task_.get(),
718 "OnScriptLoadFailed"); 725 "OnScriptLoadFailed");
719 FOR_EACH_OBSERVER(Listener, listener_list_, OnScriptLoadFailed()); 726 for (auto& observer : listener_list_)
727 observer.OnScriptLoadFailed();
720 } 728 }
721 729
722 void EmbeddedWorkerInstance::OnScriptEvaluated(bool success) { 730 void EmbeddedWorkerInstance::OnScriptEvaluated(bool success) {
723 if (!inflight_start_task_) 731 if (!inflight_start_task_)
724 return; 732 return;
725 DCHECK_EQ(EmbeddedWorkerStatus::STARTING, status_); 733 DCHECK_EQ(EmbeddedWorkerStatus::STARTING, status_);
726 734
727 TRACE_EVENT_ASYNC_STEP_PAST1("ServiceWorker", "EmbeddedWorkerInstance::Start", 735 TRACE_EVENT_ASYNC_STEP_PAST1("ServiceWorker", "EmbeddedWorkerInstance::Start",
728 inflight_start_task_.get(), "OnScriptEvaluated", 736 inflight_start_task_.get(), "OnScriptEvaluated",
729 "Success", success); 737 "Success", success);
(...skipping 13 matching lines...) Expand all
743 // |this| may be destroyed by the callback. 751 // |this| may be destroyed by the callback.
744 } 752 }
745 753
746 void EmbeddedWorkerInstance::OnStarted() { 754 void EmbeddedWorkerInstance::OnStarted() {
747 // Stop is requested before OnStarted is sent back from the worker. 755 // Stop is requested before OnStarted is sent back from the worker.
748 if (status_ == EmbeddedWorkerStatus::STOPPING) 756 if (status_ == EmbeddedWorkerStatus::STOPPING)
749 return; 757 return;
750 DCHECK(status_ == EmbeddedWorkerStatus::STARTING); 758 DCHECK(status_ == EmbeddedWorkerStatus::STARTING);
751 status_ = EmbeddedWorkerStatus::RUNNING; 759 status_ = EmbeddedWorkerStatus::RUNNING;
752 inflight_start_task_.reset(); 760 inflight_start_task_.reset();
753 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarted()); 761 for (auto& observer : listener_list_)
762 observer.OnStarted();
754 } 763 }
755 764
756 void EmbeddedWorkerInstance::OnStopped() { 765 void EmbeddedWorkerInstance::OnStopped() {
757 EmbeddedWorkerStatus old_status = status_; 766 EmbeddedWorkerStatus old_status = status_;
758 ReleaseProcess(); 767 ReleaseProcess();
759 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopped(old_status)); 768 for (auto& observer : listener_list_)
769 observer.OnStopped(old_status);
760 } 770 }
761 771
762 void EmbeddedWorkerInstance::OnDetached() { 772 void EmbeddedWorkerInstance::OnDetached() {
763 EmbeddedWorkerStatus old_status = status_; 773 EmbeddedWorkerStatus old_status = status_;
764 ReleaseProcess(); 774 ReleaseProcess();
765 FOR_EACH_OBSERVER(Listener, listener_list_, OnDetached(old_status)); 775 for (auto& observer : listener_list_)
776 observer.OnDetached(old_status);
766 } 777 }
767 778
768 void EmbeddedWorkerInstance::Detach() { 779 void EmbeddedWorkerInstance::Detach() {
769 registry_->DetachWorker(process_id(), embedded_worker_id()); 780 registry_->DetachWorker(process_id(), embedded_worker_id());
770 OnDetached(); 781 OnDetached();
771 } 782 }
772 783
773 base::WeakPtr<EmbeddedWorkerInstance> EmbeddedWorkerInstance::AsWeakPtr() { 784 base::WeakPtr<EmbeddedWorkerInstance> EmbeddedWorkerInstance::AsWeakPtr() {
774 return weak_factory_.GetWeakPtr(); 785 return weak_factory_.GetWeakPtr();
775 } 786 }
776 787
777 bool EmbeddedWorkerInstance::OnMessageReceived(const IPC::Message& message) { 788 bool EmbeddedWorkerInstance::OnMessageReceived(const IPC::Message& message) {
778 for (auto& listener : listener_list_) { 789 for (auto& listener : listener_list_) {
779 if (listener.OnMessageReceived(message)) 790 if (listener.OnMessageReceived(message))
780 return true; 791 return true;
781 } 792 }
782 return false; 793 return false;
783 } 794 }
784 795
785 void EmbeddedWorkerInstance::OnReportException( 796 void EmbeddedWorkerInstance::OnReportException(
786 const base::string16& error_message, 797 const base::string16& error_message,
787 int line_number, 798 int line_number,
788 int column_number, 799 int column_number,
789 const GURL& source_url) { 800 const GURL& source_url) {
790 FOR_EACH_OBSERVER( 801 for (auto& observer : listener_list_) {
791 Listener, 802 observer.OnReportException(error_message, line_number, column_number,
792 listener_list_, 803 source_url);
793 OnReportException(error_message, line_number, column_number, source_url)); 804 }
794 } 805 }
795 806
796 void EmbeddedWorkerInstance::OnReportConsoleMessage( 807 void EmbeddedWorkerInstance::OnReportConsoleMessage(
797 int source_identifier, 808 int source_identifier,
798 int message_level, 809 int message_level,
799 const base::string16& message, 810 const base::string16& message,
800 int line_number, 811 int line_number,
801 const GURL& source_url) { 812 const GURL& source_url) {
802 FOR_EACH_OBSERVER( 813 for (auto& observer : listener_list_) {
803 Listener, 814 observer.OnReportConsoleMessage(source_identifier, message_level, message,
804 listener_list_, 815 line_number, source_url);
805 OnReportConsoleMessage( 816 }
806 source_identifier, message_level, message, line_number, source_url));
807 } 817 }
808 818
809 int EmbeddedWorkerInstance::process_id() const { 819 int EmbeddedWorkerInstance::process_id() const {
810 if (process_handle_) 820 if (process_handle_)
811 return process_handle_->process_id(); 821 return process_handle_->process_id();
812 return ChildProcessHost::kInvalidUniqueID; 822 return ChildProcessHost::kInvalidUniqueID;
813 } 823 }
814 824
815 bool EmbeddedWorkerInstance::is_new_process() const { 825 bool EmbeddedWorkerInstance::is_new_process() const {
816 DCHECK(process_handle_); 826 DCHECK(process_handle_);
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
925 case SCRIPT_READ_FINISHED: 935 case SCRIPT_READ_FINISHED:
926 return "Script read finished"; 936 return "Script read finished";
927 case STARTING_PHASE_MAX_VALUE: 937 case STARTING_PHASE_MAX_VALUE:
928 NOTREACHED(); 938 NOTREACHED();
929 } 939 }
930 NOTREACHED() << phase; 940 NOTREACHED() << phase;
931 return std::string(); 941 return std::string();
932 } 942 }
933 943
934 } // namespace content 944 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698