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 "google_apis/gcm/engine/connection_handler_impl.h" | 5 #include "google_apis/gcm/engine/connection_handler_impl.h" |
6 | 6 |
| 7 #include <utility> |
| 8 |
7 #include "base/location.h" | 9 #include "base/location.h" |
8 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
9 #include "google/protobuf/io/coded_stream.h" | 11 #include "google/protobuf/io/coded_stream.h" |
10 #include "google/protobuf/io/zero_copy_stream_impl_lite.h" | 12 #include "google/protobuf/io/zero_copy_stream_impl_lite.h" |
11 #include "google_apis/gcm/base/mcs_util.h" | 13 #include "google_apis/gcm/base/mcs_util.h" |
12 #include "google_apis/gcm/base/socket_stream.h" | 14 #include "google_apis/gcm/base/socket_stream.h" |
13 #include "google_apis/gcm/protocol/mcs.pb.h" | 15 #include "google_apis/gcm/protocol/mcs.pb.h" |
14 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
15 #include "net/socket/stream_socket.h" | 17 #include "net/socket/stream_socket.h" |
16 | 18 |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 read_timeout_timer_.Stop(); | 383 read_timeout_timer_.Stop(); |
382 scoped_ptr<google::protobuf::MessageLite> protobuf( | 384 scoped_ptr<google::protobuf::MessageLite> protobuf( |
383 BuildProtobufFromTag(message_tag_)); | 385 BuildProtobufFromTag(message_tag_)); |
384 // Messages with no content are valid; just use the default protobuf for | 386 // Messages with no content are valid; just use the default protobuf for |
385 // that tag. | 387 // that tag. |
386 if (protobuf.get() && message_size_ == 0) { | 388 if (protobuf.get() && message_size_ == 0) { |
387 base::ThreadTaskRunnerHandle::Get()->PostTask( | 389 base::ThreadTaskRunnerHandle::Get()->PostTask( |
388 FROM_HERE, | 390 FROM_HERE, |
389 base::Bind(&ConnectionHandlerImpl::GetNextMessage, | 391 base::Bind(&ConnectionHandlerImpl::GetNextMessage, |
390 weak_ptr_factory_.GetWeakPtr())); | 392 weak_ptr_factory_.GetWeakPtr())); |
391 read_callback_.Run(protobuf.Pass()); | 393 read_callback_.Run(std::move(protobuf)); |
392 return; | 394 return; |
393 } | 395 } |
394 | 396 |
395 if (input_stream_->GetState() != SocketInputStream::READY) { | 397 if (input_stream_->GetState() != SocketInputStream::READY) { |
396 LOG(ERROR) << "Failed to extract protobuf bytes of type " | 398 LOG(ERROR) << "Failed to extract protobuf bytes of type " |
397 << static_cast<unsigned int>(message_tag_); | 399 << static_cast<unsigned int>(message_tag_); |
398 // Reset the connection. | 400 // Reset the connection. |
399 connection_callback_.Run(net::ERR_FAILED); | 401 connection_callback_.Run(net::ERR_FAILED); |
400 return; | 402 return; |
401 } | 403 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 weak_ptr_factory_.GetWeakPtr())); | 465 weak_ptr_factory_.GetWeakPtr())); |
464 if (message_tag_ == kLoginResponseTag) { | 466 if (message_tag_ == kLoginResponseTag) { |
465 if (handshake_complete_) { | 467 if (handshake_complete_) { |
466 LOG(ERROR) << "Unexpected login response."; | 468 LOG(ERROR) << "Unexpected login response."; |
467 } else { | 469 } else { |
468 handshake_complete_ = true; | 470 handshake_complete_ = true; |
469 DVLOG(1) << "GCM Handshake complete."; | 471 DVLOG(1) << "GCM Handshake complete."; |
470 connection_callback_.Run(net::OK); | 472 connection_callback_.Run(net::OK); |
471 } | 473 } |
472 } | 474 } |
473 read_callback_.Run(protobuf.Pass()); | 475 read_callback_.Run(std::move(protobuf)); |
474 } | 476 } |
475 | 477 |
476 void ConnectionHandlerImpl::OnTimeout() { | 478 void ConnectionHandlerImpl::OnTimeout() { |
477 LOG(ERROR) << "Timed out waiting for GCM Protocol buffer."; | 479 LOG(ERROR) << "Timed out waiting for GCM Protocol buffer."; |
478 CloseConnection(); | 480 CloseConnection(); |
479 connection_callback_.Run(net::ERR_TIMED_OUT); | 481 connection_callback_.Run(net::ERR_TIMED_OUT); |
480 } | 482 } |
481 | 483 |
482 void ConnectionHandlerImpl::CloseConnection() { | 484 void ConnectionHandlerImpl::CloseConnection() { |
483 DVLOG(1) << "Closing connection."; | 485 DVLOG(1) << "Closing connection."; |
484 read_timeout_timer_.Stop(); | 486 read_timeout_timer_.Stop(); |
485 if (socket_) | 487 if (socket_) |
486 socket_->Disconnect(); | 488 socket_->Disconnect(); |
487 socket_ = NULL; | 489 socket_ = NULL; |
488 handshake_complete_ = false; | 490 handshake_complete_ = false; |
489 message_tag_ = 0; | 491 message_tag_ = 0; |
490 message_size_ = 0; | 492 message_size_ = 0; |
491 size_packet_so_far_ = 0; | 493 size_packet_so_far_ = 0; |
492 payload_input_buffer_.clear(); | 494 payload_input_buffer_.clear(); |
493 input_stream_.reset(); | 495 input_stream_.reset(); |
494 output_stream_.reset(); | 496 output_stream_.reset(); |
495 weak_ptr_factory_.InvalidateWeakPtrs(); | 497 weak_ptr_factory_.InvalidateWeakPtrs(); |
496 } | 498 } |
497 | 499 |
498 } // namespace gcm | 500 } // namespace gcm |
OLD | NEW |