[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / google_apis / gcm / engine / heartbeat_manager.h
blob9266b98c8d4b21f02935002cd106dd314c27e40a
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"
14 namespace mcs_proto {
15 class HeartbeatConfig;
18 namespace gcm {
20 // A heartbeat management class, capable of sending and handling heartbeat
21 // receipt/failures and triggering reconnection as necessary.
22 class GCM_EXPORT HeartbeatManager {
23 public:
24 HeartbeatManager();
25 ~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
34 // afterwards.
35 void Stop();
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
48 // pending.
49 base::TimeTicks GetNextHeartbeatTime() const;
51 protected:
52 // Helper method to send heartbeat on timer trigger.
53 void OnHeartbeatTriggered();
55 private:
56 // Restarts the heartbeat timer.
57 void RestartTimer();
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
65 // provided).
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);
80 } // namespace gcm
82 #endif // GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_