Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / google_apis / gcm / engine / heartbeat_manager.cc
blob4467e0f4642953b9d229cb5400043b530c4b6901
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "google_apis/gcm/engine/heartbeat_manager.h"
7 #include "base/callback.h"
8 #include "base/metrics/histogram.h"
9 #include "base/time/time.h"
10 #include "base/timer/timer.h"
11 #include "google_apis/gcm/protocol/mcs.pb.h"
12 #include "net/base/network_change_notifier.h"
14 namespace gcm {
16 namespace {
17 // The default heartbeat when on a mobile or unknown network .
18 const int64 kCellHeartbeatDefaultMs = 1000 * 60 * 28; // 28 minutes.
19 // The default heartbeat when on WiFi (also used for ethernet).
20 const int64 kWifiHeartbeatDefaultMs = 1000 * 60 * 15; // 15 minutes.
21 // The default heartbeat ack interval.
22 const int64 kHeartbeatAckDefaultMs = 1000 * 60 * 1; // 1 minute.
23 // The period at which to check if the heartbeat time has passed. Used to
24 // protect against platforms where the timer is delayed by the system being
25 // suspended.
26 const int kHeartbeatMissedCheckMs = 1000 * 60 * 5; // 5 minutes.
27 } // namespace
29 HeartbeatManager::HeartbeatManager()
30 : waiting_for_ack_(false),
31 heartbeat_interval_ms_(0),
32 server_interval_ms_(0),
33 heartbeat_timer_(new base::Timer(true /* retain_user_task */,
34 false /* is_repeating */)),
35 weak_ptr_factory_(this) {}
37 HeartbeatManager::~HeartbeatManager() {}
39 void HeartbeatManager::Start(
40 const base::Closure& send_heartbeat_callback,
41 const base::Closure& trigger_reconnect_callback) {
42 DCHECK(!send_heartbeat_callback.is_null());
43 DCHECK(!trigger_reconnect_callback.is_null());
44 send_heartbeat_callback_ = send_heartbeat_callback;
45 trigger_reconnect_callback_ = trigger_reconnect_callback;
47 // Kicks off the timer.
48 waiting_for_ack_ = false;
49 RestartTimer();
52 void HeartbeatManager::Stop() {
53 heartbeat_expected_time_ = base::Time();
54 heartbeat_timer_->Stop();
55 waiting_for_ack_ = false;
58 void HeartbeatManager::OnHeartbeatAcked() {
59 if (!heartbeat_timer_->IsRunning())
60 return;
62 DCHECK(!send_heartbeat_callback_.is_null());
63 DCHECK(!trigger_reconnect_callback_.is_null());
64 waiting_for_ack_ = false;
65 RestartTimer();
68 void HeartbeatManager::UpdateHeartbeatConfig(
69 const mcs_proto::HeartbeatConfig& config) {
70 if (!config.IsInitialized() ||
71 !config.has_interval_ms() ||
72 config.interval_ms() <= 0) {
73 return;
75 DVLOG(1) << "Updating heartbeat interval to " << config.interval_ms();
76 server_interval_ms_ = config.interval_ms();
79 base::TimeTicks HeartbeatManager::GetNextHeartbeatTime() const {
80 if (heartbeat_timer_->IsRunning())
81 return heartbeat_timer_->desired_run_time();
82 else
83 return base::TimeTicks();
86 void HeartbeatManager::UpdateHeartbeatTimer(scoped_ptr<base::Timer> timer) {
87 bool was_running = heartbeat_timer_->IsRunning();
88 base::TimeDelta remaining_delay =
89 heartbeat_timer_->desired_run_time() - base::TimeTicks::Now();
90 base::Closure timer_task(heartbeat_timer_->user_task());
92 heartbeat_timer_->Stop();
93 heartbeat_timer_ = timer.Pass();
95 if (was_running)
96 heartbeat_timer_->Start(FROM_HERE, remaining_delay, timer_task);
99 void HeartbeatManager::OnHeartbeatTriggered() {
100 // Reset the weak pointers used for heartbeat checks.
101 weak_ptr_factory_.InvalidateWeakPtrs();
103 if (waiting_for_ack_) {
104 LOG(WARNING) << "Lost connection to MCS, reconnecting.";
105 Stop();
106 trigger_reconnect_callback_.Run();
107 return;
110 waiting_for_ack_ = true;
111 RestartTimer();
112 send_heartbeat_callback_.Run();
115 void HeartbeatManager::RestartTimer() {
116 if (!waiting_for_ack_) {
117 // Recalculate the timer interval based network type.
118 if (server_interval_ms_ != 0) {
119 // If a server interval is set, it overrides any local one.
120 heartbeat_interval_ms_ = server_interval_ms_;
121 } else if (net::NetworkChangeNotifier::GetConnectionType() ==
122 net::NetworkChangeNotifier::CONNECTION_WIFI ||
123 net::NetworkChangeNotifier::GetConnectionType() ==
124 net::NetworkChangeNotifier::CONNECTION_ETHERNET) {
125 heartbeat_interval_ms_ = kWifiHeartbeatDefaultMs;
126 } else {
127 // For unknown connections, use the longer cellular heartbeat interval.
128 heartbeat_interval_ms_ = kCellHeartbeatDefaultMs;
130 DVLOG(1) << "Sending next heartbeat in "
131 << heartbeat_interval_ms_ << " ms.";
132 } else {
133 heartbeat_interval_ms_ = kHeartbeatAckDefaultMs;
134 DVLOG(1) << "Resetting timer for ack with "
135 << heartbeat_interval_ms_ << " ms interval.";
138 heartbeat_expected_time_ =
139 base::Time::Now() +
140 base::TimeDelta::FromMilliseconds(heartbeat_interval_ms_);
141 heartbeat_timer_->Start(FROM_HERE,
142 base::TimeDelta::FromMilliseconds(
143 heartbeat_interval_ms_),
144 base::Bind(&HeartbeatManager::OnHeartbeatTriggered,
145 weak_ptr_factory_.GetWeakPtr()));
147 // TODO(zea): Polling is not a particularly good way to detect the missed
148 // heartbeat. Ideally we should be listening to wake-from-suspend events,
149 // although that would require platform-specific implementations.
150 base::MessageLoop::current()->PostDelayedTask(
151 FROM_HERE,
152 base::Bind(&HeartbeatManager::CheckForMissedHeartbeat,
153 weak_ptr_factory_.GetWeakPtr()),
154 base::TimeDelta::FromMilliseconds(kHeartbeatMissedCheckMs));
157 void HeartbeatManager::CheckForMissedHeartbeat() {
158 // If there's no heartbeat pending, return without doing anything.
159 if (heartbeat_expected_time_.is_null())
160 return;
162 // If the heartbeat has been missed, manually trigger it.
163 if (base::Time::Now() > heartbeat_expected_time_) {
164 UMA_HISTOGRAM_LONG_TIMES("GCM.HeartbeatMissedDelta",
165 base::Time::Now() - heartbeat_expected_time_);
166 OnHeartbeatTriggered();
167 return;
170 // Otherwise check again later.
171 base::MessageLoop::current()->PostDelayedTask(
172 FROM_HERE,
173 base::Bind(&HeartbeatManager::CheckForMissedHeartbeat,
174 weak_ptr_factory_.GetWeakPtr()),
175 base::TimeDelta::FromMilliseconds(kHeartbeatMissedCheckMs));
178 } // namespace gcm