| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/base64url.h" | 11 #include "base/base64url.h" |
| 12 #include "base/memory/ptr_util.h" |
| 11 #include "components/prefs/pref_registry_simple.h" | 13 #include "components/prefs/pref_registry_simple.h" |
| 12 #include "components/prefs/pref_service.h" | 14 #include "components/prefs/pref_service.h" |
| 13 #include "components/prefs/scoped_user_pref_update.h" | 15 #include "components/prefs/scoped_user_pref_update.h" |
| 14 #include "components/proximity_auth/cryptauth/cryptauth_client.h" | 16 #include "components/proximity_auth/cryptauth/cryptauth_client.h" |
| 15 #include "components/proximity_auth/cryptauth/pref_names.h" | 17 #include "components/proximity_auth/cryptauth/pref_names.h" |
| 16 #include "components/proximity_auth/cryptauth/sync_scheduler_impl.h" | 18 #include "components/proximity_auth/cryptauth/sync_scheduler_impl.h" |
| 17 #include "components/proximity_auth/logging/logging.h" | 19 #include "components/proximity_auth/logging/logging.h" |
| 18 | 20 |
| 19 namespace proximity_auth { | 21 namespace proximity_auth { |
| 20 | 22 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 31 // The bound on the amount to jitter the period between syncs. | 33 // The bound on the amount to jitter the period between syncs. |
| 32 const double kDeviceSyncMaxJitterRatio = 0.2; | 34 const double kDeviceSyncMaxJitterRatio = 0.2; |
| 33 | 35 |
| 34 // Keys for ExternalDeviceInfo dictionaries that are stored in the user's prefs. | 36 // Keys for ExternalDeviceInfo dictionaries that are stored in the user's prefs. |
| 35 const char kExternalDeviceKeyPublicKey[] = "public_key"; | 37 const char kExternalDeviceKeyPublicKey[] = "public_key"; |
| 36 const char kExternalDeviceKeyDeviceName[] = "device_name"; | 38 const char kExternalDeviceKeyDeviceName[] = "device_name"; |
| 37 const char kExternalDeviceKeyBluetoothAddress[] = "bluetooth_address"; | 39 const char kExternalDeviceKeyBluetoothAddress[] = "bluetooth_address"; |
| 38 | 40 |
| 39 // Converts an unlock key proto to a dictionary that can be stored in user | 41 // Converts an unlock key proto to a dictionary that can be stored in user |
| 40 // prefs. | 42 // prefs. |
| 41 scoped_ptr<base::DictionaryValue> UnlockKeyToDictionary( | 43 std::unique_ptr<base::DictionaryValue> UnlockKeyToDictionary( |
| 42 const cryptauth::ExternalDeviceInfo& device) { | 44 const cryptauth::ExternalDeviceInfo& device) { |
| 43 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue()); | 45 std::unique_ptr<base::DictionaryValue> dictionary( |
| 46 new base::DictionaryValue()); |
| 44 | 47 |
| 45 // We store the device information in Base64Url form because dictionary values | 48 // We store the device information in Base64Url form because dictionary values |
| 46 // must be valid UTF8 strings. | 49 // must be valid UTF8 strings. |
| 47 std::string public_key_b64, device_name_b64, bluetooth_address_b64; | 50 std::string public_key_b64, device_name_b64, bluetooth_address_b64; |
| 48 base::Base64UrlEncode(device.public_key(), | 51 base::Base64UrlEncode(device.public_key(), |
| 49 base::Base64UrlEncodePolicy::INCLUDE_PADDING, | 52 base::Base64UrlEncodePolicy::INCLUDE_PADDING, |
| 50 &public_key_b64); | 53 &public_key_b64); |
| 51 base::Base64UrlEncode(device.friendly_device_name(), | 54 base::Base64UrlEncode(device.friendly_device_name(), |
| 52 base::Base64UrlEncodePolicy::INCLUDE_PADDING, | 55 base::Base64UrlEncodePolicy::INCLUDE_PADDING, |
| 53 &device_name_b64); | 56 &device_name_b64); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 external_device->set_friendly_device_name(device_name); | 97 external_device->set_friendly_device_name(device_name); |
| 95 external_device->set_bluetooth_address(bluetooth_address); | 98 external_device->set_bluetooth_address(bluetooth_address); |
| 96 external_device->set_unlock_key(true); | 99 external_device->set_unlock_key(true); |
| 97 external_device->set_unlockable(false); | 100 external_device->set_unlockable(false); |
| 98 return true; | 101 return true; |
| 99 } | 102 } |
| 100 | 103 |
| 101 } // namespace | 104 } // namespace |
| 102 | 105 |
| 103 CryptAuthDeviceManager::CryptAuthDeviceManager( | 106 CryptAuthDeviceManager::CryptAuthDeviceManager( |
| 104 scoped_ptr<base::Clock> clock, | 107 std::unique_ptr<base::Clock> clock, |
| 105 scoped_ptr<CryptAuthClientFactory> client_factory, | 108 std::unique_ptr<CryptAuthClientFactory> client_factory, |
| 106 CryptAuthGCMManager* gcm_manager, | 109 CryptAuthGCMManager* gcm_manager, |
| 107 PrefService* pref_service) | 110 PrefService* pref_service) |
| 108 : clock_(std::move(clock)), | 111 : clock_(std::move(clock)), |
| 109 client_factory_(std::move(client_factory)), | 112 client_factory_(std::move(client_factory)), |
| 110 gcm_manager_(gcm_manager), | 113 gcm_manager_(gcm_manager), |
| 111 pref_service_(pref_service), | 114 pref_service_(pref_service), |
| 112 weak_ptr_factory_(this) { | 115 weak_ptr_factory_(this) { |
| 113 UpdateUnlockKeysFromPrefs(); | 116 UpdateUnlockKeysFromPrefs(); |
| 114 } | 117 } |
| 115 | 118 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 } | 180 } |
| 178 | 181 |
| 179 bool CryptAuthDeviceManager::IsRecoveringFromFailure() const { | 182 bool CryptAuthDeviceManager::IsRecoveringFromFailure() const { |
| 180 return scheduler_->GetStrategy() == | 183 return scheduler_->GetStrategy() == |
| 181 SyncScheduler::Strategy::AGGRESSIVE_RECOVERY; | 184 SyncScheduler::Strategy::AGGRESSIVE_RECOVERY; |
| 182 } | 185 } |
| 183 | 186 |
| 184 void CryptAuthDeviceManager::OnGetMyDevicesSuccess( | 187 void CryptAuthDeviceManager::OnGetMyDevicesSuccess( |
| 185 const cryptauth::GetMyDevicesResponse& response) { | 188 const cryptauth::GetMyDevicesResponse& response) { |
| 186 // Update the unlock keys stored in the user's prefs. | 189 // Update the unlock keys stored in the user's prefs. |
| 187 scoped_ptr<base::ListValue> unlock_keys_pref(new base::ListValue()); | 190 std::unique_ptr<base::ListValue> unlock_keys_pref(new base::ListValue()); |
| 188 scoped_ptr<base::ListValue> devices_as_list(new base::ListValue()); | 191 std::unique_ptr<base::ListValue> devices_as_list(new base::ListValue()); |
| 189 for (const auto& device : response.devices()) { | 192 for (const auto& device : response.devices()) { |
| 190 devices_as_list->Append(UnlockKeyToDictionary(device)); | 193 devices_as_list->Append(UnlockKeyToDictionary(device)); |
| 191 if (device.unlock_key()) | 194 if (device.unlock_key()) |
| 192 unlock_keys_pref->Append(UnlockKeyToDictionary(device)); | 195 unlock_keys_pref->Append(UnlockKeyToDictionary(device)); |
| 193 } | 196 } |
| 194 PA_LOG(INFO) << "Devices Synced:\n" << *devices_as_list; | 197 PA_LOG(INFO) << "Devices Synced:\n" << *devices_as_list; |
| 195 | 198 |
| 196 bool unlock_keys_changed = !unlock_keys_pref->Equals( | 199 bool unlock_keys_changed = !unlock_keys_pref->Equals( |
| 197 pref_service_->GetList(prefs::kCryptAuthDeviceSyncUnlockKeys)); | 200 pref_service_->GetList(prefs::kCryptAuthDeviceSyncUnlockKeys)); |
| 198 { | 201 { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 224 pref_service_->SetBoolean(prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure, | 227 pref_service_->SetBoolean(prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure, |
| 225 true); | 228 true); |
| 226 sync_request_->OnDidComplete(false); | 229 sync_request_->OnDidComplete(false); |
| 227 cryptauth_client_.reset(); | 230 cryptauth_client_.reset(); |
| 228 sync_request_.reset(); | 231 sync_request_.reset(); |
| 229 FOR_EACH_OBSERVER( | 232 FOR_EACH_OBSERVER( |
| 230 Observer, observers_, | 233 Observer, observers_, |
| 231 OnSyncFinished(SyncResult::FAILURE, DeviceChangeResult::UNCHANGED)); | 234 OnSyncFinished(SyncResult::FAILURE, DeviceChangeResult::UNCHANGED)); |
| 232 } | 235 } |
| 233 | 236 |
| 234 scoped_ptr<SyncScheduler> CryptAuthDeviceManager::CreateSyncScheduler() { | 237 std::unique_ptr<SyncScheduler> CryptAuthDeviceManager::CreateSyncScheduler() { |
| 235 return make_scoped_ptr(new SyncSchedulerImpl( | 238 return base::WrapUnique(new SyncSchedulerImpl( |
| 236 this, base::TimeDelta::FromHours(kRefreshPeriodHours), | 239 this, base::TimeDelta::FromHours(kRefreshPeriodHours), |
| 237 base::TimeDelta::FromMinutes(kDeviceSyncBaseRecoveryPeriodMinutes), | 240 base::TimeDelta::FromMinutes(kDeviceSyncBaseRecoveryPeriodMinutes), |
| 238 kDeviceSyncMaxJitterRatio, "CryptAuth DeviceSync")); | 241 kDeviceSyncMaxJitterRatio, "CryptAuth DeviceSync")); |
| 239 } | 242 } |
| 240 | 243 |
| 241 void CryptAuthDeviceManager::OnResyncMessage() { | 244 void CryptAuthDeviceManager::OnResyncMessage() { |
| 242 ForceSyncNow(cryptauth::INVOCATION_REASON_SERVER_INITIATED); | 245 ForceSyncNow(cryptauth::INVOCATION_REASON_SERVER_INITIATED); |
| 243 } | 246 } |
| 244 | 247 |
| 245 void CryptAuthDeviceManager::UpdateUnlockKeysFromPrefs() { | 248 void CryptAuthDeviceManager::UpdateUnlockKeysFromPrefs() { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 257 << "(index=" << i << "):\n" << *unlock_key_dictionary; | 260 << "(index=" << i << "):\n" << *unlock_key_dictionary; |
| 258 } | 261 } |
| 259 } else { | 262 } else { |
| 260 PA_LOG(ERROR) << "Can not get dictionary in list of unlock keys " | 263 PA_LOG(ERROR) << "Can not get dictionary in list of unlock keys " |
| 261 << "(index=" << i << "):\n" << *unlock_key_list; | 264 << "(index=" << i << "):\n" << *unlock_key_list; |
| 262 } | 265 } |
| 263 } | 266 } |
| 264 } | 267 } |
| 265 | 268 |
| 266 void CryptAuthDeviceManager::OnSyncRequested( | 269 void CryptAuthDeviceManager::OnSyncRequested( |
| 267 scoped_ptr<SyncScheduler::SyncRequest> sync_request) { | 270 std::unique_ptr<SyncScheduler::SyncRequest> sync_request) { |
| 268 FOR_EACH_OBSERVER(Observer, observers_, OnSyncStarted()); | 271 FOR_EACH_OBSERVER(Observer, observers_, OnSyncStarted()); |
| 269 | 272 |
| 270 sync_request_ = std::move(sync_request); | 273 sync_request_ = std::move(sync_request); |
| 271 cryptauth_client_ = client_factory_->CreateInstance(); | 274 cryptauth_client_ = client_factory_->CreateInstance(); |
| 272 | 275 |
| 273 cryptauth::InvocationReason invocation_reason = | 276 cryptauth::InvocationReason invocation_reason = |
| 274 cryptauth::INVOCATION_REASON_UNKNOWN; | 277 cryptauth::INVOCATION_REASON_UNKNOWN; |
| 275 | 278 |
| 276 int reason_stored_in_prefs = | 279 int reason_stored_in_prefs = |
| 277 pref_service_->GetInteger(prefs::kCryptAuthDeviceSyncReason); | 280 pref_service_->GetInteger(prefs::kCryptAuthDeviceSyncReason); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 298 request.set_invocation_reason(invocation_reason); | 301 request.set_invocation_reason(invocation_reason); |
| 299 request.set_allow_stale_read(is_sync_speculative); | 302 request.set_allow_stale_read(is_sync_speculative); |
| 300 cryptauth_client_->GetMyDevices( | 303 cryptauth_client_->GetMyDevices( |
| 301 request, base::Bind(&CryptAuthDeviceManager::OnGetMyDevicesSuccess, | 304 request, base::Bind(&CryptAuthDeviceManager::OnGetMyDevicesSuccess, |
| 302 weak_ptr_factory_.GetWeakPtr()), | 305 weak_ptr_factory_.GetWeakPtr()), |
| 303 base::Bind(&CryptAuthDeviceManager::OnGetMyDevicesFailure, | 306 base::Bind(&CryptAuthDeviceManager::OnGetMyDevicesFailure, |
| 304 weak_ptr_factory_.GetWeakPtr())); | 307 weak_ptr_factory_.GetWeakPtr())); |
| 305 } | 308 } |
| 306 | 309 |
| 307 } // namespace proximity_auth | 310 } // namespace proximity_auth |
| OLD | NEW |