| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_ | 5 #ifndef GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_ |
| 6 #define GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_ | 6 #define GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_ |
| 7 | 7 |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "google_apis/gcm/base/gcm_export.h" | 12 #include "google_apis/gcm/base/gcm_export.h" |
| 13 | 13 |
| 14 namespace base { | 14 namespace base { |
| 15 class Timer; | 15 class Timer; |
| 16 } | 16 } |
| 17 | 17 |
| 18 namespace mcs_proto { | 18 namespace mcs_proto { |
| 19 class HeartbeatConfig; | 19 class HeartbeatConfig; |
| 20 } | 20 } |
| 21 | 21 |
| 22 namespace gcm { | 22 namespace gcm { |
| 23 | 23 |
| 24 // A heartbeat management class, capable of sending and handling heartbeat | 24 // A heartbeat management class, capable of sending and handling heartbeat |
| 25 // receipt/failures and triggering reconnection as necessary. | 25 // receipt/failures and triggering reconnection as necessary. |
| 26 class GCM_EXPORT HeartbeatManager { | 26 class GCM_EXPORT HeartbeatManager { |
| 27 public: | 27 public: |
| 28 explicit HeartbeatManager(scoped_ptr<base::Timer> heartbeat_timer); | 28 HeartbeatManager(); |
| 29 ~HeartbeatManager(); | 29 ~HeartbeatManager(); |
| 30 | 30 |
| 31 // Start the heartbeat logic. | 31 // Start the heartbeat logic. |
| 32 // |send_heartbeat_callback_| is the callback the HeartbeatManager uses to | 32 // |send_heartbeat_callback_| is the callback the HeartbeatManager uses to |
| 33 // send new heartbeats. Only one heartbeat can be outstanding at a time. | 33 // send new heartbeats. Only one heartbeat can be outstanding at a time. |
| 34 void Start(const base::Closure& send_heartbeat_callback, | 34 void Start(const base::Closure& send_heartbeat_callback, |
| 35 const base::Closure& trigger_reconnect_callback); | 35 const base::Closure& trigger_reconnect_callback); |
| 36 | 36 |
| 37 // Stop the timer. Start(..) must be called again to begin sending heartbeats | 37 // Stop the timer. Start(..) must be called again to begin sending heartbeats |
| 38 // afterwards. | 38 // afterwards. |
| 39 void Stop(); | 39 void Stop(); |
| 40 | 40 |
| 41 // Reset the heartbeat timer. It is valid to call this even if no heartbeat | 41 // Reset the heartbeat timer. It is valid to call this even if no heartbeat |
| 42 // is associated with the ack (for example if another signal is used to | 42 // is associated with the ack (for example if another signal is used to |
| 43 // determine that the connection is alive). | 43 // determine that the connection is alive). |
| 44 void OnHeartbeatAcked(); | 44 void OnHeartbeatAcked(); |
| 45 | 45 |
| 46 // Updates the current heartbeat interval. | 46 // Updates the current heartbeat interval. |
| 47 void UpdateHeartbeatConfig(const mcs_proto::HeartbeatConfig& config); | 47 void UpdateHeartbeatConfig(const mcs_proto::HeartbeatConfig& config); |
| 48 | 48 |
| 49 // Returns the next scheduled heartbeat time. A null time means | 49 // Returns the next scheduled heartbeat time. A null time means |
| 50 // no heartbeat is pending. If non-null and less than the | 50 // no heartbeat is pending. If non-null and less than the |
| 51 // current time (in ticks), the heartbeat has been triggered and an ack is | 51 // current time (in ticks), the heartbeat has been triggered and an ack is |
| 52 // pending. | 52 // pending. |
| 53 base::TimeTicks GetNextHeartbeatTime() const; | 53 base::TimeTicks GetNextHeartbeatTime() const; |
| 54 | 54 |
| 55 // Updates the timer used for scheduling heartbeats. |
| 56 void UpdateHeartbeatTimer(scoped_ptr<base::Timer> timer); |
| 57 |
| 55 protected: | 58 protected: |
| 56 // Helper method to send heartbeat on timer trigger. | 59 // Helper method to send heartbeat on timer trigger. |
| 57 void OnHeartbeatTriggered(); | 60 void OnHeartbeatTriggered(); |
| 58 | 61 |
| 59 // Periodic check to see if the heartbeat has been missed due to some system | 62 // Periodic check to see if the heartbeat has been missed due to some system |
| 60 // issue (e.g. the machine was suspended and the timer did not account for | 63 // issue (e.g. the machine was suspended and the timer did not account for |
| 61 // that). | 64 // that). |
| 62 void CheckForMissedHeartbeat(); | 65 void CheckForMissedHeartbeat(); |
| 63 | 66 |
| 64 private: | 67 private: |
| (...skipping 21 matching lines...) Expand all Loading... |
| 86 base::Closure trigger_reconnect_callback_; | 89 base::Closure trigger_reconnect_callback_; |
| 87 | 90 |
| 88 base::WeakPtrFactory<HeartbeatManager> weak_ptr_factory_; | 91 base::WeakPtrFactory<HeartbeatManager> weak_ptr_factory_; |
| 89 | 92 |
| 90 DISALLOW_COPY_AND_ASSIGN(HeartbeatManager); | 93 DISALLOW_COPY_AND_ASSIGN(HeartbeatManager); |
| 91 }; | 94 }; |
| 92 | 95 |
| 93 } // namespace gcm | 96 } // namespace gcm |
| 94 | 97 |
| 95 #endif // GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_ | 98 #endif // GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_ |
| OLD | NEW |