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

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

Issue 2942993003: [cast_channel] Make CastMessageHandler a CastSocket::Observer instead of CastTransport::Delegate (Closed)
Patch Set: 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 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 << ::cast_channel::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 bool CastSocketImpl::HasObserver(const std::string& id) {
277 return base::ContainsKey(observer_map_, id);
278 }
279
280 void CastSocketImpl::AddObserver(const std::string& id,
281 std::unique_ptr<Observer> observer) {
282 DCHECK(!base::ContainsKey(observer_map_, id));
283 observer_map_.insert(std::make_pair(id, std::move(observer)));
284 }
285
277 void CastSocketImpl::OnConnectTimeout() { 286 void CastSocketImpl::OnConnectTimeout() {
278 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); 287 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
279 // Stop all pending connection setup tasks and report back to the client. 288 // Stop all pending connection setup tasks and report back to the client.
280 is_canceled_ = true; 289 is_canceled_ = true;
281 VLOG_WITH_CONNECTION(1) << "Timeout while establishing a connection."; 290 VLOG_WITH_CONNECTION(1) << "Timeout while establishing a connection.";
282 SetErrorState(ChannelError::CONNECT_TIMEOUT); 291 SetErrorState(ChannelError::CONNECT_TIMEOUT);
283 DoConnectCallback(); 292 DoConnectCallback();
284 } 293 }
285 294
286 void CastSocketImpl::ResetConnectLoopCallback() { 295 void CastSocketImpl::ResetConnectLoopCallback() {
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 if (ready_state_ == ReadyState::CLOSED) { 571 if (ready_state_ == ReadyState::CLOSED) {
563 return; 572 return;
564 } 573 }
565 574
566 VLOG_WITH_CONNECTION(1) << "Close ReadyState = " 575 VLOG_WITH_CONNECTION(1) << "Close ReadyState = "
567 << ::cast_channel::ReadyStateToString(ready_state_); 576 << ::cast_channel::ReadyStateToString(ready_state_);
568 transport_.reset(); 577 transport_.reset();
569 tcp_socket_.reset(); 578 tcp_socket_.reset();
570 socket_.reset(); 579 socket_.reset();
571 transport_security_state_.reset(); 580 transport_security_state_.reset();
581 observer_map_.clear();
572 if (GetTimer()) { 582 if (GetTimer()) {
573 GetTimer()->Stop(); 583 GetTimer()->Stop();
574 } 584 }
575 585
576 // Cancel callbacks that we queued ourselves to re-enter the connect or read 586 // Cancel callbacks that we queued ourselves to re-enter the connect or read
577 // loops. 587 // loops.
578 connect_loop_callback_.Cancel(); 588 connect_loop_callback_.Cancel();
579 connect_timeout_callback_.Cancel(); 589 connect_timeout_callback_.Cancel();
580 SetReadyState(ReadyState::CLOSED); 590 SetReadyState(ReadyState::CLOSED);
581 } 591 }
(...skipping 14 matching lines...) Expand all
596 } 606 }
597 607
598 void CastSocketImpl::SetErrorState(ChannelError error_state) { 608 void CastSocketImpl::SetErrorState(ChannelError error_state) {
599 VLOG_WITH_CONNECTION(1) << "SetErrorState " 609 VLOG_WITH_CONNECTION(1) << "SetErrorState "
600 << ::cast_channel::ChannelErrorToString(error_state); 610 << ::cast_channel::ChannelErrorToString(error_state);
601 DCHECK_EQ(ChannelError::NONE, error_state_); 611 DCHECK_EQ(ChannelError::NONE, error_state_);
602 error_state_ = error_state; 612 error_state_ = error_state;
603 delegate_->OnError(error_state_); 613 delegate_->OnError(error_state_);
604 } 614 }
605 615
616 CastSocketImpl::CastSocketMessageDelegate::CastSocketMessageDelegate(
617 cast_channel::CastSocketImpl* socket)
618 : socket_(socket) {
619 DCHECK(socket_);
620 }
621
622 CastSocketImpl::CastSocketMessageDelegate::~CastSocketMessageDelegate() {}
623
624 // CastTransport::Delegate implementation.
625 void CastSocketImpl::CastSocketMessageDelegate::OnError(
626 cast_channel::ChannelError error_state) {
627 for (auto& it : socket_->observer_map_)
628 it.second->OnError(*socket_, error_state);
629 }
630
631 void CastSocketImpl::CastSocketMessageDelegate::OnMessage(
632 const cast_channel::CastMessage& message) {
633 for (auto& it : socket_->observer_map_)
634 it.second->OnMessage(*socket_, message);
635 }
636
637 void CastSocketImpl::CastSocketMessageDelegate::Start() {}
638
606 } // namespace cast_channel 639 } // namespace cast_channel
607 #undef VLOG_WITH_CONNECTION 640 #undef VLOG_WITH_CONNECTION
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698