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

Side by Side Diff: google_apis/gcm/engine/heartbeat_manager.cc

Issue 745123002: Link GCM heartbeat with wake on wifi preference (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix missing include Created 6 years 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 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).
18 const int64 kWifiHeartbeatDefaultMs = 1000 * 60 * 15; // 15 minutes. 20 const int64 kWifiHeartbeatDefaultMs = 1000 * 60 * 15; // 15 minutes.
19 // The default heartbeat ack interval. 21 // The default heartbeat ack interval.
20 const int64 kHeartbeatAckDefaultMs = 1000 * 60 * 1; // 1 minute. 22 const int64 kHeartbeatAckDefaultMs = 1000 * 60 * 1; // 1 minute.
21 // The period at which to check if the heartbeat time has passed. Used to 23 // The period at which to check if the heartbeat time has passed. Used to
22 // protect against platforms where the timer is delayed by the system being 24 // protect against platforms where the timer is delayed by the system being
23 // suspended. 25 // suspended.
24 const int kHeartbeatMissedCheckMs = 1000 * 60 * 5; // 5 minutes. 26 const int kHeartbeatMissedCheckMs = 1000 * 60 * 5; // 5 minutes.
25 } // namespace 27 } // namespace
26 28
27 HeartbeatManager::HeartbeatManager(scoped_ptr<base::Timer> heartbeat_timer) 29 HeartbeatManager::HeartbeatManager()
28 : waiting_for_ack_(false), 30 : waiting_for_ack_(false),
29 heartbeat_interval_ms_(0), 31 heartbeat_interval_ms_(0),
30 server_interval_ms_(0), 32 server_interval_ms_(0),
31 heartbeat_timer_(heartbeat_timer.Pass()), 33 heartbeat_timer_(new base::Timer(true /* retain_user_task */,
34 false /* is_repeating */)),
32 weak_ptr_factory_(this) {} 35 weak_ptr_factory_(this) {}
33 36
34 HeartbeatManager::~HeartbeatManager() {} 37 HeartbeatManager::~HeartbeatManager() {}
35 38
36 void HeartbeatManager::Start( 39 void HeartbeatManager::Start(
37 const base::Closure& send_heartbeat_callback, 40 const base::Closure& send_heartbeat_callback,
38 const base::Closure& trigger_reconnect_callback) { 41 const base::Closure& trigger_reconnect_callback) {
39 DCHECK(!send_heartbeat_callback.is_null()); 42 DCHECK(!send_heartbeat_callback.is_null());
40 DCHECK(!trigger_reconnect_callback.is_null()); 43 DCHECK(!trigger_reconnect_callback.is_null());
41 send_heartbeat_callback_ = send_heartbeat_callback; 44 send_heartbeat_callback_ = send_heartbeat_callback;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 server_interval_ms_ = config.interval_ms(); 76 server_interval_ms_ = config.interval_ms();
74 } 77 }
75 78
76 base::TimeTicks HeartbeatManager::GetNextHeartbeatTime() const { 79 base::TimeTicks HeartbeatManager::GetNextHeartbeatTime() const {
77 if (heartbeat_timer_->IsRunning()) 80 if (heartbeat_timer_->IsRunning())
78 return heartbeat_timer_->desired_run_time(); 81 return heartbeat_timer_->desired_run_time();
79 else 82 else
80 return base::TimeTicks(); 83 return base::TimeTicks();
81 } 84 }
82 85
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());
91
92 heartbeat_timer_->Stop();
93 heartbeat_timer_ = timer.Pass();
94
95 if (was_running)
96 heartbeat_timer_->Start(FROM_HERE, remaining_delay, timer_task);
97 }
98
83 void HeartbeatManager::OnHeartbeatTriggered() { 99 void HeartbeatManager::OnHeartbeatTriggered() {
84 // Reset the weak pointers used for heartbeat checks. 100 // Reset the weak pointers used for heartbeat checks.
85 weak_ptr_factory_.InvalidateWeakPtrs(); 101 weak_ptr_factory_.InvalidateWeakPtrs();
86 102
87 if (waiting_for_ack_) { 103 if (waiting_for_ack_) {
88 LOG(WARNING) << "Lost connection to MCS, reconnecting."; 104 LOG(WARNING) << "Lost connection to MCS, reconnecting.";
89 Stop(); 105 Stop();
90 trigger_reconnect_callback_.Run(); 106 trigger_reconnect_callback_.Run();
91 return; 107 return;
92 } 108 }
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 169
154 // Otherwise check again later. 170 // Otherwise check again later.
155 base::MessageLoop::current()->PostDelayedTask( 171 base::MessageLoop::current()->PostDelayedTask(
156 FROM_HERE, 172 FROM_HERE,
157 base::Bind(&HeartbeatManager::CheckForMissedHeartbeat, 173 base::Bind(&HeartbeatManager::CheckForMissedHeartbeat,
158 weak_ptr_factory_.GetWeakPtr()), 174 weak_ptr_factory_.GetWeakPtr()),
159 base::TimeDelta::FromMilliseconds(kHeartbeatMissedCheckMs)); 175 base::TimeDelta::FromMilliseconds(kHeartbeatMissedCheckMs));
160 } 176 }
161 177
162 } // namespace gcm 178 } // namespace gcm
OLDNEW
« no previous file with comments | « google_apis/gcm/engine/heartbeat_manager.h ('k') | google_apis/gcm/engine/heartbeat_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698