| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/browser/api/cast_channel/cast_socket.h" | 5 #include "extensions/browser/api/cast_channel/cast_socket.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 #include "net/socket/client_socket_handle.h" | 43 #include "net/socket/client_socket_handle.h" |
| 44 #include "net/socket/ssl_client_socket.h" | 44 #include "net/socket/ssl_client_socket.h" |
| 45 #include "net/socket/stream_socket.h" | 45 #include "net/socket/stream_socket.h" |
| 46 #include "net/socket/tcp_client_socket.h" | 46 #include "net/socket/tcp_client_socket.h" |
| 47 #include "net/ssl/ssl_config_service.h" | 47 #include "net/ssl/ssl_config_service.h" |
| 48 #include "net/ssl/ssl_info.h" | 48 #include "net/ssl/ssl_info.h" |
| 49 | 49 |
| 50 // Helper for logging data with remote host IP and authentication state. | 50 // Helper for logging data with remote host IP and authentication state. |
| 51 // Assumes |ip_endpoint_| of type net::IPEndPoint and |channel_auth_| of enum | 51 // Assumes |ip_endpoint_| of type net::IPEndPoint and |channel_auth_| of enum |
| 52 // type ChannelAuthType are available in the current scope. | 52 // type ChannelAuthType are available in the current scope. |
| 53 #define CONNECTION_INFO() \ | 53 #define CONNECTION_INFO() \ |
| 54 "[" << ip_endpoint_.ToString() << ", auth=" << channel_auth_ << "] " | 54 "[" << ip_endpoint_.ToString() \ |
| 55 << ", auth=" << ::cast_channel::ToString(channel_auth_) << "] " |
| 55 #define VLOG_WITH_CONNECTION(level) VLOG(level) << CONNECTION_INFO() | 56 #define VLOG_WITH_CONNECTION(level) VLOG(level) << CONNECTION_INFO() |
| 56 #define LOG_WITH_CONNECTION(level) LOG(level) << CONNECTION_INFO() | 57 #define LOG_WITH_CONNECTION(level) LOG(level) << CONNECTION_INFO() |
| 57 | 58 |
| 58 namespace extensions { | 59 namespace extensions { |
| 59 static base::LazyInstance<BrowserContextKeyedAPIFactory< | 60 static base::LazyInstance<BrowserContextKeyedAPIFactory< |
| 60 ApiResourceManager<api::cast_channel::CastSocket>>>::DestructorAtExit | 61 ApiResourceManager<api::cast_channel::CastSocket>>>::DestructorAtExit |
| 61 g_factory = LAZY_INSTANCE_INITIALIZER; | 62 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 62 | 63 |
| 63 // static | 64 // static |
| 64 template <> | 65 template <> |
| (...skipping 25 matching lines...) Expand all Loading... |
| 90 std::unique_ptr<Request>*, | 91 std::unique_ptr<Request>*, |
| 91 const net::NetLogWithSource&) override { | 92 const net::NetLogWithSource&) override { |
| 92 verify_result->Reset(); | 93 verify_result->Reset(); |
| 93 verify_result->verified_cert = params.certificate(); | 94 verify_result->verified_cert = params.certificate(); |
| 94 return net::OK; | 95 return net::OK; |
| 95 } | 96 } |
| 96 }; | 97 }; |
| 97 | 98 |
| 98 } // namespace | 99 } // namespace |
| 99 | 100 |
| 101 using ChannelError = ::cast_channel::ChannelError; |
| 102 using ChannelAuthType = ::cast_channel::ChannelAuthType; |
| 103 using ReadyState = ::cast_channel::ReadyState; |
| 104 |
| 100 CastSocket::CastSocket(const std::string& owner_extension_id) | 105 CastSocket::CastSocket(const std::string& owner_extension_id) |
| 101 : ApiResource(owner_extension_id) { | 106 : ApiResource(owner_extension_id) { |
| 102 } | 107 } |
| 103 | 108 |
| 104 bool CastSocket::IsPersistent() const { | 109 bool CastSocket::IsPersistent() const { |
| 105 return true; | 110 return true; |
| 106 } | 111 } |
| 107 | 112 |
| 108 CastSocketImpl::CastSocketImpl(const std::string& owner_extension_id, | 113 CastSocketImpl::CastSocketImpl(const std::string& owner_extension_id, |
| 109 const net::IPEndPoint& ip_endpoint, | 114 const net::IPEndPoint& ip_endpoint, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 140 net_log_(net_log), | 145 net_log_(net_log), |
| 141 keep_alive_(keep_alive), | 146 keep_alive_(keep_alive), |
| 142 logger_(logger), | 147 logger_(logger), |
| 143 auth_context_(auth_context), | 148 auth_context_(auth_context), |
| 144 connect_timeout_(timeout), | 149 connect_timeout_(timeout), |
| 145 connect_timeout_timer_(new base::OneShotTimer), | 150 connect_timeout_timer_(new base::OneShotTimer), |
| 146 is_canceled_(false), | 151 is_canceled_(false), |
| 147 device_capabilities_(device_capabilities), | 152 device_capabilities_(device_capabilities), |
| 148 audio_only_(false), | 153 audio_only_(false), |
| 149 connect_state_(proto::CONN_STATE_START_CONNECT), | 154 connect_state_(proto::CONN_STATE_START_CONNECT), |
| 150 error_state_(CHANNEL_ERROR_NONE), | 155 error_state_(ChannelError::NONE), |
| 151 ready_state_(READY_STATE_NONE), | 156 ready_state_(ReadyState::NONE), |
| 152 auth_delegate_(nullptr) { | 157 auth_delegate_(nullptr) { |
| 153 DCHECK(net_log_); | 158 DCHECK(net_log_); |
| 154 net_log_source_.type = net::NetLogSourceType::SOCKET; | 159 net_log_source_.type = net::NetLogSourceType::SOCKET; |
| 155 net_log_source_.id = net_log_->NextID(); | 160 net_log_source_.id = net_log_->NextID(); |
| 156 } | 161 } |
| 157 | 162 |
| 158 CastSocketImpl::~CastSocketImpl() { | 163 CastSocketImpl::~CastSocketImpl() { |
| 159 // Ensure that resources are freed but do not run pending callbacks that | 164 // Ensure that resources are freed but do not run pending callbacks that |
| 160 // would result in re-entrancy. | 165 // would result in re-entrancy. |
| 161 CloseInternal(); | 166 CloseInternal(); |
| 162 | 167 |
| 163 if (!connect_callback_.is_null()) | 168 if (!connect_callback_.is_null()) |
| 164 base::ResetAndReturn(&connect_callback_).Run(CHANNEL_ERROR_UNKNOWN); | 169 base::ResetAndReturn(&connect_callback_).Run(ChannelError::UNKNOWN); |
| 165 } | 170 } |
| 166 | 171 |
| 167 ReadyState CastSocketImpl::ready_state() const { | 172 ReadyState CastSocketImpl::ready_state() const { |
| 168 return ready_state_; | 173 return ready_state_; |
| 169 } | 174 } |
| 170 | 175 |
| 171 ChannelError CastSocketImpl::error_state() const { | 176 ChannelError CastSocketImpl::error_state() const { |
| 172 return error_state_; | 177 return error_state_; |
| 173 } | 178 } |
| 174 | 179 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 } | 269 } |
| 265 | 270 |
| 266 void CastSocketImpl::SetTransportForTesting( | 271 void CastSocketImpl::SetTransportForTesting( |
| 267 std::unique_ptr<CastTransport> transport) { | 272 std::unique_ptr<CastTransport> transport) { |
| 268 transport_ = std::move(transport); | 273 transport_ = std::move(transport); |
| 269 } | 274 } |
| 270 | 275 |
| 271 void CastSocketImpl::Connect(std::unique_ptr<CastTransport::Delegate> delegate, | 276 void CastSocketImpl::Connect(std::unique_ptr<CastTransport::Delegate> delegate, |
| 272 base::Callback<void(ChannelError)> callback) { | 277 base::Callback<void(ChannelError)> callback) { |
| 273 DCHECK(CalledOnValidThread()); | 278 DCHECK(CalledOnValidThread()); |
| 274 VLOG_WITH_CONNECTION(1) << "Connect readyState = " << ready_state_; | 279 VLOG_WITH_CONNECTION(1) << "Connect readyState = " |
| 280 << ::cast_channel::ToString(ready_state_); |
| 275 DCHECK_EQ(proto::CONN_STATE_START_CONNECT, connect_state_); | 281 DCHECK_EQ(proto::CONN_STATE_START_CONNECT, connect_state_); |
| 276 | 282 |
| 277 delegate_ = std::move(delegate); | 283 delegate_ = std::move(delegate); |
| 278 | 284 |
| 279 if (ready_state_ != READY_STATE_NONE) { | 285 if (ready_state_ != ReadyState::NONE) { |
| 280 callback.Run(CHANNEL_ERROR_CONNECT_ERROR); | 286 callback.Run(ChannelError::CONNECT_ERROR); |
| 281 return; | 287 return; |
| 282 } | 288 } |
| 283 | 289 |
| 284 connect_callback_ = callback; | 290 connect_callback_ = callback; |
| 285 SetReadyState(READY_STATE_CONNECTING); | 291 SetReadyState(ReadyState::CONNECTING); |
| 286 SetConnectState(proto::CONN_STATE_TCP_CONNECT); | 292 SetConnectState(proto::CONN_STATE_TCP_CONNECT); |
| 287 | 293 |
| 288 // Set up connection timeout. | 294 // Set up connection timeout. |
| 289 if (connect_timeout_.InMicroseconds() > 0) { | 295 if (connect_timeout_.InMicroseconds() > 0) { |
| 290 DCHECK(connect_timeout_callback_.IsCancelled()); | 296 DCHECK(connect_timeout_callback_.IsCancelled()); |
| 291 connect_timeout_callback_.Reset( | 297 connect_timeout_callback_.Reset( |
| 292 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); | 298 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); |
| 293 GetTimer()->Start(FROM_HERE, | 299 GetTimer()->Start(FROM_HERE, |
| 294 connect_timeout_, | 300 connect_timeout_, |
| 295 connect_timeout_callback_.callback()); | 301 connect_timeout_callback_.callback()); |
| 296 } | 302 } |
| 297 | 303 |
| 298 DoConnectLoop(net::OK); | 304 DoConnectLoop(net::OK); |
| 299 } | 305 } |
| 300 | 306 |
| 301 CastTransport* CastSocketImpl::transport() const { | 307 CastTransport* CastSocketImpl::transport() const { |
| 302 return transport_.get(); | 308 return transport_.get(); |
| 303 } | 309 } |
| 304 | 310 |
| 305 void CastSocketImpl::OnConnectTimeout() { | 311 void CastSocketImpl::OnConnectTimeout() { |
| 306 DCHECK(CalledOnValidThread()); | 312 DCHECK(CalledOnValidThread()); |
| 307 // Stop all pending connection setup tasks and report back to the client. | 313 // Stop all pending connection setup tasks and report back to the client. |
| 308 is_canceled_ = true; | 314 is_canceled_ = true; |
| 309 VLOG_WITH_CONNECTION(1) << "Timeout while establishing a connection."; | 315 VLOG_WITH_CONNECTION(1) << "Timeout while establishing a connection."; |
| 310 SetErrorState(CHANNEL_ERROR_CONNECT_TIMEOUT); | 316 SetErrorState(ChannelError::CONNECT_TIMEOUT); |
| 311 DoConnectCallback(); | 317 DoConnectCallback(); |
| 312 } | 318 } |
| 313 | 319 |
| 314 void CastSocketImpl::ResetConnectLoopCallback() { | 320 void CastSocketImpl::ResetConnectLoopCallback() { |
| 315 DCHECK(connect_loop_callback_.IsCancelled()); | 321 DCHECK(connect_loop_callback_.IsCancelled()); |
| 316 connect_loop_callback_.Reset( | 322 connect_loop_callback_.Reset( |
| 317 base::Bind(&CastSocketImpl::DoConnectLoop, base::Unretained(this))); | 323 base::Bind(&CastSocketImpl::DoConnectLoop, base::Unretained(this))); |
| 318 } | 324 } |
| 319 | 325 |
| 320 void CastSocketImpl::PostTaskToStartConnectLoop(int result) { | 326 void CastSocketImpl::PostTaskToStartConnectLoop(int result) { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 case proto::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE: | 370 case proto::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE: |
| 365 rv = DoAuthChallengeSendComplete(rv); | 371 rv = DoAuthChallengeSendComplete(rv); |
| 366 break; | 372 break; |
| 367 case proto::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE: | 373 case proto::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE: |
| 368 rv = DoAuthChallengeReplyComplete(rv); | 374 rv = DoAuthChallengeReplyComplete(rv); |
| 369 DCHECK(IsTerminalState(connect_state_)); | 375 DCHECK(IsTerminalState(connect_state_)); |
| 370 break; | 376 break; |
| 371 default: | 377 default: |
| 372 NOTREACHED() << "Unknown state in connect flow: " << state; | 378 NOTREACHED() << "Unknown state in connect flow: " << state; |
| 373 SetConnectState(proto::CONN_STATE_FINISHED); | 379 SetConnectState(proto::CONN_STATE_FINISHED); |
| 374 SetErrorState(CHANNEL_ERROR_UNKNOWN); | 380 SetErrorState(ChannelError::UNKNOWN); |
| 375 DoConnectCallback(); | 381 DoConnectCallback(); |
| 376 return; | 382 return; |
| 377 } | 383 } |
| 378 } while (rv != net::ERR_IO_PENDING && !IsTerminalState(connect_state_)); | 384 } while (rv != net::ERR_IO_PENDING && !IsTerminalState(connect_state_)); |
| 379 // Exit the state machine if an asynchronous network operation is pending | 385 // Exit the state machine if an asynchronous network operation is pending |
| 380 // or if the state machine is in the terminal "finished" state. | 386 // or if the state machine is in the terminal "finished" state. |
| 381 | 387 |
| 382 if (IsTerminalState(connect_state_)) { | 388 if (IsTerminalState(connect_state_)) { |
| 383 DCHECK_NE(rv, net::ERR_IO_PENDING); | 389 DCHECK_NE(rv, net::ERR_IO_PENDING); |
| 384 GetTimer()->Stop(); | 390 GetTimer()->Stop(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 401 } | 407 } |
| 402 | 408 |
| 403 int CastSocketImpl::DoTcpConnectComplete(int connect_result) { | 409 int CastSocketImpl::DoTcpConnectComplete(int connect_result) { |
| 404 VLOG_WITH_CONNECTION(1) << "DoTcpConnectComplete: " << connect_result; | 410 VLOG_WITH_CONNECTION(1) << "DoTcpConnectComplete: " << connect_result; |
| 405 logger_->LogSocketEventWithRv(channel_id_, proto::TCP_SOCKET_CONNECT_COMPLETE, | 411 logger_->LogSocketEventWithRv(channel_id_, proto::TCP_SOCKET_CONNECT_COMPLETE, |
| 406 connect_result); | 412 connect_result); |
| 407 if (connect_result == net::OK) { | 413 if (connect_result == net::OK) { |
| 408 SetConnectState(proto::CONN_STATE_SSL_CONNECT); | 414 SetConnectState(proto::CONN_STATE_SSL_CONNECT); |
| 409 } else if (connect_result == net::ERR_CONNECTION_TIMED_OUT) { | 415 } else if (connect_result == net::ERR_CONNECTION_TIMED_OUT) { |
| 410 SetConnectState(proto::CONN_STATE_FINISHED); | 416 SetConnectState(proto::CONN_STATE_FINISHED); |
| 411 SetErrorState(CHANNEL_ERROR_CONNECT_TIMEOUT); | 417 SetErrorState(ChannelError::CONNECT_TIMEOUT); |
| 412 } else { | 418 } else { |
| 413 SetConnectState(proto::CONN_STATE_FINISHED); | 419 SetConnectState(proto::CONN_STATE_FINISHED); |
| 414 SetErrorState(CHANNEL_ERROR_CONNECT_ERROR); | 420 SetErrorState(ChannelError::CONNECT_ERROR); |
| 415 } | 421 } |
| 416 return connect_result; | 422 return connect_result; |
| 417 } | 423 } |
| 418 | 424 |
| 419 int CastSocketImpl::DoSslConnect() { | 425 int CastSocketImpl::DoSslConnect() { |
| 420 DCHECK(connect_loop_callback_.IsCancelled()); | 426 DCHECK(connect_loop_callback_.IsCancelled()); |
| 421 VLOG_WITH_CONNECTION(1) << "DoSslConnect"; | 427 VLOG_WITH_CONNECTION(1) << "DoSslConnect"; |
| 422 SetConnectState(proto::CONN_STATE_SSL_CONNECT_COMPLETE); | 428 SetConnectState(proto::CONN_STATE_SSL_CONNECT_COMPLETE); |
| 423 socket_ = CreateSslSocket(std::move(tcp_socket_)); | 429 socket_ = CreateSslSocket(std::move(tcp_socket_)); |
| 424 | 430 |
| 425 int rv = socket_->Connect( | 431 int rv = socket_->Connect( |
| 426 base::Bind(&CastSocketImpl::DoConnectLoop, base::Unretained(this))); | 432 base::Bind(&CastSocketImpl::DoConnectLoop, base::Unretained(this))); |
| 427 logger_->LogSocketEventWithRv(channel_id_, proto::SSL_SOCKET_CONNECT, rv); | 433 logger_->LogSocketEventWithRv(channel_id_, proto::SSL_SOCKET_CONNECT, rv); |
| 428 return rv; | 434 return rv; |
| 429 } | 435 } |
| 430 | 436 |
| 431 int CastSocketImpl::DoSslConnectComplete(int result) { | 437 int CastSocketImpl::DoSslConnectComplete(int result) { |
| 432 logger_->LogSocketEventWithRv(channel_id_, proto::SSL_SOCKET_CONNECT_COMPLETE, | 438 logger_->LogSocketEventWithRv(channel_id_, proto::SSL_SOCKET_CONNECT_COMPLETE, |
| 433 result); | 439 result); |
| 434 VLOG_WITH_CONNECTION(1) << "DoSslConnectComplete: " << result; | 440 VLOG_WITH_CONNECTION(1) << "DoSslConnectComplete: " << result; |
| 435 if (result == net::OK) { | 441 if (result == net::OK) { |
| 436 peer_cert_ = ExtractPeerCert(); | 442 peer_cert_ = ExtractPeerCert(); |
| 437 | 443 |
| 438 if (!peer_cert_) { | 444 if (!peer_cert_) { |
| 439 LOG_WITH_CONNECTION(WARNING) << "Could not extract peer cert."; | 445 LOG_WITH_CONNECTION(WARNING) << "Could not extract peer cert."; |
| 440 SetConnectState(proto::CONN_STATE_FINISHED); | 446 SetConnectState(proto::CONN_STATE_FINISHED); |
| 441 SetErrorState(CHANNEL_ERROR_AUTHENTICATION_ERROR); | 447 SetErrorState(ChannelError::AUTHENTICATION_ERROR); |
| 442 return net::ERR_CERT_INVALID; | 448 return net::ERR_CERT_INVALID; |
| 443 } | 449 } |
| 444 | 450 |
| 445 // SSL connection succeeded. | 451 // SSL connection succeeded. |
| 446 if (!transport_.get()) { | 452 if (!transport_.get()) { |
| 447 // Create a channel transport if one wasn't already set (e.g. by test | 453 // Create a channel transport if one wasn't already set (e.g. by test |
| 448 // code). | 454 // code). |
| 449 transport_.reset(new CastTransportImpl(this->socket_.get(), channel_id_, | 455 transport_.reset(new CastTransportImpl(this->socket_.get(), channel_id_, |
| 450 ip_endpoint_, channel_auth_, | 456 ip_endpoint_, channel_auth_, |
| 451 logger_)); | 457 logger_)); |
| 452 } | 458 } |
| 453 auth_delegate_ = new AuthTransportDelegate(this); | 459 auth_delegate_ = new AuthTransportDelegate(this); |
| 454 transport_->SetReadDelegate(base::WrapUnique(auth_delegate_)); | 460 transport_->SetReadDelegate(base::WrapUnique(auth_delegate_)); |
| 455 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_SEND); | 461 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_SEND); |
| 456 } else if (result == net::ERR_CONNECTION_TIMED_OUT) { | 462 } else if (result == net::ERR_CONNECTION_TIMED_OUT) { |
| 457 SetConnectState(proto::CONN_STATE_FINISHED); | 463 SetConnectState(proto::CONN_STATE_FINISHED); |
| 458 SetErrorState(CHANNEL_ERROR_CONNECT_TIMEOUT); | 464 SetErrorState(ChannelError::CONNECT_TIMEOUT); |
| 459 } else { | 465 } else { |
| 460 SetConnectState(proto::CONN_STATE_FINISHED); | 466 SetConnectState(proto::CONN_STATE_FINISHED); |
| 461 SetErrorState(CHANNEL_ERROR_AUTHENTICATION_ERROR); | 467 SetErrorState(ChannelError::AUTHENTICATION_ERROR); |
| 462 } | 468 } |
| 463 return result; | 469 return result; |
| 464 } | 470 } |
| 465 | 471 |
| 466 int CastSocketImpl::DoAuthChallengeSend() { | 472 int CastSocketImpl::DoAuthChallengeSend() { |
| 467 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeSend"; | 473 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeSend"; |
| 468 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE); | 474 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE); |
| 469 | 475 |
| 470 CastMessage challenge_message; | 476 CastMessage challenge_message; |
| 471 CreateAuthChallengeMessage(&challenge_message, auth_context_); | 477 CreateAuthChallengeMessage(&challenge_message, auth_context_); |
| 472 VLOG_WITH_CONNECTION(1) << "Sending challenge: " | 478 VLOG_WITH_CONNECTION(1) << "Sending challenge: " |
| 473 << CastMessageToString(challenge_message); | 479 << CastMessageToString(challenge_message); |
| 474 | 480 |
| 475 ResetConnectLoopCallback(); | 481 ResetConnectLoopCallback(); |
| 476 transport_->SendMessage(challenge_message, connect_loop_callback_.callback()); | 482 transport_->SendMessage(challenge_message, connect_loop_callback_.callback()); |
| 477 | 483 |
| 478 // Always return IO_PENDING since the result is always asynchronous. | 484 // Always return IO_PENDING since the result is always asynchronous. |
| 479 return net::ERR_IO_PENDING; | 485 return net::ERR_IO_PENDING; |
| 480 } | 486 } |
| 481 | 487 |
| 482 int CastSocketImpl::DoAuthChallengeSendComplete(int result) { | 488 int CastSocketImpl::DoAuthChallengeSendComplete(int result) { |
| 483 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeSendComplete: " << result; | 489 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeSendComplete: " << result; |
| 484 if (result < 0) { | 490 if (result < 0) { |
| 485 SetConnectState(proto::CONN_STATE_ERROR); | 491 SetConnectState(proto::CONN_STATE_ERROR); |
| 486 SetErrorState(CHANNEL_ERROR_SOCKET_ERROR); | 492 SetErrorState(ChannelError::SOCKET_ERROR); |
| 487 logger_->LogSocketEventWithRv(channel_id_, | 493 logger_->LogSocketEventWithRv(channel_id_, |
| 488 proto::SEND_AUTH_CHALLENGE_FAILED, result); | 494 proto::SEND_AUTH_CHALLENGE_FAILED, result); |
| 489 return result; | 495 return result; |
| 490 } | 496 } |
| 491 transport_->Start(); | 497 transport_->Start(); |
| 492 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE); | 498 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE); |
| 493 return net::ERR_IO_PENDING; | 499 return net::ERR_IO_PENDING; |
| 494 } | 500 } |
| 495 | 501 |
| 496 CastSocketImpl::AuthTransportDelegate::AuthTransportDelegate( | 502 CastSocketImpl::AuthTransportDelegate::AuthTransportDelegate( |
| 497 CastSocketImpl* socket) | 503 CastSocketImpl* socket) |
| 498 : socket_(socket), error_state_(CHANNEL_ERROR_NONE) { | 504 : socket_(socket), error_state_(ChannelError::NONE) { |
| 499 DCHECK(socket); | 505 DCHECK(socket); |
| 500 } | 506 } |
| 501 | 507 |
| 502 ChannelError CastSocketImpl::AuthTransportDelegate::error_state() const { | 508 ChannelError CastSocketImpl::AuthTransportDelegate::error_state() const { |
| 503 return error_state_; | 509 return error_state_; |
| 504 } | 510 } |
| 505 | 511 |
| 506 LastErrors CastSocketImpl::AuthTransportDelegate::last_errors() const { | 512 LastErrors CastSocketImpl::AuthTransportDelegate::last_errors() const { |
| 507 return last_errors_; | 513 return last_errors_; |
| 508 } | 514 } |
| 509 | 515 |
| 510 void CastSocketImpl::AuthTransportDelegate::OnError(ChannelError error_state) { | 516 void CastSocketImpl::AuthTransportDelegate::OnError(ChannelError error_state) { |
| 511 error_state_ = error_state; | 517 error_state_ = error_state; |
| 512 socket_->PostTaskToStartConnectLoop(net::ERR_CONNECTION_FAILED); | 518 socket_->PostTaskToStartConnectLoop(net::ERR_CONNECTION_FAILED); |
| 513 } | 519 } |
| 514 | 520 |
| 515 void CastSocketImpl::AuthTransportDelegate::OnMessage( | 521 void CastSocketImpl::AuthTransportDelegate::OnMessage( |
| 516 const CastMessage& message) { | 522 const CastMessage& message) { |
| 517 if (!IsAuthMessage(message)) { | 523 if (!IsAuthMessage(message)) { |
| 518 error_state_ = CHANNEL_ERROR_TRANSPORT_ERROR; | 524 error_state_ = ChannelError::TRANSPORT_ERROR; |
| 519 socket_->PostTaskToStartConnectLoop(net::ERR_INVALID_RESPONSE); | 525 socket_->PostTaskToStartConnectLoop(net::ERR_INVALID_RESPONSE); |
| 520 } else { | 526 } else { |
| 521 socket_->challenge_reply_.reset(new CastMessage(message)); | 527 socket_->challenge_reply_.reset(new CastMessage(message)); |
| 522 socket_->PostTaskToStartConnectLoop(net::OK); | 528 socket_->PostTaskToStartConnectLoop(net::OK); |
| 523 } | 529 } |
| 524 } | 530 } |
| 525 | 531 |
| 526 void CastSocketImpl::AuthTransportDelegate::Start() { | 532 void CastSocketImpl::AuthTransportDelegate::Start() { |
| 527 } | 533 } |
| 528 | 534 |
| 529 int CastSocketImpl::DoAuthChallengeReplyComplete(int result) { | 535 int CastSocketImpl::DoAuthChallengeReplyComplete(int result) { |
| 530 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeReplyComplete: " << result; | 536 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeReplyComplete: " << result; |
| 531 | 537 |
| 532 if (auth_delegate_->error_state() != CHANNEL_ERROR_NONE) { | 538 if (auth_delegate_->error_state() != ChannelError::NONE) { |
| 533 SetErrorState(auth_delegate_->error_state()); | 539 SetErrorState(auth_delegate_->error_state()); |
| 534 SetConnectState(proto::CONN_STATE_ERROR); | 540 SetConnectState(proto::CONN_STATE_ERROR); |
| 535 return net::ERR_CONNECTION_FAILED; | 541 return net::ERR_CONNECTION_FAILED; |
| 536 } | 542 } |
| 537 auth_delegate_ = nullptr; | 543 auth_delegate_ = nullptr; |
| 538 | 544 |
| 539 if (result < 0) { | 545 if (result < 0) { |
| 540 SetConnectState(proto::CONN_STATE_ERROR); | 546 SetConnectState(proto::CONN_STATE_ERROR); |
| 541 return result; | 547 return result; |
| 542 } | 548 } |
| 543 | 549 |
| 544 if (!VerifyChallengeReply()) { | 550 if (!VerifyChallengeReply()) { |
| 545 SetErrorState(CHANNEL_ERROR_AUTHENTICATION_ERROR); | 551 SetErrorState(ChannelError::AUTHENTICATION_ERROR); |
| 546 SetConnectState(proto::CONN_STATE_ERROR); | 552 SetConnectState(proto::CONN_STATE_ERROR); |
| 547 return net::ERR_CONNECTION_FAILED; | 553 return net::ERR_CONNECTION_FAILED; |
| 548 } | 554 } |
| 549 VLOG_WITH_CONNECTION(1) << "Auth challenge verification succeeded"; | 555 VLOG_WITH_CONNECTION(1) << "Auth challenge verification succeeded"; |
| 550 | 556 |
| 551 SetConnectState(proto::CONN_STATE_FINISHED); | 557 SetConnectState(proto::CONN_STATE_FINISHED); |
| 552 return net::OK; | 558 return net::OK; |
| 553 } | 559 } |
| 554 | 560 |
| 555 void CastSocketImpl::DoConnectCallback() { | 561 void CastSocketImpl::DoConnectCallback() { |
| 556 VLOG(1) << "DoConnectCallback (error_state = " << error_state_ << ")"; | 562 VLOG(1) << "DoConnectCallback (error_state = " |
| 563 << ::cast_channel::ToString(error_state_) << ")"; |
| 557 if (connect_callback_.is_null()) { | 564 if (connect_callback_.is_null()) { |
| 558 DLOG(FATAL) << "Connection callback invoked multiple times."; | 565 DLOG(FATAL) << "Connection callback invoked multiple times."; |
| 559 return; | 566 return; |
| 560 } | 567 } |
| 561 | 568 |
| 562 if (error_state_ == CHANNEL_ERROR_NONE) { | 569 if (error_state_ == ChannelError::NONE) { |
| 563 SetReadyState(READY_STATE_OPEN); | 570 SetReadyState(ReadyState::OPEN); |
| 564 transport_->SetReadDelegate(std::move(delegate_)); | 571 transport_->SetReadDelegate(std::move(delegate_)); |
| 565 } else { | 572 } else { |
| 566 CloseInternal(); | 573 CloseInternal(); |
| 567 } | 574 } |
| 568 | 575 |
| 569 base::ResetAndReturn(&connect_callback_).Run(error_state_); | 576 base::ResetAndReturn(&connect_callback_).Run(error_state_); |
| 570 } | 577 } |
| 571 | 578 |
| 572 void CastSocketImpl::Close(const net::CompletionCallback& callback) { | 579 void CastSocketImpl::Close(const net::CompletionCallback& callback) { |
| 573 DCHECK(CalledOnValidThread()); | 580 DCHECK(CalledOnValidThread()); |
| 574 CloseInternal(); | 581 CloseInternal(); |
| 575 // Run this callback last. It may delete the socket. | 582 // Run this callback last. It may delete the socket. |
| 576 callback.Run(net::OK); | 583 callback.Run(net::OK); |
| 577 } | 584 } |
| 578 | 585 |
| 579 void CastSocketImpl::CloseInternal() { | 586 void CastSocketImpl::CloseInternal() { |
| 580 // TODO(mfoltz): Enforce this when CastChannelAPITest is rewritten to create | 587 // TODO(mfoltz): Enforce this when CastChannelAPITest is rewritten to create |
| 581 // and free sockets on the same thread. crbug.com/398242 | 588 // and free sockets on the same thread. crbug.com/398242 |
| 582 DCHECK(CalledOnValidThread()); | 589 DCHECK(CalledOnValidThread()); |
| 583 if (ready_state_ == READY_STATE_CLOSED) { | 590 if (ready_state_ == ReadyState::CLOSED) { |
| 584 return; | 591 return; |
| 585 } | 592 } |
| 586 | 593 |
| 587 VLOG_WITH_CONNECTION(1) << "Close ReadyState = " << ready_state_; | 594 VLOG_WITH_CONNECTION(1) << "Close ReadyState = " |
| 595 << ::cast_channel::ToString(ready_state_); |
| 588 transport_.reset(); | 596 transport_.reset(); |
| 589 tcp_socket_.reset(); | 597 tcp_socket_.reset(); |
| 590 socket_.reset(); | 598 socket_.reset(); |
| 591 transport_security_state_.reset(); | 599 transport_security_state_.reset(); |
| 592 if (GetTimer()) { | 600 if (GetTimer()) { |
| 593 GetTimer()->Stop(); | 601 GetTimer()->Stop(); |
| 594 } | 602 } |
| 595 | 603 |
| 596 // Cancel callbacks that we queued ourselves to re-enter the connect or read | 604 // Cancel callbacks that we queued ourselves to re-enter the connect or read |
| 597 // loops. | 605 // loops. |
| 598 connect_loop_callback_.Cancel(); | 606 connect_loop_callback_.Cancel(); |
| 599 connect_timeout_callback_.Cancel(); | 607 connect_timeout_callback_.Cancel(); |
| 600 SetReadyState(READY_STATE_CLOSED); | 608 SetReadyState(ReadyState::CLOSED); |
| 601 } | 609 } |
| 602 | 610 |
| 603 bool CastSocketImpl::CalledOnValidThread() const { | 611 bool CastSocketImpl::CalledOnValidThread() const { |
| 604 return thread_checker_.CalledOnValidThread(); | 612 return thread_checker_.CalledOnValidThread(); |
| 605 } | 613 } |
| 606 | 614 |
| 607 base::Timer* CastSocketImpl::GetTimer() { | 615 base::Timer* CastSocketImpl::GetTimer() { |
| 608 return connect_timeout_timer_.get(); | 616 return connect_timeout_timer_.get(); |
| 609 } | 617 } |
| 610 | 618 |
| 611 void CastSocketImpl::SetConnectState(proto::ConnectionState connect_state) { | 619 void CastSocketImpl::SetConnectState(proto::ConnectionState connect_state) { |
| 612 if (connect_state_ != connect_state) { | 620 if (connect_state_ != connect_state) { |
| 613 connect_state_ = connect_state; | 621 connect_state_ = connect_state; |
| 614 } | 622 } |
| 615 } | 623 } |
| 616 | 624 |
| 617 void CastSocketImpl::SetReadyState(ReadyState ready_state) { | 625 void CastSocketImpl::SetReadyState(ReadyState ready_state) { |
| 618 if (ready_state_ != ready_state) | 626 if (ready_state_ != ready_state) |
| 619 ready_state_ = ready_state; | 627 ready_state_ = ready_state; |
| 620 } | 628 } |
| 621 | 629 |
| 622 void CastSocketImpl::SetErrorState(ChannelError error_state) { | 630 void CastSocketImpl::SetErrorState(ChannelError error_state) { |
| 623 VLOG_WITH_CONNECTION(1) << "SetErrorState " << error_state; | 631 VLOG_WITH_CONNECTION(1) << "SetErrorState " |
| 624 DCHECK_EQ(CHANNEL_ERROR_NONE, error_state_); | 632 << ::cast_channel::ToString(error_state); |
| 633 DCHECK_EQ(ChannelError::NONE, error_state_); |
| 625 error_state_ = error_state; | 634 error_state_ = error_state; |
| 626 delegate_->OnError(error_state_); | 635 delegate_->OnError(error_state_); |
| 627 } | 636 } |
| 628 | 637 |
| 629 } // namespace cast_channel | 638 } // namespace cast_channel |
| 630 } // namespace api | 639 } // namespace api |
| 631 } // namespace extensions | 640 } // namespace extensions |
| 632 #undef VLOG_WITH_CONNECTION | 641 #undef VLOG_WITH_CONNECTION |
| OLD | NEW |