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" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "base/sys_byteorder.h" | 13 #include "base/sys_byteorder.h" |
14 #include "chrome/browser/extensions/api/cast_channel/cast_auth_util.h" | |
14 #include "chrome/browser/extensions/api/cast_channel/cast_channel.pb.h" | 15 #include "chrome/browser/extensions/api/cast_channel/cast_channel.pb.h" |
15 #include "chrome/browser/extensions/api/cast_channel/cast_message_util.h" | 16 #include "chrome/browser/extensions/api/cast_channel/cast_message_util.h" |
16 #include "net/base/address_list.h" | 17 #include "net/base/address_list.h" |
17 #include "net/base/host_port_pair.h" | 18 #include "net/base/host_port_pair.h" |
18 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
19 #include "net/base/net_util.h" | 20 #include "net/base/net_util.h" |
20 #include "net/cert/cert_verifier.h" | 21 #include "net/cert/cert_verifier.h" |
21 #include "net/cert/x509_certificate.h" | 22 #include "net/cert/x509_certificate.h" |
22 #include "net/http/transport_security_state.h" | 23 #include "net/http/transport_security_state.h" |
23 #include "net/socket/client_socket_factory.h" | 24 #include "net/socket/client_socket_factory.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 ApiResourceManager<api::cast_channel::CastSocket>::GetFactoryInstance() { | 59 ApiResourceManager<api::cast_channel::CastSocket>::GetFactoryInstance() { |
59 return &g_factory.Get(); | 60 return &g_factory.Get(); |
60 } | 61 } |
61 | 62 |
62 namespace api { | 63 namespace api { |
63 namespace cast_channel { | 64 namespace cast_channel { |
64 | 65 |
65 const uint32 kMaxMessageSize = 65536; | 66 const uint32 kMaxMessageSize = 65536; |
66 | 67 |
67 CastSocket::CastSocket(const std::string& owner_extension_id, | 68 CastSocket::CastSocket(const std::string& owner_extension_id, |
68 const GURL& url, CastSocket::Delegate* delegate, | 69 const GURL& url, |
70 CastSocket::Delegate* delegate, | |
69 net::NetLog* net_log) : | 71 net::NetLog* net_log) : |
70 ApiResource(owner_extension_id), | 72 ApiResource(owner_extension_id), |
71 channel_id_(0), | 73 channel_id_(0), |
72 url_(url), | 74 url_(url), |
73 delegate_(delegate), | 75 delegate_(delegate), |
74 is_secure_(false), | 76 is_secure_(false), |
75 error_state_(CHANNEL_ERROR_NONE), | 77 error_state_(CHANNEL_ERROR_NONE), |
76 ready_state_(READY_STATE_NONE), | 78 ready_state_(READY_STATE_NONE), |
77 write_callback_pending_(false), | 79 write_callback_pending_(false), |
78 read_callback_pending_(false), | 80 read_callback_pending_(false), |
(...skipping 11 matching lines...) Expand all Loading... | |
90 body_read_buffer_->SetCapacity(kMaxMessageSize); | 92 body_read_buffer_->SetCapacity(kMaxMessageSize); |
91 current_read_buffer_ = header_read_buffer_; | 93 current_read_buffer_ = header_read_buffer_; |
92 } | 94 } |
93 | 95 |
94 CastSocket::~CastSocket() { } | 96 CastSocket::~CastSocket() { } |
95 | 97 |
96 const GURL& CastSocket::url() const { | 98 const GURL& CastSocket::url() const { |
97 return url_; | 99 return url_; |
98 } | 100 } |
99 | 101 |
100 bool CastSocket::ExtractPeerCert(std::string* cert) { | |
101 CHECK(peer_cert_.empty()); | |
102 net::SSLInfo ssl_info; | |
103 if (!socket_->GetSSLInfo(&ssl_info) || !ssl_info.cert.get()) | |
104 return false; | |
105 bool result = net::X509Certificate::GetDEREncoded( | |
106 ssl_info.cert->os_cert_handle(), cert); | |
107 if (result) | |
108 DVLOG(1) << "Successfully extracted peer certificate: " << *cert; | |
109 return result; | |
110 } | |
111 | |
112 scoped_ptr<net::TCPClientSocket> CastSocket::CreateTcpSocket() { | 102 scoped_ptr<net::TCPClientSocket> CastSocket::CreateTcpSocket() { |
113 net::AddressList addresses(ip_endpoint_); | 103 net::AddressList addresses(ip_endpoint_); |
114 scoped_ptr<net::TCPClientSocket> tcp_socket( | 104 scoped_ptr<net::TCPClientSocket> tcp_socket( |
115 new net::TCPClientSocket(addresses, net_log_, net_log_source_)); | 105 new net::TCPClientSocket(addresses, net_log_, net_log_source_)); |
116 // Enable keepalive | 106 // Enable keepalive |
117 tcp_socket->SetKeepAlive(true, kTcpKeepAliveDelaySecs); | 107 tcp_socket->SetKeepAlive(true, kTcpKeepAliveDelaySecs); |
118 return tcp_socket.Pass(); | 108 return tcp_socket.Pass(); |
119 } | 109 } |
120 | 110 |
121 scoped_ptr<net::SSLClientSocket> CastSocket::CreateSslSocket() { | 111 scoped_ptr<net::SSLClientSocket> CastSocket::CreateSslSocket() { |
(...skipping 17 matching lines...) Expand all Loading... | |
139 | 129 |
140 scoped_ptr<net::ClientSocketHandle> connection(new net::ClientSocketHandle); | 130 scoped_ptr<net::ClientSocketHandle> connection(new net::ClientSocketHandle); |
141 connection->SetSocket(tcp_socket_.PassAs<net::StreamSocket>()); | 131 connection->SetSocket(tcp_socket_.PassAs<net::StreamSocket>()); |
142 net::HostPortPair host_and_port = net::HostPortPair::FromIPEndPoint( | 132 net::HostPortPair host_and_port = net::HostPortPair::FromIPEndPoint( |
143 ip_endpoint_); | 133 ip_endpoint_); |
144 | 134 |
145 return net::ClientSocketFactory::GetDefaultFactory()->CreateSSLClientSocket( | 135 return net::ClientSocketFactory::GetDefaultFactory()->CreateSSLClientSocket( |
146 connection.Pass(), host_and_port, ssl_config, context); | 136 connection.Pass(), host_and_port, ssl_config, context); |
147 } | 137 } |
148 | 138 |
139 bool CastSocket::ExtractPeerCert(std::string* cert) { | |
140 CHECK(peer_cert_.empty()); | |
mark a. foltz
2013/10/23 00:33:41
CHECK(cert)
Munjal (Google)
2013/10/23 03:22:55
Done.
| |
141 net::SSLInfo ssl_info; | |
142 if (!socket_->GetSSLInfo(&ssl_info) || !ssl_info.cert.get()) | |
143 return false; | |
144 bool result = net::X509Certificate::GetDEREncoded( | |
145 ssl_info.cert->os_cert_handle(), cert); | |
146 if (result) | |
147 DVLOG(1) << "Successfully extracted peer certificate: " << *cert; | |
148 return result; | |
149 } | |
150 | |
151 int CastSocket::SendAuthChallenge() { | |
152 CastMessage challenge_message; | |
153 CreateAuthChallengeMessage(&challenge_message); | |
154 return SendMessageInternal( | |
155 challenge_message, | |
156 base::Bind(&CastSocket::OnChallengeEvent, AsWeakPtr())); | |
157 } | |
158 | |
159 int CastSocket::ReadAuthChallengeReply() { | |
160 return ReadData(); | |
161 } | |
162 | |
149 void CastSocket::OnConnectComplete(int result) { | 163 void CastSocket::OnConnectComplete(int result) { |
150 int rv = DoConnectLoop(result); | 164 int rv = DoConnectLoop(result); |
151 if (rv != net::ERR_IO_PENDING) | 165 if (rv != net::ERR_IO_PENDING) |
152 DoConnectCallback(rv); | 166 DoConnectCallback(rv); |
153 } | 167 } |
154 | 168 |
169 void CastSocket::OnChallengeEvent(int result) { | |
170 int rv = DoConnectLoop(result); | |
171 if (rv != net::ERR_IO_PENDING) | |
172 DoConnectCallback(rv); | |
173 } | |
174 | |
155 void CastSocket::Connect(const net::CompletionCallback& callback) { | 175 void CastSocket::Connect(const net::CompletionCallback& callback) { |
156 DCHECK(CalledOnValidThread()); | 176 DCHECK(CalledOnValidThread()); |
157 int result = net::ERR_CONNECTION_FAILED; | 177 int result = net::ERR_CONNECTION_FAILED; |
158 DVLOG(1) << "Connect readyState = " << ready_state_; | 178 DVLOG(1) << "Connect readyState = " << ready_state_; |
159 if (ready_state_ != READY_STATE_NONE) { | 179 if (ready_state_ != READY_STATE_NONE) { |
160 callback.Run(result); | 180 callback.Run(result); |
161 return; | 181 return; |
162 } | 182 } |
163 if (!ParseChannelUrl(url_)) { | 183 if (!ParseChannelUrl(url_)) { |
164 CloseWithError(cast_channel::CHANNEL_ERROR_CONNECT_ERROR); | 184 CloseWithError(cast_channel::CHANNEL_ERROR_CONNECT_ERROR); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
198 case CONN_STATE_TCP_CONNECT_COMPLETE: | 218 case CONN_STATE_TCP_CONNECT_COMPLETE: |
199 rv = DoTcpConnectComplete(rv); | 219 rv = DoTcpConnectComplete(rv); |
200 break; | 220 break; |
201 case CONN_STATE_SSL_CONNECT: | 221 case CONN_STATE_SSL_CONNECT: |
202 DCHECK_EQ(net::OK, rv); | 222 DCHECK_EQ(net::OK, rv); |
203 rv = DoSslConnect(); | 223 rv = DoSslConnect(); |
204 break; | 224 break; |
205 case CONN_STATE_SSL_CONNECT_COMPLETE: | 225 case CONN_STATE_SSL_CONNECT_COMPLETE: |
206 rv = DoSslConnectComplete(rv); | 226 rv = DoSslConnectComplete(rv); |
207 break; | 227 break; |
228 case CONN_STATE_AUTH_CHALLENGE_SEND: | |
229 rv = DoAuthChallengeSend(); | |
230 break; | |
231 case CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE: | |
232 rv = DoAuthChallengeSendComplete(rv); | |
233 break; | |
234 case CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE: | |
235 rv = DoAuthChallengeReplyComplete(rv); | |
236 break; | |
237 | |
208 default: | 238 default: |
209 NOTREACHED() << "BUG in CastSocket state machine code"; | 239 NOTREACHED() << "BUG in CastSocket state machine code"; |
210 break; | 240 break; |
211 } | 241 } |
212 } while (rv != net::ERR_IO_PENDING && next_state_ != CONN_STATE_NONE); | 242 } while (rv != net::ERR_IO_PENDING && next_state_ != CONN_STATE_NONE); |
213 // Get out of the loop either when: | 243 // Get out of the loop either when: |
214 // a. A network operation is pending, OR | 244 // a. A network operation is pending, OR |
215 // b. The Do* method called did not change state | 245 // b. The Do* method called did not change state |
216 | 246 |
217 return rv; | 247 return rv; |
(...skipping 13 matching lines...) Expand all Loading... | |
231 } | 261 } |
232 | 262 |
233 int CastSocket::DoSslConnect() { | 263 int CastSocket::DoSslConnect() { |
234 next_state_ = CONN_STATE_SSL_CONNECT_COMPLETE; | 264 next_state_ = CONN_STATE_SSL_CONNECT_COMPLETE; |
235 socket_ = CreateSslSocket(); | 265 socket_ = CreateSslSocket(); |
236 return socket_->Connect( | 266 return socket_->Connect( |
237 base::Bind(&CastSocket::OnConnectComplete, AsWeakPtr())); | 267 base::Bind(&CastSocket::OnConnectComplete, AsWeakPtr())); |
238 } | 268 } |
239 | 269 |
240 int CastSocket::DoSslConnectComplete(int result) { | 270 int CastSocket::DoSslConnectComplete(int result) { |
241 // TODO(mfoltz,munjal): Authenticate the channel if is_secure_ == true. | 271 // TODO(mfoltz,munjal): Authenticate the channel if is_secure_ == true. |
mark a. foltz
2013/10/23 00:33:41
Remove TODO
Munjal (Google)
2013/10/23 03:22:55
Done.
| |
242 if (result == net::ERR_CERT_AUTHORITY_INVALID && | 272 if (result == net::ERR_CERT_AUTHORITY_INVALID && |
243 peer_cert_.empty() && | 273 peer_cert_.empty() && |
244 ExtractPeerCert(&peer_cert_)) { | 274 ExtractPeerCert(&peer_cert_)) { |
245 next_state_ = CONN_STATE_TCP_CONNECT; | 275 next_state_ = CONN_STATE_TCP_CONNECT; |
276 } else if (result == net::OK && is_secure_) { | |
mark a. foltz
2013/10/23 00:33:41
is_secure_ might be better named auth_required_
Munjal (Google)
2013/10/23 03:22:55
Done.
| |
277 next_state_ = CONN_STATE_AUTH_CHALLENGE_SEND; | |
246 } | 278 } |
247 return result; | 279 return result; |
248 } | 280 } |
249 | 281 |
282 int CastSocket::DoAuthChallengeSend() { | |
283 next_state_ = CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE; | |
284 return SendAuthChallenge(); | |
285 } | |
286 | |
287 int CastSocket::DoAuthChallengeSendComplete(int result) { | |
288 if (result != net::OK) | |
289 return result; | |
290 | |
mark a. foltz
2013/10/23 00:33:41
extra newline
Munjal (Google)
2013/10/23 03:22:55
Done.
| |
291 next_state_ = CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE; | |
292 return ReadAuthChallengeReply(); | |
293 } | |
294 | |
295 int CastSocket::DoAuthChallengeReplyComplete(int result) { | |
296 if (result != net::OK) | |
297 return result; | |
298 if (VerifyChallengeReply()) | |
299 return net::OK; | |
300 else | |
301 return net::ERR_FAILED; | |
302 } | |
303 | |
304 bool CastSocket::VerifyChallengeReply() { | |
305 return AuthenticateChallengeReply(*challenge_reply_.get(), peer_cert_); | |
306 } | |
307 | |
250 void CastSocket::DoConnectCallback(int result) { | 308 void CastSocket::DoConnectCallback(int result) { |
251 ready_state_ = (result == net::OK) ? READY_STATE_OPEN : READY_STATE_CLOSED; | 309 ready_state_ = (result == net::OK) ? READY_STATE_OPEN : READY_STATE_CLOSED; |
252 error_state_ = (result == net::OK) ? | 310 error_state_ = (result == net::OK) ? |
253 CHANNEL_ERROR_NONE : CHANNEL_ERROR_CONNECT_ERROR; | 311 CHANNEL_ERROR_NONE : CHANNEL_ERROR_CONNECT_ERROR; |
254 base::ResetAndReturn(&connect_callback_).Run(result); | 312 base::ResetAndReturn(&connect_callback_).Run(result); |
255 if (result == net::OK) | 313 if (result == net::OK) |
256 ReadData(); | 314 ReadData(); |
257 } | 315 } |
258 | 316 |
259 void CastSocket::Close(const net::CompletionCallback& callback) { | 317 void CastSocket::Close(const net::CompletionCallback& callback) { |
260 DCHECK(CalledOnValidThread()); | 318 DCHECK(CalledOnValidThread()); |
261 DVLOG(1) << "Close ReadyState = " << ready_state_; | 319 DVLOG(1) << "Close ReadyState = " << ready_state_; |
262 tcp_socket_.reset(NULL); | 320 tcp_socket_.reset(NULL); |
263 socket_.reset(NULL); | 321 socket_.reset(NULL); |
264 cert_verifier_.reset(NULL); | 322 cert_verifier_.reset(NULL); |
265 transport_security_state_.reset(NULL); | 323 transport_security_state_.reset(NULL); |
266 ready_state_ = READY_STATE_CLOSED; | 324 ready_state_ = READY_STATE_CLOSED; |
267 callback.Run(net::OK); | 325 callback.Run(net::OK); |
268 } | 326 } |
269 | 327 |
270 void CastSocket::SendMessage(const MessageInfo& message, | 328 void CastSocket::SendMessage(const MessageInfo& message, |
271 const net::CompletionCallback& callback) { | 329 const net::CompletionCallback& callback) { |
272 DCHECK(CalledOnValidThread()); | 330 DCHECK(CalledOnValidThread()); |
273 DVLOG(1) << "Send ReadyState " << ready_state_; | 331 DVLOG(1) << "Send ReadyState " << ready_state_; |
274 int result = net::ERR_FAILED; | 332 int result = net::ERR_FAILED; |
275 if (ready_state_ != READY_STATE_OPEN) { | 333 if (ready_state_ != READY_STATE_OPEN) { |
276 callback.Run(result); | 334 callback.Run(result); |
277 return; | 335 return; |
278 } | 336 } |
279 WriteRequest write_request(callback); | |
280 CastMessage message_proto; | 337 CastMessage message_proto; |
281 if (!MessageInfoToCastMessage(message, &message_proto) || | 338 if (!MessageInfoToCastMessage(message, &message_proto)) { |
282 !write_request.SetContent(message_proto)) { | |
283 CloseWithError(cast_channel::CHANNEL_ERROR_INVALID_MESSAGE); | 339 CloseWithError(cast_channel::CHANNEL_ERROR_INVALID_MESSAGE); |
284 // TODO(mfoltz): Do a better job of signaling cast_channel errors to the | 340 // TODO(mfoltz): Do a better job of signaling cast_channel errors to the |
285 // caller. | 341 // caller. |
286 callback.Run(net::OK); | 342 callback.Run(net::OK); |
287 return; | 343 return; |
288 } | 344 } |
289 write_queue_.push(write_request); | 345 SendMessageInternal(message_proto, callback); |
290 WriteData(); | |
291 } | 346 } |
292 | 347 |
293 void CastSocket::WriteData() { | 348 int CastSocket::SendMessageInternal(const CastMessage& message_proto, |
349 const net::CompletionCallback& callback) { | |
350 WriteRequest write_request(callback); | |
351 if (!write_request.SetContent(message_proto)) { | |
352 CloseWithError(cast_channel::CHANNEL_ERROR_INVALID_MESSAGE); | |
353 callback.Run(net::OK); | |
354 return net::ERR_FAILED; | |
355 } | |
356 write_queue_.push(write_request); | |
357 return WriteData(); | |
358 } | |
359 | |
360 int CastSocket::WriteData() { | |
294 DCHECK(CalledOnValidThread()); | 361 DCHECK(CalledOnValidThread()); |
295 DVLOG(1) << "WriteData q = " << write_queue_.size(); | 362 DVLOG(1) << "WriteData q = " << write_queue_.size(); |
296 if (write_queue_.empty() || write_callback_pending_) | 363 if (write_queue_.empty() || write_callback_pending_) |
297 return; | 364 return net::ERR_FAILED; |
298 | 365 |
299 WriteRequest& request = write_queue_.front(); | 366 WriteRequest& request = write_queue_.front(); |
300 if (ready_state_ != READY_STATE_OPEN) { | |
301 request.callback.Run(net::ERR_FAILED); | |
302 return; | |
303 } | |
304 | 367 |
305 DVLOG(1) << "WriteData byte_count = " << request.io_buffer->size() << | 368 DVLOG(1) << "WriteData byte_count = " << request.io_buffer->size() << |
306 " bytes_written " << request.io_buffer->BytesConsumed(); | 369 " bytes_written " << request.io_buffer->BytesConsumed(); |
307 | 370 |
308 write_callback_pending_ = true; | 371 write_callback_pending_ = true; |
309 int result = socket_->Write( | 372 int result = socket_->Write( |
310 request.io_buffer.get(), | 373 request.io_buffer.get(), |
311 request.io_buffer->BytesRemaining(), | 374 request.io_buffer->BytesRemaining(), |
312 base::Bind(&CastSocket::OnWriteData, AsWeakPtr())); | 375 base::Bind(&CastSocket::OnWriteData, AsWeakPtr())); |
313 | 376 |
314 DVLOG(1) << "WriteData result = " << result; | |
315 | |
316 if (result != net::ERR_IO_PENDING) | 377 if (result != net::ERR_IO_PENDING) |
317 OnWriteData(result); | 378 OnWriteData(result); |
379 | |
380 return result; | |
318 } | 381 } |
319 | 382 |
320 void CastSocket::OnWriteData(int result) { | 383 void CastSocket::OnWriteData(int result) { |
321 DCHECK(CalledOnValidThread()); | 384 DCHECK(CalledOnValidThread()); |
322 DVLOG(1) << "OnWriteComplete result = " << result; | 385 DVLOG(1) << "OnWriteComplete result = " << result; |
323 DCHECK(write_callback_pending_); | 386 DCHECK(write_callback_pending_); |
324 DCHECK(!write_queue_.empty()); | 387 DCHECK(!write_queue_.empty()); |
325 write_callback_pending_ = false; | 388 write_callback_pending_ = false; |
326 WriteRequest& request = write_queue_.front(); | 389 WriteRequest& request = write_queue_.front(); |
327 scoped_refptr<net::DrainableIOBuffer> io_buffer = request.io_buffer; | 390 scoped_refptr<net::DrainableIOBuffer> io_buffer = request.io_buffer; |
(...skipping 23 matching lines...) Expand all Loading... | |
351 | 414 |
352 if (result < 0) { | 415 if (result < 0) { |
353 CloseWithError(CHANNEL_ERROR_SOCKET_ERROR); | 416 CloseWithError(CHANNEL_ERROR_SOCKET_ERROR); |
354 return; | 417 return; |
355 } | 418 } |
356 | 419 |
357 if (!write_queue_.empty()) | 420 if (!write_queue_.empty()) |
358 WriteData(); | 421 WriteData(); |
359 } | 422 } |
360 | 423 |
361 void CastSocket::ReadData() { | 424 int CastSocket::ReadData() { |
362 DCHECK(CalledOnValidThread()); | 425 DCHECK(CalledOnValidThread()); |
363 if (!socket_.get() || ready_state_ != READY_STATE_OPEN) { | 426 if (!socket_.get()) |
364 return; | 427 return net::ERR_FAILED; |
365 } | |
366 DCHECK(!read_callback_pending_); | 428 DCHECK(!read_callback_pending_); |
367 read_callback_pending_ = true; | 429 read_callback_pending_ = true; |
368 // Figure out if we are reading the header or body, and the remaining bytes. | 430 // Figure out if we are reading the header or body, and the remaining bytes. |
369 uint32 num_bytes_to_read = 0; | 431 uint32 num_bytes_to_read = 0; |
370 if (header_read_buffer_->RemainingCapacity() > 0) { | 432 if (header_read_buffer_->RemainingCapacity() > 0) { |
371 current_read_buffer_ = header_read_buffer_; | 433 current_read_buffer_ = header_read_buffer_; |
372 num_bytes_to_read = header_read_buffer_->RemainingCapacity(); | 434 num_bytes_to_read = header_read_buffer_->RemainingCapacity(); |
373 DCHECK_LE(num_bytes_to_read, kMessageHeaderSize); | 435 DCHECK_LE(num_bytes_to_read, kMessageHeaderSize); |
374 } else { | 436 } else { |
375 DCHECK_GT(current_message_size_, 0U); | 437 DCHECK_GT(current_message_size_, 0U); |
376 num_bytes_to_read = current_message_size_ - body_read_buffer_->offset(); | 438 num_bytes_to_read = current_message_size_ - body_read_buffer_->offset(); |
377 current_read_buffer_ = body_read_buffer_; | 439 current_read_buffer_ = body_read_buffer_; |
378 DCHECK_LE(num_bytes_to_read, kMaxMessageSize); | 440 DCHECK_LE(num_bytes_to_read, kMaxMessageSize); |
379 } | 441 } |
380 DCHECK_GT(num_bytes_to_read, 0U); | 442 DCHECK_GT(num_bytes_to_read, 0U); |
381 // We read up to num_bytes_to_read into |current_read_buffer_|. | 443 // We read up to num_bytes_to_read into |current_read_buffer_|. |
382 int result = socket_->Read( | 444 int result = socket_->Read( |
383 current_read_buffer_.get(), | 445 current_read_buffer_.get(), |
384 num_bytes_to_read, | 446 num_bytes_to_read, |
385 base::Bind(&CastSocket::OnReadData, AsWeakPtr())); | 447 base::Bind(&CastSocket::OnReadData, AsWeakPtr())); |
386 DVLOG(1) << "ReadData result = " << result; | 448 DVLOG(1) << "ReadData result = " << result; |
387 if (result > 0) { | 449 if (result > 0) { |
388 OnReadData(result); | 450 OnReadData(result); |
389 } else if (result != net::ERR_IO_PENDING) { | 451 } else if (result != net::ERR_IO_PENDING) { |
390 CloseWithError(CHANNEL_ERROR_SOCKET_ERROR); | 452 CloseWithError(CHANNEL_ERROR_SOCKET_ERROR); |
391 } | 453 } |
454 return result; | |
392 } | 455 } |
393 | 456 |
394 void CastSocket::OnReadData(int result) { | 457 void CastSocket::OnReadData(int result) { |
395 DCHECK(CalledOnValidThread()); | 458 DCHECK(CalledOnValidThread()); |
396 DVLOG(1) << "OnReadData result = " << result << | 459 DVLOG(1) << "OnReadData result = " << result |
397 " header offset = " << header_read_buffer_->offset() << | 460 << " header offset = " << header_read_buffer_->offset() |
398 " body offset = " << body_read_buffer_->offset(); | 461 << " body offset = " << body_read_buffer_->offset(); |
399 read_callback_pending_ = false; | 462 read_callback_pending_ = false; |
400 if (result <= 0) { | 463 if (result <= 0) { |
401 CloseWithError(CHANNEL_ERROR_SOCKET_ERROR); | 464 CloseWithError(CHANNEL_ERROR_SOCKET_ERROR); |
402 return; | 465 return; |
403 } | 466 } |
404 // We read some data. Move the offset in the current buffer forward. | 467 // We read some data. Move the offset in the current buffer forward. |
405 DCHECK_LE(current_read_buffer_->offset() + result, | 468 DCHECK_LE(current_read_buffer_->offset() + result, |
406 current_read_buffer_->capacity()); | 469 current_read_buffer_->capacity()); |
407 current_read_buffer_->set_offset(current_read_buffer_->offset() + result); | 470 current_read_buffer_->set_offset(current_read_buffer_->offset() + result); |
408 | 471 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
451 | 514 |
452 bool CastSocket::ParseMessageFromBody() { | 515 bool CastSocket::ParseMessageFromBody() { |
453 DCHECK(CalledOnValidThread()); | 516 DCHECK(CalledOnValidThread()); |
454 DCHECK_EQ(static_cast<uint32>(body_read_buffer_->offset()), | 517 DCHECK_EQ(static_cast<uint32>(body_read_buffer_->offset()), |
455 current_message_size_); | 518 current_message_size_); |
456 CastMessage message_proto; | 519 CastMessage message_proto; |
457 if (!message_proto.ParseFromArray( | 520 if (!message_proto.ParseFromArray( |
458 body_read_buffer_->StartOfBuffer(), | 521 body_read_buffer_->StartOfBuffer(), |
459 current_message_size_)) | 522 current_message_size_)) |
460 return false; | 523 return false; |
461 DVLOG(1) << "Parsed message " << MessageProtoToString(message_proto); | 524 DVLOG(1) << "Parsed message " << CastMessageToString(message_proto); |
462 if (delegate_) { | 525 // If the message is an auth message then we handle it internally. |
526 if (IsAuthMessage(message_proto)) { | |
527 challenge_reply_.reset(new CastMessage(message_proto)); | |
528 OnChallengeEvent(net::OK); | |
529 } else if (delegate_) { | |
463 MessageInfo message; | 530 MessageInfo message; |
464 if (!CastMessageToMessageInfo(message_proto, &message)) | 531 if (!CastMessageToMessageInfo(message_proto, &message)) |
465 return false; | 532 return false; |
466 delegate_->OnMessage(this, message); | 533 delegate_->OnMessage(this, message); |
467 } | 534 } |
468 return true; | 535 return true; |
469 } | 536 } |
470 | 537 |
471 // static | 538 // static |
472 bool CastSocket::Serialize(const CastMessage& message_proto, | 539 bool CastSocket::Serialize(const CastMessage& message_proto, |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
577 io_buffer = new net::DrainableIOBuffer(new net::StringIOBuffer(message_data), | 644 io_buffer = new net::DrainableIOBuffer(new net::StringIOBuffer(message_data), |
578 message_data.size()); | 645 message_data.size()); |
579 return true; | 646 return true; |
580 } | 647 } |
581 | 648 |
582 CastSocket::WriteRequest::~WriteRequest() { } | 649 CastSocket::WriteRequest::~WriteRequest() { } |
583 | 650 |
584 } // namespace cast_channel | 651 } // namespace cast_channel |
585 } // namespace api | 652 } // namespace api |
586 } // namespace extensions | 653 } // namespace extensions |
OLD | NEW |