| 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/logging.h" |
| 10 #include "base/macros.h" | 11 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 12 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/threading/non_thread_safe.h" | 14 #include "base/threading/non_thread_safe.h" |
| 14 #include "base/trace_event/trace_event.h" | 15 #include "base/trace_event/trace_event.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" |
| (...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopping()); | 476 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopping()); |
| 476 return status; | 477 return status; |
| 477 } | 478 } |
| 478 | 479 |
| 479 void EmbeddedWorkerInstance::StopIfIdle() { | 480 void EmbeddedWorkerInstance::StopIfIdle() { |
| 480 if (devtools_attached_) { | 481 if (devtools_attached_) { |
| 481 if (devtools_proxy_) | 482 if (devtools_proxy_) |
| 482 devtools_proxy_->NotifyWorkerStopIgnored(); | 483 devtools_proxy_->NotifyWorkerStopIgnored(); |
| 483 return; | 484 return; |
| 484 } | 485 } |
| 486 if (external_ref_count_ > 0) { |
| 487 LOG(WARNING) << "Skipped StopIfIdle due to external_ref_count_"; |
| 488 return; |
| 489 } |
| 485 Stop(); | 490 Stop(); |
| 486 } | 491 } |
| 487 | 492 |
| 488 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendMessage( | 493 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendMessage( |
| 489 const IPC::Message& message) { | 494 const IPC::Message& message) { |
| 490 DCHECK_NE(kInvalidEmbeddedWorkerThreadId, thread_id_); | 495 DCHECK_NE(kInvalidEmbeddedWorkerThreadId, thread_id_); |
| 491 if (status_ != EmbeddedWorkerStatus::RUNNING && | 496 if (status_ != EmbeddedWorkerStatus::RUNNING && |
| 492 status_ != EmbeddedWorkerStatus::STARTING) { | 497 status_ != EmbeddedWorkerStatus::STARTING) { |
| 493 return SERVICE_WORKER_ERROR_IPC_FAILED; | 498 return SERVICE_WORKER_ERROR_IPC_FAILED; |
| 494 } | 499 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 524 base::WeakPtr<ServiceWorkerContextCore> context, | 529 base::WeakPtr<ServiceWorkerContextCore> context, |
| 525 int embedded_worker_id) | 530 int embedded_worker_id) |
| 526 : context_(context), | 531 : context_(context), |
| 527 registry_(context->embedded_worker_registry()), | 532 registry_(context->embedded_worker_registry()), |
| 528 embedded_worker_id_(embedded_worker_id), | 533 embedded_worker_id_(embedded_worker_id), |
| 529 status_(EmbeddedWorkerStatus::STOPPED), | 534 status_(EmbeddedWorkerStatus::STOPPED), |
| 530 starting_phase_(NOT_STARTING), | 535 starting_phase_(NOT_STARTING), |
| 531 thread_id_(kInvalidEmbeddedWorkerThreadId), | 536 thread_id_(kInvalidEmbeddedWorkerThreadId), |
| 532 devtools_attached_(false), | 537 devtools_attached_(false), |
| 533 network_accessed_for_script_(false), | 538 network_accessed_for_script_(false), |
| 539 external_ref_count_(0), |
| 534 weak_factory_(this) {} | 540 weak_factory_(this) {} |
| 535 | 541 |
| 536 void EmbeddedWorkerInstance::OnProcessAllocated( | 542 void EmbeddedWorkerInstance::OnProcessAllocated( |
| 537 std::unique_ptr<WorkerProcessHandle> handle, | 543 std::unique_ptr<WorkerProcessHandle> handle, |
| 538 ServiceWorkerMetrics::StartSituation start_situation) { | 544 ServiceWorkerMetrics::StartSituation start_situation) { |
| 539 DCHECK_EQ(EmbeddedWorkerStatus::STARTING, status_); | 545 DCHECK_EQ(EmbeddedWorkerStatus::STARTING, status_); |
| 540 DCHECK(!process_handle_); | 546 DCHECK(!process_handle_); |
| 541 | 547 |
| 542 process_handle_ = std::move(handle); | 548 process_handle_ = std::move(handle); |
| 543 starting_phase_ = REGISTERING_TO_DEVTOOLS; | 549 starting_phase_ = REGISTERING_TO_DEVTOOLS; |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 719 EmbeddedWorkerStatus old_status = status_; | 725 EmbeddedWorkerStatus old_status = status_; |
| 720 ReleaseProcess(); | 726 ReleaseProcess(); |
| 721 FOR_EACH_OBSERVER(Listener, listener_list_, OnDetached(old_status)); | 727 FOR_EACH_OBSERVER(Listener, listener_list_, OnDetached(old_status)); |
| 722 } | 728 } |
| 723 | 729 |
| 724 void EmbeddedWorkerInstance::Detach() { | 730 void EmbeddedWorkerInstance::Detach() { |
| 725 registry_->RemoveWorker(process_id(), embedded_worker_id_); | 731 registry_->RemoveWorker(process_id(), embedded_worker_id_); |
| 726 OnDetached(); | 732 OnDetached(); |
| 727 } | 733 } |
| 728 | 734 |
| 735 void EmbeddedWorkerInstance::IncrementRefCount() { |
| 736 ++external_ref_count_; |
| 737 } |
| 738 |
| 739 void EmbeddedWorkerInstance::DecrementRefCount() { |
| 740 DCHECK(external_ref_count_ > 0); |
| 741 --external_ref_count_; |
| 742 } |
| 743 |
| 729 base::WeakPtr<EmbeddedWorkerInstance> EmbeddedWorkerInstance::AsWeakPtr() { | 744 base::WeakPtr<EmbeddedWorkerInstance> EmbeddedWorkerInstance::AsWeakPtr() { |
| 730 return weak_factory_.GetWeakPtr(); | 745 return weak_factory_.GetWeakPtr(); |
| 731 } | 746 } |
| 732 | 747 |
| 733 bool EmbeddedWorkerInstance::OnMessageReceived(const IPC::Message& message) { | 748 bool EmbeddedWorkerInstance::OnMessageReceived(const IPC::Message& message) { |
| 734 ListenerList::Iterator it(&listener_list_); | 749 ListenerList::Iterator it(&listener_list_); |
| 735 while (Listener* listener = it.GetNext()) { | 750 while (Listener* listener = it.GetNext()) { |
| 736 if (listener->OnMessageReceived(message)) | 751 if (listener->OnMessageReceived(message)) |
| 737 return true; | 752 return true; |
| 738 } | 753 } |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 881 case SCRIPT_READ_FINISHED: | 896 case SCRIPT_READ_FINISHED: |
| 882 return "Script read finished"; | 897 return "Script read finished"; |
| 883 case STARTING_PHASE_MAX_VALUE: | 898 case STARTING_PHASE_MAX_VALUE: |
| 884 NOTREACHED(); | 899 NOTREACHED(); |
| 885 } | 900 } |
| 886 NOTREACHED() << phase; | 901 NOTREACHED() << phase; |
| 887 return std::string(); | 902 return std::string(); |
| 888 } | 903 } |
| 889 | 904 |
| 890 } // namespace content | 905 } // namespace content |
| OLD | NEW |