OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "extensions/browser/api/cast_channel/cast_socket.h" |
| 6 |
| 7 #include <stdlib.h> |
| 8 #include <string.h> |
| 9 |
| 10 #include <utility> |
| 11 |
| 12 #include "base/bind.h" |
| 13 #include "base/callback_helpers.h" |
| 14 #include "base/format_macros.h" |
| 15 #include "base/lazy_instance.h" |
| 16 #include "base/location.h" |
| 17 #include "base/memory/ptr_util.h" |
| 18 #include "base/numerics/safe_conversions.h" |
| 19 #include "base/single_thread_task_runner.h" |
| 20 #include "base/strings/string_number_conversions.h" |
| 21 #include "base/strings/stringprintf.h" |
| 22 #include "base/sys_byteorder.h" |
| 23 #include "base/threading/thread_task_runner_handle.h" |
| 24 #include "base/time/time.h" |
| 25 #include "extensions/browser/api/cast_channel/cast_auth_util.h" |
| 26 #include "extensions/browser/api/cast_channel/cast_framer.h" |
| 27 #include "extensions/browser/api/cast_channel/cast_message_util.h" |
| 28 #include "extensions/browser/api/cast_channel/cast_transport.h" |
| 29 #include "extensions/browser/api/cast_channel/logger.h" |
| 30 #include "extensions/common/api/cast_channel/cast_channel.pb.h" |
| 31 #include "net/base/address_list.h" |
| 32 #include "net/base/host_port_pair.h" |
| 33 #include "net/base/net_errors.h" |
| 34 #include "net/cert/cert_verifier.h" |
| 35 #include "net/cert/cert_verify_result.h" |
| 36 #include "net/cert/ct_policy_enforcer.h" |
| 37 #include "net/cert/multi_log_ct_verifier.h" |
| 38 #include "net/cert/x509_certificate.h" |
| 39 #include "net/http/transport_security_state.h" |
| 40 #include "net/log/net_log.h" |
| 41 #include "net/log/net_log_source_type.h" |
| 42 #include "net/socket/client_socket_factory.h" |
| 43 #include "net/socket/client_socket_handle.h" |
| 44 #include "net/socket/ssl_client_socket.h" |
| 45 #include "net/socket/stream_socket.h" |
| 46 #include "net/socket/tcp_client_socket.h" |
| 47 #include "net/ssl/ssl_config_service.h" |
| 48 #include "net/ssl/ssl_info.h" |
| 49 |
| 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 |
| 52 // type ChannelAuthType are available in the current scope. |
| 53 #define CONNECTION_INFO() \ |
| 54 "[" << ip_endpoint_.ToString() \ |
| 55 << ", auth=" << ::cast_channel::ChannelAuthTypeToString(channel_auth_) \ |
| 56 << "] " |
| 57 #define VLOG_WITH_CONNECTION(level) VLOG(level) << CONNECTION_INFO() |
| 58 #define LOG_WITH_CONNECTION(level) LOG(level) << CONNECTION_INFO() |
| 59 |
| 60 namespace extensions { |
| 61 namespace api { |
| 62 namespace cast_channel { |
| 63 namespace { |
| 64 |
| 65 bool IsTerminalState(proto::ConnectionState state) { |
| 66 return state == proto::CONN_STATE_FINISHED || |
| 67 state == proto::CONN_STATE_ERROR || state == proto::CONN_STATE_TIMEOUT; |
| 68 } |
| 69 |
| 70 // Cert verifier which blindly accepts all certificates, regardless of validity. |
| 71 class FakeCertVerifier : public net::CertVerifier { |
| 72 public: |
| 73 FakeCertVerifier() {} |
| 74 ~FakeCertVerifier() override {} |
| 75 |
| 76 int Verify(const RequestParams& params, |
| 77 net::CRLSet*, |
| 78 net::CertVerifyResult* verify_result, |
| 79 const net::CompletionCallback&, |
| 80 std::unique_ptr<Request>*, |
| 81 const net::NetLogWithSource&) override { |
| 82 verify_result->Reset(); |
| 83 verify_result->verified_cert = params.certificate(); |
| 84 return net::OK; |
| 85 } |
| 86 }; |
| 87 |
| 88 } // namespace |
| 89 |
| 90 using ChannelError = ::cast_channel::ChannelError; |
| 91 using ChannelAuthType = ::cast_channel::ChannelAuthType; |
| 92 using ReadyState = ::cast_channel::ReadyState; |
| 93 |
| 94 CastSocketImpl::CastSocketImpl(const std::string& owner_extension_id, |
| 95 const net::IPEndPoint& ip_endpoint, |
| 96 ChannelAuthType channel_auth, |
| 97 net::NetLog* net_log, |
| 98 const base::TimeDelta& timeout, |
| 99 bool keep_alive, |
| 100 const scoped_refptr<Logger>& logger, |
| 101 uint64_t device_capabilities) |
| 102 : CastSocketImpl(owner_extension_id, |
| 103 ip_endpoint, |
| 104 channel_auth, |
| 105 net_log, |
| 106 timeout, |
| 107 keep_alive, |
| 108 logger, |
| 109 device_capabilities, |
| 110 AuthContext::Create()) {} |
| 111 |
| 112 CastSocketImpl::CastSocketImpl(const std::string& owner_extension_id, |
| 113 const net::IPEndPoint& ip_endpoint, |
| 114 ChannelAuthType channel_auth, |
| 115 net::NetLog* net_log, |
| 116 const base::TimeDelta& timeout, |
| 117 bool keep_alive, |
| 118 const scoped_refptr<Logger>& logger, |
| 119 uint64_t device_capabilities, |
| 120 const AuthContext& auth_context) |
| 121 : channel_id_(0), |
| 122 ip_endpoint_(ip_endpoint), |
| 123 channel_auth_(channel_auth), |
| 124 net_log_(net_log), |
| 125 keep_alive_(keep_alive), |
| 126 logger_(logger), |
| 127 auth_context_(auth_context), |
| 128 connect_timeout_(timeout), |
| 129 connect_timeout_timer_(new base::OneShotTimer), |
| 130 is_canceled_(false), |
| 131 device_capabilities_(device_capabilities), |
| 132 audio_only_(false), |
| 133 connect_state_(proto::CONN_STATE_START_CONNECT), |
| 134 error_state_(ChannelError::NONE), |
| 135 ready_state_(ReadyState::NONE), |
| 136 auth_delegate_(nullptr) { |
| 137 DCHECK(net_log_); |
| 138 net_log_source_.type = net::NetLogSourceType::SOCKET; |
| 139 net_log_source_.id = net_log_->NextID(); |
| 140 } |
| 141 |
| 142 CastSocketImpl::~CastSocketImpl() { |
| 143 // Ensure that resources are freed but do not run pending callbacks that |
| 144 // would result in re-entrancy. |
| 145 CloseInternal(); |
| 146 |
| 147 if (!connect_callback_.is_null()) |
| 148 base::ResetAndReturn(&connect_callback_).Run(ChannelError::UNKNOWN); |
| 149 } |
| 150 |
| 151 ReadyState CastSocketImpl::ready_state() const { |
| 152 return ready_state_; |
| 153 } |
| 154 |
| 155 ChannelError CastSocketImpl::error_state() const { |
| 156 return error_state_; |
| 157 } |
| 158 |
| 159 const net::IPEndPoint& CastSocketImpl::ip_endpoint() const { |
| 160 return ip_endpoint_; |
| 161 } |
| 162 |
| 163 int CastSocketImpl::id() const { |
| 164 return channel_id_; |
| 165 } |
| 166 |
| 167 void CastSocketImpl::set_id(int id) { |
| 168 channel_id_ = id; |
| 169 } |
| 170 |
| 171 ChannelAuthType CastSocketImpl::channel_auth() const { |
| 172 return channel_auth_; |
| 173 } |
| 174 |
| 175 bool CastSocketImpl::keep_alive() const { |
| 176 return keep_alive_; |
| 177 } |
| 178 |
| 179 bool CastSocketImpl::audio_only() const { |
| 180 return audio_only_; |
| 181 } |
| 182 |
| 183 std::unique_ptr<net::TCPClientSocket> CastSocketImpl::CreateTcpSocket() { |
| 184 net::AddressList addresses(ip_endpoint_); |
| 185 return std::unique_ptr<net::TCPClientSocket>( |
| 186 new net::TCPClientSocket(addresses, nullptr, net_log_, net_log_source_)); |
| 187 // Options cannot be set on the TCPClientSocket yet, because the |
| 188 // underlying platform socket will not be created until Bind() |
| 189 // or Connect() is called. |
| 190 } |
| 191 |
| 192 std::unique_ptr<net::SSLClientSocket> CastSocketImpl::CreateSslSocket( |
| 193 std::unique_ptr<net::StreamSocket> socket) { |
| 194 net::SSLConfig ssl_config; |
| 195 cert_verifier_ = base::WrapUnique(new FakeCertVerifier); |
| 196 transport_security_state_.reset(new net::TransportSecurityState); |
| 197 cert_transparency_verifier_.reset(new net::MultiLogCTVerifier()); |
| 198 ct_policy_enforcer_.reset(new net::CTPolicyEnforcer()); |
| 199 |
| 200 // Note that |context| fields remain owned by CastSocketImpl. |
| 201 net::SSLClientSocketContext context; |
| 202 context.cert_verifier = cert_verifier_.get(); |
| 203 context.transport_security_state = transport_security_state_.get(); |
| 204 context.cert_transparency_verifier = cert_transparency_verifier_.get(); |
| 205 context.ct_policy_enforcer = ct_policy_enforcer_.get(); |
| 206 |
| 207 std::unique_ptr<net::ClientSocketHandle> connection( |
| 208 new net::ClientSocketHandle); |
| 209 connection->SetSocket(std::move(socket)); |
| 210 net::HostPortPair host_and_port = net::HostPortPair::FromIPEndPoint( |
| 211 ip_endpoint_); |
| 212 |
| 213 return net::ClientSocketFactory::GetDefaultFactory()->CreateSSLClientSocket( |
| 214 std::move(connection), host_and_port, ssl_config, context); |
| 215 } |
| 216 |
| 217 scoped_refptr<net::X509Certificate> CastSocketImpl::ExtractPeerCert() { |
| 218 net::SSLInfo ssl_info; |
| 219 if (!socket_->GetSSLInfo(&ssl_info) || !ssl_info.cert.get()) |
| 220 return nullptr; |
| 221 |
| 222 return ssl_info.cert; |
| 223 } |
| 224 |
| 225 bool CastSocketImpl::VerifyChannelPolicy(const AuthResult& result) { |
| 226 audio_only_ = (result.channel_policies & AuthResult::POLICY_AUDIO_ONLY) != 0; |
| 227 if (audio_only_ && |
| 228 (device_capabilities_ & CastDeviceCapability::VIDEO_OUT) != 0) { |
| 229 LOG_WITH_CONNECTION(ERROR) |
| 230 << "Audio only channel policy enforced for video out capable device"; |
| 231 return false; |
| 232 } |
| 233 return true; |
| 234 } |
| 235 |
| 236 bool CastSocketImpl::VerifyChallengeReply() { |
| 237 DCHECK(peer_cert_); |
| 238 AuthResult result = |
| 239 AuthenticateChallengeReply(*challenge_reply_, *peer_cert_, auth_context_); |
| 240 logger_->LogSocketChallengeReplyEvent(channel_id_, result); |
| 241 if (result.success()) { |
| 242 VLOG(1) << result.error_message; |
| 243 if (!VerifyChannelPolicy(result)) { |
| 244 return false; |
| 245 } |
| 246 } |
| 247 return result.success(); |
| 248 } |
| 249 |
| 250 void CastSocketImpl::SetTransportForTesting( |
| 251 std::unique_ptr<CastTransport> transport) { |
| 252 transport_ = std::move(transport); |
| 253 } |
| 254 |
| 255 void CastSocketImpl::Connect(std::unique_ptr<CastTransport::Delegate> delegate, |
| 256 base::Callback<void(ChannelError)> callback) { |
| 257 DCHECK(CalledOnValidThread()); |
| 258 VLOG_WITH_CONNECTION(1) << "Connect readyState = " |
| 259 << ::cast_channel::ReadyStateToString(ready_state_); |
| 260 DCHECK_EQ(proto::CONN_STATE_START_CONNECT, connect_state_); |
| 261 |
| 262 delegate_ = std::move(delegate); |
| 263 |
| 264 if (ready_state_ != ReadyState::NONE) { |
| 265 callback.Run(ChannelError::CONNECT_ERROR); |
| 266 return; |
| 267 } |
| 268 |
| 269 connect_callback_ = callback; |
| 270 SetReadyState(ReadyState::CONNECTING); |
| 271 SetConnectState(proto::CONN_STATE_TCP_CONNECT); |
| 272 |
| 273 // Set up connection timeout. |
| 274 if (connect_timeout_.InMicroseconds() > 0) { |
| 275 DCHECK(connect_timeout_callback_.IsCancelled()); |
| 276 connect_timeout_callback_.Reset( |
| 277 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); |
| 278 GetTimer()->Start(FROM_HERE, |
| 279 connect_timeout_, |
| 280 connect_timeout_callback_.callback()); |
| 281 } |
| 282 |
| 283 DoConnectLoop(net::OK); |
| 284 } |
| 285 |
| 286 CastTransport* CastSocketImpl::transport() const { |
| 287 return transport_.get(); |
| 288 } |
| 289 |
| 290 void CastSocketImpl::OnConnectTimeout() { |
| 291 DCHECK(CalledOnValidThread()); |
| 292 // Stop all pending connection setup tasks and report back to the client. |
| 293 is_canceled_ = true; |
| 294 VLOG_WITH_CONNECTION(1) << "Timeout while establishing a connection."; |
| 295 SetErrorState(ChannelError::CONNECT_TIMEOUT); |
| 296 DoConnectCallback(); |
| 297 } |
| 298 |
| 299 void CastSocketImpl::ResetConnectLoopCallback() { |
| 300 DCHECK(connect_loop_callback_.IsCancelled()); |
| 301 connect_loop_callback_.Reset( |
| 302 base::Bind(&CastSocketImpl::DoConnectLoop, base::Unretained(this))); |
| 303 } |
| 304 |
| 305 void CastSocketImpl::PostTaskToStartConnectLoop(int result) { |
| 306 DCHECK(CalledOnValidThread()); |
| 307 |
| 308 ResetConnectLoopCallback(); |
| 309 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 310 FROM_HERE, base::Bind(connect_loop_callback_.callback(), result)); |
| 311 } |
| 312 |
| 313 // This method performs the state machine transitions for connection flow. |
| 314 // There are two entry points to this method: |
| 315 // 1. Connect method: this starts the flow |
| 316 // 2. Callback from network operations that finish asynchronously. |
| 317 void CastSocketImpl::DoConnectLoop(int result) { |
| 318 connect_loop_callback_.Cancel(); |
| 319 if (is_canceled_) { |
| 320 LOG_WITH_CONNECTION(ERROR) << "CANCELLED - Aborting DoConnectLoop."; |
| 321 return; |
| 322 } |
| 323 |
| 324 // Network operations can either finish synchronously or asynchronously. |
| 325 // This method executes the state machine transitions in a loop so that |
| 326 // correct state transitions happen even when network operations finish |
| 327 // synchronously. |
| 328 int rv = result; |
| 329 do { |
| 330 proto::ConnectionState state = connect_state_; |
| 331 connect_state_ = proto::CONN_STATE_UNKNOWN; |
| 332 switch (state) { |
| 333 case proto::CONN_STATE_TCP_CONNECT: |
| 334 rv = DoTcpConnect(); |
| 335 break; |
| 336 case proto::CONN_STATE_TCP_CONNECT_COMPLETE: |
| 337 rv = DoTcpConnectComplete(rv); |
| 338 break; |
| 339 case proto::CONN_STATE_SSL_CONNECT: |
| 340 DCHECK_EQ(net::OK, rv); |
| 341 rv = DoSslConnect(); |
| 342 break; |
| 343 case proto::CONN_STATE_SSL_CONNECT_COMPLETE: |
| 344 rv = DoSslConnectComplete(rv); |
| 345 break; |
| 346 case proto::CONN_STATE_AUTH_CHALLENGE_SEND: |
| 347 rv = DoAuthChallengeSend(); |
| 348 break; |
| 349 case proto::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE: |
| 350 rv = DoAuthChallengeSendComplete(rv); |
| 351 break; |
| 352 case proto::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE: |
| 353 rv = DoAuthChallengeReplyComplete(rv); |
| 354 DCHECK(IsTerminalState(connect_state_)); |
| 355 break; |
| 356 default: |
| 357 NOTREACHED() << "Unknown state in connect flow: " << state; |
| 358 SetConnectState(proto::CONN_STATE_FINISHED); |
| 359 SetErrorState(ChannelError::UNKNOWN); |
| 360 DoConnectCallback(); |
| 361 return; |
| 362 } |
| 363 } while (rv != net::ERR_IO_PENDING && !IsTerminalState(connect_state_)); |
| 364 // Exit the state machine if an asynchronous network operation is pending |
| 365 // or if the state machine is in the terminal "finished" state. |
| 366 |
| 367 if (IsTerminalState(connect_state_)) { |
| 368 DCHECK_NE(rv, net::ERR_IO_PENDING); |
| 369 GetTimer()->Stop(); |
| 370 DoConnectCallback(); |
| 371 } else { |
| 372 DCHECK_EQ(rv, net::ERR_IO_PENDING); |
| 373 } |
| 374 } |
| 375 |
| 376 int CastSocketImpl::DoTcpConnect() { |
| 377 DCHECK(connect_loop_callback_.IsCancelled()); |
| 378 VLOG_WITH_CONNECTION(1) << "DoTcpConnect"; |
| 379 SetConnectState(proto::CONN_STATE_TCP_CONNECT_COMPLETE); |
| 380 tcp_socket_ = CreateTcpSocket(); |
| 381 |
| 382 int rv = tcp_socket_->Connect( |
| 383 base::Bind(&CastSocketImpl::DoConnectLoop, base::Unretained(this))); |
| 384 logger_->LogSocketEventWithRv(channel_id_, proto::TCP_SOCKET_CONNECT, rv); |
| 385 return rv; |
| 386 } |
| 387 |
| 388 int CastSocketImpl::DoTcpConnectComplete(int connect_result) { |
| 389 VLOG_WITH_CONNECTION(1) << "DoTcpConnectComplete: " << connect_result; |
| 390 logger_->LogSocketEventWithRv(channel_id_, proto::TCP_SOCKET_CONNECT_COMPLETE, |
| 391 connect_result); |
| 392 if (connect_result == net::OK) { |
| 393 SetConnectState(proto::CONN_STATE_SSL_CONNECT); |
| 394 } else if (connect_result == net::ERR_CONNECTION_TIMED_OUT) { |
| 395 SetConnectState(proto::CONN_STATE_FINISHED); |
| 396 SetErrorState(ChannelError::CONNECT_TIMEOUT); |
| 397 } else { |
| 398 SetConnectState(proto::CONN_STATE_FINISHED); |
| 399 SetErrorState(ChannelError::CONNECT_ERROR); |
| 400 } |
| 401 return connect_result; |
| 402 } |
| 403 |
| 404 int CastSocketImpl::DoSslConnect() { |
| 405 DCHECK(connect_loop_callback_.IsCancelled()); |
| 406 VLOG_WITH_CONNECTION(1) << "DoSslConnect"; |
| 407 SetConnectState(proto::CONN_STATE_SSL_CONNECT_COMPLETE); |
| 408 socket_ = CreateSslSocket(std::move(tcp_socket_)); |
| 409 |
| 410 int rv = socket_->Connect( |
| 411 base::Bind(&CastSocketImpl::DoConnectLoop, base::Unretained(this))); |
| 412 logger_->LogSocketEventWithRv(channel_id_, proto::SSL_SOCKET_CONNECT, rv); |
| 413 return rv; |
| 414 } |
| 415 |
| 416 int CastSocketImpl::DoSslConnectComplete(int result) { |
| 417 logger_->LogSocketEventWithRv(channel_id_, proto::SSL_SOCKET_CONNECT_COMPLETE, |
| 418 result); |
| 419 VLOG_WITH_CONNECTION(1) << "DoSslConnectComplete: " << result; |
| 420 if (result == net::OK) { |
| 421 peer_cert_ = ExtractPeerCert(); |
| 422 |
| 423 if (!peer_cert_) { |
| 424 LOG_WITH_CONNECTION(WARNING) << "Could not extract peer cert."; |
| 425 SetConnectState(proto::CONN_STATE_FINISHED); |
| 426 SetErrorState(ChannelError::AUTHENTICATION_ERROR); |
| 427 return net::ERR_CERT_INVALID; |
| 428 } |
| 429 |
| 430 // SSL connection succeeded. |
| 431 if (!transport_.get()) { |
| 432 // Create a channel transport if one wasn't already set (e.g. by test |
| 433 // code). |
| 434 transport_.reset(new CastTransportImpl(this->socket_.get(), channel_id_, |
| 435 ip_endpoint_, channel_auth_, |
| 436 logger_)); |
| 437 } |
| 438 auth_delegate_ = new AuthTransportDelegate(this); |
| 439 transport_->SetReadDelegate(base::WrapUnique(auth_delegate_)); |
| 440 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_SEND); |
| 441 } else if (result == net::ERR_CONNECTION_TIMED_OUT) { |
| 442 SetConnectState(proto::CONN_STATE_FINISHED); |
| 443 SetErrorState(ChannelError::CONNECT_TIMEOUT); |
| 444 } else { |
| 445 SetConnectState(proto::CONN_STATE_FINISHED); |
| 446 SetErrorState(ChannelError::AUTHENTICATION_ERROR); |
| 447 } |
| 448 return result; |
| 449 } |
| 450 |
| 451 int CastSocketImpl::DoAuthChallengeSend() { |
| 452 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeSend"; |
| 453 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE); |
| 454 |
| 455 CastMessage challenge_message; |
| 456 CreateAuthChallengeMessage(&challenge_message, auth_context_); |
| 457 VLOG_WITH_CONNECTION(1) << "Sending challenge: " |
| 458 << CastMessageToString(challenge_message); |
| 459 |
| 460 ResetConnectLoopCallback(); |
| 461 transport_->SendMessage(challenge_message, connect_loop_callback_.callback()); |
| 462 |
| 463 // Always return IO_PENDING since the result is always asynchronous. |
| 464 return net::ERR_IO_PENDING; |
| 465 } |
| 466 |
| 467 int CastSocketImpl::DoAuthChallengeSendComplete(int result) { |
| 468 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeSendComplete: " << result; |
| 469 if (result < 0) { |
| 470 SetConnectState(proto::CONN_STATE_ERROR); |
| 471 SetErrorState(ChannelError::CAST_SOCKET_ERROR); |
| 472 logger_->LogSocketEventWithRv(channel_id_, |
| 473 proto::SEND_AUTH_CHALLENGE_FAILED, result); |
| 474 return result; |
| 475 } |
| 476 transport_->Start(); |
| 477 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE); |
| 478 return net::ERR_IO_PENDING; |
| 479 } |
| 480 |
| 481 CastSocketImpl::AuthTransportDelegate::AuthTransportDelegate( |
| 482 CastSocketImpl* socket) |
| 483 : socket_(socket), error_state_(ChannelError::NONE) { |
| 484 DCHECK(socket); |
| 485 } |
| 486 |
| 487 ChannelError CastSocketImpl::AuthTransportDelegate::error_state() const { |
| 488 return error_state_; |
| 489 } |
| 490 |
| 491 LastErrors CastSocketImpl::AuthTransportDelegate::last_errors() const { |
| 492 return last_errors_; |
| 493 } |
| 494 |
| 495 void CastSocketImpl::AuthTransportDelegate::OnError(ChannelError error_state) { |
| 496 error_state_ = error_state; |
| 497 socket_->PostTaskToStartConnectLoop(net::ERR_CONNECTION_FAILED); |
| 498 } |
| 499 |
| 500 void CastSocketImpl::AuthTransportDelegate::OnMessage( |
| 501 const CastMessage& message) { |
| 502 if (!IsAuthMessage(message)) { |
| 503 error_state_ = ChannelError::TRANSPORT_ERROR; |
| 504 socket_->PostTaskToStartConnectLoop(net::ERR_INVALID_RESPONSE); |
| 505 } else { |
| 506 socket_->challenge_reply_.reset(new CastMessage(message)); |
| 507 socket_->PostTaskToStartConnectLoop(net::OK); |
| 508 } |
| 509 } |
| 510 |
| 511 void CastSocketImpl::AuthTransportDelegate::Start() { |
| 512 } |
| 513 |
| 514 int CastSocketImpl::DoAuthChallengeReplyComplete(int result) { |
| 515 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeReplyComplete: " << result; |
| 516 |
| 517 if (auth_delegate_->error_state() != ChannelError::NONE) { |
| 518 SetErrorState(auth_delegate_->error_state()); |
| 519 SetConnectState(proto::CONN_STATE_ERROR); |
| 520 return net::ERR_CONNECTION_FAILED; |
| 521 } |
| 522 auth_delegate_ = nullptr; |
| 523 |
| 524 if (result < 0) { |
| 525 SetConnectState(proto::CONN_STATE_ERROR); |
| 526 return result; |
| 527 } |
| 528 |
| 529 if (!VerifyChallengeReply()) { |
| 530 SetErrorState(ChannelError::AUTHENTICATION_ERROR); |
| 531 SetConnectState(proto::CONN_STATE_ERROR); |
| 532 return net::ERR_CONNECTION_FAILED; |
| 533 } |
| 534 VLOG_WITH_CONNECTION(1) << "Auth challenge verification succeeded"; |
| 535 |
| 536 SetConnectState(proto::CONN_STATE_FINISHED); |
| 537 return net::OK; |
| 538 } |
| 539 |
| 540 void CastSocketImpl::DoConnectCallback() { |
| 541 VLOG(1) << "DoConnectCallback (error_state = " |
| 542 << ::cast_channel::ChannelErrorToString(error_state_) << ")"; |
| 543 if (connect_callback_.is_null()) { |
| 544 DLOG(FATAL) << "Connection callback invoked multiple times."; |
| 545 return; |
| 546 } |
| 547 |
| 548 if (error_state_ == ChannelError::NONE) { |
| 549 SetReadyState(ReadyState::OPEN); |
| 550 transport_->SetReadDelegate(std::move(delegate_)); |
| 551 } else { |
| 552 CloseInternal(); |
| 553 } |
| 554 |
| 555 base::ResetAndReturn(&connect_callback_).Run(error_state_); |
| 556 } |
| 557 |
| 558 void CastSocketImpl::Close(const net::CompletionCallback& callback) { |
| 559 DCHECK(CalledOnValidThread()); |
| 560 CloseInternal(); |
| 561 // Run this callback last. It may delete the socket. |
| 562 callback.Run(net::OK); |
| 563 } |
| 564 |
| 565 void CastSocketImpl::CloseInternal() { |
| 566 // TODO(mfoltz): Enforce this when CastChannelAPITest is rewritten to create |
| 567 // and free sockets on the same thread. crbug.com/398242 |
| 568 DCHECK(CalledOnValidThread()); |
| 569 if (ready_state_ == ReadyState::CLOSED) { |
| 570 return; |
| 571 } |
| 572 |
| 573 VLOG_WITH_CONNECTION(1) << "Close ReadyState = " |
| 574 << ::cast_channel::ReadyStateToString(ready_state_); |
| 575 transport_.reset(); |
| 576 tcp_socket_.reset(); |
| 577 socket_.reset(); |
| 578 transport_security_state_.reset(); |
| 579 if (GetTimer()) { |
| 580 GetTimer()->Stop(); |
| 581 } |
| 582 |
| 583 // Cancel callbacks that we queued ourselves to re-enter the connect or read |
| 584 // loops. |
| 585 connect_loop_callback_.Cancel(); |
| 586 connect_timeout_callback_.Cancel(); |
| 587 SetReadyState(ReadyState::CLOSED); |
| 588 } |
| 589 |
| 590 bool CastSocketImpl::CalledOnValidThread() const { |
| 591 return thread_checker_.CalledOnValidThread(); |
| 592 } |
| 593 |
| 594 base::Timer* CastSocketImpl::GetTimer() { |
| 595 return connect_timeout_timer_.get(); |
| 596 } |
| 597 |
| 598 void CastSocketImpl::SetConnectState(proto::ConnectionState connect_state) { |
| 599 if (connect_state_ != connect_state) { |
| 600 connect_state_ = connect_state; |
| 601 } |
| 602 } |
| 603 |
| 604 void CastSocketImpl::SetReadyState(ReadyState ready_state) { |
| 605 if (ready_state_ != ready_state) |
| 606 ready_state_ = ready_state; |
| 607 } |
| 608 |
| 609 void CastSocketImpl::SetErrorState(ChannelError error_state) { |
| 610 VLOG_WITH_CONNECTION(1) << "SetErrorState " |
| 611 << ::cast_channel::ChannelErrorToString(error_state); |
| 612 DCHECK_EQ(ChannelError::NONE, error_state_); |
| 613 error_state_ = error_state; |
| 614 delegate_->OnError(error_state_); |
| 615 } |
| 616 |
| 617 } // namespace cast_channel |
| 618 } // namespace api |
| 619 } // namespace extensions |
| 620 #undef VLOG_WITH_CONNECTION |
OLD | NEW |