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

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. 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/common/api/cast_channel.h"
14 #include "net/base/completion_callback.h"
15
16 namespace net {
17 class DrainableIOBuffer;
18 class IPEndPoint;
19 class IOBuffer;
20 class DrainableIOBuffer;
21 class GrowableIOBuffer;
22 } // namespace net
23
24 namespace extensions {
25 namespace core_api {
26 namespace cast_channel {
27 class CastMessage;
28 struct LastErrors;
29 class Logger;
30 class MessageFramer;
31
32 // TODO(kmarshall): Migrate CastSocket to new interface.
33 // Skeletal placeholder for a future implementation of CastSocket.
34 // New to this interface are read/write calls that operate on simple buffers.
35 // Methods that operate on CastMessage/MessageInfo objects will be removed.
36 class CastSocketPlaceholder {
mark a. foltz 2014/09/12 19:41:01 ISTM that this is a useful abstraction for unit te
Wez 2014/09/12 19:59:59 Seconded.
Kevin M 2014/09/12 20:22:25 Done.
Kevin M 2014/09/12 20:22:25 Done.
37 public:
38 CastSocketPlaceholder() {}
39 virtual ~CastSocketPlaceholder() {}
40
41 virtual int Write(net::IOBuffer* buffer,
42 size_t size,
43 const net::CompletionCallback& callback) = 0;
44 virtual int Read(net::IOBuffer* buf,
45 int buf_len,
46 const net::CompletionCallback& callback) = 0;
Wez 2014/09/12 20:23:04 Clarify the semantics of these; in particular Writ
Kevin M 2014/09/12 21:26:23 Sorry, I don't follow. The sync unit tests exercis
Wez 2014/09/19 00:01:32 My reading of the impl was that CastSocket::Write(
47 virtual void CloseWithError(ChannelError error) = 0;
48 virtual const net::IPEndPoint& ip_endpoint() const = 0;
49 virtual ChannelAuthType channel_auth() const = 0;
50 virtual int id() const = 0;
51 };
52
53 // Manager class for reading and writing messages to/from a CastSocket.
54 class CastTransport {
55 public:
56 // Object to be informed of incoming messages and errors.
57 class Delegate {
58 public:
59 // An error occurred on the channel. |last_errors| contains the last errors
60 // logged for the channel from the implementation.
61 virtual void OnError(const CastSocketPlaceholder* socket,
62 ChannelError error_state,
63 const LastErrors& last_errors) = 0;
64 // A message was received on the channel.
65 virtual void OnMessage(const CastSocketPlaceholder* socket,
66 const CastMessage& message) = 0;
67
68 protected:
69 virtual ~Delegate() {}
70 };
71
72 CastTransport(CastSocketPlaceholder* socket,
73 Delegate* read_delegate,
74 scoped_refptr<Logger> logger);
75 virtual ~CastTransport();
76
77 // Sends a CastMessage to |socket_|.
78 // |message|: The message to send.
79 // |callback|: Callback to be invoked when the write operation has finished.
80 void SendMessage(const CastMessage& message,
81 const net::CompletionCallback& callback);
82
83 // Starts reading messages from |socket_|.
84 void StartReadLoop();
85
86 // Internal write states.
Wez 2014/09/12 19:59:59 If these are internal, why aren't they protected/p
Kevin M 2014/09/12 20:22:25 They need to be visible to the ...ToProto() family
Wez 2014/09/12 20:25:05 OK, mail me the error you get if you do the includ
Kevin M 2014/09/12 21:26:23 Done, sent.
Kevin M 2014/09/16 21:23:09 Resolved in another CL, 576483003.
87 enum WriteState {
88 WRITE_STATE_NONE,
89 WRITE_STATE_WRITE,
90 WRITE_STATE_WRITE_COMPLETE,
91 WRITE_STATE_DO_CALLBACK,
92 WRITE_STATE_ERROR,
93 };
94
95 // Internal read states.
96 enum ReadState {
97 READ_STATE_NONE,
98 READ_STATE_READ,
99 READ_STATE_READ_COMPLETE,
100 READ_STATE_DO_CALLBACK,
101 READ_STATE_ERROR,
102 };
103
104 private:
105 // Holds a message to be written to the socket. |callback| is invoked when the
106 // message is fully written or an error occurrs.
107 struct WriteRequest {
108 explicit WriteRequest(const std::string& namespace_,
109 const std::string& payload,
110 const net::CompletionCallback& callback);
111 ~WriteRequest();
112
113 // Namespace of the serialized message.
114 std::string message_namespace;
115 // Write completion callback, invoked when the operation has completed or
116 // failed.
117 net::CompletionCallback callback;
118 // Buffer with outgoing data.
119 scoped_refptr<net::DrainableIOBuffer> io_buffer;
120 };
121 // Terminates all in-flight write callbacks with error code ERR_FAILED.
Wez 2014/09/12 19:59:59 nit: Personally I hate code that squishes things t
Kevin M 2014/09/12 20:22:25 OK. I like more whitespace too, but previous teams
Wez 2014/09/12 20:25:05 New team, new habits. :D
122 void FlushWriteQueue();
123
124 /////////////////////////////////////////////////////////////////////////////
125 // Following methods work together to implement write flow.
Wez 2014/09/12 19:59:59 This seems implicit from the naming, and the comme
Kevin M 2014/09/12 20:22:25 Done.
126 //
127 // Main method that performs write flow state transitions.
128 void DoWriteLoop(int result);
Wez 2014/09/12 19:59:59 nit: Consider calling this OnWriteResult and simil
Kevin M 2014/09/12 20:22:25 Done.
129 // Each of the below Do* method is executed in the corresponding
130 // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE
131 // DowriteComplete is called, and so on.
132 int DoWrite();
133 int DoWriteComplete(int result);
134 int DoWriteCallback();
135 int DoWriteError(int result);
136 /////////////////////////////////////////////////////////////////////////////
137
138 /////////////////////////////////////////////////////////////////////////////
139 // Following methods work together to implement read flow.
140 //
141 // Main method that performs write flow state transitions.
142 void DoReadLoop(int result);
143 // Each of the below Do* method is executed in the corresponding
144 // write state. For example when write state is READ_STATE_READ_COMPLETE
145 // DoReadComplete is called, and so on.
146 int DoRead();
147 int DoReadComplete(int result);
148 int DoReadCallback();
149 int DoReadError(int result);
150
151 void SetReadState(ReadState read_state);
152 void SetWriteState(WriteState write_state);
153 void SetErrorState(ChannelError error_state);
154
155 // Queue of pending writes. The message at the front of the queue is the one
156 // being written.
157 std::queue<WriteRequest> write_queue_;
158 // Buffer used for read operations. Reused for every read.
159 scoped_refptr<net::GrowableIOBuffer> read_buffer_;
160 // Constructs and parses the wire representation of message frames.
161 scoped_ptr<MessageFramer> framer_;
162 // Last message received on the socket.
163 scoped_ptr<CastMessage> current_message_;
164 // Socket used for I/O operations.
165 CastSocketPlaceholder* socket_;
166 // Methods for communicating message receipt and error status to client code.
167 Delegate* read_delegate_;
168 // Write flow state machine state.
169 WriteState write_state_;
170 // Read flow state machine state.
171 ReadState read_state_;
172 // Most recent error that occurred during read or write operation, if any.
173 ChannelError error_state_;
174
175 scoped_refptr<Logger> logger_;
176 base::ThreadChecker thread_checker_;
177 };
Wez 2014/09/12 19:59:59 DISALLOW_COPY_AND_ASSIGN
Kevin M 2014/09/12 20:22:25 Done.
178 } // namespace cast_channel
179 } // namespace core_api
180 } // namespace extensions
181
182 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698