Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/gcm_driver/gcm_client_impl.h" | 5 #include "components/gcm_driver/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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 | 60 |
| 61 enum ResetStoreError { | 61 enum ResetStoreError { |
| 62 DESTROYING_STORE_FAILED, | 62 DESTROYING_STORE_FAILED, |
| 63 INFINITE_STORE_RESET, | 63 INFINITE_STORE_RESET, |
| 64 // NOTE: always keep this entry at the end. Add new value only immediately | 64 // NOTE: always keep this entry at the end. Add new value only immediately |
| 65 // above this line. Make sure to update the corresponding histogram enum | 65 // above this line. Make sure to update the corresponding histogram enum |
| 66 // accordingly. | 66 // accordingly. |
| 67 RESET_STORE_ERROR_COUNT | 67 RESET_STORE_ERROR_COUNT |
| 68 }; | 68 }; |
| 69 | 69 |
| 70 const char kGCMScope[] = "GCM"; | |
| 70 const int kMaxRegistrationRetries = 5; | 71 const int kMaxRegistrationRetries = 5; |
| 71 const char kMessageTypeDataMessage[] = "gcm"; | 72 const char kMessageTypeDataMessage[] = "gcm"; |
| 72 const char kMessageTypeDeletedMessagesKey[] = "deleted_messages"; | 73 const char kMessageTypeDeletedMessagesKey[] = "deleted_messages"; |
| 73 const char kMessageTypeKey[] = "message_type"; | 74 const char kMessageTypeKey[] = "message_type"; |
| 74 const char kMessageTypeSendErrorKey[] = "send_error"; | 75 const char kMessageTypeSendErrorKey[] = "send_error"; |
| 75 const char kSendErrorMessageIdKey[] = "google.message_id"; | 76 const char kSendErrorMessageIdKey[] = "google.message_id"; |
| 76 const char kSendMessageFromValue[] = "gcm@chrome.com"; | 77 const char kSendMessageFromValue[] = "gcm@chrome.com"; |
| 77 const int64 kDefaultUserSerialNumber = 0LL; | 78 const int64 kDefaultUserSerialNumber = 0LL; |
| 78 | 79 |
| 79 GCMClient::Result ToGCMClientResult(MCSClient::MessageSendStatus status) { | 80 GCMClient::Result ToGCMClientResult(MCSClient::MessageSendStatus status) { |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 328 | 329 |
| 329 void GCMClientImpl::OnLoadCompleted(scoped_ptr<GCMStore::LoadResult> result) { | 330 void GCMClientImpl::OnLoadCompleted(scoped_ptr<GCMStore::LoadResult> result) { |
| 330 DCHECK_EQ(LOADING, state_); | 331 DCHECK_EQ(LOADING, state_); |
| 331 | 332 |
| 332 if (!result->success) { | 333 if (!result->success) { |
| 333 ResetStore(); | 334 ResetStore(); |
| 334 return; | 335 return; |
| 335 } | 336 } |
| 336 gcm_store_reset_ = false; | 337 gcm_store_reset_ = false; |
| 337 | 338 |
| 338 registrations_ = result->registrations; | |
| 339 device_checkin_info_.android_id = result->device_android_id; | 339 device_checkin_info_.android_id = result->device_android_id; |
| 340 device_checkin_info_.secret = result->device_security_token; | 340 device_checkin_info_.secret = result->device_security_token; |
| 341 device_checkin_info_.last_checkin_accounts = result->last_checkin_accounts; | 341 device_checkin_info_.last_checkin_accounts = result->last_checkin_accounts; |
| 342 // A case where there were previously no accounts reported with checkin is | 342 // A case where there were previously no accounts reported with checkin is |
| 343 // considered to be the same as when the list of accounts is empty. It enables | 343 // considered to be the same as when the list of accounts is empty. It enables |
| 344 // scheduling a periodic checkin for devices with no signed in users | 344 // scheduling a periodic checkin for devices with no signed in users |
| 345 // immediately after restart, while keeping |accounts_set == false| delays the | 345 // immediately after restart, while keeping |accounts_set == false| delays the |
| 346 // checkin until the list of accounts is set explicitly. | 346 // checkin until the list of accounts is set explicitly. |
| 347 if (result->last_checkin_accounts.size() == 0) | 347 if (result->last_checkin_accounts.size() == 0) |
| 348 device_checkin_info_.accounts_set = true; | 348 device_checkin_info_.accounts_set = true; |
| 349 last_checkin_time_ = result->last_checkin_time; | 349 last_checkin_time_ = result->last_checkin_time; |
| 350 gservices_settings_.UpdateFromLoadResult(*result); | 350 gservices_settings_.UpdateFromLoadResult(*result); |
| 351 | |
| 352 for (auto iter = result->registrations.begin(); | |
| 353 iter != result->registrations.end(); | |
| 354 ++iter) { | |
| 355 std::string registration_id; | |
| 356 scoped_ptr<RegistrationInfo> registration = | |
| 357 RegistrationInfo::BuildFromString( | |
| 358 iter->first, iter->second, ®istration_id); | |
| 359 if (registration.get()) | |
|
fgorski
2015/05/13 18:32:38
else?
jianli
2015/05/13 22:42:56
If we can get registration, it means that either t
| |
| 360 registrations_[make_linked_ptr(registration.release())] = registration_id; | |
| 361 } | |
| 362 | |
| 351 instance_id_data_ = result->instance_id_data; | 363 instance_id_data_ = result->instance_id_data; |
| 364 | |
| 352 load_result_ = result.Pass(); | 365 load_result_ = result.Pass(); |
| 353 state_ = LOADED; | 366 state_ = LOADED; |
| 354 | 367 |
| 355 // Don't initiate the GCM connection when GCM is in delayed start mode and | 368 // Don't initiate the GCM connection when GCM is in delayed start mode and |
| 356 // not any standalone app has registered GCM yet. | 369 // not any standalone app has registered GCM yet. |
| 357 if (start_mode_ == DELAYED_START && !HasStandaloneRegisteredApp()) | 370 if (start_mode_ == DELAYED_START && !HasStandaloneRegisteredApp()) |
| 358 return; | 371 return; |
| 359 | 372 |
| 360 StartGCM(); | 373 StartGCM(); |
| 361 } | 374 } |
| (...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 703 delegate_->OnDisconnected(); | 716 delegate_->OnDisconnected(); |
| 704 mcs_client_.reset(); | 717 mcs_client_.reset(); |
| 705 checkin_request_.reset(); | 718 checkin_request_.reset(); |
| 706 // Delete all of the pending registration and unregistration requests. | 719 // Delete all of the pending registration and unregistration requests. |
| 707 STLDeleteValues(&pending_registration_requests_); | 720 STLDeleteValues(&pending_registration_requests_); |
| 708 STLDeleteValues(&pending_unregistration_requests_); | 721 STLDeleteValues(&pending_unregistration_requests_); |
| 709 state_ = INITIALIZED; | 722 state_ = INITIALIZED; |
| 710 gcm_store_->Close(); | 723 gcm_store_->Close(); |
| 711 } | 724 } |
| 712 | 725 |
| 713 void GCMClientImpl::Register(const std::string& app_id, | 726 void GCMClientImpl::Register( |
| 714 const std::vector<std::string>& sender_ids) { | 727 const linked_ptr<RegistrationInfo>& registration_info) { |
| 715 DCHECK_EQ(state_, READY); | 728 DCHECK_EQ(state_, READY); |
| 716 | 729 |
| 717 // If the same sender ids is provided, return the cached registration ID | 730 // Find and use the cached registration ID. |
| 718 // directly. | |
| 719 RegistrationInfoMap::const_iterator registrations_iter = | 731 RegistrationInfoMap::const_iterator registrations_iter = |
| 720 registrations_.find(app_id); | 732 registrations_.find(registration_info); |
| 721 if (registrations_iter != registrations_.end() && | 733 if (registrations_iter != registrations_.end()) { |
| 722 registrations_iter->second->sender_ids == sender_ids) { | 734 bool matched = true; |
| 723 delegate_->OnRegisterFinished( | 735 |
| 724 app_id, registrations_iter->second->registration_id, SUCCESS); | 736 // For GCM registration, we also match the sender IDs since multiple |
| 725 return; | 737 // registrations are not supported. |
| 738 const GCMRegistrationInfo* gcm_registration_info = | |
| 739 GCMRegistrationInfo::FromRegistrationInfo(registration_info.get()); | |
| 740 if (gcm_registration_info) { | |
| 741 const GCMRegistrationInfo* cached_gcm_registration_info = | |
| 742 GCMRegistrationInfo::FromRegistrationInfo( | |
| 743 registrations_iter->first.get()); | |
| 744 DCHECK(cached_gcm_registration_info); | |
| 745 if (cached_gcm_registration_info && | |
| 746 gcm_registration_info->sender_ids != | |
| 747 cached_gcm_registration_info->sender_ids) { | |
| 748 matched = false; | |
| 749 } | |
| 750 } | |
| 751 | |
| 752 if (matched) { | |
| 753 delegate_->OnRegisterFinished( | |
| 754 registration_info, registrations_iter->second, SUCCESS); | |
| 755 return; | |
| 756 } | |
| 726 } | 757 } |
| 727 | 758 |
| 728 RegistrationRequest::RequestInfo request_info( | |
| 729 device_checkin_info_.android_id, | |
| 730 device_checkin_info_.secret, | |
| 731 app_id, | |
| 732 sender_ids); | |
| 733 DCHECK_EQ(0u, pending_registration_requests_.count(app_id)); | |
| 734 | |
| 735 RegistrationRequest* registration_request = | 759 RegistrationRequest* registration_request = |
| 736 new RegistrationRequest(gservices_settings_.GetRegistrationURL(), | 760 new RegistrationRequest( |
| 737 request_info, | 761 gservices_settings_.GetRegistrationURL(), |
| 738 GetGCMBackoffPolicy(), | 762 BuildRegistrationRequestInfo(*registration_info).Pass(), |
| 739 base::Bind(&GCMClientImpl::OnRegisterCompleted, | 763 GetGCMBackoffPolicy(), |
| 740 weak_ptr_factory_.GetWeakPtr(), | 764 base::Bind(&GCMClientImpl::OnRegisterCompleted, |
| 741 app_id, | 765 weak_ptr_factory_.GetWeakPtr(), |
| 742 sender_ids), | 766 registration_info), |
| 743 kMaxRegistrationRetries, | 767 kMaxRegistrationRetries, |
| 744 url_request_context_getter_, | 768 url_request_context_getter_, |
| 745 &recorder_); | 769 &recorder_); |
| 746 pending_registration_requests_[app_id] = registration_request; | 770 pending_registration_requests_[registration_info] = registration_request; |
| 747 registration_request->Start(); | 771 registration_request->Start(); |
| 748 } | 772 } |
| 749 | 773 |
| 750 void GCMClientImpl::OnRegisterCompleted( | 774 void GCMClientImpl::OnRegisterCompleted( |
| 751 const std::string& app_id, | 775 const linked_ptr<RegistrationInfo>& registration_info, |
| 752 const std::vector<std::string>& sender_ids, | |
| 753 RegistrationRequest::Status status, | 776 RegistrationRequest::Status status, |
| 754 const std::string& registration_id) { | 777 const std::string& registration_id) { |
| 755 DCHECK(delegate_); | 778 DCHECK(delegate_); |
| 756 | 779 |
| 757 Result result; | 780 Result result; |
| 758 PendingRegistrationRequests::iterator iter = | 781 PendingRegistrationRequests::iterator iter = |
| 759 pending_registration_requests_.find(app_id); | 782 pending_registration_requests_.find(registration_info); |
| 760 if (iter == pending_registration_requests_.end()) | 783 if (iter == pending_registration_requests_.end()) |
| 761 result = UNKNOWN_ERROR; | 784 result = UNKNOWN_ERROR; |
| 762 else if (status == RegistrationRequest::INVALID_SENDER) | 785 else if (status == RegistrationRequest::INVALID_SENDER) |
|
fgorski
2015/05/13 18:32:38
Are there any new cases on the server side? Could
jianli
2015/05/13 22:42:56
Confirmed that no new cases.
| |
| 763 result = INVALID_PARAMETER; | 786 result = INVALID_PARAMETER; |
| 764 else if (registration_id.empty()) | 787 else if (registration_id.empty()) |
| 765 result = SERVER_ERROR; | 788 result = SERVER_ERROR; |
| 766 else | 789 else |
| 767 result = SUCCESS; | 790 result = SUCCESS; |
| 768 | 791 |
| 769 if (result == SUCCESS) { | 792 if (result == SUCCESS) { |
| 770 // Cache it. | 793 // Cache it. |
| 771 linked_ptr<RegistrationInfo> registration(new RegistrationInfo); | 794 registrations_[registration_info] = registration_id; |
| 772 registration->sender_ids = sender_ids; | |
| 773 registration->registration_id = registration_id; | |
| 774 registrations_[app_id] = registration; | |
| 775 | 795 |
| 776 // Save it in the persistent store. | 796 // Save it in the persistent store. |
| 777 gcm_store_->AddRegistration( | 797 gcm_store_->AddRegistration( |
| 778 app_id, | 798 registration_info->GetSerializedKey(), |
| 779 registration, | 799 registration_info->GetSerializedValue(registration_id), |
| 780 base::Bind(&GCMClientImpl::UpdateRegistrationCallback, | 800 base::Bind(&GCMClientImpl::UpdateRegistrationCallback, |
| 781 weak_ptr_factory_.GetWeakPtr())); | 801 weak_ptr_factory_.GetWeakPtr())); |
| 782 } | 802 } |
| 783 | 803 |
| 784 delegate_->OnRegisterFinished( | 804 delegate_->OnRegisterFinished( |
| 785 app_id, result == SUCCESS ? registration_id : std::string(), result); | 805 registration_info, |
| 806 result == SUCCESS ? registration_id : std::string(), | |
| 807 result); | |
| 786 | 808 |
| 787 if (iter != pending_registration_requests_.end()) { | 809 if (iter != pending_registration_requests_.end()) { |
| 788 delete iter->second; | 810 delete iter->second; |
| 789 pending_registration_requests_.erase(iter); | 811 pending_registration_requests_.erase(iter); |
| 790 } | 812 } |
| 791 } | 813 } |
| 792 | 814 |
| 793 void GCMClientImpl::Unregister(const std::string& app_id) { | 815 void GCMClientImpl::Unregister( |
| 816 const linked_ptr<RegistrationInfo>& registration_info) { | |
| 794 DCHECK_EQ(state_, READY); | 817 DCHECK_EQ(state_, READY); |
| 795 if (pending_unregistration_requests_.count(app_id) == 1) | 818 if (pending_unregistration_requests_.count(registration_info) == 1) |
| 796 return; | 819 return; |
| 797 | 820 |
| 798 // Remove from the cache and persistent store. | 821 // Remove from the cache and persistent store. |
| 799 registrations_.erase(app_id); | 822 registrations_.erase(registration_info); |
| 800 gcm_store_->RemoveRegistration( | 823 gcm_store_->RemoveRegistration( |
| 801 app_id, | 824 registration_info->GetSerializedKey(), |
| 802 base::Bind(&GCMClientImpl::UpdateRegistrationCallback, | 825 base::Bind(&GCMClientImpl::UpdateRegistrationCallback, |
| 803 weak_ptr_factory_.GetWeakPtr())); | 826 weak_ptr_factory_.GetWeakPtr())); |
| 804 | 827 |
| 805 UnregistrationRequest::RequestInfo request_info( | |
| 806 device_checkin_info_.android_id, | |
| 807 device_checkin_info_.secret, | |
| 808 app_id); | |
| 809 | |
| 810 UnregistrationRequest* unregistration_request = new UnregistrationRequest( | 828 UnregistrationRequest* unregistration_request = new UnregistrationRequest( |
| 811 gservices_settings_.GetRegistrationURL(), | 829 gservices_settings_.GetRegistrationURL(), |
| 812 request_info, | 830 BuildUnregistrationRequestInfo(*registration_info).Pass(), |
| 813 GetGCMBackoffPolicy(), | 831 GetGCMBackoffPolicy(), |
| 814 base::Bind(&GCMClientImpl::OnUnregisterCompleted, | 832 base::Bind(&GCMClientImpl::OnUnregisterCompleted, |
| 815 weak_ptr_factory_.GetWeakPtr(), | 833 weak_ptr_factory_.GetWeakPtr(), |
| 816 app_id), | 834 registration_info), |
| 817 url_request_context_getter_, | 835 url_request_context_getter_, |
| 818 &recorder_); | 836 &recorder_); |
| 819 pending_unregistration_requests_[app_id] = unregistration_request; | 837 pending_unregistration_requests_[registration_info] = unregistration_request; |
| 820 unregistration_request->Start(); | 838 unregistration_request->Start(); |
| 821 } | 839 } |
| 822 | 840 |
| 823 void GCMClientImpl::OnUnregisterCompleted( | 841 void GCMClientImpl::OnUnregisterCompleted( |
| 824 const std::string& app_id, | 842 const linked_ptr<RegistrationInfo>& registration_info, |
| 825 UnregistrationRequest::Status status) { | 843 UnregistrationRequest::Status status) { |
| 826 DVLOG(1) << "Unregister completed for app: " << app_id | 844 DVLOG(1) << "Unregister completed for app: " << registration_info->app_id |
| 827 << " with " << (status ? "success." : "failure."); | 845 << " with " << (status ? "success." : "failure."); |
| 828 delegate_->OnUnregisterFinished( | 846 delegate_->OnUnregisterFinished( |
| 829 app_id, | 847 registration_info, |
| 830 status == UnregistrationRequest::SUCCESS ? SUCCESS : SERVER_ERROR); | 848 status == UnregistrationRequest::SUCCESS ? SUCCESS : SERVER_ERROR); |
| 831 | 849 |
| 832 PendingUnregistrationRequests::iterator iter = | 850 PendingUnregistrationRequests::iterator iter = |
| 833 pending_unregistration_requests_.find(app_id); | 851 pending_unregistration_requests_.find(registration_info); |
| 834 if (iter == pending_unregistration_requests_.end()) | 852 if (iter == pending_unregistration_requests_.end()) |
| 835 return; | 853 return; |
| 836 | 854 |
| 837 delete iter->second; | 855 delete iter->second; |
| 838 pending_unregistration_requests_.erase(iter); | 856 pending_unregistration_requests_.erase(iter); |
| 839 } | 857 } |
| 840 | 858 |
| 841 void GCMClientImpl::OnGCMStoreDestroyed(bool success) { | 859 void GCMClientImpl::OnGCMStoreDestroyed(bool success) { |
| 842 DLOG_IF(ERROR, !success) << "GCM store failed to be destroyed!"; | 860 DLOG_IF(ERROR, !success) << "GCM store failed to be destroyed!"; |
| 843 UMA_HISTOGRAM_BOOLEAN("GCM.StoreDestroySucceeded", success); | 861 UMA_HISTOGRAM_BOOLEAN("GCM.StoreDestroySucceeded", success); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 885 case GCMClientImpl::INITIAL_DEVICE_CHECKIN: | 903 case GCMClientImpl::INITIAL_DEVICE_CHECKIN: |
| 886 return "INITIAL_DEVICE_CHECKIN"; | 904 return "INITIAL_DEVICE_CHECKIN"; |
| 887 case GCMClientImpl::READY: | 905 case GCMClientImpl::READY: |
| 888 return "READY"; | 906 return "READY"; |
| 889 default: | 907 default: |
| 890 NOTREACHED(); | 908 NOTREACHED(); |
| 891 return std::string(); | 909 return std::string(); |
| 892 } | 910 } |
| 893 } | 911 } |
| 894 | 912 |
| 913 scoped_ptr<RegistrationRequest::RequestInfo> | |
| 914 GCMClientImpl::BuildRegistrationRequestInfo( | |
| 915 const RegistrationInfo& registration_info) const { | |
| 916 const GCMRegistrationInfo* gcm_registration_info = | |
| 917 GCMRegistrationInfo::FromRegistrationInfo(®istration_info); | |
| 918 if (gcm_registration_info) { | |
| 919 scoped_ptr<RegistrationRequest::GCMRequestInfo> request_info( | |
| 920 new RegistrationRequest::GCMRequestInfo); | |
| 921 request_info->chrome_version = chrome_build_info_.version; | |
| 922 request_info->android_id = device_checkin_info_.android_id; | |
| 923 request_info->security_token = device_checkin_info_.secret; | |
| 924 request_info->app_id = registration_info.app_id; | |
| 925 request_info->sender_ids = gcm_registration_info->sender_ids; | |
| 926 return request_info.Pass(); | |
| 927 } | |
| 928 | |
| 929 const InstanceIDTokenInfo* instance_id_token_info = | |
| 930 InstanceIDTokenInfo::FromRegistrationInfo(®istration_info); | |
| 931 if (!instance_id_token_info) | |
| 932 return scoped_ptr<RegistrationRequest::RequestInfo>(); | |
| 933 | |
| 934 scoped_ptr<RegistrationRequest::InstanceIDRequestInfo> request_info( | |
| 935 new RegistrationRequest::InstanceIDRequestInfo); | |
| 936 request_info->chrome_version = chrome_build_info_.version; | |
| 937 request_info->android_id = device_checkin_info_.android_id; | |
| 938 request_info->security_token = device_checkin_info_.secret; | |
| 939 request_info->app_id = registration_info.app_id; | |
| 940 request_info->authorized_entity = instance_id_token_info->authorized_entity; | |
| 941 request_info->scope = instance_id_token_info->scope; | |
| 942 request_info->options = instance_id_token_info->options; | |
| 943 return request_info.Pass(); | |
| 944 } | |
| 945 | |
| 946 scoped_ptr<UnregistrationRequest::RequestInfo> | |
| 947 GCMClientImpl::BuildUnregistrationRequestInfo( | |
|
fgorski
2015/05/13 18:32:39
Looks like this code belongs in registration_info
jianli
2015/05/13 22:42:56
To move these codes into registration_info, we wil
| |
| 948 const RegistrationInfo& registration_info) const { | |
| 949 const GCMRegistrationInfo* gcm_registration_info = | |
| 950 GCMRegistrationInfo::FromRegistrationInfo(®istration_info); | |
| 951 if (gcm_registration_info) { | |
| 952 scoped_ptr<UnregistrationRequest::GCMRequestInfo> request_info( | |
| 953 new UnregistrationRequest::GCMRequestInfo); | |
| 954 request_info->chrome_version = chrome_build_info_.version; | |
| 955 request_info->android_id = device_checkin_info_.android_id; | |
| 956 request_info->security_token = device_checkin_info_.secret; | |
| 957 request_info->app_id = registration_info.app_id; | |
| 958 return request_info.Pass(); | |
| 959 } | |
| 960 | |
| 961 const InstanceIDTokenInfo* instance_id_token_info = | |
| 962 InstanceIDTokenInfo::FromRegistrationInfo(®istration_info); | |
| 963 if (!instance_id_token_info) | |
| 964 return scoped_ptr<UnregistrationRequest::RequestInfo>(); | |
| 965 | |
| 966 scoped_ptr<UnregistrationRequest::InstanceIDRequestInfo> request_info( | |
| 967 new UnregistrationRequest::InstanceIDRequestInfo); | |
| 968 request_info->chrome_version = chrome_build_info_.version; | |
| 969 request_info->android_id = device_checkin_info_.android_id; | |
| 970 request_info->security_token = device_checkin_info_.secret; | |
| 971 request_info->app_id = registration_info.app_id; | |
| 972 request_info->authorized_entity = instance_id_token_info->authorized_entity; | |
| 973 request_info->scope = instance_id_token_info->scope; | |
| 974 return request_info.Pass(); | |
| 975 } | |
| 976 | |
| 895 void GCMClientImpl::SetRecording(bool recording) { | 977 void GCMClientImpl::SetRecording(bool recording) { |
| 896 recorder_.SetRecording(recording); | 978 recorder_.SetRecording(recording); |
| 897 } | 979 } |
| 898 | 980 |
| 899 void GCMClientImpl::ClearActivityLogs() { | 981 void GCMClientImpl::ClearActivityLogs() { |
| 900 recorder_.Clear(); | 982 recorder_.Clear(); |
| 901 } | 983 } |
| 902 | 984 |
| 903 GCMClient::GCMStatistics GCMClientImpl::GetStatistics() const { | 985 GCMClient::GCMStatistics GCMClientImpl::GetStatistics() const { |
| 904 GCMClient::GCMStatistics stats; | 986 GCMClient::GCMStatistics stats; |
| 905 stats.gcm_client_created = true; | 987 stats.gcm_client_created = true; |
| 906 stats.is_recording = recorder_.is_recording(); | 988 stats.is_recording = recorder_.is_recording(); |
| 907 stats.gcm_client_state = GetStateString(); | 989 stats.gcm_client_state = GetStateString(); |
| 908 stats.connection_client_created = mcs_client_.get() != NULL; | 990 stats.connection_client_created = mcs_client_.get() != NULL; |
| 909 if (connection_factory_.get()) | 991 if (connection_factory_.get()) |
| 910 stats.connection_state = connection_factory_->GetConnectionStateString(); | 992 stats.connection_state = connection_factory_->GetConnectionStateString(); |
| 911 if (mcs_client_.get()) { | 993 if (mcs_client_.get()) { |
| 912 stats.send_queue_size = mcs_client_->GetSendQueueSize(); | 994 stats.send_queue_size = mcs_client_->GetSendQueueSize(); |
| 913 stats.resend_queue_size = mcs_client_->GetResendQueueSize(); | 995 stats.resend_queue_size = mcs_client_->GetResendQueueSize(); |
| 914 } | 996 } |
| 915 if (device_checkin_info_.android_id > 0) | 997 if (device_checkin_info_.android_id > 0) |
| 916 stats.android_id = device_checkin_info_.android_id; | 998 stats.android_id = device_checkin_info_.android_id; |
| 917 recorder_.CollectActivities(&stats.recorded_activities); | 999 recorder_.CollectActivities(&stats.recorded_activities); |
| 918 | 1000 |
| 919 for (RegistrationInfoMap::const_iterator it = registrations_.begin(); | 1001 for (RegistrationInfoMap::const_iterator it = registrations_.begin(); |
| 920 it != registrations_.end(); ++it) { | 1002 it != registrations_.end(); ++it) { |
| 921 stats.registered_app_ids.push_back(it->first); | 1003 stats.registered_app_ids.push_back(it->first->app_id); |
| 922 } | 1004 } |
| 923 return stats; | 1005 return stats; |
| 924 } | 1006 } |
| 925 | 1007 |
| 926 void GCMClientImpl::OnActivityRecorded() { | 1008 void GCMClientImpl::OnActivityRecorded() { |
| 927 delegate_->OnActivityRecorded(); | 1009 delegate_->OnActivityRecorded(); |
| 928 } | 1010 } |
| 929 | 1011 |
| 930 void GCMClientImpl::OnConnected(const GURL& current_server, | 1012 void GCMClientImpl::OnConnected(const GURL& current_server, |
| 931 const net::IPEndPoint& ip_endpoint) { | 1013 const net::IPEndPoint& ip_endpoint) { |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1029 DVLOG(1) << "Unknown message_type received. Message ignored. " | 1111 DVLOG(1) << "Unknown message_type received. Message ignored. " |
| 1030 << "App ID: " << data_message_stanza.category() << "."; | 1112 << "App ID: " << data_message_stanza.category() << "."; |
| 1031 break; | 1113 break; |
| 1032 } | 1114 } |
| 1033 } | 1115 } |
| 1034 | 1116 |
| 1035 void GCMClientImpl::HandleIncomingDataMessage( | 1117 void GCMClientImpl::HandleIncomingDataMessage( |
| 1036 const mcs_proto::DataMessageStanza& data_message_stanza, | 1118 const mcs_proto::DataMessageStanza& data_message_stanza, |
| 1037 MessageData& message_data) { | 1119 MessageData& message_data) { |
| 1038 std::string app_id = data_message_stanza.category(); | 1120 std::string app_id = data_message_stanza.category(); |
| 1121 std::string sender = data_message_stanza.from(); | |
| 1039 | 1122 |
| 1040 // Drop the message when the app is not registered for the sender of the | 1123 // Drop the message when the app is not registered for the sender of the |
| 1041 // message. | 1124 // message. |
| 1042 RegistrationInfoMap::iterator iter = registrations_.find(app_id); | 1125 bool registered = false; |
| 1043 bool not_registered = | 1126 |
| 1044 iter == registrations_.end() || | 1127 // First, find among all GCM registrations. |
| 1045 std::find(iter->second->sender_ids.begin(), | 1128 scoped_ptr<GCMRegistrationInfo> gcm_registration(new GCMRegistrationInfo); |
| 1046 iter->second->sender_ids.end(), | 1129 gcm_registration->app_id = app_id; |
| 1047 data_message_stanza.from()) == iter->second->sender_ids.end(); | 1130 auto gcm_registration_iter = registrations_.find( |
| 1048 recorder_.RecordDataMessageReceived(app_id, data_message_stanza.from(), | 1131 make_linked_ptr<RegistrationInfo>(gcm_registration.release())); |
| 1049 data_message_stanza.ByteSize(), !not_registered, | 1132 if (gcm_registration_iter != registrations_.end()) { |
| 1133 GCMRegistrationInfo* cached_gcm_registration = | |
| 1134 GCMRegistrationInfo::FromRegistrationInfo( | |
| 1135 gcm_registration_iter->first.get()); | |
| 1136 if (cached_gcm_registration && | |
| 1137 std::find(cached_gcm_registration->sender_ids.begin(), | |
| 1138 cached_gcm_registration->sender_ids.end(), | |
| 1139 sender) != cached_gcm_registration->sender_ids.end()) | |
| 1140 registered = true; | |
|
fgorski
2015/05/13 18:32:38
surround with {} because of multi-line condition
jianli
2015/05/13 22:42:56
Done.
| |
| 1141 } | |
| 1142 | |
| 1143 // Then, find among all InstanceID registrations. | |
| 1144 if (!registered) { | |
| 1145 scoped_ptr<InstanceIDTokenInfo> instance_id_token(new InstanceIDTokenInfo); | |
| 1146 instance_id_token->app_id = app_id; | |
| 1147 instance_id_token->authorized_entity = sender; | |
| 1148 instance_id_token->scope = kGCMScope; | |
| 1149 auto instance_id_token_iter = registrations_.find( | |
| 1150 make_linked_ptr<RegistrationInfo>(instance_id_token.release())); | |
| 1151 if (instance_id_token_iter != registrations_.end()) | |
| 1152 registered = true; | |
| 1153 } | |
| 1154 | |
| 1155 recorder_.RecordDataMessageReceived(app_id, sender, | |
| 1156 data_message_stanza.ByteSize(), registered, | |
| 1050 GCMStatsRecorder::DATA_MESSAGE); | 1157 GCMStatsRecorder::DATA_MESSAGE); |
| 1051 if (not_registered) { | 1158 if (!registered) |
| 1052 return; | 1159 return; |
| 1053 } | |
| 1054 | 1160 |
| 1055 IncomingMessage incoming_message; | 1161 IncomingMessage incoming_message; |
| 1056 incoming_message.sender_id = data_message_stanza.from(); | 1162 incoming_message.sender_id = data_message_stanza.from(); |
| 1057 if (data_message_stanza.has_token()) | 1163 if (data_message_stanza.has_token()) |
| 1058 incoming_message.collapse_key = data_message_stanza.token(); | 1164 incoming_message.collapse_key = data_message_stanza.token(); |
| 1059 incoming_message.data = message_data; | 1165 incoming_message.data = message_data; |
| 1060 delegate_->OnMessageReceived(app_id, incoming_message); | 1166 delegate_->OnMessageReceived(app_id, incoming_message); |
| 1061 } | 1167 } |
| 1062 | 1168 |
| 1063 void GCMClientImpl::HandleIncomingSendError( | 1169 void GCMClientImpl::HandleIncomingSendError( |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 1081 delegate_->OnMessageSendError(data_message_stanza.category(), | 1187 delegate_->OnMessageSendError(data_message_stanza.category(), |
| 1082 send_error_details); | 1188 send_error_details); |
| 1083 } | 1189 } |
| 1084 | 1190 |
| 1085 bool GCMClientImpl::HasStandaloneRegisteredApp() const { | 1191 bool GCMClientImpl::HasStandaloneRegisteredApp() const { |
| 1086 if (registrations_.empty()) | 1192 if (registrations_.empty()) |
| 1087 return false; | 1193 return false; |
| 1088 // Note that account mapper is not counted as a standalone app since it is | 1194 // Note that account mapper is not counted as a standalone app since it is |
| 1089 // automatically started when other app uses GCM. | 1195 // automatically started when other app uses GCM. |
| 1090 return registrations_.size() > 1 || | 1196 return registrations_.size() > 1 || |
| 1091 !registrations_.count(kGCMAccountMapperAppId); | 1197 !ExistsGCMRegistrationInMap(registrations_, kGCMAccountMapperAppId); |
|
fgorski
2015/05/13 18:32:39
Where is that defined?
jianli
2015/05/13 22:42:56
In registration_info.h
| |
| 1092 } | 1198 } |
| 1093 | 1199 |
| 1094 } // namespace gcm | 1200 } // namespace gcm |
| OLD | NEW |