Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: extensions/browser/api/cast_channel/cast_transport.h

Issue 555283002: Create new class "CastTransport", which encapsulates the message read and write event loops. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Code review feedback addressed. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #ifndef EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_
6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_
7
8 #include <queue>
9 #include <string>
10
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 "net/base/completion_callback.h"
16
17 namespace net {
18 class DrainableIOBuffer;
19 class IPEndPoint;
20 class IOBuffer;
21 class DrainableIOBuffer;
22 class GrowableIOBuffer;
23 } // namespace net
24
25 namespace extensions {
26 namespace core_api {
27 namespace cast_channel {
28 class CastMessage;
29 struct LastErrors;
30 class Logger;
31 class MessageFramer;
32
33 // TODO(kmarshall): Migrate CastSocket to new interface.
34 // Skeletal placeholder for a future implementation of CastSocket.
35 // New to this interface are read/write calls that operate on simple buffers.
36 // Methods that operate on CastMessage/MessageInfo objects will be removed.
37 class CastSocketInterface {
Wez 2014/09/19 00:01:32 Sorry, I should have mentioned this previously; th
Kevin M 2014/09/19 18:13:06 Done. I added a TODO to point the logger class at
38 public:
39 CastSocketInterface() {}
40 virtual ~CastSocketInterface() {}
41
42 // Writes data to the socket.
43 // If the return value is >0, returns the number of bytes written
44 // synchronously to the socket.
45 // Returns net::ERR_IO_PENDING if the operation was asynchronous.
46 // All other return values are net::Error error codes.
Wez 2014/09/19 00:01:32 Suggest: "Writes at least one, and up to |size| by
Kevin M 2014/09/19 18:13:06 Done.
47 virtual int Write(net::IOBuffer* buffer,
48 size_t size,
49 const net::CompletionCallback& callback) = 0;
50 // Writes data from the socket.
Wez 2014/09/19 00:01:32 typo: Reads?
Kevin M 2014/09/19 18:13:06 Done.
51 // If the return value is >0, returns the number of bytes read
52 // synchronously from the socket.
53 // Returns net::ERR_IO_PENDING if the operation was asynchronous.
54 // All other return values are net::Error error codes.
55 virtual int Read(net::IOBuffer* buf,
56 int buf_len,
57 const net::CompletionCallback& callback) = 0;
Wez 2014/09/19 00:01:32 These methods are identical in syntax and semantic
Kevin M 2014/09/19 18:13:06 I'm uncertain. Socket has the pure virtual methods
58 virtual void CloseWithError(ChannelError error) = 0;
59 virtual const net::IPEndPoint& ip_endpoint() const = 0;
60 virtual ChannelAuthType channel_auth() const = 0;
61 virtual int id() const = 0;
62 };
63
64 // Manager class for reading and writing messages to/from a CastSocket.
65 class CastTransport {
66 public:
67 // Object to be informed of incoming messages and errors.
68 class Delegate {
69 public:
70 // An error occurred on the channel. |last_errors| contains the last errors
71 // logged for the channel from the implementation.
72 virtual void OnError(const CastSocketInterface* socket,
73 ChannelError error_state,
74 const LastErrors& last_errors) = 0;
75 // A message was received on the channel.
76 virtual void OnMessage(const CastSocketInterface* socket,
77 const CastMessage& message) = 0;
78
79 protected:
80 virtual ~Delegate() {}
81 };
82
83 CastTransport(CastSocketInterface* socket,
84 Delegate* read_delegate,
85 scoped_refptr<Logger> logger);
Wez 2014/09/19 00:01:32 nit: Please add a comment to clarify the lifetime
Kevin M 2014/09/19 18:13:06 Done.
Kevin M 2014/09/19 18:13:06 Done.
86 virtual ~CastTransport();
87
88 // Sends a CastMessage to |socket_|.
89 // |message|: The message to send.
90 // |callback|: Callback to be invoked when the write operation has finished.
91 void SendMessage(const CastMessage& message,
92 const net::CompletionCallback& callback);
93
94 // Starts reading messages from |socket_|.
95 void StartReadLoop();
96
97 private:
98 // Internal write states.
99 enum WriteState {
100 WRITE_STATE_NONE,
101 WRITE_STATE_WRITE,
102 WRITE_STATE_WRITE_COMPLETE,
103 WRITE_STATE_DO_CALLBACK,
104 WRITE_STATE_ERROR,
105 };
106
107 // Internal read states.
108 enum ReadState {
109 READ_STATE_NONE,
110 READ_STATE_READ,
111 READ_STATE_READ_COMPLETE,
112 READ_STATE_DO_CALLBACK,
113 READ_STATE_ERROR,
114 };
115
116 // Holds a message to be written to the socket. |callback| is invoked when the
117 // message is fully written or an error occurrs.
118 struct WriteRequest {
119 explicit WriteRequest(const std::string& namespace_,
120 const std::string& payload,
121 const net::CompletionCallback& callback);
122 ~WriteRequest();
123
124 // Namespace of the serialized message.
125 std::string message_namespace;
126 // Write completion callback, invoked when the operation has completed or
127 // failed.
128 net::CompletionCallback callback;
129 // Buffer with outgoing data.
130 scoped_refptr<net::DrainableIOBuffer> io_buffer;
131 };
132
133 static proto::ReadState ReadStateToProto(CastTransport::ReadState state);
134 static proto::WriteState WriteStateToProto(CastTransport::WriteState state);
135 static proto::ErrorState ErrorStateToProto(ChannelError state);
Wez 2014/09/19 00:01:32 Why do these need to be part of the class, rather
Kevin M 2014/09/19 18:13:06 They probably should go in an anonymous namespace,
Wez 2014/09/25 02:09:14 Acknowledged.
136
137 // Terminates all in-flight write callbacks with error code ERR_FAILED.
138 void FlushWriteQueue();
139
140 // Main method that performs write flow state transitions.
141 void OnWriteResult(int result);
142
143 // Each of the below Do* method is executed in the corresponding
144 // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE
145 // DowriteComplete is called, and so on.
146 int DoWrite();
147 int DoWriteComplete(int result);
148 int DoWriteCallback();
149 int DoWriteError(int result);
150
151 // Main method that performs write flow state transitions.
152 void OnReadResult(int result);
153
154 // Each of the below Do* method is executed in the corresponding
155 // write state. For example when write state is READ_STATE_READ_COMPLETE
156 // DoReadComplete is called, and so on.
157 int DoRead();
158 int DoReadComplete(int result);
159 int DoReadCallback();
160 int DoReadError(int result);
161
162 void SetReadState(ReadState read_state);
163 void SetWriteState(WriteState write_state);
164 void SetErrorState(ChannelError error_state);
165
166 // Queue of pending writes. The message at the front of the queue is the one
167 // being written.
168 std::queue<WriteRequest> write_queue_;
169
170 // Buffer used for read operations. Reused for every read.
171 scoped_refptr<net::GrowableIOBuffer> read_buffer_;
172
173 // Constructs and parses the wire representation of message frames.
174 scoped_ptr<MessageFramer> framer_;
175
176 // Last message received on the socket.
177 scoped_ptr<CastMessage> current_message_;
178
179 // Socket used for I/O operations.
180 CastSocketInterface* const socket_;
181
182 // Methods for communicating message receipt and error status to client code.
183 Delegate* const read_delegate_;
184
185 // Write flow state machine state.
186 WriteState write_state_;
187
188 // Read flow state machine state.
189 ReadState read_state_;
190
191 // Most recent error that occurred during read or write operation, if any.
192 ChannelError error_state_;
193
194 scoped_refptr<Logger> logger_;
195 base::ThreadChecker thread_checker_;
196
197 DISALLOW_COPY_AND_ASSIGN(CastTransport);
198 };
199 } // namespace cast_channel
200 } // namespace core_api
201 } // namespace extensions
202
203 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698