Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / extensions / browser / api / cast_channel / keep_alive_delegate.h
blob2526ff580d4ac7547761187643b073975c3039c3
1 // Copyright 2015 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 EXTENSIONS_BROWSER_API_CAST_CHANNEL_KEEP_ALIVE_DELEGATE_H_
6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_KEEP_ALIVE_DELEGATE_H_
8 #include "base/threading/thread_checker.h"
9 #include "base/timer/timer.h"
10 #include "extensions/browser/api/cast_channel/cast_transport.h"
11 #include "extensions/common/api/cast_channel/cast_channel.pb.h"
13 namespace extensions {
14 namespace core_api {
15 namespace cast_channel {
17 class CastSocket;
18 class Logger;
20 // Decorator delegate which provides keep-alive functionality.
21 // Keep-alive messages are handled by this object; all other messages and
22 // errors are passed to |inner_delegate_|.
23 class KeepAliveDelegate : public CastTransport::Delegate {
24 public:
25 // |socket|: The socket to be kept alive.
26 // |logger|: The logging object which collects protocol events and error
27 // details.
28 // |inner_delegate|: The delegate which processes all non-keep-alive
29 // messages. This object assumes ownership of
30 // |inner_delegate|.
31 // |ping_interval|: The amount of idle time to wait before sending a PING to
32 // the remote end.
33 // |liveness_timeout|: The amount of idle time to wait before terminating the
34 // connection.
35 KeepAliveDelegate(CastSocket* socket,
36 scoped_refptr<Logger> logger,
37 scoped_ptr<CastTransport::Delegate> inner_delegate,
38 base::TimeDelta ping_interval,
39 base::TimeDelta liveness_timeout);
41 ~KeepAliveDelegate() override;
43 // Creates a keep-alive message (e.g. PING or PONG).
44 static CastMessage CreateKeepAliveMessage(const char* message_type);
46 void SetTimersForTest(scoped_ptr<base::Timer> injected_ping_timer,
47 scoped_ptr<base::Timer> injected_liveness_timer);
49 // CastTransport::Delegate implementation.
50 void Start() override;
51 void OnError(ChannelError error_state) override;
52 void OnMessage(const CastMessage& message) override;
54 static const char kHeartbeatPingType[];
55 static const char kHeartbeatPongType[];
57 private:
58 // Restarts the ping/liveness timeout timers. Called when a message
59 // is received from the remote end.
60 void ResetTimers();
62 // Sends a formatted PING or PONG message to the remote side.
63 void SendKeepAliveMessage(const CastMessage& message,
64 const char* message_type) const;
66 // Callback for SendKeepAliveMessage.
67 void SendKeepAliveMessageComplete(const char* message_type, int rv) const;
69 // Called when the liveness timer expires, indicating that the remote
70 // end has not responded within the |liveness_timeout_| interval.
71 void LivenessTimeout() const;
73 // Indicates that Start() was called.
74 bool started_;
76 // Socket that is managed by the keep-alive object.
77 CastSocket* socket_;
79 // Logging object.
80 scoped_refptr<Logger> logger_;
82 // Delegate object which receives all non-keep alive messages.
83 scoped_ptr<CastTransport::Delegate> inner_delegate_;
85 // Amount of idle time to wait before disconnecting.
86 base::TimeDelta liveness_timeout_;
88 // Amount of idle time to wait before pinging the receiver.
89 base::TimeDelta ping_interval_;
91 // Fired when |ping_interval_| is exceeded or when triggered by test code.
92 scoped_ptr<base::Timer> ping_timer_;
94 // Fired when |liveness_timer_| is exceeded.
95 scoped_ptr<base::Timer> liveness_timer_;
97 // The PING message to send over the wire.
98 CastMessage ping_message_;
100 // The PONG message to send over the wire.
101 CastMessage pong_message_;
103 base::ThreadChecker thread_checker_;
105 DISALLOW_COPY_AND_ASSIGN(KeepAliveDelegate);
108 } // namespace cast_channel
109 } // namespace core_api
110 } // namespace extensions
112 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_KEEP_ALIVE_DELEGATE_H_