Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/extensions/api/cast_channel/cast_socket.h" | 5 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 67 | 67 |
| 68 CastSocket::CastSocket(const std::string& owner_extension_id, | 68 CastSocket::CastSocket(const std::string& owner_extension_id, |
| 69 const GURL& url, | 69 const GURL& url, |
| 70 CastSocket::Delegate* delegate, | 70 CastSocket::Delegate* delegate, |
| 71 net::NetLog* net_log) : | 71 net::NetLog* net_log) : |
| 72 ApiResource(owner_extension_id), | 72 ApiResource(owner_extension_id), |
| 73 channel_id_(0), | 73 channel_id_(0), |
| 74 url_(url), | 74 url_(url), |
| 75 delegate_(delegate), | 75 delegate_(delegate), |
| 76 auth_required_(false), | 76 auth_required_(false), |
| 77 error_state_(CHANNEL_ERROR_NONE), | |
| 78 ready_state_(READY_STATE_NONE), | |
| 79 write_callback_pending_(false), | |
| 80 read_callback_pending_(false), | |
| 81 current_message_size_(0), | 77 current_message_size_(0), |
| 82 net_log_(net_log), | 78 net_log_(net_log), |
| 83 next_state_(CONN_STATE_NONE), | 79 connect_state_(CONN_STATE_NONE), |
| 84 in_connect_loop_(false) { | 80 write_state_(WRITE_STATE_NONE), |
| 81 read_state_(READ_STATE_NONE), | |
| 82 error_state_(CHANNEL_ERROR_NONE), | |
| 83 ready_state_(READY_STATE_NONE) { | |
| 85 DCHECK(net_log_); | 84 DCHECK(net_log_); |
| 86 net_log_source_.type = net::NetLog::SOURCE_SOCKET; | 85 net_log_source_.type = net::NetLog::SOURCE_SOCKET; |
| 87 net_log_source_.id = net_log_->NextID(); | 86 net_log_source_.id = net_log_->NextID(); |
| 88 | 87 |
| 89 // We reuse these buffers for each message. | 88 // We reuse these buffers for each message. |
| 90 header_read_buffer_ = new net::GrowableIOBuffer(); | 89 header_read_buffer_ = new net::GrowableIOBuffer(); |
| 91 header_read_buffer_->SetCapacity(kMessageHeaderSize); | 90 header_read_buffer_->SetCapacity(kMessageHeaderSize); |
| 92 body_read_buffer_ = new net::GrowableIOBuffer(); | 91 body_read_buffer_ = new net::GrowableIOBuffer(); |
| 93 body_read_buffer_->SetCapacity(kMaxMessageSize); | 92 body_read_buffer_->SetCapacity(kMaxMessageSize); |
| 94 current_read_buffer_ = header_read_buffer_; | 93 current_read_buffer_ = header_read_buffer_; |
| 95 } | 94 } |
| 96 | 95 |
| 97 CastSocket::~CastSocket() { } | 96 CastSocket::~CastSocket() { } |
| 98 | 97 |
| 99 const GURL& CastSocket::url() const { | 98 const GURL& CastSocket::url() const { |
| 100 return url_; | 99 return url_; |
| 101 } | 100 } |
| 102 | 101 |
| 103 scoped_ptr<net::TCPClientSocket> CastSocket::CreateTcpSocket() { | 102 scoped_ptr<net::TCPClientSocket> CastSocket::CreateTcpSocket() { |
| 104 net::AddressList addresses(ip_endpoint_); | 103 net::AddressList addresses(ip_endpoint_); |
| 105 scoped_ptr<net::TCPClientSocket> tcp_socket( | 104 return scoped_ptr<net::TCPClientSocket>( |
| 106 new net::TCPClientSocket(addresses, net_log_, net_log_source_)); | 105 new net::TCPClientSocket(addresses, net_log_, net_log_source_)); |
| 107 // Options cannot be set on the TCPClientSocket yet, because the | 106 // Options cannot be set on the TCPClientSocket yet, because the |
| 108 // underlying platform socket will not be created until we Bind() | 107 // underlying platform socket will not be created until we Bind() |
| 109 // or Connect() it. | 108 // or Connect() it. |
| 110 return tcp_socket.Pass(); | |
| 111 } | 109 } |
| 112 | 110 |
| 113 scoped_ptr<net::SSLClientSocket> CastSocket::CreateSslSocket() { | 111 scoped_ptr<net::SSLClientSocket> CastSocket::CreateSslSocket( |
| 112 scoped_ptr<net::StreamSocket> socket) { | |
| 114 net::SSLConfig ssl_config; | 113 net::SSLConfig ssl_config; |
| 115 // If a peer cert was extracted in a previous attempt to connect, then | 114 // If a peer cert was extracted in a previous attempt to connect, then |
| 116 // whitelist that cert. | 115 // whitelist that cert. |
| 117 if (!peer_cert_.empty()) { | 116 if (!peer_cert_.empty()) { |
| 118 net::SSLConfig::CertAndStatus cert_and_status; | 117 net::SSLConfig::CertAndStatus cert_and_status; |
| 119 cert_and_status.cert_status = net::CERT_STATUS_AUTHORITY_INVALID; | 118 cert_and_status.cert_status = net::CERT_STATUS_AUTHORITY_INVALID; |
| 120 cert_and_status.der_cert = peer_cert_; | 119 cert_and_status.der_cert = peer_cert_; |
| 121 ssl_config.allowed_bad_certs.push_back(cert_and_status); | 120 ssl_config.allowed_bad_certs.push_back(cert_and_status); |
| 122 } | 121 } |
| 123 | 122 |
| 124 cert_verifier_.reset(net::CertVerifier::CreateDefault()); | 123 cert_verifier_.reset(net::CertVerifier::CreateDefault()); |
| 125 transport_security_state_.reset(new net::TransportSecurityState); | 124 transport_security_state_.reset(new net::TransportSecurityState); |
| 126 net::SSLClientSocketContext context; | 125 net::SSLClientSocketContext context; |
| 127 // CertVerifier and TransportSecurityState are owned by us, not the | 126 // CertVerifier and TransportSecurityState are owned by us, not the |
| 128 // context object. | 127 // context object. |
| 129 context.cert_verifier = cert_verifier_.get(); | 128 context.cert_verifier = cert_verifier_.get(); |
| 130 context.transport_security_state = transport_security_state_.get(); | 129 context.transport_security_state = transport_security_state_.get(); |
| 131 | 130 |
| 132 scoped_ptr<net::ClientSocketHandle> connection(new net::ClientSocketHandle); | 131 scoped_ptr<net::ClientSocketHandle> connection(new net::ClientSocketHandle); |
| 133 connection->SetSocket(tcp_socket_.PassAs<net::StreamSocket>()); | 132 connection->SetSocket(socket.Pass()); |
| 134 net::HostPortPair host_and_port = net::HostPortPair::FromIPEndPoint( | 133 net::HostPortPair host_and_port = net::HostPortPair::FromIPEndPoint( |
| 135 ip_endpoint_); | 134 ip_endpoint_); |
| 136 | 135 |
| 137 return net::ClientSocketFactory::GetDefaultFactory()->CreateSSLClientSocket( | 136 return net::ClientSocketFactory::GetDefaultFactory()->CreateSSLClientSocket( |
| 138 connection.Pass(), host_and_port, ssl_config, context); | 137 connection.Pass(), host_and_port, ssl_config, context); |
| 139 } | 138 } |
| 140 | 139 |
| 141 bool CastSocket::ExtractPeerCert(std::string* cert) { | 140 bool CastSocket::ExtractPeerCert(std::string* cert) { |
| 142 DCHECK(cert); | 141 DCHECK(cert); |
| 143 DCHECK(peer_cert_.empty()); | 142 DCHECK(peer_cert_.empty()); |
| 144 net::SSLInfo ssl_info; | 143 net::SSLInfo ssl_info; |
| 145 if (!socket_->GetSSLInfo(&ssl_info) || !ssl_info.cert.get()) | 144 if (!socket_->GetSSLInfo(&ssl_info) || !ssl_info.cert.get()) |
| 146 return false; | 145 return false; |
| 147 bool result = net::X509Certificate::GetDEREncoded( | 146 bool result = net::X509Certificate::GetDEREncoded( |
| 148 ssl_info.cert->os_cert_handle(), cert); | 147 ssl_info.cert->os_cert_handle(), cert); |
| 149 if (result) | 148 if (result) |
| 150 VLOG(1) << "Successfully extracted peer certificate: " << *cert; | 149 VLOG(1) << "Successfully extracted peer certificate: " << *cert; |
| 151 return result; | 150 return result; |
| 152 } | 151 } |
| 153 | 152 |
| 154 int CastSocket::SendAuthChallenge() { | 153 bool CastSocket::VerifyChallengeReply() { |
| 155 CastMessage challenge_message; | 154 return AuthenticateChallengeReply(*challenge_reply_.get(), peer_cert_); |
| 156 CreateAuthChallengeMessage(&challenge_message); | |
| 157 VLOG(1) << "Sending challenge: " << CastMessageToString(challenge_message); | |
| 158 int result = SendMessageInternal( | |
| 159 challenge_message, | |
| 160 base::Bind(&CastSocket::OnChallengeEvent, AsWeakPtr())); | |
| 161 return (result < 0) ? result : net::OK; | |
| 162 } | |
| 163 | |
| 164 int CastSocket::ReadAuthChallengeReply() { | |
| 165 int result = ReadData(); | |
| 166 return (result < 0) ? result : net::OK; | |
| 167 } | |
| 168 | |
| 169 void CastSocket::OnConnectComplete(int result) { | |
| 170 int rv = DoConnectLoop(result); | |
| 171 if (rv != net::ERR_IO_PENDING) | |
| 172 DoConnectCallback(rv); | |
| 173 } | |
| 174 | |
| 175 void CastSocket::OnChallengeEvent(int result) { | |
| 176 // result >= 0 means read or write succeeded synchronously. | |
| 177 int rv = DoConnectLoop(result >= 0 ? net::OK : result); | |
| 178 if (rv != net::ERR_IO_PENDING) | |
| 179 DoConnectCallback(rv); | |
| 180 } | 155 } |
| 181 | 156 |
| 182 void CastSocket::Connect(const net::CompletionCallback& callback) { | 157 void CastSocket::Connect(const net::CompletionCallback& callback) { |
| 183 DCHECK(CalledOnValidThread()); | 158 DCHECK(CalledOnValidThread()); |
| 184 int result = net::ERR_CONNECTION_FAILED; | |
| 185 VLOG(1) << "Connect readyState = " << ready_state_; | 159 VLOG(1) << "Connect readyState = " << ready_state_; |
| 186 if (ready_state_ != READY_STATE_NONE) { | 160 if (ready_state_ != READY_STATE_NONE) { |
| 187 callback.Run(result); | 161 callback.Run(net::ERR_CONNECTION_FAILED); |
| 188 return; | 162 return; |
| 189 } | 163 } |
| 190 if (!ParseChannelUrl(url_)) { | 164 if (!ParseChannelUrl(url_)) { |
| 191 CloseWithError(cast_channel::CHANNEL_ERROR_CONNECT_ERROR); | 165 callback.Run(net::ERR_CONNECTION_FAILED); |
| 192 callback.Run(result); | |
| 193 return; | 166 return; |
| 194 } | 167 } |
| 168 | |
| 169 ready_state_ = READY_STATE_CONNECTING; | |
| 195 connect_callback_ = callback; | 170 connect_callback_ = callback; |
| 196 next_state_ = CONN_STATE_TCP_CONNECT; | 171 connect_state_ = CONN_STATE_TCP_CONNECT; |
| 197 int rv = DoConnectLoop(net::OK); | 172 DoConnectLoop(net::OK); |
|
Ryan Sleevi
2013/12/11 08:14:14
DESIGN nit: the net/ idiom is as follows
This bec
Munjal (Google)
2013/12/11 23:39:41
Yes. I changed it todo return void and do thecallb
| |
| 198 if (rv != net::ERR_IO_PENDING) | 173 } |
| 199 DoConnectCallback(rv); | 174 |
| 175 void CastSocket::PostTaskToStartConnectLoop(int result) { | |
|
Ryan Sleevi
2013/12/11 08:14:14
DESIGN NIT: Rather than have DoConnectLoop invoke
Munjal (Google)
2013/12/11 23:39:41
The current code only has one place where the call
Ryan Sleevi
2013/12/12 00:48:14
I apologize if my comment before was not clearer.
Munjal (Google)
2013/12/12 17:53:47
There is only one public method that starts the co
| |
| 176 DCHECK(CalledOnValidThread()); | |
| 177 base::MessageLoop::current()->PostTask( | |
| 178 FROM_HERE, | |
| 179 base::Bind(&CastSocket::DoConnectLoop, AsWeakPtr(), result)); | |
| 200 } | 180 } |
| 201 | 181 |
| 202 // This method performs the state machine transitions for connection flow. | 182 // This method performs the state machine transitions for connection flow. |
| 203 // There are two entry points to this method: | 183 // There are two entry points to this method: |
| 204 // 1. public Connect method: this starts the flow | 184 // 1. Connect method: this starts the flow |
| 205 // 2. OnConnectComplete: callback method called when an async operation | 185 // 2. Callback from network operations that finish asynchronously |
| 206 // is done. OnConnectComplete calls this method to continue the state | 186 void CastSocket::DoConnectLoop(int result) { |
| 207 // machine transitions. | |
| 208 int CastSocket::DoConnectLoop(int result) { | |
| 209 // Avoid re-entrancy as a result of synchronous completion. | |
| 210 if (in_connect_loop_) | |
| 211 return net::ERR_IO_PENDING; | |
| 212 in_connect_loop_ = true; | |
| 213 | |
| 214 // Network operations can either finish synchronously or asynchronously. | 187 // Network operations can either finish synchronously or asynchronously. |
| 215 // This method executes the state machine transitions in a loop so that | 188 // This method executes the state machine transitions in a loop so that |
| 216 // correct state transitions happen even when network operations finish | 189 // correct state transitions happen even when network operations finish |
| 217 // synchronously. | 190 // synchronously. |
| 218 int rv = result; | 191 int rv = result; |
| 219 do { | 192 do { |
| 220 ConnectionState state = next_state_; | 193 ConnectionState state = connect_state_; |
| 221 // All the Do* methods do not set next_state_ in case of an | 194 // All the Do* methods do not set connect_state_ in case of an |
| 222 // error. So set next_state_ to NONE to figure out if the Do* | 195 // error. So set connect_state_ to NONE to figure out if the Do* |
| 223 // method changed state or not. | 196 // method changed state or not. |
|
Ryan Sleevi
2013/12/11 08:14:14
comment nit: This makes the comment describe imple
Munjal (Google)
2013/12/11 23:39:41
Done.
| |
| 224 next_state_ = CONN_STATE_NONE; | 197 connect_state_ = CONN_STATE_NONE; |
| 225 switch (state) { | 198 switch (state) { |
| 226 case CONN_STATE_TCP_CONNECT: | 199 case CONN_STATE_TCP_CONNECT: |
| 227 rv = DoTcpConnect(); | 200 rv = DoTcpConnect(); |
| 228 break; | 201 break; |
| 229 case CONN_STATE_TCP_CONNECT_COMPLETE: | 202 case CONN_STATE_TCP_CONNECT_COMPLETE: |
| 230 rv = DoTcpConnectComplete(rv); | 203 rv = DoTcpConnectComplete(rv); |
| 231 break; | 204 break; |
| 232 case CONN_STATE_SSL_CONNECT: | 205 case CONN_STATE_SSL_CONNECT: |
| 233 DCHECK_EQ(net::OK, rv); | 206 DCHECK_EQ(net::OK, rv); |
| 234 rv = DoSslConnect(); | 207 rv = DoSslConnect(); |
| 235 break; | 208 break; |
| 236 case CONN_STATE_SSL_CONNECT_COMPLETE: | 209 case CONN_STATE_SSL_CONNECT_COMPLETE: |
| 237 rv = DoSslConnectComplete(rv); | 210 rv = DoSslConnectComplete(rv); |
| 238 break; | 211 break; |
| 239 case CONN_STATE_AUTH_CHALLENGE_SEND: | 212 case CONN_STATE_AUTH_CHALLENGE_SEND: |
| 240 rv = DoAuthChallengeSend(); | 213 rv = DoAuthChallengeSend(); |
| 241 break; | 214 break; |
| 242 case CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE: | 215 case CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE: |
| 243 rv = DoAuthChallengeSendComplete(rv); | 216 rv = DoAuthChallengeSendComplete(rv); |
| 244 break; | 217 break; |
| 245 case CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE: | 218 case CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE: |
| 246 rv = DoAuthChallengeReplyComplete(rv); | 219 rv = DoAuthChallengeReplyComplete(rv); |
| 247 break; | 220 break; |
| 248 | |
| 249 default: | 221 default: |
| 250 NOTREACHED() << "BUG in CastSocket state machine code"; | 222 NOTREACHED() << "BUG in CastSocket connection state machine code"; |
| 251 break; | 223 break; |
| 252 } | 224 } |
| 253 } while (rv != net::ERR_IO_PENDING && next_state_ != CONN_STATE_NONE); | 225 } while (rv != net::ERR_IO_PENDING && connect_state_ != CONN_STATE_NONE); |
| 254 // Get out of the loop either when: | 226 // Get out of the loop either when: |
| 255 // a. A network operation is pending, OR | 227 // a. A network operation is pending, OR |
| 256 // b. The Do* method called did not change state | 228 // b. The Do* method called did not change state |
| 257 | 229 |
| 258 in_connect_loop_ = false; | 230 // If there is no pending IO and if we still got out of the loop then |
| 259 | 231 // either we are done successfully or there was an error; invoke the |
| 260 return rv; | 232 // callback in either case. |
|
Ryan Sleevi
2013/12/11 08:14:14
comment nit: pronouns are considered harmful - htt
Munjal (Google)
2013/12/11 23:39:41
Done.
| |
| 233 if (rv != net::ERR_IO_PENDING) | |
| 234 DoConnectCallback(rv); | |
| 261 } | 235 } |
| 262 | 236 |
| 263 int CastSocket::DoTcpConnect() { | 237 int CastSocket::DoTcpConnect() { |
| 264 VLOG(1) << "DoTcpConnect"; | 238 VLOG(1) << "DoTcpConnect"; |
| 265 next_state_ = CONN_STATE_TCP_CONNECT_COMPLETE; | 239 connect_state_ = CONN_STATE_TCP_CONNECT_COMPLETE; |
| 266 tcp_socket_ = CreateTcpSocket(); | 240 tcp_socket_ = CreateTcpSocket(); |
| 267 return tcp_socket_->Connect( | 241 return tcp_socket_->Connect( |
| 268 base::Bind(&CastSocket::OnConnectComplete, AsWeakPtr())); | 242 base::Bind(&CastSocket::DoConnectLoop, AsWeakPtr())); |
| 269 } | 243 } |
| 270 | 244 |
| 271 int CastSocket::DoTcpConnectComplete(int result) { | 245 int CastSocket::DoTcpConnectComplete(int result) { |
| 272 VLOG(1) << "DoTcpConnectComplete: " << result; | 246 VLOG(1) << "DoTcpConnectComplete: " << result; |
| 273 if (result == net::OK) { | 247 if (result == net::OK) { |
| 274 // Enable TCP protocol-level keep-alive. | 248 // Enable TCP protocol-level keep-alive. |
| 275 bool result = tcp_socket_->SetKeepAlive(true, kTcpKeepAliveDelaySecs); | 249 bool result = tcp_socket_->SetKeepAlive(true, kTcpKeepAliveDelaySecs); |
| 276 LOG_IF(WARNING, !result) << "Failed to SetKeepAlive."; | 250 LOG_IF(WARNING, !result) << "Failed to SetKeepAlive."; |
| 277 next_state_ = CONN_STATE_SSL_CONNECT; | 251 connect_state_ = CONN_STATE_SSL_CONNECT; |
| 278 } | 252 } |
| 279 return result; | 253 return result; |
| 280 } | 254 } |
| 281 | 255 |
| 282 int CastSocket::DoSslConnect() { | 256 int CastSocket::DoSslConnect() { |
| 283 VLOG(1) << "DoSslConnect"; | 257 VLOG(1) << "DoSslConnect"; |
| 284 next_state_ = CONN_STATE_SSL_CONNECT_COMPLETE; | 258 connect_state_ = CONN_STATE_SSL_CONNECT_COMPLETE; |
| 285 socket_ = CreateSslSocket(); | 259 socket_ = CreateSslSocket(tcp_socket_.PassAs<net::StreamSocket>()); |
| 286 return socket_->Connect( | 260 return socket_->Connect( |
| 287 base::Bind(&CastSocket::OnConnectComplete, AsWeakPtr())); | 261 base::Bind(&CastSocket::DoConnectLoop, AsWeakPtr())); |
| 288 } | 262 } |
| 289 | 263 |
| 290 int CastSocket::DoSslConnectComplete(int result) { | 264 int CastSocket::DoSslConnectComplete(int result) { |
| 291 VLOG(1) << "DoSslConnectComplete: " << result; | 265 VLOG(1) << "DoSslConnectComplete: " << result; |
| 292 if (result == net::ERR_CERT_AUTHORITY_INVALID && | 266 if (result == net::ERR_CERT_AUTHORITY_INVALID && |
| 293 peer_cert_.empty() && | 267 peer_cert_.empty() && |
| 294 ExtractPeerCert(&peer_cert_)) { | 268 ExtractPeerCert(&peer_cert_)) { |
| 295 next_state_ = CONN_STATE_TCP_CONNECT; | 269 connect_state_ = CONN_STATE_TCP_CONNECT; |
| 296 } else if (result == net::OK && auth_required_) { | 270 } else if (result == net::OK && auth_required_) { |
| 297 next_state_ = CONN_STATE_AUTH_CHALLENGE_SEND; | 271 connect_state_ = CONN_STATE_AUTH_CHALLENGE_SEND; |
| 298 } | 272 } |
| 299 return result; | 273 return result; |
| 300 } | 274 } |
| 301 | 275 |
| 302 int CastSocket::DoAuthChallengeSend() { | 276 int CastSocket::DoAuthChallengeSend() { |
| 303 VLOG(1) << "DoAuthChallengeSend"; | 277 VLOG(1) << "DoAuthChallengeSend"; |
| 304 next_state_ = CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE; | 278 connect_state_ = CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE; |
| 305 return SendAuthChallenge(); | 279 CastMessage challenge_message; |
| 280 CreateAuthChallengeMessage(&challenge_message); | |
| 281 VLOG(1) << "Sending challenge: " << CastMessageToString(challenge_message); | |
| 282 // Post a task to start write loop so that DoWriteLoop is not nested inside | |
| 283 // DoConnectLoop. This is not strictly necessary but keeps the write loop | |
| 284 // code decoupled from connect loop code. | |
|
Ryan Sleevi
2013/12/11 08:14:14
This is really unfortunate that you're forcing alw
Munjal (Google)
2013/12/11 23:39:41
See my overall comment on the patch that addresses
| |
| 285 base::MessageLoop::current()->PostTask( | |
| 286 FROM_HERE, | |
| 287 base::Bind(&CastSocket::SendCastMessageInternal, AsWeakPtr(), | |
| 288 challenge_message, | |
| 289 base::Bind(&CastSocket::DoConnectLoop, AsWeakPtr()))); | |
| 290 // Always return IO_PENDING since we always get the result asynchronously | |
|
Ryan Sleevi
2013/12/11 08:14:14
comment nits re: pronouns apply throughout this fi
Munjal (Google)
2013/12/11 23:39:41
Done.
| |
| 291 return net::ERR_IO_PENDING; | |
| 306 } | 292 } |
| 307 | 293 |
| 308 int CastSocket::DoAuthChallengeSendComplete(int result) { | 294 int CastSocket::DoAuthChallengeSendComplete(int result) { |
| 309 VLOG(1) << "DoAuthChallengeSendComplete: " << result; | 295 VLOG(1) << "DoAuthChallengeSendComplete: " << result; |
| 310 if (result != net::OK) | 296 if (result < 0) |
| 311 return result; | 297 return result; |
| 312 next_state_ = CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE; | 298 connect_state_ = CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE; |
| 313 return ReadAuthChallengeReply(); | 299 // Post a task to start read loop so that DoReadLoop is not nested inside |
| 300 // DoConnectLoop. This is not strictly necessary but keeps the read loop | |
| 301 // code decoupled from connect loop code. | |
| 302 PostTaskToStartReadLoop(); | |
| 303 // Always return IO_PENDING since we always get the result asynchronously | |
| 304 return net::ERR_IO_PENDING; | |
| 314 } | 305 } |
| 315 | 306 |
| 316 int CastSocket::DoAuthChallengeReplyComplete(int result) { | 307 int CastSocket::DoAuthChallengeReplyComplete(int result) { |
| 317 VLOG(1) << "DoAuthChallengeReplyComplete: " << result; | 308 VLOG(1) << "DoAuthChallengeReplyComplete: " << result; |
| 318 if (result != net::OK) | 309 if (result < 0) |
| 319 return result; | 310 return result; |
| 320 if (!VerifyChallengeReply()) | 311 if (!VerifyChallengeReply()) |
| 321 return net::ERR_FAILED; | 312 return net::ERR_FAILED; |
| 322 VLOG(1) << "Auth challenge verification succeeded"; | 313 VLOG(1) << "Auth challenge verification succeeded"; |
| 323 return net::OK; | 314 return net::OK; |
| 324 } | 315 } |
| 325 | 316 |
| 326 bool CastSocket::VerifyChallengeReply() { | |
| 327 return AuthenticateChallengeReply(*challenge_reply_.get(), peer_cert_); | |
| 328 } | |
| 329 | |
| 330 void CastSocket::DoConnectCallback(int result) { | 317 void CastSocket::DoConnectCallback(int result) { |
| 331 ready_state_ = (result == net::OK) ? READY_STATE_OPEN : READY_STATE_CLOSED; | 318 ready_state_ = (result == net::OK) ? READY_STATE_OPEN : READY_STATE_CLOSED; |
| 332 error_state_ = (result == net::OK) ? | 319 error_state_ = (result == net::OK) ? |
| 333 CHANNEL_ERROR_NONE : CHANNEL_ERROR_CONNECT_ERROR; | 320 CHANNEL_ERROR_NONE : CHANNEL_ERROR_CONNECT_ERROR; |
| 321 if (result == net::OK) // Start the read loop | |
| 322 PostTaskToStartReadLoop(); | |
| 334 base::ResetAndReturn(&connect_callback_).Run(result); | 323 base::ResetAndReturn(&connect_callback_).Run(result); |
| 335 // Start the ReadData loop if not already started. | |
| 336 // If auth_required_ is true we would've started a ReadData loop already. | |
| 337 // TODO(munjal): This is a bit ugly. Refactor read and write code. | |
| 338 if (result == net::OK && !auth_required_) | |
| 339 ReadData(); | |
| 340 } | 324 } |
| 341 | 325 |
| 342 void CastSocket::Close(const net::CompletionCallback& callback) { | 326 void CastSocket::Close(const net::CompletionCallback& callback) { |
| 343 DCHECK(CalledOnValidThread()); | 327 DCHECK(CalledOnValidThread()); |
| 344 VLOG(1) << "Close ReadyState = " << ready_state_; | 328 VLOG(1) << "Close ReadyState = " << ready_state_; |
| 345 tcp_socket_.reset(NULL); | 329 tcp_socket_.reset(NULL); |
|
Ryan Sleevi
2013/12/11 08:14:14
Just .reset() should be sufficient for all of thes
Munjal (Google)
2013/12/11 23:39:41
Done.
| |
| 346 socket_.reset(NULL); | 330 socket_.reset(NULL); |
| 347 cert_verifier_.reset(NULL); | 331 cert_verifier_.reset(NULL); |
| 348 transport_security_state_.reset(NULL); | 332 transport_security_state_.reset(NULL); |
| 349 ready_state_ = READY_STATE_CLOSED; | 333 ready_state_ = READY_STATE_CLOSED; |
| 350 callback.Run(net::OK); | 334 callback.Run(net::OK); |
|
Ryan Sleevi
2013/12/11 08:14:14
|callback| can delete |this|, correct?
Seems like
Munjal (Google)
2013/12/11 23:39:41
Done.
| |
| 351 } | 335 } |
| 352 | 336 |
| 353 void CastSocket::SendMessage(const MessageInfo& message, | 337 void CastSocket::SendMessage(const MessageInfo& message, |
| 354 const net::CompletionCallback& callback) { | 338 const net::CompletionCallback& callback) { |
| 355 DCHECK(CalledOnValidThread()); | 339 DCHECK(CalledOnValidThread()); |
| 356 VLOG(1) << "Send ReadyState " << ready_state_; | |
| 357 int result = net::ERR_FAILED; | |
| 358 if (ready_state_ != READY_STATE_OPEN) { | 340 if (ready_state_ != READY_STATE_OPEN) { |
| 359 callback.Run(result); | 341 callback.Run(net::ERR_FAILED); |
| 360 return; | 342 return; |
| 361 } | 343 } |
| 362 CastMessage message_proto; | 344 CastMessage message_proto; |
| 363 if (!MessageInfoToCastMessage(message, &message_proto)) { | 345 if (!MessageInfoToCastMessage(message, &message_proto)) { |
| 364 CloseWithError(cast_channel::CHANNEL_ERROR_INVALID_MESSAGE); | 346 callback.Run(net::ERR_FAILED); |
| 365 // TODO(mfoltz): Do a better job of signaling cast_channel errors to the | 347 return; |
| 366 // caller. | 348 } |
| 367 callback.Run(net::OK); | 349 |
| 368 return; | 350 SendCastMessageInternal(message_proto, callback); |
| 369 } | 351 } |
| 370 SendMessageInternal(message_proto, callback); | 352 |
| 371 } | 353 void CastSocket::SendCastMessageInternal( |
| 372 | 354 const CastMessage& message, |
| 373 int CastSocket::SendMessageInternal(const CastMessage& message_proto, | 355 const net::CompletionCallback& callback) { |
| 374 const net::CompletionCallback& callback) { | |
| 375 WriteRequest write_request(callback); | 356 WriteRequest write_request(callback); |
| 376 if (!write_request.SetContent(message_proto)) | 357 if (!write_request.SetContent(message)) { |
| 377 return net::ERR_FAILED; | 358 callback.Run(net::ERR_FAILED); |
| 359 return; | |
| 360 } | |
| 361 | |
| 378 write_queue_.push(write_request); | 362 write_queue_.push(write_request); |
| 379 return WriteData(); | 363 if (write_state_ == WRITE_STATE_NONE) { |
| 380 } | 364 write_state_ = WRITE_STATE_WRITE; |
| 381 | 365 DoWriteLoop(net::OK); |
| 382 int CastSocket::WriteData() { | 366 } |
| 367 } | |
| 368 | |
| 369 void CastSocket::DoWriteLoop(int result) { | |
| 383 DCHECK(CalledOnValidThread()); | 370 DCHECK(CalledOnValidThread()); |
| 384 VLOG(1) << "WriteData q = " << write_queue_.size(); | 371 VLOG(1) << "WriteData q = " << write_queue_.size(); |
| 385 if (write_queue_.empty() || write_callback_pending_) | 372 |
| 386 return net::ERR_FAILED; | 373 if (write_queue_.empty()) |
| 387 | 374 return; |
| 375 | |
| 376 // Network operations can either finish synchronously or asynchronously. | |
| 377 // This method executes the state machine transitions in a loop so that | |
| 378 // write state transitions happen even when network operations finish | |
| 379 // synchronously. | |
| 380 int rv = result; | |
| 381 do { | |
| 382 WriteState state = write_state_; | |
| 383 write_state_ = WRITE_STATE_NONE; | |
| 384 switch (state) { | |
| 385 case WRITE_STATE_WRITE: | |
| 386 rv = DoWrite(); | |
| 387 break; | |
| 388 case WRITE_STATE_WRITE_COMPLETE: | |
| 389 rv = DoWriteComplete(rv); | |
| 390 break; | |
| 391 case WRITE_STATE_DO_CALLBACK: | |
| 392 rv = DoWriteCallback(); | |
| 393 break; | |
| 394 case WRITE_STATE_ERROR: | |
| 395 rv = DoWriteError(rv); | |
| 396 break; | |
| 397 default: | |
| 398 NOTREACHED() << "BUG in CastSocket write state machine code"; | |
| 399 break; | |
| 400 } | |
| 401 } while (!write_queue_.empty() && | |
| 402 rv != net::ERR_IO_PENDING && | |
| 403 write_state_ != WRITE_STATE_NONE); | |
| 404 | |
| 405 // If we came out of the loop and the result is ERR_FAILED then close | |
| 406 // with error. | |
| 407 if (rv == net::ERR_FAILED) | |
| 408 CloseWithError(error_state_); | |
|
Ryan Sleevi
2013/12/11 08:14:14
|this| can be deleted, can't it? Subtle.
| |
| 409 } | |
| 410 | |
| 411 int CastSocket::DoWrite() { | |
| 412 DCHECK(!write_queue_.empty()); | |
| 388 WriteRequest& request = write_queue_.front(); | 413 WriteRequest& request = write_queue_.front(); |
| 389 | 414 |
| 390 VLOG(1) << "WriteData byte_count = " << request.io_buffer->size() | 415 VLOG(1) << "WriteData byte_count = " << request.io_buffer->size() |
| 391 << " bytes_written " << request.io_buffer->BytesConsumed(); | 416 << " bytes_written " << request.io_buffer->BytesConsumed(); |
| 392 | 417 |
| 393 write_callback_pending_ = true; | 418 write_state_ = WRITE_STATE_WRITE_COMPLETE; |
| 394 int result = socket_->Write( | 419 |
| 420 return socket_->Write( | |
| 395 request.io_buffer.get(), | 421 request.io_buffer.get(), |
| 396 request.io_buffer->BytesRemaining(), | 422 request.io_buffer->BytesRemaining(), |
| 397 base::Bind(&CastSocket::OnWriteData, AsWeakPtr())); | 423 base::Bind(&CastSocket::DoWriteLoop, AsWeakPtr())); |
| 398 | 424 } |
| 399 if (result != net::ERR_IO_PENDING) | 425 |
| 400 OnWriteData(result); | 426 int CastSocket::DoWriteComplete(int result) { |
| 401 | 427 DCHECK(!write_queue_.empty()); |
| 402 return result; | 428 if (result <= 0) { // NOTE that 0 also indicates an error |
| 403 } | 429 error_state_ = CHANNEL_ERROR_SOCKET_ERROR; |
| 404 | 430 write_state_ = WRITE_STATE_ERROR; |
| 405 void CastSocket::OnWriteData(int result) { | 431 return result == 0 ? net::ERR_FAILED : result; |
| 406 DCHECK(CalledOnValidThread()); | 432 } |
| 407 VLOG(1) << "OnWriteComplete result = " << result; | 433 |
| 408 DCHECK(write_callback_pending_); | 434 // Some bytes were successfully written |
| 409 DCHECK(!write_queue_.empty()); | |
| 410 write_callback_pending_ = false; | |
| 411 WriteRequest& request = write_queue_.front(); | 435 WriteRequest& request = write_queue_.front(); |
| 412 scoped_refptr<net::DrainableIOBuffer> io_buffer = request.io_buffer; | 436 scoped_refptr<net::DrainableIOBuffer> io_buffer = request.io_buffer; |
| 413 | 437 io_buffer->DidConsume(result); |
| 414 if (result >= 0) { | 438 if (io_buffer->BytesRemaining() == 0) // Message fully sent |
| 415 io_buffer->DidConsume(result); | 439 write_state_ = WRITE_STATE_DO_CALLBACK; |
| 416 if (io_buffer->BytesRemaining() > 0) { | 440 else |
| 417 VLOG(1) << "OnWriteComplete size = " << io_buffer->size() | 441 write_state_ = WRITE_STATE_WRITE; |
| 418 << " consumed " << io_buffer->BytesConsumed() | 442 |
| 419 << " remaining " << io_buffer->BytesRemaining() | 443 return net::OK; |
| 420 << " # requests " << write_queue_.size(); | 444 } |
| 421 WriteData(); | 445 |
| 422 return; | 446 int CastSocket::DoWriteCallback() { |
| 447 DCHECK(!write_queue_.empty()); | |
| 448 WriteRequest& request = write_queue_.front(); | |
| 449 int bytes_consumed = request.io_buffer->BytesConsumed(); | |
| 450 | |
| 451 // If we are in connection flow, then we should have exaclty one item in | |
| 452 // the write queue. | |
| 453 if (ready_state_ == READY_STATE_CONNECTING) { | |
| 454 write_queue_.pop(); | |
| 455 DCHECK(write_queue_.empty()); | |
| 456 PostTaskToStartConnectLoop(bytes_consumed); | |
| 457 } else { | |
| 458 WriteRequest& request = write_queue_.front(); | |
| 459 request.callback.Run(bytes_consumed); | |
| 460 write_queue_.pop(); | |
| 461 } | |
| 462 write_state_ = WRITE_STATE_WRITE; | |
| 463 return net::OK; | |
| 464 } | |
| 465 | |
| 466 int CastSocket::DoWriteError(int result) { | |
| 467 DCHECK(!write_queue_.empty()); | |
| 468 DCHECK_LT(result, 0); | |
| 469 | |
| 470 // If we are in connection flow, then we should have exactly one item in | |
| 471 // the write queue. | |
| 472 if (ready_state_ == READY_STATE_CONNECTING) { | |
| 473 write_queue_.pop(); | |
| 474 DCHECK(write_queue_.empty()); | |
| 475 PostTaskToStartConnectLoop(result); | |
| 476 return net::ERR_FAILED; | |
| 477 } | |
| 478 | |
| 479 while (!write_queue_.empty()) { | |
| 480 WriteRequest& request = write_queue_.front(); | |
| 481 request.callback.Run(result); | |
| 482 write_queue_.pop(); | |
| 483 } | |
| 484 return net::ERR_FAILED; | |
| 485 } | |
| 486 | |
| 487 void CastSocket::PostTaskToStartReadLoop() { | |
| 488 DCHECK(CalledOnValidThread()); | |
| 489 base::MessageLoop::current()->PostTask( | |
| 490 FROM_HERE, | |
| 491 base::Bind(&CastSocket::StartReadLoop, AsWeakPtr())); | |
| 492 } | |
| 493 | |
| 494 void CastSocket::StartReadLoop() { | |
| 495 // If we are in READ_STATE_NONE then get into appropriate | |
| 496 // starting state and start the read loop | |
| 497 if (read_state_ == READ_STATE_NONE) { | |
| 498 read_state_ = READ_STATE_READ; | |
| 499 DoReadLoop(net::OK); | |
| 500 } | |
| 501 } | |
| 502 | |
| 503 void CastSocket::DoReadLoop(int result) { | |
| 504 DCHECK(CalledOnValidThread()); | |
| 505 // Network operations can either finish synchronously or asynchronously. | |
| 506 // This method executes the state machine transitions in a loop so that | |
| 507 // write state transitions happen even when network operations finish | |
| 508 // synchronously. | |
| 509 int rv = result; | |
| 510 do { | |
| 511 ReadState state = read_state_; | |
| 512 read_state_ = READ_STATE_NONE; | |
| 513 | |
| 514 switch (state) { | |
| 515 case READ_STATE_READ: | |
| 516 rv = DoRead(); | |
| 517 break; | |
| 518 case READ_STATE_READ_COMPLETE: | |
| 519 rv = DoReadComplete(rv); | |
| 520 break; | |
| 521 case READ_STATE_DO_CALLBACK: | |
| 522 rv = DoReadCallback(); | |
| 523 break; | |
| 524 case READ_STATE_ERROR: | |
| 525 rv = DoReadError(rv); | |
| 526 default: | |
| 527 NOTREACHED() << "BUG in read state machine"; | |
| 528 break; | |
| 423 } | 529 } |
| 424 DCHECK_EQ(io_buffer->BytesConsumed(), io_buffer->size()); | 530 } while (rv != net::ERR_IO_PENDING && read_state_ != READ_STATE_NONE); |
| 425 DCHECK_EQ(io_buffer->BytesRemaining(), 0); | 531 |
| 426 result = io_buffer->BytesConsumed(); | 532 // If we came out of the loop and the result is ERR_FAILED then close |
| 427 } | 533 // with error. |
| 428 | 534 if (rv == net::ERR_FAILED) |
| 429 request.callback.Run(result); | 535 CloseWithError(error_state_); |
| 430 write_queue_.pop(); | 536 } |
| 431 | 537 |
| 432 VLOG(1) << "OnWriteComplete size = " << io_buffer->size() | 538 int CastSocket::DoRead() { |
| 433 << " consumed " << io_buffer->BytesConsumed() | 539 read_state_ = READ_STATE_READ_COMPLETE; |
| 434 << " remaining " << io_buffer->BytesRemaining() | |
| 435 << " # requests " << write_queue_.size(); | |
| 436 | |
| 437 if (result < 0) { | |
| 438 CloseWithError(CHANNEL_ERROR_SOCKET_ERROR); | |
| 439 return; | |
| 440 } | |
| 441 | |
| 442 if (!write_queue_.empty()) | |
| 443 WriteData(); | |
| 444 } | |
| 445 | |
| 446 int CastSocket::ReadData() { | |
| 447 DCHECK(CalledOnValidThread()); | |
| 448 if (!socket_.get()) | |
| 449 return net::ERR_FAILED; | |
| 450 DCHECK(!read_callback_pending_); | |
| 451 read_callback_pending_ = true; | |
| 452 // Figure out if we are reading the header or body, and the remaining bytes. | 540 // Figure out if we are reading the header or body, and the remaining bytes. |
| 453 uint32 num_bytes_to_read = 0; | 541 uint32 num_bytes_to_read = 0; |
| 454 if (header_read_buffer_->RemainingCapacity() > 0) { | 542 if (header_read_buffer_->RemainingCapacity() > 0) { |
| 455 current_read_buffer_ = header_read_buffer_; | 543 current_read_buffer_ = header_read_buffer_; |
| 456 num_bytes_to_read = header_read_buffer_->RemainingCapacity(); | 544 num_bytes_to_read = header_read_buffer_->RemainingCapacity(); |
| 457 DCHECK_LE(num_bytes_to_read, kMessageHeaderSize); | 545 DCHECK_LE(num_bytes_to_read, kMessageHeaderSize); |
| 458 } else { | 546 } else { |
| 459 DCHECK_GT(current_message_size_, 0U); | 547 DCHECK_GT(current_message_size_, 0U); |
| 460 num_bytes_to_read = current_message_size_ - body_read_buffer_->offset(); | 548 num_bytes_to_read = current_message_size_ - body_read_buffer_->offset(); |
| 461 current_read_buffer_ = body_read_buffer_; | 549 current_read_buffer_ = body_read_buffer_; |
| 462 DCHECK_LE(num_bytes_to_read, kMaxMessageSize); | 550 DCHECK_LE(num_bytes_to_read, kMaxMessageSize); |
| 463 } | 551 } |
| 464 DCHECK_GT(num_bytes_to_read, 0U); | 552 DCHECK_GT(num_bytes_to_read, 0U); |
| 553 | |
| 465 // We read up to num_bytes_to_read into |current_read_buffer_|. | 554 // We read up to num_bytes_to_read into |current_read_buffer_|. |
| 466 int result = socket_->Read( | 555 return socket_->Read( |
| 467 current_read_buffer_.get(), | 556 current_read_buffer_.get(), |
| 468 num_bytes_to_read, | 557 num_bytes_to_read, |
| 469 base::Bind(&CastSocket::OnReadData, AsWeakPtr())); | 558 base::Bind(&CastSocket::DoReadLoop, AsWeakPtr())); |
| 470 VLOG(1) << "ReadData result = " << result; | |
| 471 if (result > 0) { | |
| 472 OnReadData(result); | |
| 473 } else if (result != net::ERR_IO_PENDING) { | |
| 474 CloseWithError(CHANNEL_ERROR_SOCKET_ERROR); | |
| 475 } | |
| 476 return result; | |
| 477 } | 559 } |
| 478 | 560 |
| 479 void CastSocket::OnReadData(int result) { | 561 int CastSocket::DoReadComplete(int result) { |
| 480 DCHECK(CalledOnValidThread()); | 562 VLOG(1) << "DoReadDataComplete result = " << result |
| 481 VLOG(1) << "OnReadData result = " << result | |
| 482 << " header offset = " << header_read_buffer_->offset() | 563 << " header offset = " << header_read_buffer_->offset() |
| 483 << " body offset = " << body_read_buffer_->offset(); | 564 << " body offset = " << body_read_buffer_->offset(); |
| 484 read_callback_pending_ = false; | 565 if (result <= 0) { // 0 means EOF: the peer closed the socket |
| 485 if (result <= 0) { | 566 error_state_ = CHANNEL_ERROR_SOCKET_ERROR; |
| 486 CloseWithError(CHANNEL_ERROR_SOCKET_ERROR); | 567 read_state_ = READ_STATE_ERROR; |
| 487 return; | 568 return result == 0 ? net::ERR_FAILED : result; |
| 488 } | 569 } |
| 570 | |
| 489 // We read some data. Move the offset in the current buffer forward. | 571 // We read some data. Move the offset in the current buffer forward. |
| 490 DCHECK_LE(current_read_buffer_->offset() + result, | 572 DCHECK_LE(current_read_buffer_->offset() + result, |
| 491 current_read_buffer_->capacity()); | 573 current_read_buffer_->capacity()); |
| 492 current_read_buffer_->set_offset(current_read_buffer_->offset() + result); | 574 current_read_buffer_->set_offset(current_read_buffer_->offset() + result); |
| 575 read_state_ = READ_STATE_READ; | |
| 493 | 576 |
| 494 bool should_continue = true; | |
| 495 if (current_read_buffer_.get() == header_read_buffer_.get() && | 577 if (current_read_buffer_.get() == header_read_buffer_.get() && |
| 496 current_read_buffer_->RemainingCapacity() == 0) { | 578 current_read_buffer_->RemainingCapacity() == 0) { |
| 497 // If we have read a full header, process the contents. | 579 // If we have read a full header, process the contents. |
| 498 should_continue = ProcessHeader(); | 580 if (!ProcessHeader()) { |
| 581 error_state_ = cast_channel::CHANNEL_ERROR_INVALID_MESSAGE; | |
| 582 read_state_ = READ_STATE_ERROR; | |
| 583 } | |
| 499 } else if (current_read_buffer_.get() == body_read_buffer_.get() && | 584 } else if (current_read_buffer_.get() == body_read_buffer_.get() && |
| 500 static_cast<uint32>(current_read_buffer_->offset()) == | 585 static_cast<uint32>(current_read_buffer_->offset()) == |
| 501 current_message_size_) { | 586 current_message_size_) { |
| 502 // If we have read a full body, process the contents. | 587 // If we have read a full body, process the contents. |
| 503 should_continue = ProcessBody(); | 588 if (ProcessBody()) { |
| 589 read_state_ = READ_STATE_DO_CALLBACK; | |
| 590 } else { | |
| 591 error_state_ = cast_channel::CHANNEL_ERROR_INVALID_MESSAGE; | |
| 592 read_state_ = READ_STATE_ERROR; | |
| 593 } | |
| 504 } | 594 } |
| 505 if (should_continue) | 595 |
| 506 ReadData(); | 596 return net::OK; |
| 597 } | |
| 598 | |
| 599 int CastSocket::DoReadCallback() { | |
| 600 read_state_ = READ_STATE_READ; | |
| 601 if (IsAuthMessage(current_message_)) { | |
| 602 // If we received an auth message then check that we are in connect flow. | |
| 603 if (ready_state_ == READY_STATE_CONNECTING) { | |
| 604 challenge_reply_.reset(new CastMessage(current_message_)); | |
| 605 PostTaskToStartConnectLoop(net::OK); | |
| 606 } else { | |
| 607 read_state_ = READ_STATE_ERROR; | |
| 608 } | |
| 609 } else if (delegate_) { | |
| 610 MessageInfo message; | |
| 611 if (CastMessageToMessageInfo(current_message_, &message)) | |
| 612 delegate_->OnMessage(this, message); | |
| 613 else | |
| 614 read_state_ = READ_STATE_ERROR; | |
| 615 } | |
| 616 current_message_.Clear(); | |
| 617 return net::OK; | |
| 618 } | |
| 619 | |
| 620 int CastSocket::DoReadError(int result) { | |
| 621 // This method is not strictly necessary but provides consistency with | |
| 622 // the write flow | |
| 623 DCHECK_LT(result, 0); | |
| 624 return net::ERR_FAILED; | |
| 507 } | 625 } |
| 508 | 626 |
| 509 bool CastSocket::ProcessHeader() { | 627 bool CastSocket::ProcessHeader() { |
| 510 DCHECK_EQ(static_cast<uint32>(header_read_buffer_->offset()), | 628 DCHECK_EQ(static_cast<uint32>(header_read_buffer_->offset()), |
| 511 kMessageHeaderSize); | 629 kMessageHeaderSize); |
| 512 MessageHeader header; | 630 MessageHeader header; |
| 513 MessageHeader::ReadFromIOBuffer(header_read_buffer_.get(), &header); | 631 MessageHeader::ReadFromIOBuffer(header_read_buffer_.get(), &header); |
| 514 if (header.message_size > kMaxMessageSize) { | 632 if (header.message_size > kMaxMessageSize) |
| 515 CloseWithError(cast_channel::CHANNEL_ERROR_INVALID_MESSAGE); | |
| 516 return false; | 633 return false; |
| 517 } | 634 |
| 518 VLOG(1) << "Parsed header { message_size: " << header.message_size << " }"; | 635 VLOG(1) << "Parsed header { message_size: " << header.message_size << " }"; |
| 519 current_message_size_ = header.message_size; | 636 current_message_size_ = header.message_size; |
| 520 return true; | 637 return true; |
| 521 } | 638 } |
| 522 | 639 |
| 523 bool CastSocket::ProcessBody() { | 640 bool CastSocket::ProcessBody() { |
| 524 DCHECK_EQ(static_cast<uint32>(body_read_buffer_->offset()), | 641 DCHECK_EQ(static_cast<uint32>(body_read_buffer_->offset()), |
| 525 current_message_size_); | 642 current_message_size_); |
| 526 if (!ParseMessageFromBody()) { | 643 if (!current_message_.ParseFromArray( |
| 527 CloseWithError(cast_channel::CHANNEL_ERROR_INVALID_MESSAGE); | 644 body_read_buffer_->StartOfBuffer(), |
| 645 current_message_size_)) { | |
| 528 return false; | 646 return false; |
| 529 } | 647 } |
| 530 current_message_size_ = 0; | 648 current_message_size_ = 0; |
| 531 header_read_buffer_->set_offset(0); | 649 header_read_buffer_->set_offset(0); |
| 532 body_read_buffer_->set_offset(0); | 650 body_read_buffer_->set_offset(0); |
| 533 current_read_buffer_ = header_read_buffer_; | 651 current_read_buffer_ = header_read_buffer_; |
| 534 return true; | 652 return true; |
| 535 } | 653 } |
| 536 | 654 |
| 537 bool CastSocket::ParseMessageFromBody() { | |
| 538 DCHECK(CalledOnValidThread()); | |
| 539 DCHECK_EQ(static_cast<uint32>(body_read_buffer_->offset()), | |
| 540 current_message_size_); | |
| 541 CastMessage message_proto; | |
| 542 if (!message_proto.ParseFromArray( | |
| 543 body_read_buffer_->StartOfBuffer(), | |
| 544 current_message_size_)) | |
| 545 return false; | |
| 546 VLOG(1) << "Parsed message " << CastMessageToString(message_proto); | |
| 547 // If the message is an auth message then we handle it internally. | |
| 548 if (IsAuthMessage(message_proto)) { | |
| 549 challenge_reply_.reset(new CastMessage(message_proto)); | |
| 550 OnChallengeEvent(net::OK); | |
| 551 } else if (delegate_) { | |
| 552 MessageInfo message; | |
| 553 if (!CastMessageToMessageInfo(message_proto, &message)) | |
| 554 return false; | |
| 555 delegate_->OnMessage(this, message); | |
| 556 } | |
| 557 return true; | |
| 558 } | |
| 559 | |
| 560 // static | 655 // static |
| 561 bool CastSocket::Serialize(const CastMessage& message_proto, | 656 bool CastSocket::Serialize(const CastMessage& message_proto, |
| 562 std::string* message_data) { | 657 std::string* message_data) { |
| 563 DCHECK(message_data); | 658 DCHECK(message_data); |
| 564 message_proto.SerializeToString(message_data); | 659 message_proto.SerializeToString(message_data); |
| 565 size_t message_size = message_data->size(); | 660 size_t message_size = message_data->size(); |
| 566 if (message_size > kMaxMessageSize) { | 661 if (message_size > kMaxMessageSize) { |
| 567 message_data->clear(); | 662 message_data->clear(); |
| 568 return false; | 663 return false; |
| 569 } | 664 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 614 if (!base::StringToInt(port_str, &port)) | 709 if (!base::StringToInt(port_str, &port)) |
| 615 return false; | 710 return false; |
| 616 net::IPAddressNumber ip_address; | 711 net::IPAddressNumber ip_address; |
| 617 if (!net::ParseIPLiteralToNumber(ip_address_str, &ip_address)) | 712 if (!net::ParseIPLiteralToNumber(ip_address_str, &ip_address)) |
| 618 return false; | 713 return false; |
| 619 ip_endpoint_ = net::IPEndPoint(ip_address, port); | 714 ip_endpoint_ = net::IPEndPoint(ip_address, port); |
| 620 return true; | 715 return true; |
| 621 }; | 716 }; |
| 622 | 717 |
| 623 void CastSocket::FillChannelInfo(ChannelInfo* channel_info) const { | 718 void CastSocket::FillChannelInfo(ChannelInfo* channel_info) const { |
| 624 DCHECK(CalledOnValidThread()); | |
| 625 channel_info->channel_id = channel_id_; | 719 channel_info->channel_id = channel_id_; |
| 626 channel_info->url = url_.spec(); | 720 channel_info->url = url_.spec(); |
| 627 channel_info->ready_state = ready_state_; | 721 channel_info->ready_state = ready_state_; |
| 628 channel_info->error_state = error_state_; | 722 channel_info->error_state = error_state_; |
| 629 } | 723 } |
| 630 | 724 |
| 631 bool CastSocket::CalledOnValidThread() const { | 725 bool CastSocket::CalledOnValidThread() const { |
| 632 return thread_checker_.CalledOnValidThread(); | 726 return thread_checker_.CalledOnValidThread(); |
| 633 } | 727 } |
| 634 | 728 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 670 io_buffer = new net::DrainableIOBuffer(new net::StringIOBuffer(message_data), | 764 io_buffer = new net::DrainableIOBuffer(new net::StringIOBuffer(message_data), |
| 671 message_data.size()); | 765 message_data.size()); |
| 672 return true; | 766 return true; |
| 673 } | 767 } |
| 674 | 768 |
| 675 CastSocket::WriteRequest::~WriteRequest() { } | 769 CastSocket::WriteRequest::~WriteRequest() { } |
| 676 | 770 |
| 677 } // namespace cast_channel | 771 } // namespace cast_channel |
| 678 } // namespace api | 772 } // namespace api |
| 679 } // namespace extensions | 773 } // namespace extensions |
| OLD | NEW |