Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / google_apis / gcm / engine / heartbeat_manager.h
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.
4
5 #ifndef GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_
6 #define GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_
7
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 "google_apis/gcm/base/gcm_export.h"
13
14 namespace base {
15 class Timer;
16 }
17
18 namespace mcs_proto {
19 class HeartbeatConfig;
20 }
21
22 namespace gcm {
23
24 // A heartbeat management class, capable of sending and handling heartbeat
25 // receipt/failures and triggering reconnection as necessary.
26 class GCM_EXPORT HeartbeatManager {
27  public:
28   explicit HeartbeatManager(scoped_ptr<base::Timer> heartbeat_timer);
29   ~HeartbeatManager();
30
31   // Start the heartbeat logic.
32   // |send_heartbeat_callback_| is the callback the HeartbeatManager uses to
33   // send new heartbeats. Only one heartbeat can be outstanding at a time.
34   void Start(const base::Closure& send_heartbeat_callback,
35              const base::Closure& trigger_reconnect_callback);
36
37   // Stop the timer. Start(..) must be called again to begin sending heartbeats
38   // afterwards.
39   void Stop();
40
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
43   // determine that the connection is alive).
44   void OnHeartbeatAcked();
45
46   // Updates the current heartbeat interval.
47   void UpdateHeartbeatConfig(const mcs_proto::HeartbeatConfig& config);
48
49   // Returns the next scheduled heartbeat time. A null time means
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
52   // pending.
53   base::TimeTicks GetNextHeartbeatTime() const;
54
55  protected:
56   // Helper method to send heartbeat on timer trigger.
57   void OnHeartbeatTriggered();
58
59   // 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
61   // that).
62   void CheckForMissedHeartbeat();
63
64  private:
65   // Restarts the heartbeat timer.
66   void RestartTimer();
67
68   // The base::Time at which the heartbeat timer is expected to fire. Used to
69   // check if a heartbeat was somehow lost/delayed.
70   base::Time heartbeat_expected_time_;
71
72   // Whether the last heartbeat ping sent has been acknowledged or not.
73   bool waiting_for_ack_;
74
75   // The current heartbeat interval.
76   int heartbeat_interval_ms_;
77   // The most recent server-provided heartbeat interval (0 if none has been
78   // provided).
79   int server_interval_ms_;
80
81   // Timer for triggering heartbeats.
82   scoped_ptr<base::Timer> heartbeat_timer_;
83
84   // Callbacks for interacting with the the connection.
85   base::Closure send_heartbeat_callback_;
86   base::Closure trigger_reconnect_callback_;
87
88   base::WeakPtrFactory<HeartbeatManager> weak_ptr_factory_;
89
90   DISALLOW_COPY_AND_ASSIGN(HeartbeatManager);
91 };
92
93 }  // namespace gcm
94
95 #endif  // GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_