Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(496)

Side by Side Diff: chromeos/dbus/power_policy_controller.cc

Issue 946643002: Use PowerSaveBlocker for audio and video on Chrome OS. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: apply nits Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromeos/dbus/power_policy_controller.h" 5 #include "chromeos/dbus/power_policy_controller.h"
6 6
7 #include <utility>
8
7 #include "base/format_macros.h" 9 #include "base/format_macros.h"
8 #include "base/logging.h" 10 #include "base/logging.h"
9 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
11 13
12 namespace chromeos { 14 namespace chromeos {
13 15
14 namespace { 16 namespace {
15 17
16 PowerPolicyController* g_power_policy_controller = nullptr; 18 PowerPolicyController* g_power_policy_controller = nullptr;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 case PowerPolicyController::ACTION_SHUT_DOWN: 52 case PowerPolicyController::ACTION_SHUT_DOWN:
51 return power_manager::PowerManagementPolicy_Action_SHUT_DOWN; 53 return power_manager::PowerManagementPolicy_Action_SHUT_DOWN;
52 case PowerPolicyController::ACTION_DO_NOTHING: 54 case PowerPolicyController::ACTION_DO_NOTHING:
53 return power_manager::PowerManagementPolicy_Action_DO_NOTHING; 55 return power_manager::PowerManagementPolicy_Action_DO_NOTHING;
54 default: 56 default:
55 NOTREACHED() << "Unhandled action " << action; 57 NOTREACHED() << "Unhandled action " << action;
56 return power_manager::PowerManagementPolicy_Action_DO_NOTHING; 58 return power_manager::PowerManagementPolicy_Action_DO_NOTHING;
57 } 59 }
58 } 60 }
59 61
62 // Returns false if |use_audio_activity| and |use_audio_activity| prevent wake
63 // locks created for |reason| from being honored or true otherwise.
64 bool IsWakeLockReasonHonored(PowerPolicyController::WakeLockReason reason,
65 bool use_audio_activity,
66 bool use_video_activity) {
67 if (reason == PowerPolicyController::REASON_AUDIO_PLAYBACK &&
68 !use_audio_activity)
69 return false;
70 if (reason == PowerPolicyController::REASON_VIDEO_PLAYBACK &&
71 !use_video_activity)
72 return false;
73 return true;
74 }
75
60 } // namespace 76 } // namespace
61 77
62 const int PowerPolicyController::kScreenLockAfterOffDelayMs = 10000; // 10 sec. 78 const int PowerPolicyController::kScreenLockAfterOffDelayMs = 10000; // 10 sec.
79 const char PowerPolicyController::kPrefsReason[] = "Prefs";
63 80
64 // -1 is interpreted as "unset" by powerd, resulting in powerd's default 81 // -1 is interpreted as "unset" by powerd, resulting in powerd's default
65 // delays being used instead. There are no similarly-interpreted values 82 // delays being used instead. There are no similarly-interpreted values
66 // for the other fields, unfortunately (but the constructor-assigned values 83 // for the other fields, unfortunately (but the constructor-assigned values
67 // will only reach powerd if Chrome messes up and forgets to override them 84 // will only reach powerd if Chrome messes up and forgets to override them
68 // with the pref-assigned values). 85 // with the pref-assigned values).
69 PowerPolicyController::PrefValues::PrefValues() 86 PowerPolicyController::PrefValues::PrefValues()
70 : ac_screen_dim_delay_ms(-1), 87 : ac_screen_dim_delay_ms(-1),
71 ac_screen_off_delay_ms(-1), 88 ac_screen_off_delay_ms(-1),
72 ac_screen_lock_delay_ms(-1), 89 ac_screen_lock_delay_ms(-1),
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 values.wait_for_initial_user_activity); 234 values.wait_for_initial_user_activity);
218 prefs_policy_.set_force_nonzero_brightness_for_user_activity( 235 prefs_policy_.set_force_nonzero_brightness_for_user_activity(
219 values.force_nonzero_brightness_for_user_activity); 236 values.force_nonzero_brightness_for_user_activity);
220 237
221 honor_screen_wake_locks_ = values.allow_screen_wake_locks; 238 honor_screen_wake_locks_ = values.allow_screen_wake_locks;
222 239
223 prefs_were_set_ = true; 240 prefs_were_set_ = true;
224 SendCurrentPolicy(); 241 SendCurrentPolicy();
225 } 242 }
226 243
227 int PowerPolicyController::AddScreenWakeLock(const std::string& reason) { 244 int PowerPolicyController::AddScreenWakeLock(WakeLockReason reason,
228 int id = next_wake_lock_id_++; 245 const std::string& description) {
229 screen_wake_locks_[id] = reason; 246 return AddWakeLockInternal(WakeLock::TYPE_SCREEN, reason, description);
230 SendCurrentPolicy();
231 return id;
232 } 247 }
233 248
234 int PowerPolicyController::AddSystemWakeLock(const std::string& reason) { 249 int PowerPolicyController::AddSystemWakeLock(WakeLockReason reason,
235 int id = next_wake_lock_id_++; 250 const std::string& description) {
236 system_wake_locks_[id] = reason; 251 return AddWakeLockInternal(WakeLock::TYPE_SYSTEM, reason, description);
237 SendCurrentPolicy();
238 return id;
239 } 252 }
240 253
241 void PowerPolicyController::RemoveWakeLock(int id) { 254 void PowerPolicyController::RemoveWakeLock(int id) {
242 if (!screen_wake_locks_.erase(id) && !system_wake_locks_.erase(id)) 255 if (!wake_locks_.erase(id))
243 LOG(WARNING) << "Ignoring request to remove nonexistent wake lock " << id; 256 LOG(WARNING) << "Ignoring request to remove nonexistent wake lock " << id;
244 else 257 else
245 SendCurrentPolicy(); 258 SendCurrentPolicy();
246 } 259 }
247 260
248 void PowerPolicyController::PowerManagerRestarted() { 261 void PowerPolicyController::PowerManagerRestarted() {
249 SendCurrentPolicy(); 262 SendCurrentPolicy();
250 } 263 }
251 264
252 PowerPolicyController::PowerPolicyController(PowerManagerClient* client) 265 PowerPolicyController::PowerPolicyController(PowerManagerClient* client)
253 : client_(client), 266 : client_(client),
254 prefs_were_set_(false), 267 prefs_were_set_(false),
255 honor_screen_wake_locks_(true), 268 honor_screen_wake_locks_(true),
256 next_wake_lock_id_(1) { 269 next_wake_lock_id_(1) {
257 DCHECK(client_); 270 DCHECK(client_);
258 client_->AddObserver(this); 271 client_->AddObserver(this);
259 } 272 }
260 273
261 PowerPolicyController::~PowerPolicyController() { 274 PowerPolicyController::~PowerPolicyController() {
262 client_->RemoveObserver(this); 275 client_->RemoveObserver(this);
263 } 276 }
264 277
278 PowerPolicyController::WakeLock::WakeLock(Type type,
279 WakeLockReason reason,
280 const std::string& description)
281 : type(type), reason(reason), description(description) {
282 }
283
284 PowerPolicyController::WakeLock::~WakeLock() {
285 }
286
287 int PowerPolicyController::AddWakeLockInternal(WakeLock::Type type,
288 WakeLockReason reason,
289 const std::string& description) {
290 const int id = next_wake_lock_id_++;
291 wake_locks_.insert(std::make_pair(id, WakeLock(type, reason, description)));
292 SendCurrentPolicy();
293 return id;
294 }
295
265 void PowerPolicyController::SendCurrentPolicy() { 296 void PowerPolicyController::SendCurrentPolicy() {
266 std::string reason; 297 std::string causes;
267 298
268 power_manager::PowerManagementPolicy policy = prefs_policy_; 299 power_manager::PowerManagementPolicy policy = prefs_policy_;
269 if (prefs_were_set_) 300 if (prefs_were_set_)
270 reason = "Prefs"; 301 causes = kPrefsReason;
271 302
272 if (honor_screen_wake_locks_ && !screen_wake_locks_.empty()) { 303 bool have_screen_wake_locks = false;
304 bool have_system_wake_locks = false;
305 for (const auto& it : wake_locks_) {
306 // Skip audio and video locks that should be ignored due to policy.
307 if (!IsWakeLockReasonHonored(it.second.reason, policy.use_audio_activity(),
308 policy.use_video_activity()))
309 continue;
310
311 switch (it.second.type) {
312 case WakeLock::TYPE_SCREEN:
313 have_screen_wake_locks = true;
314 break;
315 case WakeLock::TYPE_SYSTEM:
316 have_system_wake_locks = true;
317 break;
318 }
319 causes += (causes.empty() ? "" : ", ") + it.second.description;
320 }
321
322 if (honor_screen_wake_locks_ && have_screen_wake_locks) {
273 policy.mutable_ac_delays()->set_screen_dim_ms(0); 323 policy.mutable_ac_delays()->set_screen_dim_ms(0);
274 policy.mutable_ac_delays()->set_screen_off_ms(0); 324 policy.mutable_ac_delays()->set_screen_off_ms(0);
275 policy.mutable_ac_delays()->set_screen_lock_ms(0); 325 policy.mutable_ac_delays()->set_screen_lock_ms(0);
276 policy.mutable_battery_delays()->set_screen_dim_ms(0); 326 policy.mutable_battery_delays()->set_screen_dim_ms(0);
277 policy.mutable_battery_delays()->set_screen_off_ms(0); 327 policy.mutable_battery_delays()->set_screen_off_ms(0);
278 policy.mutable_battery_delays()->set_screen_lock_ms(0); 328 policy.mutable_battery_delays()->set_screen_lock_ms(0);
279 } 329 }
280 330
281 if (!screen_wake_locks_.empty() || !system_wake_locks_.empty()) { 331 if (have_screen_wake_locks || have_system_wake_locks) {
282 if (!policy.has_ac_idle_action() || policy.ac_idle_action() == 332 if (!policy.has_ac_idle_action() || policy.ac_idle_action() ==
283 power_manager::PowerManagementPolicy_Action_SUSPEND) { 333 power_manager::PowerManagementPolicy_Action_SUSPEND) {
284 policy.set_ac_idle_action( 334 policy.set_ac_idle_action(
285 power_manager::PowerManagementPolicy_Action_DO_NOTHING); 335 power_manager::PowerManagementPolicy_Action_DO_NOTHING);
286 } 336 }
287 if (!policy.has_battery_idle_action() || policy.battery_idle_action() == 337 if (!policy.has_battery_idle_action() || policy.battery_idle_action() ==
288 power_manager::PowerManagementPolicy_Action_SUSPEND) { 338 power_manager::PowerManagementPolicy_Action_SUSPEND) {
289 policy.set_battery_idle_action( 339 policy.set_battery_idle_action(
290 power_manager::PowerManagementPolicy_Action_DO_NOTHING); 340 power_manager::PowerManagementPolicy_Action_DO_NOTHING);
291 } 341 }
292 } 342 }
293 343
294 for (WakeLockMap::const_iterator it = screen_wake_locks_.begin(); 344 if (!causes.empty())
295 it != screen_wake_locks_.end(); ++it) { 345 policy.set_reason(causes);
296 reason += (reason.empty() ? "" : ", ") + it->second;
297 }
298 for (WakeLockMap::const_iterator it = system_wake_locks_.begin();
299 it != system_wake_locks_.end(); ++it) {
300 reason += (reason.empty() ? "" : ", ") + it->second;
301 }
302
303 if (!reason.empty())
304 policy.set_reason(reason);
305 client_->SetPolicy(policy); 346 client_->SetPolicy(policy);
306 } 347 }
307 348
308 } // namespace chromeos 349 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/power_policy_controller.h ('k') | chromeos/dbus/power_policy_controller_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698