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/cryptauth/cryptauth_device_manager.h" | 5 #include "components/cryptauth/cryptauth_device_manager.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdexcept> | 8 #include <stdexcept> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 bool CryptAuthDeviceManager::IsSyncInProgress() const { | 360 bool CryptAuthDeviceManager::IsSyncInProgress() const { |
361 return scheduler_->GetSyncState() == | 361 return scheduler_->GetSyncState() == |
362 SyncScheduler::SyncState::SYNC_IN_PROGRESS; | 362 SyncScheduler::SyncState::SYNC_IN_PROGRESS; |
363 } | 363 } |
364 | 364 |
365 bool CryptAuthDeviceManager::IsRecoveringFromFailure() const { | 365 bool CryptAuthDeviceManager::IsRecoveringFromFailure() const { |
366 return scheduler_->GetStrategy() == | 366 return scheduler_->GetStrategy() == |
367 SyncScheduler::Strategy::AGGRESSIVE_RECOVERY; | 367 SyncScheduler::Strategy::AGGRESSIVE_RECOVERY; |
368 } | 368 } |
369 | 369 |
| 370 std::vector<ExternalDeviceInfo> CryptAuthDeviceManager::synced_devices() const { |
| 371 return synced_devices_; |
| 372 } |
| 373 |
| 374 std::vector<ExternalDeviceInfo> CryptAuthDeviceManager::UnlockKeys() const { |
| 375 std::vector<ExternalDeviceInfo> unlock_keys; |
| 376 for (const auto& device : synced_devices_) { |
| 377 if (device.unlock_key()) { |
| 378 unlock_keys.push_back(device); |
| 379 } |
| 380 } |
| 381 return unlock_keys; |
| 382 } |
| 383 |
| 384 std::vector<ExternalDeviceInfo> CryptAuthDeviceManager::TetherHosts() const { |
| 385 std::vector<ExternalDeviceInfo> tether_hosts; |
| 386 for (const auto& device : synced_devices_) { |
| 387 if (device.mobile_hotspot_supported()) { |
| 388 tether_hosts.push_back(device); |
| 389 } |
| 390 } |
| 391 return tether_hosts; |
| 392 } |
| 393 |
370 void CryptAuthDeviceManager::OnGetMyDevicesSuccess( | 394 void CryptAuthDeviceManager::OnGetMyDevicesSuccess( |
371 const cryptauth::GetMyDevicesResponse& response) { | 395 const cryptauth::GetMyDevicesResponse& response) { |
372 // Update the unlock keys stored in the user's prefs. | 396 // Update the synced devices stored in the user's prefs. |
373 std::unique_ptr<base::ListValue> unlock_keys_pref(new base::ListValue()); | |
374 std::unique_ptr<base::ListValue> devices_as_list(new base::ListValue()); | 397 std::unique_ptr<base::ListValue> devices_as_list(new base::ListValue()); |
375 for (const auto& device : response.devices()) { | 398 for (const auto& device : response.devices()) { |
376 devices_as_list->Append(UnlockKeyToDictionary(device)); | 399 devices_as_list->Append(UnlockKeyToDictionary(device)); |
377 if (device.unlock_key()) | |
378 unlock_keys_pref->Append(UnlockKeyToDictionary(device)); | |
379 } | 400 } |
380 PA_LOG(INFO) << "Devices Synced:\n" << *devices_as_list; | 401 PA_LOG(INFO) << "Devices Synced:\n" << *devices_as_list; |
381 | 402 |
382 bool unlock_keys_changed = !unlock_keys_pref->Equals( | 403 bool unlock_keys_changed = !devices_as_list->Equals( |
383 pref_service_->GetList(prefs::kCryptAuthDeviceSyncUnlockKeys)); | 404 pref_service_->GetList(prefs::kCryptAuthDeviceSyncUnlockKeys)); |
384 { | 405 { |
385 ListPrefUpdate update(pref_service_, prefs::kCryptAuthDeviceSyncUnlockKeys); | 406 ListPrefUpdate update(pref_service_, prefs::kCryptAuthDeviceSyncUnlockKeys); |
386 update.Get()->Swap(unlock_keys_pref.get()); | 407 update.Get()->Swap(devices_as_list.get()); |
387 } | 408 } |
388 UpdateUnlockKeysFromPrefs(); | 409 UpdateUnlockKeysFromPrefs(); |
389 | 410 |
390 // Reset metadata used for scheduling syncing. | 411 // Reset metadata used for scheduling syncing. |
391 pref_service_->SetBoolean(prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure, | 412 pref_service_->SetBoolean(prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure, |
392 false); | 413 false); |
393 pref_service_->SetDouble(prefs::kCryptAuthDeviceSyncLastSyncTimeSeconds, | 414 pref_service_->SetDouble(prefs::kCryptAuthDeviceSyncLastSyncTimeSeconds, |
394 clock_->Now().ToDoubleT()); | 415 clock_->Now().ToDoubleT()); |
395 pref_service_->SetInteger(prefs::kCryptAuthDeviceSyncReason, | 416 pref_service_->SetInteger(prefs::kCryptAuthDeviceSyncReason, |
396 cryptauth::INVOCATION_REASON_UNKNOWN); | 417 cryptauth::INVOCATION_REASON_UNKNOWN); |
(...skipping 27 matching lines...) Expand all Loading... |
424 kDeviceSyncMaxJitterRatio, "CryptAuth DeviceSync"); | 445 kDeviceSyncMaxJitterRatio, "CryptAuth DeviceSync"); |
425 } | 446 } |
426 | 447 |
427 void CryptAuthDeviceManager::OnResyncMessage() { | 448 void CryptAuthDeviceManager::OnResyncMessage() { |
428 ForceSyncNow(cryptauth::INVOCATION_REASON_SERVER_INITIATED); | 449 ForceSyncNow(cryptauth::INVOCATION_REASON_SERVER_INITIATED); |
429 } | 450 } |
430 | 451 |
431 void CryptAuthDeviceManager::UpdateUnlockKeysFromPrefs() { | 452 void CryptAuthDeviceManager::UpdateUnlockKeysFromPrefs() { |
432 const base::ListValue* unlock_key_list = | 453 const base::ListValue* unlock_key_list = |
433 pref_service_->GetList(prefs::kCryptAuthDeviceSyncUnlockKeys); | 454 pref_service_->GetList(prefs::kCryptAuthDeviceSyncUnlockKeys); |
434 unlock_keys_.clear(); | 455 synced_devices_.clear(); |
435 for (size_t i = 0; i < unlock_key_list->GetSize(); ++i) { | 456 for (size_t i = 0; i < unlock_key_list->GetSize(); ++i) { |
436 const base::DictionaryValue* unlock_key_dictionary; | 457 const base::DictionaryValue* unlock_key_dictionary; |
437 if (unlock_key_list->GetDictionary(i, &unlock_key_dictionary)) { | 458 if (unlock_key_list->GetDictionary(i, &unlock_key_dictionary)) { |
438 cryptauth::ExternalDeviceInfo unlock_key; | 459 cryptauth::ExternalDeviceInfo unlock_key; |
439 if (DictionaryToUnlockKey(*unlock_key_dictionary, &unlock_key)) { | 460 if (DictionaryToUnlockKey(*unlock_key_dictionary, &unlock_key)) { |
440 unlock_keys_.push_back(unlock_key); | 461 synced_devices_.push_back(unlock_key); |
441 } else { | 462 } else { |
442 PA_LOG(ERROR) << "Unable to deserialize unlock key dictionary " | 463 PA_LOG(ERROR) << "Unable to deserialize unlock key dictionary " |
443 << "(index=" << i << "):\n" << *unlock_key_dictionary; | 464 << "(index=" << i << "):\n" << *unlock_key_dictionary; |
444 } | 465 } |
445 } else { | 466 } else { |
446 PA_LOG(ERROR) << "Can not get dictionary in list of unlock keys " | 467 PA_LOG(ERROR) << "Can not get dictionary in list of unlock keys " |
447 << "(index=" << i << "):\n" << *unlock_key_list; | 468 << "(index=" << i << "):\n" << *unlock_key_list; |
448 } | 469 } |
449 } | 470 } |
450 } | 471 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 request.set_invocation_reason(invocation_reason); | 506 request.set_invocation_reason(invocation_reason); |
486 request.set_allow_stale_read(is_sync_speculative); | 507 request.set_allow_stale_read(is_sync_speculative); |
487 cryptauth_client_->GetMyDevices( | 508 cryptauth_client_->GetMyDevices( |
488 request, base::Bind(&CryptAuthDeviceManager::OnGetMyDevicesSuccess, | 509 request, base::Bind(&CryptAuthDeviceManager::OnGetMyDevicesSuccess, |
489 weak_ptr_factory_.GetWeakPtr()), | 510 weak_ptr_factory_.GetWeakPtr()), |
490 base::Bind(&CryptAuthDeviceManager::OnGetMyDevicesFailure, | 511 base::Bind(&CryptAuthDeviceManager::OnGetMyDevicesFailure, |
491 weak_ptr_factory_.GetWeakPtr())); | 512 weak_ptr_factory_.GetWeakPtr())); |
492 } | 513 } |
493 | 514 |
494 } // namespace cryptauth | 515 } // namespace cryptauth |
OLD | NEW |