1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/policy/policy_recommendation_restorer.h"
7 #include "ash/session/session_controller_impl.h"
9 #include "base/check.h"
10 #include "base/containers/contains.h"
11 #include "base/functional/bind.h"
12 #include "base/notreached.h"
13 #include "components/prefs/pref_change_registrar.h"
14 #include "components/prefs/pref_service.h"
15 #include "ui/base/user_activity/user_activity_detector.h"
21 // The amount of idle time after which recommended values are restored.
22 constexpr base::TimeDelta kRestoreDelayInMinutes = base::Minutes(1);
26 PolicyRecommendationRestorer::PolicyRecommendationRestorer() {
27 Shell::Get()->session_controller()->AddObserver(this);
30 PolicyRecommendationRestorer::~PolicyRecommendationRestorer() {
32 Shell::Get()->session_controller()->RemoveObserver(this);
35 void PolicyRecommendationRestorer::ObservePref(const std::string& pref_name) {
37 Shell::Get()->session_controller()->GetSigninScreenPrefService();
39 DCHECK(!base::Contains(pref_names_, pref_name));
41 if (!pref_change_registrar_) {
42 pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>();
43 pref_change_registrar_->Init(prefs);
46 pref_change_registrar_->Add(
47 pref_name, base::BindRepeating(&PolicyRecommendationRestorer::Restore,
48 base::Unretained(this), true));
49 pref_names_.insert(pref_name);
50 Restore(false /* allow_delay */, pref_name);
53 void PolicyRecommendationRestorer::OnActiveUserPrefServiceChanged(
54 PrefService* pref_service) {
55 active_user_pref_connected_ = true;
60 void PolicyRecommendationRestorer::OnUserActivity(const ui::Event* event) {
61 if (restore_timer_.IsRunning())
62 restore_timer_.Reset();
65 void PolicyRecommendationRestorer::DisableForTesting() {
66 disabled_for_testing_ = true;
69 void PolicyRecommendationRestorer::Restore(bool allow_delay,
70 const std::string& pref_name) {
71 const PrefService::Preference* pref =
72 pref_change_registrar_->prefs()->FindPreference(pref_name);
78 if (!pref->GetRecommendedValue() || !pref->HasUserSetting())
81 if (active_user_pref_connected_) {
83 } else if (allow_delay) {
84 // Skip the delay if there has been no user input since |pref_name| is
85 // started observing recommended value.
86 const ui::UserActivityDetector* user_activity_detector =
87 ui::UserActivityDetector::Get();
88 if (user_activity_detector &&
89 user_activity_detector->last_activity_time().is_null()) {
96 else if (!disabled_for_testing_)
97 pref_change_registrar_->prefs()->ClearPref(pref->name());
100 void PolicyRecommendationRestorer::RestoreAll() {
101 for (const auto& pref_name : pref_names_)
102 Restore(false, pref_name);
105 void PolicyRecommendationRestorer::StartTimer() {
106 // Listen for user activity so that the timer can be reset while the user is
107 // active, causing it to fire only when the user remains idle for
108 // |kRestoreDelayInMinutes|.
109 ui::UserActivityDetector* user_activity_detector =
110 ui::UserActivityDetector::Get();
111 if (user_activity_detector && !user_activity_detector->HasObserver(this))
112 user_activity_detector->AddObserver(this);
114 // There should be a separate timer for each pref. However, in the common
115 // case of the user changing settings, a single timer is sufficient. This is
116 // because a change initiated by the user implies user activity, so that even
117 // if there was a separate timer per pref, they would all be reset at that
118 // point, causing them to fire at exactly the same time. In the much rarer
119 // case of a recommended value changing, a single timer is a close
120 // approximation of the behavior that would be obtained by resetting the timer
121 // for the affected pref only.
122 restore_timer_.Start(FROM_HERE, kRestoreDelayInMinutes,
123 base::BindOnce(&PolicyRecommendationRestorer::RestoreAll,
124 base::Unretained(this)));
127 void PolicyRecommendationRestorer::StopTimer() {
128 restore_timer_.Stop();
129 if (ui::UserActivityDetector::Get())
130 ui::UserActivityDetector::Get()->RemoveObserver(this);