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 "extensions/common/api/cast_channel/logging.pb.h"
16 #include "net/base/completion_callback.h"
19 class DrainableIOBuffer
;
22 class DrainableIOBuffer
;
23 class GrowableIOBuffer
;
27 namespace extensions
{
29 namespace cast_channel
{
37 virtual ~CastTransport() {}
39 // Object to be informed of incoming messages and read errors.
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
65 // Virtual for testing.
66 virtual void Start() = 0;
68 // Changes the delegate for processing read events. Pending reads remain
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
{
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
,
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
;
99 // Internal write states.
103 WRITE_STATE_WRITE_COMPLETE
,
104 WRITE_STATE_DO_CALLBACK
,
108 // Internal read states.
112 READ_STATE_READ_COMPLETE
,
113 READ_STATE_DO_CALLBACK
,
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
);
125 // Namespace of the serialized message.
126 std::string message_namespace
;
127 // Write completion callback, invoked when the operation has completed or
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.
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.
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
172 // Queue of pending writes. The message at the front of the queue is the one
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.
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_