| 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 "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
| 12 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/numerics/safe_conversions.h" | 14 #include "base/numerics/safe_conversions.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/sys_byteorder.h" | 17 #include "base/sys_byteorder.h" |
| 18 #include "extensions/browser/api/cast_channel/cast_auth_util.h" | 18 #include "extensions/browser/api/cast_channel/cast_auth_util.h" |
| 19 #include "extensions/browser/api/cast_channel/cast_channel.pb.h" | |
| 20 #include "extensions/browser/api/cast_channel/cast_framer.h" | 19 #include "extensions/browser/api/cast_channel/cast_framer.h" |
| 21 #include "extensions/browser/api/cast_channel/cast_message_util.h" | 20 #include "extensions/browser/api/cast_channel/cast_message_util.h" |
| 22 #include "extensions/browser/api/cast_channel/logger.h" | 21 #include "extensions/browser/api/cast_channel/logger.h" |
| 23 #include "extensions/browser/api/cast_channel/logger_util.h" | 22 #include "extensions/browser/api/cast_channel/logger_util.h" |
| 23 #include "extensions/common/api/cast_channel/cast_channel.pb.h" |
| 24 #include "net/base/address_list.h" | 24 #include "net/base/address_list.h" |
| 25 #include "net/base/host_port_pair.h" | 25 #include "net/base/host_port_pair.h" |
| 26 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
| 27 #include "net/base/net_util.h" | 27 #include "net/base/net_util.h" |
| 28 #include "net/cert/cert_verifier.h" | 28 #include "net/cert/cert_verifier.h" |
| 29 #include "net/cert/x509_certificate.h" | 29 #include "net/cert/x509_certificate.h" |
| 30 #include "net/http/transport_security_state.h" | 30 #include "net/http/transport_security_state.h" |
| 31 #include "net/socket/client_socket_factory.h" | 31 #include "net/socket/client_socket_factory.h" |
| 32 #include "net/socket/client_socket_handle.h" | 32 #include "net/socket/client_socket_handle.h" |
| 33 #include "net/socket/ssl_client_socket.h" | 33 #include "net/socket/ssl_client_socket.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 59 // static | 59 // static |
| 60 template <> | 60 template <> |
| 61 BrowserContextKeyedAPIFactory< | 61 BrowserContextKeyedAPIFactory< |
| 62 ApiResourceManager<core_api::cast_channel::CastSocket> >* | 62 ApiResourceManager<core_api::cast_channel::CastSocket> >* |
| 63 ApiResourceManager<core_api::cast_channel::CastSocket>::GetFactoryInstance() { | 63 ApiResourceManager<core_api::cast_channel::CastSocket>::GetFactoryInstance() { |
| 64 return g_factory.Pointer(); | 64 return g_factory.Pointer(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 namespace core_api { | 67 namespace core_api { |
| 68 namespace cast_channel { | 68 namespace cast_channel { |
| 69 | |
| 70 namespace { | |
| 71 | |
| 72 proto::ReadyState ReadyStateToProto(ReadyState state) { | |
| 73 switch (state) { | |
| 74 case READY_STATE_NONE: | |
| 75 return proto::READY_STATE_NONE; | |
| 76 case READY_STATE_CONNECTING: | |
| 77 return proto::READY_STATE_CONNECTING; | |
| 78 case READY_STATE_OPEN: | |
| 79 return proto::READY_STATE_OPEN; | |
| 80 case READY_STATE_CLOSING: | |
| 81 return proto::READY_STATE_CLOSING; | |
| 82 case READY_STATE_CLOSED: | |
| 83 return proto::READY_STATE_CLOSED; | |
| 84 default: | |
| 85 NOTREACHED(); | |
| 86 return proto::READY_STATE_NONE; | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 proto::ConnectionState ConnectStateToProto(CastSocket::ConnectionState state) { | |
| 91 switch (state) { | |
| 92 case CastSocket::CONN_STATE_NONE: | |
| 93 return proto::CONN_STATE_NONE; | |
| 94 case CastSocket::CONN_STATE_TCP_CONNECT: | |
| 95 return proto::CONN_STATE_TCP_CONNECT; | |
| 96 case CastSocket::CONN_STATE_TCP_CONNECT_COMPLETE: | |
| 97 return proto::CONN_STATE_TCP_CONNECT_COMPLETE; | |
| 98 case CastSocket::CONN_STATE_SSL_CONNECT: | |
| 99 return proto::CONN_STATE_SSL_CONNECT; | |
| 100 case CastSocket::CONN_STATE_SSL_CONNECT_COMPLETE: | |
| 101 return proto::CONN_STATE_SSL_CONNECT_COMPLETE; | |
| 102 case CastSocket::CONN_STATE_AUTH_CHALLENGE_SEND: | |
| 103 return proto::CONN_STATE_AUTH_CHALLENGE_SEND; | |
| 104 case CastSocket::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE: | |
| 105 return proto::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE; | |
| 106 case CastSocket::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE: | |
| 107 return proto::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE; | |
| 108 default: | |
| 109 NOTREACHED(); | |
| 110 return proto::CONN_STATE_NONE; | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 proto::ReadState ReadStateToProto(CastSocket::ReadState state) { | |
| 115 switch (state) { | |
| 116 case CastSocket::READ_STATE_NONE: | |
| 117 return proto::READ_STATE_NONE; | |
| 118 case CastSocket::READ_STATE_READ: | |
| 119 return proto::READ_STATE_READ; | |
| 120 case CastSocket::READ_STATE_READ_COMPLETE: | |
| 121 return proto::READ_STATE_READ_COMPLETE; | |
| 122 case CastSocket::READ_STATE_DO_CALLBACK: | |
| 123 return proto::READ_STATE_DO_CALLBACK; | |
| 124 case CastSocket::READ_STATE_ERROR: | |
| 125 return proto::READ_STATE_ERROR; | |
| 126 default: | |
| 127 NOTREACHED(); | |
| 128 return proto::READ_STATE_NONE; | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 proto::WriteState WriteStateToProto(CastSocket::WriteState state) { | |
| 133 switch (state) { | |
| 134 case CastSocket::WRITE_STATE_NONE: | |
| 135 return proto::WRITE_STATE_NONE; | |
| 136 case CastSocket::WRITE_STATE_WRITE: | |
| 137 return proto::WRITE_STATE_WRITE; | |
| 138 case CastSocket::WRITE_STATE_WRITE_COMPLETE: | |
| 139 return proto::WRITE_STATE_WRITE_COMPLETE; | |
| 140 case CastSocket::WRITE_STATE_DO_CALLBACK: | |
| 141 return proto::WRITE_STATE_DO_CALLBACK; | |
| 142 case CastSocket::WRITE_STATE_ERROR: | |
| 143 return proto::WRITE_STATE_ERROR; | |
| 144 default: | |
| 145 NOTREACHED(); | |
| 146 return proto::WRITE_STATE_NONE; | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 proto::ErrorState ErrorStateToProto(ChannelError state) { | |
| 151 switch (state) { | |
| 152 case CHANNEL_ERROR_NONE: | |
| 153 return proto::CHANNEL_ERROR_NONE; | |
| 154 case CHANNEL_ERROR_CHANNEL_NOT_OPEN: | |
| 155 return proto::CHANNEL_ERROR_CHANNEL_NOT_OPEN; | |
| 156 case CHANNEL_ERROR_AUTHENTICATION_ERROR: | |
| 157 return proto::CHANNEL_ERROR_AUTHENTICATION_ERROR; | |
| 158 case CHANNEL_ERROR_CONNECT_ERROR: | |
| 159 return proto::CHANNEL_ERROR_CONNECT_ERROR; | |
| 160 case CHANNEL_ERROR_SOCKET_ERROR: | |
| 161 return proto::CHANNEL_ERROR_SOCKET_ERROR; | |
| 162 case CHANNEL_ERROR_TRANSPORT_ERROR: | |
| 163 return proto::CHANNEL_ERROR_TRANSPORT_ERROR; | |
| 164 case CHANNEL_ERROR_INVALID_MESSAGE: | |
| 165 return proto::CHANNEL_ERROR_INVALID_MESSAGE; | |
| 166 case CHANNEL_ERROR_INVALID_CHANNEL_ID: | |
| 167 return proto::CHANNEL_ERROR_INVALID_CHANNEL_ID; | |
| 168 case CHANNEL_ERROR_CONNECT_TIMEOUT: | |
| 169 return proto::CHANNEL_ERROR_CONNECT_TIMEOUT; | |
| 170 case CHANNEL_ERROR_UNKNOWN: | |
| 171 return proto::CHANNEL_ERROR_UNKNOWN; | |
| 172 default: | |
| 173 NOTREACHED(); | |
| 174 return proto::CHANNEL_ERROR_NONE; | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 } // namespace | |
| 179 | |
| 180 CastSocket::CastSocket(const std::string& owner_extension_id, | 69 CastSocket::CastSocket(const std::string& owner_extension_id, |
| 181 const net::IPEndPoint& ip_endpoint, | 70 const net::IPEndPoint& ip_endpoint, |
| 182 ChannelAuthType channel_auth, | 71 ChannelAuthType channel_auth, |
| 183 CastSocket::Delegate* delegate, | 72 CastSocket::Delegate* delegate, |
| 184 net::NetLog* net_log, | 73 net::NetLog* net_log, |
| 185 const base::TimeDelta& timeout, | 74 const base::TimeDelta& timeout, |
| 186 const scoped_refptr<Logger>& logger) | 75 const scoped_refptr<Logger>& logger) |
| 187 : ApiResource(owner_extension_id), | 76 : ApiResource(owner_extension_id), |
| 188 channel_id_(0), | 77 channel_id_(0), |
| 189 ip_endpoint_(ip_endpoint), | 78 ip_endpoint_(ip_endpoint), |
| 190 channel_auth_(channel_auth), | 79 channel_auth_(channel_auth), |
| 191 delegate_(delegate), | 80 delegate_(delegate), |
| 192 net_log_(net_log), | 81 net_log_(net_log), |
| 193 logger_(logger), | 82 logger_(logger), |
| 194 connect_timeout_(timeout), | 83 connect_timeout_(timeout), |
| 195 connect_timeout_timer_(new base::OneShotTimer<CastSocket>), | 84 connect_timeout_timer_(new base::OneShotTimer<CastSocket>), |
| 196 is_canceled_(false), | 85 is_canceled_(false), |
| 197 connect_state_(CONN_STATE_NONE), | 86 connect_state_(proto::CONN_STATE_NONE), |
| 198 write_state_(WRITE_STATE_NONE), | 87 write_state_(proto::WRITE_STATE_NONE), |
| 199 read_state_(READ_STATE_NONE), | 88 read_state_(proto::READ_STATE_NONE), |
| 200 error_state_(CHANNEL_ERROR_NONE), | 89 error_state_(CHANNEL_ERROR_NONE), |
| 201 ready_state_(READY_STATE_NONE) { | 90 ready_state_(READY_STATE_NONE) { |
| 202 DCHECK(net_log_); | 91 DCHECK(net_log_); |
| 203 DCHECK(channel_auth_ == CHANNEL_AUTH_TYPE_SSL || | 92 DCHECK(channel_auth_ == CHANNEL_AUTH_TYPE_SSL || |
| 204 channel_auth_ == CHANNEL_AUTH_TYPE_SSL_VERIFIED); | 93 channel_auth_ == CHANNEL_AUTH_TYPE_SSL_VERIFIED); |
| 205 net_log_source_.type = net::NetLog::SOURCE_SOCKET; | 94 net_log_source_.type = net::NetLog::SOURCE_SOCKET; |
| 206 net_log_source_.id = net_log_->NextID(); | 95 net_log_source_.id = net_log_->NextID(); |
| 207 | 96 |
| 208 // Buffer is reused across messages. | 97 // Buffer is reused across messages. |
| 209 read_buffer_ = new net::GrowableIOBuffer(); | 98 read_buffer_ = new net::GrowableIOBuffer(); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 VLOG_WITH_CONNECTION(1) << "Connect readyState = " << ready_state_; | 186 VLOG_WITH_CONNECTION(1) << "Connect readyState = " << ready_state_; |
| 298 if (ready_state_ != READY_STATE_NONE) { | 187 if (ready_state_ != READY_STATE_NONE) { |
| 299 logger_->LogSocketEventWithDetails( | 188 logger_->LogSocketEventWithDetails( |
| 300 channel_id_, proto::CONNECT_FAILED, "ReadyState not NONE"); | 189 channel_id_, proto::CONNECT_FAILED, "ReadyState not NONE"); |
| 301 callback.Run(net::ERR_CONNECTION_FAILED); | 190 callback.Run(net::ERR_CONNECTION_FAILED); |
| 302 return; | 191 return; |
| 303 } | 192 } |
| 304 | 193 |
| 305 connect_callback_ = callback; | 194 connect_callback_ = callback; |
| 306 SetReadyState(READY_STATE_CONNECTING); | 195 SetReadyState(READY_STATE_CONNECTING); |
| 307 SetConnectState(CONN_STATE_TCP_CONNECT); | 196 SetConnectState(proto::CONN_STATE_TCP_CONNECT); |
| 308 | 197 |
| 309 if (connect_timeout_.InMicroseconds() > 0) { | 198 if (connect_timeout_.InMicroseconds() > 0) { |
| 310 DCHECK(connect_timeout_callback_.IsCancelled()); | 199 DCHECK(connect_timeout_callback_.IsCancelled()); |
| 311 connect_timeout_callback_.Reset( | 200 connect_timeout_callback_.Reset( |
| 312 base::Bind(&CastSocket::OnConnectTimeout, base::Unretained(this))); | 201 base::Bind(&CastSocket::OnConnectTimeout, base::Unretained(this))); |
| 313 GetTimer()->Start(FROM_HERE, | 202 GetTimer()->Start(FROM_HERE, |
| 314 connect_timeout_, | 203 connect_timeout_, |
| 315 connect_timeout_callback_.callback()); | 204 connect_timeout_callback_.callback()); |
| 316 } | 205 } |
| 317 DoConnectLoop(net::OK); | 206 DoConnectLoop(net::OK); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 345 if (is_canceled_) { | 234 if (is_canceled_) { |
| 346 LOG(ERROR) << "CANCELLED - Aborting DoConnectLoop."; | 235 LOG(ERROR) << "CANCELLED - Aborting DoConnectLoop."; |
| 347 return; | 236 return; |
| 348 } | 237 } |
| 349 // Network operations can either finish synchronously or asynchronously. | 238 // Network operations can either finish synchronously or asynchronously. |
| 350 // This method executes the state machine transitions in a loop so that | 239 // This method executes the state machine transitions in a loop so that |
| 351 // correct state transitions happen even when network operations finish | 240 // correct state transitions happen even when network operations finish |
| 352 // synchronously. | 241 // synchronously. |
| 353 int rv = result; | 242 int rv = result; |
| 354 do { | 243 do { |
| 355 ConnectionState state = connect_state_; | 244 proto::ConnectionState state = connect_state_; |
| 356 // Default to CONN_STATE_NONE, which breaks the processing loop if any | 245 // Default to CONN_STATE_NONE, which breaks the processing loop if any |
| 357 // handler fails to transition to another state to continue processing. | 246 // handler fails to transition to another state to continue processing. |
| 358 connect_state_ = CONN_STATE_NONE; | 247 connect_state_ = proto::CONN_STATE_NONE; |
| 359 switch (state) { | 248 switch (state) { |
| 360 case CONN_STATE_TCP_CONNECT: | 249 case proto::CONN_STATE_TCP_CONNECT: |
| 361 rv = DoTcpConnect(); | 250 rv = DoTcpConnect(); |
| 362 break; | 251 break; |
| 363 case CONN_STATE_TCP_CONNECT_COMPLETE: | 252 case proto::CONN_STATE_TCP_CONNECT_COMPLETE: |
| 364 rv = DoTcpConnectComplete(rv); | 253 rv = DoTcpConnectComplete(rv); |
| 365 break; | 254 break; |
| 366 case CONN_STATE_SSL_CONNECT: | 255 case proto::CONN_STATE_SSL_CONNECT: |
| 367 DCHECK_EQ(net::OK, rv); | 256 DCHECK_EQ(net::OK, rv); |
| 368 rv = DoSslConnect(); | 257 rv = DoSslConnect(); |
| 369 break; | 258 break; |
| 370 case CONN_STATE_SSL_CONNECT_COMPLETE: | 259 case proto::CONN_STATE_SSL_CONNECT_COMPLETE: |
| 371 rv = DoSslConnectComplete(rv); | 260 rv = DoSslConnectComplete(rv); |
| 372 break; | 261 break; |
| 373 case CONN_STATE_AUTH_CHALLENGE_SEND: | 262 case proto::CONN_STATE_AUTH_CHALLENGE_SEND: |
| 374 rv = DoAuthChallengeSend(); | 263 rv = DoAuthChallengeSend(); |
| 375 break; | 264 break; |
| 376 case CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE: | 265 case proto::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE: |
| 377 rv = DoAuthChallengeSendComplete(rv); | 266 rv = DoAuthChallengeSendComplete(rv); |
| 378 break; | 267 break; |
| 379 case CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE: | 268 case proto::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE: |
| 380 rv = DoAuthChallengeReplyComplete(rv); | 269 rv = DoAuthChallengeReplyComplete(rv); |
| 381 break; | 270 break; |
| 382 default: | 271 default: |
| 383 NOTREACHED() << "BUG in connect flow. Unknown state: " << state; | 272 NOTREACHED() << "BUG in connect flow. Unknown state: " << state; |
| 384 break; | 273 break; |
| 385 } | 274 } |
| 386 } while (rv != net::ERR_IO_PENDING && connect_state_ != CONN_STATE_NONE); | 275 } while (rv != net::ERR_IO_PENDING && |
| 387 // Get out of the loop either when: | 276 connect_state_ != proto::CONN_STATE_NONE); |
| 388 // a. A network operation is pending, OR | 277 // Get out of the loop either when: // a. A network operation is pending, OR |
| 389 // b. The Do* method called did not change state | 278 // b. The Do* method called did not change state |
| 390 | 279 |
| 391 // No state change occurred in do-while loop above. This means state has | 280 // No state change occurred in do-while loop above. This means state has |
| 392 // transitioned to NONE. | 281 // transitioned to NONE. |
| 393 if (connect_state_ == CONN_STATE_NONE) { | 282 if (connect_state_ == proto::CONN_STATE_NONE) { |
| 394 logger_->LogSocketConnectState(channel_id_, | 283 logger_->LogSocketConnectState(channel_id_, connect_state_); |
| 395 ConnectStateToProto(connect_state_)); | |
| 396 } | 284 } |
| 397 | 285 |
| 398 // Connect loop is finished: if there is no pending IO invoke the callback. | 286 // Connect loop is finished: if there is no pending IO invoke the callback. |
| 399 if (rv != net::ERR_IO_PENDING) { | 287 if (rv != net::ERR_IO_PENDING) { |
| 400 GetTimer()->Stop(); | 288 GetTimer()->Stop(); |
| 401 DoConnectCallback(rv); | 289 DoConnectCallback(rv); |
| 402 } | 290 } |
| 403 } | 291 } |
| 404 | 292 |
| 405 int CastSocket::DoTcpConnect() { | 293 int CastSocket::DoTcpConnect() { |
| 406 DCHECK(connect_loop_callback_.IsCancelled()); | 294 DCHECK(connect_loop_callback_.IsCancelled()); |
| 407 VLOG_WITH_CONNECTION(1) << "DoTcpConnect"; | 295 VLOG_WITH_CONNECTION(1) << "DoTcpConnect"; |
| 408 SetConnectState(CONN_STATE_TCP_CONNECT_COMPLETE); | 296 SetConnectState(proto::CONN_STATE_TCP_CONNECT_COMPLETE); |
| 409 tcp_socket_ = CreateTcpSocket(); | 297 tcp_socket_ = CreateTcpSocket(); |
| 410 | 298 |
| 411 int rv = tcp_socket_->Connect( | 299 int rv = tcp_socket_->Connect( |
| 412 base::Bind(&CastSocket::DoConnectLoop, base::Unretained(this))); | 300 base::Bind(&CastSocket::DoConnectLoop, base::Unretained(this))); |
| 413 logger_->LogSocketEventWithRv(channel_id_, proto::TCP_SOCKET_CONNECT, rv); | 301 logger_->LogSocketEventWithRv(channel_id_, proto::TCP_SOCKET_CONNECT, rv); |
| 414 return rv; | 302 return rv; |
| 415 } | 303 } |
| 416 | 304 |
| 417 int CastSocket::DoTcpConnectComplete(int result) { | 305 int CastSocket::DoTcpConnectComplete(int result) { |
| 418 VLOG_WITH_CONNECTION(1) << "DoTcpConnectComplete: " << result; | 306 VLOG_WITH_CONNECTION(1) << "DoTcpConnectComplete: " << result; |
| 419 if (result == net::OK) { | 307 if (result == net::OK) { |
| 420 // Enable TCP protocol-level keep-alive. | 308 // Enable TCP protocol-level keep-alive. |
| 421 bool result = tcp_socket_->SetKeepAlive(true, kTcpKeepAliveDelaySecs); | 309 bool result = tcp_socket_->SetKeepAlive(true, kTcpKeepAliveDelaySecs); |
| 422 LOG_IF(WARNING, !result) << "Failed to SetKeepAlive."; | 310 LOG_IF(WARNING, !result) << "Failed to SetKeepAlive."; |
| 423 logger_->LogSocketEventWithRv( | 311 logger_->LogSocketEventWithRv( |
| 424 channel_id_, proto::TCP_SOCKET_SET_KEEP_ALIVE, result ? 1 : 0); | 312 channel_id_, proto::TCP_SOCKET_SET_KEEP_ALIVE, result ? 1 : 0); |
| 425 SetConnectState(CONN_STATE_SSL_CONNECT); | 313 SetConnectState(proto::CONN_STATE_SSL_CONNECT); |
| 426 } | 314 } |
| 427 return result; | 315 return result; |
| 428 } | 316 } |
| 429 | 317 |
| 430 int CastSocket::DoSslConnect() { | 318 int CastSocket::DoSslConnect() { |
| 431 DCHECK(connect_loop_callback_.IsCancelled()); | 319 DCHECK(connect_loop_callback_.IsCancelled()); |
| 432 VLOG_WITH_CONNECTION(1) << "DoSslConnect"; | 320 VLOG_WITH_CONNECTION(1) << "DoSslConnect"; |
| 433 SetConnectState(CONN_STATE_SSL_CONNECT_COMPLETE); | 321 SetConnectState(proto::CONN_STATE_SSL_CONNECT_COMPLETE); |
| 434 socket_ = CreateSslSocket(tcp_socket_.PassAs<net::StreamSocket>()); | 322 socket_ = CreateSslSocket(tcp_socket_.PassAs<net::StreamSocket>()); |
| 435 | 323 |
| 436 int rv = socket_->Connect( | 324 int rv = socket_->Connect( |
| 437 base::Bind(&CastSocket::DoConnectLoop, base::Unretained(this))); | 325 base::Bind(&CastSocket::DoConnectLoop, base::Unretained(this))); |
| 438 logger_->LogSocketEventWithRv(channel_id_, proto::SSL_SOCKET_CONNECT, rv); | 326 logger_->LogSocketEventWithRv(channel_id_, proto::SSL_SOCKET_CONNECT, rv); |
| 439 return rv; | 327 return rv; |
| 440 } | 328 } |
| 441 | 329 |
| 442 int CastSocket::DoSslConnectComplete(int result) { | 330 int CastSocket::DoSslConnectComplete(int result) { |
| 443 VLOG_WITH_CONNECTION(1) << "DoSslConnectComplete: " << result; | 331 VLOG_WITH_CONNECTION(1) << "DoSslConnectComplete: " << result; |
| 444 if (result == net::ERR_CERT_AUTHORITY_INVALID && | 332 if (result == net::ERR_CERT_AUTHORITY_INVALID && |
| 445 peer_cert_.empty() && ExtractPeerCert(&peer_cert_)) { | 333 peer_cert_.empty() && ExtractPeerCert(&peer_cert_)) { |
| 446 SetConnectState(CONN_STATE_TCP_CONNECT); | 334 SetConnectState(proto::CONN_STATE_TCP_CONNECT); |
| 447 } else if (result == net::OK && | 335 } else if (result == net::OK && |
| 448 channel_auth_ == CHANNEL_AUTH_TYPE_SSL_VERIFIED) { | 336 channel_auth_ == CHANNEL_AUTH_TYPE_SSL_VERIFIED) { |
| 449 SetConnectState(CONN_STATE_AUTH_CHALLENGE_SEND); | 337 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_SEND); |
| 450 } | 338 } |
| 451 return result; | 339 return result; |
| 452 } | 340 } |
| 453 | 341 |
| 454 int CastSocket::DoAuthChallengeSend() { | 342 int CastSocket::DoAuthChallengeSend() { |
| 455 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeSend"; | 343 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeSend"; |
| 456 SetConnectState(CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE); | 344 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE); |
| 457 | 345 |
| 458 CastMessage challenge_message; | 346 CastMessage challenge_message; |
| 459 CreateAuthChallengeMessage(&challenge_message); | 347 CreateAuthChallengeMessage(&challenge_message); |
| 460 VLOG_WITH_CONNECTION(1) << "Sending challenge: " | 348 VLOG_WITH_CONNECTION(1) << "Sending challenge: " |
| 461 << CastMessageToString(challenge_message); | 349 << CastMessageToString(challenge_message); |
| 462 // Post a task to send auth challenge so that DoWriteLoop is not nested inside | 350 // Post a task to send auth challenge so that DoWriteLoop is not nested inside |
| 463 // DoConnectLoop. This is not strictly necessary but keeps the write loop | 351 // DoConnectLoop. This is not strictly necessary but keeps the write loop |
| 464 // code decoupled from connect loop code. | 352 // code decoupled from connect loop code. |
| 465 DCHECK(send_auth_challenge_callback_.IsCancelled()); | 353 DCHECK(send_auth_challenge_callback_.IsCancelled()); |
| 466 send_auth_challenge_callback_.Reset( | 354 send_auth_challenge_callback_.Reset( |
| (...skipping 15 matching lines...) Expand all Loading... |
| 482 DCHECK_GT(result, 0); | 370 DCHECK_GT(result, 0); |
| 483 DCHECK_EQ(write_queue_.size(), 1UL); | 371 DCHECK_EQ(write_queue_.size(), 1UL); |
| 484 PostTaskToStartConnectLoop(result); | 372 PostTaskToStartConnectLoop(result); |
| 485 } | 373 } |
| 486 | 374 |
| 487 int CastSocket::DoAuthChallengeSendComplete(int result) { | 375 int CastSocket::DoAuthChallengeSendComplete(int result) { |
| 488 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeSendComplete: " << result; | 376 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeSendComplete: " << result; |
| 489 if (result < 0) { | 377 if (result < 0) { |
| 490 return result; | 378 return result; |
| 491 } | 379 } |
| 492 SetConnectState(CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE); | 380 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE); |
| 493 | 381 |
| 494 // Post a task to start read loop so that DoReadLoop is not nested inside | 382 // Post a task to start read loop so that DoReadLoop is not nested inside |
| 495 // DoConnectLoop. This is not strictly necessary but keeps the read loop | 383 // DoConnectLoop. This is not strictly necessary but keeps the read loop |
| 496 // code decoupled from connect loop code. | 384 // code decoupled from connect loop code. |
| 497 PostTaskToStartReadLoop(); | 385 PostTaskToStartReadLoop(); |
| 498 // Always return IO_PENDING since the result is always asynchronous. | 386 // Always return IO_PENDING since the result is always asynchronous. |
| 499 return net::ERR_IO_PENDING; | 387 return net::ERR_IO_PENDING; |
| 500 } | 388 } |
| 501 | 389 |
| 502 int CastSocket::DoAuthChallengeReplyComplete(int result) { | 390 int CastSocket::DoAuthChallengeReplyComplete(int result) { |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 608 callback.Run(net::ERR_FAILED); | 496 callback.Run(net::ERR_FAILED); |
| 609 return; | 497 return; |
| 610 } | 498 } |
| 611 | 499 |
| 612 write_queue_.push(write_request); | 500 write_queue_.push(write_request); |
| 613 logger_->LogSocketEventForMessage( | 501 logger_->LogSocketEventForMessage( |
| 614 channel_id_, | 502 channel_id_, |
| 615 proto::MESSAGE_ENQUEUED, | 503 proto::MESSAGE_ENQUEUED, |
| 616 message.namespace_(), | 504 message.namespace_(), |
| 617 base::StringPrintf("Queue size: %" PRIuS, write_queue_.size())); | 505 base::StringPrintf("Queue size: %" PRIuS, write_queue_.size())); |
| 618 if (write_state_ == WRITE_STATE_NONE) { | 506 if (write_state_ == proto::WRITE_STATE_NONE) { |
| 619 SetWriteState(WRITE_STATE_WRITE); | 507 SetWriteState(proto::WRITE_STATE_WRITE); |
| 620 DoWriteLoop(net::OK); | 508 DoWriteLoop(net::OK); |
| 621 } | 509 } |
| 622 } | 510 } |
| 623 | 511 |
| 624 void CastSocket::DoWriteLoop(int result) { | 512 void CastSocket::DoWriteLoop(int result) { |
| 625 DCHECK(CalledOnValidThread()); | 513 DCHECK(CalledOnValidThread()); |
| 626 VLOG_WITH_CONNECTION(1) << "DoWriteLoop queue size: " << write_queue_.size(); | 514 VLOG_WITH_CONNECTION(1) << "DoWriteLoop queue size: " << write_queue_.size(); |
| 627 | 515 |
| 628 if (write_queue_.empty()) { | 516 if (write_queue_.empty()) { |
| 629 SetWriteState(WRITE_STATE_NONE); | 517 SetWriteState(proto::WRITE_STATE_NONE); |
| 630 return; | 518 return; |
| 631 } | 519 } |
| 632 | 520 |
| 633 // Network operations can either finish synchronously or asynchronously. | 521 // Network operations can either finish synchronously or asynchronously. |
| 634 // This method executes the state machine transitions in a loop so that | 522 // This method executes the state machine transitions in a loop so that |
| 635 // write state transitions happen even when network operations finish | 523 // write state transitions happen even when network operations finish |
| 636 // synchronously. | 524 // synchronously. |
| 637 int rv = result; | 525 int rv = result; |
| 638 do { | 526 do { |
| 639 WriteState state = write_state_; | 527 proto::WriteState state = write_state_; |
| 640 write_state_ = WRITE_STATE_NONE; | 528 write_state_ = proto::WRITE_STATE_NONE; |
| 641 switch (state) { | 529 switch (state) { |
| 642 case WRITE_STATE_WRITE: | 530 case proto::WRITE_STATE_WRITE: |
| 643 rv = DoWrite(); | 531 rv = DoWrite(); |
| 644 break; | 532 break; |
| 645 case WRITE_STATE_WRITE_COMPLETE: | 533 case proto::WRITE_STATE_WRITE_COMPLETE: |
| 646 rv = DoWriteComplete(rv); | 534 rv = DoWriteComplete(rv); |
| 647 break; | 535 break; |
| 648 case WRITE_STATE_DO_CALLBACK: | 536 case proto::WRITE_STATE_DO_CALLBACK: |
| 649 rv = DoWriteCallback(); | 537 rv = DoWriteCallback(); |
| 650 break; | 538 break; |
| 651 case WRITE_STATE_ERROR: | 539 case proto::WRITE_STATE_ERROR: |
| 652 rv = DoWriteError(rv); | 540 rv = DoWriteError(rv); |
| 653 break; | 541 break; |
| 654 default: | 542 default: |
| 655 NOTREACHED() << "BUG in write flow. Unknown state: " << state; | 543 NOTREACHED() << "BUG in write flow. Unknown state: " << state; |
| 656 break; | 544 break; |
| 657 } | 545 } |
| 658 } while (!write_queue_.empty() && | 546 } while (!write_queue_.empty() && rv != net::ERR_IO_PENDING && |
| 659 rv != net::ERR_IO_PENDING && | 547 write_state_ != proto::WRITE_STATE_NONE); |
| 660 write_state_ != WRITE_STATE_NONE); | |
| 661 | 548 |
| 662 // No state change occurred in do-while loop above. This means state has | 549 // No state change occurred in do-while loop above. This means state has |
| 663 // transitioned to NONE. | 550 // transitioned to NONE. |
| 664 if (write_state_ == WRITE_STATE_NONE) { | 551 if (write_state_ == proto::WRITE_STATE_NONE) { |
| 665 logger_->LogSocketWriteState(channel_id_, WriteStateToProto(write_state_)); | 552 logger_->LogSocketWriteState(channel_id_, write_state_); |
| 666 } | 553 } |
| 667 | 554 |
| 668 // If write loop is done because the queue is empty then set write | 555 // If write loop is done because the queue is empty then set write |
| 669 // state to NONE | 556 // state to NONE |
| 670 if (write_queue_.empty()) { | 557 if (write_queue_.empty()) { |
| 671 SetWriteState(WRITE_STATE_NONE); | 558 SetWriteState(proto::WRITE_STATE_NONE); |
| 672 } | 559 } |
| 673 | 560 |
| 674 // Write loop is done - if the result is ERR_FAILED then close with error. | 561 // Write loop is done - if the result is ERR_FAILED then close with error. |
| 675 if (rv == net::ERR_FAILED) { | 562 if (rv == net::ERR_FAILED) { |
| 676 CloseWithError(); | 563 CloseWithError(); |
| 677 } | 564 } |
| 678 } | 565 } |
| 679 | 566 |
| 680 int CastSocket::DoWrite() { | 567 int CastSocket::DoWrite() { |
| 681 DCHECK(!write_queue_.empty()); | 568 DCHECK(!write_queue_.empty()); |
| 682 WriteRequest& request = write_queue_.front(); | 569 WriteRequest& request = write_queue_.front(); |
| 683 | 570 |
| 684 VLOG_WITH_CONNECTION(2) << "WriteData byte_count = " | 571 VLOG_WITH_CONNECTION(2) << "WriteData byte_count = " |
| 685 << request.io_buffer->size() << " bytes_written " | 572 << request.io_buffer->size() << " bytes_written " |
| 686 << request.io_buffer->BytesConsumed(); | 573 << request.io_buffer->BytesConsumed(); |
| 687 | 574 |
| 688 SetWriteState(WRITE_STATE_WRITE_COMPLETE); | 575 SetWriteState(proto::WRITE_STATE_WRITE_COMPLETE); |
| 689 | 576 |
| 690 int rv = socket_->Write( | 577 int rv = socket_->Write( |
| 691 request.io_buffer.get(), | 578 request.io_buffer.get(), |
| 692 request.io_buffer->BytesRemaining(), | 579 request.io_buffer->BytesRemaining(), |
| 693 base::Bind(&CastSocket::DoWriteLoop, base::Unretained(this))); | 580 base::Bind(&CastSocket::DoWriteLoop, base::Unretained(this))); |
| 694 logger_->LogSocketEventWithRv(channel_id_, proto::SOCKET_WRITE, rv); | 581 logger_->LogSocketEventWithRv(channel_id_, proto::SOCKET_WRITE, rv); |
| 695 | 582 |
| 696 return rv; | 583 return rv; |
| 697 } | 584 } |
| 698 | 585 |
| 699 int CastSocket::DoWriteComplete(int result) { | 586 int CastSocket::DoWriteComplete(int result) { |
| 700 DCHECK(!write_queue_.empty()); | 587 DCHECK(!write_queue_.empty()); |
| 701 if (result <= 0) { // NOTE that 0 also indicates an error | 588 if (result <= 0) { // NOTE that 0 also indicates an error |
| 702 SetErrorState(CHANNEL_ERROR_SOCKET_ERROR); | 589 SetErrorState(CHANNEL_ERROR_SOCKET_ERROR); |
| 703 SetWriteState(WRITE_STATE_ERROR); | 590 SetWriteState(proto::WRITE_STATE_ERROR); |
| 704 return result == 0 ? net::ERR_FAILED : result; | 591 return result == 0 ? net::ERR_FAILED : result; |
| 705 } | 592 } |
| 706 | 593 |
| 707 // Some bytes were successfully written | 594 // Some bytes were successfully written |
| 708 WriteRequest& request = write_queue_.front(); | 595 WriteRequest& request = write_queue_.front(); |
| 709 scoped_refptr<net::DrainableIOBuffer> io_buffer = request.io_buffer; | 596 scoped_refptr<net::DrainableIOBuffer> io_buffer = request.io_buffer; |
| 710 io_buffer->DidConsume(result); | 597 io_buffer->DidConsume(result); |
| 711 if (io_buffer->BytesRemaining() == 0) { // Message fully sent | 598 if (io_buffer->BytesRemaining() == 0) { // Message fully sent |
| 712 SetWriteState(WRITE_STATE_DO_CALLBACK); | 599 SetWriteState(proto::WRITE_STATE_DO_CALLBACK); |
| 713 } else { | 600 } else { |
| 714 SetWriteState(WRITE_STATE_WRITE); | 601 SetWriteState(proto::WRITE_STATE_WRITE); |
| 715 } | 602 } |
| 716 | 603 |
| 717 return net::OK; | 604 return net::OK; |
| 718 } | 605 } |
| 719 | 606 |
| 720 int CastSocket::DoWriteCallback() { | 607 int CastSocket::DoWriteCallback() { |
| 721 DCHECK(!write_queue_.empty()); | 608 DCHECK(!write_queue_.empty()); |
| 722 | 609 |
| 723 SetWriteState(WRITE_STATE_WRITE); | 610 SetWriteState(proto::WRITE_STATE_WRITE); |
| 724 | 611 |
| 725 WriteRequest& request = write_queue_.front(); | 612 WriteRequest& request = write_queue_.front(); |
| 726 int bytes_consumed = request.io_buffer->BytesConsumed(); | 613 int bytes_consumed = request.io_buffer->BytesConsumed(); |
| 727 logger_->LogSocketEventForMessage( | 614 logger_->LogSocketEventForMessage( |
| 728 channel_id_, | 615 channel_id_, |
| 729 proto::MESSAGE_WRITTEN, | 616 proto::MESSAGE_WRITTEN, |
| 730 request.message_namespace, | 617 request.message_namespace, |
| 731 base::StringPrintf("Bytes: %d", bytes_consumed)); | 618 base::StringPrintf("Bytes: %d", bytes_consumed)); |
| 732 request.callback.Run(bytes_consumed); | 619 request.callback.Run(bytes_consumed); |
| 733 write_queue_.pop(); | 620 write_queue_.pop(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 762 DCHECK(read_loop_callback_.IsCancelled()); | 649 DCHECK(read_loop_callback_.IsCancelled()); |
| 763 read_loop_callback_.Reset(base::Bind(&CastSocket::StartReadLoop, | 650 read_loop_callback_.Reset(base::Bind(&CastSocket::StartReadLoop, |
| 764 base::Unretained(this))); | 651 base::Unretained(this))); |
| 765 base::MessageLoop::current()->PostTask(FROM_HERE, | 652 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 766 read_loop_callback_.callback()); | 653 read_loop_callback_.callback()); |
| 767 } | 654 } |
| 768 | 655 |
| 769 void CastSocket::StartReadLoop() { | 656 void CastSocket::StartReadLoop() { |
| 770 read_loop_callback_.Cancel(); | 657 read_loop_callback_.Cancel(); |
| 771 // Read loop would have already been started if read state is not NONE | 658 // Read loop would have already been started if read state is not NONE |
| 772 if (read_state_ == READ_STATE_NONE) { | 659 if (read_state_ == proto::READ_STATE_NONE) { |
| 773 SetReadState(READ_STATE_READ); | 660 SetReadState(proto::READ_STATE_READ); |
| 774 DoReadLoop(net::OK); | 661 DoReadLoop(net::OK); |
| 775 } | 662 } |
| 776 } | 663 } |
| 777 | 664 |
| 778 void CastSocket::DoReadLoop(int result) { | 665 void CastSocket::DoReadLoop(int result) { |
| 779 DCHECK(CalledOnValidThread()); | 666 DCHECK(CalledOnValidThread()); |
| 780 // Network operations can either finish synchronously or asynchronously. | 667 // Network operations can either finish synchronously or asynchronously. |
| 781 // This method executes the state machine transitions in a loop so that | 668 // This method executes the state machine transitions in a loop so that |
| 782 // write state transitions happen even when network operations finish | 669 // write state transitions happen even when network operations finish |
| 783 // synchronously. | 670 // synchronously. |
| 784 int rv = result; | 671 int rv = result; |
| 785 do { | 672 do { |
| 786 ReadState state = read_state_; | 673 proto::ReadState state = read_state_; |
| 787 read_state_ = READ_STATE_NONE; | 674 read_state_ = proto::READ_STATE_NONE; |
| 788 | 675 |
| 789 switch (state) { | 676 switch (state) { |
| 790 case READ_STATE_READ: | 677 case proto::READ_STATE_READ: |
| 791 rv = DoRead(); | 678 rv = DoRead(); |
| 792 break; | 679 break; |
| 793 case READ_STATE_READ_COMPLETE: | 680 case proto::READ_STATE_READ_COMPLETE: |
| 794 rv = DoReadComplete(rv); | 681 rv = DoReadComplete(rv); |
| 795 break; | 682 break; |
| 796 case READ_STATE_DO_CALLBACK: | 683 case proto::READ_STATE_DO_CALLBACK: |
| 797 rv = DoReadCallback(); | 684 rv = DoReadCallback(); |
| 798 break; | 685 break; |
| 799 case READ_STATE_ERROR: | 686 case proto::READ_STATE_ERROR: |
| 800 rv = DoReadError(rv); | 687 rv = DoReadError(rv); |
| 801 DCHECK_EQ(read_state_, READ_STATE_NONE); | 688 DCHECK_EQ(read_state_, proto::READ_STATE_NONE); |
| 802 break; | 689 break; |
| 803 default: | 690 default: |
| 804 NOTREACHED() << "BUG in read flow. Unknown state: " << state; | 691 NOTREACHED() << "BUG in read flow. Unknown state: " << state; |
| 805 break; | 692 break; |
| 806 } | 693 } |
| 807 } while (rv != net::ERR_IO_PENDING && read_state_ != READ_STATE_NONE); | 694 } while (rv != net::ERR_IO_PENDING && read_state_ != proto::READ_STATE_NONE); |
| 808 | 695 |
| 809 // No state change occurred in do-while loop above. This means state has | 696 // No state change occurred in do-while loop above. This means state has |
| 810 // transitioned to NONE. | 697 // transitioned to NONE. |
| 811 if (read_state_ == READ_STATE_NONE) { | 698 if (read_state_ == proto::READ_STATE_NONE) { |
| 812 logger_->LogSocketReadState(channel_id_, ReadStateToProto(read_state_)); | 699 logger_->LogSocketReadState(channel_id_, read_state_); |
| 813 } | 700 } |
| 814 | 701 |
| 815 if (rv == net::ERR_FAILED) { | 702 if (rv == net::ERR_FAILED) { |
| 816 if (ready_state_ == READY_STATE_CONNECTING) { | 703 if (ready_state_ == READY_STATE_CONNECTING) { |
| 817 // Read errors during the handshake should notify the caller via the | 704 // Read errors during the handshake should notify the caller via the |
| 818 // connect callback. This will also send error status via the OnError | 705 // connect callback. This will also send error status via the OnError |
| 819 // delegate. | 706 // delegate. |
| 820 PostTaskToStartConnectLoop(net::ERR_FAILED); | 707 PostTaskToStartConnectLoop(net::ERR_FAILED); |
| 821 } else { | 708 } else { |
| 822 // Connection is already established. Close and send error status via the | 709 // Connection is already established. Close and send error status via the |
| 823 // OnError delegate. | 710 // OnError delegate. |
| 824 CloseWithError(); | 711 CloseWithError(); |
| 825 } | 712 } |
| 826 } | 713 } |
| 827 } | 714 } |
| 828 | 715 |
| 829 int CastSocket::DoRead() { | 716 int CastSocket::DoRead() { |
| 830 SetReadState(READ_STATE_READ_COMPLETE); | 717 SetReadState(proto::READ_STATE_READ_COMPLETE); |
| 831 | 718 |
| 832 // Determine how many bytes need to be read. | 719 // Determine how many bytes need to be read. |
| 833 size_t num_bytes_to_read = framer_->BytesRequested(); | 720 size_t num_bytes_to_read = framer_->BytesRequested(); |
| 834 | 721 |
| 835 // Read up to num_bytes_to_read into |current_read_buffer_|. | 722 // Read up to num_bytes_to_read into |current_read_buffer_|. |
| 836 int rv = socket_->Read( | 723 int rv = socket_->Read( |
| 837 read_buffer_.get(), | 724 read_buffer_.get(), |
| 838 base::checked_cast<uint32>(num_bytes_to_read), | 725 base::checked_cast<uint32>(num_bytes_to_read), |
| 839 base::Bind(&CastSocket::DoReadLoop, base::Unretained(this))); | 726 base::Bind(&CastSocket::DoReadLoop, base::Unretained(this))); |
| 840 logger_->LogSocketEventWithRv(channel_id_, proto::SOCKET_READ, rv); | 727 logger_->LogSocketEventWithRv(channel_id_, proto::SOCKET_READ, rv); |
| 841 | 728 |
| 842 return rv; | 729 return rv; |
| 843 } | 730 } |
| 844 | 731 |
| 845 int CastSocket::DoReadComplete(int result) { | 732 int CastSocket::DoReadComplete(int result) { |
| 846 VLOG_WITH_CONNECTION(2) << "DoReadComplete result = " << result; | 733 VLOG_WITH_CONNECTION(2) << "DoReadComplete result = " << result; |
| 847 | 734 |
| 848 if (result <= 0) { // 0 means EOF: the peer closed the socket | 735 if (result <= 0) { // 0 means EOF: the peer closed the socket |
| 849 VLOG_WITH_CONNECTION(1) << "Read error, peer closed the socket"; | 736 VLOG_WITH_CONNECTION(1) << "Read error, peer closed the socket"; |
| 850 SetErrorState(CHANNEL_ERROR_SOCKET_ERROR); | 737 SetErrorState(CHANNEL_ERROR_SOCKET_ERROR); |
| 851 SetReadState(READ_STATE_ERROR); | 738 SetReadState(proto::READ_STATE_ERROR); |
| 852 return result == 0 ? net::ERR_FAILED : result; | 739 return result == 0 ? net::ERR_FAILED : result; |
| 853 } | 740 } |
| 854 | 741 |
| 855 size_t message_size; | 742 size_t message_size; |
| 856 DCHECK(current_message_.get() == NULL); | 743 DCHECK(current_message_.get() == NULL); |
| 857 current_message_ = framer_->Ingest(result, &message_size, &error_state_); | 744 current_message_ = framer_->Ingest(result, &message_size, &error_state_); |
| 858 if (current_message_.get()) { | 745 if (current_message_.get()) { |
| 859 DCHECK_EQ(error_state_, CHANNEL_ERROR_NONE); | 746 DCHECK_EQ(error_state_, CHANNEL_ERROR_NONE); |
| 860 DCHECK_GT(message_size, static_cast<size_t>(0)); | 747 DCHECK_GT(message_size, static_cast<size_t>(0)); |
| 861 logger_->LogSocketEventForMessage( | 748 logger_->LogSocketEventForMessage( |
| 862 channel_id_, | 749 channel_id_, |
| 863 proto::MESSAGE_READ, | 750 proto::MESSAGE_READ, |
| 864 current_message_->namespace_(), | 751 current_message_->namespace_(), |
| 865 base::StringPrintf("Message size: %u", | 752 base::StringPrintf("Message size: %u", |
| 866 static_cast<uint32>(message_size))); | 753 static_cast<uint32>(message_size))); |
| 867 SetReadState(READ_STATE_DO_CALLBACK); | 754 SetReadState(proto::READ_STATE_DO_CALLBACK); |
| 868 } else if (error_state_ != CHANNEL_ERROR_NONE) { | 755 } else if (error_state_ != CHANNEL_ERROR_NONE) { |
| 869 DCHECK(current_message_.get() == NULL); | 756 DCHECK(current_message_.get() == NULL); |
| 870 SetReadState(READ_STATE_ERROR); | 757 SetReadState(proto::READ_STATE_ERROR); |
| 871 } else { | 758 } else { |
| 872 DCHECK(current_message_.get() == NULL); | 759 DCHECK(current_message_.get() == NULL); |
| 873 SetReadState(READ_STATE_READ); | 760 SetReadState(proto::READ_STATE_READ); |
| 874 } | 761 } |
| 875 return net::OK; | 762 return net::OK; |
| 876 } | 763 } |
| 877 | 764 |
| 878 int CastSocket::DoReadCallback() { | 765 int CastSocket::DoReadCallback() { |
| 879 SetReadState(READ_STATE_READ); | 766 SetReadState(proto::READ_STATE_READ); |
| 880 const CastMessage& message = *current_message_; | 767 const CastMessage& message = *current_message_; |
| 881 if (ready_state_ == READY_STATE_CONNECTING) { | 768 if (ready_state_ == READY_STATE_CONNECTING) { |
| 882 if (IsAuthMessage(message)) { | 769 if (IsAuthMessage(message)) { |
| 883 challenge_reply_.reset(new CastMessage(message)); | 770 challenge_reply_.reset(new CastMessage(message)); |
| 884 logger_->LogSocketEvent(channel_id_, proto::RECEIVED_CHALLENGE_REPLY); | 771 logger_->LogSocketEvent(channel_id_, proto::RECEIVED_CHALLENGE_REPLY); |
| 885 PostTaskToStartConnectLoop(net::OK); | 772 PostTaskToStartConnectLoop(net::OK); |
| 886 current_message_.reset(); | 773 current_message_.reset(); |
| 887 return net::OK; | 774 return net::OK; |
| 888 } else { | 775 } else { |
| 889 SetReadState(READ_STATE_ERROR); | 776 SetReadState(proto::READ_STATE_ERROR); |
| 890 SetErrorState(CHANNEL_ERROR_INVALID_MESSAGE); | 777 SetErrorState(CHANNEL_ERROR_INVALID_MESSAGE); |
| 891 current_message_.reset(); | 778 current_message_.reset(); |
| 892 return net::ERR_INVALID_RESPONSE; | 779 return net::ERR_INVALID_RESPONSE; |
| 893 } | 780 } |
| 894 } | 781 } |
| 895 | 782 |
| 896 MessageInfo message_info; | 783 MessageInfo message_info; |
| 897 if (!CastMessageToMessageInfo(message, &message_info)) { | 784 if (!CastMessageToMessageInfo(message, &message_info)) { |
| 898 current_message_.reset(); | 785 current_message_.reset(); |
| 899 SetReadState(READ_STATE_ERROR); | 786 SetReadState(proto::READ_STATE_ERROR); |
| 900 SetErrorState(CHANNEL_ERROR_INVALID_MESSAGE); | 787 SetErrorState(CHANNEL_ERROR_INVALID_MESSAGE); |
| 901 return net::ERR_INVALID_RESPONSE; | 788 return net::ERR_INVALID_RESPONSE; |
| 902 } | 789 } |
| 903 | 790 |
| 904 logger_->LogSocketEventForMessage(channel_id_, | 791 logger_->LogSocketEventForMessage(channel_id_, |
| 905 proto::NOTIFY_ON_MESSAGE, | 792 proto::NOTIFY_ON_MESSAGE, |
| 906 message.namespace_(), | 793 message.namespace_(), |
| 907 std::string()); | 794 std::string()); |
| 908 delegate_->OnMessage(this, message_info); | 795 delegate_->OnMessage(this, message_info); |
| 909 current_message_.reset(); | 796 current_message_.reset(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 932 } | 819 } |
| 933 | 820 |
| 934 bool CastSocket::CalledOnValidThread() const { | 821 bool CastSocket::CalledOnValidThread() const { |
| 935 return thread_checker_.CalledOnValidThread(); | 822 return thread_checker_.CalledOnValidThread(); |
| 936 } | 823 } |
| 937 | 824 |
| 938 base::Timer* CastSocket::GetTimer() { | 825 base::Timer* CastSocket::GetTimer() { |
| 939 return connect_timeout_timer_.get(); | 826 return connect_timeout_timer_.get(); |
| 940 } | 827 } |
| 941 | 828 |
| 942 void CastSocket::SetConnectState(ConnectionState connect_state) { | 829 void CastSocket::SetConnectState(proto::ConnectionState connect_state) { |
| 943 if (connect_state_ != connect_state) { | 830 if (connect_state_ != connect_state) { |
| 944 connect_state_ = connect_state; | 831 connect_state_ = connect_state; |
| 945 logger_->LogSocketConnectState(channel_id_, | 832 logger_->LogSocketConnectState(channel_id_, connect_state_); |
| 946 ConnectStateToProto(connect_state_)); | |
| 947 } | 833 } |
| 948 } | 834 } |
| 949 | 835 |
| 950 void CastSocket::SetReadyState(ReadyState ready_state) { | 836 void CastSocket::SetReadyState(ReadyState ready_state) { |
| 951 if (ready_state_ != ready_state) { | 837 if (ready_state_ != ready_state) { |
| 952 ready_state_ = ready_state; | 838 ready_state_ = ready_state; |
| 953 logger_->LogSocketReadyState(channel_id_, ReadyStateToProto(ready_state_)); | 839 logger_->LogSocketReadyState(channel_id_, ReadyStateToProto(ready_state_)); |
| 954 } | 840 } |
| 955 } | 841 } |
| 956 | 842 |
| 957 void CastSocket::SetErrorState(ChannelError error_state) { | 843 void CastSocket::SetErrorState(ChannelError error_state) { |
| 958 if (error_state_ != error_state) { | 844 if (error_state_ != error_state) { |
| 959 error_state_ = error_state; | 845 error_state_ = error_state; |
| 960 logger_->LogSocketErrorState(channel_id_, ErrorStateToProto(error_state_)); | 846 logger_->LogSocketErrorState(channel_id_, ErrorStateToProto(error_state_)); |
| 961 } | 847 } |
| 962 } | 848 } |
| 963 | 849 |
| 964 void CastSocket::SetReadState(ReadState read_state) { | 850 void CastSocket::SetReadState(proto::ReadState read_state) { |
| 965 if (read_state_ != read_state) { | 851 if (read_state_ != read_state) { |
| 966 read_state_ = read_state; | 852 read_state_ = read_state; |
| 967 logger_->LogSocketReadState(channel_id_, ReadStateToProto(read_state_)); | 853 logger_->LogSocketReadState(channel_id_, read_state_); |
| 968 } | 854 } |
| 969 } | 855 } |
| 970 | 856 |
| 971 void CastSocket::SetWriteState(WriteState write_state) { | 857 void CastSocket::SetWriteState(proto::WriteState write_state) { |
| 972 if (write_state_ != write_state) { | 858 if (write_state_ != write_state) { |
| 973 write_state_ = write_state; | 859 write_state_ = write_state; |
| 974 logger_->LogSocketWriteState(channel_id_, WriteStateToProto(write_state_)); | 860 logger_->LogSocketWriteState(channel_id_, write_state_); |
| 975 } | 861 } |
| 976 } | 862 } |
| 977 | 863 |
| 978 CastSocket::WriteRequest::WriteRequest(const net::CompletionCallback& callback) | 864 CastSocket::WriteRequest::WriteRequest(const net::CompletionCallback& callback) |
| 979 : callback(callback) { } | 865 : callback(callback) { } |
| 980 | 866 |
| 981 bool CastSocket::WriteRequest::SetContent(const CastMessage& message_proto) { | 867 bool CastSocket::WriteRequest::SetContent(const CastMessage& message_proto) { |
| 982 DCHECK(!io_buffer.get()); | 868 DCHECK(!io_buffer.get()); |
| 983 std::string message_data; | 869 std::string message_data; |
| 984 if (!MessageFramer::Serialize(message_proto, &message_data)) { | 870 if (!MessageFramer::Serialize(message_proto, &message_data)) { |
| 985 return false; | 871 return false; |
| 986 } | 872 } |
| 987 message_namespace = message_proto.namespace_(); | 873 message_namespace = message_proto.namespace_(); |
| 988 io_buffer = new net::DrainableIOBuffer(new net::StringIOBuffer(message_data), | 874 io_buffer = new net::DrainableIOBuffer(new net::StringIOBuffer(message_data), |
| 989 message_data.size()); | 875 message_data.size()); |
| 990 return true; | 876 return true; |
| 991 } | 877 } |
| 992 | 878 |
| 993 CastSocket::WriteRequest::~WriteRequest() { } | 879 CastSocket::WriteRequest::~WriteRequest() { } |
| 994 } // namespace cast_channel | 880 } // namespace cast_channel |
| 995 } // namespace core_api | 881 } // namespace core_api |
| 996 } // namespace extensions | 882 } // namespace extensions |
| 997 | 883 |
| 998 #undef VLOG_WITH_CONNECTION | 884 #undef VLOG_WITH_CONNECTION |
| OLD | NEW |