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