Add ICU message format support
[chromium-blink-merge.git] / extensions / browser / api / cast_channel / keep_alive_delegate.h
blobaf7fd44d5a3bde6facbaf987b8e54f666bd419c7
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 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);
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.
75 void Stop();
77 // Indicates that Start() was called.
78 bool started_;
80 // Socket that is managed by the keep-alive object.
81 CastSocket* socket_;
83 // Logging 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
113 } // namespace api
114 } // namespace extensions
116 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_KEEP_ALIVE_DELEGATE_H_