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 "google_apis/gcm/engine/mcs_client.h" | 5 #include "google_apis/gcm/engine/mcs_client.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/stringprintf.h" | |
|
jianli
2014/03/26 20:17:18
nit: is this needed?
juyik
2014/03/26 20:57:32
Done. Not anymore.
| |
| 13 #include "base/time/clock.h" | 14 #include "base/time/clock.h" |
| 14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 15 #include "google_apis/gcm/base/mcs_util.h" | 16 #include "google_apis/gcm/base/mcs_util.h" |
| 16 #include "google_apis/gcm/base/socket_stream.h" | 17 #include "google_apis/gcm/base/socket_stream.h" |
| 17 #include "google_apis/gcm/engine/connection_factory.h" | 18 #include "google_apis/gcm/engine/connection_factory.h" |
| 19 #include "google_apis/gcm/gcm_stats_recorder.h" | |
| 18 | 20 |
| 19 using namespace google::protobuf::io; | 21 using namespace google::protobuf::io; |
| 20 | 22 |
| 21 namespace gcm { | 23 namespace gcm { |
| 22 | 24 |
| 23 namespace { | 25 namespace { |
| 24 | 26 |
| 25 typedef scoped_ptr<google::protobuf::MessageLite> MCSProto; | 27 typedef scoped_ptr<google::protobuf::MessageLite> MCSProto; |
| 26 | 28 |
| 27 // The category of messages intended for the GCM client itself from MCS. | 29 // The category of messages intended for the GCM client itself from MCS. |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 | 123 |
| 122 // The protobuf of the message itself. | 124 // The protobuf of the message itself. |
| 123 MCSProto protobuf; | 125 MCSProto protobuf; |
| 124 }; | 126 }; |
| 125 | 127 |
| 126 ReliablePacketInfo::ReliablePacketInfo() | 128 ReliablePacketInfo::ReliablePacketInfo() |
| 127 : stream_id(0), tag(0) { | 129 : stream_id(0), tag(0) { |
| 128 } | 130 } |
| 129 ReliablePacketInfo::~ReliablePacketInfo() {} | 131 ReliablePacketInfo::~ReliablePacketInfo() {} |
| 130 | 132 |
| 133 int MCSClient::GetSendQueueSize() const { | |
| 134 return to_send_.size(); | |
| 135 } | |
| 136 | |
| 137 int MCSClient::GetUnackedQueueSize() const { | |
| 138 return to_resend_.size(); | |
| 139 } | |
| 140 | |
| 131 std::string MCSClient::GetStateString() const { | 141 std::string MCSClient::GetStateString() const { |
| 132 switch(state_) { | 142 switch(state_) { |
| 133 case UNINITIALIZED: | 143 case UNINITIALIZED: |
| 134 return "UNINITIALIZED"; | 144 return "UNINITIALIZED"; |
| 135 case LOADED: | 145 case LOADED: |
| 136 return "LOADED"; | 146 return "LOADED"; |
| 137 case CONNECTING: | 147 case CONNECTING: |
| 138 return "CONNECTING"; | 148 return "CONNECTING"; |
| 139 case CONNECTED: | 149 case CONNECTED: |
| 140 return "CONNECTED"; | 150 return "CONNECTED"; |
| 141 default: | 151 default: |
| 142 NOTREACHED(); | 152 NOTREACHED(); |
| 143 return std::string(); | 153 return std::string(); |
| 144 } | 154 } |
| 145 } | 155 } |
| 146 | 156 |
| 147 MCSClient::MCSClient(const std::string& version_string, | 157 MCSClient::MCSClient(const std::string& version_string, |
| 148 base::Clock* clock, | 158 base::Clock* clock, |
| 149 ConnectionFactory* connection_factory, | 159 ConnectionFactory* connection_factory, |
| 150 GCMStore* gcm_store) | 160 GCMStore* gcm_store, |
| 161 GCMStatsRecorder* recorder) | |
| 151 : version_string_(version_string), | 162 : version_string_(version_string), |
| 152 clock_(clock), | 163 clock_(clock), |
| 153 state_(UNINITIALIZED), | 164 state_(UNINITIALIZED), |
| 154 android_id_(0), | 165 android_id_(0), |
| 155 security_token_(0), | 166 security_token_(0), |
| 156 connection_factory_(connection_factory), | 167 connection_factory_(connection_factory), |
| 157 connection_handler_(NULL), | 168 connection_handler_(NULL), |
| 158 last_device_to_server_stream_id_received_(0), | 169 last_device_to_server_stream_id_received_(0), |
| 159 last_server_to_device_stream_id_received_(0), | 170 last_server_to_device_stream_id_received_(0), |
| 160 stream_id_out_(0), | 171 stream_id_out_(0), |
| 161 stream_id_in_(0), | 172 stream_id_in_(0), |
| 162 gcm_store_(gcm_store), | 173 gcm_store_(gcm_store), |
| 174 recorder_(recorder), | |
| 163 weak_ptr_factory_(this) { | 175 weak_ptr_factory_(this) { |
| 164 } | 176 } |
| 165 | 177 |
| 166 MCSClient::~MCSClient() { | 178 MCSClient::~MCSClient() { |
| 167 } | 179 } |
| 168 | 180 |
| 169 void MCSClient::Initialize( | 181 void MCSClient::Initialize( |
| 170 const ErrorCallback& error_callback, | 182 const ErrorCallback& error_callback, |
| 171 const OnMessageReceivedCallback& message_received_callback, | 183 const OnMessageReceivedCallback& message_received_callback, |
| 172 const OnMessageSentCallback& message_sent_callback, | 184 const OnMessageSentCallback& message_sent_callback, |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 488 if (packet_info->tag == kDataMessageStanzaTag) { | 500 if (packet_info->tag == kDataMessageStanzaTag) { |
| 489 mcs_proto::DataMessageStanza* data_message = | 501 mcs_proto::DataMessageStanza* data_message = |
| 490 reinterpret_cast<mcs_proto::DataMessageStanza*>( | 502 reinterpret_cast<mcs_proto::DataMessageStanza*>( |
| 491 packet_info->protobuf.get()); | 503 packet_info->protobuf.get()); |
| 492 uint64 sent = data_message->sent(); | 504 uint64 sent = data_message->sent(); |
| 493 DCHECK_GT(sent, 0U); | 505 DCHECK_GT(sent, 0U); |
| 494 int queued = (clock_->Now().ToInternalValue() / | 506 int queued = (clock_->Now().ToInternalValue() / |
| 495 base::Time::kMicrosecondsPerSecond) - sent; | 507 base::Time::kMicrosecondsPerSecond) - sent; |
| 496 DVLOG(1) << "Message was queued for " << queued << " seconds."; | 508 DVLOG(1) << "Message was queued for " << queued << " seconds."; |
| 497 data_message->set_queued(queued); | 509 data_message->set_queued(queued); |
| 510 recorder_->RecordSentToWire( | |
| 511 data_message->category(), | |
| 512 data_message->to(), | |
| 513 data_message->id(), | |
| 514 queued); | |
| 498 } | 515 } |
| 499 | 516 |
| 500 // Set the proper last received stream id to acknowledge received server | 517 // Set the proper last received stream id to acknowledge received server |
| 501 // packets. | 518 // packets. |
| 502 DVLOG(1) << "Setting last stream id received to " | 519 DVLOG(1) << "Setting last stream id received to " |
| 503 << stream_id_in_; | 520 << stream_id_in_; |
| 504 SetLastStreamIdReceived(stream_id_in_, | 521 SetLastStreamIdReceived(stream_id_in_, |
| 505 packet_info->protobuf.get()); | 522 packet_info->protobuf.get()); |
| 506 if (stream_id_in_ != last_server_to_device_stream_id_received_) { | 523 if (stream_id_in_ != last_server_to_device_stream_id_received_) { |
| 507 last_server_to_device_stream_id_received_ = stream_id_in_; | 524 last_server_to_device_stream_id_received_ = stream_id_in_; |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 855 } | 872 } |
| 856 | 873 |
| 857 void MCSClient::NotifyMessageSendStatus( | 874 void MCSClient::NotifyMessageSendStatus( |
| 858 const google::protobuf::MessageLite& protobuf, | 875 const google::protobuf::MessageLite& protobuf, |
| 859 MessageSendStatus status) { | 876 MessageSendStatus status) { |
| 860 if (GetMCSProtoTag(protobuf) != kDataMessageStanzaTag) | 877 if (GetMCSProtoTag(protobuf) != kDataMessageStanzaTag) |
| 861 return; | 878 return; |
| 862 | 879 |
| 863 const mcs_proto::DataMessageStanza* data_message_stanza = | 880 const mcs_proto::DataMessageStanza* data_message_stanza = |
| 864 reinterpret_cast<const mcs_proto::DataMessageStanza*>(&protobuf); | 881 reinterpret_cast<const mcs_proto::DataMessageStanza*>(&protobuf); |
| 882 recorder_->RecordNotifySendStatus( | |
| 883 data_message_stanza->category(), | |
| 884 data_message_stanza->to(), | |
| 885 data_message_stanza->id(), | |
| 886 status, | |
| 887 protobuf.ByteSize(), | |
| 888 data_message_stanza->ttl()); | |
| 865 message_sent_callback_.Run( | 889 message_sent_callback_.Run( |
| 866 data_message_stanza->device_user_id(), | 890 data_message_stanza->device_user_id(), |
| 867 data_message_stanza->category(), | 891 data_message_stanza->category(), |
| 868 data_message_stanza->id(), | 892 data_message_stanza->id(), |
| 869 status); | 893 status); |
| 870 } | 894 } |
| 871 | 895 |
| 872 MCSClient::MCSPacketInternal MCSClient::PopMessageForSend() { | 896 MCSClient::MCSPacketInternal MCSClient::PopMessageForSend() { |
| 873 MCSPacketInternal packet = to_send_.front(); | 897 MCSPacketInternal packet = to_send_.front(); |
| 874 to_send_.pop_front(); | 898 to_send_.pop_front(); |
| 875 | 899 |
| 876 if (packet->tag == kDataMessageStanzaTag) { | 900 if (packet->tag == kDataMessageStanzaTag) { |
| 877 mcs_proto::DataMessageStanza* data_message = | 901 mcs_proto::DataMessageStanza* data_message = |
| 878 reinterpret_cast<mcs_proto::DataMessageStanza*>(packet->protobuf.get()); | 902 reinterpret_cast<mcs_proto::DataMessageStanza*>(packet->protobuf.get()); |
| 879 CollapseKey collapse_key(*data_message); | 903 CollapseKey collapse_key(*data_message); |
| 880 if (collapse_key.IsValid()) | 904 if (collapse_key.IsValid()) |
| 881 collapse_key_map_.erase(collapse_key); | 905 collapse_key_map_.erase(collapse_key); |
| 882 } | 906 } |
| 883 | 907 |
| 884 return packet; | 908 return packet; |
| 885 } | 909 } |
| 886 | 910 |
| 887 } // namespace gcm | 911 } // namespace gcm |
| OLD | NEW |