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 807 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1337 "ServiceWorkerVersion::OnFetchEventFinished", | 1348 "ServiceWorkerVersion::OnFetchEventFinished", |
1338 "Request id", request_id); | 1349 "Request id", request_id); |
1339 FetchCallback* callback = fetch_callbacks_.Lookup(request_id); | 1350 FetchCallback* callback = fetch_callbacks_.Lookup(request_id); |
1340 if (!callback) { | 1351 if (!callback) { |
1341 NOTREACHED() << "Got unexpected message: " << request_id; | 1352 NOTREACHED() << "Got unexpected message: " << request_id; |
1342 return; | 1353 return; |
1343 } | 1354 } |
1344 | 1355 |
1345 // TODO(kinuko): Record other event statuses too. | 1356 // TODO(kinuko): Record other event statuses too. |
1346 const bool handled = (result == SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE); | 1357 const bool handled = (result == SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE); |
1347 metrics_->RecordEventStatus(handled); | 1358 metrics_->RecordEventHandledStatus(ServiceWorkerMetrics::EVENT_TYPE_FETCH, |
| 1359 handled); |
1348 | 1360 |
1349 scoped_refptr<ServiceWorkerVersion> protect(this); | 1361 scoped_refptr<ServiceWorkerVersion> protect(this); |
1350 callback->Run(SERVICE_WORKER_OK, result, response); | 1362 callback->Run(SERVICE_WORKER_OK, result, response); |
1351 RemoveCallbackAndStopIfRedundant(&fetch_callbacks_, request_id); | 1363 RemoveCallbackAndStopIfRedundant(&fetch_callbacks_, request_id); |
1352 } | 1364 } |
1353 | 1365 |
1354 void ServiceWorkerVersion::OnSyncEventFinished( | 1366 void ServiceWorkerVersion::OnSyncEventFinished( |
1355 int request_id, | 1367 int request_id, |
1356 blink::WebServiceWorkerEventResult result) { | 1368 blink::WebServiceWorkerEventResult result) { |
1357 TRACE_EVENT1("ServiceWorker", | 1369 TRACE_EVENT1("ServiceWorker", |
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1740 } | 1752 } |
1741 // Keep the live registration while starting the worker. | 1753 // Keep the live registration while starting the worker. |
1742 start_callbacks_.push_back( | 1754 start_callbacks_.push_back( |
1743 base::Bind(&RunStartWorkerCallback, callback, protect)); | 1755 base::Bind(&RunStartWorkerCallback, callback, protect)); |
1744 StartWorkerInternal(pause_after_download); | 1756 StartWorkerInternal(pause_after_download); |
1745 return; | 1757 return; |
1746 } | 1758 } |
1747 } | 1759 } |
1748 | 1760 |
1749 void ServiceWorkerVersion::StartWorkerInternal(bool pause_after_download) { | 1761 void ServiceWorkerVersion::StartWorkerInternal(bool pause_after_download) { |
| 1762 if (!metrics_) |
| 1763 metrics_.reset(new Metrics(this)); |
1750 if (!timeout_timer_.IsRunning()) | 1764 if (!timeout_timer_.IsRunning()) |
1751 StartTimeoutTimer(); | 1765 StartTimeoutTimer(); |
1752 if (running_status() == STOPPED) { | 1766 if (running_status() == STOPPED) { |
1753 embedded_worker_->Start( | 1767 embedded_worker_->Start( |
1754 version_id_, scope_, script_url_, pause_after_download, | 1768 version_id_, scope_, script_url_, pause_after_download, |
1755 base::Bind(&ServiceWorkerVersion::OnStartSentAndScriptEvaluated, | 1769 base::Bind(&ServiceWorkerVersion::OnStartSentAndScriptEvaluated, |
1756 weak_factory_.GetWeakPtr())); | 1770 weak_factory_.GetWeakPtr())); |
1757 } | 1771 } |
1758 } | 1772 } |
1759 | 1773 |
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2112 return; | 2126 return; |
2113 context_->UpdateServiceWorker(registration.get(), | 2127 context_->UpdateServiceWorker(registration.get(), |
2114 false /* force_bypass_cache */); | 2128 false /* force_bypass_cache */); |
2115 } | 2129 } |
2116 | 2130 |
2117 void ServiceWorkerVersion::OnStoppedInternal( | 2131 void ServiceWorkerVersion::OnStoppedInternal( |
2118 EmbeddedWorkerInstance::Status old_status) { | 2132 EmbeddedWorkerInstance::Status old_status) { |
2119 DCHECK_EQ(STOPPED, running_status()); | 2133 DCHECK_EQ(STOPPED, running_status()); |
2120 scoped_refptr<ServiceWorkerVersion> protect(this); | 2134 scoped_refptr<ServiceWorkerVersion> protect(this); |
2121 | 2135 |
| 2136 DCHECK(metrics_); |
| 2137 metrics_.reset(); |
| 2138 |
2122 bool should_restart = !is_redundant() && !start_callbacks_.empty() && | 2139 bool should_restart = !is_redundant() && !start_callbacks_.empty() && |
2123 (old_status != EmbeddedWorkerInstance::STARTING); | 2140 (old_status != EmbeddedWorkerInstance::STARTING); |
2124 | 2141 |
2125 ClearTick(&stop_time_); | 2142 ClearTick(&stop_time_); |
2126 StopTimeoutTimer(); | 2143 StopTimeoutTimer(); |
2127 | 2144 |
2128 if (ping_controller_->IsTimedOut()) | 2145 if (ping_controller_->IsTimedOut()) |
2129 should_restart = false; | 2146 should_restart = false; |
2130 | 2147 |
2131 // Fire all stop callbacks. | 2148 // Fire all stop callbacks. |
(...skipping 26 matching lines...) Expand all Loading... |
2158 | 2175 |
2159 streaming_url_request_jobs_.clear(); | 2176 streaming_url_request_jobs_.clear(); |
2160 | 2177 |
2161 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); | 2178 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); |
2162 | 2179 |
2163 if (should_restart) | 2180 if (should_restart) |
2164 StartWorkerInternal(false /* pause_after_download */); | 2181 StartWorkerInternal(false /* pause_after_download */); |
2165 } | 2182 } |
2166 | 2183 |
2167 } // namespace content | 2184 } // namespace content |
OLD | NEW |