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(prefs::kPowerBatteryIdleAction
,
54 PowerPolicyController::ACTION_SUSPEND
);
55 registry
->RegisterIntegerPref(prefs::kPowerLidClosedAction
,
56 PowerPolicyController::ACTION_SUSPEND
);
60 void PowerPrefs::RegisterLoginProfilePrefs(
61 user_prefs::PrefRegistrySyncable
* registry
) {
62 RegisterProfilePrefs(registry
);
64 registry
->RegisterIntegerPref(prefs::kPowerBatteryIdleAction
,
65 PowerPolicyController::ACTION_SHUT_DOWN
);
66 registry
->RegisterIntegerPref(prefs::kPowerLidClosedAction
,
67 PowerPolicyController::ACTION_SHUT_DOWN
);
70 void PowerPrefs::Observe(int type
,
71 const content::NotificationSource
& source
,
72 const content::NotificationDetails
& details
) {
74 case chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
: {
75 // Update |profile_| when entering the login screen.
76 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
77 if (!profile_manager
|| !profile_manager
->IsLoggedIn())
78 SetProfile(ProfileHelper::GetSigninProfile());
81 case chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED
:
82 // Update the policy in case different delays have been set for the lock
84 screen_is_locked_
= *content::Details
<bool>(details
).ptr();
85 UpdatePowerPolicyFromPrefs();
87 case chrome::NOTIFICATION_SESSION_STARTED
:
88 // Update |profile_| when entering a session.
89 SetProfile(ProfileManager::GetPrimaryUserProfile());
91 case chrome::NOTIFICATION_PROFILE_DESTROYED
: {
92 // Update |profile_| when exiting a session or shutting down.
93 Profile
* profile
= content::Source
<Profile
>(source
).ptr();
94 if (profile_
== profile
)
104 void PowerPrefs::UpdatePowerPolicyFromPrefs() {
105 if (!pref_change_registrar_
|| !pref_change_registrar_
->prefs()) {
110 const PrefService
* prefs
= pref_change_registrar_
->prefs();
111 PowerPolicyController::PrefValues values
;
112 values
.ac_screen_dim_delay_ms
=
113 prefs
->GetInteger(screen_is_locked_
? prefs::kPowerLockScreenDimDelayMs
:
114 prefs::kPowerAcScreenDimDelayMs
);
115 values
.ac_screen_off_delay_ms
=
116 prefs
->GetInteger(screen_is_locked_
? prefs::kPowerLockScreenOffDelayMs
:
117 prefs::kPowerAcScreenOffDelayMs
);
118 values
.ac_screen_lock_delay_ms
=
119 prefs
->GetInteger(prefs::kPowerAcScreenLockDelayMs
);
120 values
.ac_idle_warning_delay_ms
=
121 prefs
->GetInteger(prefs::kPowerAcIdleWarningDelayMs
);
122 values
.ac_idle_delay_ms
=
123 prefs
->GetInteger(prefs::kPowerAcIdleDelayMs
);
124 values
.battery_screen_dim_delay_ms
=
125 prefs
->GetInteger(screen_is_locked_
? prefs::kPowerLockScreenDimDelayMs
:
126 prefs::kPowerBatteryScreenDimDelayMs
);
127 values
.battery_screen_off_delay_ms
=
128 prefs
->GetInteger(screen_is_locked_
? prefs::kPowerLockScreenOffDelayMs
:
129 prefs::kPowerBatteryScreenOffDelayMs
);
130 values
.battery_screen_lock_delay_ms
=
131 prefs
->GetInteger(prefs::kPowerBatteryScreenLockDelayMs
);
132 values
.battery_idle_warning_delay_ms
=
133 prefs
->GetInteger(prefs::kPowerBatteryIdleWarningDelayMs
);
134 values
.battery_idle_delay_ms
=
135 prefs
->GetInteger(prefs::kPowerBatteryIdleDelayMs
);
136 values
.ac_idle_action
= static_cast<PowerPolicyController::Action
>(
137 prefs
->GetInteger(prefs::kPowerAcIdleAction
));
138 values
.battery_idle_action
= static_cast<PowerPolicyController::Action
>(
139 prefs
->GetInteger(prefs::kPowerBatteryIdleAction
));
140 values
.lid_closed_action
= static_cast<PowerPolicyController::Action
>(
141 prefs
->GetInteger(prefs::kPowerLidClosedAction
));
142 values
.use_audio_activity
=
143 prefs
->GetBoolean(prefs::kPowerUseAudioActivity
);
144 values
.use_video_activity
=
145 prefs
->GetBoolean(prefs::kPowerUseVideoActivity
);
146 values
.allow_screen_wake_locks
=
147 prefs
->GetBoolean(prefs::kPowerAllowScreenWakeLocks
);
148 values
.enable_auto_screen_lock
=
149 prefs
->GetBoolean(prefs::kEnableAutoScreenLock
);
150 values
.presentation_screen_dim_delay_factor
=
151 prefs
->GetDouble(prefs::kPowerPresentationScreenDimDelayFactor
);
152 values
.user_activity_screen_dim_delay_factor
=
153 prefs
->GetDouble(prefs::kPowerUserActivityScreenDimDelayFactor
);
154 values
.wait_for_initial_user_activity
=
155 prefs
->GetBoolean(prefs::kPowerWaitForInitialUserActivity
);
156 values
.force_nonzero_brightness_for_user_activity
=
157 prefs
->GetBoolean(prefs::kPowerForceNonzeroBrightnessForUserActivity
);
159 power_policy_controller_
->ApplyPrefs(values
);
163 void PowerPrefs::RegisterProfilePrefs(
164 user_prefs::PrefRegistrySyncable
* registry
) {
165 registry
->RegisterIntegerPref(prefs::kPowerAcScreenDimDelayMs
, 420000);
166 registry
->RegisterIntegerPref(prefs::kPowerAcScreenOffDelayMs
, 480000);
167 registry
->RegisterIntegerPref(prefs::kPowerAcScreenLockDelayMs
, 0);
168 registry
->RegisterIntegerPref(prefs::kPowerAcIdleWarningDelayMs
, 0);
169 registry
->RegisterIntegerPref(prefs::kPowerAcIdleDelayMs
, 1800000);
170 registry
->RegisterIntegerPref(prefs::kPowerBatteryScreenDimDelayMs
, 300000);
171 registry
->RegisterIntegerPref(prefs::kPowerBatteryScreenOffDelayMs
, 360000);
172 registry
->RegisterIntegerPref(prefs::kPowerBatteryScreenLockDelayMs
, 0);
173 registry
->RegisterIntegerPref(prefs::kPowerBatteryIdleWarningDelayMs
, 0);
174 registry
->RegisterIntegerPref(prefs::kPowerBatteryIdleDelayMs
, 600000);
175 registry
->RegisterIntegerPref(prefs::kPowerLockScreenDimDelayMs
, 30000);
176 registry
->RegisterIntegerPref(prefs::kPowerLockScreenOffDelayMs
, 40000);
177 registry
->RegisterIntegerPref(prefs::kPowerAcIdleAction
,
178 PowerPolicyController::ACTION_SUSPEND
);
179 registry
->RegisterBooleanPref(prefs::kPowerUseAudioActivity
, true);
180 registry
->RegisterBooleanPref(prefs::kPowerUseVideoActivity
, true);
181 registry
->RegisterBooleanPref(prefs::kPowerAllowScreenWakeLocks
, true);
182 registry
->RegisterBooleanPref(
183 prefs::kEnableAutoScreenLock
,
185 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
186 registry
->RegisterDoublePref(prefs::kPowerPresentationScreenDimDelayFactor
,
188 registry
->RegisterDoublePref(prefs::kPowerUserActivityScreenDimDelayFactor
,
190 registry
->RegisterBooleanPref(prefs::kPowerWaitForInitialUserActivity
, false);
191 registry
->RegisterBooleanPref(
192 prefs::kPowerForceNonzeroBrightnessForUserActivity
, true);
195 void PowerPrefs::SetProfile(Profile
* profile
) {
196 // No need to reapply policy if profile hasn't changed, e.g. when adding a
197 // secondary user to an existing session.
198 if (profile
== profile_
)
202 pref_change_registrar_
.reset();
207 base::Closure
update_callback(base::Bind(
208 &PowerPrefs::UpdatePowerPolicyFromPrefs
,
209 base::Unretained(this)));
210 pref_change_registrar_
.reset(new PrefChangeRegistrar
);
211 pref_change_registrar_
->Init(profile
->GetPrefs());
212 pref_change_registrar_
->Add(prefs::kPowerAcScreenDimDelayMs
, update_callback
);
213 pref_change_registrar_
->Add(prefs::kPowerAcScreenOffDelayMs
, update_callback
);
214 pref_change_registrar_
->Add(prefs::kPowerAcScreenLockDelayMs
,
216 pref_change_registrar_
->Add(prefs::kPowerAcIdleWarningDelayMs
,
218 pref_change_registrar_
->Add(prefs::kPowerAcIdleDelayMs
, update_callback
);
219 pref_change_registrar_
->Add(prefs::kPowerBatteryScreenDimDelayMs
,
221 pref_change_registrar_
->Add(prefs::kPowerBatteryScreenOffDelayMs
,
223 pref_change_registrar_
->Add(prefs::kPowerBatteryScreenLockDelayMs
,
225 pref_change_registrar_
->Add(prefs::kPowerBatteryIdleWarningDelayMs
,
227 pref_change_registrar_
->Add(prefs::kPowerBatteryIdleDelayMs
, update_callback
);
228 pref_change_registrar_
->Add(prefs::kPowerLockScreenDimDelayMs
,
230 pref_change_registrar_
->Add(prefs::kPowerLockScreenOffDelayMs
,
232 pref_change_registrar_
->Add(prefs::kPowerAcIdleAction
, update_callback
);
233 pref_change_registrar_
->Add(prefs::kPowerBatteryIdleAction
, update_callback
);
234 pref_change_registrar_
->Add(prefs::kPowerLidClosedAction
, update_callback
);
235 pref_change_registrar_
->Add(prefs::kPowerUseAudioActivity
, update_callback
);
236 pref_change_registrar_
->Add(prefs::kPowerUseVideoActivity
, update_callback
);
237 pref_change_registrar_
->Add(prefs::kPowerAllowScreenWakeLocks
,
239 pref_change_registrar_
->Add(prefs::kEnableAutoScreenLock
, update_callback
);
240 pref_change_registrar_
->Add(prefs::kPowerPresentationScreenDimDelayFactor
,
242 pref_change_registrar_
->Add(prefs::kPowerUserActivityScreenDimDelayFactor
,
244 pref_change_registrar_
->Add(prefs::kPowerWaitForInitialUserActivity
,
246 pref_change_registrar_
->Add(
247 prefs::kPowerForceNonzeroBrightnessForUserActivity
, update_callback
);
249 UpdatePowerPolicyFromPrefs();
252 } // namespace chromeos