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.
5 #ifndef EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_
6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_
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"
18 class DrainableIOBuffer;
21 class DrainableIOBuffer;
22 class GrowableIOBuffer;
25 namespace extensions {
27 namespace cast_channel {
33 // TODO(kmarshall): Migrate CastSocket to new interface.
34 // Redirect references to CastSocket in logger.h to this interface once
35 // the interface is promoted to cast_socket.h.
36 class CastSocketInterface {
38 CastSocketInterface() {}
39 virtual ~CastSocketInterface() {}
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
45 // Asynchronous writes are cancleled if the CastSocket is deleted.
46 // All values <= zero indicate an error.
47 virtual int Write(net::IOBuffer* buffer,
49 const net::CompletionCallback& callback) = 0;
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
55 // All values <= zero indicate an error.
56 virtual int Read(net::IOBuffer* buf,
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;
65 // Manager class for reading and writing messages to/from a CastSocket.
68 // Object to be informed of incoming messages and errors.
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;
81 virtual ~Delegate() {}
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();
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);
99 // Starts reading messages from |socket_|.
100 void StartReadLoop();
103 // Internal write states.
107 WRITE_STATE_WRITE_COMPLETE,
108 WRITE_STATE_DO_CALLBACK,
112 // Internal read states.
116 READ_STATE_READ_COMPLETE,
117 READ_STATE_DO_CALLBACK,
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);
129 // Namespace of the serialized message.
130 std::string message_namespace;
131 // Write completion callback, invoked when the operation has completed or
133 net::CompletionCallback callback;
134 // Buffer with outgoing data.
135 scoped_refptr<net::DrainableIOBuffer> io_buffer;
138 static proto::ReadState ReadStateToProto(CastTransport::ReadState state);
139 static proto::WriteState WriteStateToProto(CastTransport::WriteState state);
140 static proto::ErrorState ErrorStateToProto(ChannelError state);
142 // Terminates all in-flight write callbacks with error code ERR_FAILED.
143 void FlushWriteQueue();
145 // Main method that performs write flow state transitions.
146 void OnWriteResult(int result);
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.
152 int DoWriteComplete(int result);
153 int DoWriteCallback();
154 int DoWriteError(int result);
156 // Main method that performs write flow state transitions.
157 void OnReadResult(int result);
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.
163 int DoReadComplete(int result);
164 int DoReadCallback();
165 int DoReadError(int result);
167 void SetReadState(ReadState read_state);
168 void SetWriteState(WriteState write_state);
169 void SetErrorState(ChannelError error_state);
171 // Queue of pending writes. The message at the front of the queue is the one
173 std::queue<WriteRequest> write_queue_;
175 // Buffer used for read operations. Reused for every read.
176 scoped_refptr<net::GrowableIOBuffer> read_buffer_;
178 // Constructs and parses the wire representation of message frames.
179 scoped_ptr<MessageFramer> framer_;
181 // Last message received on the socket.
182 scoped_ptr<CastMessage> current_message_;
184 // Socket used for I/O operations.
185 CastSocketInterface* const socket_;
187 // Methods for communicating message receipt and error status to client code.
188 Delegate* const read_delegate_;
190 // Write flow state machine state.
191 WriteState write_state_;
193 // Read flow state machine state.
194 ReadState read_state_;
196 // Most recent error that occurred during read or write operation, if any.
197 ChannelError error_state_;
199 scoped_refptr<Logger> logger_;
200 base::ThreadChecker thread_checker_;
202 DISALLOW_COPY_AND_ASSIGN(CastTransport);
204 } // namespace cast_channel
205 } // namespace core_api
206 } // namespace extensions
208 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_