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/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"
20 class DrainableIOBuffer
;
23 class DrainableIOBuffer
;
24 class GrowableIOBuffer
;
28 namespace extensions
{
30 namespace cast_channel
{
38 virtual ~CastTransport() {}
40 // Object to be informed of incoming messages and read errors.
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
66 // Virtual for testing.
67 virtual void Start() = 0;
69 // Changes the delegate for processing read events. Pending reads remain
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
{
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
,
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
;
100 // Internal write states.
104 WRITE_STATE_WRITE_COMPLETE
,
105 WRITE_STATE_DO_CALLBACK
,
109 // Internal read states.
113 READ_STATE_READ_COMPLETE
,
114 READ_STATE_DO_CALLBACK
,
118 // Holds a message to be written to the socket. |callback| is invoked when the
119 // message is fully written or an error occurrs.
120 struct WriteRequest
{
121 explicit WriteRequest(const std::string
& namespace_
,
122 const std::string
& payload
,
123 const net::CompletionCallback
& callback
);
126 // Namespace of the serialized message.
127 std::string message_namespace
;
128 // Write completion callback, invoked when the operation has completed or
130 net::CompletionCallback callback
;
131 // Buffer with outgoing data.
132 scoped_refptr
<net::DrainableIOBuffer
> io_buffer
;
135 static proto::ReadState
ReadStateToProto(CastTransportImpl::ReadState state
);
136 static proto::WriteState
WriteStateToProto(
137 CastTransportImpl::WriteState state
);
138 static proto::ErrorState
ErrorStateToProto(ChannelError state
);
140 void SetReadState(ReadState read_state
);
141 void SetWriteState(WriteState write_state
);
142 void SetErrorState(ChannelError error_state
);
144 // Terminates all in-flight write callbacks with error code ERR_FAILED.
145 void FlushWriteQueue();
147 // Main method that performs write flow state transitions.
148 void OnWriteResult(int result
);
150 // Each of the below Do* method is executed in the corresponding
151 // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE
152 // DowriteComplete is called, and so on.
154 int DoWriteComplete(int result
);
155 int DoWriteCallback();
156 int DoWriteError(int result
);
158 // Main method that performs write flow state transitions.
159 void OnReadResult(int result
);
161 // Each of the below Do* method is executed in the corresponding
162 // write state. For example when read state is READ_STATE_READ_COMPLETE
163 // DoReadComplete is called, and so on.
165 int DoReadComplete(int result
);
166 int DoReadCallback();
167 int DoReadError(int result
);
169 // Indicates that the transport object is started and may receive and send
173 // Queue of pending writes. The message at the front of the queue is the one
175 std::queue
<WriteRequest
> write_queue_
;
177 // Buffer used for read operations. Reused for every read.
178 scoped_refptr
<net::GrowableIOBuffer
> read_buffer_
;
180 // Constructs and parses the wire representation of message frames.
181 scoped_ptr
<MessageFramer
> framer_
;
183 // Last message received on the socket.
184 scoped_ptr
<CastMessage
> current_message_
;
186 // Socket used for I/O operations.
187 net::Socket
* const socket_
;
189 // Methods for communicating message receipt and error status to client code.
190 scoped_ptr
<Delegate
> delegate_
;
192 // Write flow state machine state.
193 WriteState write_state_
;
195 // Read flow state machine state.
196 ReadState read_state_
;
198 // The last error encountered by the channel.
199 ChannelError error_state_
;
201 // Connection metadata for logging purposes.
202 // Socket ID assigned by ApiResourceManager.
205 // IP address of the remote end.
206 const net::IPEndPoint ip_endpoint_
;
208 // Authentication level for the connection.
209 ChannelAuthType channel_auth_
;
211 // Accumulates details of events and errors, for debugging purposes.
212 scoped_refptr
<Logger
> logger_
;
214 DISALLOW_COPY_AND_ASSIGN(CastTransportImpl
);
216 } // namespace cast_channel
217 } // namespace core_api
218 } // namespace extensions
220 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_