Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / api / cast_channel / cast_transport.h
blobec94c6e5af4ee6b098f8c1c4a3d6f4f21602b418
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_
8 #include <queue>
9 #include <string>
11 #include "base/memory/ref_counted.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "base/threading/thread_checker.h"
14 #include "extensions/browser/api/cast_channel/logger.h"
15 #include "extensions/common/api/cast_channel.h"
16 #include "extensions/common/api/cast_channel/logging.pb.h"
17 #include "net/base/completion_callback.h"
19 namespace net {
20 class DrainableIOBuffer;
21 class IPEndPoint;
22 class IOBuffer;
23 class DrainableIOBuffer;
24 class GrowableIOBuffer;
25 class Socket;
26 } // namespace net
28 namespace extensions {
29 namespace api {
30 namespace cast_channel {
31 class CastMessage;
32 struct LastErrors;
33 class Logger;
34 class MessageFramer;
36 class CastTransport {
37 public:
38 virtual ~CastTransport() {}
40 // Object to be informed of incoming messages and read errors.
41 class Delegate {
42 public:
43 virtual ~Delegate() {}
45 // Called once Transport is successfully initialized and started.
46 // Owned read delegates are Start()ed automatically.
47 virtual void Start() = 0;
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;
53 // A message was received on the channel.
54 virtual void OnMessage(const CastMessage& message) = 0;
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;
64 // Initializes the reading state machine and starts reading from the
65 // underlying socket.
66 // Virtual for testing.
67 virtual void Start() = 0;
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(scoped_ptr<Delegate> delegate) = 0;
76 // Manager class for reading and writing messages to/from a socket.
77 class CastTransportImpl : public CastTransport, public base::NonThreadSafe {
78 public:
79 // Adds a CastMessage read/write layer to a socket.
80 // Message read events are propagated to the owner via |read_delegate|.
81 // |vlog_prefix| sets the prefix used for all VLOGged output.
82 // |socket| and |logger| must all out-live the
83 // CastTransportImpl instance.
84 // |read_delegate| is owned by this CastTransportImpl object.
85 CastTransportImpl(net::Socket* socket,
86 int channel_id,
87 const net::IPEndPoint& ip_endpoint_,
88 ChannelAuthType channel_auth_,
89 scoped_refptr<Logger> logger);
91 ~CastTransportImpl() override;
93 // CastTransport interface.
94 void SendMessage(const CastMessage& message,
95 const net::CompletionCallback& callback) override;
96 void Start() override;
97 void SetReadDelegate(scoped_ptr<Delegate> delegate) override;
99 private:
100 // Internal write states.
101 enum WriteState {
102 WRITE_STATE_UNKNOWN,
103 WRITE_STATE_WRITE,
104 WRITE_STATE_WRITE_COMPLETE,
105 WRITE_STATE_DO_CALLBACK,
106 WRITE_STATE_HANDLE_ERROR,
107 WRITE_STATE_ERROR,
108 WRITE_STATE_IDLE,
111 // Internal read states.
112 enum ReadState {
113 READ_STATE_UNKNOWN,
114 READ_STATE_READ,
115 READ_STATE_READ_COMPLETE,
116 READ_STATE_DO_CALLBACK,
117 READ_STATE_HANDLE_ERROR,
118 READ_STATE_ERROR,
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();
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;
138 static proto::ReadState ReadStateToProto(CastTransportImpl::ReadState state);
139 static proto::WriteState WriteStateToProto(
140 CastTransportImpl::WriteState state);
141 static proto::ErrorState ErrorStateToProto(ChannelError state);
142 static bool IsTerminalReadState(ReadState read_state);
143 static bool IsTerminalWriteState(WriteState write_state);
145 void SetReadState(ReadState read_state);
146 void SetWriteState(WriteState write_state);
147 void SetErrorState(ChannelError error_state);
149 // Terminates all in-flight write callbacks with error code ERR_FAILED.
150 void FlushWriteQueue();
152 // Main method that performs write flow state transitions.
153 void OnWriteResult(int result);
155 // Each of the below Do* method is executed in the corresponding
156 // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE
157 // DowriteComplete is called, and so on.
158 int DoWrite();
159 int DoWriteComplete(int result);
160 int DoWriteCallback();
161 int DoWriteHandleError(int result);
163 // Main method that performs write flow state transitions.
164 void OnReadResult(int result);
166 // Each of the below Do* method is executed in the corresponding
167 // write state. For example when read state is READ_STATE_READ_COMPLETE
168 // DoReadComplete is called, and so on.
169 int DoRead();
170 int DoReadComplete(int result);
171 int DoReadCallback();
172 int DoReadHandleError(int result);
174 // Indicates that the transport object is started and may receive and send
175 // messages.
176 bool started_;
178 // Queue of pending writes. The message at the front of the queue is the one
179 // being written.
180 std::queue<WriteRequest> write_queue_;
182 // Buffer used for read operations. Reused for every read.
183 scoped_refptr<net::GrowableIOBuffer> read_buffer_;
185 // Constructs and parses the wire representation of message frames.
186 scoped_ptr<MessageFramer> framer_;
188 // Last message received on the socket.
189 scoped_ptr<CastMessage> current_message_;
191 // Socket used for I/O operations.
192 net::Socket* const socket_;
194 // Methods for communicating message receipt and error status to client code.
195 scoped_ptr<Delegate> delegate_;
197 // Write flow state machine state.
198 WriteState write_state_;
200 // Read flow state machine state.
201 ReadState read_state_;
203 // The last error encountered by the channel.
204 ChannelError error_state_;
206 // Connection metadata for logging purposes.
207 // Socket ID assigned by ApiResourceManager.
208 int channel_id_;
210 // IP address of the remote end.
211 const net::IPEndPoint ip_endpoint_;
213 // Authentication level for the connection.
214 ChannelAuthType channel_auth_;
216 // Accumulates details of events and errors, for debugging purposes.
217 scoped_refptr<Logger> logger_;
219 DISALLOW_COPY_AND_ASSIGN(CastTransportImpl);
221 } // namespace cast_channel
222 } // namespace api
223 } // namespace extensions
225 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_