Chromium Code Reviews| Index: chromeos/dbus/power_policy_controller.cc |
| diff --git a/chromeos/dbus/power_policy_controller.cc b/chromeos/dbus/power_policy_controller.cc |
| index 40621c840423fbfeaf112ceb0c0a1e77c1f98e14..5dbc3f15981f672d63f7e4e72dc6ab792583f78c 100644 |
| --- a/chromeos/dbus/power_policy_controller.cc |
| +++ b/chromeos/dbus/power_policy_controller.cc |
| @@ -4,6 +4,8 @@ |
| #include "chromeos/dbus/power_policy_controller.h" |
| +#include <utility> |
| + |
| #include "base/format_macros.h" |
| #include "base/logging.h" |
| #include "base/strings/string_util.h" |
| @@ -57,9 +59,24 @@ power_manager::PowerManagementPolicy_Action GetProtoAction( |
| } |
| } |
| +// Returns false if |use_audio_activity| and |use_audio_activity| prevent wake |
| +// locks created for |reason| from being honored or true otherwise. |
| +bool IsWakeLockReasonHonored(PowerPolicyController::WakeLockReason reason, |
| + bool use_audio_activity, |
| + bool use_video_activity) { |
| + if (reason == PowerPolicyController::REASON_AUDIO_PLAYBACK && |
| + !use_audio_activity) |
|
bartfab (slow)
2015/02/23 13:56:49
Nit: Add {}.
Daniel Erat
2015/02/23 16:34:05
i hope you don't mind, but i think i'm going to ig
|
| + return false; |
| + if (reason == PowerPolicyController::REASON_VIDEO_PLAYBACK && |
| + !use_video_activity) |
|
bartfab (slow)
2015/02/23 13:56:49
Nit: Add {}.
|
| + return false; |
| + return true; |
| +} |
| + |
| } // namespace |
| const int PowerPolicyController::kScreenLockAfterOffDelayMs = 10000; // 10 sec. |
| +const char PowerPolicyController::kPrefsReason[] = "Prefs"; |
| // -1 is interpreted as "unset" by powerd, resulting in powerd's default |
| // delays being used instead. There are no similarly-interpreted values |
| @@ -224,22 +241,18 @@ void PowerPolicyController::ApplyPrefs(const PrefValues& values) { |
| SendCurrentPolicy(); |
| } |
| -int PowerPolicyController::AddScreenWakeLock(const std::string& reason) { |
| - int id = next_wake_lock_id_++; |
| - screen_wake_locks_[id] = reason; |
| - SendCurrentPolicy(); |
| - return id; |
| +int PowerPolicyController::AddScreenWakeLock(WakeLockReason reason, |
| + const std::string& description) { |
| + return AddWakeLockInternal(WakeLock::TYPE_SCREEN, reason, description); |
| } |
| -int PowerPolicyController::AddSystemWakeLock(const std::string& reason) { |
| - int id = next_wake_lock_id_++; |
| - system_wake_locks_[id] = reason; |
| - SendCurrentPolicy(); |
| - return id; |
| +int PowerPolicyController::AddSystemWakeLock(WakeLockReason reason, |
| + const std::string& description) { |
| + return AddWakeLockInternal(WakeLock::TYPE_SYSTEM, reason, description); |
| } |
| void PowerPolicyController::RemoveWakeLock(int id) { |
| - if (!screen_wake_locks_.erase(id) && !system_wake_locks_.erase(id)) |
| + if (!wake_locks_.erase(id)) |
| LOG(WARNING) << "Ignoring request to remove nonexistent wake lock " << id; |
| else |
| SendCurrentPolicy(); |
| @@ -262,14 +275,51 @@ PowerPolicyController::~PowerPolicyController() { |
| client_->RemoveObserver(this); |
| } |
| +PowerPolicyController::WakeLock::WakeLock(Type type, |
| + WakeLockReason reason, |
| + const std::string& description) |
| + : type(type), reason(reason), description(description) { |
| +} |
| + |
| +PowerPolicyController::WakeLock::~WakeLock() { |
| +} |
| + |
| +int PowerPolicyController::AddWakeLockInternal(WakeLock::Type type, |
| + WakeLockReason reason, |
| + const std::string& description) { |
| + int id = next_wake_lock_id_++; |
|
bartfab (slow)
2015/02/23 13:56:49
Nit: const.
Daniel Erat
2015/02/23 16:34:05
Done.
|
| + wake_locks_.insert(std::make_pair(id, WakeLock(type, reason, description))); |
| + SendCurrentPolicy(); |
| + return id; |
| +} |
| + |
| void PowerPolicyController::SendCurrentPolicy() { |
| - std::string reason; |
| + std::string causes; |
| power_manager::PowerManagementPolicy policy = prefs_policy_; |
| if (prefs_were_set_) |
| - reason = "Prefs"; |
| + causes = kPrefsReason; |
| + |
| + bool have_screen_wake_locks = false; |
| + bool have_system_wake_locks = false; |
| + for (const auto& it : wake_locks_) { |
| + // Skip audio and video locks that should be ignored due to policy. |
| + if (!IsWakeLockReasonHonored(it.second.reason, policy.use_audio_activity(), |
| + policy.use_video_activity())) |
|
bartfab (slow)
2015/02/23 13:56:49
Nit: Add {}.
|
| + continue; |
| + |
| + switch (it.second.type) { |
| + case WakeLock::TYPE_SCREEN: |
| + have_screen_wake_locks = true; |
| + break; |
| + case WakeLock::TYPE_SYSTEM: |
| + have_system_wake_locks = true; |
| + break; |
| + } |
| + causes += (causes.empty() ? "" : ", ") + it.second.description; |
| + } |
| - if (honor_screen_wake_locks_ && !screen_wake_locks_.empty()) { |
| + if (honor_screen_wake_locks_ && have_screen_wake_locks) { |
| policy.mutable_ac_delays()->set_screen_dim_ms(0); |
| policy.mutable_ac_delays()->set_screen_off_ms(0); |
| policy.mutable_ac_delays()->set_screen_lock_ms(0); |
| @@ -278,7 +328,7 @@ void PowerPolicyController::SendCurrentPolicy() { |
| policy.mutable_battery_delays()->set_screen_lock_ms(0); |
| } |
| - if (!screen_wake_locks_.empty() || !system_wake_locks_.empty()) { |
| + if (have_screen_wake_locks || have_system_wake_locks) { |
| if (!policy.has_ac_idle_action() || policy.ac_idle_action() == |
| power_manager::PowerManagementPolicy_Action_SUSPEND) { |
| policy.set_ac_idle_action( |
| @@ -291,17 +341,8 @@ void PowerPolicyController::SendCurrentPolicy() { |
| } |
| } |
| - for (WakeLockMap::const_iterator it = screen_wake_locks_.begin(); |
| - it != screen_wake_locks_.end(); ++it) { |
| - reason += (reason.empty() ? "" : ", ") + it->second; |
| - } |
| - for (WakeLockMap::const_iterator it = system_wake_locks_.begin(); |
| - it != system_wake_locks_.end(); ++it) { |
| - reason += (reason.empty() ? "" : ", ") + it->second; |
| - } |
| - |
| - if (!reason.empty()) |
| - policy.set_reason(reason); |
| + if (!causes.empty()) |
| + policy.set_reason(causes); |
| client_->SetPolicy(policy); |
| } |