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.
5 #include "ash/policy/policy_recommendation_restorer.h"
7 #include "ash/session/session_controller.h"
10 #include "base/logging.h"
11 #include "base/stl_util.h"
12 #include "components/prefs/pref_change_registrar.h"
13 #include "components/prefs/pref_service.h"
14 #include "ui/base/user_activity/user_activity_detector.h"
20 // The amount of idle time after which recommended values are restored.
21 constexpr base::TimeDelta kRestoreDelayInMinutes =
22 base::TimeDelta::FromMinutes(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::ContainsKey(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::Restore(bool allow_delay,
66 const std::string& pref_name) {
67 const PrefService::Preference* pref =
68 pref_change_registrar_->prefs()->FindPreference(pref_name);
74 if (!pref->GetRecommendedValue() || !pref->HasUserSetting())
77 if (active_user_pref_connected_) {
79 } else if (allow_delay) {
80 // Skip the delay if there has been no user input since |pref_name| is
81 // started observing recommended value.
82 const ui::UserActivityDetector* user_activity_detector =
83 ui::UserActivityDetector::Get();
84 if (user_activity_detector &&
85 user_activity_detector->last_activity_time().is_null()) {
93 pref_change_registrar_->prefs()->ClearPref(pref->name());
96 void PolicyRecommendationRestorer::RestoreAll() {
97 for (const auto& pref_name : pref_names_)
98 Restore(false, pref_name);
101 void PolicyRecommendationRestorer::StartTimer() {
102 // Listen for user activity so that the timer can be reset while the user is
103 // active, causing it to fire only when the user remains idle for
104 // |kRestoreDelayInMinutes|.
105 ui::UserActivityDetector* user_activity_detector =
106 ui::UserActivityDetector::Get();
107 if (user_activity_detector && !user_activity_detector->HasObserver(this))
108 user_activity_detector->AddObserver(this);
110 // There should be a separate timer for each pref. However, in the common
111 // case of the user changing settings, a single timer is sufficient. This is
112 // because a change initiated by the user implies user activity, so that even
113 // if there was a separate timer per pref, they would all be reset at that
114 // point, causing them to fire at exactly the same time. In the much rarer
115 // case of a recommended value changing, a single timer is a close
116 // approximation of the behavior that would be obtained by resetting the timer
117 // for the affected pref only.
118 restore_timer_.Start(
119 FROM_HERE, kRestoreDelayInMinutes,
120 base::BindRepeating(&PolicyRecommendationRestorer::RestoreAll,
121 base::Unretained(this)));
124 void PolicyRecommendationRestorer::StopTimer() {
125 restore_timer_.Stop();
126 if (ui::UserActivityDetector::Get())
127 ui::UserActivityDetector::Get()->RemoveObserver(this);