1 // Copyright 2013 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 "chrome/browser/chromeos/policy/recommendation_restorer.h"
8 #include "base/bind_helpers.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/time/time.h"
13 #include "base/values.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/chromeos/profiles/profile_helper.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/common/pref_names.h"
18 #include "content/public/browser/notification_details.h"
19 #include "content/public/browser/notification_service.h"
20 #include "content/public/browser/notification_source.h"
21 #include "ui/base/user_activity/user_activity_detector.h"
26 // The amount of idle time after which recommended values are restored.
27 const int kRestoreDelayInMs
= 60 * 1000; // 1 minute.
30 RecommendationRestorer::RecommendationRestorer(Profile
* profile
)
32 if (!chromeos::ProfileHelper::IsSigninProfile(profile
))
35 pref_change_registrar_
.Init(profile
->GetPrefs());
36 pref_change_registrar_
.Add(
37 prefs::kAccessibilityLargeCursorEnabled
,
39 &RecommendationRestorer::Restore
, base::Unretained(this), true));
40 pref_change_registrar_
.Add(
41 prefs::kAccessibilitySpokenFeedbackEnabled
,
43 &RecommendationRestorer::Restore
, base::Unretained(this), true));
44 pref_change_registrar_
.Add(
45 prefs::kAccessibilityHighContrastEnabled
,
47 &RecommendationRestorer::Restore
, base::Unretained(this), true));
48 pref_change_registrar_
.Add(
49 prefs::kAccessibilityScreenMagnifierEnabled
,
51 &RecommendationRestorer::Restore
, base::Unretained(this), true));
52 pref_change_registrar_
.Add(
53 prefs::kAccessibilityScreenMagnifierType
,
55 &RecommendationRestorer::Restore
, base::Unretained(this), true));
56 pref_change_registrar_
.Add(
57 prefs::kAccessibilityVirtualKeyboardEnabled
,
59 &RecommendationRestorer::Restore
, base::Unretained(this), true));
61 notification_registrar_
.Add(this, chrome::NOTIFICATION_LOGIN_USER_CHANGED
,
62 content::NotificationService::AllSources());
67 RecommendationRestorer::~RecommendationRestorer() {
70 void RecommendationRestorer::Shutdown() {
72 pref_change_registrar_
.RemoveAll();
73 notification_registrar_
.RemoveAll();
76 void RecommendationRestorer::Observe(
78 const content::NotificationSource
& source
,
79 const content::NotificationDetails
& details
) {
80 if (type
== chrome::NOTIFICATION_LOGIN_USER_CHANGED
) {
82 notification_registrar_
.RemoveAll();
90 void RecommendationRestorer::OnUserActivity(const ui::Event
* event
) {
91 if (restore_timer_
.IsRunning())
92 restore_timer_
.Reset();
95 void RecommendationRestorer::Restore(bool allow_delay
,
96 const std::string
& pref_name
) {
97 const PrefService::Preference
* pref
=
98 pref_change_registrar_
.prefs()->FindPreference(pref_name
.c_str());
104 if (!pref
->GetRecommendedValue() || !pref
->HasUserSetting())
109 } else if (allow_delay
) {
110 // Skip the delay if there has been no user input since the browser started.
111 const ui::UserActivityDetector
* user_activity_detector
=
112 ui::UserActivityDetector::Get();
113 if (user_activity_detector
&&
114 user_activity_detector
->last_activity_time().is_null()) {
122 pref_change_registrar_
.prefs()->ClearPref(pref
->name().c_str());
125 void RecommendationRestorer::RestoreAll() {
126 Restore(false, prefs::kAccessibilityLargeCursorEnabled
);
127 Restore(false, prefs::kAccessibilitySpokenFeedbackEnabled
);
128 Restore(false, prefs::kAccessibilityHighContrastEnabled
);
129 Restore(false, prefs::kAccessibilityScreenMagnifierEnabled
);
130 Restore(false, prefs::kAccessibilityScreenMagnifierType
);
131 Restore(false, prefs::kAccessibilityVirtualKeyboardEnabled
);
134 void RecommendationRestorer::StartTimer() {
135 // Listen for user activity so that the timer can be reset while the user is
136 // active, causing it to fire only when the user remains idle for
137 // |kRestoreDelayInMs|.
138 ui::UserActivityDetector
* user_activity_detector
=
139 ui::UserActivityDetector::Get();
140 if (user_activity_detector
&& !user_activity_detector
->HasObserver(this))
141 user_activity_detector
->AddObserver(this);
143 // There should be a separate timer for each pref. However, in the common
144 // case of the user changing settings, a single timer is sufficient. This is
145 // because a change initiated by the user implies user activity, so that even
146 // if there was a separate timer per pref, they would all be reset at that
147 // point, causing them to fire at exactly the same time. In the much rarer
148 // case of a recommended value changing, a single timer is a close
149 // approximation of the behavior that would be obtained by resetting the timer
150 // for the affected pref only.
151 restore_timer_
.Start(FROM_HERE
,
152 base::TimeDelta::FromMilliseconds(kRestoreDelayInMs
),
153 base::Bind(&RecommendationRestorer::RestoreAll
,
154 base::Unretained(this)));
157 void RecommendationRestorer::StopTimer() {
158 restore_timer_
.Stop();
159 if (ui::UserActivityDetector::Get())
160 ui::UserActivityDetector::Get()->RemoveObserver(this);
163 } // namespace policy