| 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/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 9 #include "base/power_monitor/power_monitor.h" | 10 #include "base/power_monitor/power_monitor.h" |
| 11 #include "base/thread_task_runner_handle.h" |
| 10 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 11 #include "base/timer/timer.h" | 13 #include "base/timer/timer.h" |
| 12 #include "google_apis/gcm/protocol/mcs.pb.h" | 14 #include "google_apis/gcm/protocol/mcs.pb.h" |
| 13 #include "net/base/network_change_notifier.h" | 15 #include "net/base/network_change_notifier.h" |
| 14 | 16 |
| 15 namespace gcm { | 17 namespace gcm { |
| 16 | 18 |
| 17 namespace { | 19 namespace { |
| 18 // The default heartbeat when on a mobile or unknown network . | 20 // The default heartbeat when on a mobile or unknown network . |
| 19 const int kCellHeartbeatDefaultMs = 1000 * 60 * 28; // 28 minutes. | 21 const int kCellHeartbeatDefaultMs = 1000 * 60 * 28; // 28 minutes. |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 heartbeat_timer_->Start(FROM_HERE, | 169 heartbeat_timer_->Start(FROM_HERE, |
| 168 base::TimeDelta::FromMilliseconds( | 170 base::TimeDelta::FromMilliseconds( |
| 169 heartbeat_interval_ms_), | 171 heartbeat_interval_ms_), |
| 170 base::Bind(&HeartbeatManager::OnHeartbeatTriggered, | 172 base::Bind(&HeartbeatManager::OnHeartbeatTriggered, |
| 171 weak_ptr_factory_.GetWeakPtr())); | 173 weak_ptr_factory_.GetWeakPtr())); |
| 172 | 174 |
| 173 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 175 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 174 // Windows, Mac, Android, iOS, and Chrome OS all provide a way to be notified | 176 // Windows, Mac, Android, iOS, and Chrome OS all provide a way to be notified |
| 175 // when the system is suspending or resuming. The only one that does not is | 177 // when the system is suspending or resuming. The only one that does not is |
| 176 // Linux so we need to poll to check for missed heartbeats. | 178 // Linux so we need to poll to check for missed heartbeats. |
| 177 base::MessageLoop::current()->PostDelayedTask( | 179 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 178 FROM_HERE, | 180 FROM_HERE, |
| 179 base::Bind(&HeartbeatManager::CheckForMissedHeartbeat, | 181 base::Bind(&HeartbeatManager::CheckForMissedHeartbeat, |
| 180 weak_ptr_factory_.GetWeakPtr()), | 182 weak_ptr_factory_.GetWeakPtr()), |
| 181 base::TimeDelta::FromMilliseconds(kHeartbeatMissedCheckMs)); | 183 base::TimeDelta::FromMilliseconds(kHeartbeatMissedCheckMs)); |
| 182 #endif // defined(OS_LINUX) && !defined(OS_CHROMEOS) | 184 #endif // defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 183 } | 185 } |
| 184 | 186 |
| 185 void HeartbeatManager::CheckForMissedHeartbeat() { | 187 void HeartbeatManager::CheckForMissedHeartbeat() { |
| 186 // If there's no heartbeat pending, return without doing anything. | 188 // If there's no heartbeat pending, return without doing anything. |
| 187 if (heartbeat_expected_time_.is_null()) | 189 if (heartbeat_expected_time_.is_null()) |
| 188 return; | 190 return; |
| 189 | 191 |
| 190 // If the heartbeat has been missed, manually trigger it. | 192 // If the heartbeat has been missed, manually trigger it. |
| 191 if (base::Time::Now() > heartbeat_expected_time_) { | 193 if (base::Time::Now() > heartbeat_expected_time_) { |
| 192 UMA_HISTOGRAM_LONG_TIMES("GCM.HeartbeatMissedDelta", | 194 UMA_HISTOGRAM_LONG_TIMES("GCM.HeartbeatMissedDelta", |
| 193 base::Time::Now() - heartbeat_expected_time_); | 195 base::Time::Now() - heartbeat_expected_time_); |
| 194 OnHeartbeatTriggered(); | 196 OnHeartbeatTriggered(); |
| 195 return; | 197 return; |
| 196 } | 198 } |
| 197 | 199 |
| 198 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 200 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 199 // Otherwise check again later. | 201 // Otherwise check again later. |
| 200 base::MessageLoop::current()->PostDelayedTask( | 202 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 201 FROM_HERE, | 203 FROM_HERE, |
| 202 base::Bind(&HeartbeatManager::CheckForMissedHeartbeat, | 204 base::Bind(&HeartbeatManager::CheckForMissedHeartbeat, |
| 203 weak_ptr_factory_.GetWeakPtr()), | 205 weak_ptr_factory_.GetWeakPtr()), |
| 204 base::TimeDelta::FromMilliseconds(kHeartbeatMissedCheckMs)); | 206 base::TimeDelta::FromMilliseconds(kHeartbeatMissedCheckMs)); |
| 205 #endif // defined(OS_LINUX) && !defined(OS_CHROMEOS) | 207 #endif // defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 206 } | 208 } |
| 207 | 209 |
| 208 int HeartbeatManager::GetDefaultHeartbeatInterval() { | 210 int HeartbeatManager::GetDefaultHeartbeatInterval() { |
| 209 // For unknown connections, use the longer cellular heartbeat interval. | 211 // For unknown connections, use the longer cellular heartbeat interval. |
| 210 int heartbeat_interval_ms = kCellHeartbeatDefaultMs; | 212 int heartbeat_interval_ms = kCellHeartbeatDefaultMs; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 interval <= max_heartbeat_interval; | 258 interval <= max_heartbeat_interval; |
| 257 } | 259 } |
| 258 | 260 |
| 259 void HeartbeatManager::ResetConnection( | 261 void HeartbeatManager::ResetConnection( |
| 260 ConnectionFactory::ConnectionResetReason reason) { | 262 ConnectionFactory::ConnectionResetReason reason) { |
| 261 Stop(); | 263 Stop(); |
| 262 trigger_reconnect_callback_.Run(reason); | 264 trigger_reconnect_callback_.Run(reason); |
| 263 } | 265 } |
| 264 | 266 |
| 265 } // namespace gcm | 267 } // namespace gcm |
| OLD | NEW |