Chromium Code Reviews| 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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 Loading... | |
| 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 path is | |
| 529 // removed. | |
|
falken
2017/01/05 06:40:17
for clarity, "legacy IPC path"
shimazu
2017/01/05 07:42:56
Done.
| |
| 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 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 942 case SCRIPT_READ_FINISHED: | 970 case SCRIPT_READ_FINISHED: |
| 943 return "Script read finished"; | 971 return "Script read finished"; |
| 944 case STARTING_PHASE_MAX_VALUE: | 972 case STARTING_PHASE_MAX_VALUE: |
| 945 NOTREACHED(); | 973 NOTREACHED(); |
| 946 } | 974 } |
| 947 NOTREACHED() << phase; | 975 NOTREACHED() << phase; |
| 948 return std::string(); | 976 return std::string(); |
| 949 } | 977 } |
| 950 | 978 |
| 951 } // namespace content | 979 } // namespace content |
| OLD | NEW |