policy_recommendation_restorer.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2018 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/policy/policy_recommendation_restorer.h"
  5. #include "ash/session/session_controller_impl.h"
  6. #include "ash/shell.h"
  7. #include "base/bind.h"
  8. #include "base/check.h"
  9. #include "base/containers/contains.h"
  10. #include "base/notreached.h"
  11. #include "components/prefs/pref_change_registrar.h"
  12. #include "components/prefs/pref_service.h"
  13. #include "ui/base/user_activity/user_activity_detector.h"
  14. namespace ash {
  15. namespace {
  16. // The amount of idle time after which recommended values are restored.
  17. constexpr base::TimeDelta kRestoreDelayInMinutes = base::Minutes(1);
  18. } // namespace
  19. PolicyRecommendationRestorer::PolicyRecommendationRestorer() {
  20. Shell::Get()->session_controller()->AddObserver(this);
  21. }
  22. PolicyRecommendationRestorer::~PolicyRecommendationRestorer() {
  23. StopTimer();
  24. Shell::Get()->session_controller()->RemoveObserver(this);
  25. }
  26. void PolicyRecommendationRestorer::ObservePref(const std::string& pref_name) {
  27. PrefService* prefs =
  28. Shell::Get()->session_controller()->GetSigninScreenPrefService();
  29. DCHECK(prefs);
  30. DCHECK(!base::Contains(pref_names_, pref_name));
  31. if (!pref_change_registrar_) {
  32. pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>();
  33. pref_change_registrar_->Init(prefs);
  34. }
  35. pref_change_registrar_->Add(
  36. pref_name, base::BindRepeating(&PolicyRecommendationRestorer::Restore,
  37. base::Unretained(this), true));
  38. pref_names_.insert(pref_name);
  39. Restore(false /* allow_delay */, pref_name);
  40. }
  41. void PolicyRecommendationRestorer::OnActiveUserPrefServiceChanged(
  42. PrefService* pref_service) {
  43. active_user_pref_connected_ = true;
  44. StopTimer();
  45. RestoreAll();
  46. }
  47. void PolicyRecommendationRestorer::OnUserActivity(const ui::Event* event) {
  48. if (restore_timer_.IsRunning())
  49. restore_timer_.Reset();
  50. }
  51. void PolicyRecommendationRestorer::DisableForTesting() {
  52. disabled_for_testing_ = true;
  53. }
  54. void PolicyRecommendationRestorer::Restore(bool allow_delay,
  55. const std::string& pref_name) {
  56. const PrefService::Preference* pref =
  57. pref_change_registrar_->prefs()->FindPreference(pref_name);
  58. if (!pref) {
  59. NOTREACHED();
  60. return;
  61. }
  62. if (!pref->GetRecommendedValue() || !pref->HasUserSetting())
  63. return;
  64. if (active_user_pref_connected_) {
  65. allow_delay = false;
  66. } else if (allow_delay) {
  67. // Skip the delay if there has been no user input since |pref_name| is
  68. // started observing recommended value.
  69. const ui::UserActivityDetector* user_activity_detector =
  70. ui::UserActivityDetector::Get();
  71. if (user_activity_detector &&
  72. user_activity_detector->last_activity_time().is_null()) {
  73. allow_delay = false;
  74. }
  75. }
  76. if (allow_delay)
  77. StartTimer();
  78. else if (!disabled_for_testing_)
  79. pref_change_registrar_->prefs()->ClearPref(pref->name());
  80. }
  81. void PolicyRecommendationRestorer::RestoreAll() {
  82. for (const auto& pref_name : pref_names_)
  83. Restore(false, pref_name);
  84. }
  85. void PolicyRecommendationRestorer::StartTimer() {
  86. // Listen for user activity so that the timer can be reset while the user is
  87. // active, causing it to fire only when the user remains idle for
  88. // |kRestoreDelayInMinutes|.
  89. ui::UserActivityDetector* user_activity_detector =
  90. ui::UserActivityDetector::Get();
  91. if (user_activity_detector && !user_activity_detector->HasObserver(this))
  92. user_activity_detector->AddObserver(this);
  93. // There should be a separate timer for each pref. However, in the common
  94. // case of the user changing settings, a single timer is sufficient. This is
  95. // because a change initiated by the user implies user activity, so that even
  96. // if there was a separate timer per pref, they would all be reset at that
  97. // point, causing them to fire at exactly the same time. In the much rarer
  98. // case of a recommended value changing, a single timer is a close
  99. // approximation of the behavior that would be obtained by resetting the timer
  100. // for the affected pref only.
  101. restore_timer_.Start(FROM_HERE, kRestoreDelayInMinutes,
  102. base::BindOnce(&PolicyRecommendationRestorer::RestoreAll,
  103. base::Unretained(this)));
  104. }
  105. void PolicyRecommendationRestorer::StopTimer() {
  106. restore_timer_.Stop();
  107. if (ui::UserActivityDetector::Get())
  108. ui::UserActivityDetector::Get()->RemoveObserver(this);
  109. }
  110. } // namespace ash