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