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/weak_ptr.h"
11 #include "base/timer/timer.h"
12 #include "google_apis/gcm/base/gcm_export.h"
15 class HeartbeatConfig
;
20 // A heartbeat management class, capable of sending and handling heartbeat
21 // receipt/failures and triggering reconnection as necessary.
22 class GCM_EXPORT HeartbeatManager
{
27 // Start the heartbeat logic.
28 // |send_heartbeat_callback_| is the callback the HeartbeatManager uses to
29 // send new heartbeats. Only one heartbeat can be outstanding at a time.
30 void Start(const base::Closure
& send_heartbeat_callback
,
31 const base::Closure
& trigger_reconnect_callback
);
33 // Stop the timer. Start(..) must be called again to begin sending heartbeats
37 // Reset the heartbeat timer. It is valid to call this even if no heartbeat
38 // is associated with the ack (for example if another signal is used to
39 // determine that the connection is alive).
40 void OnHeartbeatAcked();
42 // Updates the current heartbeat interval.
43 void UpdateHeartbeatConfig(const mcs_proto::HeartbeatConfig
& config
);
45 // Returns the next scheduled heartbeat time. A null time means
46 // no heartbeat is pending. If non-null and less than the
47 // current time (in ticks), the heartbeat has been triggered and an ack is
49 base::TimeTicks
GetNextHeartbeatTime() const;
52 // Helper method to send heartbeat on timer trigger.
53 void OnHeartbeatTriggered();
56 // Restarts the heartbeat timer.
59 // Whether the last heartbeat ping sent has been acknowledged or not.
60 bool waiting_for_ack_
;
62 // The current heartbeat interval.
63 int heartbeat_interval_ms_
;
64 // The most recent server-provided heartbeat interval (0 if none has been
66 int server_interval_ms_
;
68 // Timer for triggering heartbeats.
69 base::Timer heartbeat_timer_
;
71 // Callbacks for interacting with the the connection.
72 base::Closure send_heartbeat_callback_
;
73 base::Closure trigger_reconnect_callback_
;
75 base::WeakPtrFactory
<HeartbeatManager
> weak_ptr_factory_
;
77 DISALLOW_COPY_AND_ASSIGN(HeartbeatManager
);
82 #endif // GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_