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/location.h" | 9 #include "base/location.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 } | 295 } |
296 | 296 |
297 GCMClientImpl::GCMClientImpl(scoped_ptr<GCMInternalsBuilder> internals_builder) | 297 GCMClientImpl::GCMClientImpl(scoped_ptr<GCMInternalsBuilder> internals_builder) |
298 : internals_builder_(internals_builder.Pass()), | 298 : internals_builder_(internals_builder.Pass()), |
299 state_(UNINITIALIZED), | 299 state_(UNINITIALIZED), |
300 delegate_(NULL), | 300 delegate_(NULL), |
301 start_mode_(DELAYED_START), | 301 start_mode_(DELAYED_START), |
302 clock_(internals_builder_->BuildClock()), | 302 clock_(internals_builder_->BuildClock()), |
303 gcm_store_reset_(false), | 303 gcm_store_reset_(false), |
304 url_request_context_getter_(NULL), | 304 url_request_context_getter_(NULL), |
305 pending_registration_requests_deleter_(&pending_registration_requests_), | |
306 pending_unregistration_requests_deleter_( | |
307 &pending_unregistration_requests_), | |
308 periodic_checkin_ptr_factory_(this), | 305 periodic_checkin_ptr_factory_(this), |
309 destroying_gcm_store_ptr_factory_(this), | 306 destroying_gcm_store_ptr_factory_(this), |
310 weak_ptr_factory_(this) { | 307 weak_ptr_factory_(this) { |
311 } | 308 } |
312 | 309 |
313 GCMClientImpl::~GCMClientImpl() { | 310 GCMClientImpl::~GCMClientImpl() { |
314 } | 311 } |
315 | 312 |
316 void GCMClientImpl::Initialize( | 313 void GCMClientImpl::Initialize( |
317 const ChromeBuildInfo& chrome_build_info, | 314 const ChromeBuildInfo& chrome_build_info, |
(...skipping 514 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
832 | 829 |
833 void GCMClientImpl::ResetCache() { | 830 void GCMClientImpl::ResetCache() { |
834 weak_ptr_factory_.InvalidateWeakPtrs(); | 831 weak_ptr_factory_.InvalidateWeakPtrs(); |
835 periodic_checkin_ptr_factory_.InvalidateWeakPtrs(); | 832 periodic_checkin_ptr_factory_.InvalidateWeakPtrs(); |
836 device_checkin_info_.Reset(); | 833 device_checkin_info_.Reset(); |
837 connection_factory_.reset(); | 834 connection_factory_.reset(); |
838 delegate_->OnDisconnected(); | 835 delegate_->OnDisconnected(); |
839 mcs_client_.reset(); | 836 mcs_client_.reset(); |
840 checkin_request_.reset(); | 837 checkin_request_.reset(); |
841 // Delete all of the pending registration and unregistration requests. | 838 // Delete all of the pending registration and unregistration requests. |
842 STLDeleteValues(&pending_registration_requests_); | 839 pending_registration_requests_.clear(); |
843 STLDeleteValues(&pending_unregistration_requests_); | 840 pending_unregistration_requests_.clear(); |
844 } | 841 } |
845 | 842 |
846 void GCMClientImpl::Register( | 843 void GCMClientImpl::Register( |
847 const linked_ptr<RegistrationInfo>& registration_info) { | 844 const linked_ptr<RegistrationInfo>& registration_info) { |
848 DCHECK_EQ(state_, READY); | 845 DCHECK_EQ(state_, READY); |
849 | 846 |
850 // Find and use the cached registration ID. | 847 // Find and use the cached registration ID. |
851 RegistrationInfoMap::const_iterator registrations_iter = | 848 RegistrationInfoMap::const_iterator registrations_iter = |
852 registrations_.find(registration_info); | 849 registrations_.find(registration_info); |
853 if (registrations_iter != registrations_.end()) { | 850 if (registrations_iter != registrations_.end()) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
910 ConstructGCMVersion(chrome_build_info_.version), | 907 ConstructGCMVersion(chrome_build_info_.version), |
911 instance_id_token_info->options)); | 908 instance_id_token_info->options)); |
912 source_to_record = instance_id_token_info->authorized_entity; | 909 source_to_record = instance_id_token_info->authorized_entity; |
913 } | 910 } |
914 | 911 |
915 RegistrationRequest::RequestInfo request_info( | 912 RegistrationRequest::RequestInfo request_info( |
916 device_checkin_info_.android_id, | 913 device_checkin_info_.android_id, |
917 device_checkin_info_.secret, | 914 device_checkin_info_.secret, |
918 registration_info->app_id); | 915 registration_info->app_id); |
919 | 916 |
920 RegistrationRequest* registration_request = | 917 scoped_ptr<RegistrationRequest> registration_request(new RegistrationRequest( |
921 new RegistrationRequest(gservices_settings_.GetRegistrationURL(), | 918 gservices_settings_.GetRegistrationURL(), request_info, |
922 request_info, | 919 request_handler.Pass(), GetGCMBackoffPolicy(), |
923 request_handler.Pass(), | 920 base::Bind(&GCMClientImpl::OnRegisterCompleted, |
924 GetGCMBackoffPolicy(), | 921 weak_ptr_factory_.GetWeakPtr(), registration_info), |
925 base::Bind(&GCMClientImpl::OnRegisterCompleted, | 922 kMaxRegistrationRetries, url_request_context_getter_, &recorder_, |
926 weak_ptr_factory_.GetWeakPtr(), | 923 source_to_record)); |
927 registration_info), | |
928 kMaxRegistrationRetries, | |
929 url_request_context_getter_, | |
930 &recorder_, | |
931 source_to_record); | |
932 pending_registration_requests_[registration_info] = registration_request; | |
933 registration_request->Start(); | 924 registration_request->Start(); |
| 925 pending_registration_requests_.insert(registration_info, |
| 926 registration_request.Pass()); |
934 } | 927 } |
935 | 928 |
936 void GCMClientImpl::OnRegisterCompleted( | 929 void GCMClientImpl::OnRegisterCompleted( |
937 const linked_ptr<RegistrationInfo>& registration_info, | 930 const linked_ptr<RegistrationInfo>& registration_info, |
938 RegistrationRequest::Status status, | 931 RegistrationRequest::Status status, |
939 const std::string& registration_id) { | 932 const std::string& registration_id) { |
940 DCHECK(delegate_); | 933 DCHECK(delegate_); |
941 | 934 |
942 Result result; | 935 Result result; |
943 PendingRegistrationRequests::iterator iter = | 936 PendingRegistrationRequests::const_iterator iter = |
944 pending_registration_requests_.find(registration_info); | 937 pending_registration_requests_.find(registration_info); |
945 if (iter == pending_registration_requests_.end()) | 938 if (iter == pending_registration_requests_.end()) |
946 result = UNKNOWN_ERROR; | 939 result = UNKNOWN_ERROR; |
947 else if (status == RegistrationRequest::INVALID_SENDER) | 940 else if (status == RegistrationRequest::INVALID_SENDER) |
948 result = INVALID_PARAMETER; | 941 result = INVALID_PARAMETER; |
949 else if (registration_id.empty()) | 942 else if (registration_id.empty()) |
950 result = SERVER_ERROR; | 943 result = SERVER_ERROR; |
951 else | 944 else |
952 result = SUCCESS; | 945 result = SUCCESS; |
953 | 946 |
954 if (result == SUCCESS) { | 947 if (result == SUCCESS) { |
955 // Cache it. | 948 // Cache it. |
956 registrations_[registration_info] = registration_id; | 949 registrations_[registration_info] = registration_id; |
957 | 950 |
958 // Save it in the persistent store. | 951 // Save it in the persistent store. |
959 gcm_store_->AddRegistration( | 952 gcm_store_->AddRegistration( |
960 registration_info->GetSerializedKey(), | 953 registration_info->GetSerializedKey(), |
961 registration_info->GetSerializedValue(registration_id), | 954 registration_info->GetSerializedValue(registration_id), |
962 base::Bind(&GCMClientImpl::UpdateRegistrationCallback, | 955 base::Bind(&GCMClientImpl::UpdateRegistrationCallback, |
963 weak_ptr_factory_.GetWeakPtr())); | 956 weak_ptr_factory_.GetWeakPtr())); |
964 } | 957 } |
965 | 958 |
966 delegate_->OnRegisterFinished( | 959 delegate_->OnRegisterFinished( |
967 registration_info, | 960 registration_info, |
968 result == SUCCESS ? registration_id : std::string(), | 961 result == SUCCESS ? registration_id : std::string(), |
969 result); | 962 result); |
970 | 963 |
971 if (iter != pending_registration_requests_.end()) { | 964 if (iter != pending_registration_requests_.end()) |
972 delete iter->second; | |
973 pending_registration_requests_.erase(iter); | 965 pending_registration_requests_.erase(iter); |
974 } | |
975 } | 966 } |
976 | 967 |
977 void GCMClientImpl::Unregister( | 968 void GCMClientImpl::Unregister( |
978 const linked_ptr<RegistrationInfo>& registration_info) { | 969 const linked_ptr<RegistrationInfo>& registration_info) { |
979 DCHECK_EQ(state_, READY); | 970 DCHECK_EQ(state_, READY); |
980 | 971 |
981 scoped_ptr<UnregistrationRequest::CustomRequestHandler> request_handler; | 972 scoped_ptr<UnregistrationRequest::CustomRequestHandler> request_handler; |
982 | 973 |
983 const GCMRegistrationInfo* gcm_registration_info = | 974 const GCMRegistrationInfo* gcm_registration_info = |
984 GCMRegistrationInfo::FromRegistrationInfo(registration_info.get()); | 975 GCMRegistrationInfo::FromRegistrationInfo(registration_info.get()); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1048 registration_info->GetSerializedKey(), | 1039 registration_info->GetSerializedKey(), |
1049 base::Bind(&GCMClientImpl::UpdateRegistrationCallback, | 1040 base::Bind(&GCMClientImpl::UpdateRegistrationCallback, |
1050 weak_ptr_factory_.GetWeakPtr())); | 1041 weak_ptr_factory_.GetWeakPtr())); |
1051 } | 1042 } |
1052 | 1043 |
1053 UnregistrationRequest::RequestInfo request_info( | 1044 UnregistrationRequest::RequestInfo request_info( |
1054 device_checkin_info_.android_id, | 1045 device_checkin_info_.android_id, |
1055 device_checkin_info_.secret, | 1046 device_checkin_info_.secret, |
1056 registration_info->app_id); | 1047 registration_info->app_id); |
1057 | 1048 |
1058 UnregistrationRequest* unregistration_request = new UnregistrationRequest( | 1049 scoped_ptr<UnregistrationRequest> unregistration_request( |
1059 gservices_settings_.GetRegistrationURL(), | 1050 new UnregistrationRequest( |
1060 request_info, | 1051 gservices_settings_.GetRegistrationURL(), request_info, |
1061 request_handler.Pass(), | 1052 request_handler.Pass(), GetGCMBackoffPolicy(), |
1062 GetGCMBackoffPolicy(), | 1053 base::Bind(&GCMClientImpl::OnUnregisterCompleted, |
1063 base::Bind(&GCMClientImpl::OnUnregisterCompleted, | 1054 weak_ptr_factory_.GetWeakPtr(), registration_info), |
1064 weak_ptr_factory_.GetWeakPtr(), | 1055 kMaxUnregistrationRetries, url_request_context_getter_, &recorder_)); |
1065 registration_info), | |
1066 kMaxUnregistrationRetries, | |
1067 url_request_context_getter_, | |
1068 &recorder_); | |
1069 pending_unregistration_requests_[registration_info] = unregistration_request; | |
1070 unregistration_request->Start(); | 1056 unregistration_request->Start(); |
| 1057 pending_unregistration_requests_.insert(registration_info, |
| 1058 unregistration_request.Pass()); |
1071 } | 1059 } |
1072 | 1060 |
1073 void GCMClientImpl::OnUnregisterCompleted( | 1061 void GCMClientImpl::OnUnregisterCompleted( |
1074 const linked_ptr<RegistrationInfo>& registration_info, | 1062 const linked_ptr<RegistrationInfo>& registration_info, |
1075 UnregistrationRequest::Status status) { | 1063 UnregistrationRequest::Status status) { |
1076 DVLOG(1) << "Unregister completed for app: " << registration_info->app_id | 1064 DVLOG(1) << "Unregister completed for app: " << registration_info->app_id |
1077 << " with " << (status ? "success." : "failure."); | 1065 << " with " << (status ? "success." : "failure."); |
1078 | 1066 |
1079 Result result; | 1067 Result result; |
1080 switch (status) { | 1068 switch (status) { |
1081 case UnregistrationRequest::SUCCESS: | 1069 case UnregistrationRequest::SUCCESS: |
1082 result = SUCCESS; | 1070 result = SUCCESS; |
1083 break; | 1071 break; |
1084 case UnregistrationRequest::INVALID_PARAMETERS: | 1072 case UnregistrationRequest::INVALID_PARAMETERS: |
1085 result = INVALID_PARAMETER; | 1073 result = INVALID_PARAMETER; |
1086 break; | 1074 break; |
1087 default: | 1075 default: |
1088 // All other errors are treated as SERVER_ERROR. | 1076 // All other errors are treated as SERVER_ERROR. |
1089 result = SERVER_ERROR; | 1077 result = SERVER_ERROR; |
1090 break; | 1078 break; |
1091 } | 1079 } |
1092 delegate_->OnUnregisterFinished(registration_info, result); | 1080 delegate_->OnUnregisterFinished(registration_info, result); |
1093 | 1081 |
1094 PendingUnregistrationRequests::iterator iter = | 1082 pending_unregistration_requests_.erase(registration_info); |
1095 pending_unregistration_requests_.find(registration_info); | |
1096 if (iter == pending_unregistration_requests_.end()) | |
1097 return; | |
1098 | |
1099 delete iter->second; | |
1100 pending_unregistration_requests_.erase(iter); | |
1101 } | 1083 } |
1102 | 1084 |
1103 void GCMClientImpl::OnGCMStoreDestroyed(bool success) { | 1085 void GCMClientImpl::OnGCMStoreDestroyed(bool success) { |
1104 DLOG_IF(ERROR, !success) << "GCM store failed to be destroyed!"; | 1086 DLOG_IF(ERROR, !success) << "GCM store failed to be destroyed!"; |
1105 UMA_HISTOGRAM_BOOLEAN("GCM.StoreDestroySucceeded", success); | 1087 UMA_HISTOGRAM_BOOLEAN("GCM.StoreDestroySucceeded", success); |
1106 } | 1088 } |
1107 | 1089 |
1108 void GCMClientImpl::Send(const std::string& app_id, | 1090 void GCMClientImpl::Send(const std::string& app_id, |
1109 const std::string& receiver_id, | 1091 const std::string& receiver_id, |
1110 const OutgoingMessage& message) { | 1092 const OutgoingMessage& message) { |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1372 bool GCMClientImpl::HasStandaloneRegisteredApp() const { | 1354 bool GCMClientImpl::HasStandaloneRegisteredApp() const { |
1373 if (registrations_.empty()) | 1355 if (registrations_.empty()) |
1374 return false; | 1356 return false; |
1375 // Note that account mapper is not counted as a standalone app since it is | 1357 // Note that account mapper is not counted as a standalone app since it is |
1376 // automatically started when other app uses GCM. | 1358 // automatically started when other app uses GCM. |
1377 return registrations_.size() > 1 || | 1359 return registrations_.size() > 1 || |
1378 !ExistsGCMRegistrationInMap(registrations_, kGCMAccountMapperAppId); | 1360 !ExistsGCMRegistrationInMap(registrations_, kGCMAccountMapperAppId); |
1379 } | 1361 } |
1380 | 1362 |
1381 } // namespace gcm | 1363 } // namespace gcm |
OLD | NEW |