diff options
author | chirantan <chirantan@chromium.org> | 2015-03-09 13:48:58 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-03-09 20:49:43 +0000 |
commit | 7f8789c70bb9049c9357f512e92f8b44c061e892 (patch) | |
tree | bdaf22837f5018673b33b440ac847a3bc39fe404 /google_apis | |
parent | 496c0b55f055b8b421169196a3597246b119bac1 (diff) | |
download | chromium_src-7f8789c70bb9049c9357f512e92f8b44c061e892.zip chromium_src-7f8789c70bb9049c9357f512e92f8b44c061e892.tar.gz chromium_src-7f8789c70bb9049c9357f512e92f8b44c061e892.tar.bz2 |
Use base::PowerObserver to check for missed heartbeats
base::PowerObserver provides a platform-agnostic way to be notified
about system suspend and resume events. Use it to check for missed
heartbeats instead of polling. Unfortunately we still need to poll on
linux because there is no common system-wide notification about suspend
and resume events.
BUG=421725
Review URL: https://codereview.chromium.org/989803002
Cr-Commit-Position: refs/heads/master@{#319730}
Diffstat (limited to 'google_apis')
-rw-r--r-- | google_apis/gcm/engine/heartbeat_manager.cc | 36 | ||||
-rw-r--r-- | google_apis/gcm/engine/heartbeat_manager.h | 8 | ||||
-rw-r--r-- | google_apis/gcm/engine/heartbeat_manager_unittest.cc | 2 |
3 files changed, 37 insertions, 9 deletions
diff --git a/google_apis/gcm/engine/heartbeat_manager.cc b/google_apis/gcm/engine/heartbeat_manager.cc index 4467e0f..53e0344 100644 --- a/google_apis/gcm/engine/heartbeat_manager.cc +++ b/google_apis/gcm/engine/heartbeat_manager.cc @@ -6,6 +6,7 @@ #include "base/callback.h" #include "base/metrics/histogram.h" +#include "base/power_monitor/power_monitor.h" #include "base/time/time.h" #include "base/timer/timer.h" #include "google_apis/gcm/protocol/mcs.pb.h" @@ -20,10 +21,15 @@ const int64 kCellHeartbeatDefaultMs = 1000 * 60 * 28; // 28 minutes. const int64 kWifiHeartbeatDefaultMs = 1000 * 60 * 15; // 15 minutes. // The default heartbeat ack interval. const int64 kHeartbeatAckDefaultMs = 1000 * 60 * 1; // 1 minute. + +#if defined(OS_LINUX) && !defined(OS_CHROMEOS) // The period at which to check if the heartbeat time has passed. Used to // protect against platforms where the timer is delayed by the system being -// suspended. +// suspended. Only needed on linux because the other OSes provide a standard +// way to be notified of system suspend and resume events. const int kHeartbeatMissedCheckMs = 1000 * 60 * 5; // 5 minutes. +#endif // defined(OS_LINUX) && !defined(OS_CHROMEOS) + } // namespace HeartbeatManager::HeartbeatManager() @@ -32,9 +38,19 @@ HeartbeatManager::HeartbeatManager() server_interval_ms_(0), heartbeat_timer_(new base::Timer(true /* retain_user_task */, false /* is_repeating */)), - weak_ptr_factory_(this) {} + weak_ptr_factory_(this) { + // Listen for system suspend and resume events. + base::PowerMonitor* monitor = base::PowerMonitor::Get(); + if (monitor) + monitor->AddObserver(this); +} -HeartbeatManager::~HeartbeatManager() {} +HeartbeatManager::~HeartbeatManager() { + // Stop listening for system suspend and resume events. + base::PowerMonitor* monitor = base::PowerMonitor::Get(); + if (monitor) + monitor->RemoveObserver(this); +} void HeartbeatManager::Start( const base::Closure& send_heartbeat_callback, @@ -96,6 +112,10 @@ void HeartbeatManager::UpdateHeartbeatTimer(scoped_ptr<base::Timer> timer) { heartbeat_timer_->Start(FROM_HERE, remaining_delay, timer_task); } +void HeartbeatManager::OnResume() { + CheckForMissedHeartbeat(); +} + void HeartbeatManager::OnHeartbeatTriggered() { // Reset the weak pointers used for heartbeat checks. weak_ptr_factory_.InvalidateWeakPtrs(); @@ -144,14 +164,16 @@ void HeartbeatManager::RestartTimer() { base::Bind(&HeartbeatManager::OnHeartbeatTriggered, weak_ptr_factory_.GetWeakPtr())); - // TODO(zea): Polling is not a particularly good way to detect the missed - // heartbeat. Ideally we should be listening to wake-from-suspend events, - // although that would require platform-specific implementations. +#if defined(OS_LINUX) && !defined(OS_CHROMEOS) + // Windows, Mac, Android, iOS, and Chrome OS all provide a way to be notified + // when the system is suspending or resuming. The only one that does not is + // Linux so we need to poll to check for missed heartbeats. base::MessageLoop::current()->PostDelayedTask( FROM_HERE, base::Bind(&HeartbeatManager::CheckForMissedHeartbeat, weak_ptr_factory_.GetWeakPtr()), base::TimeDelta::FromMilliseconds(kHeartbeatMissedCheckMs)); +#endif // defined(OS_LINUX) && !defined(OS_CHROMEOS) } void HeartbeatManager::CheckForMissedHeartbeat() { @@ -167,12 +189,14 @@ void HeartbeatManager::CheckForMissedHeartbeat() { return; } +#if defined(OS_LINUX) && !defined(OS_CHROMEOS) // Otherwise check again later. base::MessageLoop::current()->PostDelayedTask( FROM_HERE, base::Bind(&HeartbeatManager::CheckForMissedHeartbeat, weak_ptr_factory_.GetWeakPtr()), base::TimeDelta::FromMilliseconds(kHeartbeatMissedCheckMs)); +#endif // defined(OS_LINUX) && !defined(OS_CHROMEOS) } } // namespace gcm diff --git a/google_apis/gcm/engine/heartbeat_manager.h b/google_apis/gcm/engine/heartbeat_manager.h index 0addf90..96123fda 100644 --- a/google_apis/gcm/engine/heartbeat_manager.h +++ b/google_apis/gcm/engine/heartbeat_manager.h @@ -9,6 +9,7 @@ #include "base/logging.h" #include "base/memory/scoped_ptr.h" #include "base/memory/weak_ptr.h" +#include "base/power_monitor/power_observer.h" #include "google_apis/gcm/base/gcm_export.h" namespace base { @@ -23,10 +24,10 @@ namespace gcm { // A heartbeat management class, capable of sending and handling heartbeat // receipt/failures and triggering reconnection as necessary. -class GCM_EXPORT HeartbeatManager { +class GCM_EXPORT HeartbeatManager : public base::PowerObserver { public: HeartbeatManager(); - ~HeartbeatManager(); + ~HeartbeatManager() override; // Start the heartbeat logic. // |send_heartbeat_callback_| is the callback the HeartbeatManager uses to @@ -55,6 +56,9 @@ class GCM_EXPORT HeartbeatManager { // Updates the timer used for scheduling heartbeats. void UpdateHeartbeatTimer(scoped_ptr<base::Timer> timer); + // base::PowerObserver override. + void OnResume() override; + protected: // Helper method to send heartbeat on timer trigger. void OnHeartbeatTriggered(); diff --git a/google_apis/gcm/engine/heartbeat_manager_unittest.cc b/google_apis/gcm/engine/heartbeat_manager_unittest.cc index 3057e32..2c00623 100644 --- a/google_apis/gcm/engine/heartbeat_manager_unittest.cc +++ b/google_apis/gcm/engine/heartbeat_manager_unittest.cc @@ -26,7 +26,7 @@ mcs_proto::HeartbeatConfig BuildHeartbeatConfig(int interval_ms) { class TestHeartbeatManager : public HeartbeatManager { public: TestHeartbeatManager() {} - virtual ~TestHeartbeatManager() {} + ~TestHeartbeatManager() override {} // Bypass the heartbeat timer, and send the heartbeat now. void TriggerHearbeat(); |