OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/proximity_auth/cryptauth/cryptauth_enrollment_manager.h" | 5 #include "components/proximity_auth/cryptauth/cryptauth_enrollment_manager.h" |
6 | 6 |
7 #include "base/prefs/pref_registry_simple.h" | 7 #include "base/prefs/pref_registry_simple.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
9 #include "base/time/clock.h" | 9 #include "base/time/clock.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 const double kEnrollmentMaxJitterRatio = 0.2; | 34 const double kEnrollmentMaxJitterRatio = 0.2; |
35 | 35 |
36 } // namespace | 36 } // namespace |
37 | 37 |
38 CryptAuthEnrollmentManager::CryptAuthEnrollmentManager( | 38 CryptAuthEnrollmentManager::CryptAuthEnrollmentManager( |
39 scoped_ptr<base::Clock> clock, | 39 scoped_ptr<base::Clock> clock, |
40 scoped_ptr<CryptAuthEnrollerFactory> enroller_factory, | 40 scoped_ptr<CryptAuthEnrollerFactory> enroller_factory, |
41 const std::string& user_public_key, | 41 const std::string& user_public_key, |
42 const std::string& user_private_key, | 42 const std::string& user_private_key, |
43 const cryptauth::GcmDeviceInfo& device_info, | 43 const cryptauth::GcmDeviceInfo& device_info, |
| 44 CryptAuthGCMManager* gcm_manager, |
44 PrefService* pref_service) | 45 PrefService* pref_service) |
45 : clock_(clock.Pass()), | 46 : clock_(clock.Pass()), |
46 enroller_factory_(enroller_factory.Pass()), | 47 enroller_factory_(enroller_factory.Pass()), |
47 user_public_key_(user_public_key), | 48 user_public_key_(user_public_key), |
48 user_private_key_(user_private_key), | 49 user_private_key_(user_private_key), |
49 device_info_(device_info), | 50 device_info_(device_info), |
| 51 gcm_manager_(gcm_manager), |
50 pref_service_(pref_service), | 52 pref_service_(pref_service), |
51 weak_ptr_factory_(this) { | 53 weak_ptr_factory_(this) {} |
52 } | |
53 | 54 |
54 CryptAuthEnrollmentManager::~CryptAuthEnrollmentManager() { | 55 CryptAuthEnrollmentManager::~CryptAuthEnrollmentManager() { |
| 56 gcm_manager_->RemoveObserver(this); |
55 } | 57 } |
56 | 58 |
57 // static | 59 // static |
58 void CryptAuthEnrollmentManager::RegisterPrefs(PrefRegistrySimple* registry) { | 60 void CryptAuthEnrollmentManager::RegisterPrefs(PrefRegistrySimple* registry) { |
59 registry->RegisterBooleanPref( | 61 registry->RegisterBooleanPref( |
60 prefs::kCryptAuthEnrollmentIsRecoveringFromFailure, false); | 62 prefs::kCryptAuthEnrollmentIsRecoveringFromFailure, false); |
61 registry->RegisterDoublePref( | 63 registry->RegisterDoublePref( |
62 prefs::kCryptAuthEnrollmentLastEnrollmentTimeSeconds, 0.0); | 64 prefs::kCryptAuthEnrollmentLastEnrollmentTimeSeconds, 0.0); |
63 registry->RegisterIntegerPref(prefs::kCryptAuthEnrollmentReason, | 65 registry->RegisterIntegerPref(prefs::kCryptAuthEnrollmentReason, |
64 cryptauth::INVOCATION_REASON_UNKNOWN); | 66 cryptauth::INVOCATION_REASON_UNKNOWN); |
65 } | 67 } |
66 | 68 |
67 void CryptAuthEnrollmentManager::Start() { | 69 void CryptAuthEnrollmentManager::Start() { |
| 70 gcm_manager_->AddObserver(this); |
| 71 |
68 bool is_recovering_from_failure = | 72 bool is_recovering_from_failure = |
69 pref_service_->GetBoolean( | 73 pref_service_->GetBoolean( |
70 prefs::kCryptAuthEnrollmentIsRecoveringFromFailure) || | 74 prefs::kCryptAuthEnrollmentIsRecoveringFromFailure) || |
71 !IsEnrollmentValid(); | 75 !IsEnrollmentValid(); |
72 | 76 |
73 base::Time last_successful_enrollment = GetLastEnrollmentTime(); | 77 base::Time last_successful_enrollment = GetLastEnrollmentTime(); |
74 base::TimeDelta elapsed_time_since_last_sync = | 78 base::TimeDelta elapsed_time_since_last_sync = |
75 clock_->Now() - last_successful_enrollment; | 79 clock_->Now() - last_successful_enrollment; |
76 | 80 |
77 scheduler_ = CreateSyncScheduler(); | 81 scheduler_ = CreateSyncScheduler(); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 FOR_EACH_OBSERVER(Observer, observers_, OnEnrollmentFinished(success)); | 147 FOR_EACH_OBSERVER(Observer, observers_, OnEnrollmentFinished(success)); |
144 } | 148 } |
145 | 149 |
146 scoped_ptr<SyncScheduler> CryptAuthEnrollmentManager::CreateSyncScheduler() { | 150 scoped_ptr<SyncScheduler> CryptAuthEnrollmentManager::CreateSyncScheduler() { |
147 return make_scoped_ptr(new SyncSchedulerImpl( | 151 return make_scoped_ptr(new SyncSchedulerImpl( |
148 this, base::TimeDelta::FromDays(kEnrollmentRefreshPeriodDays), | 152 this, base::TimeDelta::FromDays(kEnrollmentRefreshPeriodDays), |
149 base::TimeDelta::FromMinutes(kEnrollmentBaseRecoveryPeriodMinutes), | 153 base::TimeDelta::FromMinutes(kEnrollmentBaseRecoveryPeriodMinutes), |
150 kEnrollmentMaxJitterRatio, "CryptAuth Enrollment")); | 154 kEnrollmentMaxJitterRatio, "CryptAuth Enrollment")); |
151 } | 155 } |
152 | 156 |
| 157 void CryptAuthEnrollmentManager::OnGCMRegistrationResult(bool success) { |
| 158 if (!sync_request_) |
| 159 return; |
| 160 |
| 161 PA_LOG(INFO) << "GCM registration for CryptAuth Enrollment completed: " |
| 162 << success; |
| 163 if (success) |
| 164 DoCryptAuthEnrollment(); |
| 165 else |
| 166 OnEnrollmentFinished(false); |
| 167 } |
| 168 |
| 169 void CryptAuthEnrollmentManager::OnReenrollMessage() { |
| 170 ForceEnrollmentNow(cryptauth::INVOCATION_REASON_SERVER_INITIATED); |
| 171 } |
| 172 |
153 void CryptAuthEnrollmentManager::OnSyncRequested( | 173 void CryptAuthEnrollmentManager::OnSyncRequested( |
154 scoped_ptr<SyncScheduler::SyncRequest> sync_request) { | 174 scoped_ptr<SyncScheduler::SyncRequest> sync_request) { |
155 FOR_EACH_OBSERVER(Observer, observers_, OnEnrollmentStarted()); | 175 FOR_EACH_OBSERVER(Observer, observers_, OnEnrollmentStarted()); |
156 | 176 |
157 sync_request_ = sync_request.Pass(); | 177 sync_request_ = sync_request.Pass(); |
158 cryptauth_enroller_ = enroller_factory_->CreateInstance(); | |
159 | 178 |
| 179 if (gcm_manager_->GetRegistrationId().empty()) { |
| 180 gcm_manager_->RegisterWithGCM(); |
| 181 } else { |
| 182 DoCryptAuthEnrollment(); |
| 183 } |
| 184 } |
| 185 |
| 186 void CryptAuthEnrollmentManager::DoCryptAuthEnrollment() { |
| 187 DCHECK(sync_request_); |
160 cryptauth::InvocationReason invocation_reason = | 188 cryptauth::InvocationReason invocation_reason = |
161 cryptauth::INVOCATION_REASON_UNKNOWN; | 189 cryptauth::INVOCATION_REASON_UNKNOWN; |
162 | 190 |
163 int reason_stored_in_prefs = | 191 int reason_stored_in_prefs = |
164 pref_service_->GetInteger(prefs::kCryptAuthEnrollmentReason); | 192 pref_service_->GetInteger(prefs::kCryptAuthEnrollmentReason); |
165 | 193 |
166 if (cryptauth::InvocationReason_IsValid(reason_stored_in_prefs) && | 194 if (cryptauth::InvocationReason_IsValid(reason_stored_in_prefs) && |
167 reason_stored_in_prefs != cryptauth::INVOCATION_REASON_UNKNOWN) { | 195 reason_stored_in_prefs != cryptauth::INVOCATION_REASON_UNKNOWN) { |
168 invocation_reason = | 196 invocation_reason = |
169 static_cast<cryptauth::InvocationReason>(reason_stored_in_prefs); | 197 static_cast<cryptauth::InvocationReason>(reason_stored_in_prefs); |
170 } else if (GetLastEnrollmentTime().is_null()) { | 198 } else if (GetLastEnrollmentTime().is_null()) { |
171 invocation_reason = cryptauth::INVOCATION_REASON_INITIALIZATION; | 199 invocation_reason = cryptauth::INVOCATION_REASON_INITIALIZATION; |
172 } else if (!IsEnrollmentValid()) { | 200 } else if (!IsEnrollmentValid()) { |
173 invocation_reason = cryptauth::INVOCATION_REASON_EXPIRATION; | 201 invocation_reason = cryptauth::INVOCATION_REASON_EXPIRATION; |
174 } else if (scheduler_->GetStrategy() == | 202 } else if (scheduler_->GetStrategy() == |
175 SyncScheduler::Strategy::PERIODIC_REFRESH) { | 203 SyncScheduler::Strategy::PERIODIC_REFRESH) { |
176 invocation_reason = cryptauth::INVOCATION_REASON_PERIODIC; | 204 invocation_reason = cryptauth::INVOCATION_REASON_PERIODIC; |
177 } else if (scheduler_->GetStrategy() == | 205 } else if (scheduler_->GetStrategy() == |
178 SyncScheduler::Strategy::AGGRESSIVE_RECOVERY) { | 206 SyncScheduler::Strategy::AGGRESSIVE_RECOVERY) { |
179 invocation_reason = cryptauth::INVOCATION_REASON_FAILURE_RECOVERY; | 207 invocation_reason = cryptauth::INVOCATION_REASON_FAILURE_RECOVERY; |
180 } | 208 } |
181 | 209 |
182 PA_LOG(INFO) << "Making enrollment with reason: " << invocation_reason; | 210 PA_LOG(INFO) << "Making enrollment with reason: " << invocation_reason; |
| 211 cryptauth_enroller_ = enroller_factory_->CreateInstance(); |
183 cryptauth_enroller_->Enroll( | 212 cryptauth_enroller_->Enroll( |
184 user_public_key_, user_private_key_, device_info_, invocation_reason, | 213 user_public_key_, user_private_key_, device_info_, invocation_reason, |
185 base::Bind(&CryptAuthEnrollmentManager::OnEnrollmentFinished, | 214 base::Bind(&CryptAuthEnrollmentManager::OnEnrollmentFinished, |
186 weak_ptr_factory_.GetWeakPtr())); | 215 weak_ptr_factory_.GetWeakPtr())); |
187 } | 216 } |
188 | 217 |
189 } // namespace proximity_auth | 218 } // namespace proximity_auth |
OLD | NEW |