OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_ |
7 | 7 |
8 #include <queue> | 8 #include <queue> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 | 108 |
109 protected: | 109 protected: |
110 // Creates an instance of TCPClientSocket. | 110 // Creates an instance of TCPClientSocket. |
111 virtual scoped_ptr<net::TCPClientSocket> CreateTcpSocket(); | 111 virtual scoped_ptr<net::TCPClientSocket> CreateTcpSocket(); |
112 // Creates an instance of SSLClientSocket. | 112 // Creates an instance of SSLClientSocket. |
113 virtual scoped_ptr<net::SSLClientSocket> CreateSslSocket(); | 113 virtual scoped_ptr<net::SSLClientSocket> CreateSslSocket(); |
114 // Extracts peer certificate from SSLClientSocket instance when the socket | 114 // Extracts peer certificate from SSLClientSocket instance when the socket |
115 // is in cert error state. | 115 // is in cert error state. |
116 // Returns whether certificate is successfully extracted. | 116 // Returns whether certificate is successfully extracted. |
117 virtual bool ExtractPeerCert(std::string* cert); | 117 virtual bool ExtractPeerCert(std::string* cert); |
118 // Sends a challenge request to the receiver. | |
119 virtual int SendAuthChallenge(); | |
120 // Reads auth challenge reply from the receiver. | |
121 virtual int ReadAuthChallengeReply(); | |
122 // Verifies whether the challenge reply received from the peer is valid: | 118 // Verifies whether the challenge reply received from the peer is valid: |
123 // 1. Signature in the reply is valid. | 119 // 1. Signature in the reply is valid. |
124 // 2. Certificate is rooted to a trusted CA. | 120 // 2. Certificate is rooted to a trusted CA. |
125 virtual bool VerifyChallengeReply(); | 121 virtual bool VerifyChallengeReply(); |
126 | 122 |
127 // Returns whether we are executing in a valid thread. | 123 // Returns whether we are executing in a valid thread. |
128 virtual bool CalledOnValidThread() const; | 124 virtual bool CalledOnValidThread() const; |
129 | 125 |
130 private: | 126 private: |
131 friend class ApiResourceManager<CastSocket>; | 127 friend class ApiResourceManager<CastSocket>; |
132 friend class CastSocketTest; | 128 friend class CastSocketTest; |
133 | 129 |
134 static const char* service_name() { | 130 static const char* service_name() { |
135 return "CastSocketManager"; | 131 return "CastSocketManager"; |
136 } | 132 } |
137 | 133 |
138 // Internal connection states. | 134 // Internal connection states. |
139 enum ConnectionState { | 135 enum ConnectionState { |
140 CONN_STATE_NONE, | 136 CONN_STATE_NONE, |
141 CONN_STATE_TCP_CONNECT, | 137 CONN_STATE_TCP_CONNECT, |
142 CONN_STATE_TCP_CONNECT_COMPLETE, | 138 CONN_STATE_TCP_CONNECT_COMPLETE, |
143 CONN_STATE_SSL_CONNECT, | 139 CONN_STATE_SSL_CONNECT, |
144 CONN_STATE_SSL_CONNECT_COMPLETE, | 140 CONN_STATE_SSL_CONNECT_COMPLETE, |
145 CONN_STATE_AUTH_CHALLENGE_SEND, | 141 CONN_STATE_AUTH_CHALLENGE_SEND, |
146 CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE, | 142 CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE, |
147 CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE, | 143 CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE, |
148 }; | 144 }; |
149 | 145 |
146 // Internal write states. | |
147 enum WriteState { | |
148 WRITE_STATE_NONE, | |
149 WRITE_STATE_WRITE, | |
150 WRITE_STATE_WRITE_COMPLETE, | |
151 WRITE_STATE_ERROR, | |
152 }; | |
153 | |
154 // Internal read states. | |
155 enum ReadState { | |
156 READ_STATE_NONE, | |
157 READ_STATE_READ, | |
158 READ_STATE_READ_COMPLETE, | |
159 READ_STATE_ERROR, | |
160 }; | |
161 | |
150 ///////////////////////////////////////////////////////////////////////////// | 162 ///////////////////////////////////////////////////////////////////////////// |
151 // Following methods work together to implement the following flow: | 163 // Following methods work together to implement the following flow: |
152 // 1. Create a new TCP socket and connect to it | 164 // 1. Create a new TCP socket and connect to it |
153 // 2. Create a new SSL socket and try connecting to it | 165 // 2. Create a new SSL socket and try connecting to it |
154 // 3. If connection fails due to invalid cert authority, then extract the | 166 // 3. If connection fails due to invalid cert authority, then extract the |
155 // peer certificate from the error. | 167 // peer certificate from the error. |
156 // 4. Whitelist the peer certificate and try #1 and #2 again. | 168 // 4. Whitelist the peer certificate and try #1 and #2 again. |
157 // 5. If SSL socket is connected successfully, and if protocol is casts:// | 169 // 5. If SSL socket is connected successfully, and if protocol is casts:// |
158 // then issue an auth challenge request. | 170 // then issue an auth challenge request. |
159 // 6. Validate the auth challenge response. | 171 // 6. Validate the auth challenge response. |
160 | 172 // |
161 // Main method that performs connection state transitions. | 173 // Main method that performs connection state transitions. |
162 int DoConnectLoop(int result); | 174 void DoConnectLoop(int result); |
163 // Each of the below Do* method is executed in the corresponding | 175 // Each of the below Do* method is executed in the corresponding |
164 // connection state. For e.g. when connection state is TCP_CONNECT | 176 // connection state. For e.g. when connection state is TCP_CONNECT |
165 // DoTcpConnect is called, and so on. | 177 // DoTcpConnect is called, and so on. |
166 int DoTcpConnect(); | 178 int DoTcpConnect(); |
167 int DoTcpConnectComplete(int result); | 179 int DoTcpConnectComplete(int result); |
168 int DoSslConnect(); | 180 int DoSslConnect(); |
169 int DoSslConnectComplete(int result); | 181 int DoSslConnectComplete(int result); |
170 int DoAuthChallengeSend(); | 182 int DoAuthChallengeSend(); |
171 int DoAuthChallengeSendComplete(int result); | 183 int DoAuthChallengeSendComplete(int result); |
172 int DoAuthChallengeReplyComplete(int result); | 184 int DoAuthChallengeReplyComplete(int result); |
173 ///////////////////////////////////////////////////////////////////////////// | 185 ///////////////////////////////////////////////////////////////////////////// |
174 | 186 |
175 // Callback method for callbacks from underlying sockets. | 187 // Internal implementation of the Connect method. |
176 void OnConnectComplete(int result); | 188 void ConnectInternal(); |
Wez
2013/11/26 02:45:56
So is this not part of DoConnectLoop, then?
Munjal (Google)
2013/11/26 20:06:14
No. But now I removed it anyway.
| |
177 | |
178 // Callback method when a challenge request is sent or a reply is received. | |
179 void OnChallengeEvent(int result); | |
180 | |
181 // Runs the external connection callback and resets it. | 189 // Runs the external connection callback and resets it. |
182 void DoConnectCallback(int result); | 190 void DoConnectCallback(int result); |
183 | |
184 // Verifies that the URL is a valid cast:// or casts:// URL and sets url_ to | 191 // Verifies that the URL is a valid cast:// or casts:// URL and sets url_ to |
185 // the result. | 192 // the result. |
186 bool ParseChannelUrl(const GURL& url); | 193 bool ParseChannelUrl(const GURL& url); |
Wez
2013/11/26 02:45:56
nit: Why is this mixed-in in between the Connect a
Munjal (Google)
2013/11/26 20:06:14
This is a helper used by connection methods.
Wez
2013/11/27 04:15:19
OK; I think it would help to clarify that, e.g. "C
| |
187 | 194 |
188 // Sends the given |message| and invokes the given callback when done. | 195 ///////////////////////////////////////////////////////////////////////////// |
189 int SendMessageInternal(const CastMessage& message, | 196 // Following methods work together to implement write flow. |
190 const net::CompletionCallback& callback); | 197 // |
198 // Main method that performs write flow state transitions. | |
199 void DoWriteLoop(int result); | |
200 // Each of the below Do* method is executed in the corresponding | |
201 // write state. For e.g. when write state is WRITE_STATE_WRITE_COMPLETE | |
202 // DowriteComplete is called, and so on. | |
203 int DoWrite(); | |
204 int DoWriteComplete(int result); | |
205 int DoWriteError(int result); | |
206 ///////////////////////////////////////////////////////////////////////////// | |
191 | 207 |
192 // Writes data to the socket from the WriteRequest at the head of the queue. | 208 // Posts a task to send the given message. |
Wez
2013/11/26 02:45:56
Why are these methods mixed-in between the write &
Munjal (Google)
2013/11/26 20:06:14
These are all helper methods using during write fl
Wez
2013/11/27 04:15:19
OK; as above, I think it'd help to have a block co
| |
193 // Calls OnWriteData() on completion. | 209 void PostTaskToSendCastMessage(const CastMessage& message, |
194 int WriteData(); | 210 const net::CompletionCallback& callback); |
195 void OnWriteData(int result); | 211 // Adds |message| to the write queue and starts the write loop if needed. |
212 void SendCastMessageInternal(const CastMessage& message, | |
213 const net::CompletionCallback& callback); | |
214 // Removes all messages from the write queue. | |
215 void ClearWriteQueue(); | |
196 | 216 |
197 // Reads data from the socket into one of the read buffers. Calls | 217 ///////////////////////////////////////////////////////////////////////////// |
198 // OnReadData() on completion. | 218 // Following methods work together to implement read flow. |
199 int ReadData(); | 219 // |
200 void OnReadData(int result); | 220 // Main method that performs write flow state transitions. |
221 void DoReadLoop(int result); | |
222 // Each of the below Do* method is executed in the corresponding | |
223 // write state. For e.g. when write state is READ_STATE_READ_COMPLETE | |
224 // DoReadComplete is called, and so on. | |
225 int DoRead(); | |
226 int DoReadComplete(int result); | |
227 int DoReadError(int result); | |
228 ///////////////////////////////////////////////////////////////////////////// | |
201 | 229 |
230 // Posts a task to start the read loop. | |
231 void PostTaskToStartReadLoop(); | |
232 // Stars the read loop if not already started. | |
233 void StartReadLoop(); | |
202 // Processes the contents of header_read_buffer_ and returns true on success. | 234 // Processes the contents of header_read_buffer_ and returns true on success. |
203 bool ProcessHeader(); | 235 bool ProcessHeader(); |
204 // Processes the contents of body_read_buffer_ and returns true on success. | 236 // Processes the contents of body_read_buffer_ and returns true on success. |
205 bool ProcessBody(); | 237 bool ProcessBody(); |
206 // Parses the message held in body_read_buffer_ and notifies |delegate_| if a | 238 // Parses the message held in body_read_buffer_ and notifies |delegate_| if a |
207 // message was extracted from the buffer. Returns true on success. | 239 // message was extracted from the buffer. Returns true on success. |
208 bool ParseMessageFromBody(); | 240 bool ParseMessageFromBody(); |
209 | 241 |
210 // Serializes the content of message_proto (with a header) to |message_data|. | 242 // Serializes the content of message_proto (with a header) to |message_data|. |
211 static bool Serialize(const CastMessage& message_proto, | 243 static bool Serialize(const CastMessage& message_proto, |
212 std::string* message_data); | 244 std::string* message_data); |
213 | 245 |
214 // Closes the socket and sets |error_state_|. Also signals |error| via | 246 // Closes the socket and sets |error_state_|. Also signals |error| via |
215 // |delegate_|. | 247 // |delegate_|. |
216 void CloseWithError(ChannelError error); | 248 void CloseWithError(ChannelError error); |
217 | 249 |
218 base::ThreadChecker thread_checker_; | 250 base::ThreadChecker thread_checker_; |
219 | 251 |
220 // The id of the channel. | 252 // The id of the channel. |
221 int channel_id_; | 253 int channel_id_; |
222 | 254 |
223 // The URL of the peer (cast:// or casts://). | 255 // The URL of the peer (cast:// or casts://). |
224 GURL url_; | 256 GURL url_; |
225 // Delegate to inform of incoming messages and errors. | 257 // Delegate to inform of incoming messages and errors. |
226 Delegate* delegate_; | 258 Delegate* delegate_; |
227 // True if we should perform receiver authentication. | 259 // True if we should perform receiver authentication. |
228 bool auth_required_; | 260 bool auth_required_; |
229 // The IP endpoint of the peer. | 261 // The IP endpoint of the peer. |
230 net::IPEndPoint ip_endpoint_; | 262 net::IPEndPoint ip_endpoint_; |
231 // The last error encountered by the channel. | |
232 ChannelError error_state_; | |
233 // The current status of the channel. | |
234 ReadyState ready_state_; | |
235 | |
236 // True when there is a write callback pending. | |
237 bool write_callback_pending_; | |
238 // True when there is a read callback pending. | |
239 bool read_callback_pending_; | |
240 | 263 |
241 // IOBuffer for reading the message header. | 264 // IOBuffer for reading the message header. |
242 scoped_refptr<net::GrowableIOBuffer> header_read_buffer_; | 265 scoped_refptr<net::GrowableIOBuffer> header_read_buffer_; |
243 // IOBuffer for reading the message body. | 266 // IOBuffer for reading the message body. |
244 scoped_refptr<net::GrowableIOBuffer> body_read_buffer_; | 267 scoped_refptr<net::GrowableIOBuffer> body_read_buffer_; |
245 // IOBuffer we are currently reading into. | 268 // IOBuffer we are currently reading into. |
246 scoped_refptr<net::GrowableIOBuffer> current_read_buffer_; | 269 scoped_refptr<net::GrowableIOBuffer> current_read_buffer_; |
247 // The number of bytes in the current message body. | 270 // The number of bytes in the current message body. |
248 uint32 current_message_size_; | 271 uint32 current_message_size_; |
249 | 272 |
250 // The NetLog for this service. | 273 // The NetLog for this service. |
251 net::NetLog* net_log_; | 274 net::NetLog* net_log_; |
252 // The NetLog source for this service. | 275 // The NetLog source for this service. |
253 net::NetLog::Source net_log_source_; | 276 net::NetLog::Source net_log_source_; |
254 | 277 |
255 // Next connection state to transition to. | |
256 ConnectionState next_state_; | |
257 // Owned ptr to the underlying TCP socket. | 278 // Owned ptr to the underlying TCP socket. |
258 scoped_ptr<net::TCPClientSocket> tcp_socket_; | 279 scoped_ptr<net::TCPClientSocket> tcp_socket_; |
259 // Owned ptr to the underlying SSL socket. | 280 // Owned ptr to the underlying SSL socket. |
260 scoped_ptr<net::SSLClientSocket> socket_; | 281 scoped_ptr<net::SSLClientSocket> socket_; |
261 // Certificate of the peer. This field may be empty if the peer | 282 // Certificate of the peer. This field may be empty if the peer |
262 // certificate is not yet fetched. | 283 // certificate is not yet fetched. |
263 std::string peer_cert_; | 284 std::string peer_cert_; |
264 scoped_ptr<net::CertVerifier> cert_verifier_; | 285 scoped_ptr<net::CertVerifier> cert_verifier_; |
265 scoped_ptr<net::TransportSecurityState> transport_security_state_; | 286 scoped_ptr<net::TransportSecurityState> transport_security_state_; |
266 // Reply received from the receiver to a challenge request. | 287 // Reply received from the receiver to a challenge request. |
267 scoped_ptr<CastMessage> challenge_reply_; | 288 scoped_ptr<CastMessage> challenge_reply_; |
268 | 289 |
269 // Callback invoked when the socket is connected. | 290 // Callback invoked when the socket is connected. |
270 net::CompletionCallback connect_callback_; | 291 net::CompletionCallback connect_callback_; |
271 | 292 |
293 // Connection flow state machine state. | |
294 ConnectionState connect_state_; | |
295 // Write flow state machine state. | |
296 WriteState write_state_; | |
297 // Read flow state machine state. | |
298 ReadState read_state_; | |
299 // The last error encountered by the channel. | |
300 ChannelError error_state_; | |
301 // The current status of the channel. | |
302 ReadyState ready_state_; | |
303 | |
272 // Message header struct. If fields are added, be sure to update | 304 // Message header struct. If fields are added, be sure to update |
273 // kMessageHeaderSize in the .cc. | 305 // kMessageHeaderSize in the .cc. |
274 struct MessageHeader { | 306 struct MessageHeader { |
275 MessageHeader(); | 307 MessageHeader(); |
276 // Sets the message size. | 308 // Sets the message size. |
277 void SetMessageSize(size_t message_size); | 309 void SetMessageSize(size_t message_size); |
278 // Prepends this header to |str|. | 310 // Prepends this header to |str|. |
279 void PrependToString(std::string* str); | 311 void PrependToString(std::string* str); |
280 // Reads |header| from the beginning of |buffer|. | 312 // Reads |header| from the beginning of |buffer|. |
281 static void ReadFromIOBuffer(net::GrowableIOBuffer* buffer, | 313 static void ReadFromIOBuffer(net::GrowableIOBuffer* buffer, |
(...skipping 12 matching lines...) Expand all Loading... | |
294 // and prepending the header. Must only be called once. | 326 // and prepending the header. Must only be called once. |
295 bool SetContent(const CastMessage& message_proto); | 327 bool SetContent(const CastMessage& message_proto); |
296 | 328 |
297 net::CompletionCallback callback; | 329 net::CompletionCallback callback; |
298 scoped_refptr<net::DrainableIOBuffer> io_buffer; | 330 scoped_refptr<net::DrainableIOBuffer> io_buffer; |
299 }; | 331 }; |
300 // Queue of pending writes. The message at the front of the queue is the one | 332 // Queue of pending writes. The message at the front of the queue is the one |
301 // being written. | 333 // being written. |
302 std::queue<WriteRequest> write_queue_; | 334 std::queue<WriteRequest> write_queue_; |
303 | 335 |
304 // Used to protect against DoConnectLoop() re-entrancy. | |
305 bool in_connect_loop_; | |
306 | |
307 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestCastURLs); | 336 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestCastURLs); |
308 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestRead); | 337 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestRead); |
309 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadMany); | 338 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadMany); |
310 DISALLOW_COPY_AND_ASSIGN(CastSocket); | 339 DISALLOW_COPY_AND_ASSIGN(CastSocket); |
311 }; | 340 }; |
312 | 341 |
313 } // namespace cast_channel | 342 } // namespace cast_channel |
314 } // namespace api | 343 } // namespace api |
315 } // namespace extensions | 344 } // namespace extensions |
316 | 345 |
317 #endif // CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_ | 346 #endif // CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_ |
OLD | NEW |