proximity_auth_local_state_pref_manager.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/components/proximity_auth/proximity_auth_local_state_pref_manager.h"
  5. #include <memory>
  6. #include <vector>
  7. #include "ash/components/multidevice/logging/logging.h"
  8. #include "ash/components/proximity_auth/proximity_auth_pref_names.h"
  9. #include "ash/services/multidevice_setup/public/cpp/prefs.h"
  10. #include "base/logging.h"
  11. #include "base/values.h"
  12. #include "components/prefs/pref_registry_simple.h"
  13. #include "components/prefs/pref_service.h"
  14. #include "components/prefs/scoped_user_pref_update.h"
  15. namespace proximity_auth {
  16. ProximityAuthLocalStatePrefManager::ProximityAuthLocalStatePrefManager(
  17. PrefService* local_state)
  18. : local_state_(local_state) {}
  19. ProximityAuthLocalStatePrefManager::~ProximityAuthLocalStatePrefManager() {}
  20. // static.
  21. void ProximityAuthLocalStatePrefManager::RegisterPrefs(
  22. PrefRegistrySimple* registry) {
  23. // Prefs for all users are stored in a dictionary under this pref name.
  24. registry->RegisterDictionaryPref(prefs::kEasyUnlockLocalStateUserPrefs);
  25. // Most Smart Lock prefs are stored in regular user prefs, and then copied out
  26. // to local state for reference. This particular pref, in contrast, needs its
  27. // source of truth to be in the local state, because it needs to be written
  28. // to from the login screen.
  29. registry->RegisterDictionaryPref(
  30. prefs::kProximityAuthHasShownLoginDisabledMessage);
  31. }
  32. void ProximityAuthLocalStatePrefManager::SetIsEasyUnlockEnabled(
  33. bool is_easy_unlock_enabled) const {
  34. NOTREACHED();
  35. }
  36. void ProximityAuthLocalStatePrefManager::SetEasyUnlockEnabledStateSet() const {
  37. NOTREACHED();
  38. }
  39. void ProximityAuthLocalStatePrefManager::SetActiveUser(
  40. const AccountId& active_user) {
  41. active_user_ = active_user;
  42. }
  43. void ProximityAuthLocalStatePrefManager::SetLastPromotionCheckTimestampMs(
  44. int64_t timestamp_ms) {
  45. NOTREACHED();
  46. }
  47. int64_t ProximityAuthLocalStatePrefManager::GetLastPromotionCheckTimestampMs()
  48. const {
  49. NOTREACHED();
  50. return 0;
  51. }
  52. void ProximityAuthLocalStatePrefManager::SetPromotionShownCount(int count) {
  53. NOTREACHED();
  54. }
  55. int ProximityAuthLocalStatePrefManager::GetPromotionShownCount() const {
  56. NOTREACHED();
  57. return 0;
  58. }
  59. bool ProximityAuthLocalStatePrefManager::IsEasyUnlockAllowed() const {
  60. const base::Value::Dict* user_prefs = GetActiveUserPrefsDictionary();
  61. if (user_prefs) {
  62. absl::optional<bool> pref_value =
  63. user_prefs->FindBool(ash::multidevice_setup::kSmartLockAllowedPrefName);
  64. if (pref_value.has_value()) {
  65. return pref_value.value();
  66. }
  67. }
  68. PA_LOG(ERROR) << "Failed to get easyunlock_allowed.";
  69. return true;
  70. }
  71. bool ProximityAuthLocalStatePrefManager::IsEasyUnlockEnabled() const {
  72. const base::Value::Dict* user_prefs = GetActiveUserPrefsDictionary();
  73. if (user_prefs) {
  74. absl::optional<bool> pref_value =
  75. user_prefs->FindBool(ash::multidevice_setup::kSmartLockEnabledPrefName);
  76. if (pref_value.has_value()) {
  77. return pref_value.value();
  78. }
  79. }
  80. PA_LOG(ERROR) << "Failed to get easyunlock_enabled.";
  81. return false;
  82. }
  83. bool ProximityAuthLocalStatePrefManager::IsEasyUnlockEnabledStateSet() const {
  84. NOTREACHED();
  85. return false;
  86. }
  87. bool ProximityAuthLocalStatePrefManager::IsChromeOSLoginAllowed() const {
  88. const base::Value::Dict* user_prefs = GetActiveUserPrefsDictionary();
  89. if (user_prefs) {
  90. absl::optional<bool> pref_value = user_prefs->FindBool(
  91. ash::multidevice_setup::kSmartLockSigninAllowedPrefName);
  92. if (pref_value.has_value()) {
  93. return pref_value.value();
  94. }
  95. }
  96. PA_LOG(VERBOSE) << "Failed to get is_chrome_login_allowed, not disallowing";
  97. return true;
  98. }
  99. void ProximityAuthLocalStatePrefManager::SetIsChromeOSLoginEnabled(
  100. bool is_enabled) {
  101. NOTREACHED();
  102. }
  103. bool ProximityAuthLocalStatePrefManager::IsChromeOSLoginEnabled() const {
  104. const base::Value::Dict* user_prefs = GetActiveUserPrefsDictionary();
  105. if (user_prefs) {
  106. absl::optional<bool> pref_value =
  107. user_prefs->FindBool(prefs::kProximityAuthIsChromeOSLoginEnabled);
  108. if (pref_value.has_value()) {
  109. return pref_value.value();
  110. }
  111. }
  112. PA_LOG(ERROR) << "Failed to get is_chrome_login_enabled.";
  113. return false;
  114. }
  115. void ProximityAuthLocalStatePrefManager::SetHasShownLoginDisabledMessage(
  116. bool has_shown) {
  117. DictionaryPrefUpdate update(local_state_,
  118. prefs::kEasyUnlockLocalStateUserPrefs);
  119. base::Value* current_user_prefs =
  120. update.Get()->FindDictKey(active_user_.GetUserEmail());
  121. if (current_user_prefs) {
  122. current_user_prefs->SetBoolKey(
  123. prefs::kProximityAuthHasShownLoginDisabledMessage, has_shown);
  124. return;
  125. }
  126. // Create an otherwise empty dictionary in order to ensure |has_shown| is
  127. // persisted for |active_user_|.
  128. base::Value new_current_user_prefs(base::Value::Type::DICTIONARY);
  129. new_current_user_prefs.SetBoolKey(
  130. prefs::kProximityAuthHasShownLoginDisabledMessage, has_shown);
  131. update->SetKey(active_user_.GetUserEmail(),
  132. std::move(new_current_user_prefs));
  133. }
  134. bool ProximityAuthLocalStatePrefManager::HasShownLoginDisabledMessage() const {
  135. const base::Value::Dict* user_prefs = GetActiveUserPrefsDictionary();
  136. if (!user_prefs)
  137. return false;
  138. return user_prefs->FindBool(prefs::kProximityAuthHasShownLoginDisabledMessage)
  139. .value_or(false);
  140. }
  141. const base::Value::Dict*
  142. ProximityAuthLocalStatePrefManager::GetActiveUserPrefsDictionary() const {
  143. if (!active_user_.is_valid()) {
  144. PA_LOG(ERROR) << "No active account.";
  145. return nullptr;
  146. }
  147. const base::Value::Dict& all_user_prefs_dict =
  148. local_state_->GetValueDict(prefs::kEasyUnlockLocalStateUserPrefs);
  149. const base::Value::Dict* current_user_prefs =
  150. all_user_prefs_dict.FindDict(active_user_.GetUserEmail());
  151. if (!current_user_prefs) {
  152. PA_LOG(ERROR) << "Failed to find prefs for current user.";
  153. return nullptr;
  154. }
  155. return current_user_prefs;
  156. }
  157. } // namespace proximity_auth