Update {virtual,override,final} to follow C++11 style.
[chromium-blink-merge.git] / extensions / browser / api / cast_channel / cast_transport.h
blob8a84a6d1ecbf3e247d0db1345a0e0c261793f9ad
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. |last_errors| contains the last errors
49 // logged for the channel from the implementation.
50 // The caller is responsible for closing |socket| if an error occurred.
51 virtual void OnError(ChannelError error_state,
52 const LastErrors& last_errors) = 0;
54 // A message was received on the channel.
55 virtual void OnMessage(const CastMessage& message) = 0;
58 // Sends a CastMessage to |socket_|.
59 // |message|: The message to send.
60 // |callback|: Callback to be invoked when the write operation has finished.
61 // Virtual for testing.
62 virtual void SendMessage(const CastMessage& message,
63 const net::CompletionCallback& callback) = 0;
65 // Initializes the reading state machine and starts reading from the
66 // underlying socket.
67 // Virtual for testing.
68 virtual void Start() = 0;
70 // Changes the delegate for processing read events. Pending reads remain
71 // in-flight.
72 // Ownership of the pointee of |delegate| is assumed by the transport.
73 // Prior delegates are deleted automatically.
74 virtual void SetReadDelegate(scoped_ptr<Delegate> delegate) = 0;
77 // Manager class for reading and writing messages to/from a socket.
78 class CastTransportImpl : public CastTransport, public base::NonThreadSafe {
79 public:
80 // Adds a CastMessage read/write layer to a socket.
81 // Message read events are propagated to the owner via |read_delegate|.
82 // |vlog_prefix| sets the prefix used for all VLOGged output.
83 // |socket| and |logger| must all out-live the
84 // CastTransportImpl instance.
85 // |read_delegate| is owned by this CastTransportImpl object.
86 CastTransportImpl(net::Socket* socket,
87 int channel_id,
88 const net::IPEndPoint& ip_endpoint_,
89 ChannelAuthType channel_auth_,
90 scoped_refptr<Logger> logger);
92 ~CastTransportImpl() override;
94 // CastTransport interface.
95 void SendMessage(const CastMessage& message,
96 const net::CompletionCallback& callback) override;
97 void Start() override;
98 void SetReadDelegate(scoped_ptr<Delegate> delegate) override;
100 private:
101 // Internal write states.
102 enum WriteState {
103 WRITE_STATE_NONE,
104 WRITE_STATE_WRITE,
105 WRITE_STATE_WRITE_COMPLETE,
106 WRITE_STATE_DO_CALLBACK,
107 WRITE_STATE_ERROR,
110 // Internal read states.
111 enum ReadState {
112 READ_STATE_NONE,
113 READ_STATE_READ,
114 READ_STATE_READ_COMPLETE,
115 READ_STATE_DO_CALLBACK,
116 READ_STATE_ERROR,
119 // Holds a message to be written to the socket. |callback| is invoked when the
120 // message is fully written or an error occurrs.
121 struct WriteRequest {
122 explicit WriteRequest(const std::string& namespace_,
123 const std::string& payload,
124 const net::CompletionCallback& callback);
125 ~WriteRequest();
127 // Namespace of the serialized message.
128 std::string message_namespace;
129 // Write completion callback, invoked when the operation has completed or
130 // failed.
131 net::CompletionCallback callback;
132 // Buffer with outgoing data.
133 scoped_refptr<net::DrainableIOBuffer> io_buffer;
136 static proto::ReadState ReadStateToProto(CastTransportImpl::ReadState state);
137 static proto::WriteState WriteStateToProto(
138 CastTransportImpl::WriteState state);
139 static proto::ErrorState ErrorStateToProto(ChannelError state);
141 void SetReadState(ReadState read_state);
142 void SetWriteState(WriteState write_state);
143 void SetErrorState(ChannelError error_state);
145 // Terminates all in-flight write callbacks with error code ERR_FAILED.
146 void FlushWriteQueue();
148 // Main method that performs write flow state transitions.
149 void OnWriteResult(int result);
151 // Each of the below Do* method is executed in the corresponding
152 // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE
153 // DowriteComplete is called, and so on.
154 int DoWrite();
155 int DoWriteComplete(int result);
156 int DoWriteCallback();
157 int DoWriteError(int result);
159 // Main method that performs write flow state transitions.
160 void OnReadResult(int result);
162 // Each of the below Do* method is executed in the corresponding
163 // write state. For example when read state is READ_STATE_READ_COMPLETE
164 // DoReadComplete is called, and so on.
165 int DoRead();
166 int DoReadComplete(int result);
167 int DoReadCallback();
168 int DoReadError(int result);
170 // Indicates that the transport object is started and may receive and send
171 // messages.
172 bool started_;
174 // Queue of pending writes. The message at the front of the queue is the one
175 // being written.
176 std::queue<WriteRequest> write_queue_;
178 // Buffer used for read operations. Reused for every read.
179 scoped_refptr<net::GrowableIOBuffer> read_buffer_;
181 // Constructs and parses the wire representation of message frames.
182 scoped_ptr<MessageFramer> framer_;
184 // Last message received on the socket.
185 scoped_ptr<CastMessage> current_message_;
187 // Socket used for I/O operations.
188 net::Socket* const socket_;
190 // Methods for communicating message receipt and error status to client code.
191 scoped_ptr<Delegate> read_delegate_;
193 // Write flow state machine state.
194 WriteState write_state_;
196 // Read flow state machine state.
197 ReadState read_state_;
199 // The last error encountered by the channel.
200 ChannelError error_state_;
202 // Connection metadata for logging purposes.
203 // Socket ID assigned by ApiResourceManager.
204 int channel_id_;
206 // IP address of the remote end.
207 const net::IPEndPoint ip_endpoint_;
209 // Authentication level for the connection.
210 ChannelAuthType channel_auth_;
212 // Accumulates details of events and errors, for debugging purposes.
213 scoped_refptr<Logger> logger_;
215 DISALLOW_COPY_AND_ASSIGN(CastTransportImpl);
217 } // namespace cast_channel
218 } // namespace core_api
219 } // namespace extensions
221 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_