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

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

Issue 1187623006: Service Worker: Update stale workers (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review comments Created 5 years, 6 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/service_worker_version.h" 5 #include "content/browser/service_worker/service_worker_version.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/metrics/histogram_macros.h" 10 #include "base/metrics/histogram_macros.h"
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 private: 408 private:
409 size_t fired_events = 0; 409 size_t fired_events = 0;
410 size_t handled_events = 0; 410 size_t handled_events = 0;
411 DISALLOW_COPY_AND_ASSIGN(Metrics); 411 DISALLOW_COPY_AND_ASSIGN(Metrics);
412 }; 412 };
413 413
414 // A controller for periodically sending a ping to the worker to see 414 // A controller for periodically sending a ping to the worker to see
415 // if the worker is not stalling. 415 // if the worker is not stalling.
416 class ServiceWorkerVersion::PingController { 416 class ServiceWorkerVersion::PingController {
417 public: 417 public:
418 PingController(ServiceWorkerVersion* version) : version_(version) {} 418 explicit PingController(ServiceWorkerVersion* version) : version_(version) {}
419 ~PingController() {} 419 ~PingController() {}
420 420
421 void Activate() { ping_state_ = PINGING; } 421 void Activate() { ping_state_ = PINGING; }
422 422
423 void Deactivate() { 423 void Deactivate() {
424 ClearTick(&ping_time_); 424 ClearTick(&ping_time_);
425 ping_state_ = NOT_PINGING; 425 ping_state_ = NOT_PINGING;
426 } 426 }
427 427
428 void OnPongReceived() { ClearTick(&ping_time_); } 428 void OnPongReceived() { ClearTick(&ping_time_); }
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
597 } 597 }
598 } 598 }
599 stop_callbacks_.push_back(callback); 599 stop_callbacks_.push_back(callback);
600 } 600 }
601 601
602 void ServiceWorkerVersion::ScheduleUpdate() { 602 void ServiceWorkerVersion::ScheduleUpdate() {
603 if (update_timer_.IsRunning()) { 603 if (update_timer_.IsRunning()) {
604 update_timer_.Reset(); 604 update_timer_.Reset();
605 return; 605 return;
606 } 606 }
607 update_timer_.Start( 607 update_timer_.Start(FROM_HERE,
608 FROM_HERE, base::TimeDelta::FromSeconds(kUpdateDelaySeconds), 608 base::TimeDelta::FromSeconds(kUpdateDelaySeconds),
609 base::Bind(&ServiceWorkerVersion::StartUpdate, 609 base::Bind(&ServiceWorkerVersion::StartUpdate, this));
kinuko 2015/06/22 14:02:53 Hmm. This one makes the timer's internal task hold
kinuko 2015/06/22 14:21:09 Maybe we could rely on context or some external pa
falken 2015/06/23 07:53:27 Yeah I don't see a great way to do this, I'm think
610 weak_factory_.GetWeakPtr()));
611 } 610 }
612 611
613 void ServiceWorkerVersion::DeferScheduledUpdate() { 612 void ServiceWorkerVersion::DeferScheduledUpdate() {
614 if (update_timer_.IsRunning()) 613 if (update_timer_.IsRunning())
615 update_timer_.Reset(); 614 update_timer_.Reset();
616 } 615 }
617 616
618 void ServiceWorkerVersion::StartUpdate() { 617 void ServiceWorkerVersion::StartUpdate() {
619 update_timer_.Stop(); 618 update_timer_.Stop();
620 if (!context_) 619 if (!context_)
621 return; 620 return;
622 ServiceWorkerRegistration* registration = 621 context_->storage()->FindRegistrationForId(
623 context_->GetLiveRegistration(registration_id_); 622 registration_id_, scope_.GetOrigin(),
624 if (!registration || !registration->GetNewestVersion()) 623 base::Bind(&ServiceWorkerVersion::FoundRegistrationForUpdate, this));
625 return;
626 context_->UpdateServiceWorker(registration, false /* force_bypass_cache */);
627 } 624 }
628 625
629 void ServiceWorkerVersion::DispatchMessageEvent( 626 void ServiceWorkerVersion::DispatchMessageEvent(
630 const base::string16& message, 627 const base::string16& message,
631 const std::vector<TransferredMessagePort>& sent_message_ports, 628 const std::vector<TransferredMessagePort>& sent_message_ports,
632 const StatusCallback& callback) { 629 const StatusCallback& callback) {
633 for (const TransferredMessagePort& port : sent_message_ports) { 630 for (const TransferredMessagePort& port : sent_message_ports) {
634 MessagePortService::GetInstance()->HoldMessages(port.id); 631 MessagePortService::GetInstance()->HoldMessages(port.id);
635 } 632 }
636 633
(...skipping 1081 matching lines...) Expand 10 before | Expand all | Expand 10 after
1718 RecordStartWorkerResult(status); 1715 RecordStartWorkerResult(status);
1719 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED)); 1716 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED));
1720 return; 1717 return;
1721 } 1718 }
1722 if (is_redundant()) { 1719 if (is_redundant()) {
1723 RecordStartWorkerResult(SERVICE_WORKER_ERROR_REDUNDANT); 1720 RecordStartWorkerResult(SERVICE_WORKER_ERROR_REDUNDANT);
1724 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_REDUNDANT)); 1721 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_REDUNDANT));
1725 return; 1722 return;
1726 } 1723 }
1727 1724
1725 MarkIfStale();
1726
1728 switch (running_status()) { 1727 switch (running_status()) {
1729 case RUNNING: 1728 case RUNNING:
1730 RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); 1729 RunSoon(base::Bind(callback, SERVICE_WORKER_OK));
1731 return; 1730 return;
1732 case STOPPING: 1731 case STOPPING:
1733 case STOPPED: 1732 case STOPPED:
1734 case STARTING: 1733 case STARTING:
1735 if (start_callbacks_.empty()) { 1734 if (start_callbacks_.empty()) {
1736 start_callbacks_.push_back( 1735 start_callbacks_.push_back(
1737 base::Bind(&ServiceWorkerVersion::RecordStartWorkerResult, 1736 base::Bind(&ServiceWorkerVersion::RecordStartWorkerResult,
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
1830 // Ping will be activated in OnScriptLoaded. 1829 // Ping will be activated in OnScriptLoaded.
1831 ping_controller_->Deactivate(); 1830 ping_controller_->Deactivate();
1832 1831
1833 timeout_timer_.Start(FROM_HERE, 1832 timeout_timer_.Start(FROM_HERE,
1834 base::TimeDelta::FromSeconds(kTimeoutTimerDelaySeconds), 1833 base::TimeDelta::FromSeconds(kTimeoutTimerDelaySeconds),
1835 this, &ServiceWorkerVersion::OnTimeoutTimer); 1834 this, &ServiceWorkerVersion::OnTimeoutTimer);
1836 } 1835 }
1837 1836
1838 void ServiceWorkerVersion::StopTimeoutTimer() { 1837 void ServiceWorkerVersion::StopTimeoutTimer() {
1839 timeout_timer_.Stop(); 1838 timeout_timer_.Stop();
1839
1840 // Trigger update if worker is stale.
1841 if (!stale_time_.is_null()) {
1842 ClearTick(&stale_time_);
1843 if (!update_timer_.IsRunning())
1844 ScheduleUpdate();
1845 }
1840 } 1846 }
1841 1847
1842 void ServiceWorkerVersion::OnTimeoutTimer() { 1848 void ServiceWorkerVersion::OnTimeoutTimer() {
1843 DCHECK(running_status() == STARTING || running_status() == RUNNING || 1849 DCHECK(running_status() == STARTING || running_status() == RUNNING ||
1844 running_status() == STOPPING) 1850 running_status() == STOPPING)
1845 << running_status(); 1851 << running_status();
1846 1852
1853 MarkIfStale();
1854
1855 // Trigger update if worker is stale and we waited long enough for it to go
1856 // idle.
1857 if (GetTickDuration(stale_time_) >
1858 base::TimeDelta::FromMinutes(kRequestTimeoutMinutes)) {
1859 ClearTick(&stale_time_);
1860 if (!update_timer_.IsRunning())
1861 ScheduleUpdate();
1862 }
1863
1847 // Starting a worker hasn't finished within a certain period. 1864 // Starting a worker hasn't finished within a certain period.
1848 if (GetTickDuration(start_time_) > 1865 if (GetTickDuration(start_time_) >
1849 base::TimeDelta::FromMinutes(kStartWorkerTimeoutMinutes)) { 1866 base::TimeDelta::FromMinutes(kStartWorkerTimeoutMinutes)) {
1850 DCHECK(running_status() == STARTING || running_status() == STOPPING) 1867 DCHECK(running_status() == STARTING || running_status() == STOPPING)
1851 << running_status(); 1868 << running_status();
1852 scoped_refptr<ServiceWorkerVersion> protect(this); 1869 scoped_refptr<ServiceWorkerVersion> protect(this);
1853 RunCallbacks(this, &start_callbacks_, SERVICE_WORKER_ERROR_TIMEOUT); 1870 RunCallbacks(this, &start_callbacks_, SERVICE_WORKER_ERROR_TIMEOUT);
1854 if (running_status() == STARTING) 1871 if (running_status() == STARTING)
1855 embedded_worker_->Stop(); 1872 embedded_worker_->Stop();
1856 return; 1873 return;
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
2049 case net::ERR_ABORTED: 2066 case net::ERR_ABORTED:
2050 return SERVICE_WORKER_ERROR_ABORT; 2067 return SERVICE_WORKER_ERROR_ABORT;
2051 default: 2068 default:
2052 return SERVICE_WORKER_ERROR_NETWORK; 2069 return SERVICE_WORKER_ERROR_NETWORK;
2053 } 2070 }
2054 } 2071 }
2055 2072
2056 return default_code; 2073 return default_code;
2057 } 2074 }
2058 2075
2076 void ServiceWorkerVersion::MarkIfStale() {
2077 if (!context_)
2078 return;
2079 if (update_timer_.IsRunning() || !stale_time_.is_null())
2080 return;
2081 ServiceWorkerRegistration* registration =
2082 context_->GetLiveRegistration(registration_id_);
2083 if (!registration || registration->active_version() != this)
2084 return;
2085 base::TimeDelta time_since_last_check =
2086 base::Time::Now() - registration->last_update_check();
2087 if (time_since_last_check > base::TimeDelta::FromHours(24))
kinuko 2015/06/22 14:21:08 Could we use a constant or have a comment for '24'
2088 RestartTick(&stale_time_);
2089 }
2090
2091 void ServiceWorkerVersion::FoundRegistrationForUpdate(
2092 ServiceWorkerStatusCode status,
2093 const scoped_refptr<ServiceWorkerRegistration>& registration) {
2094 if (status != SERVICE_WORKER_OK || registration->active_version() != this)
2095 return;
2096 context_->UpdateServiceWorker(registration.get(),
2097 false /* force_bypass_cache */);
2098 }
2099
2059 } // namespace content 2100 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698