| 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_device_manager.h" | 5 #include "components/proximity_auth/cryptauth/cryptauth_device_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/prefs/scoped_user_pref_update.h" | 9 #include "base/prefs/scoped_user_pref_update.h" |
| 10 #include "components/proximity_auth/cryptauth/base64url.h" | 10 #include "components/proximity_auth/cryptauth/base64url.h" |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 external_device->set_unlock_key(true); | 81 external_device->set_unlock_key(true); |
| 82 external_device->set_unlockable(false); | 82 external_device->set_unlockable(false); |
| 83 return true; | 83 return true; |
| 84 } | 84 } |
| 85 | 85 |
| 86 } // namespace | 86 } // namespace |
| 87 | 87 |
| 88 CryptAuthDeviceManager::CryptAuthDeviceManager( | 88 CryptAuthDeviceManager::CryptAuthDeviceManager( |
| 89 scoped_ptr<base::Clock> clock, | 89 scoped_ptr<base::Clock> clock, |
| 90 scoped_ptr<CryptAuthClientFactory> client_factory, | 90 scoped_ptr<CryptAuthClientFactory> client_factory, |
| 91 CryptAuthGCMManager* gcm_manager, |
| 91 PrefService* pref_service) | 92 PrefService* pref_service) |
| 92 : clock_(clock.Pass()), | 93 : clock_(clock.Pass()), |
| 93 client_factory_(client_factory.Pass()), | 94 client_factory_(client_factory.Pass()), |
| 95 gcm_manager_(gcm_manager), |
| 94 pref_service_(pref_service), | 96 pref_service_(pref_service), |
| 95 weak_ptr_factory_(this) { | 97 weak_ptr_factory_(this) {} |
| 96 } | |
| 97 | 98 |
| 98 CryptAuthDeviceManager::~CryptAuthDeviceManager() { | 99 CryptAuthDeviceManager::~CryptAuthDeviceManager() { |
| 100 gcm_manager_->RemoveObserver(this); |
| 99 } | 101 } |
| 100 | 102 |
| 101 // static | 103 // static |
| 102 void CryptAuthDeviceManager::RegisterPrefs(PrefRegistrySimple* registry) { | 104 void CryptAuthDeviceManager::RegisterPrefs(PrefRegistrySimple* registry) { |
| 103 registry->RegisterDoublePref(prefs::kCryptAuthDeviceSyncLastSyncTimeSeconds, | 105 registry->RegisterDoublePref(prefs::kCryptAuthDeviceSyncLastSyncTimeSeconds, |
| 104 0.0); | 106 0.0); |
| 105 registry->RegisterBooleanPref( | 107 registry->RegisterBooleanPref( |
| 106 prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure, false); | 108 prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure, false); |
| 107 registry->RegisterIntegerPref(prefs::kCryptAuthDeviceSyncReason, | 109 registry->RegisterIntegerPref(prefs::kCryptAuthDeviceSyncReason, |
| 108 cryptauth::INVOCATION_REASON_UNKNOWN); | 110 cryptauth::INVOCATION_REASON_UNKNOWN); |
| 109 registry->RegisterListPref(prefs::kCryptAuthDeviceSyncUnlockKeys); | 111 registry->RegisterListPref(prefs::kCryptAuthDeviceSyncUnlockKeys); |
| 110 } | 112 } |
| 111 | 113 |
| 112 void CryptAuthDeviceManager::Start() { | 114 void CryptAuthDeviceManager::Start() { |
| 113 UpdateUnlockKeysFromPrefs(); | 115 UpdateUnlockKeysFromPrefs(); |
| 114 | 116 |
| 117 gcm_manager_->AddObserver(this); |
| 118 |
| 115 base::Time last_successful_sync = GetLastSyncTime(); | 119 base::Time last_successful_sync = GetLastSyncTime(); |
| 116 base::TimeDelta elapsed_time_since_last_sync = | 120 base::TimeDelta elapsed_time_since_last_sync = |
| 117 clock_->Now() - last_successful_sync; | 121 clock_->Now() - last_successful_sync; |
| 118 | 122 |
| 119 bool is_recovering_from_failure = | 123 bool is_recovering_from_failure = |
| 120 pref_service_->GetBoolean( | 124 pref_service_->GetBoolean( |
| 121 prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure) || | 125 prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure) || |
| 122 last_successful_sync.is_null(); | 126 last_successful_sync.is_null(); |
| 123 | 127 |
| 124 scheduler_ = CreateSyncScheduler(); | 128 scheduler_ = CreateSyncScheduler(); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 OnSyncFinished(SyncResult::FAILURE, DeviceChangeResult::UNCHANGED)); | 213 OnSyncFinished(SyncResult::FAILURE, DeviceChangeResult::UNCHANGED)); |
| 210 } | 214 } |
| 211 | 215 |
| 212 scoped_ptr<SyncScheduler> CryptAuthDeviceManager::CreateSyncScheduler() { | 216 scoped_ptr<SyncScheduler> CryptAuthDeviceManager::CreateSyncScheduler() { |
| 213 return make_scoped_ptr(new SyncSchedulerImpl( | 217 return make_scoped_ptr(new SyncSchedulerImpl( |
| 214 this, base::TimeDelta::FromHours(kRefreshPeriodHours), | 218 this, base::TimeDelta::FromHours(kRefreshPeriodHours), |
| 215 base::TimeDelta::FromMinutes(kDeviceSyncBaseRecoveryPeriodMinutes), | 219 base::TimeDelta::FromMinutes(kDeviceSyncBaseRecoveryPeriodMinutes), |
| 216 kDeviceSyncMaxJitterRatio, "CryptAuth DeviceSync")); | 220 kDeviceSyncMaxJitterRatio, "CryptAuth DeviceSync")); |
| 217 } | 221 } |
| 218 | 222 |
| 223 void CryptAuthDeviceManager::OnResyncMessage() { |
| 224 ForceSyncNow(cryptauth::INVOCATION_REASON_SERVER_INITIATED); |
| 225 } |
| 226 |
| 219 void CryptAuthDeviceManager::UpdateUnlockKeysFromPrefs() { | 227 void CryptAuthDeviceManager::UpdateUnlockKeysFromPrefs() { |
| 220 const base::ListValue* unlock_key_list = | 228 const base::ListValue* unlock_key_list = |
| 221 pref_service_->GetList(prefs::kCryptAuthDeviceSyncUnlockKeys); | 229 pref_service_->GetList(prefs::kCryptAuthDeviceSyncUnlockKeys); |
| 222 unlock_keys_.clear(); | 230 unlock_keys_.clear(); |
| 223 for (size_t i = 0; i < unlock_key_list->GetSize(); ++i) { | 231 for (size_t i = 0; i < unlock_key_list->GetSize(); ++i) { |
| 224 const base::DictionaryValue* unlock_key_dictionary; | 232 const base::DictionaryValue* unlock_key_dictionary; |
| 225 if (unlock_key_list->GetDictionary(i, &unlock_key_dictionary)) { | 233 if (unlock_key_list->GetDictionary(i, &unlock_key_dictionary)) { |
| 226 cryptauth::ExternalDeviceInfo unlock_key; | 234 cryptauth::ExternalDeviceInfo unlock_key; |
| 227 if (DictionaryToUnlockKey(*unlock_key_dictionary, &unlock_key)) { | 235 if (DictionaryToUnlockKey(*unlock_key_dictionary, &unlock_key)) { |
| 228 unlock_keys_.push_back(unlock_key); | 236 unlock_keys_.push_back(unlock_key); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 cryptauth::GetMyDevicesRequest request; | 273 cryptauth::GetMyDevicesRequest request; |
| 266 request.set_invocation_reason(invocation_reason); | 274 request.set_invocation_reason(invocation_reason); |
| 267 cryptauth_client_->GetMyDevices( | 275 cryptauth_client_->GetMyDevices( |
| 268 request, base::Bind(&CryptAuthDeviceManager::OnGetMyDevicesSuccess, | 276 request, base::Bind(&CryptAuthDeviceManager::OnGetMyDevicesSuccess, |
| 269 weak_ptr_factory_.GetWeakPtr()), | 277 weak_ptr_factory_.GetWeakPtr()), |
| 270 base::Bind(&CryptAuthDeviceManager::OnGetMyDevicesFailure, | 278 base::Bind(&CryptAuthDeviceManager::OnGetMyDevicesFailure, |
| 271 weak_ptr_factory_.GetWeakPtr())); | 279 weak_ptr_factory_.GetWeakPtr())); |
| 272 } | 280 } |
| 273 | 281 |
| 274 } // namespace proximity_auth | 282 } // namespace proximity_auth |
| OLD | NEW |