OLD | NEW |
(Empty) | |
| 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 EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_ |
| 6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_ |
| 7 |
| 8 #include <queue> |
| 9 #include <string> |
| 10 |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/sequence_checker.h" |
| 14 #include "base/threading/thread_checker.h" |
| 15 #include "components/cast_channel/cast_channel_enum.h" |
| 16 #include "extensions/browser/api/cast_channel/logger.h" |
| 17 #include "extensions/common/api/cast_channel/logging.pb.h" |
| 18 #include "net/base/completion_callback.h" |
| 19 #include "net/base/ip_endpoint.h" |
| 20 |
| 21 namespace net { |
| 22 class DrainableIOBuffer; |
| 23 class DrainableIOBuffer; |
| 24 class GrowableIOBuffer; |
| 25 class Socket; |
| 26 } // namespace net |
| 27 |
| 28 namespace extensions { |
| 29 namespace api { |
| 30 namespace cast_channel { |
| 31 class CastMessage; |
| 32 class MessageFramer; |
| 33 |
| 34 class CastTransport { |
| 35 public: |
| 36 virtual ~CastTransport() {} |
| 37 |
| 38 // Object to be informed of incoming messages and read errors. |
| 39 class Delegate { |
| 40 public: |
| 41 using ChannelError = ::cast_channel::ChannelError; |
| 42 |
| 43 virtual ~Delegate() {} |
| 44 |
| 45 // Called once Transport is successfully initialized and started. |
| 46 // Owned read delegates are Start()ed automatically. |
| 47 virtual void Start() = 0; |
| 48 |
| 49 // An error occurred on the channel. |
| 50 // The caller is responsible for closing |socket| if an error occurred. |
| 51 virtual void OnError(ChannelError error_state) = 0; |
| 52 |
| 53 // A message was received on the channel. |
| 54 virtual void OnMessage(const CastMessage& message) = 0; |
| 55 }; |
| 56 |
| 57 // Sends a CastMessage to |socket_|. |
| 58 // |message|: The message to send. |
| 59 // |callback|: Callback to be invoked when the write operation has finished. |
| 60 // Virtual for testing. |
| 61 virtual void SendMessage(const CastMessage& message, |
| 62 const net::CompletionCallback& callback) = 0; |
| 63 |
| 64 // Initializes the reading state machine and starts reading from the |
| 65 // underlying socket. |
| 66 // Virtual for testing. |
| 67 virtual void Start() = 0; |
| 68 |
| 69 // Changes the delegate for processing read events. Pending reads remain |
| 70 // in-flight. |
| 71 // Ownership of the pointee of |delegate| is assumed by the transport. |
| 72 // Prior delegates are deleted automatically. |
| 73 virtual void SetReadDelegate(std::unique_ptr<Delegate> delegate) = 0; |
| 74 }; |
| 75 |
| 76 // Manager class for reading and writing messages to/from a socket. |
| 77 class CastTransportImpl : public CastTransport { |
| 78 public: |
| 79 using ChannelAuthType = ::cast_channel::ChannelAuthType; |
| 80 using ChannelError = ::cast_channel::ChannelError; |
| 81 |
| 82 // Adds a CastMessage read/write layer to a socket. |
| 83 // Message read events are propagated to the owner via |read_delegate|. |
| 84 // |vlog_prefix| sets the prefix used for all VLOGged output. |
| 85 // |socket| and |logger| must all out-live the |
| 86 // CastTransportImpl instance. |
| 87 // |read_delegate| is owned by this CastTransportImpl object. |
| 88 CastTransportImpl(net::Socket* socket, |
| 89 int channel_id, |
| 90 const net::IPEndPoint& ip_endpoint_, |
| 91 ChannelAuthType channel_auth_, |
| 92 scoped_refptr<Logger> logger); |
| 93 |
| 94 ~CastTransportImpl() override; |
| 95 |
| 96 // CastTransport interface. |
| 97 void SendMessage(const CastMessage& message, |
| 98 const net::CompletionCallback& callback) override; |
| 99 void Start() override; |
| 100 void SetReadDelegate(std::unique_ptr<Delegate> delegate) override; |
| 101 |
| 102 private: |
| 103 // Internal write states. |
| 104 enum WriteState { |
| 105 WRITE_STATE_UNKNOWN, |
| 106 WRITE_STATE_WRITE, |
| 107 WRITE_STATE_WRITE_COMPLETE, |
| 108 WRITE_STATE_DO_CALLBACK, |
| 109 WRITE_STATE_HANDLE_ERROR, |
| 110 WRITE_STATE_ERROR, |
| 111 WRITE_STATE_IDLE, |
| 112 }; |
| 113 |
| 114 // Internal read states. |
| 115 enum ReadState { |
| 116 READ_STATE_UNKNOWN, |
| 117 READ_STATE_READ, |
| 118 READ_STATE_READ_COMPLETE, |
| 119 READ_STATE_DO_CALLBACK, |
| 120 READ_STATE_HANDLE_ERROR, |
| 121 READ_STATE_ERROR, |
| 122 }; |
| 123 |
| 124 // Holds a message to be written to the socket. |callback| is invoked when the |
| 125 // message is fully written or an error occurrs. |
| 126 struct WriteRequest { |
| 127 explicit WriteRequest(const std::string& namespace_, |
| 128 const std::string& payload, |
| 129 const net::CompletionCallback& callback); |
| 130 WriteRequest(const WriteRequest& other); |
| 131 ~WriteRequest(); |
| 132 |
| 133 // Namespace of the serialized message. |
| 134 std::string message_namespace; |
| 135 // Write completion callback, invoked when the operation has completed or |
| 136 // failed. |
| 137 net::CompletionCallback callback; |
| 138 // Buffer with outgoing data. |
| 139 scoped_refptr<net::DrainableIOBuffer> io_buffer; |
| 140 }; |
| 141 |
| 142 static proto::ReadState ReadStateToProto(CastTransportImpl::ReadState state); |
| 143 static proto::WriteState WriteStateToProto( |
| 144 CastTransportImpl::WriteState state); |
| 145 static proto::ErrorState ErrorStateToProto(ChannelError state); |
| 146 static bool IsTerminalReadState(ReadState read_state); |
| 147 static bool IsTerminalWriteState(WriteState write_state); |
| 148 |
| 149 void SetReadState(ReadState read_state); |
| 150 void SetWriteState(WriteState write_state); |
| 151 void SetErrorState(ChannelError error_state); |
| 152 |
| 153 // Terminates all in-flight write callbacks with error code ERR_FAILED. |
| 154 void FlushWriteQueue(); |
| 155 |
| 156 // Main method that performs write flow state transitions. |
| 157 void OnWriteResult(int result); |
| 158 |
| 159 // Each of the below Do* method is executed in the corresponding |
| 160 // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE |
| 161 // DowriteComplete is called, and so on. |
| 162 int DoWrite(); |
| 163 int DoWriteComplete(int result); |
| 164 int DoWriteCallback(); |
| 165 int DoWriteHandleError(int result); |
| 166 |
| 167 // Main method that performs write flow state transitions. |
| 168 void OnReadResult(int result); |
| 169 |
| 170 // Each of the below Do* method is executed in the corresponding |
| 171 // write state. For example when read state is READ_STATE_READ_COMPLETE |
| 172 // DoReadComplete is called, and so on. |
| 173 int DoRead(); |
| 174 int DoReadComplete(int result); |
| 175 int DoReadCallback(); |
| 176 int DoReadHandleError(int result); |
| 177 |
| 178 // Indicates that the transport object is started and may receive and send |
| 179 // messages. |
| 180 bool started_; |
| 181 |
| 182 // Queue of pending writes. The message at the front of the queue is the one |
| 183 // being written. |
| 184 std::queue<WriteRequest> write_queue_; |
| 185 |
| 186 // Buffer used for read operations. Reused for every read. |
| 187 scoped_refptr<net::GrowableIOBuffer> read_buffer_; |
| 188 |
| 189 // Constructs and parses the wire representation of message frames. |
| 190 std::unique_ptr<MessageFramer> framer_; |
| 191 |
| 192 // Last message received on the socket. |
| 193 std::unique_ptr<CastMessage> current_message_; |
| 194 |
| 195 // Socket used for I/O operations. |
| 196 net::Socket* const socket_; |
| 197 |
| 198 // Methods for communicating message receipt and error status to client code. |
| 199 std::unique_ptr<Delegate> delegate_; |
| 200 |
| 201 // Write flow state machine state. |
| 202 WriteState write_state_; |
| 203 |
| 204 // Read flow state machine state. |
| 205 ReadState read_state_; |
| 206 |
| 207 // The last error encountered by the channel. |
| 208 ChannelError error_state_; |
| 209 |
| 210 // Connection metadata for logging purposes. |
| 211 // Socket ID assigned by ApiResourceManager. |
| 212 int channel_id_; |
| 213 |
| 214 // IP address of the remote end. |
| 215 const net::IPEndPoint ip_endpoint_; |
| 216 |
| 217 // Authentication level for the connection. |
| 218 ChannelAuthType channel_auth_; |
| 219 |
| 220 // Accumulates details of events and errors, for debugging purposes. |
| 221 scoped_refptr<Logger> logger_; |
| 222 |
| 223 SEQUENCE_CHECKER(sequence_checker_); |
| 224 |
| 225 DISALLOW_COPY_AND_ASSIGN(CastTransportImpl); |
| 226 }; |
| 227 } // namespace cast_channel |
| 228 } // namespace api |
| 229 } // namespace extensions |
| 230 |
| 231 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_ |
OLD | NEW |