| 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/service_worker_version.h" | 5 #include "content/browser/service_worker/service_worker_version.h" |
| 6 | 6 |
| 7 #include <map> |
| 8 #include <string> |
| 9 |
| 7 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 8 #include "base/location.h" | 11 #include "base/location.h" |
| 9 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 10 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 12 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 13 #include "base/strings/string16.h" | 16 #include "base/strings/string16.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
| 16 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| (...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 stashed_port_manager->AddPort(service_worker.get(), message_port_id, name); | 391 stashed_port_manager->AddPort(service_worker.get(), message_port_id, name); |
| 389 } | 392 } |
| 390 | 393 |
| 391 } // namespace | 394 } // namespace |
| 392 | 395 |
| 393 const int ServiceWorkerVersion::kStartWorkerTimeoutMinutes = 5; | 396 const int ServiceWorkerVersion::kStartWorkerTimeoutMinutes = 5; |
| 394 const int ServiceWorkerVersion::kRequestTimeoutMinutes = 5; | 397 const int ServiceWorkerVersion::kRequestTimeoutMinutes = 5; |
| 395 | 398 |
| 396 class ServiceWorkerVersion::Metrics { | 399 class ServiceWorkerVersion::Metrics { |
| 397 public: | 400 public: |
| 401 using EventType = ServiceWorkerMetrics::EventType; |
| 398 explicit Metrics(ServiceWorkerVersion* owner) : owner_(owner) {} | 402 explicit Metrics(ServiceWorkerVersion* owner) : owner_(owner) {} |
| 399 ~Metrics() { | 403 ~Metrics() { |
| 400 ServiceWorkerMetrics::RecordEventStatus(fired_events_, handled_events_); | 404 for (const auto& ev : event_stats_) { |
| 405 ServiceWorkerMetrics::RecordEventHandledRatio(owner_->scope(), ev.first, |
| 406 ev.second.handled_events, |
| 407 ev.second.fired_events); |
| 408 } |
| 401 } | 409 } |
| 402 | 410 |
| 403 void RecordEventStatus(bool handled) { | 411 void RecordEventHandledStatus(EventType event, bool handled) { |
| 404 ++fired_events_; | 412 event_stats_[event].fired_events++; |
| 405 if (handled) | 413 if (handled) |
| 406 ++handled_events_; | 414 event_stats_[event].handled_events++; |
| 407 } | 415 } |
| 408 | 416 |
| 409 void NotifyStopping() { | 417 void NotifyStopping() { |
| 410 stop_status_ = ServiceWorkerMetrics::STOP_STATUS_STOPPING; | 418 stop_status_ = ServiceWorkerMetrics::STOP_STATUS_STOPPING; |
| 411 } | 419 } |
| 412 | 420 |
| 413 void NotifyStopped() { | 421 void NotifyStopped() { |
| 414 switch (stop_status_) { | 422 switch (stop_status_) { |
| 415 case ServiceWorkerMetrics::STOP_STATUS_STOPPED: | 423 case ServiceWorkerMetrics::STOP_STATUS_STOPPED: |
| 416 case ServiceWorkerMetrics::STOP_STATUS_STALLED_THEN_STOPPED: | 424 case ServiceWorkerMetrics::STOP_STATUS_STALLED_THEN_STOPPED: |
| (...skipping 14 matching lines...) Expand all Loading... |
| 431 | 439 |
| 432 void NotifyStalledInStopping() { | 440 void NotifyStalledInStopping() { |
| 433 if (stop_status_ != ServiceWorkerMetrics::STOP_STATUS_STOPPING) | 441 if (stop_status_ != ServiceWorkerMetrics::STOP_STATUS_STOPPING) |
| 434 return; | 442 return; |
| 435 stop_status_ = ServiceWorkerMetrics::STOP_STATUS_STALLED; | 443 stop_status_ = ServiceWorkerMetrics::STOP_STATUS_STALLED; |
| 436 if (IsInstalled(owner_->status())) | 444 if (IsInstalled(owner_->status())) |
| 437 ServiceWorkerMetrics::RecordStopWorkerStatus(stop_status_); | 445 ServiceWorkerMetrics::RecordStopWorkerStatus(stop_status_); |
| 438 } | 446 } |
| 439 | 447 |
| 440 private: | 448 private: |
| 449 struct EventStat { |
| 450 size_t fired_events = 0; |
| 451 size_t handled_events = 0; |
| 452 }; |
| 453 |
| 441 ServiceWorkerVersion* owner_; | 454 ServiceWorkerVersion* owner_; |
| 442 size_t fired_events_ = 0; | 455 std::map<EventType, EventStat> event_stats_; |
| 443 size_t handled_events_ = 0; | |
| 444 ServiceWorkerMetrics::StopWorkerStatus stop_status_ = | 456 ServiceWorkerMetrics::StopWorkerStatus stop_status_ = |
| 445 ServiceWorkerMetrics::STOP_STATUS_STOPPING; | 457 ServiceWorkerMetrics::STOP_STATUS_STOPPING; |
| 446 | 458 |
| 447 DISALLOW_COPY_AND_ASSIGN(Metrics); | 459 DISALLOW_COPY_AND_ASSIGN(Metrics); |
| 448 }; | 460 }; |
| 449 | 461 |
| 450 // A controller for periodically sending a ping to the worker to see | 462 // A controller for periodically sending a ping to the worker to see |
| 451 // if the worker is not stalling. | 463 // if the worker is not stalling. |
| 452 class ServiceWorkerVersion::PingController { | 464 class ServiceWorkerVersion::PingController { |
| 453 public: | 465 public: |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 509 const GURL& script_url, | 521 const GURL& script_url, |
| 510 int64 version_id, | 522 int64 version_id, |
| 511 base::WeakPtr<ServiceWorkerContextCore> context) | 523 base::WeakPtr<ServiceWorkerContextCore> context) |
| 512 : version_id_(version_id), | 524 : version_id_(version_id), |
| 513 registration_id_(registration->id()), | 525 registration_id_(registration->id()), |
| 514 script_url_(script_url), | 526 script_url_(script_url), |
| 515 scope_(registration->pattern()), | 527 scope_(registration->pattern()), |
| 516 context_(context), | 528 context_(context), |
| 517 script_cache_map_(this, context), | 529 script_cache_map_(this, context), |
| 518 ping_controller_(new PingController(this)), | 530 ping_controller_(new PingController(this)), |
| 519 metrics_(new Metrics(this)), | |
| 520 weak_factory_(this) { | 531 weak_factory_(this) { |
| 521 DCHECK(context_); | 532 DCHECK(context_); |
| 522 DCHECK(registration); | 533 DCHECK(registration); |
| 523 context_->AddLiveVersion(this); | 534 context_->AddLiveVersion(this); |
| 524 embedded_worker_ = context_->embedded_worker_registry()->CreateWorker(); | 535 embedded_worker_ = context_->embedded_worker_registry()->CreateWorker(); |
| 525 embedded_worker_->AddListener(this); | 536 embedded_worker_->AddListener(this); |
| 526 } | 537 } |
| 527 | 538 |
| 528 ServiceWorkerVersion::~ServiceWorkerVersion() { | 539 ServiceWorkerVersion::~ServiceWorkerVersion() { |
| 529 // The user may have closed the tab waiting for SW to start up. | 540 // The user may have closed the tab waiting for SW to start up. |
| (...skipping 814 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1344 "ServiceWorkerVersion::OnFetchEventFinished", | 1355 "ServiceWorkerVersion::OnFetchEventFinished", |
| 1345 "Request id", request_id); | 1356 "Request id", request_id); |
| 1346 FetchCallback* callback = fetch_callbacks_.Lookup(request_id); | 1357 FetchCallback* callback = fetch_callbacks_.Lookup(request_id); |
| 1347 if (!callback) { | 1358 if (!callback) { |
| 1348 NOTREACHED() << "Got unexpected message: " << request_id; | 1359 NOTREACHED() << "Got unexpected message: " << request_id; |
| 1349 return; | 1360 return; |
| 1350 } | 1361 } |
| 1351 | 1362 |
| 1352 // TODO(kinuko): Record other event statuses too. | 1363 // TODO(kinuko): Record other event statuses too. |
| 1353 const bool handled = (result == SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE); | 1364 const bool handled = (result == SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE); |
| 1354 metrics_->RecordEventStatus(handled); | 1365 metrics_->RecordEventHandledStatus(ServiceWorkerMetrics::EVENT_TYPE_FETCH, |
| 1366 handled); |
| 1355 | 1367 |
| 1356 scoped_refptr<ServiceWorkerVersion> protect(this); | 1368 scoped_refptr<ServiceWorkerVersion> protect(this); |
| 1357 callback->Run(SERVICE_WORKER_OK, result, response); | 1369 callback->Run(SERVICE_WORKER_OK, result, response); |
| 1358 RemoveCallbackAndStopIfRedundant(&fetch_callbacks_, request_id); | 1370 RemoveCallbackAndStopIfRedundant(&fetch_callbacks_, request_id); |
| 1359 } | 1371 } |
| 1360 | 1372 |
| 1361 void ServiceWorkerVersion::OnSyncEventFinished( | 1373 void ServiceWorkerVersion::OnSyncEventFinished( |
| 1362 int request_id, | 1374 int request_id, |
| 1363 blink::WebServiceWorkerEventResult result) { | 1375 blink::WebServiceWorkerEventResult result) { |
| 1364 TRACE_EVENT1("ServiceWorker", | 1376 TRACE_EVENT1("ServiceWorker", |
| (...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1752 } | 1764 } |
| 1753 // Keep the live registration while starting the worker. | 1765 // Keep the live registration while starting the worker. |
| 1754 start_callbacks_.push_back( | 1766 start_callbacks_.push_back( |
| 1755 base::Bind(&RunStartWorkerCallback, callback, protect)); | 1767 base::Bind(&RunStartWorkerCallback, callback, protect)); |
| 1756 StartWorkerInternal(pause_after_download); | 1768 StartWorkerInternal(pause_after_download); |
| 1757 return; | 1769 return; |
| 1758 } | 1770 } |
| 1759 } | 1771 } |
| 1760 | 1772 |
| 1761 void ServiceWorkerVersion::StartWorkerInternal(bool pause_after_download) { | 1773 void ServiceWorkerVersion::StartWorkerInternal(bool pause_after_download) { |
| 1774 if (!metrics_) |
| 1775 metrics_.reset(new Metrics(this)); |
| 1762 if (!timeout_timer_.IsRunning()) | 1776 if (!timeout_timer_.IsRunning()) |
| 1763 StartTimeoutTimer(); | 1777 StartTimeoutTimer(); |
| 1764 if (running_status() == STOPPED) { | 1778 if (running_status() == STOPPED) { |
| 1765 embedded_worker_->Start( | 1779 embedded_worker_->Start( |
| 1766 version_id_, scope_, script_url_, pause_after_download, | 1780 version_id_, scope_, script_url_, pause_after_download, |
| 1767 base::Bind(&ServiceWorkerVersion::OnStartSentAndScriptEvaluated, | 1781 base::Bind(&ServiceWorkerVersion::OnStartSentAndScriptEvaluated, |
| 1768 weak_factory_.GetWeakPtr())); | 1782 weak_factory_.GetWeakPtr())); |
| 1769 } | 1783 } |
| 1770 } | 1784 } |
| 1771 | 1785 |
| (...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2124 return; | 2138 return; |
| 2125 context_->UpdateServiceWorker(registration.get(), | 2139 context_->UpdateServiceWorker(registration.get(), |
| 2126 false /* force_bypass_cache */); | 2140 false /* force_bypass_cache */); |
| 2127 } | 2141 } |
| 2128 | 2142 |
| 2129 void ServiceWorkerVersion::OnStoppedInternal( | 2143 void ServiceWorkerVersion::OnStoppedInternal( |
| 2130 EmbeddedWorkerInstance::Status old_status) { | 2144 EmbeddedWorkerInstance::Status old_status) { |
| 2131 DCHECK_EQ(STOPPED, running_status()); | 2145 DCHECK_EQ(STOPPED, running_status()); |
| 2132 scoped_refptr<ServiceWorkerVersion> protect(this); | 2146 scoped_refptr<ServiceWorkerVersion> protect(this); |
| 2133 | 2147 |
| 2148 DCHECK(metrics_); |
| 2149 metrics_.reset(); |
| 2150 |
| 2134 bool should_restart = !is_redundant() && !start_callbacks_.empty() && | 2151 bool should_restart = !is_redundant() && !start_callbacks_.empty() && |
| 2135 (old_status != EmbeddedWorkerInstance::STARTING); | 2152 (old_status != EmbeddedWorkerInstance::STARTING); |
| 2136 | 2153 |
| 2137 ClearTick(&stop_time_); | 2154 ClearTick(&stop_time_); |
| 2138 StopTimeoutTimer(); | 2155 StopTimeoutTimer(); |
| 2139 | 2156 |
| 2140 if (ping_controller_->IsTimedOut()) | 2157 if (ping_controller_->IsTimedOut()) |
| 2141 should_restart = false; | 2158 should_restart = false; |
| 2142 | 2159 |
| 2143 // Fire all stop callbacks. | 2160 // Fire all stop callbacks. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 2170 | 2187 |
| 2171 streaming_url_request_jobs_.clear(); | 2188 streaming_url_request_jobs_.clear(); |
| 2172 | 2189 |
| 2173 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); | 2190 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); |
| 2174 | 2191 |
| 2175 if (should_restart) | 2192 if (should_restart) |
| 2176 StartWorkerInternal(false /* pause_after_download */); | 2193 StartWorkerInternal(false /* pause_after_download */); |
| 2177 } | 2194 } |
| 2178 | 2195 |
| 2179 } // namespace content | 2196 } // namespace content |
| OLD | NEW |