Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / power / power_prefs.cc
blob252bb5a891f3face3c4892ea3b0b52436c0c2716
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"
7 #include "base/bind.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"
25 namespace chromeos {
27 PowerPrefs::PowerPrefs(PowerPolicyController* power_policy_controller)
28 : power_policy_controller_(power_policy_controller),
29 profile_(NULL) {
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() {
44 // static
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);
59 // static
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) {
77 switch (type) {
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());
83 break;
85 case chrome::NOTIFICATION_SESSION_STARTED:
86 // Update |profile_| when entering a session.
87 SetProfile(ProfileManager::GetPrimaryUserProfile());
88 break;
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)
93 SetProfile(NULL);
94 break;
96 default:
97 NOTREACHED();
98 break;
102 void PowerPrefs::UpdatePowerPolicyFromPrefs() {
103 if (!pref_change_registrar_ || !pref_change_registrar_->prefs()) {
104 NOTREACHED();
105 return;
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);
154 // static
155 void PowerPrefs::RegisterProfilePrefs(
156 user_prefs::PrefRegistrySyncable* registry) {
157 registry->RegisterIntegerPref(
158 prefs::kPowerAcScreenDimDelayMs,
159 420000,
160 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
161 registry->RegisterIntegerPref(
162 prefs::kPowerAcScreenOffDelayMs,
163 480000,
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,
175 1800000,
176 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
177 registry->RegisterIntegerPref(
178 prefs::kPowerBatteryScreenDimDelayMs,
179 300000,
180 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
181 registry->RegisterIntegerPref(
182 prefs::kPowerBatteryScreenOffDelayMs,
183 360000,
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,
195 600000,
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,
203 true,
204 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
205 registry->RegisterBooleanPref(
206 prefs::kPowerUseVideoActivity,
207 true,
208 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
209 registry->RegisterBooleanPref(
210 prefs::kPowerAllowScreenWakeLocks,
211 true,
212 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
213 registry->RegisterBooleanPref(
214 prefs::kEnableScreenLock,
215 false,
216 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
217 registry->RegisterDoublePref(
218 prefs::kPowerPresentationScreenDimDelayFactor,
219 2.0,
220 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
221 registry->RegisterDoublePref(
222 prefs::kPowerUserActivityScreenDimDelayFactor,
223 2.0,
224 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
225 registry->RegisterBooleanPref(
226 prefs::kPowerWaitForInitialUserActivity,
227 false,
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_)
235 return;
237 profile_ = profile;
238 pref_change_registrar_.reset();
240 if (!profile)
241 return;
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,
251 update_callback);
252 pref_change_registrar_->Add(prefs::kPowerAcIdleWarningDelayMs,
253 update_callback);
254 pref_change_registrar_->Add(prefs::kPowerAcIdleDelayMs, update_callback);
255 pref_change_registrar_->Add(prefs::kPowerBatteryScreenDimDelayMs,
256 update_callback);
257 pref_change_registrar_->Add(prefs::kPowerBatteryScreenOffDelayMs,
258 update_callback);
259 pref_change_registrar_->Add(prefs::kPowerBatteryScreenLockDelayMs,
260 update_callback);
261 pref_change_registrar_->Add(prefs::kPowerBatteryIdleWarningDelayMs,
262 update_callback);
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,
270 update_callback);
271 pref_change_registrar_->Add(prefs::kEnableScreenLock, update_callback);
272 pref_change_registrar_->Add(prefs::kPowerPresentationScreenDimDelayFactor,
273 update_callback);
274 pref_change_registrar_->Add(prefs::kPowerUserActivityScreenDimDelayFactor,
275 update_callback);
276 pref_change_registrar_->Add(prefs::kPowerWaitForInitialUserActivity,
277 update_callback);
279 UpdatePowerPolicyFromPrefs();
282 } // namespace chromeos