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