Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / chromeos / power / power_prefs.cc
blob5dc80f9f4995422bac007ee11e8e22fe242eb9d1
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/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"
25 namespace chromeos {
27 PowerPrefs::PowerPrefs(PowerPolicyController* power_policy_controller)
28 : power_policy_controller_(power_policy_controller),
29 profile_(NULL),
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() {
48 // static
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);
59 // static
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) {
73 switch (type) {
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());
79 break;
81 case chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED:
82 // Update the policy in case different delays have been set for the lock
83 // screen.
84 screen_is_locked_ = *content::Details<bool>(details).ptr();
85 UpdatePowerPolicyFromPrefs();
86 break;
87 case chrome::NOTIFICATION_SESSION_STARTED:
88 // Update |profile_| when entering a session.
89 SetProfile(ProfileManager::GetPrimaryUserProfile());
90 break;
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)
95 SetProfile(NULL);
96 break;
98 default:
99 NOTREACHED();
100 break;
104 void PowerPrefs::UpdatePowerPolicyFromPrefs() {
105 if (!pref_change_registrar_ || !pref_change_registrar_->prefs()) {
106 NOTREACHED();
107 return;
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);
162 // static
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,
184 false,
185 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
186 registry->RegisterDoublePref(prefs::kPowerPresentationScreenDimDelayFactor,
187 2.0);
188 registry->RegisterDoublePref(prefs::kPowerUserActivityScreenDimDelayFactor,
189 2.0);
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_)
199 return;
201 profile_ = profile;
202 pref_change_registrar_.reset();
204 if (!profile)
205 return;
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,
215 update_callback);
216 pref_change_registrar_->Add(prefs::kPowerAcIdleWarningDelayMs,
217 update_callback);
218 pref_change_registrar_->Add(prefs::kPowerAcIdleDelayMs, update_callback);
219 pref_change_registrar_->Add(prefs::kPowerBatteryScreenDimDelayMs,
220 update_callback);
221 pref_change_registrar_->Add(prefs::kPowerBatteryScreenOffDelayMs,
222 update_callback);
223 pref_change_registrar_->Add(prefs::kPowerBatteryScreenLockDelayMs,
224 update_callback);
225 pref_change_registrar_->Add(prefs::kPowerBatteryIdleWarningDelayMs,
226 update_callback);
227 pref_change_registrar_->Add(prefs::kPowerBatteryIdleDelayMs, update_callback);
228 pref_change_registrar_->Add(prefs::kPowerLockScreenDimDelayMs,
229 update_callback);
230 pref_change_registrar_->Add(prefs::kPowerLockScreenOffDelayMs,
231 update_callback);
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,
238 update_callback);
239 pref_change_registrar_->Add(prefs::kEnableAutoScreenLock, update_callback);
240 pref_change_registrar_->Add(prefs::kPowerPresentationScreenDimDelayFactor,
241 update_callback);
242 pref_change_registrar_->Add(prefs::kPowerUserActivityScreenDimDelayFactor,
243 update_callback);
244 pref_change_registrar_->Add(prefs::kPowerWaitForInitialUserActivity,
245 update_callback);
246 pref_change_registrar_->Add(
247 prefs::kPowerForceNonzeroBrightnessForUserActivity, update_callback);
249 UpdatePowerPolicyFromPrefs();
252 } // namespace chromeos