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/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" |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 clock, | 139 clock, |
| 140 connection_factory, | 140 connection_factory, |
| 141 gcm_store, | 141 gcm_store, |
| 142 recorder)); | 142 recorder)); |
| 143 } | 143 } |
| 144 | 144 |
| 145 scoped_ptr<ConnectionFactory> GCMInternalsBuilder::BuildConnectionFactory( | 145 scoped_ptr<ConnectionFactory> GCMInternalsBuilder::BuildConnectionFactory( |
| 146 const std::vector<GURL>& endpoints, | 146 const std::vector<GURL>& endpoints, |
| 147 const net::BackoffEntry::Policy& backoff_policy, | 147 const net::BackoffEntry::Policy& backoff_policy, |
| 148 scoped_refptr<net::HttpNetworkSession> network_session, | 148 scoped_refptr<net::HttpNetworkSession> network_session, |
| 149 net::NetLog* net_log) { | 149 net::NetLog* net_log, |
| 150 GCMStatsRecorder* recorder) { | |
| 150 return make_scoped_ptr<ConnectionFactory>( | 151 return make_scoped_ptr<ConnectionFactory>( |
| 151 new ConnectionFactoryImpl(endpoints, | 152 new ConnectionFactoryImpl(endpoints, |
| 152 backoff_policy, | 153 backoff_policy, |
| 153 network_session, | 154 network_session, |
| 154 net_log)); | 155 net_log, |
| 156 recorder)); | |
| 155 } | 157 } |
| 156 | 158 |
| 157 GCMClientImpl::GCMClientImpl(scoped_ptr<GCMInternalsBuilder> internals_builder) | 159 GCMClientImpl::GCMClientImpl(scoped_ptr<GCMInternalsBuilder> internals_builder) |
| 158 : internals_builder_(internals_builder.Pass()), | 160 : internals_builder_(internals_builder.Pass()), |
| 159 state_(UNINITIALIZED), | 161 state_(UNINITIALIZED), |
| 160 clock_(internals_builder_->BuildClock()), | 162 clock_(internals_builder_->BuildClock()), |
| 161 url_request_context_getter_(NULL), | 163 url_request_context_getter_(NULL), |
| 162 pending_registration_requests_deleter_(&pending_registration_requests_), | 164 pending_registration_requests_deleter_(&pending_registration_requests_), |
| 163 pending_unregistration_requests_deleter_( | 165 pending_unregistration_requests_deleter_( |
| 164 &pending_unregistration_requests_), | 166 &pending_unregistration_requests_), |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 233 | 235 |
| 234 void GCMClientImpl::InitializeMCSClient( | 236 void GCMClientImpl::InitializeMCSClient( |
| 235 scoped_ptr<GCMStore::LoadResult> result) { | 237 scoped_ptr<GCMStore::LoadResult> result) { |
| 236 std::vector<GURL> endpoints; | 238 std::vector<GURL> endpoints; |
| 237 endpoints.push_back(GURL(kMCSEndpointMain)); | 239 endpoints.push_back(GURL(kMCSEndpointMain)); |
| 238 endpoints.push_back(GURL(kMCSEndpointFallback)); | 240 endpoints.push_back(GURL(kMCSEndpointFallback)); |
| 239 connection_factory_ = internals_builder_->BuildConnectionFactory( | 241 connection_factory_ = internals_builder_->BuildConnectionFactory( |
| 240 endpoints, | 242 endpoints, |
| 241 kDefaultBackoffPolicy, | 243 kDefaultBackoffPolicy, |
| 242 network_session_, | 244 network_session_, |
| 243 net_log_.net_log()); | 245 net_log_.net_log(), |
| 246 &recorder_); | |
| 244 mcs_client_ = internals_builder_->BuildMCSClient( | 247 mcs_client_ = internals_builder_->BuildMCSClient( |
| 245 chrome_build_proto_.chrome_version(), | 248 chrome_build_proto_.chrome_version(), |
| 246 clock_.get(), | 249 clock_.get(), |
| 247 connection_factory_.get(), | 250 connection_factory_.get(), |
| 248 gcm_store_.get(), | 251 gcm_store_.get(), |
| 249 &recorder_).Pass(); | 252 &recorder_).Pass(); |
| 250 | 253 |
| 251 mcs_client_->Initialize( | 254 mcs_client_->Initialize( |
| 252 base::Bind(&GCMClientImpl::OnMCSError, weak_ptr_factory_.GetWeakPtr()), | 255 base::Bind(&GCMClientImpl::OnMCSError, weak_ptr_factory_.GetWeakPtr()), |
| 253 base::Bind(&GCMClientImpl::OnMessageReceivedFromMCS, | 256 base::Bind(&GCMClientImpl::OnMessageReceivedFromMCS, |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 410 DCHECK_EQ(0u, pending_registration_requests_.count(app_id)); | 413 DCHECK_EQ(0u, pending_registration_requests_.count(app_id)); |
| 411 | 414 |
| 412 RegistrationRequest* registration_request = | 415 RegistrationRequest* registration_request = |
| 413 new RegistrationRequest(request_info, | 416 new RegistrationRequest(request_info, |
| 414 kDefaultBackoffPolicy, | 417 kDefaultBackoffPolicy, |
| 415 base::Bind(&GCMClientImpl::OnRegisterCompleted, | 418 base::Bind(&GCMClientImpl::OnRegisterCompleted, |
| 416 weak_ptr_factory_.GetWeakPtr(), | 419 weak_ptr_factory_.GetWeakPtr(), |
| 417 app_id, | 420 app_id, |
| 418 sender_ids), | 421 sender_ids), |
| 419 kMaxRegistrationRetries, | 422 kMaxRegistrationRetries, |
| 420 url_request_context_getter_); | 423 url_request_context_getter_, |
| 424 &recorder_); | |
| 421 pending_registration_requests_[app_id] = registration_request; | 425 pending_registration_requests_[app_id] = registration_request; |
| 422 registration_request->Start(); | 426 registration_request->Start(); |
| 423 } | 427 } |
| 424 | 428 |
| 425 void GCMClientImpl::OnRegisterCompleted( | 429 void GCMClientImpl::OnRegisterCompleted( |
| 426 const std::string& app_id, | 430 const std::string& app_id, |
| 427 const std::vector<std::string>& sender_ids, | 431 const std::vector<std::string>& sender_ids, |
| 428 RegistrationRequest::Status status, | 432 RegistrationRequest::Status status, |
| 429 const std::string& registration_id) { | 433 const std::string& registration_id) { |
| 430 DCHECK(delegate_); | 434 DCHECK(delegate_); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 482 device_checkin_info_.secret, | 486 device_checkin_info_.secret, |
| 483 app_id); | 487 app_id); |
| 484 | 488 |
| 485 UnregistrationRequest* unregistration_request = | 489 UnregistrationRequest* unregistration_request = |
| 486 new UnregistrationRequest( | 490 new UnregistrationRequest( |
| 487 request_info, | 491 request_info, |
| 488 kDefaultBackoffPolicy, | 492 kDefaultBackoffPolicy, |
| 489 base::Bind(&GCMClientImpl::OnUnregisterCompleted, | 493 base::Bind(&GCMClientImpl::OnUnregisterCompleted, |
| 490 weak_ptr_factory_.GetWeakPtr(), | 494 weak_ptr_factory_.GetWeakPtr(), |
| 491 app_id), | 495 app_id), |
| 492 url_request_context_getter_); | 496 url_request_context_getter_, |
| 497 &recorder_); | |
| 493 pending_unregistration_requests_[app_id] = unregistration_request; | 498 pending_unregistration_requests_[app_id] = unregistration_request; |
| 494 unregistration_request->Start(); | 499 unregistration_request->Start(); |
| 495 } | 500 } |
| 496 | 501 |
| 497 void GCMClientImpl::OnUnregisterCompleted( | 502 void GCMClientImpl::OnUnregisterCompleted( |
| 498 const std::string& app_id, | 503 const std::string& app_id, |
| 499 UnregistrationRequest::Status status) { | 504 UnregistrationRequest::Status status) { |
| 500 DVLOG(1) << "Unregister completed for app: " << app_id | 505 DVLOG(1) << "Unregister completed for app: " << app_id |
| 501 << " with " << (status ? "success." : "failure."); | 506 << " with " << (status ? "success." : "failure."); |
| 502 delegate_->OnUnregisterFinished( | 507 delegate_->OnUnregisterFinished( |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 576 stats.is_recording = recorder_.is_recording(); | 581 stats.is_recording = recorder_.is_recording(); |
| 577 stats.gcm_client_state = GetStateString(); | 582 stats.gcm_client_state = GetStateString(); |
| 578 stats.connection_client_created = mcs_client_.get() != NULL; | 583 stats.connection_client_created = mcs_client_.get() != NULL; |
| 579 if (mcs_client_.get()) { | 584 if (mcs_client_.get()) { |
| 580 stats.connection_state = mcs_client_->GetStateString(); | 585 stats.connection_state = mcs_client_->GetStateString(); |
| 581 stats.send_queue_size = mcs_client_->GetSendQueueSize(); | 586 stats.send_queue_size = mcs_client_->GetSendQueueSize(); |
| 582 stats.resend_queue_size = mcs_client_->GetResendQueueSize(); | 587 stats.resend_queue_size = mcs_client_->GetResendQueueSize(); |
| 583 } | 588 } |
| 584 if (device_checkin_info_.android_id > 0) | 589 if (device_checkin_info_.android_id > 0) |
| 585 stats.android_id = device_checkin_info_.android_id; | 590 stats.android_id = device_checkin_info_.android_id; |
| 586 recorder_.CollectSendingActivities(&stats.sending_activities); | 591 recorder_.CollectActivities(&stats.connection_activities, |
| 592 &stats.registration_activities, | |
| 593 &stats.receiving_activities, | |
| 594 &stats.sending_activities); | |
| 587 | 595 |
| 588 for (RegistrationInfoMap::const_iterator it = registrations_.begin(); | 596 for (RegistrationInfoMap::const_iterator it = registrations_.begin(); |
| 589 it != registrations_.end(); ++it) { | 597 it != registrations_.end(); ++it) { |
| 590 stats.registered_app_ids.push_back(it->first); | 598 stats.registered_app_ids.push_back(it->first); |
| 591 } | 599 } |
| 592 return stats; | 600 return stats; |
| 593 } | 601 } |
| 594 | 602 |
| 595 void GCMClientImpl::OnMessageReceivedFromMCS(const gcm::MCSMessage& message) { | 603 void GCMClientImpl::OnMessageReceivedFromMCS(const gcm::MCSMessage& message) { |
| 596 switch (message.tag()) { | 604 switch (message.tag()) { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 660 if (iter != message_data.end()) { | 668 if (iter != message_data.end()) { |
| 661 message_type = DecodeMessageType(iter->second); | 669 message_type = DecodeMessageType(iter->second); |
| 662 message_data.erase(iter); | 670 message_data.erase(iter); |
| 663 } | 671 } |
| 664 | 672 |
| 665 switch (message_type) { | 673 switch (message_type) { |
| 666 case DATA_MESSAGE: | 674 case DATA_MESSAGE: |
| 667 HandleIncomingDataMessage(data_message_stanza, message_data); | 675 HandleIncomingDataMessage(data_message_stanza, message_data); |
| 668 break; | 676 break; |
| 669 case DELETED_MESSAGES: | 677 case DELETED_MESSAGES: |
| 678 recorder_.RecordDataMessageRecieved(data_message_stanza.category(), | |
| 679 data_message_stanza.from(), | |
| 680 data_message_stanza.ByteSize(), | |
| 681 true, | |
| 682 true); | |
| 670 delegate_->OnMessagesDeleted(data_message_stanza.category()); | 683 delegate_->OnMessagesDeleted(data_message_stanza.category()); |
| 671 break; | 684 break; |
| 672 case SEND_ERROR: | 685 case SEND_ERROR: |
| 673 HandleIncomingSendError(data_message_stanza, message_data); | 686 HandleIncomingSendError(data_message_stanza, message_data); |
| 674 break; | 687 break; |
| 675 case UNKNOWN: | 688 case UNKNOWN: |
| 676 default: // Treat default the same as UNKNOWN. | 689 default: // Treat default the same as UNKNOWN. |
| 677 DVLOG(1) << "Unknown message_type received. Message ignored. " | 690 DVLOG(1) << "Unknown message_type received. Message ignored. " |
| 678 << "App ID: " << data_message_stanza.category() << "."; | 691 << "App ID: " << data_message_stanza.category() << "."; |
| 679 break; | 692 break; |
| 680 } | 693 } |
| 681 } | 694 } |
| 682 | 695 |
| 683 void GCMClientImpl::HandleIncomingDataMessage( | 696 void GCMClientImpl::HandleIncomingDataMessage( |
| 684 const mcs_proto::DataMessageStanza& data_message_stanza, | 697 const mcs_proto::DataMessageStanza& data_message_stanza, |
| 685 MessageData& message_data) { | 698 MessageData& message_data) { |
| 686 std::string app_id = data_message_stanza.category(); | 699 std::string app_id = data_message_stanza.category(); |
| 687 | 700 |
| 688 // Drop the message when the app is not registered for the sender of the | 701 // Drop the message when the app is not registered for the sender of the |
| 689 // message. | 702 // message. |
| 690 RegistrationInfoMap::iterator iter = registrations_.find(app_id); | 703 RegistrationInfoMap::iterator iter = registrations_.find(app_id); |
| 691 if (iter == registrations_.end() || | 704 if (iter == registrations_.end() || |
| 692 std::find(iter->second->sender_ids.begin(), | 705 std::find(iter->second->sender_ids.begin(), |
| 693 iter->second->sender_ids.end(), | 706 iter->second->sender_ids.end(), |
| 694 data_message_stanza.from()) == iter->second->sender_ids.end()) { | 707 data_message_stanza.from()) == iter->second->sender_ids.end()) { |
| 708 recorder_.RecordDataMessageRecieved(app_id, data_message_stanza.from(), | |
|
jianli
2014/04/23 23:16:39
You can combine this recording with the following
juyik
2014/04/24 00:53:45
Done.
| |
| 709 data_message_stanza.ByteSize(), false, false); | |
| 695 return; | 710 return; |
| 696 } | 711 } |
| 697 | 712 |
| 698 IncomingMessage incoming_message; | 713 IncomingMessage incoming_message; |
| 699 incoming_message.sender_id = data_message_stanza.from(); | 714 incoming_message.sender_id = data_message_stanza.from(); |
| 700 if (data_message_stanza.has_token()) | 715 if (data_message_stanza.has_token()) |
| 701 incoming_message.collapse_key = data_message_stanza.token(); | 716 incoming_message.collapse_key = data_message_stanza.token(); |
| 702 incoming_message.data = message_data; | 717 incoming_message.data = message_data; |
| 718 recorder_.RecordDataMessageRecieved(app_id, data_message_stanza.from(), | |
| 719 data_message_stanza.ByteSize(), true, false); | |
| 703 delegate_->OnMessageReceived(app_id, incoming_message); | 720 delegate_->OnMessageReceived(app_id, incoming_message); |
| 704 } | 721 } |
| 705 | 722 |
| 706 void GCMClientImpl::HandleIncomingSendError( | 723 void GCMClientImpl::HandleIncomingSendError( |
| 707 const mcs_proto::DataMessageStanza& data_message_stanza, | 724 const mcs_proto::DataMessageStanza& data_message_stanza, |
| 708 MessageData& message_data) { | 725 MessageData& message_data) { |
| 709 SendErrorDetails send_error_details; | 726 SendErrorDetails send_error_details; |
| 710 send_error_details.additional_data = message_data; | 727 send_error_details.additional_data = message_data; |
| 711 send_error_details.result = SERVER_ERROR; | 728 send_error_details.result = SERVER_ERROR; |
| 712 | 729 |
| 713 MessageData::iterator iter = | 730 MessageData::iterator iter = |
| 714 send_error_details.additional_data.find(kSendErrorMessageIdKey); | 731 send_error_details.additional_data.find(kSendErrorMessageIdKey); |
| 715 if (iter != send_error_details.additional_data.end()) { | 732 if (iter != send_error_details.additional_data.end()) { |
| 716 send_error_details.message_id = iter->second; | 733 send_error_details.message_id = iter->second; |
| 717 send_error_details.additional_data.erase(iter); | 734 send_error_details.additional_data.erase(iter); |
| 718 } | 735 } |
| 719 | 736 |
| 720 recorder_.RecordIncomingSendError( | 737 recorder_.RecordIncomingSendError( |
| 721 data_message_stanza.category(), | 738 data_message_stanza.category(), |
| 722 data_message_stanza.to(), | 739 data_message_stanza.to(), |
| 723 data_message_stanza.id()); | 740 data_message_stanza.id()); |
| 724 delegate_->OnMessageSendError(data_message_stanza.category(), | 741 delegate_->OnMessageSendError(data_message_stanza.category(), |
| 725 send_error_details); | 742 send_error_details); |
| 726 } | 743 } |
| 727 | 744 |
| 728 } // namespace gcm | 745 } // namespace gcm |
| OLD | NEW |