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