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
{
15 namespace cast_channel
{
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
{
25 // |socket|: The socket to be kept alive.
26 // |logger|: The logging object which collects protocol events and error
28 // |inner_delegate|: The delegate which processes all non-keep-alive
29 // messages. This object assumes ownership of
31 // |ping_interval|: The amount of idle time to wait before sending a PING to
33 // |liveness_timeout|: The amount of idle time to wait before terminating the
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
[];
58 // Restarts the ping/liveness timeout timers. Called when a message
59 // is received from the remote end.
62 // Sends a formatted PING or PONG message to the remote side.
63 void SendKeepAliveMessage(const CastMessage
& message
,
64 const char* message_type
);
66 // Callback for SendKeepAliveMessage.
67 void SendKeepAliveMessageComplete(const char* message_type
, int rv
);
69 // Called when the liveness timer expires, indicating that the remote
70 // end has not responded within the |liveness_timeout_| interval.
71 void LivenessTimeout();
73 // Stops the ping and liveness timers if they are started.
74 // To be called after an error.
77 // Indicates that Start() was called.
80 // Socket that is managed by the keep-alive object.
84 scoped_refptr
<Logger
> logger_
;
86 // Delegate object which receives all non-keep alive messages.
87 scoped_ptr
<CastTransport::Delegate
> inner_delegate_
;
89 // Amount of idle time to wait before disconnecting.
90 base::TimeDelta liveness_timeout_
;
92 // Amount of idle time to wait before pinging the receiver.
93 base::TimeDelta ping_interval_
;
95 // Fired when |ping_interval_| is exceeded or when triggered by test code.
96 scoped_ptr
<base::Timer
> ping_timer_
;
98 // Fired when |liveness_timer_| is exceeded.
99 scoped_ptr
<base::Timer
> liveness_timer_
;
101 // The PING message to send over the wire.
102 CastMessage ping_message_
;
104 // The PONG message to send over the wire.
105 CastMessage pong_message_
;
107 base::ThreadChecker thread_checker_
;
109 DISALLOW_COPY_AND_ASSIGN(KeepAliveDelegate
);
112 } // namespace cast_channel
114 } // namespace extensions
116 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_KEEP_ALIVE_DELEGATE_H_