OLD | NEW |
---|---|
(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 CastSocketInterface { | |
37 public: | |
38 CastSocketInterface() {} | |
39 virtual ~CastSocketInterface() {} | |
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; | |
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 CastSocketInterface* socket, | |
62 ChannelError error_state, | |
63 const LastErrors& last_errors) = 0; | |
64 // A message was received on the channel. | |
65 virtual void OnMessage(const CastSocketInterface* socket, | |
66 const CastMessage& message) = 0; | |
67 | |
68 protected: | |
69 virtual ~Delegate() {} | |
70 }; | |
71 | |
72 CastTransport(CastSocketInterface* 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. | |
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 | |
122 // Terminates all in-flight write callbacks with error code ERR_FAILED. | |
123 void FlushWriteQueue(); | |
124 | |
125 // Main method that performs write flow state transitions. | |
126 void OnWriteResult(int result); | |
127 | |
128 // Each of the below Do* method is executed in the corresponding | |
129 // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE | |
130 // DowriteComplete is called, and so on. | |
131 int DoWrite(); | |
132 int DoWriteComplete(int result); | |
133 int DoWriteCallback(); | |
134 int DoWriteError(int result); | |
135 | |
136 // Main method that performs write flow state transitions. | |
137 void OnReadResult(int result); | |
138 | |
139 // Each of the below Do* method is executed in the corresponding | |
140 // write state. For example when write state is READ_STATE_READ_COMPLETE | |
141 // DoReadComplete is called, and so on. | |
142 int DoRead(); | |
143 int DoReadComplete(int result); | |
144 int DoReadCallback(); | |
145 int DoReadError(int result); | |
146 | |
147 void SetReadState(ReadState read_state); | |
148 void SetWriteState(WriteState write_state); | |
149 void SetErrorState(ChannelError error_state); | |
150 | |
151 // Queue of pending writes. The message at the front of the queue is the one | |
152 // being written. | |
153 std::queue<WriteRequest> write_queue_; | |
154 | |
155 // Buffer used for read operations. Reused for every read. | |
156 scoped_refptr<net::GrowableIOBuffer> read_buffer_; | |
157 | |
158 // Constructs and parses the wire representation of message frames. | |
159 scoped_ptr<MessageFramer> framer_; | |
160 | |
161 // Last message received on the socket. | |
162 scoped_ptr<CastMessage> current_message_; | |
163 | |
164 // Socket used for I/O operations. | |
165 CastSocketInterface* socket_; | |
miu
2014/09/15 19:08:36
Should be:
CastSocketInterface* const socket_;
Kevin M
2014/09/16 21:23:10
Done.
miu
2014/09/16 21:39:53
? Doesn't seem to be in patch set 5.
Kevin M
2014/09/16 21:45:59
Didn't catch that git cl upload failed (oops, bad
| |
166 | |
167 // Methods for communicating message receipt and error status to client code. | |
168 Delegate* read_delegate_; | |
169 | |
170 // Write flow state machine state. | |
171 WriteState write_state_; | |
172 | |
173 // Read flow state machine state. | |
174 ReadState read_state_; | |
175 | |
176 // Most recent error that occurred during read or write operation, if any. | |
177 ChannelError error_state_; | |
178 | |
179 scoped_refptr<Logger> logger_; | |
180 base::ThreadChecker thread_checker_; | |
181 | |
182 DISALLOW_COPY_AND_ASSIGN(CastTransport); | |
183 }; | |
184 } // namespace cast_channel | |
185 } // namespace core_api | |
186 } // namespace extensions | |
187 | |
188 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_ | |
OLD | NEW |