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/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/time/time.h" |
8 #include "base/timer/timer.h" | 10 #include "base/timer/timer.h" |
9 #include "google_apis/gcm/protocol/mcs.pb.h" | 11 #include "google_apis/gcm/protocol/mcs.pb.h" |
10 #include "net/base/network_change_notifier.h" | 12 #include "net/base/network_change_notifier.h" |
11 | 13 |
12 namespace gcm { | 14 namespace gcm { |
13 | 15 |
14 namespace { | 16 namespace { |
15 // The default heartbeat when on a mobile or unknown network . | 17 // The default heartbeat when on a mobile or unknown network . |
16 const int64 kCellHeartbeatDefaultMs = 1000 * 60 * 28; // 28 minutes. | 18 const int64 kCellHeartbeatDefaultMs = 1000 * 60 * 28; // 28 minutes. |
17 // The default heartbeat when on WiFi (also used for ethernet). | 19 // The default heartbeat when on WiFi (also used for ethernet). |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 server_interval_ms_ = config.interval_ms(); | 75 server_interval_ms_ = config.interval_ms(); |
74 } | 76 } |
75 | 77 |
76 base::TimeTicks HeartbeatManager::GetNextHeartbeatTime() const { | 78 base::TimeTicks HeartbeatManager::GetNextHeartbeatTime() const { |
77 if (heartbeat_timer_->IsRunning()) | 79 if (heartbeat_timer_->IsRunning()) |
78 return heartbeat_timer_->desired_run_time(); | 80 return heartbeat_timer_->desired_run_time(); |
79 else | 81 else |
80 return base::TimeTicks(); | 82 return base::TimeTicks(); |
81 } | 83 } |
82 | 84 |
| 85 void HeartbeatManager::UpdateHeartbeatTimer(scoped_ptr<base::Timer> timer) { |
| 86 bool was_running = heartbeat_timer_->IsRunning(); |
| 87 base::TimeDelta remaining_delay = |
| 88 heartbeat_timer_->desired_run_time() - base::TimeTicks::Now(); |
| 89 base::Closure timer_task(heartbeat_timer_->user_task()); |
| 90 |
| 91 heartbeat_timer_->Stop(); |
| 92 heartbeat_timer_ = timer.Pass(); |
| 93 |
| 94 if (was_running) |
| 95 heartbeat_timer_->Start(FROM_HERE, remaining_delay, timer_task); |
| 96 } |
| 97 |
83 void HeartbeatManager::OnHeartbeatTriggered() { | 98 void HeartbeatManager::OnHeartbeatTriggered() { |
84 // Reset the weak pointers used for heartbeat checks. | 99 // Reset the weak pointers used for heartbeat checks. |
85 weak_ptr_factory_.InvalidateWeakPtrs(); | 100 weak_ptr_factory_.InvalidateWeakPtrs(); |
86 | 101 |
87 if (waiting_for_ack_) { | 102 if (waiting_for_ack_) { |
88 LOG(WARNING) << "Lost connection to MCS, reconnecting."; | 103 LOG(WARNING) << "Lost connection to MCS, reconnecting."; |
89 Stop(); | 104 Stop(); |
90 trigger_reconnect_callback_.Run(); | 105 trigger_reconnect_callback_.Run(); |
91 return; | 106 return; |
92 } | 107 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 168 |
154 // Otherwise check again later. | 169 // Otherwise check again later. |
155 base::MessageLoop::current()->PostDelayedTask( | 170 base::MessageLoop::current()->PostDelayedTask( |
156 FROM_HERE, | 171 FROM_HERE, |
157 base::Bind(&HeartbeatManager::CheckForMissedHeartbeat, | 172 base::Bind(&HeartbeatManager::CheckForMissedHeartbeat, |
158 weak_ptr_factory_.GetWeakPtr()), | 173 weak_ptr_factory_.GetWeakPtr()), |
159 base::TimeDelta::FromMilliseconds(kHeartbeatMissedCheckMs)); | 174 base::TimeDelta::FromMilliseconds(kHeartbeatMissedCheckMs)); |
160 } | 175 } |
161 | 176 |
162 } // namespace gcm | 177 } // namespace gcm |
OLD | NEW |