Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(324)

Side by Side Diff: chrome/browser/extensions/api/cast_channel/cast_socket.cc

Issue 160973002: Two chagnes to cast socket: (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 // sizeof() for the fields. 64 // sizeof() for the fields.
65 const uint32 kMessageHeaderSize = sizeof(uint32); 65 const uint32 kMessageHeaderSize = sizeof(uint32);
66 66
67 CastSocket::CastSocket(const std::string& owner_extension_id, 67 CastSocket::CastSocket(const std::string& owner_extension_id,
68 const GURL& url, 68 const GURL& url,
69 CastSocket::Delegate* delegate, 69 CastSocket::Delegate* delegate,
70 net::NetLog* net_log) : 70 net::NetLog* net_log) :
71 ApiResource(owner_extension_id), 71 ApiResource(owner_extension_id),
72 channel_id_(0), 72 channel_id_(0),
73 url_(url), 73 url_(url),
74 url_log_str_("[URL: " + url.spec() + "] "),
Wez 2014/02/12 21:13:16 nit: Is "URL:" redundant here?
mark a. foltz 2014/02/12 21:45:02 This is really only for the purposes of logging an
Wez 2014/02/12 21:52:14 +1
Munjal (Google) 2014/02/12 22:34:36 Done. Started doing that but thought I cannot do
Munjal (Google) 2014/02/12 22:34:36 Done.
74 delegate_(delegate), 75 delegate_(delegate),
75 auth_required_(false), 76 auth_required_(false),
76 current_message_size_(0), 77 current_message_size_(0),
78 current_message_(new CastMessage()),
77 net_log_(net_log), 79 net_log_(net_log),
78 connect_state_(CONN_STATE_NONE), 80 connect_state_(CONN_STATE_NONE),
79 write_state_(WRITE_STATE_NONE), 81 write_state_(WRITE_STATE_NONE),
80 read_state_(READ_STATE_NONE), 82 read_state_(READ_STATE_NONE),
81 error_state_(CHANNEL_ERROR_NONE), 83 error_state_(CHANNEL_ERROR_NONE),
82 ready_state_(READY_STATE_NONE) { 84 ready_state_(READY_STATE_NONE) {
83 DCHECK(net_log_); 85 DCHECK(net_log_);
84 net_log_source_.type = net::NetLog::SOURCE_SOCKET; 86 net_log_source_.type = net::NetLog::SOURCE_SOCKET;
85 net_log_source_.id = net_log_->NextID(); 87 net_log_source_.id = net_log_->NextID();
86 88
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 140
139 bool CastSocket::ExtractPeerCert(std::string* cert) { 141 bool CastSocket::ExtractPeerCert(std::string* cert) {
140 DCHECK(cert); 142 DCHECK(cert);
141 DCHECK(peer_cert_.empty()); 143 DCHECK(peer_cert_.empty());
142 net::SSLInfo ssl_info; 144 net::SSLInfo ssl_info;
143 if (!socket_->GetSSLInfo(&ssl_info) || !ssl_info.cert.get()) 145 if (!socket_->GetSSLInfo(&ssl_info) || !ssl_info.cert.get())
144 return false; 146 return false;
145 bool result = net::X509Certificate::GetDEREncoded( 147 bool result = net::X509Certificate::GetDEREncoded(
146 ssl_info.cert->os_cert_handle(), cert); 148 ssl_info.cert->os_cert_handle(), cert);
147 if (result) 149 if (result)
148 VLOG(1) << "[URL: " << url_.spec() 150 VLOG(1) << url_log_str_
149 << "] Successfully extracted peer certificate: " << *cert; 151 << "Successfully extracted peer certificate: " << *cert;
150 return result; 152 return result;
151 } 153 }
152 154
153 bool CastSocket::VerifyChallengeReply() { 155 bool CastSocket::VerifyChallengeReply() {
154 return AuthenticateChallengeReply(*challenge_reply_.get(), peer_cert_); 156 return AuthenticateChallengeReply(*challenge_reply_.get(), peer_cert_);
155 } 157 }
156 158
157 void CastSocket::Connect(const net::CompletionCallback& callback) { 159 void CastSocket::Connect(const net::CompletionCallback& callback) {
158 DCHECK(CalledOnValidThread()); 160 DCHECK(CalledOnValidThread());
159 VLOG(1) << "Connect readyState = " << ready_state_; 161 VLOG(1) << url_log_str_ << "Connect readyState = " << ready_state_;
160 if (ready_state_ != READY_STATE_NONE) { 162 if (ready_state_ != READY_STATE_NONE) {
161 callback.Run(net::ERR_CONNECTION_FAILED); 163 callback.Run(net::ERR_CONNECTION_FAILED);
162 return; 164 return;
163 } 165 }
164 if (!ParseChannelUrl(url_)) { 166 if (!ParseChannelUrl(url_)) {
165 callback.Run(net::ERR_CONNECTION_FAILED); 167 callback.Run(net::ERR_CONNECTION_FAILED);
166 return; 168 return;
167 } 169 }
168 170
169 ready_state_ = READY_STATE_CONNECTING; 171 ready_state_ = READY_STATE_CONNECTING;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 // Get out of the loop either when: 227 // Get out of the loop either when:
226 // a. A network operation is pending, OR 228 // a. A network operation is pending, OR
227 // b. The Do* method called did not change state 229 // b. The Do* method called did not change state
228 230
229 // Connect loop is finished: if there is no pending IO invoke the callback. 231 // Connect loop is finished: if there is no pending IO invoke the callback.
230 if (rv != net::ERR_IO_PENDING) 232 if (rv != net::ERR_IO_PENDING)
231 DoConnectCallback(rv); 233 DoConnectCallback(rv);
232 } 234 }
233 235
234 int CastSocket::DoTcpConnect() { 236 int CastSocket::DoTcpConnect() {
235 VLOG(1) << "DoTcpConnect"; 237 VLOG(1) << url_log_str_ << "DoTcpConnect";
236 connect_state_ = CONN_STATE_TCP_CONNECT_COMPLETE; 238 connect_state_ = CONN_STATE_TCP_CONNECT_COMPLETE;
237 tcp_socket_ = CreateTcpSocket(); 239 tcp_socket_ = CreateTcpSocket();
238 return tcp_socket_->Connect( 240 return tcp_socket_->Connect(
239 base::Bind(&CastSocket::DoConnectLoop, AsWeakPtr())); 241 base::Bind(&CastSocket::DoConnectLoop, AsWeakPtr()));
240 } 242 }
241 243
242 int CastSocket::DoTcpConnectComplete(int result) { 244 int CastSocket::DoTcpConnectComplete(int result) {
243 VLOG(1) << "DoTcpConnectComplete: " << result; 245 VLOG(1) << url_log_str_ << "DoTcpConnectComplete: " << result;
244 if (result == net::OK) { 246 if (result == net::OK) {
245 // Enable TCP protocol-level keep-alive. 247 // Enable TCP protocol-level keep-alive.
246 bool result = tcp_socket_->SetKeepAlive(true, kTcpKeepAliveDelaySecs); 248 bool result = tcp_socket_->SetKeepAlive(true, kTcpKeepAliveDelaySecs);
247 LOG_IF(WARNING, !result) << "Failed to SetKeepAlive."; 249 LOG_IF(WARNING, !result) << "Failed to SetKeepAlive.";
248 connect_state_ = CONN_STATE_SSL_CONNECT; 250 connect_state_ = CONN_STATE_SSL_CONNECT;
249 } 251 }
250 return result; 252 return result;
251 } 253 }
252 254
253 int CastSocket::DoSslConnect() { 255 int CastSocket::DoSslConnect() {
254 VLOG(1) << "DoSslConnect"; 256 VLOG(1) << url_log_str_ << "DoSslConnect";
255 connect_state_ = CONN_STATE_SSL_CONNECT_COMPLETE; 257 connect_state_ = CONN_STATE_SSL_CONNECT_COMPLETE;
256 socket_ = CreateSslSocket(tcp_socket_.PassAs<net::StreamSocket>()); 258 socket_ = CreateSslSocket(tcp_socket_.PassAs<net::StreamSocket>());
257 return socket_->Connect( 259 return socket_->Connect(
258 base::Bind(&CastSocket::DoConnectLoop, AsWeakPtr())); 260 base::Bind(&CastSocket::DoConnectLoop, AsWeakPtr()));
259 } 261 }
260 262
261 int CastSocket::DoSslConnectComplete(int result) { 263 int CastSocket::DoSslConnectComplete(int result) {
262 VLOG(1) << "DoSslConnectComplete: " << result; 264 VLOG(1) << url_log_str_ << "DoSslConnectComplete: " << result;
263 if (result == net::ERR_CERT_AUTHORITY_INVALID && 265 if (result == net::ERR_CERT_AUTHORITY_INVALID &&
264 peer_cert_.empty() && 266 peer_cert_.empty() &&
265 ExtractPeerCert(&peer_cert_)) { 267 ExtractPeerCert(&peer_cert_)) {
266 connect_state_ = CONN_STATE_TCP_CONNECT; 268 connect_state_ = CONN_STATE_TCP_CONNECT;
267 } else if (result == net::OK && auth_required_) { 269 } else if (result == net::OK && auth_required_) {
268 connect_state_ = CONN_STATE_AUTH_CHALLENGE_SEND; 270 connect_state_ = CONN_STATE_AUTH_CHALLENGE_SEND;
269 } 271 }
270 return result; 272 return result;
271 } 273 }
272 274
273 int CastSocket::DoAuthChallengeSend() { 275 int CastSocket::DoAuthChallengeSend() {
274 VLOG(1) << "DoAuthChallengeSend"; 276 VLOG(1) << url_log_str_ << "DoAuthChallengeSend";
275 connect_state_ = CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE; 277 connect_state_ = CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE;
276 CastMessage challenge_message; 278 CastMessage challenge_message;
277 CreateAuthChallengeMessage(&challenge_message); 279 CreateAuthChallengeMessage(&challenge_message);
278 VLOG(1) << "Sending challenge: " << CastMessageToString(challenge_message); 280 VLOG(1) << url_log_str_
281 << "Sending challenge: " << CastMessageToString(challenge_message);
279 // Post a task to send auth challenge so that DoWriteLoop is not nested inside 282 // Post a task to send auth challenge so that DoWriteLoop is not nested inside
280 // DoConnectLoop. This is not strictly necessary but keeps the write loop 283 // DoConnectLoop. This is not strictly necessary but keeps the write loop
281 // code decoupled from connect loop code. 284 // code decoupled from connect loop code.
282 base::MessageLoop::current()->PostTask( 285 base::MessageLoop::current()->PostTask(
283 FROM_HERE, 286 FROM_HERE,
284 base::Bind(&CastSocket::SendCastMessageInternal, AsWeakPtr(), 287 base::Bind(&CastSocket::SendCastMessageInternal, AsWeakPtr(),
285 challenge_message, 288 challenge_message,
286 base::Bind(&CastSocket::DoConnectLoop, AsWeakPtr()))); 289 base::Bind(&CastSocket::DoConnectLoop, AsWeakPtr())));
287 // Always return IO_PENDING since the result is always asynchronous. 290 // Always return IO_PENDING since the result is always asynchronous.
288 return net::ERR_IO_PENDING; 291 return net::ERR_IO_PENDING;
289 } 292 }
290 293
291 int CastSocket::DoAuthChallengeSendComplete(int result) { 294 int CastSocket::DoAuthChallengeSendComplete(int result) {
292 VLOG(1) << "DoAuthChallengeSendComplete: " << result; 295 VLOG(1) << url_log_str_ << "DoAuthChallengeSendComplete: " << result;
293 if (result < 0) 296 if (result < 0)
294 return result; 297 return result;
295 connect_state_ = CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE; 298 connect_state_ = CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE;
296 // Post a task to start read loop so that DoReadLoop is not nested inside 299 // Post a task to start read loop so that DoReadLoop is not nested inside
297 // DoConnectLoop. This is not strictly necessary but keeps the read loop 300 // DoConnectLoop. This is not strictly necessary but keeps the read loop
298 // code decoupled from connect loop code. 301 // code decoupled from connect loop code.
299 PostTaskToStartReadLoop(); 302 PostTaskToStartReadLoop();
300 // Always return IO_PENDING since the result is always asynchronous. 303 // Always return IO_PENDING since the result is always asynchronous.
301 return net::ERR_IO_PENDING; 304 return net::ERR_IO_PENDING;
302 } 305 }
303 306
304 int CastSocket::DoAuthChallengeReplyComplete(int result) { 307 int CastSocket::DoAuthChallengeReplyComplete(int result) {
305 VLOG(1) << "DoAuthChallengeReplyComplete: " << result; 308 VLOG(1) << url_log_str_ << "DoAuthChallengeReplyComplete: " << result;
306 if (result < 0) 309 if (result < 0)
307 return result; 310 return result;
308 if (!VerifyChallengeReply()) 311 if (!VerifyChallengeReply())
309 return net::ERR_FAILED; 312 return net::ERR_FAILED;
310 VLOG(1) << "Auth challenge verification succeeded"; 313 VLOG(1) << url_log_str_ << "Auth challenge verification succeeded";
311 return net::OK; 314 return net::OK;
312 } 315 }
313 316
314 void CastSocket::DoConnectCallback(int result) { 317 void CastSocket::DoConnectCallback(int result) {
315 ready_state_ = (result == net::OK) ? READY_STATE_OPEN : READY_STATE_CLOSED; 318 ready_state_ = (result == net::OK) ? READY_STATE_OPEN : READY_STATE_CLOSED;
316 error_state_ = (result == net::OK) ? 319 error_state_ = (result == net::OK) ?
317 CHANNEL_ERROR_NONE : CHANNEL_ERROR_CONNECT_ERROR; 320 CHANNEL_ERROR_NONE : CHANNEL_ERROR_CONNECT_ERROR;
318 if (result == net::OK) // Start the read loop 321 if (result == net::OK) // Start the read loop
319 PostTaskToStartReadLoop(); 322 PostTaskToStartReadLoop();
320 base::ResetAndReturn(&connect_callback_).Run(result); 323 base::ResetAndReturn(&connect_callback_).Run(result);
321 } 324 }
322 325
323 void CastSocket::Close(const net::CompletionCallback& callback) { 326 void CastSocket::Close(const net::CompletionCallback& callback) {
324 DCHECK(CalledOnValidThread()); 327 DCHECK(CalledOnValidThread());
325 VLOG(1) << "Close ReadyState = " << ready_state_; 328 VLOG(1) << url_log_str_ << "Close ReadyState = " << ready_state_;
326 tcp_socket_.reset(); 329 tcp_socket_.reset();
327 socket_.reset(); 330 socket_.reset();
328 cert_verifier_.reset(); 331 cert_verifier_.reset();
329 transport_security_state_.reset(); 332 transport_security_state_.reset();
330 ready_state_ = READY_STATE_CLOSED; 333 ready_state_ = READY_STATE_CLOSED;
331 callback.Run(net::OK); 334 callback.Run(net::OK);
332 // |callback| can delete |this| 335 // |callback| can delete |this|
333 } 336 }
334 337
335 void CastSocket::SendMessage(const MessageInfo& message, 338 void CastSocket::SendMessage(const MessageInfo& message,
(...skipping 23 matching lines...) Expand all
359 362
360 write_queue_.push(write_request); 363 write_queue_.push(write_request);
361 if (write_state_ == WRITE_STATE_NONE) { 364 if (write_state_ == WRITE_STATE_NONE) {
362 write_state_ = WRITE_STATE_WRITE; 365 write_state_ = WRITE_STATE_WRITE;
363 DoWriteLoop(net::OK); 366 DoWriteLoop(net::OK);
364 } 367 }
365 } 368 }
366 369
367 void CastSocket::DoWriteLoop(int result) { 370 void CastSocket::DoWriteLoop(int result) {
368 DCHECK(CalledOnValidThread()); 371 DCHECK(CalledOnValidThread());
369 VLOG(1) << "WriteData q = " << write_queue_.size(); 372 VLOG(1) << url_log_str_ << "DoWriteLoop queue size: " << write_queue_.size();
370 373
371 if (write_queue_.empty()) { 374 if (write_queue_.empty()) {
372 write_state_ = WRITE_STATE_NONE; 375 write_state_ = WRITE_STATE_NONE;
373 return; 376 return;
374 } 377 }
375 378
376 // Network operations can either finish synchronously or asynchronously. 379 // Network operations can either finish synchronously or asynchronously.
377 // This method executes the state machine transitions in a loop so that 380 // This method executes the state machine transitions in a loop so that
378 // write state transitions happen even when network operations finish 381 // write state transitions happen even when network operations finish
379 // synchronously. 382 // synchronously.
(...skipping 29 matching lines...) Expand all
409 412
410 // Write loop is done - if the result is ERR_FAILED then close with error. 413 // Write loop is done - if the result is ERR_FAILED then close with error.
411 if (rv == net::ERR_FAILED) 414 if (rv == net::ERR_FAILED)
412 CloseWithError(error_state_); 415 CloseWithError(error_state_);
413 } 416 }
414 417
415 int CastSocket::DoWrite() { 418 int CastSocket::DoWrite() {
416 DCHECK(!write_queue_.empty()); 419 DCHECK(!write_queue_.empty());
417 WriteRequest& request = write_queue_.front(); 420 WriteRequest& request = write_queue_.front();
418 421
419 VLOG(1) << "WriteData byte_count = " << request.io_buffer->size() 422 VLOG(2) << url_log_str_
420 << " bytes_written " << request.io_buffer->BytesConsumed(); 423 << "WriteData byte_count = " << request.io_buffer->size()
424 << " bytes_written " << request.io_buffer->BytesConsumed();
421 425
422 write_state_ = WRITE_STATE_WRITE_COMPLETE; 426 write_state_ = WRITE_STATE_WRITE_COMPLETE;
423 427
424 return socket_->Write( 428 return socket_->Write(
425 request.io_buffer.get(), 429 request.io_buffer.get(),
426 request.io_buffer->BytesRemaining(), 430 request.io_buffer->BytesRemaining(),
427 base::Bind(&CastSocket::DoWriteLoop, AsWeakPtr())); 431 base::Bind(&CastSocket::DoWriteLoop, AsWeakPtr()));
428 } 432 }
429 433
430 int CastSocket::DoWriteComplete(int result) { 434 int CastSocket::DoWriteComplete(int result) {
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 DCHECK_GT(num_bytes_to_read, 0U); 561 DCHECK_GT(num_bytes_to_read, 0U);
558 562
559 // Read up to num_bytes_to_read into |current_read_buffer_|. 563 // Read up to num_bytes_to_read into |current_read_buffer_|.
560 return socket_->Read( 564 return socket_->Read(
561 current_read_buffer_.get(), 565 current_read_buffer_.get(),
562 num_bytes_to_read, 566 num_bytes_to_read,
563 base::Bind(&CastSocket::DoReadLoop, AsWeakPtr())); 567 base::Bind(&CastSocket::DoReadLoop, AsWeakPtr()));
564 } 568 }
565 569
566 int CastSocket::DoReadComplete(int result) { 570 int CastSocket::DoReadComplete(int result) {
567 VLOG(1) << "DoReadDataComplete result = " << result 571 VLOG(2) << url_log_str_ << "DoReadComplete result = " << result
568 << " header offset = " << header_read_buffer_->offset() 572 << " header offset = " << header_read_buffer_->offset()
569 << " body offset = " << body_read_buffer_->offset(); 573 << " body offset = " << body_read_buffer_->offset();
570 if (result <= 0) { // 0 means EOF: the peer closed the socket 574 if (result <= 0) { // 0 means EOF: the peer closed the socket
575 VLOG(1) << url_log_str_ << "Read error, peer closed the socket";
571 error_state_ = CHANNEL_ERROR_SOCKET_ERROR; 576 error_state_ = CHANNEL_ERROR_SOCKET_ERROR;
572 read_state_ = READ_STATE_ERROR; 577 read_state_ = READ_STATE_ERROR;
573 return result == 0 ? net::ERR_FAILED : result; 578 return result == 0 ? net::ERR_FAILED : result;
574 } 579 }
575 580
576 // Some data was read. Move the offset in the current buffer forward. 581 // Some data was read. Move the offset in the current buffer forward.
577 DCHECK_LE(current_read_buffer_->offset() + result, 582 DCHECK_LE(current_read_buffer_->offset() + result,
578 current_read_buffer_->capacity()); 583 current_read_buffer_->capacity());
579 current_read_buffer_->set_offset(current_read_buffer_->offset() + result); 584 current_read_buffer_->set_offset(current_read_buffer_->offset() + result);
580 read_state_ = READ_STATE_READ; 585 read_state_ = READ_STATE_READ;
(...skipping 15 matching lines...) Expand all
596 error_state_ = cast_channel::CHANNEL_ERROR_INVALID_MESSAGE; 601 error_state_ = cast_channel::CHANNEL_ERROR_INVALID_MESSAGE;
597 read_state_ = READ_STATE_ERROR; 602 read_state_ = READ_STATE_ERROR;
598 } 603 }
599 } 604 }
600 605
601 return net::OK; 606 return net::OK;
602 } 607 }
603 608
604 int CastSocket::DoReadCallback() { 609 int CastSocket::DoReadCallback() {
605 read_state_ = READ_STATE_READ; 610 read_state_ = READ_STATE_READ;
606 if (IsAuthMessage(current_message_)) { 611 const CastMessage& message = *(current_message_.get());
612 if (IsAuthMessage(message)) {
607 // An auth message is received, check that connect flow is running. 613 // An auth message is received, check that connect flow is running.
608 if (ready_state_ == READY_STATE_CONNECTING) { 614 if (ready_state_ == READY_STATE_CONNECTING) {
609 challenge_reply_.reset(new CastMessage(current_message_)); 615 challenge_reply_.reset(new CastMessage(message));
610 PostTaskToStartConnectLoop(net::OK); 616 PostTaskToStartConnectLoop(net::OK);
611 } else { 617 } else {
612 read_state_ = READ_STATE_ERROR; 618 read_state_ = READ_STATE_ERROR;
613 } 619 }
614 } else if (delegate_) { 620 } else if (delegate_) {
615 MessageInfo message; 621 MessageInfo message_info;
616 if (CastMessageToMessageInfo(current_message_, &message)) 622 if (CastMessageToMessageInfo(message, &message_info))
617 delegate_->OnMessage(this, message); 623 delegate_->OnMessage(this, message_info);
618 else 624 else
619 read_state_ = READ_STATE_ERROR; 625 read_state_ = READ_STATE_ERROR;
620 } 626 }
621 current_message_.Clear(); 627 current_message_->Clear();
Wez 2014/02/12 21:13:16 Rather than clearing & re-using the CastMessage, w
Munjal (Google) 2014/02/12 22:34:36 So that we don't create many objects on the heap e
622 return net::OK; 628 return net::OK;
623 } 629 }
624 630
625 int CastSocket::DoReadError(int result) { 631 int CastSocket::DoReadError(int result) {
626 DCHECK_LE(result, 0); 632 DCHECK_LE(result, 0);
627 // If inside connection flow, then get back to connect loop. 633 // If inside connection flow, then get back to connect loop.
628 if (ready_state_ == READY_STATE_CONNECTING) { 634 if (ready_state_ == READY_STATE_CONNECTING) {
629 PostTaskToStartConnectLoop(result); 635 PostTaskToStartConnectLoop(result);
630 // does not try to report error also. 636 // does not try to report error also.
631 return net::OK; 637 return net::OK;
632 } 638 }
633 return net::ERR_FAILED; 639 return net::ERR_FAILED;
634 } 640 }
635 641
636 bool CastSocket::ProcessHeader() { 642 bool CastSocket::ProcessHeader() {
637 DCHECK_EQ(static_cast<uint32>(header_read_buffer_->offset()), 643 DCHECK_EQ(static_cast<uint32>(header_read_buffer_->offset()),
638 kMessageHeaderSize); 644 kMessageHeaderSize);
639 MessageHeader header; 645 MessageHeader header;
640 MessageHeader::ReadFromIOBuffer(header_read_buffer_.get(), &header); 646 MessageHeader::ReadFromIOBuffer(header_read_buffer_.get(), &header);
641 if (header.message_size > kMaxMessageSize) 647 if (header.message_size > kMaxMessageSize)
642 return false; 648 return false;
643 649
644 VLOG(1) << "Parsed header { message_size: " << header.message_size << " }"; 650 VLOG(2) << url_log_str_
651 << "Parsed header { message_size: " << header.message_size << " }";
645 current_message_size_ = header.message_size; 652 current_message_size_ = header.message_size;
646 return true; 653 return true;
647 } 654 }
648 655
649 bool CastSocket::ProcessBody() { 656 bool CastSocket::ProcessBody() {
650 DCHECK_EQ(static_cast<uint32>(body_read_buffer_->offset()), 657 DCHECK_EQ(static_cast<uint32>(body_read_buffer_->offset()),
651 current_message_size_); 658 current_message_size_);
652 if (!current_message_.ParseFromArray( 659 if (!current_message_->ParseFromArray(
653 body_read_buffer_->StartOfBuffer(), current_message_size_)) { 660 body_read_buffer_->StartOfBuffer(), current_message_size_)) {
654 return false; 661 return false;
655 } 662 }
656 current_message_size_ = 0; 663 current_message_size_ = 0;
657 header_read_buffer_->set_offset(0); 664 header_read_buffer_->set_offset(0);
658 body_read_buffer_->set_offset(0); 665 body_read_buffer_->set_offset(0);
659 current_read_buffer_ = header_read_buffer_; 666 current_read_buffer_ = header_read_buffer_;
660 return true; 667 return true;
661 } 668 }
662 669
(...skipping 16 matching lines...) Expand all
679 void CastSocket::CloseWithError(ChannelError error) { 686 void CastSocket::CloseWithError(ChannelError error) {
680 DCHECK(CalledOnValidThread()); 687 DCHECK(CalledOnValidThread());
681 socket_.reset(NULL); 688 socket_.reset(NULL);
682 ready_state_ = READY_STATE_CLOSED; 689 ready_state_ = READY_STATE_CLOSED;
683 error_state_ = error; 690 error_state_ = error;
684 if (delegate_) 691 if (delegate_)
685 delegate_->OnError(this, error); 692 delegate_->OnError(this, error);
686 } 693 }
687 694
688 bool CastSocket::ParseChannelUrl(const GURL& url) { 695 bool CastSocket::ParseChannelUrl(const GURL& url) {
689 VLOG(1) << "url = " + url.spec(); 696 VLOG(2) << url_log_str_ << "ParseChannelUrl";
690 if (url.SchemeIs(kCastInsecureScheme)) { 697 if (url.SchemeIs(kCastInsecureScheme)) {
691 auth_required_ = false; 698 auth_required_ = false;
692 } else if (url.SchemeIs(kCastSecureScheme)) { 699 } else if (url.SchemeIs(kCastSecureScheme)) {
693 auth_required_ = true; 700 auth_required_ = true;
694 } else { 701 } else {
695 return false; 702 return false;
696 } 703 }
697 // TODO(mfoltz): Manual parsing, yech. Register cast[s] as standard schemes? 704 // TODO(mfoltz): Manual parsing, yech. Register cast[s] as standard schemes?
698 // TODO(mfoltz): Test for IPv6 addresses. Brackets or no brackets? 705 // TODO(mfoltz): Test for IPv6 addresses. Brackets or no brackets?
699 // TODO(mfoltz): Maybe enforce restriction to IPv4 private and IPv6 link-local 706 // TODO(mfoltz): Maybe enforce restriction to IPv4 private and IPv6
700 // networks 707 // link-local networks
Wez 2014/02/12 21:13:16 Did this need re-wrapping?
Munjal (Google) 2014/02/12 22:34:36 Yes, I think it was 81 without wrapping.
701 const std::string& path = url.path(); 708 const std::string& path = url.path();
702 // Shortest possible: //A:B 709 // Shortest possible: //A:B
703 if (path.size() < 5) { 710 if (path.size() < 5) {
704 return false; 711 return false;
705 } 712 }
706 if (path.find("//") != 0) { 713 if (path.find("//") != 0) {
707 return false; 714 return false;
708 } 715 }
709 size_t colon = path.find_last_of(':'); 716 size_t colon = path.find_last_of(':');
710 if (colon == std::string::npos || colon < 3 || colon > path.size() - 2) { 717 if (colon == std::string::npos || colon < 3 || colon > path.size() - 2) {
711 return false; 718 return false;
712 } 719 }
713 const std::string& ip_address_str = path.substr(2, colon - 2); 720 const std::string& ip_address_str = path.substr(2, colon - 2);
714 const std::string& port_str = path.substr(colon + 1); 721 const std::string& port_str = path.substr(colon + 1);
715 VLOG(1) << "addr " << ip_address_str << " port " << port_str; 722 VLOG(2) << url_log_str_ << "IP: " << ip_address_str << " Port: " << port_str;
716 int port; 723 int port;
717 if (!base::StringToInt(port_str, &port)) 724 if (!base::StringToInt(port_str, &port))
718 return false; 725 return false;
719 net::IPAddressNumber ip_address; 726 net::IPAddressNumber ip_address;
720 if (!net::ParseIPLiteralToNumber(ip_address_str, &ip_address)) 727 if (!net::ParseIPLiteralToNumber(ip_address_str, &ip_address))
721 return false; 728 return false;
722 ip_endpoint_ = net::IPEndPoint(ip_address, port); 729 ip_endpoint_ = net::IPEndPoint(ip_address, port);
723 return true; 730 return true;
724 }; 731 };
725 732
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
772 io_buffer = new net::DrainableIOBuffer(new net::StringIOBuffer(message_data), 779 io_buffer = new net::DrainableIOBuffer(new net::StringIOBuffer(message_data),
773 message_data.size()); 780 message_data.size());
774 return true; 781 return true;
775 } 782 }
776 783
777 CastSocket::WriteRequest::~WriteRequest() { } 784 CastSocket::WriteRequest::~WriteRequest() { }
778 785
779 } // namespace cast_channel 786 } // namespace cast_channel
780 } // namespace api 787 } // namespace api
781 } // namespace extensions 788 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698