Revert "[M120 Migration]Fix for crash during chrome exit"
[platform/framework/web/chromium-efl.git] / ash / policy / policy_recommendation_restorer.cc
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.
4
5 #include "ash/policy/policy_recommendation_restorer.h"
6
7 #include "ash/session/session_controller_impl.h"
8 #include "ash/shell.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"
16
17 namespace ash {
18
19 namespace {
20
21 // The amount of idle time after which recommended values are restored.
22 constexpr base::TimeDelta kRestoreDelayInMinutes = base::Minutes(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::Contains(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::DisableForTesting() {
66   disabled_for_testing_ = true;
67 }
68
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);
73   if (!pref) {
74     NOTREACHED();
75     return;
76   }
77
78   if (!pref->GetRecommendedValue() || !pref->HasUserSetting())
79     return;
80
81   if (active_user_pref_connected_) {
82     allow_delay = false;
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()) {
90       allow_delay = false;
91     }
92   }
93
94   if (allow_delay)
95     StartTimer();
96   else if (!disabled_for_testing_)
97     pref_change_registrar_->prefs()->ClearPref(pref->name());
98 }
99
100 void PolicyRecommendationRestorer::RestoreAll() {
101   for (const auto& pref_name : pref_names_)
102     Restore(false, pref_name);
103 }
104
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);
113
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)));
125 }
126
127 void PolicyRecommendationRestorer::StopTimer() {
128   restore_timer_.Stop();
129   if (ui::UserActivityDetector::Get())
130     ui::UserActivityDetector::Get()->RemoveObserver(this);
131 }
132
133 }  // namespace ash