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 namespace api { | 61 namespace api { |
60 namespace cast_channel { | 62 namespace cast_channel { |
61 namespace { | 63 namespace { |
62 | 64 |
63 bool IsTerminalState(proto::ConnectionState state) { | 65 bool IsTerminalState(proto::ConnectionState state) { |
64 return state == proto::CONN_STATE_FINISHED || | 66 return state == proto::CONN_STATE_FINISHED || |
(...skipping 13 matching lines...) Expand all Loading... |
78 std::unique_ptr<Request>*, | 80 std::unique_ptr<Request>*, |
79 const net::NetLogWithSource&) override { | 81 const net::NetLogWithSource&) override { |
80 verify_result->Reset(); | 82 verify_result->Reset(); |
81 verify_result->verified_cert = params.certificate(); | 83 verify_result->verified_cert = params.certificate(); |
82 return net::OK; | 84 return net::OK; |
83 } | 85 } |
84 }; | 86 }; |
85 | 87 |
86 } // namespace | 88 } // namespace |
87 | 89 |
| 90 using ChannelError = ::cast_channel::ChannelError; |
| 91 using ChannelAuthType = ::cast_channel::ChannelAuthType; |
| 92 using ReadyState = ::cast_channel::ReadyState; |
| 93 |
88 CastSocketImpl::CastSocketImpl(const std::string& owner_extension_id, | 94 CastSocketImpl::CastSocketImpl(const std::string& owner_extension_id, |
89 const net::IPEndPoint& ip_endpoint, | 95 const net::IPEndPoint& ip_endpoint, |
90 ChannelAuthType channel_auth, | 96 ChannelAuthType channel_auth, |
91 net::NetLog* net_log, | 97 net::NetLog* net_log, |
92 const base::TimeDelta& timeout, | 98 const base::TimeDelta& timeout, |
93 bool keep_alive, | 99 bool keep_alive, |
94 const scoped_refptr<Logger>& logger, | 100 const scoped_refptr<Logger>& logger, |
95 uint64_t device_capabilities) | 101 uint64_t device_capabilities) |
96 : CastSocketImpl(owner_extension_id, | 102 : CastSocketImpl(owner_extension_id, |
97 ip_endpoint, | 103 ip_endpoint, |
(...skipping 20 matching lines...) Expand all Loading... |
118 net_log_(net_log), | 124 net_log_(net_log), |
119 keep_alive_(keep_alive), | 125 keep_alive_(keep_alive), |
120 logger_(logger), | 126 logger_(logger), |
121 auth_context_(auth_context), | 127 auth_context_(auth_context), |
122 connect_timeout_(timeout), | 128 connect_timeout_(timeout), |
123 connect_timeout_timer_(new base::OneShotTimer), | 129 connect_timeout_timer_(new base::OneShotTimer), |
124 is_canceled_(false), | 130 is_canceled_(false), |
125 device_capabilities_(device_capabilities), | 131 device_capabilities_(device_capabilities), |
126 audio_only_(false), | 132 audio_only_(false), |
127 connect_state_(proto::CONN_STATE_START_CONNECT), | 133 connect_state_(proto::CONN_STATE_START_CONNECT), |
128 error_state_(CHANNEL_ERROR_NONE), | 134 error_state_(ChannelError::NONE), |
129 ready_state_(READY_STATE_NONE), | 135 ready_state_(ReadyState::NONE), |
130 auth_delegate_(nullptr) { | 136 auth_delegate_(nullptr) { |
131 DCHECK(net_log_); | 137 DCHECK(net_log_); |
132 net_log_source_.type = net::NetLogSourceType::SOCKET; | 138 net_log_source_.type = net::NetLogSourceType::SOCKET; |
133 net_log_source_.id = net_log_->NextID(); | 139 net_log_source_.id = net_log_->NextID(); |
134 } | 140 } |
135 | 141 |
136 CastSocketImpl::~CastSocketImpl() { | 142 CastSocketImpl::~CastSocketImpl() { |
137 // Ensure that resources are freed but do not run pending callbacks that | 143 // Ensure that resources are freed but do not run pending callbacks that |
138 // would result in re-entrancy. | 144 // would result in re-entrancy. |
139 CloseInternal(); | 145 CloseInternal(); |
140 | 146 |
141 if (!connect_callback_.is_null()) | 147 if (!connect_callback_.is_null()) |
142 base::ResetAndReturn(&connect_callback_).Run(CHANNEL_ERROR_UNKNOWN); | 148 base::ResetAndReturn(&connect_callback_).Run(ChannelError::UNKNOWN); |
143 } | 149 } |
144 | 150 |
145 ReadyState CastSocketImpl::ready_state() const { | 151 ReadyState CastSocketImpl::ready_state() const { |
146 return ready_state_; | 152 return ready_state_; |
147 } | 153 } |
148 | 154 |
149 ChannelError CastSocketImpl::error_state() const { | 155 ChannelError CastSocketImpl::error_state() const { |
150 return error_state_; | 156 return error_state_; |
151 } | 157 } |
152 | 158 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 } | 248 } |
243 | 249 |
244 void CastSocketImpl::SetTransportForTesting( | 250 void CastSocketImpl::SetTransportForTesting( |
245 std::unique_ptr<CastTransport> transport) { | 251 std::unique_ptr<CastTransport> transport) { |
246 transport_ = std::move(transport); | 252 transport_ = std::move(transport); |
247 } | 253 } |
248 | 254 |
249 void CastSocketImpl::Connect(std::unique_ptr<CastTransport::Delegate> delegate, | 255 void CastSocketImpl::Connect(std::unique_ptr<CastTransport::Delegate> delegate, |
250 base::Callback<void(ChannelError)> callback) { | 256 base::Callback<void(ChannelError)> callback) { |
251 DCHECK(CalledOnValidThread()); | 257 DCHECK(CalledOnValidThread()); |
252 VLOG_WITH_CONNECTION(1) << "Connect readyState = " << ready_state_; | 258 VLOG_WITH_CONNECTION(1) << "Connect readyState = " |
| 259 << ::cast_channel::ReadyStateToString(ready_state_); |
253 DCHECK_EQ(proto::CONN_STATE_START_CONNECT, connect_state_); | 260 DCHECK_EQ(proto::CONN_STATE_START_CONNECT, connect_state_); |
254 | 261 |
255 delegate_ = std::move(delegate); | 262 delegate_ = std::move(delegate); |
256 | 263 |
257 if (ready_state_ != READY_STATE_NONE) { | 264 if (ready_state_ != ReadyState::NONE) { |
258 callback.Run(CHANNEL_ERROR_CONNECT_ERROR); | 265 callback.Run(ChannelError::CONNECT_ERROR); |
259 return; | 266 return; |
260 } | 267 } |
261 | 268 |
262 connect_callback_ = callback; | 269 connect_callback_ = callback; |
263 SetReadyState(READY_STATE_CONNECTING); | 270 SetReadyState(ReadyState::CONNECTING); |
264 SetConnectState(proto::CONN_STATE_TCP_CONNECT); | 271 SetConnectState(proto::CONN_STATE_TCP_CONNECT); |
265 | 272 |
266 // Set up connection timeout. | 273 // Set up connection timeout. |
267 if (connect_timeout_.InMicroseconds() > 0) { | 274 if (connect_timeout_.InMicroseconds() > 0) { |
268 DCHECK(connect_timeout_callback_.IsCancelled()); | 275 DCHECK(connect_timeout_callback_.IsCancelled()); |
269 connect_timeout_callback_.Reset( | 276 connect_timeout_callback_.Reset( |
270 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); | 277 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); |
271 GetTimer()->Start(FROM_HERE, | 278 GetTimer()->Start(FROM_HERE, |
272 connect_timeout_, | 279 connect_timeout_, |
273 connect_timeout_callback_.callback()); | 280 connect_timeout_callback_.callback()); |
274 } | 281 } |
275 | 282 |
276 DoConnectLoop(net::OK); | 283 DoConnectLoop(net::OK); |
277 } | 284 } |
278 | 285 |
279 CastTransport* CastSocketImpl::transport() const { | 286 CastTransport* CastSocketImpl::transport() const { |
280 return transport_.get(); | 287 return transport_.get(); |
281 } | 288 } |
282 | 289 |
283 void CastSocketImpl::OnConnectTimeout() { | 290 void CastSocketImpl::OnConnectTimeout() { |
284 DCHECK(CalledOnValidThread()); | 291 DCHECK(CalledOnValidThread()); |
285 // Stop all pending connection setup tasks and report back to the client. | 292 // Stop all pending connection setup tasks and report back to the client. |
286 is_canceled_ = true; | 293 is_canceled_ = true; |
287 VLOG_WITH_CONNECTION(1) << "Timeout while establishing a connection."; | 294 VLOG_WITH_CONNECTION(1) << "Timeout while establishing a connection."; |
288 SetErrorState(CHANNEL_ERROR_CONNECT_TIMEOUT); | 295 SetErrorState(ChannelError::CONNECT_TIMEOUT); |
289 DoConnectCallback(); | 296 DoConnectCallback(); |
290 } | 297 } |
291 | 298 |
292 void CastSocketImpl::ResetConnectLoopCallback() { | 299 void CastSocketImpl::ResetConnectLoopCallback() { |
293 DCHECK(connect_loop_callback_.IsCancelled()); | 300 DCHECK(connect_loop_callback_.IsCancelled()); |
294 connect_loop_callback_.Reset( | 301 connect_loop_callback_.Reset( |
295 base::Bind(&CastSocketImpl::DoConnectLoop, base::Unretained(this))); | 302 base::Bind(&CastSocketImpl::DoConnectLoop, base::Unretained(this))); |
296 } | 303 } |
297 | 304 |
298 void CastSocketImpl::PostTaskToStartConnectLoop(int result) { | 305 void CastSocketImpl::PostTaskToStartConnectLoop(int result) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 case proto::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE: | 349 case proto::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE: |
343 rv = DoAuthChallengeSendComplete(rv); | 350 rv = DoAuthChallengeSendComplete(rv); |
344 break; | 351 break; |
345 case proto::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE: | 352 case proto::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE: |
346 rv = DoAuthChallengeReplyComplete(rv); | 353 rv = DoAuthChallengeReplyComplete(rv); |
347 DCHECK(IsTerminalState(connect_state_)); | 354 DCHECK(IsTerminalState(connect_state_)); |
348 break; | 355 break; |
349 default: | 356 default: |
350 NOTREACHED() << "Unknown state in connect flow: " << state; | 357 NOTREACHED() << "Unknown state in connect flow: " << state; |
351 SetConnectState(proto::CONN_STATE_FINISHED); | 358 SetConnectState(proto::CONN_STATE_FINISHED); |
352 SetErrorState(CHANNEL_ERROR_UNKNOWN); | 359 SetErrorState(ChannelError::UNKNOWN); |
353 DoConnectCallback(); | 360 DoConnectCallback(); |
354 return; | 361 return; |
355 } | 362 } |
356 } while (rv != net::ERR_IO_PENDING && !IsTerminalState(connect_state_)); | 363 } while (rv != net::ERR_IO_PENDING && !IsTerminalState(connect_state_)); |
357 // Exit the state machine if an asynchronous network operation is pending | 364 // Exit the state machine if an asynchronous network operation is pending |
358 // or if the state machine is in the terminal "finished" state. | 365 // or if the state machine is in the terminal "finished" state. |
359 | 366 |
360 if (IsTerminalState(connect_state_)) { | 367 if (IsTerminalState(connect_state_)) { |
361 DCHECK_NE(rv, net::ERR_IO_PENDING); | 368 DCHECK_NE(rv, net::ERR_IO_PENDING); |
362 GetTimer()->Stop(); | 369 GetTimer()->Stop(); |
(...skipping 16 matching lines...) Expand all Loading... |
379 } | 386 } |
380 | 387 |
381 int CastSocketImpl::DoTcpConnectComplete(int connect_result) { | 388 int CastSocketImpl::DoTcpConnectComplete(int connect_result) { |
382 VLOG_WITH_CONNECTION(1) << "DoTcpConnectComplete: " << connect_result; | 389 VLOG_WITH_CONNECTION(1) << "DoTcpConnectComplete: " << connect_result; |
383 logger_->LogSocketEventWithRv(channel_id_, proto::TCP_SOCKET_CONNECT_COMPLETE, | 390 logger_->LogSocketEventWithRv(channel_id_, proto::TCP_SOCKET_CONNECT_COMPLETE, |
384 connect_result); | 391 connect_result); |
385 if (connect_result == net::OK) { | 392 if (connect_result == net::OK) { |
386 SetConnectState(proto::CONN_STATE_SSL_CONNECT); | 393 SetConnectState(proto::CONN_STATE_SSL_CONNECT); |
387 } else if (connect_result == net::ERR_CONNECTION_TIMED_OUT) { | 394 } else if (connect_result == net::ERR_CONNECTION_TIMED_OUT) { |
388 SetConnectState(proto::CONN_STATE_FINISHED); | 395 SetConnectState(proto::CONN_STATE_FINISHED); |
389 SetErrorState(CHANNEL_ERROR_CONNECT_TIMEOUT); | 396 SetErrorState(ChannelError::CONNECT_TIMEOUT); |
390 } else { | 397 } else { |
391 SetConnectState(proto::CONN_STATE_FINISHED); | 398 SetConnectState(proto::CONN_STATE_FINISHED); |
392 SetErrorState(CHANNEL_ERROR_CONNECT_ERROR); | 399 SetErrorState(ChannelError::CONNECT_ERROR); |
393 } | 400 } |
394 return connect_result; | 401 return connect_result; |
395 } | 402 } |
396 | 403 |
397 int CastSocketImpl::DoSslConnect() { | 404 int CastSocketImpl::DoSslConnect() { |
398 DCHECK(connect_loop_callback_.IsCancelled()); | 405 DCHECK(connect_loop_callback_.IsCancelled()); |
399 VLOG_WITH_CONNECTION(1) << "DoSslConnect"; | 406 VLOG_WITH_CONNECTION(1) << "DoSslConnect"; |
400 SetConnectState(proto::CONN_STATE_SSL_CONNECT_COMPLETE); | 407 SetConnectState(proto::CONN_STATE_SSL_CONNECT_COMPLETE); |
401 socket_ = CreateSslSocket(std::move(tcp_socket_)); | 408 socket_ = CreateSslSocket(std::move(tcp_socket_)); |
402 | 409 |
403 int rv = socket_->Connect( | 410 int rv = socket_->Connect( |
404 base::Bind(&CastSocketImpl::DoConnectLoop, base::Unretained(this))); | 411 base::Bind(&CastSocketImpl::DoConnectLoop, base::Unretained(this))); |
405 logger_->LogSocketEventWithRv(channel_id_, proto::SSL_SOCKET_CONNECT, rv); | 412 logger_->LogSocketEventWithRv(channel_id_, proto::SSL_SOCKET_CONNECT, rv); |
406 return rv; | 413 return rv; |
407 } | 414 } |
408 | 415 |
409 int CastSocketImpl::DoSslConnectComplete(int result) { | 416 int CastSocketImpl::DoSslConnectComplete(int result) { |
410 logger_->LogSocketEventWithRv(channel_id_, proto::SSL_SOCKET_CONNECT_COMPLETE, | 417 logger_->LogSocketEventWithRv(channel_id_, proto::SSL_SOCKET_CONNECT_COMPLETE, |
411 result); | 418 result); |
412 VLOG_WITH_CONNECTION(1) << "DoSslConnectComplete: " << result; | 419 VLOG_WITH_CONNECTION(1) << "DoSslConnectComplete: " << result; |
413 if (result == net::OK) { | 420 if (result == net::OK) { |
414 peer_cert_ = ExtractPeerCert(); | 421 peer_cert_ = ExtractPeerCert(); |
415 | 422 |
416 if (!peer_cert_) { | 423 if (!peer_cert_) { |
417 LOG_WITH_CONNECTION(WARNING) << "Could not extract peer cert."; | 424 LOG_WITH_CONNECTION(WARNING) << "Could not extract peer cert."; |
418 SetConnectState(proto::CONN_STATE_FINISHED); | 425 SetConnectState(proto::CONN_STATE_FINISHED); |
419 SetErrorState(CHANNEL_ERROR_AUTHENTICATION_ERROR); | 426 SetErrorState(ChannelError::AUTHENTICATION_ERROR); |
420 return net::ERR_CERT_INVALID; | 427 return net::ERR_CERT_INVALID; |
421 } | 428 } |
422 | 429 |
423 // SSL connection succeeded. | 430 // SSL connection succeeded. |
424 if (!transport_.get()) { | 431 if (!transport_.get()) { |
425 // Create a channel transport if one wasn't already set (e.g. by test | 432 // Create a channel transport if one wasn't already set (e.g. by test |
426 // code). | 433 // code). |
427 transport_.reset(new CastTransportImpl(this->socket_.get(), channel_id_, | 434 transport_.reset(new CastTransportImpl(this->socket_.get(), channel_id_, |
428 ip_endpoint_, channel_auth_, | 435 ip_endpoint_, channel_auth_, |
429 logger_)); | 436 logger_)); |
430 } | 437 } |
431 auth_delegate_ = new AuthTransportDelegate(this); | 438 auth_delegate_ = new AuthTransportDelegate(this); |
432 transport_->SetReadDelegate(base::WrapUnique(auth_delegate_)); | 439 transport_->SetReadDelegate(base::WrapUnique(auth_delegate_)); |
433 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_SEND); | 440 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_SEND); |
434 } else if (result == net::ERR_CONNECTION_TIMED_OUT) { | 441 } else if (result == net::ERR_CONNECTION_TIMED_OUT) { |
435 SetConnectState(proto::CONN_STATE_FINISHED); | 442 SetConnectState(proto::CONN_STATE_FINISHED); |
436 SetErrorState(CHANNEL_ERROR_CONNECT_TIMEOUT); | 443 SetErrorState(ChannelError::CONNECT_TIMEOUT); |
437 } else { | 444 } else { |
438 SetConnectState(proto::CONN_STATE_FINISHED); | 445 SetConnectState(proto::CONN_STATE_FINISHED); |
439 SetErrorState(CHANNEL_ERROR_AUTHENTICATION_ERROR); | 446 SetErrorState(ChannelError::AUTHENTICATION_ERROR); |
440 } | 447 } |
441 return result; | 448 return result; |
442 } | 449 } |
443 | 450 |
444 int CastSocketImpl::DoAuthChallengeSend() { | 451 int CastSocketImpl::DoAuthChallengeSend() { |
445 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeSend"; | 452 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeSend"; |
446 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE); | 453 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE); |
447 | 454 |
448 CastMessage challenge_message; | 455 CastMessage challenge_message; |
449 CreateAuthChallengeMessage(&challenge_message, auth_context_); | 456 CreateAuthChallengeMessage(&challenge_message, auth_context_); |
450 VLOG_WITH_CONNECTION(1) << "Sending challenge: " | 457 VLOG_WITH_CONNECTION(1) << "Sending challenge: " |
451 << CastMessageToString(challenge_message); | 458 << CastMessageToString(challenge_message); |
452 | 459 |
453 ResetConnectLoopCallback(); | 460 ResetConnectLoopCallback(); |
454 transport_->SendMessage(challenge_message, connect_loop_callback_.callback()); | 461 transport_->SendMessage(challenge_message, connect_loop_callback_.callback()); |
455 | 462 |
456 // Always return IO_PENDING since the result is always asynchronous. | 463 // Always return IO_PENDING since the result is always asynchronous. |
457 return net::ERR_IO_PENDING; | 464 return net::ERR_IO_PENDING; |
458 } | 465 } |
459 | 466 |
460 int CastSocketImpl::DoAuthChallengeSendComplete(int result) { | 467 int CastSocketImpl::DoAuthChallengeSendComplete(int result) { |
461 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeSendComplete: " << result; | 468 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeSendComplete: " << result; |
462 if (result < 0) { | 469 if (result < 0) { |
463 SetConnectState(proto::CONN_STATE_ERROR); | 470 SetConnectState(proto::CONN_STATE_ERROR); |
464 SetErrorState(CHANNEL_ERROR_SOCKET_ERROR); | 471 SetErrorState(ChannelError::CAST_SOCKET_ERROR); |
465 logger_->LogSocketEventWithRv(channel_id_, | 472 logger_->LogSocketEventWithRv(channel_id_, |
466 proto::SEND_AUTH_CHALLENGE_FAILED, result); | 473 proto::SEND_AUTH_CHALLENGE_FAILED, result); |
467 return result; | 474 return result; |
468 } | 475 } |
469 transport_->Start(); | 476 transport_->Start(); |
470 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE); | 477 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE); |
471 return net::ERR_IO_PENDING; | 478 return net::ERR_IO_PENDING; |
472 } | 479 } |
473 | 480 |
474 CastSocketImpl::AuthTransportDelegate::AuthTransportDelegate( | 481 CastSocketImpl::AuthTransportDelegate::AuthTransportDelegate( |
475 CastSocketImpl* socket) | 482 CastSocketImpl* socket) |
476 : socket_(socket), error_state_(CHANNEL_ERROR_NONE) { | 483 : socket_(socket), error_state_(ChannelError::NONE) { |
477 DCHECK(socket); | 484 DCHECK(socket); |
478 } | 485 } |
479 | 486 |
480 ChannelError CastSocketImpl::AuthTransportDelegate::error_state() const { | 487 ChannelError CastSocketImpl::AuthTransportDelegate::error_state() const { |
481 return error_state_; | 488 return error_state_; |
482 } | 489 } |
483 | 490 |
484 LastErrors CastSocketImpl::AuthTransportDelegate::last_errors() const { | 491 LastErrors CastSocketImpl::AuthTransportDelegate::last_errors() const { |
485 return last_errors_; | 492 return last_errors_; |
486 } | 493 } |
487 | 494 |
488 void CastSocketImpl::AuthTransportDelegate::OnError(ChannelError error_state) { | 495 void CastSocketImpl::AuthTransportDelegate::OnError(ChannelError error_state) { |
489 error_state_ = error_state; | 496 error_state_ = error_state; |
490 socket_->PostTaskToStartConnectLoop(net::ERR_CONNECTION_FAILED); | 497 socket_->PostTaskToStartConnectLoop(net::ERR_CONNECTION_FAILED); |
491 } | 498 } |
492 | 499 |
493 void CastSocketImpl::AuthTransportDelegate::OnMessage( | 500 void CastSocketImpl::AuthTransportDelegate::OnMessage( |
494 const CastMessage& message) { | 501 const CastMessage& message) { |
495 if (!IsAuthMessage(message)) { | 502 if (!IsAuthMessage(message)) { |
496 error_state_ = CHANNEL_ERROR_TRANSPORT_ERROR; | 503 error_state_ = ChannelError::TRANSPORT_ERROR; |
497 socket_->PostTaskToStartConnectLoop(net::ERR_INVALID_RESPONSE); | 504 socket_->PostTaskToStartConnectLoop(net::ERR_INVALID_RESPONSE); |
498 } else { | 505 } else { |
499 socket_->challenge_reply_.reset(new CastMessage(message)); | 506 socket_->challenge_reply_.reset(new CastMessage(message)); |
500 socket_->PostTaskToStartConnectLoop(net::OK); | 507 socket_->PostTaskToStartConnectLoop(net::OK); |
501 } | 508 } |
502 } | 509 } |
503 | 510 |
504 void CastSocketImpl::AuthTransportDelegate::Start() { | 511 void CastSocketImpl::AuthTransportDelegate::Start() { |
505 } | 512 } |
506 | 513 |
507 int CastSocketImpl::DoAuthChallengeReplyComplete(int result) { | 514 int CastSocketImpl::DoAuthChallengeReplyComplete(int result) { |
508 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeReplyComplete: " << result; | 515 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeReplyComplete: " << result; |
509 | 516 |
510 if (auth_delegate_->error_state() != CHANNEL_ERROR_NONE) { | 517 if (auth_delegate_->error_state() != ChannelError::NONE) { |
511 SetErrorState(auth_delegate_->error_state()); | 518 SetErrorState(auth_delegate_->error_state()); |
512 SetConnectState(proto::CONN_STATE_ERROR); | 519 SetConnectState(proto::CONN_STATE_ERROR); |
513 return net::ERR_CONNECTION_FAILED; | 520 return net::ERR_CONNECTION_FAILED; |
514 } | 521 } |
515 auth_delegate_ = nullptr; | 522 auth_delegate_ = nullptr; |
516 | 523 |
517 if (result < 0) { | 524 if (result < 0) { |
518 SetConnectState(proto::CONN_STATE_ERROR); | 525 SetConnectState(proto::CONN_STATE_ERROR); |
519 return result; | 526 return result; |
520 } | 527 } |
521 | 528 |
522 if (!VerifyChallengeReply()) { | 529 if (!VerifyChallengeReply()) { |
523 SetErrorState(CHANNEL_ERROR_AUTHENTICATION_ERROR); | 530 SetErrorState(ChannelError::AUTHENTICATION_ERROR); |
524 SetConnectState(proto::CONN_STATE_ERROR); | 531 SetConnectState(proto::CONN_STATE_ERROR); |
525 return net::ERR_CONNECTION_FAILED; | 532 return net::ERR_CONNECTION_FAILED; |
526 } | 533 } |
527 VLOG_WITH_CONNECTION(1) << "Auth challenge verification succeeded"; | 534 VLOG_WITH_CONNECTION(1) << "Auth challenge verification succeeded"; |
528 | 535 |
529 SetConnectState(proto::CONN_STATE_FINISHED); | 536 SetConnectState(proto::CONN_STATE_FINISHED); |
530 return net::OK; | 537 return net::OK; |
531 } | 538 } |
532 | 539 |
533 void CastSocketImpl::DoConnectCallback() { | 540 void CastSocketImpl::DoConnectCallback() { |
534 VLOG(1) << "DoConnectCallback (error_state = " << error_state_ << ")"; | 541 VLOG(1) << "DoConnectCallback (error_state = " |
| 542 << ::cast_channel::ChannelErrorToString(error_state_) << ")"; |
535 if (connect_callback_.is_null()) { | 543 if (connect_callback_.is_null()) { |
536 DLOG(FATAL) << "Connection callback invoked multiple times."; | 544 DLOG(FATAL) << "Connection callback invoked multiple times."; |
537 return; | 545 return; |
538 } | 546 } |
539 | 547 |
540 if (error_state_ == CHANNEL_ERROR_NONE) { | 548 if (error_state_ == ChannelError::NONE) { |
541 SetReadyState(READY_STATE_OPEN); | 549 SetReadyState(ReadyState::OPEN); |
542 transport_->SetReadDelegate(std::move(delegate_)); | 550 transport_->SetReadDelegate(std::move(delegate_)); |
543 } else { | 551 } else { |
544 CloseInternal(); | 552 CloseInternal(); |
545 } | 553 } |
546 | 554 |
547 base::ResetAndReturn(&connect_callback_).Run(error_state_); | 555 base::ResetAndReturn(&connect_callback_).Run(error_state_); |
548 } | 556 } |
549 | 557 |
550 void CastSocketImpl::Close(const net::CompletionCallback& callback) { | 558 void CastSocketImpl::Close(const net::CompletionCallback& callback) { |
551 DCHECK(CalledOnValidThread()); | 559 DCHECK(CalledOnValidThread()); |
552 CloseInternal(); | 560 CloseInternal(); |
553 // Run this callback last. It may delete the socket. | 561 // Run this callback last. It may delete the socket. |
554 callback.Run(net::OK); | 562 callback.Run(net::OK); |
555 } | 563 } |
556 | 564 |
557 void CastSocketImpl::CloseInternal() { | 565 void CastSocketImpl::CloseInternal() { |
558 // TODO(mfoltz): Enforce this when CastChannelAPITest is rewritten to create | 566 // TODO(mfoltz): Enforce this when CastChannelAPITest is rewritten to create |
559 // and free sockets on the same thread. crbug.com/398242 | 567 // and free sockets on the same thread. crbug.com/398242 |
560 DCHECK(CalledOnValidThread()); | 568 DCHECK(CalledOnValidThread()); |
561 if (ready_state_ == READY_STATE_CLOSED) { | 569 if (ready_state_ == ReadyState::CLOSED) { |
562 return; | 570 return; |
563 } | 571 } |
564 | 572 |
565 VLOG_WITH_CONNECTION(1) << "Close ReadyState = " << ready_state_; | 573 VLOG_WITH_CONNECTION(1) << "Close ReadyState = " |
| 574 << ::cast_channel::ReadyStateToString(ready_state_); |
566 transport_.reset(); | 575 transport_.reset(); |
567 tcp_socket_.reset(); | 576 tcp_socket_.reset(); |
568 socket_.reset(); | 577 socket_.reset(); |
569 transport_security_state_.reset(); | 578 transport_security_state_.reset(); |
570 if (GetTimer()) { | 579 if (GetTimer()) { |
571 GetTimer()->Stop(); | 580 GetTimer()->Stop(); |
572 } | 581 } |
573 | 582 |
574 // Cancel callbacks that we queued ourselves to re-enter the connect or read | 583 // Cancel callbacks that we queued ourselves to re-enter the connect or read |
575 // loops. | 584 // loops. |
576 connect_loop_callback_.Cancel(); | 585 connect_loop_callback_.Cancel(); |
577 connect_timeout_callback_.Cancel(); | 586 connect_timeout_callback_.Cancel(); |
578 SetReadyState(READY_STATE_CLOSED); | 587 SetReadyState(ReadyState::CLOSED); |
579 } | 588 } |
580 | 589 |
581 bool CastSocketImpl::CalledOnValidThread() const { | 590 bool CastSocketImpl::CalledOnValidThread() const { |
582 return thread_checker_.CalledOnValidThread(); | 591 return thread_checker_.CalledOnValidThread(); |
583 } | 592 } |
584 | 593 |
585 base::Timer* CastSocketImpl::GetTimer() { | 594 base::Timer* CastSocketImpl::GetTimer() { |
586 return connect_timeout_timer_.get(); | 595 return connect_timeout_timer_.get(); |
587 } | 596 } |
588 | 597 |
589 void CastSocketImpl::SetConnectState(proto::ConnectionState connect_state) { | 598 void CastSocketImpl::SetConnectState(proto::ConnectionState connect_state) { |
590 if (connect_state_ != connect_state) { | 599 if (connect_state_ != connect_state) { |
591 connect_state_ = connect_state; | 600 connect_state_ = connect_state; |
592 } | 601 } |
593 } | 602 } |
594 | 603 |
595 void CastSocketImpl::SetReadyState(ReadyState ready_state) { | 604 void CastSocketImpl::SetReadyState(ReadyState ready_state) { |
596 if (ready_state_ != ready_state) | 605 if (ready_state_ != ready_state) |
597 ready_state_ = ready_state; | 606 ready_state_ = ready_state; |
598 } | 607 } |
599 | 608 |
600 void CastSocketImpl::SetErrorState(ChannelError error_state) { | 609 void CastSocketImpl::SetErrorState(ChannelError error_state) { |
601 VLOG_WITH_CONNECTION(1) << "SetErrorState " << error_state; | 610 VLOG_WITH_CONNECTION(1) << "SetErrorState " |
602 DCHECK_EQ(CHANNEL_ERROR_NONE, error_state_); | 611 << ::cast_channel::ChannelErrorToString(error_state); |
| 612 DCHECK_EQ(ChannelError::NONE, error_state_); |
603 error_state_ = error_state; | 613 error_state_ = error_state; |
604 delegate_->OnError(error_state_); | 614 delegate_->OnError(error_state_); |
605 } | 615 } |
606 | 616 |
607 } // namespace cast_channel | 617 } // namespace cast_channel |
608 } // namespace api | 618 } // namespace api |
609 } // namespace extensions | 619 } // namespace extensions |
610 #undef VLOG_WITH_CONNECTION | 620 #undef VLOG_WITH_CONNECTION |
OLD | NEW |