| 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 "components/cast_channel/cast_socket.h" | 5 #include "components/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 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 } | 233 } |
| 234 } | 234 } |
| 235 return result.success(); | 235 return result.success(); |
| 236 } | 236 } |
| 237 | 237 |
| 238 void CastSocketImpl::SetTransportForTesting( | 238 void CastSocketImpl::SetTransportForTesting( |
| 239 std::unique_ptr<CastTransport> transport) { | 239 std::unique_ptr<CastTransport> transport) { |
| 240 transport_ = std::move(transport); | 240 transport_ = std::move(transport); |
| 241 } | 241 } |
| 242 | 242 |
| 243 void CastSocketImpl::Connect(std::unique_ptr<CastTransport::Delegate> delegate, | 243 void CastSocketImpl::Connect(base::Callback<void(ChannelError)> callback) { |
| 244 base::Callback<void(ChannelError)> callback) { | |
| 245 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | 244 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 246 VLOG_WITH_CONNECTION(1) << "Connect readyState = " | 245 VLOG_WITH_CONNECTION(1) << "Connect readyState = " |
| 247 << ::cast_channel::ReadyStateToString(ready_state_); | 246 << ReadyStateToString(ready_state_); |
| 248 DCHECK_EQ(ConnectionState::START_CONNECT, connect_state_); | 247 DCHECK_EQ(ConnectionState::START_CONNECT, connect_state_); |
| 249 | 248 |
| 250 delegate_ = std::move(delegate); | 249 delegate_ = base::MakeUnique<CastSocketMessageDelegate>(this); |
| 251 | 250 |
| 252 if (ready_state_ != ReadyState::NONE) { | 251 if (ready_state_ != ReadyState::NONE) { |
| 253 callback.Run(ChannelError::CONNECT_ERROR); | 252 callback.Run(ChannelError::CONNECT_ERROR); |
| 254 return; | 253 return; |
| 255 } | 254 } |
| 256 | 255 |
| 257 connect_callback_ = callback; | 256 connect_callback_ = callback; |
| 258 SetReadyState(ReadyState::CONNECTING); | 257 SetReadyState(ReadyState::CONNECTING); |
| 259 SetConnectState(ConnectionState::TCP_CONNECT); | 258 SetConnectState(ConnectionState::TCP_CONNECT); |
| 260 | 259 |
| 261 // Set up connection timeout. | 260 // Set up connection timeout. |
| 262 if (connect_timeout_.InMicroseconds() > 0) { | 261 if (connect_timeout_.InMicroseconds() > 0) { |
| 263 DCHECK(connect_timeout_callback_.IsCancelled()); | 262 DCHECK(connect_timeout_callback_.IsCancelled()); |
| 264 connect_timeout_callback_.Reset( | 263 connect_timeout_callback_.Reset( |
| 265 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); | 264 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); |
| 266 GetTimer()->Start(FROM_HERE, connect_timeout_, | 265 GetTimer()->Start(FROM_HERE, connect_timeout_, |
| 267 connect_timeout_callback_.callback()); | 266 connect_timeout_callback_.callback()); |
| 268 } | 267 } |
| 269 | 268 |
| 270 DoConnectLoop(net::OK); | 269 DoConnectLoop(net::OK); |
| 271 } | 270 } |
| 272 | 271 |
| 273 CastTransport* CastSocketImpl::transport() const { | 272 CastTransport* CastSocketImpl::transport() const { |
| 274 return transport_.get(); | 273 return transport_.get(); |
| 275 } | 274 } |
| 276 | 275 |
| 276 void CastSocketImpl::AddObserver(Observer* observer) { |
| 277 DCHECK(observer); |
| 278 if (!observers_.HasObserver(observer)) |
| 279 observers_.AddObserver(observer); |
| 280 } |
| 281 |
| 277 void CastSocketImpl::OnConnectTimeout() { | 282 void CastSocketImpl::OnConnectTimeout() { |
| 278 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | 283 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 279 // Stop all pending connection setup tasks and report back to the client. | 284 // Stop all pending connection setup tasks and report back to the client. |
| 280 is_canceled_ = true; | 285 is_canceled_ = true; |
| 281 VLOG_WITH_CONNECTION(1) << "Timeout while establishing a connection."; | 286 VLOG_WITH_CONNECTION(1) << "Timeout while establishing a connection."; |
| 282 SetErrorState(ChannelError::CONNECT_TIMEOUT); | 287 SetErrorState(ChannelError::CONNECT_TIMEOUT); |
| 283 DoConnectCallback(); | 288 DoConnectCallback(); |
| 284 } | 289 } |
| 285 | 290 |
| 286 void CastSocketImpl::ResetConnectLoopCallback() { | 291 void CastSocketImpl::ResetConnectLoopCallback() { |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 return net::ERR_CONNECTION_FAILED; | 524 return net::ERR_CONNECTION_FAILED; |
| 520 } | 525 } |
| 521 VLOG_WITH_CONNECTION(1) << "Auth challenge verification succeeded"; | 526 VLOG_WITH_CONNECTION(1) << "Auth challenge verification succeeded"; |
| 522 | 527 |
| 523 SetConnectState(ConnectionState::FINISHED); | 528 SetConnectState(ConnectionState::FINISHED); |
| 524 return net::OK; | 529 return net::OK; |
| 525 } | 530 } |
| 526 | 531 |
| 527 void CastSocketImpl::DoConnectCallback() { | 532 void CastSocketImpl::DoConnectCallback() { |
| 528 VLOG(1) << "DoConnectCallback (error_state = " | 533 VLOG(1) << "DoConnectCallback (error_state = " |
| 529 << ::cast_channel::ChannelErrorToString(error_state_) << ")"; | 534 << ChannelErrorToString(error_state_) << ")"; |
| 530 if (connect_callback_.is_null()) { | 535 if (connect_callback_.is_null()) { |
| 531 DLOG(FATAL) << "Connection callback invoked multiple times."; | 536 DLOG(FATAL) << "Connection callback invoked multiple times."; |
| 532 return; | 537 return; |
| 533 } | 538 } |
| 534 | 539 |
| 535 if (error_state_ == ChannelError::NONE) { | 540 if (error_state_ == ChannelError::NONE) { |
| 536 SetReadyState(ReadyState::OPEN); | 541 SetReadyState(ReadyState::OPEN); |
| 537 if (keep_alive()) { | 542 if (keep_alive()) { |
| 538 auto* keep_alive_delegate = | 543 auto* keep_alive_delegate = |
| 539 new KeepAliveDelegate(this, logger_, std::move(delegate_), | 544 new KeepAliveDelegate(this, logger_, std::move(delegate_), |
| (...skipping 17 matching lines...) Expand all Loading... |
| 557 | 562 |
| 558 void CastSocketImpl::CloseInternal() { | 563 void CastSocketImpl::CloseInternal() { |
| 559 // TODO(mfoltz): Enforce this when CastChannelAPITest is rewritten to create | 564 // TODO(mfoltz): Enforce this when CastChannelAPITest is rewritten to create |
| 560 // and free sockets on the same thread. crbug.com/398242 | 565 // and free sockets on the same thread. crbug.com/398242 |
| 561 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | 566 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 562 if (ready_state_ == ReadyState::CLOSED) { | 567 if (ready_state_ == ReadyState::CLOSED) { |
| 563 return; | 568 return; |
| 564 } | 569 } |
| 565 | 570 |
| 566 VLOG_WITH_CONNECTION(1) << "Close ReadyState = " | 571 VLOG_WITH_CONNECTION(1) << "Close ReadyState = " |
| 567 << ::cast_channel::ReadyStateToString(ready_state_); | 572 << ReadyStateToString(ready_state_); |
| 568 transport_.reset(); | 573 transport_.reset(); |
| 569 tcp_socket_.reset(); | 574 tcp_socket_.reset(); |
| 570 socket_.reset(); | 575 socket_.reset(); |
| 571 transport_security_state_.reset(); | 576 transport_security_state_.reset(); |
| 572 if (GetTimer()) { | 577 if (GetTimer()) { |
| 573 GetTimer()->Stop(); | 578 GetTimer()->Stop(); |
| 574 } | 579 } |
| 575 | 580 |
| 576 // Cancel callbacks that we queued ourselves to re-enter the connect or read | 581 // Cancel callbacks that we queued ourselves to re-enter the connect or read |
| 577 // loops. | 582 // loops. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 590 } | 595 } |
| 591 } | 596 } |
| 592 | 597 |
| 593 void CastSocketImpl::SetReadyState(ReadyState ready_state) { | 598 void CastSocketImpl::SetReadyState(ReadyState ready_state) { |
| 594 if (ready_state_ != ready_state) | 599 if (ready_state_ != ready_state) |
| 595 ready_state_ = ready_state; | 600 ready_state_ = ready_state; |
| 596 } | 601 } |
| 597 | 602 |
| 598 void CastSocketImpl::SetErrorState(ChannelError error_state) { | 603 void CastSocketImpl::SetErrorState(ChannelError error_state) { |
| 599 VLOG_WITH_CONNECTION(1) << "SetErrorState " | 604 VLOG_WITH_CONNECTION(1) << "SetErrorState " |
| 600 << ::cast_channel::ChannelErrorToString(error_state); | 605 << ChannelErrorToString(error_state); |
| 601 DCHECK_EQ(ChannelError::NONE, error_state_); | 606 DCHECK_EQ(ChannelError::NONE, error_state_); |
| 602 error_state_ = error_state; | 607 error_state_ = error_state; |
| 603 delegate_->OnError(error_state_); | 608 delegate_->OnError(error_state_); |
| 604 } | 609 } |
| 605 | 610 |
| 611 CastSocketImpl::CastSocketMessageDelegate::CastSocketMessageDelegate( |
| 612 CastSocketImpl* socket) |
| 613 : socket_(socket) { |
| 614 DCHECK(socket_); |
| 615 } |
| 616 |
| 617 CastSocketImpl::CastSocketMessageDelegate::~CastSocketMessageDelegate() {} |
| 618 |
| 619 // CastTransport::Delegate implementation. |
| 620 void CastSocketImpl::CastSocketMessageDelegate::OnError( |
| 621 ChannelError error_state) { |
| 622 for (auto& observer : socket_->observers_) |
| 623 observer.OnError(*socket_, error_state); |
| 624 } |
| 625 |
| 626 void CastSocketImpl::CastSocketMessageDelegate::OnMessage( |
| 627 const CastMessage& message) { |
| 628 for (auto& observer : socket_->observers_) |
| 629 observer.OnMessage(*socket_, message); |
| 630 } |
| 631 |
| 632 void CastSocketImpl::CastSocketMessageDelegate::Start() {} |
| 633 |
| 606 } // namespace cast_channel | 634 } // namespace cast_channel |
| 607 #undef VLOG_WITH_CONNECTION | 635 #undef VLOG_WITH_CONNECTION |
| OLD | NEW |