OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "google_apis/gcm/engine/heartbeat_manager.h" | 5 #include "google_apis/gcm/engine/heartbeat_manager.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/power_monitor/power_monitor.h" | 10 #include "base/power_monitor/power_monitor.h" |
11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "base/timer/timer.h" | 13 #include "base/timer/timer.h" |
14 #include "google_apis/gcm/protocol/mcs.pb.h" | 14 #include "google_apis/gcm/protocol/mcs.pb.h" |
15 #include "net/base/network_change_notifier.h" | 15 #include "net/base/network_change_notifier.h" |
16 | 16 |
17 namespace gcm { | 17 namespace gcm { |
18 | 18 |
19 namespace { | 19 namespace { |
20 // The default heartbeat when on a mobile or unknown network . | 20 // The default heartbeat when on a mobile or unknown network . |
21 const int kCellHeartbeatDefaultMs = 1000 * 60 * 28; // 28 minutes. | 21 const int kCellHeartbeatDefaultMs = 1000 * 60 * 28; // 28 minutes. |
22 // The default heartbeat when on WiFi (also used for ethernet). | 22 // The default heartbeat when on WiFi (also used for ethernet). |
23 const int kWifiHeartbeatDefaultMs = 1000 * 60 * 15; // 15 minutes. | 23 const int kWifiHeartbeatDefaultMs = 1000 * 60 * 15; // 15 minutes. |
24 // The default heartbeat ack interval. | 24 // The default heartbeat ack interval. |
25 const int kHeartbeatAckDefaultMs = 1000 * 60 * 1; // 1 minute. | 25 const int kHeartbeatAckDefaultMs = 1000 * 60 * 1; // 1 minute. |
26 // Minimum allowed client default heartbeat interval. | 26 // Minimum allowed client default heartbeat interval. |
27 const int kMinClientHeartbeatIntervalMs = 1000 * 60 * 2; // 2 minutes. | 27 const int kMinClientHeartbeatIntervalMs = 1000 * 60 * 2; // 2 minutes. |
| 28 // Minimum time spent sleeping before we force a new heartbeat. |
| 29 const int kMinSuspendTimeMs = 1000 * 10; // 10 seconds. |
28 | 30 |
29 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 31 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
30 // The period at which to check if the heartbeat time has passed. Used to | 32 // The period at which to check if the heartbeat time has passed. Used to |
31 // protect against platforms where the timer is delayed by the system being | 33 // protect against platforms where the timer is delayed by the system being |
32 // suspended. Only needed on linux because the other OSes provide a standard | 34 // suspended. Only needed on linux because the other OSes provide a standard |
33 // way to be notified of system suspend and resume events. | 35 // way to be notified of system suspend and resume events. |
34 const int kHeartbeatMissedCheckMs = 1000 * 60 * 5; // 5 minutes. | 36 const int kHeartbeatMissedCheckMs = 1000 * 60 * 5; // 5 minutes. |
35 #endif // defined(OS_LINUX) && !defined(OS_CHROMEOS) | 37 #endif // defined(OS_LINUX) && !defined(OS_CHROMEOS) |
36 | 38 |
37 } // namespace | 39 } // namespace |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 heartbeat_timer_->desired_run_time() - base::TimeTicks::Now(); | 113 heartbeat_timer_->desired_run_time() - base::TimeTicks::Now(); |
112 base::Closure timer_task(heartbeat_timer_->user_task()); | 114 base::Closure timer_task(heartbeat_timer_->user_task()); |
113 | 115 |
114 heartbeat_timer_->Stop(); | 116 heartbeat_timer_->Stop(); |
115 heartbeat_timer_ = timer.Pass(); | 117 heartbeat_timer_ = timer.Pass(); |
116 | 118 |
117 if (was_running) | 119 if (was_running) |
118 heartbeat_timer_->Start(FROM_HERE, remaining_delay, timer_task); | 120 heartbeat_timer_->Start(FROM_HERE, remaining_delay, timer_task); |
119 } | 121 } |
120 | 122 |
| 123 void HeartbeatManager::OnSuspend() { |
| 124 // The system is going to sleep. Record the time, so on resume we know how |
| 125 // much time the machine was suspended. |
| 126 suspend_time_ = base::Time::Now(); |
| 127 } |
| 128 |
121 void HeartbeatManager::OnResume() { | 129 void HeartbeatManager::OnResume() { |
122 CheckForMissedHeartbeat(); | 130 // The system just resumed from sleep. It's likely that the connection to |
| 131 // MCS was silently lost during that time, even if a heartbeat is not yet |
| 132 // due. Force a heartbeat to detect if the connection is still good. |
| 133 base::TimeDelta elapsed = base::Time::Now() - suspend_time_; |
| 134 UMA_HISTOGRAM_LONG_TIMES("GCM.SuspendTime", elapsed); |
| 135 |
| 136 // Make sure a minimum amount of time has passed before forcing a heartbeat to |
| 137 // avoid any tight loop scenarios. |
| 138 if (elapsed > base::TimeDelta::FromMilliseconds(kMinSuspendTimeMs)) |
| 139 OnHeartbeatTriggered(); |
123 } | 140 } |
124 | 141 |
125 void HeartbeatManager::OnHeartbeatTriggered() { | 142 void HeartbeatManager::OnHeartbeatTriggered() { |
126 // Reset the weak pointers used for heartbeat checks. | 143 // Reset the weak pointers used for heartbeat checks. |
127 weak_ptr_factory_.InvalidateWeakPtrs(); | 144 weak_ptr_factory_.InvalidateWeakPtrs(); |
128 | 145 |
129 if (waiting_for_ack_) { | 146 if (waiting_for_ack_) { |
130 LOG(WARNING) << "Lost connection to MCS, reconnecting."; | 147 LOG(WARNING) << "Lost connection to MCS, reconnecting."; |
131 ResetConnection(ConnectionFactory::HEARTBEAT_FAILURE); | 148 ResetConnection(ConnectionFactory::HEARTBEAT_FAILURE); |
132 return; | 149 return; |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 interval <= max_heartbeat_interval; | 275 interval <= max_heartbeat_interval; |
259 } | 276 } |
260 | 277 |
261 void HeartbeatManager::ResetConnection( | 278 void HeartbeatManager::ResetConnection( |
262 ConnectionFactory::ConnectionResetReason reason) { | 279 ConnectionFactory::ConnectionResetReason reason) { |
263 Stop(); | 280 Stop(); |
264 trigger_reconnect_callback_.Run(reason); | 281 trigger_reconnect_callback_.Run(reason); |
265 } | 282 } |
266 | 283 |
267 } // namespace gcm | 284 } // namespace gcm |
OLD | NEW |