Add a link to crbug.com/512301 to TODO in media_permission.cc regarding pepper media...
[chromium-blink-merge.git] / google_apis / gcm / engine / heartbeat_manager.h
bloba6d47ea241f2cecc4958875e04ac336730832ddc
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"
14 #include "google_apis/gcm/engine/connection_factory.h"
16 namespace base {
17 class Timer;
20 namespace mcs_proto {
21 class HeartbeatConfig;
24 namespace gcm {
26 // A heartbeat management class, capable of sending and handling heartbeat
27 // receipt/failures and triggering reconnection as necessary.
28 class GCM_EXPORT HeartbeatManager : public base::PowerObserver {
29 public:
30 typedef base::Callback<void(ConnectionFactory::ConnectionResetReason)>
31 ReconnectCallback;
33 HeartbeatManager();
34 ~HeartbeatManager() override;
36 // Start the heartbeat logic.
37 // |send_heartbeat_callback_| is the callback the HeartbeatManager uses to
38 // send new heartbeats. Only one heartbeat can be outstanding at a time.
39 void Start(const base::Closure& send_heartbeat_callback,
40 const ReconnectCallback& trigger_reconnect_callback);
42 // Stop the timer. Start(..) must be called again to begin sending heartbeats
43 // afterwards.
44 void Stop();
46 // Reset the heartbeat timer. It is valid to call this even if no heartbeat
47 // is associated with the ack (for example if another signal is used to
48 // determine that the connection is alive).
49 void OnHeartbeatAcked();
51 // Updates the current heartbeat interval.
52 void UpdateHeartbeatConfig(const mcs_proto::HeartbeatConfig& config);
54 // Returns the next scheduled heartbeat time. A null time means
55 // no heartbeat is pending. If non-null and less than the
56 // current time (in ticks), the heartbeat has been triggered and an ack is
57 // pending.
58 base::TimeTicks GetNextHeartbeatTime() const;
60 // Updates the timer used for scheduling heartbeats.
61 void UpdateHeartbeatTimer(scoped_ptr<base::Timer> timer);
63 // base::PowerObserver override.
64 void OnResume() override;
66 // Maximum and minimum of the custom client interval that can be requested,
67 // calculated based on the network conditions.
68 int GetMaxClientHeartbeatIntervalMs();
69 int GetMinClientHeartbeatIntervalMs();
71 // Sets, gets and validates the custom client interval. If the interval is
72 // less than the current custom heartbeat interval, the connection will be
73 // reset to update the receiving server.
74 void SetClientHeartbeatIntervalMs(int interval_ms);
75 int GetClientHeartbeatIntervalMs();
76 bool HasClientHeartbeatInterval();
77 bool IsValidClientHeartbeatInterval(int interval);
79 protected:
80 // Helper method to send heartbeat on timer trigger.
81 void OnHeartbeatTriggered();
83 // Periodic check to see if the heartbeat has been missed due to some system
84 // issue (e.g. the machine was suspended and the timer did not account for
85 // that).
86 void CheckForMissedHeartbeat();
88 private:
89 // Restarts the heartbeat timer.
90 void RestartTimer();
92 // Calculates default heartbeat interval, depending on current network.
93 int GetDefaultHeartbeatInterval();
95 // Stops the heartbeat and triggers connection reset with a |reason|.
96 void ResetConnection(ConnectionFactory::ConnectionResetReason reason);
98 // The base::Time at which the heartbeat timer is expected to fire. Used to
99 // check if a heartbeat was somehow lost/delayed.
100 base::Time heartbeat_expected_time_;
102 // Whether the last heartbeat ping sent has been acknowledged or not.
103 bool waiting_for_ack_;
105 // The current heartbeat interval.
106 int heartbeat_interval_ms_;
107 // The most recent server-provided heartbeat interval (0 if none has been
108 // provided).
109 int server_interval_ms_;
111 // Custom interval requested by the client.
112 int client_interval_ms_;
114 // Timer for triggering heartbeats.
115 scoped_ptr<base::Timer> heartbeat_timer_;
117 // Callbacks for interacting with the the connection.
118 base::Closure send_heartbeat_callback_;
119 ReconnectCallback trigger_reconnect_callback_;
121 base::WeakPtrFactory<HeartbeatManager> weak_ptr_factory_;
123 DISALLOW_COPY_AND_ASSIGN(HeartbeatManager);
126 } // namespace gcm
128 #endif // GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_