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

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

Issue 2578023002: ServiceWorker: Stop don't send a message before connection established (Closed)
Patch Set: Reset starting_phase when releasing the process Created 3 years, 11 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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 // This could be called on the UI thread if |client_| still be valid when the 121 // This could be called on the UI thread if |client_| still be valid when the
122 // message loop on the UI thread gets destructed. 122 // message loop on the UI thread gets destructed.
123 // TODO(shimazu): Remove this after https://crbug.com/604762 is fixed 123 // TODO(shimazu): Remove this after https://crbug.com/604762 is fixed
124 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { 124 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
125 DCHECK(ServiceWorkerUtils::IsMojoForServiceWorkerEnabled()); 125 DCHECK(ServiceWorkerUtils::IsMojoForServiceWorkerEnabled());
126 return; 126 return;
127 } 127 }
128 instance->Detach(); 128 instance->Detach();
129 } 129 }
130 130
131 bool HasSentStartWorker(EmbeddedWorkerInstance::StartingPhase phase) {
132 switch (phase) {
133 case EmbeddedWorkerInstance::NOT_STARTING:
134 case EmbeddedWorkerInstance::ALLOCATING_PROCESS:
135 case EmbeddedWorkerInstance::REGISTERING_TO_DEVTOOLS:
136 return false;
137 case EmbeddedWorkerInstance::SENT_START_WORKER:
138 case EmbeddedWorkerInstance::SCRIPT_DOWNLOADING:
139 case EmbeddedWorkerInstance::SCRIPT_READ_STARTED:
140 case EmbeddedWorkerInstance::SCRIPT_READ_FINISHED:
141 case EmbeddedWorkerInstance::SCRIPT_LOADED:
142 case EmbeddedWorkerInstance::SCRIPT_EVALUATED:
143 case EmbeddedWorkerInstance::THREAD_STARTED:
144 return true;
145 case EmbeddedWorkerInstance::STARTING_PHASE_MAX_VALUE:
146 NOTREACHED();
147 }
148 return false;
149 }
150
131 } // namespace 151 } // namespace
132 152
133 // Lives on IO thread, proxies notifications to DevToolsManager that lives on 153 // Lives on IO thread, proxies notifications to DevToolsManager that lives on
134 // UI thread. Owned by EmbeddedWorkerInstance. 154 // UI thread. Owned by EmbeddedWorkerInstance.
135 class EmbeddedWorkerInstance::DevToolsProxy : public base::NonThreadSafe { 155 class EmbeddedWorkerInstance::DevToolsProxy : public base::NonThreadSafe {
136 public: 156 public:
137 DevToolsProxy(int process_id, int agent_route_id) 157 DevToolsProxy(int process_id, int agent_route_id)
138 : process_id_(process_id), 158 : process_id_(process_id),
139 agent_route_id_(agent_route_id) {} 159 agent_route_id_(agent_route_id) {}
140 160
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 base::Bind(&CallDetach, base::Unretained(this))); 505 base::Bind(&CallDetach, base::Unretained(this)));
486 } 506 }
487 507
488 pending_dispatcher_request_ = std::move(dispatcher_request); 508 pending_dispatcher_request_ = std::move(dispatcher_request);
489 509
490 inflight_start_task_.reset( 510 inflight_start_task_.reset(
491 new StartTask(this, params->script_url, std::move(request))); 511 new StartTask(this, params->script_url, std::move(request)));
492 inflight_start_task_->Start(std::move(params), callback); 512 inflight_start_task_->Start(std::move(params), callback);
493 } 513 }
494 514
495 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { 515 bool EmbeddedWorkerInstance::Stop() {
496 DCHECK(status_ == EmbeddedWorkerStatus::STARTING || 516 DCHECK(status_ == EmbeddedWorkerStatus::STARTING ||
497 status_ == EmbeddedWorkerStatus::RUNNING) 517 status_ == EmbeddedWorkerStatus::RUNNING)
498 << static_cast<int>(status_); 518 << static_cast<int>(status_);
499 519
500 // Abort an inflight start task. 520 // Abort an inflight start task.
501 inflight_start_task_.reset(); 521 inflight_start_task_.reset();
502 522
503 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_IPC_FAILED;
504 if (ServiceWorkerUtils::IsMojoForServiceWorkerEnabled()) { 523 if (ServiceWorkerUtils::IsMojoForServiceWorkerEnabled()) {
505 status = SERVICE_WORKER_OK; 524 if (status_ == EmbeddedWorkerStatus::STARTING &&
525 !HasSentStartWorker(starting_phase())) {
526 // Don't send the StopWorker message when the StartWorker message hasn't
527 // been sent.
528 // TODO(shimazu): Invoke OnStopping/OnStopped after the legacy IPC path is
529 // removed.
530 OnDetached();
531 return false;
532 }
506 client_->StopWorker(base::Bind(&EmbeddedWorkerRegistry::OnWorkerStopped, 533 client_->StopWorker(base::Bind(&EmbeddedWorkerRegistry::OnWorkerStopped,
507 base::Unretained(registry_.get()), 534 base::Unretained(registry_.get()),
508 process_id(), embedded_worker_id())); 535 process_id(), embedded_worker_id()));
509 } else { 536 } else {
510 status = registry_->StopWorker(process_id(), embedded_worker_id_); 537 ServiceWorkerStatusCode status =
511 } 538 registry_->StopWorker(process_id(), embedded_worker_id_);
512 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.SendStopWorker.Status", status, 539 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.SendStopWorker.Status", status,
513 SERVICE_WORKER_ERROR_MAX_VALUE); 540 SERVICE_WORKER_ERROR_MAX_VALUE);
514 // StopWorker could fail if we were starting up and don't have a process yet, 541 // StopWorker could fail if we were starting up and don't have a process
515 // or we can no longer communicate with the process. So just detach. 542 // yet, or we can no longer communicate with the process. So just detach.
516 if (status != SERVICE_WORKER_OK) { 543 if (status != SERVICE_WORKER_OK) {
517 OnDetached(); 544 OnDetached();
518 return status; 545 return false;
546 }
519 } 547 }
520 548
521 status_ = EmbeddedWorkerStatus::STOPPING; 549 status_ = EmbeddedWorkerStatus::STOPPING;
522 for (auto& observer : listener_list_) 550 for (auto& observer : listener_list_)
523 observer.OnStopping(); 551 observer.OnStopping();
524 return status; 552 return true;
525 } 553 }
526 554
527 void EmbeddedWorkerInstance::StopIfIdle() { 555 void EmbeddedWorkerInstance::StopIfIdle() {
528 if (devtools_attached_) { 556 if (devtools_attached_) {
529 if (devtools_proxy_) { 557 if (devtools_proxy_) {
530 // Check ShouldNotifyWorkerStopIgnored not to show the same message 558 // Check ShouldNotifyWorkerStopIgnored not to show the same message
531 // multiple times in DevTools. 559 // multiple times in DevTools.
532 if (devtools_proxy_->ShouldNotifyWorkerStopIgnored()) { 560 if (devtools_proxy_->ShouldNotifyWorkerStopIgnored()) {
533 AddMessageToConsole(CONSOLE_MESSAGE_LEVEL_DEBUG, 561 AddMessageToConsole(CONSOLE_MESSAGE_LEVEL_DEBUG,
534 kServiceWorkerTerminationCanceledMesage); 562 kServiceWorkerTerminationCanceledMesage);
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
860 } 888 }
861 889
862 void EmbeddedWorkerInstance::ReleaseProcess() { 890 void EmbeddedWorkerInstance::ReleaseProcess() {
863 // Abort an inflight start task. 891 // Abort an inflight start task.
864 inflight_start_task_.reset(); 892 inflight_start_task_.reset();
865 893
866 client_.reset(); 894 client_.reset();
867 devtools_proxy_.reset(); 895 devtools_proxy_.reset();
868 process_handle_.reset(); 896 process_handle_.reset();
869 status_ = EmbeddedWorkerStatus::STOPPED; 897 status_ = EmbeddedWorkerStatus::STOPPED;
898 starting_phase_ = NOT_STARTING;
870 thread_id_ = kInvalidEmbeddedWorkerThreadId; 899 thread_id_ = kInvalidEmbeddedWorkerThreadId;
871 } 900 }
872 901
873 void EmbeddedWorkerInstance::OnStartFailed(const StatusCallback& callback, 902 void EmbeddedWorkerInstance::OnStartFailed(const StatusCallback& callback,
874 ServiceWorkerStatusCode status) { 903 ServiceWorkerStatusCode status) {
875 EmbeddedWorkerStatus old_status = status_; 904 EmbeddedWorkerStatus old_status = status_;
876 ReleaseProcess(); 905 ReleaseProcess();
877 base::WeakPtr<EmbeddedWorkerInstance> weak_this = weak_factory_.GetWeakPtr(); 906 base::WeakPtr<EmbeddedWorkerInstance> weak_this = weak_factory_.GetWeakPtr();
878 callback.Run(status); 907 callback.Run(status);
879 if (weak_this && old_status != EmbeddedWorkerStatus::STOPPED) { 908 if (weak_this && old_status != EmbeddedWorkerStatus::STOPPED) {
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
942 case SCRIPT_READ_FINISHED: 971 case SCRIPT_READ_FINISHED:
943 return "Script read finished"; 972 return "Script read finished";
944 case STARTING_PHASE_MAX_VALUE: 973 case STARTING_PHASE_MAX_VALUE:
945 NOTREACHED(); 974 NOTREACHED();
946 } 975 }
947 NOTREACHED() << phase; 976 NOTREACHED() << phase;
948 return std::string(); 977 return std::string();
949 } 978 }
950 979
951 } // namespace content 980 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698