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
3 // found in the LICENSE file.
5 #ifndef GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_
6 #define GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_
8 #include "base/callback.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/power_monitor/power_observer.h"
13 #include "google_apis/gcm/base/gcm_export.h"
20 class HeartbeatConfig
;
25 // A heartbeat management class, capable of sending and handling heartbeat
26 // receipt/failures and triggering reconnection as necessary.
27 class GCM_EXPORT HeartbeatManager
: public base::PowerObserver
{
30 ~HeartbeatManager() override
;
32 // Start the heartbeat logic.
33 // |send_heartbeat_callback_| is the callback the HeartbeatManager uses to
34 // send new heartbeats. Only one heartbeat can be outstanding at a time.
35 void Start(const base::Closure
& send_heartbeat_callback
,
36 const base::Closure
& trigger_reconnect_callback
);
38 // Stop the timer. Start(..) must be called again to begin sending heartbeats
42 // Reset the heartbeat timer. It is valid to call this even if no heartbeat
43 // is associated with the ack (for example if another signal is used to
44 // determine that the connection is alive).
45 void OnHeartbeatAcked();
47 // Updates the current heartbeat interval.
48 void UpdateHeartbeatConfig(const mcs_proto::HeartbeatConfig
& config
);
50 // Returns the next scheduled heartbeat time. A null time means
51 // no heartbeat is pending. If non-null and less than the
52 // current time (in ticks), the heartbeat has been triggered and an ack is
54 base::TimeTicks
GetNextHeartbeatTime() const;
56 // Updates the timer used for scheduling heartbeats.
57 void UpdateHeartbeatTimer(scoped_ptr
<base::Timer
> timer
);
59 // base::PowerObserver override.
60 void OnResume() override
;
63 // Helper method to send heartbeat on timer trigger.
64 void OnHeartbeatTriggered();
66 // Periodic check to see if the heartbeat has been missed due to some system
67 // issue (e.g. the machine was suspended and the timer did not account for
69 void CheckForMissedHeartbeat();
72 // Restarts the heartbeat timer.
75 // The base::Time at which the heartbeat timer is expected to fire. Used to
76 // check if a heartbeat was somehow lost/delayed.
77 base::Time heartbeat_expected_time_
;
79 // Whether the last heartbeat ping sent has been acknowledged or not.
80 bool waiting_for_ack_
;
82 // The current heartbeat interval.
83 int heartbeat_interval_ms_
;
84 // The most recent server-provided heartbeat interval (0 if none has been
86 int server_interval_ms_
;
88 // Timer for triggering heartbeats.
89 scoped_ptr
<base::Timer
> heartbeat_timer_
;
91 // Callbacks for interacting with the the connection.
92 base::Closure send_heartbeat_callback_
;
93 base::Closure trigger_reconnect_callback_
;
95 base::WeakPtrFactory
<HeartbeatManager
> weak_ptr_factory_
;
97 DISALLOW_COPY_AND_ASSIGN(HeartbeatManager
);
102 #endif // GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_