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