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/power/power_prefs.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/logging.h"
11 #include "base/prefs/pref_change_registrar.h"
12 #include "base/prefs/pref_service.h"
13 #include "chrome/browser/browser_process.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/browser/profiles/profile_manager.h"
18 #include "chrome/common/pref_names.h"
19 #include "chromeos/dbus/power_policy_controller.h"
20 #include "components/pref_registry/pref_registry_syncable.h"
21 #include "content/public/browser/notification_details.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/notification_source.h"
27 PowerPrefs::PowerPrefs(PowerPolicyController
* power_policy_controller
)
28 : power_policy_controller_(power_policy_controller
),
30 screen_is_locked_(false) {
31 notification_registrar_
.Add(this,
32 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
,
33 content::NotificationService::AllSources());
34 notification_registrar_
.Add(this,
35 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED
,
36 content::NotificationService::AllSources());
37 notification_registrar_
.Add(this,
38 chrome::NOTIFICATION_SESSION_STARTED
,
39 content::NotificationService::AllSources());
40 notification_registrar_
.Add(this,
41 chrome::NOTIFICATION_PROFILE_DESTROYED
,
42 content::NotificationService::AllSources());
45 PowerPrefs::~PowerPrefs() {
49 void PowerPrefs::RegisterUserProfilePrefs(
50 user_prefs::PrefRegistrySyncable
* registry
) {
51 RegisterProfilePrefs(registry
);
53 registry
->RegisterIntegerPref(
54 prefs::kPowerBatteryIdleAction
,
55 PowerPolicyController::ACTION_SUSPEND
,
56 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
57 registry
->RegisterIntegerPref(
58 prefs::kPowerLidClosedAction
,
59 PowerPolicyController::ACTION_SUSPEND
,
60 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
64 void PowerPrefs::RegisterLoginProfilePrefs(
65 user_prefs::PrefRegistrySyncable
* registry
) {
66 RegisterProfilePrefs(registry
);
68 registry
->RegisterIntegerPref(
69 prefs::kPowerBatteryIdleAction
,
70 PowerPolicyController::ACTION_SHUT_DOWN
,
71 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
72 registry
->RegisterIntegerPref(
73 prefs::kPowerLidClosedAction
,
74 PowerPolicyController::ACTION_SHUT_DOWN
,
75 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
78 void PowerPrefs::Observe(int type
,
79 const content::NotificationSource
& source
,
80 const content::NotificationDetails
& details
) {
82 case chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
: {
83 // Update |profile_| when entering the login screen.
84 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
85 if (!profile_manager
|| !profile_manager
->IsLoggedIn())
86 SetProfile(ProfileHelper::GetSigninProfile());
89 case chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED
:
90 // Update the policy in case different delays have been set for the lock
92 screen_is_locked_
= *content::Details
<bool>(details
).ptr();
93 UpdatePowerPolicyFromPrefs();
95 case chrome::NOTIFICATION_SESSION_STARTED
:
96 // Update |profile_| when entering a session.
97 SetProfile(ProfileManager::GetPrimaryUserProfile());
99 case chrome::NOTIFICATION_PROFILE_DESTROYED
: {
100 // Update |profile_| when exiting a session or shutting down.
101 Profile
* profile
= content::Source
<Profile
>(source
).ptr();
102 if (profile_
== profile
)
112 void PowerPrefs::UpdatePowerPolicyFromPrefs() {
113 if (!pref_change_registrar_
|| !pref_change_registrar_
->prefs()) {
118 const PrefService
* prefs
= pref_change_registrar_
->prefs();
119 PowerPolicyController::PrefValues values
;
120 values
.ac_screen_dim_delay_ms
=
121 prefs
->GetInteger(screen_is_locked_
? prefs::kPowerLockScreenDimDelayMs
:
122 prefs::kPowerAcScreenDimDelayMs
);
123 values
.ac_screen_off_delay_ms
=
124 prefs
->GetInteger(screen_is_locked_
? prefs::kPowerLockScreenOffDelayMs
:
125 prefs::kPowerAcScreenOffDelayMs
);
126 values
.ac_screen_lock_delay_ms
=
127 prefs
->GetInteger(prefs::kPowerAcScreenLockDelayMs
);
128 values
.ac_idle_warning_delay_ms
=
129 prefs
->GetInteger(prefs::kPowerAcIdleWarningDelayMs
);
130 values
.ac_idle_delay_ms
=
131 prefs
->GetInteger(prefs::kPowerAcIdleDelayMs
);
132 values
.battery_screen_dim_delay_ms
=
133 prefs
->GetInteger(screen_is_locked_
? prefs::kPowerLockScreenDimDelayMs
:
134 prefs::kPowerBatteryScreenDimDelayMs
);
135 values
.battery_screen_off_delay_ms
=
136 prefs
->GetInteger(screen_is_locked_
? prefs::kPowerLockScreenOffDelayMs
:
137 prefs::kPowerBatteryScreenOffDelayMs
);
138 values
.battery_screen_lock_delay_ms
=
139 prefs
->GetInteger(prefs::kPowerBatteryScreenLockDelayMs
);
140 values
.battery_idle_warning_delay_ms
=
141 prefs
->GetInteger(prefs::kPowerBatteryIdleWarningDelayMs
);
142 values
.battery_idle_delay_ms
=
143 prefs
->GetInteger(prefs::kPowerBatteryIdleDelayMs
);
144 values
.ac_idle_action
= static_cast<PowerPolicyController::Action
>(
145 prefs
->GetInteger(prefs::kPowerAcIdleAction
));
146 values
.battery_idle_action
= static_cast<PowerPolicyController::Action
>(
147 prefs
->GetInteger(prefs::kPowerBatteryIdleAction
));
148 values
.lid_closed_action
= static_cast<PowerPolicyController::Action
>(
149 prefs
->GetInteger(prefs::kPowerLidClosedAction
));
150 values
.use_audio_activity
=
151 prefs
->GetBoolean(prefs::kPowerUseAudioActivity
);
152 values
.use_video_activity
=
153 prefs
->GetBoolean(prefs::kPowerUseVideoActivity
);
154 values
.allow_screen_wake_locks
=
155 prefs
->GetBoolean(prefs::kPowerAllowScreenWakeLocks
);
156 values
.enable_auto_screen_lock
=
157 prefs
->GetBoolean(prefs::kEnableAutoScreenLock
);
158 values
.presentation_screen_dim_delay_factor
=
159 prefs
->GetDouble(prefs::kPowerPresentationScreenDimDelayFactor
);
160 values
.user_activity_screen_dim_delay_factor
=
161 prefs
->GetDouble(prefs::kPowerUserActivityScreenDimDelayFactor
);
162 values
.wait_for_initial_user_activity
=
163 prefs
->GetBoolean(prefs::kPowerWaitForInitialUserActivity
);
164 values
.force_nonzero_brightness_for_user_activity
=
165 prefs
->GetBoolean(prefs::kPowerForceNonzeroBrightnessForUserActivity
);
167 power_policy_controller_
->ApplyPrefs(values
);
171 void PowerPrefs::RegisterProfilePrefs(
172 user_prefs::PrefRegistrySyncable
* registry
) {
173 registry
->RegisterIntegerPref(
174 prefs::kPowerAcScreenDimDelayMs
,
176 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
177 registry
->RegisterIntegerPref(
178 prefs::kPowerAcScreenOffDelayMs
,
180 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
181 registry
->RegisterIntegerPref(
182 prefs::kPowerAcScreenLockDelayMs
,
184 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
185 registry
->RegisterIntegerPref(
186 prefs::kPowerAcIdleWarningDelayMs
,
188 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
189 registry
->RegisterIntegerPref(
190 prefs::kPowerAcIdleDelayMs
,
192 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
193 registry
->RegisterIntegerPref(
194 prefs::kPowerBatteryScreenDimDelayMs
,
196 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
197 registry
->RegisterIntegerPref(
198 prefs::kPowerBatteryScreenOffDelayMs
,
200 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
201 registry
->RegisterIntegerPref(
202 prefs::kPowerBatteryScreenLockDelayMs
,
204 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
205 registry
->RegisterIntegerPref(
206 prefs::kPowerBatteryIdleWarningDelayMs
,
208 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
209 registry
->RegisterIntegerPref(
210 prefs::kPowerBatteryIdleDelayMs
,
212 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
213 registry
->RegisterIntegerPref(
214 prefs::kPowerLockScreenDimDelayMs
,
216 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
217 registry
->RegisterIntegerPref(
218 prefs::kPowerLockScreenOffDelayMs
,
220 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
221 registry
->RegisterIntegerPref(
222 prefs::kPowerAcIdleAction
,
223 PowerPolicyController::ACTION_SUSPEND
,
224 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
225 registry
->RegisterBooleanPref(
226 prefs::kPowerUseAudioActivity
,
228 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
229 registry
->RegisterBooleanPref(
230 prefs::kPowerUseVideoActivity
,
232 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
233 registry
->RegisterBooleanPref(
234 prefs::kPowerAllowScreenWakeLocks
,
236 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
237 registry
->RegisterBooleanPref(
238 prefs::kEnableAutoScreenLock
,
240 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
241 registry
->RegisterDoublePref(
242 prefs::kPowerPresentationScreenDimDelayFactor
,
244 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
245 registry
->RegisterDoublePref(
246 prefs::kPowerUserActivityScreenDimDelayFactor
,
248 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
249 registry
->RegisterBooleanPref(
250 prefs::kPowerWaitForInitialUserActivity
,
252 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
253 registry
->RegisterBooleanPref(
254 prefs::kPowerForceNonzeroBrightnessForUserActivity
,
256 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
259 void PowerPrefs::SetProfile(Profile
* profile
) {
260 // No need to reapply policy if profile hasn't changed, e.g. when adding a
261 // secondary user to an existing session.
262 if (profile
== profile_
)
266 pref_change_registrar_
.reset();
271 base::Closure
update_callback(base::Bind(
272 &PowerPrefs::UpdatePowerPolicyFromPrefs
,
273 base::Unretained(this)));
274 pref_change_registrar_
.reset(new PrefChangeRegistrar
);
275 pref_change_registrar_
->Init(profile
->GetPrefs());
276 pref_change_registrar_
->Add(prefs::kPowerAcScreenDimDelayMs
, update_callback
);
277 pref_change_registrar_
->Add(prefs::kPowerAcScreenOffDelayMs
, update_callback
);
278 pref_change_registrar_
->Add(prefs::kPowerAcScreenLockDelayMs
,
280 pref_change_registrar_
->Add(prefs::kPowerAcIdleWarningDelayMs
,
282 pref_change_registrar_
->Add(prefs::kPowerAcIdleDelayMs
, update_callback
);
283 pref_change_registrar_
->Add(prefs::kPowerBatteryScreenDimDelayMs
,
285 pref_change_registrar_
->Add(prefs::kPowerBatteryScreenOffDelayMs
,
287 pref_change_registrar_
->Add(prefs::kPowerBatteryScreenLockDelayMs
,
289 pref_change_registrar_
->Add(prefs::kPowerBatteryIdleWarningDelayMs
,
291 pref_change_registrar_
->Add(prefs::kPowerBatteryIdleDelayMs
, update_callback
);
292 pref_change_registrar_
->Add(prefs::kPowerLockScreenDimDelayMs
,
294 pref_change_registrar_
->Add(prefs::kPowerLockScreenOffDelayMs
,
296 pref_change_registrar_
->Add(prefs::kPowerAcIdleAction
, update_callback
);
297 pref_change_registrar_
->Add(prefs::kPowerBatteryIdleAction
, update_callback
);
298 pref_change_registrar_
->Add(prefs::kPowerLidClosedAction
, update_callback
);
299 pref_change_registrar_
->Add(prefs::kPowerUseAudioActivity
, update_callback
);
300 pref_change_registrar_
->Add(prefs::kPowerUseVideoActivity
, update_callback
);
301 pref_change_registrar_
->Add(prefs::kPowerAllowScreenWakeLocks
,
303 pref_change_registrar_
->Add(prefs::kEnableAutoScreenLock
, update_callback
);
304 pref_change_registrar_
->Add(prefs::kPowerPresentationScreenDimDelayFactor
,
306 pref_change_registrar_
->Add(prefs::kPowerUserActivityScreenDimDelayFactor
,
308 pref_change_registrar_
->Add(prefs::kPowerWaitForInitialUserActivity
,
310 pref_change_registrar_
->Add(
311 prefs::kPowerForceNonzeroBrightnessForUserActivity
, update_callback
);
313 UpdatePowerPolicyFromPrefs();
316 } // namespace chromeos