| 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/gcm_client_impl.h" | 5 #include "google_apis/gcm/gcm_client_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
| 14 #include "base/strings/stringprintf.h" |
| 14 #include "base/time/default_clock.h" | 15 #include "base/time/default_clock.h" |
| 15 #include "google_apis/gcm/base/mcs_message.h" | 16 #include "google_apis/gcm/base/mcs_message.h" |
| 16 #include "google_apis/gcm/base/mcs_util.h" | 17 #include "google_apis/gcm/base/mcs_util.h" |
| 17 #include "google_apis/gcm/engine/checkin_request.h" | 18 #include "google_apis/gcm/engine/checkin_request.h" |
| 18 #include "google_apis/gcm/engine/connection_factory_impl.h" | 19 #include "google_apis/gcm/engine/connection_factory_impl.h" |
| 19 #include "google_apis/gcm/engine/gcm_store_impl.h" | 20 #include "google_apis/gcm/engine/gcm_store_impl.h" |
| 20 #include "google_apis/gcm/engine/mcs_client.h" | 21 #include "google_apis/gcm/engine/mcs_client.h" |
| 22 #include "google_apis/gcm/gcm_stats_recorder.h" |
| 21 #include "google_apis/gcm/protocol/mcs.pb.h" | 23 #include "google_apis/gcm/protocol/mcs.pb.h" |
| 22 #include "net/http/http_network_session.h" | 24 #include "net/http/http_network_session.h" |
| 23 #include "net/url_request/url_request_context.h" | 25 #include "net/url_request/url_request_context.h" |
| 24 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 25 | 27 |
| 26 namespace gcm { | 28 namespace gcm { |
| 27 | 29 |
| 28 namespace { | 30 namespace { |
| 29 | 31 |
| 30 // Backoff policy. Shared across reconnection logic and checkin/(un)registration | 32 // Backoff policy. Shared across reconnection logic and checkin/(un)registration |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 GCMInternalsBuilder::~GCMInternalsBuilder() {} | 123 GCMInternalsBuilder::~GCMInternalsBuilder() {} |
| 122 | 124 |
| 123 scoped_ptr<base::Clock> GCMInternalsBuilder::BuildClock() { | 125 scoped_ptr<base::Clock> GCMInternalsBuilder::BuildClock() { |
| 124 return make_scoped_ptr<base::Clock>(new base::DefaultClock()); | 126 return make_scoped_ptr<base::Clock>(new base::DefaultClock()); |
| 125 } | 127 } |
| 126 | 128 |
| 127 scoped_ptr<MCSClient> GCMInternalsBuilder::BuildMCSClient( | 129 scoped_ptr<MCSClient> GCMInternalsBuilder::BuildMCSClient( |
| 128 const std::string& version, | 130 const std::string& version, |
| 129 base::Clock* clock, | 131 base::Clock* clock, |
| 130 ConnectionFactory* connection_factory, | 132 ConnectionFactory* connection_factory, |
| 131 GCMStore* gcm_store) { | 133 GCMStore* gcm_store, |
| 134 GCMStatsRecorder* recorder) { |
| 132 return make_scoped_ptr<MCSClient>( | 135 return make_scoped_ptr<MCSClient>( |
| 133 new MCSClient(version, | 136 new MCSClient(version, |
| 134 clock, | 137 clock, |
| 135 connection_factory, | 138 connection_factory, |
| 136 gcm_store)); | 139 gcm_store, |
| 140 recorder)); |
| 137 } | 141 } |
| 138 | 142 |
| 139 scoped_ptr<ConnectionFactory> GCMInternalsBuilder::BuildConnectionFactory( | 143 scoped_ptr<ConnectionFactory> GCMInternalsBuilder::BuildConnectionFactory( |
| 140 const std::vector<GURL>& endpoints, | 144 const std::vector<GURL>& endpoints, |
| 141 const net::BackoffEntry::Policy& backoff_policy, | 145 const net::BackoffEntry::Policy& backoff_policy, |
| 142 scoped_refptr<net::HttpNetworkSession> network_session, | 146 scoped_refptr<net::HttpNetworkSession> network_session, |
| 143 net::NetLog* net_log) { | 147 net::NetLog* net_log) { |
| 144 return make_scoped_ptr<ConnectionFactory>( | 148 return make_scoped_ptr<ConnectionFactory>( |
| 145 new ConnectionFactoryImpl(endpoints, | 149 new ConnectionFactoryImpl(endpoints, |
| 146 backoff_policy, | 150 backoff_policy, |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 endpoints.push_back(GURL(kMCSEndpointFallback)); | 234 endpoints.push_back(GURL(kMCSEndpointFallback)); |
| 231 connection_factory_ = internals_builder_->BuildConnectionFactory( | 235 connection_factory_ = internals_builder_->BuildConnectionFactory( |
| 232 endpoints, | 236 endpoints, |
| 233 kDefaultBackoffPolicy, | 237 kDefaultBackoffPolicy, |
| 234 network_session_, | 238 network_session_, |
| 235 net_log_.net_log()); | 239 net_log_.net_log()); |
| 236 mcs_client_ = internals_builder_->BuildMCSClient( | 240 mcs_client_ = internals_builder_->BuildMCSClient( |
| 237 chrome_build_proto_.chrome_version(), | 241 chrome_build_proto_.chrome_version(), |
| 238 clock_.get(), | 242 clock_.get(), |
| 239 connection_factory_.get(), | 243 connection_factory_.get(), |
| 240 gcm_store_.get()).Pass(); | 244 gcm_store_.get(), |
| 245 &recorder_).Pass(); |
| 241 | 246 |
| 242 mcs_client_->Initialize( | 247 mcs_client_->Initialize( |
| 243 base::Bind(&GCMClientImpl::OnMCSError, weak_ptr_factory_.GetWeakPtr()), | 248 base::Bind(&GCMClientImpl::OnMCSError, weak_ptr_factory_.GetWeakPtr()), |
| 244 base::Bind(&GCMClientImpl::OnMessageReceivedFromMCS, | 249 base::Bind(&GCMClientImpl::OnMessageReceivedFromMCS, |
| 245 weak_ptr_factory_.GetWeakPtr()), | 250 weak_ptr_factory_.GetWeakPtr()), |
| 246 base::Bind(&GCMClientImpl::OnMessageSentToMCS, | 251 base::Bind(&GCMClientImpl::OnMessageSentToMCS, |
| 247 weak_ptr_factory_.GetWeakPtr()), | 252 weak_ptr_factory_.GetWeakPtr()), |
| 248 result.Pass()); | 253 result.Pass()); |
| 249 } | 254 } |
| 250 | 255 |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 case GCMClientImpl::INITIAL_DEVICE_CHECKIN: | 520 case GCMClientImpl::INITIAL_DEVICE_CHECKIN: |
| 516 return "INITIAL_DEVICE_CHECKIN"; | 521 return "INITIAL_DEVICE_CHECKIN"; |
| 517 case GCMClientImpl::READY: | 522 case GCMClientImpl::READY: |
| 518 return "READY"; | 523 return "READY"; |
| 519 default: | 524 default: |
| 520 NOTREACHED(); | 525 NOTREACHED(); |
| 521 return std::string(); | 526 return std::string(); |
| 522 } | 527 } |
| 523 } | 528 } |
| 524 | 529 |
| 530 void GCMClientImpl::SetRecording(bool recording) { |
| 531 recorder_.SetRecording(recording); |
| 532 } |
| 533 |
| 534 void GCMClientImpl::ClearActivityLogs() { |
| 535 recorder_.Clear(); |
| 536 } |
| 537 |
| 525 GCMClient::GCMStatistics GCMClientImpl::GetStatistics() const { | 538 GCMClient::GCMStatistics GCMClientImpl::GetStatistics() const { |
| 526 GCMClient::GCMStatistics stats; | 539 GCMClient::GCMStatistics stats; |
| 527 stats.gcm_client_state = GCMClientImpl::GetStateString(); | 540 stats.is_recording = recorder_.is_recording(); |
| 541 stats.gcm_client_state = GetStateString(); |
| 528 stats.connection_client_created = mcs_client_.get() != NULL; | 542 stats.connection_client_created = mcs_client_.get() != NULL; |
| 529 if (mcs_client_.get()) { | 543 if (mcs_client_.get()) { |
| 530 stats.connection_state = mcs_client_->GetStateString(); | 544 stats.connection_state = mcs_client_->GetStateString(); |
| 531 // TODO(juyik): add more statistics such as message metadata list, etc. | 545 stats.send_queue_size = mcs_client_->GetSendQueueSize(); |
| 546 stats.unacked_queue_size = mcs_client_->GetUnackedQueueSize(); |
| 532 } | 547 } |
| 533 if (device_checkin_info_.android_id > 0) | 548 if (device_checkin_info_.android_id > 0) |
| 534 stats.android_id = device_checkin_info_.android_id; | 549 stats.android_id = device_checkin_info_.android_id; |
| 550 |
| 551 for (std::deque<GCMStatsRecorder::SendingActivity>::const_iterator it = |
| 552 recorder_.sending_activities().begin(); |
| 553 it != recorder_.sending_activities().end(); |
| 554 ++it) { |
| 555 stats.sending.push_back(*it); |
| 556 } |
| 557 for (RegistrationInfoMap::const_iterator it = registrations_.begin(); |
| 558 it != registrations_.end(); ++it) { |
| 559 stats.registered_app_ids.push_back(it->first); |
| 560 } |
| 535 return stats; | 561 return stats; |
| 536 } | 562 } |
| 537 | 563 |
| 538 void GCMClientImpl::OnMessageReceivedFromMCS(const gcm::MCSMessage& message) { | 564 void GCMClientImpl::OnMessageReceivedFromMCS(const gcm::MCSMessage& message) { |
| 539 switch (message.tag()) { | 565 switch (message.tag()) { |
| 540 case kLoginResponseTag: | 566 case kLoginResponseTag: |
| 541 DVLOG(1) << "Login response received by GCM Client. Ignoring."; | 567 DVLOG(1) << "Login response received by GCM Client. Ignoring."; |
| 542 return; | 568 return; |
| 543 case kDataMessageStanzaTag: | 569 case kDataMessageStanzaTag: |
| 544 DVLOG(1) << "A downstream message received. Processing..."; | 570 DVLOG(1) << "A downstream message received. Processing..."; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 653 send_error_details.additional_data = message_data; | 679 send_error_details.additional_data = message_data; |
| 654 send_error_details.result = SERVER_ERROR; | 680 send_error_details.result = SERVER_ERROR; |
| 655 | 681 |
| 656 MessageData::iterator iter = | 682 MessageData::iterator iter = |
| 657 send_error_details.additional_data.find(kSendErrorMessageIdKey); | 683 send_error_details.additional_data.find(kSendErrorMessageIdKey); |
| 658 if (iter != send_error_details.additional_data.end()) { | 684 if (iter != send_error_details.additional_data.end()) { |
| 659 send_error_details.message_id = iter->second; | 685 send_error_details.message_id = iter->second; |
| 660 send_error_details.additional_data.erase(iter); | 686 send_error_details.additional_data.erase(iter); |
| 661 } | 687 } |
| 662 | 688 |
| 689 recorder_.RecordIncomingSendError( |
| 690 data_message_stanza.category(), |
| 691 data_message_stanza.to(), |
| 692 data_message_stanza.id(), |
| 693 data_message_stanza.ByteSize(), |
| 694 data_message_stanza.ttl()); |
| 663 delegate_->OnMessageSendError(data_message_stanza.category(), | 695 delegate_->OnMessageSendError(data_message_stanza.category(), |
| 664 send_error_details); | 696 send_error_details); |
| 665 } | 697 } |
| 666 | 698 |
| 667 } // namespace gcm | 699 } // namespace gcm |
| OLD | NEW |