Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / extensions / browser / api / cast_channel / cast_transport.h
blob3d6cc6c31eeeece8a3cdcf7f5efec971c74acb8e
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 // An error occurred on the channel. |last_errors| contains the last errors
45 // logged for the channel from the implementation.
46 // The caller is responsible for closing |socket| if an error occurred.
47 virtual void OnError(ChannelError error_state,
48 const LastErrors& last_errors) = 0;
49 // A message was received on the channel.
50 virtual void OnMessage(const CastMessage& message) = 0;
53 // Sends a CastMessage to |socket_|.
54 // |message|: The message to send.
55 // |callback|: Callback to be invoked when the write operation has finished.
56 // Virtual for testing.
57 virtual void SendMessage(const CastMessage& message,
58 const net::CompletionCallback& callback) = 0;
60 // Initializes the reading state machine and starts reading from the
61 // underlying socket.
62 // Virtual for testing.
63 virtual void StartReading() = 0;
65 // Changes the delegate for processing read events. Pending reads remain
66 // in-flight.
67 virtual void SetReadDelegate(Delegate* delegate) = 0;
70 // Manager class for reading and writing messages to/from a socket.
71 // TODO(kmarshall): Handle heartbeat messages in this layer.
72 class CastTransportImpl : public CastTransport, public base::NonThreadSafe {
73 public:
74 // Adds a CastMessage read/write layer to a socket.
75 // Message read events are propagated to the owner via |read_delegate|.
76 // |socket|, |read_delegate| and |logger| must all out-live the
77 // CastTransportImpl instance.
78 // |vlog_prefix| sets the prefix used for all VLOGged output.
79 CastTransportImpl(net::Socket* socket,
80 Delegate* read_delegate,
81 int channel_id,
82 const net::IPEndPoint& ip_endpoint_,
83 ChannelAuthType channel_auth_,
84 scoped_refptr<Logger> logger);
86 virtual ~CastTransportImpl();
88 // CastTransport interface.
89 virtual void SendMessage(const CastMessage& message,
90 const net::CompletionCallback& callback) override;
91 virtual void StartReading() override;
92 virtual void SetReadDelegate(Delegate* delegate) override;
94 private:
95 // Internal write states.
96 enum WriteState {
97 WRITE_STATE_NONE,
98 WRITE_STATE_WRITE,
99 WRITE_STATE_WRITE_COMPLETE,
100 WRITE_STATE_DO_CALLBACK,
101 WRITE_STATE_ERROR,
104 // Internal read states.
105 enum ReadState {
106 READ_STATE_NONE,
107 READ_STATE_READ,
108 READ_STATE_READ_COMPLETE,
109 READ_STATE_DO_CALLBACK,
110 READ_STATE_ERROR,
113 // Holds a message to be written to the socket. |callback| is invoked when the
114 // message is fully written or an error occurrs.
115 struct WriteRequest {
116 explicit WriteRequest(const std::string& namespace_,
117 const std::string& payload,
118 const net::CompletionCallback& callback);
119 ~WriteRequest();
121 // Namespace of the serialized message.
122 std::string message_namespace;
123 // Write completion callback, invoked when the operation has completed or
124 // failed.
125 net::CompletionCallback callback;
126 // Buffer with outgoing data.
127 scoped_refptr<net::DrainableIOBuffer> io_buffer;
130 static proto::ReadState ReadStateToProto(CastTransportImpl::ReadState state);
131 static proto::WriteState WriteStateToProto(
132 CastTransportImpl::WriteState state);
133 static proto::ErrorState ErrorStateToProto(ChannelError state);
135 void SetReadState(ReadState read_state);
136 void SetWriteState(WriteState write_state);
137 void SetErrorState(ChannelError error_state);
139 // Terminates all in-flight write callbacks with error code ERR_FAILED.
140 void FlushWriteQueue();
142 // Main method that performs write flow state transitions.
143 void OnWriteResult(int result);
145 // Each of the below Do* method is executed in the corresponding
146 // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE
147 // DowriteComplete is called, and so on.
148 int DoWrite();
149 int DoWriteComplete(int result);
150 int DoWriteCallback();
151 int DoWriteError(int result);
153 // Main method that performs write flow state transitions.
154 void OnReadResult(int result);
156 // Each of the below Do* method is executed in the corresponding
157 // write state. For example when read state is READ_STATE_READ_COMPLETE
158 // DoReadComplete is called, and so on.
159 int DoRead();
160 int DoReadComplete(int result);
161 int DoReadCallback();
162 int DoReadError(int result);
164 // Queue of pending writes. The message at the front of the queue is the one
165 // being written.
166 std::queue<WriteRequest> write_queue_;
168 // Buffer used for read operations. Reused for every read.
169 scoped_refptr<net::GrowableIOBuffer> read_buffer_;
171 // Constructs and parses the wire representation of message frames.
172 scoped_ptr<MessageFramer> framer_;
174 // Last message received on the socket.
175 scoped_ptr<CastMessage> current_message_;
177 // Socket used for I/O operations.
178 net::Socket* const socket_;
180 // Methods for communicating message receipt and error status to client code.
181 Delegate* read_delegate_;
183 // Write flow state machine state.
184 WriteState write_state_;
186 // Read flow state machine state.
187 ReadState read_state_;
189 // The last error encountered by the channel.
190 ChannelError error_state_;
192 // Connection metadata for logging purposes.
193 // Socket ID assigned by ApiResourceManager.
194 int channel_id_;
196 // IP address of the remote end.
197 const net::IPEndPoint ip_endpoint_;
199 // Authentication level for the connection.
200 ChannelAuthType channel_auth_;
202 // Accumulates details of events and errors, for debugging purposes.
203 scoped_refptr<Logger> logger_;
205 DISALLOW_COPY_AND_ASSIGN(CastTransportImpl);
207 } // namespace cast_channel
208 } // namespace core_api
209 } // namespace extensions
211 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_