Upload upstream chromium 69.0.3497
[platform/framework/web/chromium-efl.git] / ash / policy / policy_recommendation_restorer.cc
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
5 #include "ash/policy/policy_recommendation_restorer.h"
6
7 #include "ash/session/session_controller.h"
8 #include "ash/shell.h"
9 #include "base/bind.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"
15
16 namespace ash {
17
18 namespace {
19
20 // The amount of idle time after which recommended values are restored.
21 constexpr base::TimeDelta kRestoreDelayInMinutes =
22     base::TimeDelta::FromMinutes(1);
23
24 }  // namespace
25
26 PolicyRecommendationRestorer::PolicyRecommendationRestorer() {
27   Shell::Get()->session_controller()->AddObserver(this);
28 }
29
30 PolicyRecommendationRestorer::~PolicyRecommendationRestorer() {
31   StopTimer();
32   Shell::Get()->session_controller()->RemoveObserver(this);
33 }
34
35 void PolicyRecommendationRestorer::ObservePref(const std::string& pref_name) {
36   PrefService* prefs =
37       Shell::Get()->session_controller()->GetSigninScreenPrefService();
38   DCHECK(prefs);
39   DCHECK(!base::ContainsKey(pref_names_, pref_name));
40
41   if (!pref_change_registrar_) {
42     pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>();
43     pref_change_registrar_->Init(prefs);
44   }
45
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);
51 }
52
53 void PolicyRecommendationRestorer::OnActiveUserPrefServiceChanged(
54     PrefService* pref_service) {
55   active_user_pref_connected_ = true;
56   StopTimer();
57   RestoreAll();
58 }
59
60 void PolicyRecommendationRestorer::OnUserActivity(const ui::Event* event) {
61   if (restore_timer_.IsRunning())
62     restore_timer_.Reset();
63 }
64
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);
69   if (!pref) {
70     NOTREACHED();
71     return;
72   }
73
74   if (!pref->GetRecommendedValue() || !pref->HasUserSetting())
75     return;
76
77   if (active_user_pref_connected_) {
78     allow_delay = false;
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()) {
86       allow_delay = false;
87     }
88   }
89
90   if (allow_delay)
91     StartTimer();
92   else
93     pref_change_registrar_->prefs()->ClearPref(pref->name());
94 }
95
96 void PolicyRecommendationRestorer::RestoreAll() {
97   for (const auto& pref_name : pref_names_)
98     Restore(false, pref_name);
99 }
100
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);
109
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)));
122 }
123
124 void PolicyRecommendationRestorer::StopTimer() {
125   restore_timer_.Stop();
126   if (ui::UserActivityDetector::Get())
127     ui::UserActivityDetector::Get()->RemoveObserver(this);
128 }
129
130 }  // namespace ash