OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/policy/recommendation_restorer.h" | 5 #include "chrome/browser/chromeos/policy/recommendation_restorer.h" |
6 | 6 |
7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
8 #include "ash/wm/user_activity_detector.h" | 8 #include "ash/wm/user_activity_detector.h" |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/browser/browser_process.h" |
16 #include "chrome/browser/chrome_notification_types.h" | 17 #include "chrome/browser/chrome_notification_types.h" |
17 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 18 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
18 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
20 #include "content/public/browser/notification_details.h" | 21 #include "content/public/browser/notification_details.h" |
21 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
22 #include "content/public/browser/notification_source.h" | 23 #include "content/public/browser/notification_source.h" |
23 | 24 |
24 namespace policy { | 25 namespace policy { |
25 | 26 |
26 namespace { | 27 namespace { |
27 // The amount of idle time after which recommended values are restored. | 28 // The amount of idle time after which recommended values are restored. |
28 const int kRestoreDelayInMs = 60 * 1000; // 1 minute. | 29 const int kRestoreDelayInMs = 60 * 1000; // 1 minute. |
29 } // namespace | 30 } // namespace |
30 | 31 |
31 RecommendationRestorer::RecommendationRestorer(Profile* profile) | 32 RecommendationRestorer::RecommendationRestorer(Profile* profile) |
32 : logged_in_(false) { | 33 : logged_in_(false) { |
33 if (!chromeos::ProfileHelper::IsSigninProfile(profile)) | 34 if (!chromeos::ProfileHelper::IsSigninProfile(profile)) |
34 return; | 35 return; |
35 | 36 |
| 37 global_pref_change_registrar_.Init(g_browser_process->local_state()); |
| 38 global_pref_change_registrar_.Add( |
| 39 prefs::kSpokenFeedbackEnabled, |
| 40 base::Bind(&RecommendationRestorer::RestoreGlobalPref, |
| 41 base::Unretained(this), true)); |
| 42 |
36 pref_change_registrar_.Init(profile->GetPrefs()); | 43 pref_change_registrar_.Init(profile->GetPrefs()); |
37 pref_change_registrar_.Add(prefs::kLargeCursorEnabled, | 44 pref_change_registrar_.Add( |
38 base::Bind(&RecommendationRestorer::Restore, | 45 prefs::kLargeCursorEnabled, |
39 base::Unretained(this), true)); | 46 base::Bind(&RecommendationRestorer::RestoreUserPref, |
40 pref_change_registrar_.Add(prefs::kSpokenFeedbackEnabled, | 47 base::Unretained(this), true)); |
41 base::Bind(&RecommendationRestorer::Restore, | 48 pref_change_registrar_.Add( |
42 base::Unretained(this), true)); | 49 prefs::kHighContrastEnabled, |
43 pref_change_registrar_.Add(prefs::kHighContrastEnabled, | 50 base::Bind(&RecommendationRestorer::RestoreUserPref, |
44 base::Bind(&RecommendationRestorer::Restore, | 51 base::Unretained(this), true)); |
45 base::Unretained(this), true)); | |
46 pref_change_registrar_.Add(prefs::kScreenMagnifierEnabled, | 52 pref_change_registrar_.Add(prefs::kScreenMagnifierEnabled, |
47 base::Bind(&RecommendationRestorer::Restore, | 53 base::Bind(&RecommendationRestorer::RestoreUserPref, |
48 base::Unretained(this), true)); | 54 base::Unretained(this), true)); |
49 pref_change_registrar_.Add(prefs::kScreenMagnifierType, | 55 pref_change_registrar_.Add(prefs::kScreenMagnifierType, |
50 base::Bind(&RecommendationRestorer::Restore, | 56 base::Bind(&RecommendationRestorer::RestoreUserPref, |
51 base::Unretained(this), true)); | 57 base::Unretained(this), true)); |
52 | 58 |
53 notification_registrar_.Add(this, chrome::NOTIFICATION_LOGIN_USER_CHANGED, | 59 notification_registrar_.Add(this, chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
54 content::NotificationService::AllSources()); | 60 content::NotificationService::AllSources()); |
55 | 61 |
56 RestoreAll(); | 62 RestoreAll(); |
57 } | 63 } |
58 | 64 |
59 RecommendationRestorer::~RecommendationRestorer() { | 65 RecommendationRestorer::~RecommendationRestorer() { |
60 } | 66 } |
61 | 67 |
62 void RecommendationRestorer::Shutdown() { | 68 void RecommendationRestorer::Shutdown() { |
63 StopTimer(); | 69 StopTimer(); |
64 pref_change_registrar_.RemoveAll(); | 70 pref_change_registrar_.RemoveAll(); |
| 71 global_pref_change_registrar_.RemoveAll(); |
65 notification_registrar_.RemoveAll(); | 72 notification_registrar_.RemoveAll(); |
66 } | 73 } |
67 | 74 |
68 void RecommendationRestorer::Observe( | 75 void RecommendationRestorer::Observe( |
69 int type, | 76 int type, |
70 const content::NotificationSource& source, | 77 const content::NotificationSource& source, |
71 const content::NotificationDetails& details) { | 78 const content::NotificationDetails& details) { |
72 if (type == chrome::NOTIFICATION_LOGIN_USER_CHANGED) { | 79 if (type == chrome::NOTIFICATION_LOGIN_USER_CHANGED) { |
73 logged_in_ = true; | 80 logged_in_ = true; |
74 notification_registrar_.RemoveAll(); | 81 notification_registrar_.RemoveAll(); |
75 StopTimer(); | 82 StopTimer(); |
76 RestoreAll(); | 83 RestoreAll(); |
77 } else { | 84 } else { |
78 NOTREACHED(); | 85 NOTREACHED(); |
79 } | 86 } |
80 } | 87 } |
81 | 88 |
82 void RecommendationRestorer::OnUserActivity(const ui::Event* event) { | 89 void RecommendationRestorer::OnUserActivity(const ui::Event* event) { |
83 if (restore_timer_.IsRunning()) | 90 if (restore_timer_.IsRunning()) |
84 restore_timer_.Reset(); | 91 restore_timer_.Reset(); |
85 } | 92 } |
86 | 93 |
87 void RecommendationRestorer::Restore(bool allow_delay, | 94 void RecommendationRestorer::RestoreUserPref(bool allow_delay, |
88 const std::string& pref_name) { | 95 const std::string& pref_name) { |
| 96 RestorePrefInternal(allow_delay, pref_change_registrar_.prefs(), pref_name); |
| 97 } |
| 98 |
| 99 void RecommendationRestorer::RestoreGlobalPref(bool allow_delay, |
| 100 const std::string& pref_name) { |
| 101 RestorePrefInternal(allow_delay, |
| 102 global_pref_change_registrar_.prefs(), |
| 103 pref_name); |
| 104 } |
| 105 |
| 106 void RecommendationRestorer::RestorePrefInternal( |
| 107 bool allow_delay, |
| 108 PrefService* prefs, |
| 109 const std::string& pref_name) { |
89 const PrefService::Preference* pref = | 110 const PrefService::Preference* pref = |
90 pref_change_registrar_.prefs()->FindPreference(pref_name.c_str()); | 111 prefs->FindPreference(pref_name.c_str()); |
91 if (!pref) { | 112 if (!pref) { |
92 NOTREACHED(); | 113 NOTREACHED(); |
93 return; | 114 return; |
94 } | 115 } |
95 | 116 |
96 if (!pref->GetRecommendedValue() || !pref->HasUserSetting()) | 117 if (!pref->GetRecommendedValue() || !pref->HasUserSetting()) |
97 return; | 118 return; |
98 | 119 |
99 if (logged_in_) { | 120 if (logged_in_) { |
100 allow_delay = false; | 121 allow_delay = false; |
101 } else if (allow_delay && ash::Shell::HasInstance()) { | 122 } else if (allow_delay && ash::Shell::HasInstance()) { |
102 // Skip the delay if there has been no user input since the browser started. | 123 // Skip the delay if there has been no user input since the browser started. |
103 const ash::UserActivityDetector* user_activity_detector = | 124 const ash::UserActivityDetector* user_activity_detector = |
104 ash::Shell::GetInstance()->user_activity_detector(); | 125 ash::Shell::GetInstance()->user_activity_detector(); |
105 allow_delay = !user_activity_detector->last_activity_time().is_null(); | 126 allow_delay = !user_activity_detector->last_activity_time().is_null(); |
106 } | 127 } |
107 | 128 |
108 if (allow_delay) | 129 if (allow_delay) |
109 StartTimer(); | 130 StartTimer(); |
110 else | 131 else |
111 pref_change_registrar_.prefs()->ClearPref(pref->name().c_str()); | 132 prefs->ClearPref(pref->name().c_str()); |
112 } | 133 } |
113 | 134 |
114 void RecommendationRestorer::RestoreAll() { | 135 void RecommendationRestorer::RestoreAll() { |
115 Restore(false, prefs::kLargeCursorEnabled); | 136 RestoreGlobalPref(false, prefs::kSpokenFeedbackEnabled); |
116 Restore(false, prefs::kSpokenFeedbackEnabled); | 137 RestoreUserPref(false, prefs::kLargeCursorEnabled); |
117 Restore(false, prefs::kHighContrastEnabled); | 138 RestoreUserPref(false, prefs::kHighContrastEnabled); |
118 Restore(false, prefs::kScreenMagnifierEnabled); | 139 RestoreUserPref(false, prefs::kScreenMagnifierEnabled); |
119 Restore(false, prefs::kScreenMagnifierType); | 140 RestoreUserPref(false, prefs::kScreenMagnifierType); |
120 } | 141 } |
121 | 142 |
122 void RecommendationRestorer::StartTimer() { | 143 void RecommendationRestorer::StartTimer() { |
123 // Listen for user activity so that the timer can be reset while the user is | 144 // Listen for user activity so that the timer can be reset while the user is |
124 // active, causing it to fire only when the user remains idle for | 145 // active, causing it to fire only when the user remains idle for |
125 // |kRestoreDelayInMs|. | 146 // |kRestoreDelayInMs|. |
126 if (ash::Shell::HasInstance()) { | 147 if (ash::Shell::HasInstance()) { |
127 ash::UserActivityDetector* user_activity_detector = | 148 ash::UserActivityDetector* user_activity_detector = |
128 ash::Shell::GetInstance()->user_activity_detector(); | 149 ash::Shell::GetInstance()->user_activity_detector(); |
129 if (!user_activity_detector->HasObserver(this)) | 150 if (!user_activity_detector->HasObserver(this)) |
(...skipping 14 matching lines...) Expand all Loading... |
144 base::Unretained(this))); | 165 base::Unretained(this))); |
145 } | 166 } |
146 | 167 |
147 void RecommendationRestorer::StopTimer() { | 168 void RecommendationRestorer::StopTimer() { |
148 restore_timer_.Stop(); | 169 restore_timer_.Stop(); |
149 if (ash::Shell::HasInstance()) | 170 if (ash::Shell::HasInstance()) |
150 ash::Shell::GetInstance()->user_activity_detector()->RemoveObserver(this); | 171 ash::Shell::GetInstance()->user_activity_detector()->RemoveObserver(this); |
151 } | 172 } |
152 | 173 |
153 } // namespace policy | 174 } // namespace policy |
OLD | NEW |