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/user_prefs/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 notification_registrar_
.Add(this,
31 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
,
32 content::NotificationService::AllSources());
33 notification_registrar_
.Add(this,
34 chrome::NOTIFICATION_SESSION_STARTED
,
35 content::NotificationService::AllSources());
36 notification_registrar_
.Add(this,
37 chrome::NOTIFICATION_PROFILE_DESTROYED
,
38 content::NotificationService::AllSources());
41 PowerPrefs::~PowerPrefs() {
45 void PowerPrefs::RegisterUserProfilePrefs(
46 user_prefs::PrefRegistrySyncable
* registry
) {
47 RegisterProfilePrefs(registry
);
49 registry
->RegisterIntegerPref(
50 prefs::kPowerBatteryIdleAction
,
51 PowerPolicyController::ACTION_SUSPEND
,
52 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
53 registry
->RegisterIntegerPref(
54 prefs::kPowerLidClosedAction
,
55 PowerPolicyController::ACTION_SUSPEND
,
56 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
60 void PowerPrefs::RegisterLoginProfilePrefs(
61 user_prefs::PrefRegistrySyncable
* registry
) {
62 RegisterProfilePrefs(registry
);
64 registry
->RegisterIntegerPref(
65 prefs::kPowerBatteryIdleAction
,
66 PowerPolicyController::ACTION_SHUT_DOWN
,
67 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
68 registry
->RegisterIntegerPref(
69 prefs::kPowerLidClosedAction
,
70 PowerPolicyController::ACTION_SHUT_DOWN
,
71 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
74 void PowerPrefs::Observe(int type
,
75 const content::NotificationSource
& source
,
76 const content::NotificationDetails
& details
) {
78 case chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
: {
79 // Update |profile_| when entering the login screen.
80 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
81 if (!profile_manager
|| !profile_manager
->IsLoggedIn())
82 SetProfile(ProfileHelper::GetSigninProfile());
85 case chrome::NOTIFICATION_SESSION_STARTED
:
86 // Update |profile_| when entering a session.
87 SetProfile(ProfileManager::GetPrimaryUserProfile());
89 case chrome::NOTIFICATION_PROFILE_DESTROYED
: {
90 // Update |profile_| when exiting a session or shutting down.
91 Profile
* profile
= content::Source
<Profile
>(source
).ptr();
92 if (profile_
== profile
)
102 void PowerPrefs::UpdatePowerPolicyFromPrefs() {
103 if (!pref_change_registrar_
|| !pref_change_registrar_
->prefs()) {
108 const PrefService
* prefs
= pref_change_registrar_
->prefs();
109 PowerPolicyController::PrefValues values
;
110 values
.ac_screen_dim_delay_ms
=
111 prefs
->GetInteger(prefs::kPowerAcScreenDimDelayMs
);
112 values
.ac_screen_off_delay_ms
=
113 prefs
->GetInteger(prefs::kPowerAcScreenOffDelayMs
);
114 values
.ac_screen_lock_delay_ms
=
115 prefs
->GetInteger(prefs::kPowerAcScreenLockDelayMs
);
116 values
.ac_idle_warning_delay_ms
=
117 prefs
->GetInteger(prefs::kPowerAcIdleWarningDelayMs
);
118 values
.ac_idle_delay_ms
=
119 prefs
->GetInteger(prefs::kPowerAcIdleDelayMs
);
120 values
.battery_screen_dim_delay_ms
=
121 prefs
->GetInteger(prefs::kPowerBatteryScreenDimDelayMs
);
122 values
.battery_screen_off_delay_ms
=
123 prefs
->GetInteger(prefs::kPowerBatteryScreenOffDelayMs
);
124 values
.battery_screen_lock_delay_ms
=
125 prefs
->GetInteger(prefs::kPowerBatteryScreenLockDelayMs
);
126 values
.battery_idle_warning_delay_ms
=
127 prefs
->GetInteger(prefs::kPowerBatteryIdleWarningDelayMs
);
128 values
.battery_idle_delay_ms
=
129 prefs
->GetInteger(prefs::kPowerBatteryIdleDelayMs
);
130 values
.ac_idle_action
= static_cast<PowerPolicyController::Action
>(
131 prefs
->GetInteger(prefs::kPowerAcIdleAction
));
132 values
.battery_idle_action
= static_cast<PowerPolicyController::Action
>(
133 prefs
->GetInteger(prefs::kPowerBatteryIdleAction
));
134 values
.lid_closed_action
= static_cast<PowerPolicyController::Action
>(
135 prefs
->GetInteger(prefs::kPowerLidClosedAction
));
136 values
.use_audio_activity
=
137 prefs
->GetBoolean(prefs::kPowerUseAudioActivity
);
138 values
.use_video_activity
=
139 prefs
->GetBoolean(prefs::kPowerUseVideoActivity
);
140 values
.allow_screen_wake_locks
=
141 prefs
->GetBoolean(prefs::kPowerAllowScreenWakeLocks
);
142 values
.enable_screen_lock
=
143 prefs
->GetBoolean(prefs::kEnableScreenLock
);
144 values
.presentation_screen_dim_delay_factor
=
145 prefs
->GetDouble(prefs::kPowerPresentationScreenDimDelayFactor
);
146 values
.user_activity_screen_dim_delay_factor
=
147 prefs
->GetDouble(prefs::kPowerUserActivityScreenDimDelayFactor
);
148 values
.wait_for_initial_user_activity
=
149 prefs
->GetBoolean(prefs::kPowerWaitForInitialUserActivity
);
151 power_policy_controller_
->ApplyPrefs(values
);
155 void PowerPrefs::RegisterProfilePrefs(
156 user_prefs::PrefRegistrySyncable
* registry
) {
157 registry
->RegisterIntegerPref(
158 prefs::kPowerAcScreenDimDelayMs
,
160 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
161 registry
->RegisterIntegerPref(
162 prefs::kPowerAcScreenOffDelayMs
,
164 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
165 registry
->RegisterIntegerPref(
166 prefs::kPowerAcScreenLockDelayMs
,
168 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
169 registry
->RegisterIntegerPref(
170 prefs::kPowerAcIdleWarningDelayMs
,
172 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
173 registry
->RegisterIntegerPref(
174 prefs::kPowerAcIdleDelayMs
,
176 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
177 registry
->RegisterIntegerPref(
178 prefs::kPowerBatteryScreenDimDelayMs
,
180 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
181 registry
->RegisterIntegerPref(
182 prefs::kPowerBatteryScreenOffDelayMs
,
184 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
185 registry
->RegisterIntegerPref(
186 prefs::kPowerBatteryScreenLockDelayMs
,
188 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
189 registry
->RegisterIntegerPref(
190 prefs::kPowerBatteryIdleWarningDelayMs
,
192 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
193 registry
->RegisterIntegerPref(
194 prefs::kPowerBatteryIdleDelayMs
,
196 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
197 registry
->RegisterIntegerPref(
198 prefs::kPowerAcIdleAction
,
199 PowerPolicyController::ACTION_SUSPEND
,
200 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
201 registry
->RegisterBooleanPref(
202 prefs::kPowerUseAudioActivity
,
204 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
205 registry
->RegisterBooleanPref(
206 prefs::kPowerUseVideoActivity
,
208 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
209 registry
->RegisterBooleanPref(
210 prefs::kPowerAllowScreenWakeLocks
,
212 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
213 registry
->RegisterBooleanPref(
214 prefs::kEnableScreenLock
,
216 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
217 registry
->RegisterDoublePref(
218 prefs::kPowerPresentationScreenDimDelayFactor
,
220 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
221 registry
->RegisterDoublePref(
222 prefs::kPowerUserActivityScreenDimDelayFactor
,
224 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
225 registry
->RegisterBooleanPref(
226 prefs::kPowerWaitForInitialUserActivity
,
228 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
231 void PowerPrefs::SetProfile(Profile
* profile
) {
232 // No need to reapply policy if profile hasn't changed, e.g. when adding a
233 // secondary user to an existing session.
234 if (profile
== profile_
)
238 pref_change_registrar_
.reset();
243 base::Closure
update_callback(base::Bind(
244 &PowerPrefs::UpdatePowerPolicyFromPrefs
,
245 base::Unretained(this)));
246 pref_change_registrar_
.reset(new PrefChangeRegistrar
);
247 pref_change_registrar_
->Init(profile
->GetPrefs());
248 pref_change_registrar_
->Add(prefs::kPowerAcScreenDimDelayMs
, update_callback
);
249 pref_change_registrar_
->Add(prefs::kPowerAcScreenOffDelayMs
, update_callback
);
250 pref_change_registrar_
->Add(prefs::kPowerAcScreenLockDelayMs
,
252 pref_change_registrar_
->Add(prefs::kPowerAcIdleWarningDelayMs
,
254 pref_change_registrar_
->Add(prefs::kPowerAcIdleDelayMs
, update_callback
);
255 pref_change_registrar_
->Add(prefs::kPowerBatteryScreenDimDelayMs
,
257 pref_change_registrar_
->Add(prefs::kPowerBatteryScreenOffDelayMs
,
259 pref_change_registrar_
->Add(prefs::kPowerBatteryScreenLockDelayMs
,
261 pref_change_registrar_
->Add(prefs::kPowerBatteryIdleWarningDelayMs
,
263 pref_change_registrar_
->Add(prefs::kPowerBatteryIdleDelayMs
, update_callback
);
264 pref_change_registrar_
->Add(prefs::kPowerAcIdleAction
, update_callback
);
265 pref_change_registrar_
->Add(prefs::kPowerBatteryIdleAction
, update_callback
);
266 pref_change_registrar_
->Add(prefs::kPowerLidClosedAction
, update_callback
);
267 pref_change_registrar_
->Add(prefs::kPowerUseAudioActivity
, update_callback
);
268 pref_change_registrar_
->Add(prefs::kPowerUseVideoActivity
, update_callback
);
269 pref_change_registrar_
->Add(prefs::kPowerAllowScreenWakeLocks
,
271 pref_change_registrar_
->Add(prefs::kEnableScreenLock
, update_callback
);
272 pref_change_registrar_
->Add(prefs::kPowerPresentationScreenDimDelayFactor
,
274 pref_change_registrar_
->Add(prefs::kPowerUserActivityScreenDimDelayFactor
,
276 pref_change_registrar_
->Add(prefs::kPowerWaitForInitialUserActivity
,
279 UpdatePowerPolicyFromPrefs();
282 } // namespace chromeos