Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Side by Side Diff: components/cast_channel/cast_socket.cc

Issue 2925053005: [cast_channel] Implement CastSocketService::OpenSocket() (Closed)
Patch Set: resolve code review comments from Mark Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 DCHECK(net_log_); 129 DCHECK(net_log_);
130 net_log_source_.type = net::NetLogSourceType::SOCKET; 130 net_log_source_.type = net::NetLogSourceType::SOCKET;
131 net_log_source_.id = net_log_->NextID(); 131 net_log_source_.id = net_log_->NextID();
132 } 132 }
133 133
134 CastSocketImpl::~CastSocketImpl() { 134 CastSocketImpl::~CastSocketImpl() {
135 // Ensure that resources are freed but do not run pending callbacks that 135 // Ensure that resources are freed but do not run pending callbacks that
136 // would result in re-entrancy. 136 // would result in re-entrancy.
137 CloseInternal(); 137 CloseInternal();
138 138
139 if (!connect_callback_.is_null()) 139 for (auto& connect_callback : connect_callbacks_)
140 base::ResetAndReturn(&connect_callback_).Run(ChannelError::UNKNOWN); 140 connect_callback.Run(channel_id_, ChannelError::UNKNOWN);
141 connect_callbacks_.clear();
141 } 142 }
142 143
143 ReadyState CastSocketImpl::ready_state() const { 144 ReadyState CastSocketImpl::ready_state() const {
144 return ready_state_; 145 return ready_state_;
145 } 146 }
146 147
147 ChannelError CastSocketImpl::error_state() const { 148 ChannelError CastSocketImpl::error_state() const {
148 return error_state_; 149 return error_state_;
149 } 150 }
150 151
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 } 234 }
234 } 235 }
235 return result.success(); 236 return result.success();
236 } 237 }
237 238
238 void CastSocketImpl::SetTransportForTesting( 239 void CastSocketImpl::SetTransportForTesting(
239 std::unique_ptr<CastTransport> transport) { 240 std::unique_ptr<CastTransport> transport) {
240 transport_ = std::move(transport); 241 transport_ = std::move(transport);
241 } 242 }
242 243
243 void CastSocketImpl::Connect(base::Callback<void(ChannelError)> callback) { 244 void CastSocketImpl::Connect(const OnOpenCallback& callback) {
245 switch (ready_state_) {
246 case ReadyState::NONE:
247 connect_callbacks_.push_back(callback);
248 Connect();
249 break;
250 case ReadyState::CONNECTING:
251 connect_callbacks_.push_back(callback);
252 break;
253 case ReadyState::OPEN:
254 callback.Run(channel_id_, ChannelError::NONE);
255 break;
256 case ReadyState::CLOSED:
257 callback.Run(channel_id_, ChannelError::CONNECT_ERROR);
258 break;
259 default:
260 NOTREACHED() << "Unknown ReadyState: "
261 << ReadyStateToString(ready_state_);
262 }
263 }
264
265 void CastSocketImpl::Connect() {
244 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); 266 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
245 VLOG_WITH_CONNECTION(1) << "Connect readyState = " 267 VLOG_WITH_CONNECTION(1) << "Connect readyState = "
246 << ReadyStateToString(ready_state_); 268 << ReadyStateToString(ready_state_);
247 DCHECK_EQ(ConnectionState::START_CONNECT, connect_state_); 269 DCHECK_EQ(ConnectionState::START_CONNECT, connect_state_);
248 270
249 delegate_ = base::MakeUnique<CastSocketMessageDelegate>(this); 271 delegate_ = base::MakeUnique<CastSocketMessageDelegate>(this);
250 272
251 if (ready_state_ != ReadyState::NONE) { 273 DCHECK_EQ(ReadyState::NONE, ready_state_);
imcheng 2017/06/24 01:29:36 Move the DCHECKs to the top of this method.
zhaobin 2017/06/26 19:04:09 Done.
252 callback.Run(ChannelError::CONNECT_ERROR);
253 return;
254 }
255 274
256 connect_callback_ = callback;
257 SetReadyState(ReadyState::CONNECTING); 275 SetReadyState(ReadyState::CONNECTING);
258 SetConnectState(ConnectionState::TCP_CONNECT); 276 SetConnectState(ConnectionState::TCP_CONNECT);
259 277
260 // Set up connection timeout. 278 // Set up connection timeout.
261 if (connect_timeout_.InMicroseconds() > 0) { 279 if (connect_timeout_.InMicroseconds() > 0) {
262 DCHECK(connect_timeout_callback_.IsCancelled()); 280 DCHECK(connect_timeout_callback_.IsCancelled());
263 connect_timeout_callback_.Reset( 281 connect_timeout_callback_.Reset(
264 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); 282 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this)));
265 GetTimer()->Start(FROM_HERE, connect_timeout_, 283 GetTimer()->Start(FROM_HERE, connect_timeout_,
266 connect_timeout_callback_.callback()); 284 connect_timeout_callback_.callback());
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 } 543 }
526 VLOG_WITH_CONNECTION(1) << "Auth challenge verification succeeded"; 544 VLOG_WITH_CONNECTION(1) << "Auth challenge verification succeeded";
527 545
528 SetConnectState(ConnectionState::FINISHED); 546 SetConnectState(ConnectionState::FINISHED);
529 return net::OK; 547 return net::OK;
530 } 548 }
531 549
532 void CastSocketImpl::DoConnectCallback() { 550 void CastSocketImpl::DoConnectCallback() {
533 VLOG(1) << "DoConnectCallback (error_state = " 551 VLOG(1) << "DoConnectCallback (error_state = "
534 << ChannelErrorToString(error_state_) << ")"; 552 << ChannelErrorToString(error_state_) << ")";
535 if (connect_callback_.is_null()) { 553 if (connect_callbacks_.empty()) {
536 DLOG(FATAL) << "Connection callback invoked multiple times."; 554 DLOG(FATAL) << "Connection callback invoked multiple times.";
537 return; 555 return;
538 } 556 }
539 557
540 if (error_state_ == ChannelError::NONE) { 558 if (error_state_ == ChannelError::NONE) {
541 SetReadyState(ReadyState::OPEN); 559 SetReadyState(ReadyState::OPEN);
542 if (keep_alive()) { 560 if (keep_alive()) {
543 auto* keep_alive_delegate = 561 auto* keep_alive_delegate =
544 new KeepAliveDelegate(this, logger_, std::move(delegate_), 562 new KeepAliveDelegate(this, logger_, std::move(delegate_),
545 ping_interval_, liveness_timeout_); 563 ping_interval_, liveness_timeout_);
546 delegate_.reset(keep_alive_delegate); 564 delegate_.reset(keep_alive_delegate);
547 } 565 }
548 transport_->SetReadDelegate(std::move(delegate_)); 566 transport_->SetReadDelegate(std::move(delegate_));
549 } else { 567 } else {
550 CloseInternal(); 568 CloseInternal();
551 } 569 }
552 570
553 base::ResetAndReturn(&connect_callback_).Run(error_state_); 571 for (auto& connect_callback : connect_callbacks_)
572 connect_callback.Run(channel_id_, error_state_);
573 connect_callbacks_.clear();
554 } 574 }
555 575
556 void CastSocketImpl::Close(const net::CompletionCallback& callback) { 576 void CastSocketImpl::Close(const net::CompletionCallback& callback) {
557 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); 577 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
558 CloseInternal(); 578 CloseInternal();
559 // Run this callback last. It may delete the socket. 579 // Run this callback last. It may delete the socket.
560 callback.Run(net::OK); 580 callback.Run(net::OK);
561 } 581 }
562 582
563 void CastSocketImpl::CloseInternal() { 583 void CastSocketImpl::CloseInternal() {
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
626 void CastSocketImpl::CastSocketMessageDelegate::OnMessage( 646 void CastSocketImpl::CastSocketMessageDelegate::OnMessage(
627 const CastMessage& message) { 647 const CastMessage& message) {
628 for (auto& observer : socket_->observers_) 648 for (auto& observer : socket_->observers_)
629 observer.OnMessage(*socket_, message); 649 observer.OnMessage(*socket_, message);
630 } 650 }
631 651
632 void CastSocketImpl::CastSocketMessageDelegate::Start() {} 652 void CastSocketImpl::CastSocketMessageDelegate::Start() {}
633 653
634 } // namespace cast_channel 654 } // namespace cast_channel
635 #undef VLOG_WITH_CONNECTION 655 #undef VLOG_WITH_CONNECTION
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698