Chromium Code Reviews| 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/memory/ref_counted.h" | |
| 12 #include "base/threading/thread_checker.h" | |
| 13 #include "extensions/browser/api/cast_channel/logger.h" | |
| 14 #include "extensions/common/api/cast_channel.h" | |
| 15 #include "net/base/completion_callback.h" | |
| 16 | |
| 17 namespace net { | |
| 18 class DrainableIOBuffer; | |
| 19 class IPEndPoint; | |
| 20 class IOBuffer; | |
| 21 class DrainableIOBuffer; | |
| 22 class GrowableIOBuffer; | |
| 23 } // namespace net | |
| 24 | |
| 25 namespace extensions { | |
| 26 namespace core_api { | |
| 27 namespace cast_channel { | |
| 28 class CastMessage; | |
| 29 struct LastErrors; | |
| 30 class Logger; | |
| 31 class MessageFramer; | |
| 32 | |
| 33 // TODO(kmarshall): Migrate CastSocket to new interface. | |
| 34 // Redirect references to CastSocketImpl in logger.h to this interface once | |
| 35 // the interface is promoted to cast_socket.h. | |
| 36 class CastSocketInterface { | |
|
Wez
2014/09/20 00:41:30
Why is this still CastSocketInterface rather than
Kevin M
2014/09/22 19:56:51
Will do, but I'll keep it as CastSocketInterface u
| |
| 37 public: | |
| 38 CastSocketInterface() {} | |
| 39 virtual ~CastSocketInterface() {} | |
| 40 | |
| 41 // Writes at least one, and up to |size| bytes to the socket. | |
| 42 // Returns net::ERR_IO_PENDING if the operation will complete | |
| 43 // asynchronously, in which case |callback| will be invoked | |
| 44 // on completion. | |
| 45 // Asynchronous writes are cancleled if the CastSocket is deleted. | |
| 46 // All values <= zero indicate an error. | |
| 47 virtual int Write(net::IOBuffer* buffer, | |
| 48 size_t size, | |
| 49 const net::CompletionCallback& callback) = 0; | |
| 50 | |
| 51 // Reads at least one, and up to |size| bytes from the socket. | |
| 52 // Returns net::ERR_IO_PENDING if the operation will complete | |
| 53 // asynchronously, in which case |callback| will be invoked | |
| 54 // on completion. | |
| 55 // All values <= zero indicate an error. | |
| 56 virtual int Read(net::IOBuffer* buf, | |
| 57 int buf_len, | |
| 58 const net::CompletionCallback& callback) = 0; | |
| 59 virtual void CloseWithError(ChannelError error) = 0; | |
| 60 virtual const net::IPEndPoint& ip_endpoint() const = 0; | |
| 61 virtual ChannelAuthType channel_auth() const = 0; | |
| 62 virtual int id() const = 0; | |
| 63 }; | |
| 64 | |
| 65 // Manager class for reading and writing messages to/from a CastSocket. | |
| 66 class CastTransport { | |
| 67 public: | |
| 68 // Object to be informed of incoming messages and errors. | |
| 69 class Delegate { | |
| 70 public: | |
| 71 // An error occurred on the channel. |last_errors| contains the last errors | |
| 72 // logged for the channel from the implementation. | |
| 73 virtual void OnError(const CastSocketInterface* socket, | |
| 74 ChannelError error_state, | |
| 75 const LastErrors& last_errors) = 0; | |
| 76 // A message was received on the channel. | |
| 77 virtual void OnMessage(const CastSocketInterface* socket, | |
| 78 const CastMessage& message) = 0; | |
| 79 | |
| 80 protected: | |
| 81 virtual ~Delegate() {} | |
| 82 }; | |
| 83 | |
| 84 // Adds a CastMessage read/write layer to a socket. | |
| 85 // Message read events are propagated to the owner via |read_delegate|. | |
| 86 // The CastTransport object should be deleted prior to the | |
| 87 // underlying socket being deleted. | |
| 88 CastTransport(CastSocketInterface* socket, | |
| 89 Delegate* read_delegate, | |
| 90 scoped_refptr<Logger> logger); | |
| 91 virtual ~CastTransport(); | |
| 92 | |
| 93 // Sends a CastMessage to |socket_|. | |
| 94 // |message|: The message to send. | |
| 95 // |callback|: Callback to be invoked when the write operation has finished. | |
| 96 void SendMessage(const CastMessage& message, | |
| 97 const net::CompletionCallback& callback); | |
| 98 | |
| 99 // Starts reading messages from |socket_|. | |
| 100 void StartReadLoop(); | |
| 101 | |
| 102 private: | |
| 103 // Internal write states. | |
| 104 enum WriteState { | |
| 105 WRITE_STATE_NONE, | |
| 106 WRITE_STATE_WRITE, | |
| 107 WRITE_STATE_WRITE_COMPLETE, | |
| 108 WRITE_STATE_DO_CALLBACK, | |
| 109 WRITE_STATE_ERROR, | |
| 110 }; | |
| 111 | |
| 112 // Internal read states. | |
| 113 enum ReadState { | |
| 114 READ_STATE_NONE, | |
| 115 READ_STATE_READ, | |
| 116 READ_STATE_READ_COMPLETE, | |
| 117 READ_STATE_DO_CALLBACK, | |
| 118 READ_STATE_ERROR, | |
| 119 }; | |
| 120 | |
| 121 // Holds a message to be written to the socket. |callback| is invoked when the | |
| 122 // message is fully written or an error occurrs. | |
| 123 struct WriteRequest { | |
| 124 explicit WriteRequest(const std::string& namespace_, | |
| 125 const std::string& payload, | |
| 126 const net::CompletionCallback& callback); | |
| 127 ~WriteRequest(); | |
| 128 | |
| 129 // Namespace of the serialized message. | |
| 130 std::string message_namespace; | |
| 131 // Write completion callback, invoked when the operation has completed or | |
| 132 // failed. | |
| 133 net::CompletionCallback callback; | |
| 134 // Buffer with outgoing data. | |
| 135 scoped_refptr<net::DrainableIOBuffer> io_buffer; | |
| 136 }; | |
| 137 | |
| 138 static proto::ReadState ReadStateToProto(CastTransport::ReadState state); | |
| 139 static proto::WriteState WriteStateToProto(CastTransport::WriteState state); | |
| 140 static proto::ErrorState ErrorStateToProto(ChannelError state); | |
| 141 | |
| 142 // Terminates all in-flight write callbacks with error code ERR_FAILED. | |
| 143 void FlushWriteQueue(); | |
| 144 | |
| 145 // Main method that performs write flow state transitions. | |
| 146 void OnWriteResult(int result); | |
| 147 | |
| 148 // Each of the below Do* method is executed in the corresponding | |
| 149 // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE | |
| 150 // DowriteComplete is called, and so on. | |
| 151 int DoWrite(); | |
| 152 int DoWriteComplete(int result); | |
| 153 int DoWriteCallback(); | |
| 154 int DoWriteError(int result); | |
| 155 | |
| 156 // Main method that performs write flow state transitions. | |
| 157 void OnReadResult(int result); | |
| 158 | |
| 159 // Each of the below Do* method is executed in the corresponding | |
| 160 // write state. For example when write state is READ_STATE_READ_COMPLETE | |
| 161 // DoReadComplete is called, and so on. | |
| 162 int DoRead(); | |
| 163 int DoReadComplete(int result); | |
| 164 int DoReadCallback(); | |
| 165 int DoReadError(int result); | |
| 166 | |
| 167 void SetReadState(ReadState read_state); | |
| 168 void SetWriteState(WriteState write_state); | |
| 169 void SetErrorState(ChannelError error_state); | |
| 170 | |
| 171 // Queue of pending writes. The message at the front of the queue is the one | |
| 172 // being written. | |
| 173 std::queue<WriteRequest> write_queue_; | |
| 174 | |
| 175 // Buffer used for read operations. Reused for every read. | |
| 176 scoped_refptr<net::GrowableIOBuffer> read_buffer_; | |
| 177 | |
| 178 // Constructs and parses the wire representation of message frames. | |
| 179 scoped_ptr<MessageFramer> framer_; | |
| 180 | |
| 181 // Last message received on the socket. | |
| 182 scoped_ptr<CastMessage> current_message_; | |
| 183 | |
| 184 // Socket used for I/O operations. | |
| 185 CastSocketInterface* const socket_; | |
| 186 | |
| 187 // Methods for communicating message receipt and error status to client code. | |
| 188 Delegate* const read_delegate_; | |
| 189 | |
| 190 // Write flow state machine state. | |
| 191 WriteState write_state_; | |
| 192 | |
| 193 // Read flow state machine state. | |
| 194 ReadState read_state_; | |
| 195 | |
| 196 // Most recent error that occurred during read or write operation, if any. | |
| 197 ChannelError error_state_; | |
| 198 | |
| 199 scoped_refptr<Logger> logger_; | |
| 200 base::ThreadChecker thread_checker_; | |
| 201 | |
| 202 DISALLOW_COPY_AND_ASSIGN(CastTransport); | |
| 203 }; | |
| 204 } // namespace cast_channel | |
| 205 } // namespace core_api | |
| 206 } // namespace extensions | |
| 207 | |
| 208 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_ | |
| OLD | NEW |