Chromium Code Reviews| 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 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h" | 5 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 67 | 67 |
| 68 CastSocket::CastSocket(const std::string& owner_extension_id, | 68 CastSocket::CastSocket(const std::string& owner_extension_id, |
| 69 const GURL& url, | 69 const GURL& url, |
| 70 CastSocket::Delegate* delegate, | 70 CastSocket::Delegate* delegate, |
| 71 net::NetLog* net_log) : | 71 net::NetLog* net_log) : |
| 72 ApiResource(owner_extension_id), | 72 ApiResource(owner_extension_id), |
| 73 channel_id_(0), | 73 channel_id_(0), |
| 74 url_(url), | 74 url_(url), |
| 75 delegate_(delegate), | 75 delegate_(delegate), |
| 76 auth_required_(false), | 76 auth_required_(false), |
| 77 error_state_(CHANNEL_ERROR_NONE), | |
| 78 ready_state_(READY_STATE_NONE), | |
| 79 write_callback_pending_(false), | |
| 80 read_callback_pending_(false), | |
| 81 current_message_size_(0), | 77 current_message_size_(0), |
| 82 net_log_(net_log), | 78 net_log_(net_log), |
| 83 next_state_(CONN_STATE_NONE), | 79 connect_state_(CONN_STATE_NONE), |
| 84 in_connect_loop_(false) { | 80 write_state_(WRITE_STATE_NONE), |
| 81 read_state_(READ_STATE_NONE), | |
| 82 error_state_(CHANNEL_ERROR_NONE), | |
| 83 ready_state_(READY_STATE_NONE) { | |
| 85 DCHECK(net_log_); | 84 DCHECK(net_log_); |
| 86 net_log_source_.type = net::NetLog::SOURCE_SOCKET; | 85 net_log_source_.type = net::NetLog::SOURCE_SOCKET; |
| 87 net_log_source_.id = net_log_->NextID(); | 86 net_log_source_.id = net_log_->NextID(); |
| 88 | 87 |
| 89 // We reuse these buffers for each message. | 88 // We reuse these buffers for each message. |
| 90 header_read_buffer_ = new net::GrowableIOBuffer(); | 89 header_read_buffer_ = new net::GrowableIOBuffer(); |
| 91 header_read_buffer_->SetCapacity(kMessageHeaderSize); | 90 header_read_buffer_->SetCapacity(kMessageHeaderSize); |
| 92 body_read_buffer_ = new net::GrowableIOBuffer(); | 91 body_read_buffer_ = new net::GrowableIOBuffer(); |
| 93 body_read_buffer_->SetCapacity(kMaxMessageSize); | 92 body_read_buffer_->SetCapacity(kMaxMessageSize); |
| 94 current_read_buffer_ = header_read_buffer_; | 93 current_read_buffer_ = header_read_buffer_; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 net::SSLInfo ssl_info; | 143 net::SSLInfo ssl_info; |
| 145 if (!socket_->GetSSLInfo(&ssl_info) || !ssl_info.cert.get()) | 144 if (!socket_->GetSSLInfo(&ssl_info) || !ssl_info.cert.get()) |
| 146 return false; | 145 return false; |
| 147 bool result = net::X509Certificate::GetDEREncoded( | 146 bool result = net::X509Certificate::GetDEREncoded( |
| 148 ssl_info.cert->os_cert_handle(), cert); | 147 ssl_info.cert->os_cert_handle(), cert); |
| 149 if (result) | 148 if (result) |
| 150 VLOG(1) << "Successfully extracted peer certificate: " << *cert; | 149 VLOG(1) << "Successfully extracted peer certificate: " << *cert; |
| 151 return result; | 150 return result; |
| 152 } | 151 } |
| 153 | 152 |
| 154 int CastSocket::SendAuthChallenge() { | 153 bool CastSocket::VerifyChallengeReply() { |
| 155 CastMessage challenge_message; | 154 return AuthenticateChallengeReply(*challenge_reply_.get(), peer_cert_); |
| 156 CreateAuthChallengeMessage(&challenge_message); | |
| 157 VLOG(1) << "Sending challenge: " << CastMessageToString(challenge_message); | |
| 158 int result = SendMessageInternal( | |
| 159 challenge_message, | |
| 160 base::Bind(&CastSocket::OnChallengeEvent, AsWeakPtr())); | |
| 161 return (result < 0) ? result : net::OK; | |
| 162 } | |
| 163 | |
| 164 int CastSocket::ReadAuthChallengeReply() { | |
| 165 int result = ReadData(); | |
| 166 return (result < 0) ? result : net::OK; | |
| 167 } | |
| 168 | |
| 169 void CastSocket::OnConnectComplete(int result) { | |
| 170 int rv = DoConnectLoop(result); | |
| 171 if (rv != net::ERR_IO_PENDING) | |
| 172 DoConnectCallback(rv); | |
| 173 } | |
| 174 | |
| 175 void CastSocket::OnChallengeEvent(int result) { | |
| 176 // result >= 0 means read or write succeeded synchronously. | |
| 177 int rv = DoConnectLoop(result >= 0 ? net::OK : result); | |
| 178 if (rv != net::ERR_IO_PENDING) | |
| 179 DoConnectCallback(rv); | |
| 180 } | 155 } |
| 181 | 156 |
| 182 void CastSocket::Connect(const net::CompletionCallback& callback) { | 157 void CastSocket::Connect(const net::CompletionCallback& callback) { |
| 183 DCHECK(CalledOnValidThread()); | 158 DCHECK(CalledOnValidThread()); |
| 184 int result = net::ERR_CONNECTION_FAILED; | |
| 185 VLOG(1) << "Connect readyState = " << ready_state_; | 159 VLOG(1) << "Connect readyState = " << ready_state_; |
| 186 if (ready_state_ != READY_STATE_NONE) { | 160 if (ready_state_ != READY_STATE_NONE) { |
| 187 callback.Run(result); | 161 callback.Run(net::ERR_CONNECTION_FAILED); |
| 188 return; | 162 return; |
| 189 } | 163 } |
| 190 if (!ParseChannelUrl(url_)) { | 164 if (!ParseChannelUrl(url_)) { |
| 191 CloseWithError(cast_channel::CHANNEL_ERROR_CONNECT_ERROR); | 165 CloseWithError(cast_channel::CHANNEL_ERROR_CONNECT_ERROR); |
| 192 callback.Run(result); | 166 callback.Run(net::ERR_CONNECTION_FAILED); |
| 193 return; | 167 return; |
| 194 } | 168 } |
| 169 | |
| 195 connect_callback_ = callback; | 170 connect_callback_ = callback; |
| 196 next_state_ = CONN_STATE_TCP_CONNECT; | 171 connect_state_ = CONN_STATE_TCP_CONNECT; |
| 197 int rv = DoConnectLoop(net::OK); | 172 DoConnectLoop(net::OK); |
| 198 if (rv != net::ERR_IO_PENDING) | |
| 199 DoConnectCallback(rv); | |
| 200 } | 173 } |
| 201 | 174 |
| 202 // This method performs the state machine transitions for connection flow. | 175 // This method performs the state machine transitions for connection flow. |
| 203 // There are two entry points to this method: | 176 // There are two entry points to this method: |
| 204 // 1. public Connect method: this starts the flow | 177 // 1. Connect method: this starts the flow |
| 205 // 2. OnConnectComplete: callback method called when an async operation | 178 // 2. Callback from network operations that finish asynchronously |
| 206 // is done. OnConnectComplete calls this method to continue the state | 179 void CastSocket::DoConnectLoop(int result) { |
| 207 // machine transitions. | |
| 208 int CastSocket::DoConnectLoop(int result) { | |
| 209 // Avoid re-entrancy as a result of synchronous completion. | |
| 210 if (in_connect_loop_) | |
| 211 return net::ERR_IO_PENDING; | |
| 212 in_connect_loop_ = true; | |
| 213 | |
| 214 // Network operations can either finish synchronously or asynchronously. | 180 // Network operations can either finish synchronously or asynchronously. |
| 215 // This method executes the state machine transitions in a loop so that | 181 // This method executes the state machine transitions in a loop so that |
| 216 // correct state transitions happen even when network operations finish | 182 // correct state transitions happen even when network operations finish |
| 217 // synchronously. | 183 // synchronously. |
| 218 int rv = result; | 184 int rv = result; |
| 219 do { | 185 do { |
| 220 ConnectionState state = next_state_; | 186 ConnectionState state = connect_state_; |
| 221 // All the Do* methods do not set next_state_ in case of an | 187 // All the Do* methods do not set connect_state_ in case of an |
| 222 // error. So set next_state_ to NONE to figure out if the Do* | 188 // error. So set connect_state_ to NONE to figure out if the Do* |
| 223 // method changed state or not. | 189 // method changed state or not. |
| 224 next_state_ = CONN_STATE_NONE; | 190 connect_state_ = CONN_STATE_NONE; |
| 225 switch (state) { | 191 switch (state) { |
| 226 case CONN_STATE_TCP_CONNECT: | 192 case CONN_STATE_TCP_CONNECT: |
| 227 rv = DoTcpConnect(); | 193 rv = DoTcpConnect(); |
| 228 break; | 194 break; |
| 229 case CONN_STATE_TCP_CONNECT_COMPLETE: | 195 case CONN_STATE_TCP_CONNECT_COMPLETE: |
| 230 rv = DoTcpConnectComplete(rv); | 196 rv = DoTcpConnectComplete(rv); |
| 231 break; | 197 break; |
| 232 case CONN_STATE_SSL_CONNECT: | 198 case CONN_STATE_SSL_CONNECT: |
| 233 DCHECK_EQ(net::OK, rv); | 199 DCHECK_EQ(net::OK, rv); |
| 234 rv = DoSslConnect(); | 200 rv = DoSslConnect(); |
| 235 break; | 201 break; |
| 236 case CONN_STATE_SSL_CONNECT_COMPLETE: | 202 case CONN_STATE_SSL_CONNECT_COMPLETE: |
| 237 rv = DoSslConnectComplete(rv); | 203 rv = DoSslConnectComplete(rv); |
| 238 break; | 204 break; |
| 239 case CONN_STATE_AUTH_CHALLENGE_SEND: | 205 case CONN_STATE_AUTH_CHALLENGE_SEND: |
| 240 rv = DoAuthChallengeSend(); | 206 rv = DoAuthChallengeSend(); |
| 241 break; | 207 break; |
| 242 case CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE: | 208 case CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE: |
| 243 rv = DoAuthChallengeSendComplete(rv); | 209 rv = DoAuthChallengeSendComplete(rv); |
| 244 break; | 210 break; |
| 245 case CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE: | 211 case CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE: |
| 246 rv = DoAuthChallengeReplyComplete(rv); | 212 rv = DoAuthChallengeReplyComplete(rv); |
| 247 break; | 213 break; |
| 248 | |
| 249 default: | 214 default: |
| 250 NOTREACHED() << "BUG in CastSocket state machine code"; | 215 NOTREACHED() << "BUG in CastSocket connection state machine code"; |
| 251 break; | 216 break; |
| 252 } | 217 } |
| 253 } while (rv != net::ERR_IO_PENDING && next_state_ != CONN_STATE_NONE); | 218 } while (rv != net::ERR_IO_PENDING && connect_state_ != CONN_STATE_NONE); |
| 254 // Get out of the loop either when: | 219 // Get out of the loop either when: |
| 255 // a. A network operation is pending, OR | 220 // a. A network operation is pending, OR |
| 256 // b. The Do* method called did not change state | 221 // b. The Do* method called did not change state |
| 257 | 222 |
| 258 in_connect_loop_ = false; | 223 // If there is no pending IO and if we still got out of the loop then |
| 259 | 224 // either we are done successfully or there was an error; invoke the |
| 260 return rv; | 225 // callback in either case. |
| 226 if (rv != net::ERR_IO_PENDING) | |
| 227 DoConnectCallback(rv); | |
| 261 } | 228 } |
| 262 | 229 |
| 263 int CastSocket::DoTcpConnect() { | 230 int CastSocket::DoTcpConnect() { |
| 264 VLOG(1) << "DoTcpConnect"; | 231 VLOG(1) << "DoTcpConnect"; |
| 265 next_state_ = CONN_STATE_TCP_CONNECT_COMPLETE; | 232 connect_state_ = CONN_STATE_TCP_CONNECT_COMPLETE; |
| 266 tcp_socket_ = CreateTcpSocket(); | 233 tcp_socket_ = CreateTcpSocket(); |
| 267 return tcp_socket_->Connect( | 234 return tcp_socket_->Connect( |
| 268 base::Bind(&CastSocket::OnConnectComplete, AsWeakPtr())); | 235 base::Bind(&CastSocket::DoConnectLoop, AsWeakPtr())); |
| 269 } | 236 } |
| 270 | 237 |
| 271 int CastSocket::DoTcpConnectComplete(int result) { | 238 int CastSocket::DoTcpConnectComplete(int result) { |
| 272 VLOG(1) << "DoTcpConnectComplete: " << result; | 239 VLOG(1) << "DoTcpConnectComplete: " << result; |
| 273 if (result == net::OK) { | 240 if (result == net::OK) { |
| 274 // Enable TCP protocol-level keep-alive. | 241 // Enable TCP protocol-level keep-alive. |
| 275 bool result = tcp_socket_->SetKeepAlive(true, kTcpKeepAliveDelaySecs); | 242 bool success = tcp_socket_->SetKeepAlive(true, kTcpKeepAliveDelaySecs); |
| 276 LOG_IF(WARNING, !result) << "Failed to SetKeepAlive."; | 243 LOG_IF(WARNING, !success) << "Failed to SetKeepAlive."; |
| 277 next_state_ = CONN_STATE_SSL_CONNECT; | 244 connect_state_ = CONN_STATE_SSL_CONNECT; |
| 278 } | 245 } |
| 279 return result; | 246 return result; |
| 280 } | 247 } |
| 281 | 248 |
| 282 int CastSocket::DoSslConnect() { | 249 int CastSocket::DoSslConnect() { |
| 283 VLOG(1) << "DoSslConnect"; | 250 VLOG(1) << "DoSslConnect"; |
| 284 next_state_ = CONN_STATE_SSL_CONNECT_COMPLETE; | 251 connect_state_ = CONN_STATE_SSL_CONNECT_COMPLETE; |
| 285 socket_ = CreateSslSocket(); | 252 socket_ = CreateSslSocket(); |
| 286 return socket_->Connect( | 253 return socket_->Connect( |
| 287 base::Bind(&CastSocket::OnConnectComplete, AsWeakPtr())); | 254 base::Bind(&CastSocket::DoConnectLoop, AsWeakPtr())); |
| 288 } | 255 } |
| 289 | 256 |
| 290 int CastSocket::DoSslConnectComplete(int result) { | 257 int CastSocket::DoSslConnectComplete(int result) { |
| 291 VLOG(1) << "DoSslConnectComplete: " << result; | 258 VLOG(1) << "DoSslConnectComplete: " << result; |
| 292 if (result == net::ERR_CERT_AUTHORITY_INVALID && | 259 if (result == net::ERR_CERT_AUTHORITY_INVALID && |
| 293 peer_cert_.empty() && | 260 peer_cert_.empty() && |
| 294 ExtractPeerCert(&peer_cert_)) { | 261 ExtractPeerCert(&peer_cert_)) { |
| 295 next_state_ = CONN_STATE_TCP_CONNECT; | 262 connect_state_ = CONN_STATE_TCP_CONNECT; |
| 296 } else if (result == net::OK && auth_required_) { | 263 } else if (result == net::OK && auth_required_) { |
| 297 next_state_ = CONN_STATE_AUTH_CHALLENGE_SEND; | 264 connect_state_ = CONN_STATE_AUTH_CHALLENGE_SEND; |
| 298 } | 265 } |
| 299 return result; | 266 return result; |
| 300 } | 267 } |
| 301 | 268 |
| 302 int CastSocket::DoAuthChallengeSend() { | 269 int CastSocket::DoAuthChallengeSend() { |
| 303 VLOG(1) << "DoAuthChallengeSend"; | 270 VLOG(1) << "DoAuthChallengeSend"; |
| 304 next_state_ = CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE; | 271 connect_state_ = CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE; |
| 305 return SendAuthChallenge(); | 272 CastMessage challenge_message; |
| 273 CreateAuthChallengeMessage(&challenge_message); | |
| 274 VLOG(1) << "Sending challenge: " << CastMessageToString(challenge_message); | |
| 275 // Post a task to send a challenge message to avoid re-entrancy | |
|
mark a. foltz
2013/12/03 00:56:00
Not a big fan of posting a task to implement a con
Munjal (Google)
2013/12/03 18:10:58
Even if we do what you are suggesting - return bac
| |
| 276 base::MessageLoop::current()->PostTask( | |
| 277 FROM_HERE, | |
| 278 base::Bind(&CastSocket::SendCastMessageInternal, AsWeakPtr(), | |
| 279 challenge_message, | |
| 280 base::Bind(&CastSocket::DoConnectLoop, AsWeakPtr()))); | |
| 281 // Always return IO_PENDING since we always get the result asynchronously | |
| 282 return net::ERR_IO_PENDING; | |
| 306 } | 283 } |
| 307 | 284 |
| 308 int CastSocket::DoAuthChallengeSendComplete(int result) { | 285 int CastSocket::DoAuthChallengeSendComplete(int result) { |
| 309 VLOG(1) << "DoAuthChallengeSendComplete: " << result; | 286 VLOG(1) << "DoAuthChallengeSendComplete: " << result; |
| 310 if (result != net::OK) | 287 if (result < 0) |
| 311 return result; | 288 return result; |
| 312 next_state_ = CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE; | 289 connect_state_ = CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE; |
| 313 return ReadAuthChallengeReply(); | 290 PostTaskToStartReadLoop(); |
| 291 // Always return IO_PENDING since we always get the result asynchronously | |
| 292 return net::ERR_IO_PENDING; | |
| 314 } | 293 } |
| 315 | 294 |
| 316 int CastSocket::DoAuthChallengeReplyComplete(int result) { | 295 int CastSocket::DoAuthChallengeReplyComplete(int result) { |
| 317 VLOG(1) << "DoAuthChallengeReplyComplete: " << result; | 296 VLOG(1) << "DoAuthChallengeReplyComplete: " << result; |
| 318 if (result != net::OK) | 297 if (result < 0) |
| 319 return result; | 298 return result; |
| 320 if (!VerifyChallengeReply()) | 299 if (!VerifyChallengeReply()) |
| 321 return net::ERR_FAILED; | 300 return net::ERR_FAILED; |
| 322 VLOG(1) << "Auth challenge verification succeeded"; | 301 VLOG(1) << "Auth challenge verification succeeded"; |
| 323 return net::OK; | 302 return net::OK; |
| 324 } | 303 } |
| 325 | 304 |
| 326 bool CastSocket::VerifyChallengeReply() { | |
| 327 return AuthenticateChallengeReply(*challenge_reply_.get(), peer_cert_); | |
| 328 } | |
| 329 | |
| 330 void CastSocket::DoConnectCallback(int result) { | 305 void CastSocket::DoConnectCallback(int result) { |
| 331 ready_state_ = (result == net::OK) ? READY_STATE_OPEN : READY_STATE_CLOSED; | 306 ready_state_ = (result == net::OK) ? READY_STATE_OPEN : READY_STATE_CLOSED; |
| 332 error_state_ = (result == net::OK) ? | 307 error_state_ = (result == net::OK) ? |
| 333 CHANNEL_ERROR_NONE : CHANNEL_ERROR_CONNECT_ERROR; | 308 CHANNEL_ERROR_NONE : CHANNEL_ERROR_CONNECT_ERROR; |
| 309 // Start the ReadData loop if not already started. | |
| 310 if (result == net::OK) | |
| 311 PostTaskToStartReadLoop(); | |
| 334 base::ResetAndReturn(&connect_callback_).Run(result); | 312 base::ResetAndReturn(&connect_callback_).Run(result); |
| 335 // Start the ReadData loop if not already started. | |
| 336 // If auth_required_ is true we would've started a ReadData loop already. | |
| 337 // TODO(munjal): This is a bit ugly. Refactor read and write code. | |
| 338 if (result == net::OK && !auth_required_) | |
| 339 ReadData(); | |
| 340 } | 313 } |
| 341 | 314 |
| 342 void CastSocket::Close(const net::CompletionCallback& callback) { | 315 void CastSocket::Close(const net::CompletionCallback& callback) { |
| 343 DCHECK(CalledOnValidThread()); | 316 DCHECK(CalledOnValidThread()); |
| 344 VLOG(1) << "Close ReadyState = " << ready_state_; | 317 VLOG(1) << "Close ReadyState = " << ready_state_; |
| 345 tcp_socket_.reset(NULL); | 318 tcp_socket_.reset(NULL); |
| 346 socket_.reset(NULL); | 319 socket_.reset(NULL); |
| 347 cert_verifier_.reset(NULL); | 320 cert_verifier_.reset(NULL); |
| 348 transport_security_state_.reset(NULL); | 321 transport_security_state_.reset(NULL); |
| 349 ready_state_ = READY_STATE_CLOSED; | 322 ready_state_ = READY_STATE_CLOSED; |
| 350 callback.Run(net::OK); | 323 callback.Run(net::OK); |
| 351 } | 324 } |
| 352 | 325 |
| 353 void CastSocket::SendMessage(const MessageInfo& message, | 326 void CastSocket::SendMessage(const MessageInfo& message, |
| 354 const net::CompletionCallback& callback) { | 327 const net::CompletionCallback& callback) { |
| 355 DCHECK(CalledOnValidThread()); | 328 DCHECK(CalledOnValidThread()); |
| 356 VLOG(1) << "Send ReadyState " << ready_state_; | |
| 357 int result = net::ERR_FAILED; | |
| 358 if (ready_state_ != READY_STATE_OPEN) { | 329 if (ready_state_ != READY_STATE_OPEN) { |
| 359 callback.Run(result); | 330 callback.Run(net::ERR_FAILED); |
| 360 return; | 331 return; |
| 361 } | 332 } |
| 362 CastMessage message_proto; | 333 CastMessage message_proto; |
| 363 if (!MessageInfoToCastMessage(message, &message_proto)) { | 334 if (!MessageInfoToCastMessage(message, &message_proto)) { |
| 364 CloseWithError(cast_channel::CHANNEL_ERROR_INVALID_MESSAGE); | 335 callback.Run(net::ERR_FAILED); |
| 365 // TODO(mfoltz): Do a better job of signaling cast_channel errors to the | |
| 366 // caller. | |
| 367 callback.Run(net::OK); | |
| 368 return; | 336 return; |
| 369 } | 337 } |
| 370 SendMessageInternal(message_proto, callback); | 338 |
| 339 SendCastMessageInternal(message_proto, callback); | |
| 371 } | 340 } |
| 372 | 341 |
| 373 int CastSocket::SendMessageInternal(const CastMessage& message_proto, | 342 void CastSocket::SendCastMessageInternal( |
| 374 const net::CompletionCallback& callback) { | 343 const CastMessage& message, |
| 344 const net::CompletionCallback& callback) { | |
| 375 WriteRequest write_request(callback); | 345 WriteRequest write_request(callback); |
| 376 if (!write_request.SetContent(message_proto)) | 346 if (!write_request.SetContent(message)) { |
| 377 return net::ERR_FAILED; | 347 callback.Run(net::ERR_FAILED); |
| 348 return; | |
| 349 } | |
| 350 | |
| 378 write_queue_.push(write_request); | 351 write_queue_.push(write_request); |
| 379 return WriteData(); | 352 if (write_state_ == WRITE_STATE_NONE) { |
| 353 write_state_ = WRITE_STATE_WRITE; | |
| 354 DoWriteLoop(net::OK); | |
| 355 } | |
| 380 } | 356 } |
| 381 | 357 |
| 382 int CastSocket::WriteData() { | 358 void CastSocket::ClearWriteQueue() { |
| 359 while (!write_queue_.empty()) | |
| 360 write_queue_.pop(); | |
| 361 } | |
| 362 | |
| 363 void CastSocket::DoWriteLoop(int result) { | |
| 383 DCHECK(CalledOnValidThread()); | 364 DCHECK(CalledOnValidThread()); |
| 384 VLOG(1) << "WriteData q = " << write_queue_.size(); | 365 VLOG(1) << "WriteData q = " << write_queue_.size(); |
| 385 if (write_queue_.empty() || write_callback_pending_) | |
| 386 return net::ERR_FAILED; | |
| 387 | 366 |
| 367 if (write_queue_.empty()) | |
| 368 return; | |
| 369 | |
| 370 int rv = result; | |
|
mark a. foltz
2013/12/03 00:56:00
You might add a brief comment referring the reader
Munjal (Google)
2013/12/03 18:10:58
Done.
| |
| 371 do { | |
| 372 WriteState state = write_state_; | |
| 373 write_state_ = WRITE_STATE_NONE; | |
| 374 switch (state) { | |
| 375 case WRITE_STATE_WRITE: | |
| 376 rv = DoWrite(); | |
| 377 break; | |
| 378 case WRITE_STATE_WRITE_COMPLETE: | |
| 379 rv = DoWriteComplete(rv); | |
| 380 break; | |
| 381 case WRITE_STATE_ERROR: | |
| 382 rv = DoWriteError(rv); | |
| 383 break; | |
| 384 default: | |
| 385 NOTREACHED() << "BUG in CastSocket write state machine code"; | |
| 386 break; | |
| 387 } | |
| 388 } while (!write_queue_.empty() && | |
| 389 rv != net::ERR_IO_PENDING && | |
| 390 write_state_ != WRITE_STATE_NONE); | |
| 391 } | |
| 392 | |
| 393 int CastSocket::DoWrite() { | |
| 394 DCHECK(!write_queue_.empty()); | |
| 388 WriteRequest& request = write_queue_.front(); | 395 WriteRequest& request = write_queue_.front(); |
| 389 | 396 |
| 390 VLOG(1) << "WriteData byte_count = " << request.io_buffer->size() | 397 VLOG(1) << "WriteData byte_count = " << request.io_buffer->size() |
| 391 << " bytes_written " << request.io_buffer->BytesConsumed(); | 398 << " bytes_written " << request.io_buffer->BytesConsumed(); |
| 392 | 399 |
| 393 write_callback_pending_ = true; | 400 write_state_ = WRITE_STATE_WRITE_COMPLETE; |
| 394 int result = socket_->Write( | 401 |
| 402 return socket_->Write( | |
| 395 request.io_buffer.get(), | 403 request.io_buffer.get(), |
| 396 request.io_buffer->BytesRemaining(), | 404 request.io_buffer->BytesRemaining(), |
| 397 base::Bind(&CastSocket::OnWriteData, AsWeakPtr())); | 405 base::Bind(&CastSocket::DoWriteLoop, AsWeakPtr())); |
| 398 | |
| 399 if (result != net::ERR_IO_PENDING) | |
| 400 OnWriteData(result); | |
| 401 | |
| 402 return result; | |
| 403 } | 406 } |
| 404 | 407 |
| 405 void CastSocket::OnWriteData(int result) { | 408 int CastSocket::DoWriteComplete(int result) { |
| 406 DCHECK(CalledOnValidThread()); | |
| 407 VLOG(1) << "OnWriteComplete result = " << result; | |
| 408 DCHECK(write_callback_pending_); | |
| 409 DCHECK(!write_queue_.empty()); | 409 DCHECK(!write_queue_.empty()); |
| 410 write_callback_pending_ = false; | |
| 411 WriteRequest& request = write_queue_.front(); | 410 WriteRequest& request = write_queue_.front(); |
| 412 scoped_refptr<net::DrainableIOBuffer> io_buffer = request.io_buffer; | |
| 413 | 411 |
| 414 if (result >= 0) { | 412 if (result < 0) { |
| 415 io_buffer->DidConsume(result); | 413 // ERROR: remove the write request, get into error state and |
| 416 if (io_buffer->BytesRemaining() > 0) { | 414 // do the write completion callback. |
| 417 VLOG(1) << "OnWriteComplete size = " << io_buffer->size() | 415 write_queue_.pop(); |
| 418 << " consumed " << io_buffer->BytesConsumed() | 416 write_state_ = WRITE_STATE_ERROR; |
| 419 << " remaining " << io_buffer->BytesRemaining() | 417 request.callback.Run(result); |
| 420 << " # requests " << write_queue_.size(); | 418 return result; |
| 421 WriteData(); | |
| 422 return; | |
| 423 } | |
| 424 DCHECK_EQ(io_buffer->BytesConsumed(), io_buffer->size()); | |
| 425 DCHECK_EQ(io_buffer->BytesRemaining(), 0); | |
| 426 result = io_buffer->BytesConsumed(); | |
| 427 } | 419 } |
| 428 | 420 |
| 429 request.callback.Run(result); | 421 if (result > 0) { |
|
mark a. foltz
2013/12/03 00:56:00
Thank you, this is clearer and easier to read.
Munjal (Google)
2013/12/03 18:10:58
Sure.
| |
| 430 write_queue_.pop(); | 422 scoped_refptr<net::DrainableIOBuffer> io_buffer = request.io_buffer; |
| 431 | 423 // Some bytes were successfully written |
| 432 VLOG(1) << "OnWriteComplete size = " << io_buffer->size() | 424 io_buffer->DidConsume(result); |
| 433 << " consumed " << io_buffer->BytesConsumed() | 425 if (io_buffer->BytesRemaining() == 0) { |
| 434 << " remaining " << io_buffer->BytesRemaining() | 426 // A message is sent fully, do the write completion callback |
| 435 << " # requests " << write_queue_.size(); | 427 request.callback.Run(io_buffer->BytesConsumed()); |
| 436 | 428 write_queue_.pop(); |
| 437 if (result < 0) { | 429 } |
| 438 CloseWithError(CHANNEL_ERROR_SOCKET_ERROR); | |
| 439 return; | |
| 440 } | 430 } |
| 441 | 431 |
| 442 if (!write_queue_.empty()) | 432 write_state_ = WRITE_STATE_WRITE; |
| 443 WriteData(); | 433 return net::OK; |
| 444 } | 434 } |
| 445 | 435 |
| 446 int CastSocket::ReadData() { | 436 int CastSocket::DoWriteError(int result) { |
| 437 DCHECK(!write_queue_.empty()); | |
| 438 DCHECK_LT(result, 0); | |
| 439 // TODO(munjal): Consider reporting error to all pending writes | |
| 440 ClearWriteQueue(); | |
| 441 CloseWithError(CHANNEL_ERROR_SOCKET_ERROR); | |
| 442 return net::OK; | |
| 443 } | |
| 444 | |
| 445 void CastSocket::PostTaskToStartReadLoop() { | |
| 447 DCHECK(CalledOnValidThread()); | 446 DCHECK(CalledOnValidThread()); |
| 448 if (!socket_.get()) | 447 // Post a task to avoid re-entrancy into DoConnectLoop |
| 449 return net::ERR_FAILED; | 448 base::MessageLoop::current()->PostTask( |
| 450 DCHECK(!read_callback_pending_); | 449 FROM_HERE, |
| 451 read_callback_pending_ = true; | 450 base::Bind(&CastSocket::StartReadLoop, AsWeakPtr())); |
| 451 } | |
| 452 | |
| 453 void CastSocket::StartReadLoop() { | |
| 454 // If we are in READ_STATE_NONE then get into appropriate | |
| 455 // starting state and start the read loop | |
| 456 if (read_state_ == READ_STATE_NONE) { | |
| 457 read_state_ = READ_STATE_READ; | |
| 458 DoReadLoop(net::OK); | |
| 459 } | |
| 460 } | |
| 461 | |
| 462 void CastSocket::DoReadLoop(int result) { | |
| 463 DCHECK(CalledOnValidThread()); | |
| 464 int rv = result; | |
| 465 do { | |
| 466 ReadState state = read_state_; | |
| 467 read_state_ = READ_STATE_NONE; | |
| 468 | |
|
mark a. foltz
2013/12/03 00:56:00
Add a brief comment describing the control flow an
Munjal (Google)
2013/12/03 18:10:58
Done.
| |
| 469 switch (state) { | |
| 470 case READ_STATE_READ: | |
| 471 rv = DoRead(); | |
| 472 break; | |
| 473 case READ_STATE_READ_COMPLETE: | |
| 474 rv = DoReadComplete(rv); | |
| 475 break; | |
| 476 case READ_STATE_ERROR: | |
| 477 rv = DoReadError(rv); | |
| 478 break; | |
| 479 default: | |
| 480 NOTREACHED() << "BUG in read state machine"; | |
| 481 break; | |
| 482 } | |
| 483 } while (rv != net::ERR_IO_PENDING && read_state_ != READ_STATE_NONE); | |
| 484 } | |
| 485 | |
| 486 int CastSocket::DoRead() { | |
| 487 read_state_ = READ_STATE_READ_COMPLETE; | |
| 452 // Figure out if we are reading the header or body, and the remaining bytes. | 488 // Figure out if we are reading the header or body, and the remaining bytes. |
| 453 uint32 num_bytes_to_read = 0; | 489 uint32 num_bytes_to_read = 0; |
| 454 if (header_read_buffer_->RemainingCapacity() > 0) { | 490 if (header_read_buffer_->RemainingCapacity() > 0) { |
| 455 current_read_buffer_ = header_read_buffer_; | 491 current_read_buffer_ = header_read_buffer_; |
| 456 num_bytes_to_read = header_read_buffer_->RemainingCapacity(); | 492 num_bytes_to_read = header_read_buffer_->RemainingCapacity(); |
| 457 DCHECK_LE(num_bytes_to_read, kMessageHeaderSize); | 493 DCHECK_LE(num_bytes_to_read, kMessageHeaderSize); |
| 458 } else { | 494 } else { |
| 459 DCHECK_GT(current_message_size_, 0U); | 495 DCHECK_GT(current_message_size_, 0U); |
| 460 num_bytes_to_read = current_message_size_ - body_read_buffer_->offset(); | 496 num_bytes_to_read = current_message_size_ - body_read_buffer_->offset(); |
| 461 current_read_buffer_ = body_read_buffer_; | 497 current_read_buffer_ = body_read_buffer_; |
| 462 DCHECK_LE(num_bytes_to_read, kMaxMessageSize); | 498 DCHECK_LE(num_bytes_to_read, kMaxMessageSize); |
| 463 } | 499 } |
| 464 DCHECK_GT(num_bytes_to_read, 0U); | 500 DCHECK_GT(num_bytes_to_read, 0U); |
| 501 | |
| 465 // We read up to num_bytes_to_read into |current_read_buffer_|. | 502 // We read up to num_bytes_to_read into |current_read_buffer_|. |
| 466 int result = socket_->Read( | 503 return socket_->Read( |
| 467 current_read_buffer_.get(), | 504 current_read_buffer_.get(), |
| 468 num_bytes_to_read, | 505 num_bytes_to_read, |
| 469 base::Bind(&CastSocket::OnReadData, AsWeakPtr())); | 506 base::Bind(&CastSocket::DoReadLoop, AsWeakPtr())); |
| 470 VLOG(1) << "ReadData result = " << result; | |
| 471 if (result > 0) { | |
| 472 OnReadData(result); | |
| 473 } else if (result != net::ERR_IO_PENDING) { | |
| 474 CloseWithError(CHANNEL_ERROR_SOCKET_ERROR); | |
| 475 } | |
| 476 return result; | |
| 477 } | 507 } |
| 478 | 508 |
| 479 void CastSocket::OnReadData(int result) { | 509 int CastSocket::DoReadComplete(int result) { |
| 480 DCHECK(CalledOnValidThread()); | 510 DCHECK(CalledOnValidThread()); |
| 481 VLOG(1) << "OnReadData result = " << result | 511 VLOG(1) << "DoReadDataComplete result = " << result |
| 482 << " header offset = " << header_read_buffer_->offset() | 512 << " header offset = " << header_read_buffer_->offset() |
| 483 << " body offset = " << body_read_buffer_->offset(); | 513 << " body offset = " << body_read_buffer_->offset(); |
| 484 read_callback_pending_ = false; | |
| 485 if (result <= 0) { | 514 if (result <= 0) { |
| 486 CloseWithError(CHANNEL_ERROR_SOCKET_ERROR); | 515 error_state_ = CHANNEL_ERROR_SOCKET_ERROR; |
| 487 return; | 516 read_state_ = READ_STATE_ERROR; |
| 517 return result; | |
| 488 } | 518 } |
| 519 | |
| 489 // We read some data. Move the offset in the current buffer forward. | 520 // We read some data. Move the offset in the current buffer forward. |
| 490 DCHECK_LE(current_read_buffer_->offset() + result, | 521 DCHECK_LE(current_read_buffer_->offset() + result, |
| 491 current_read_buffer_->capacity()); | 522 current_read_buffer_->capacity()); |
| 492 current_read_buffer_->set_offset(current_read_buffer_->offset() + result); | 523 current_read_buffer_->set_offset(current_read_buffer_->offset() + result); |
| 493 | 524 |
| 494 bool should_continue = true; | 525 bool success = true; |
| 495 if (current_read_buffer_.get() == header_read_buffer_.get() && | 526 if (current_read_buffer_.get() == header_read_buffer_.get() && |
| 496 current_read_buffer_->RemainingCapacity() == 0) { | 527 current_read_buffer_->RemainingCapacity() == 0) { |
| 497 // If we have read a full header, process the contents. | 528 // If we have read a full header, process the contents. |
| 498 should_continue = ProcessHeader(); | 529 success = ProcessHeader(); |
| 499 } else if (current_read_buffer_.get() == body_read_buffer_.get() && | 530 } else if (current_read_buffer_.get() == body_read_buffer_.get() && |
| 500 static_cast<uint32>(current_read_buffer_->offset()) == | 531 static_cast<uint32>(current_read_buffer_->offset()) == |
| 501 current_message_size_) { | 532 current_message_size_) { |
| 502 // If we have read a full body, process the contents. | 533 // If we have read a full body, process the contents. |
| 503 should_continue = ProcessBody(); | 534 success = ProcessBody(); |
| 504 } | 535 } |
| 505 if (should_continue) | 536 |
| 506 ReadData(); | 537 if (success) |
| 538 read_state_ = READ_STATE_READ; | |
| 539 return net::OK; | |
| 540 } | |
| 541 | |
| 542 int CastSocket::DoReadError(int result) { | |
| 543 DCHECK_LT(result, 0); | |
| 544 CloseWithError(error_state_); | |
| 545 return net::OK; | |
| 507 } | 546 } |
| 508 | 547 |
| 509 bool CastSocket::ProcessHeader() { | 548 bool CastSocket::ProcessHeader() { |
| 510 DCHECK_EQ(static_cast<uint32>(header_read_buffer_->offset()), | 549 DCHECK_EQ(static_cast<uint32>(header_read_buffer_->offset()), |
| 511 kMessageHeaderSize); | 550 kMessageHeaderSize); |
| 512 MessageHeader header; | 551 MessageHeader header; |
| 513 MessageHeader::ReadFromIOBuffer(header_read_buffer_.get(), &header); | 552 MessageHeader::ReadFromIOBuffer(header_read_buffer_.get(), &header); |
| 514 if (header.message_size > kMaxMessageSize) { | 553 if (header.message_size > kMaxMessageSize) { |
| 515 CloseWithError(cast_channel::CHANNEL_ERROR_INVALID_MESSAGE); | 554 error_state_ = cast_channel::CHANNEL_ERROR_INVALID_MESSAGE; |
| 516 return false; | 555 return false; |
| 517 } | 556 } |
| 518 VLOG(1) << "Parsed header { message_size: " << header.message_size << " }"; | 557 VLOG(1) << "Parsed header { message_size: " << header.message_size << " }"; |
| 519 current_message_size_ = header.message_size; | 558 current_message_size_ = header.message_size; |
| 520 return true; | 559 return true; |
| 521 } | 560 } |
| 522 | 561 |
| 523 bool CastSocket::ProcessBody() { | 562 bool CastSocket::ProcessBody() { |
| 524 DCHECK_EQ(static_cast<uint32>(body_read_buffer_->offset()), | 563 DCHECK_EQ(static_cast<uint32>(body_read_buffer_->offset()), |
| 525 current_message_size_); | 564 current_message_size_); |
| 526 if (!ParseMessageFromBody()) { | 565 if (!ParseMessageFromBody()) { |
| 527 CloseWithError(cast_channel::CHANNEL_ERROR_INVALID_MESSAGE); | 566 error_state_ = cast_channel::CHANNEL_ERROR_INVALID_MESSAGE; |
| 528 return false; | 567 return false; |
| 529 } | 568 } |
| 530 current_message_size_ = 0; | 569 current_message_size_ = 0; |
| 531 header_read_buffer_->set_offset(0); | 570 header_read_buffer_->set_offset(0); |
| 532 body_read_buffer_->set_offset(0); | 571 body_read_buffer_->set_offset(0); |
| 533 current_read_buffer_ = header_read_buffer_; | 572 current_read_buffer_ = header_read_buffer_; |
| 534 return true; | 573 return true; |
| 535 } | 574 } |
| 536 | 575 |
| 537 bool CastSocket::ParseMessageFromBody() { | 576 bool CastSocket::ParseMessageFromBody() { |
| 538 DCHECK(CalledOnValidThread()); | 577 DCHECK(CalledOnValidThread()); |
| 539 DCHECK_EQ(static_cast<uint32>(body_read_buffer_->offset()), | 578 DCHECK_EQ(static_cast<uint32>(body_read_buffer_->offset()), |
| 540 current_message_size_); | 579 current_message_size_); |
| 541 CastMessage message_proto; | 580 CastMessage message_proto; |
| 542 if (!message_proto.ParseFromArray( | 581 if (!message_proto.ParseFromArray( |
| 543 body_read_buffer_->StartOfBuffer(), | 582 body_read_buffer_->StartOfBuffer(), |
| 544 current_message_size_)) | 583 current_message_size_)) { |
| 545 return false; | 584 return false; |
| 585 } | |
| 546 VLOG(1) << "Parsed message " << CastMessageToString(message_proto); | 586 VLOG(1) << "Parsed message " << CastMessageToString(message_proto); |
| 547 // If the message is an auth message then we handle it internally. | 587 // If the message is an auth message then we handle it internally. |
| 548 if (IsAuthMessage(message_proto)) { | 588 if (IsAuthMessage(message_proto)) { |
| 549 challenge_reply_.reset(new CastMessage(message_proto)); | 589 challenge_reply_.reset(new CastMessage(message_proto)); |
| 550 OnChallengeEvent(net::OK); | 590 DoConnectLoop(net::OK); |
|
mark a. foltz
2013/12/03 00:56:00
So the flow here is DoConnectLoop -> DoAuthChallen
Munjal (Google)
2013/12/03 18:10:58
I am trying an approach to either remove the need
| |
| 551 } else if (delegate_) { | 591 } else if (delegate_) { |
| 552 MessageInfo message; | 592 MessageInfo message; |
| 553 if (!CastMessageToMessageInfo(message_proto, &message)) | 593 if (!CastMessageToMessageInfo(message_proto, &message)) |
| 554 return false; | 594 return false; |
| 555 delegate_->OnMessage(this, message); | 595 delegate_->OnMessage(this, message); |
| 556 } | 596 } |
| 557 return true; | 597 return true; |
| 558 } | 598 } |
| 559 | 599 |
| 560 // static | 600 // static |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 670 io_buffer = new net::DrainableIOBuffer(new net::StringIOBuffer(message_data), | 710 io_buffer = new net::DrainableIOBuffer(new net::StringIOBuffer(message_data), |
| 671 message_data.size()); | 711 message_data.size()); |
| 672 return true; | 712 return true; |
| 673 } | 713 } |
| 674 | 714 |
| 675 CastSocket::WriteRequest::~WriteRequest() { } | 715 CastSocket::WriteRequest::~WriteRequest() { } |
| 676 | 716 |
| 677 } // namespace cast_channel | 717 } // namespace cast_channel |
| 678 } // namespace api | 718 } // namespace api |
| 679 } // namespace extensions | 719 } // namespace extensions |
| OLD | NEW |