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

Unified Diff: chromeos/dbus/power_policy_controller.cc

Issue 14134004: chromeos: Move default power management policy into Chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: change char pointers in PrefNames to std::strings Created 7 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: chromeos/dbus/power_policy_controller.cc
diff --git a/chromeos/dbus/power_policy_controller.cc b/chromeos/dbus/power_policy_controller.cc
index 25849106a2e5944f54c80a4272512c36511a67f6..d50cc9205b8eea86ff1c4f3aa1d425bb29335b8e 100644
--- a/chromeos/dbus/power_policy_controller.cc
+++ b/chromeos/dbus/power_policy_controller.cc
@@ -4,7 +4,9 @@
#include "chromeos/dbus/power_policy_controller.h"
+#include "base/bind.h"
#include "base/logging.h"
+#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "chromeos/dbus/dbus_thread_manager.h"
@@ -12,68 +14,20 @@ namespace chromeos {
namespace {
-// If |pref|, a PrefService::Preference containing an integer, has been
-// explicitly set to 0 or a positive value, assigns it to |proto_field|, a
-// int32 field in |proto|, a google::protobuf::MessageLite*. If |proto|
-// was updated, |got_prefs|, a bool*, is set to true; otherwise it is left
-// unchanged.
-#define SET_DELAY_FROM_PREF(pref, proto_field, proto, got_prefs) \
- { \
- int value = GetIntPrefValue(pref); \
- if (value >= 0) { \
- (proto)->set_##proto_field(value); \
- *got_prefs = true; \
- } \
- }
-
-// Similar to SET_DELAY_FROM_PREF() but sets a
-// power_manager::PowerManagementPolicy_Action field instead.
-#define SET_ACTION_FROM_PREF(pref, proto_field, proto, got_prefs) \
- { \
- int value = GetIntPrefValue(pref); \
- if (value >= 0) { \
- (proto)->set_##proto_field( \
- static_cast<power_manager::PowerManagementPolicy_Action>(value)); \
- *got_prefs = true; \
- } \
- }
-
-// If |pref|, a PrefService::Preference containing a bool, has been set,
-// assigns it to |proto_field|, a bool field in |proto|, a
-// google::protobuf::MessageLite*. If |proto| was updated, |got_prefs|, a
-// bool*, is set to true; otherwise it is left unchanged.
-#define SET_BOOL_FROM_PREF(pref, proto_field, proto, got_prefs) \
- if (!pref.IsDefaultValue()) { \
- bool value = false; \
- if (pref.GetValue()->GetAsBoolean(&value)) { \
- (proto)->set_##proto_field(value); \
- *got_prefs = true; \
- } else { \
- LOG(DFATAL) << pref.name() << " pref has non-boolean value"; \
- } \
- }
-
-// Returns a zero or positive integer value from |pref|. Returns -1 if the
-// pref is unset and logs an error if it's set to a negative value.
-int GetIntPrefValue(const PrefService::Preference& pref) {
- if (pref.IsDefaultValue())
- return -1;
-
- int value = -1;
- if (!pref.GetValue()->GetAsInteger(&value)) {
- LOG(DFATAL) << pref.name() << " pref has non-integer value";
- return -1;
- }
-
- if (value < 0)
- LOG(WARNING) << pref.name() << " pref has negative value";
- return value;
-}
+// Initializes |pref|, a PrefMember*, to track changes to |pref_name|, the
+// name of a member within |pref_names_|.
+#define INIT_PREF(pref_name, pref) \
+ DCHECK(!pref_names_.pref_name.empty()); \
+ (pref)->Init(pref_names_.pref_name.c_str(), pref_service_, \
+ base::Bind(&PowerPolicyController::OnPrefChanged, \
+ weak_ptr_factory_.GetWeakPtr()));
// Returns the power_manager::PowerManagementPolicy_Action value
-// corresponding to |action|.
-power_manager::PowerManagementPolicy_Action GetProtoAction(
- PowerPolicyController::Action action) {
+// corresponding to the Action contained in |pref|.
+power_manager::PowerManagementPolicy_Action GetProtoActionFromPref(
+ const IntegerPrefMember& pref) {
+ PowerPolicyController::Action action =
+ static_cast<PowerPolicyController::Action>(pref.GetValue());
switch (action) {
case PowerPolicyController::ACTION_SUSPEND:
return power_manager::PowerManagementPolicy_Action_SUSPEND;
@@ -95,11 +49,12 @@ PowerPolicyController::PowerPolicyController(DBusThreadManager* manager,
PowerManagerClient* client)
: manager_(manager),
client_(client),
- prefs_were_set_(false),
- next_block_id_(1) {
+ pref_service_(NULL),
+ next_block_id_(1),
+ weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
manager_->AddObserver(this);
client_->AddObserver(this);
- SendEmptyPolicy();
+ SendCurrentPolicy();
}
PowerPolicyController::~PowerPolicyController() {
@@ -113,70 +68,31 @@ PowerPolicyController::~PowerPolicyController() {
manager_ = NULL;
}
-void PowerPolicyController::UpdatePolicyFromPrefs(
- const PrefService::Preference& ac_screen_dim_delay_ms_pref,
- const PrefService::Preference& ac_screen_off_delay_ms_pref,
- const PrefService::Preference& ac_screen_lock_delay_ms_pref,
- const PrefService::Preference& ac_idle_warning_delay_ms_pref,
- const PrefService::Preference& ac_idle_delay_ms_pref,
- const PrefService::Preference& battery_screen_dim_delay_ms_pref,
- const PrefService::Preference& battery_screen_off_delay_ms_pref,
- const PrefService::Preference& battery_screen_lock_delay_ms_pref,
- const PrefService::Preference& battery_idle_warning_delay_ms_pref,
- const PrefService::Preference& battery_idle_delay_ms_pref,
- const PrefService::Preference& idle_action_pref,
- const PrefService::Preference& lid_closed_action_pref,
- const PrefService::Preference& use_audio_activity_pref,
- const PrefService::Preference& use_video_activity_pref,
- const PrefService::Preference& presentation_idle_delay_factor_pref) {
- prefs_policy_.Clear();
- bool got_prefs = false;
-
- power_manager::PowerManagementPolicy::Delays* delays =
- prefs_policy_.mutable_ac_delays();
- SET_DELAY_FROM_PREF(
- ac_screen_dim_delay_ms_pref, screen_dim_ms, delays, &got_prefs);
- SET_DELAY_FROM_PREF(
- ac_screen_off_delay_ms_pref, screen_off_ms, delays, &got_prefs);
- SET_DELAY_FROM_PREF(
- ac_screen_lock_delay_ms_pref, screen_lock_ms, delays, &got_prefs);
- SET_DELAY_FROM_PREF(
- ac_idle_warning_delay_ms_pref, idle_warning_ms, delays, &got_prefs);
- SET_DELAY_FROM_PREF(ac_idle_delay_ms_pref, idle_ms, delays, &got_prefs);
-
- delays = prefs_policy_.mutable_battery_delays();
- SET_DELAY_FROM_PREF(
- battery_screen_dim_delay_ms_pref, screen_dim_ms, delays, &got_prefs);
- SET_DELAY_FROM_PREF(
- battery_screen_off_delay_ms_pref, screen_off_ms, delays, &got_prefs);
- SET_DELAY_FROM_PREF(
- battery_screen_lock_delay_ms_pref, screen_lock_ms, delays, &got_prefs);
- SET_DELAY_FROM_PREF(
- battery_idle_warning_delay_ms_pref, idle_warning_ms, delays, &got_prefs);
- SET_DELAY_FROM_PREF(battery_idle_delay_ms_pref, idle_ms, delays, &got_prefs);
-
- SET_ACTION_FROM_PREF(
- idle_action_pref, idle_action, &prefs_policy_, &got_prefs);
- SET_ACTION_FROM_PREF(
- lid_closed_action_pref, lid_closed_action, &prefs_policy_, &got_prefs);
-
- SET_BOOL_FROM_PREF(
- use_audio_activity_pref, use_audio_activity, &prefs_policy_, &got_prefs);
- SET_BOOL_FROM_PREF(
- use_video_activity_pref, use_video_activity, &prefs_policy_, &got_prefs);
-
- if (!presentation_idle_delay_factor_pref.IsDefaultValue()) {
- double value = 0.0;
- if (presentation_idle_delay_factor_pref.GetValue()->GetAsDouble(&value)) {
- prefs_policy_.set_presentation_idle_delay_factor(value);
- got_prefs = true;
- } else {
- LOG(DFATAL) << presentation_idle_delay_factor_pref.name()
- << " pref has non-double value";
- }
- }
+void PowerPolicyController::InitPrefs(PrefService* pref_service,
+ const PrefNames& pref_names) {
+ DCHECK(pref_service);
+ pref_service_ = pref_service;
+ pref_names_ = pref_names;
+
+ INIT_PREF(ac_screen_dim_delay_ms, &ac_screen_dim_delay_ms_pref_);
+ INIT_PREF(ac_screen_off_delay_ms, &ac_screen_off_delay_ms_pref_);
+ INIT_PREF(ac_screen_lock_delay_ms, &ac_screen_lock_delay_ms_pref_);
+ INIT_PREF(ac_idle_warning_delay_ms, &ac_idle_warning_delay_ms_pref_);
+ INIT_PREF(ac_idle_delay_ms, &ac_idle_delay_ms_pref_);
+ INIT_PREF(battery_screen_dim_delay_ms, &battery_screen_dim_delay_ms_pref_);
+ INIT_PREF(battery_screen_off_delay_ms, &battery_screen_off_delay_ms_pref_);
+ INIT_PREF(battery_screen_lock_delay_ms, &battery_screen_lock_delay_ms_pref_);
+ INIT_PREF(battery_idle_warning_delay_ms,
+ &battery_idle_warning_delay_ms_pref_);
+ INIT_PREF(battery_idle_delay_ms, &battery_idle_delay_ms_pref_);
+ INIT_PREF(idle_action, &idle_action_pref_);
+ INIT_PREF(lid_closed_action, &lid_closed_action_pref_);
+ INIT_PREF(use_audio_activity, &use_audio_activity_pref_);
+ INIT_PREF(use_video_activity, &use_video_activity_pref_);
+ INIT_PREF(enable_screen_lock, &enable_screen_lock_pref_);
+ INIT_PREF(presentation_idle_delay_factor,
+ &presentation_idle_delay_factor_pref_);
- prefs_were_set_ = got_prefs;
SendCurrentPolicy();
}
@@ -211,12 +127,47 @@ void PowerPolicyController::PowerManagerRestarted() {
SendCurrentPolicy();
}
+void PowerPolicyController::OnPrefChanged(const std::string& pref_name) {
+ SendCurrentPolicy();
+}
+
void PowerPolicyController::SendCurrentPolicy() {
- std::string reason;
+ power_manager::PowerManagementPolicy policy;
+
+ if (pref_service_) {
+ power_manager::PowerManagementPolicy::Delays* delays =
+ policy.mutable_ac_delays();
+ delays->set_screen_dim_ms(ac_screen_dim_delay_ms_pref_.GetValue());
+ delays->set_screen_off_ms(ac_screen_off_delay_ms_pref_.GetValue());
+ delays->set_screen_lock_ms(ac_screen_lock_delay_ms_pref_.GetValue());
+ delays->set_idle_warning_ms(ac_idle_warning_delay_ms_pref_.GetValue());
+ delays->set_idle_ms(ac_idle_delay_ms_pref_.GetValue());
+ if (enable_screen_lock_pref_.GetValue() && delays->screen_off_ms() > 0 &&
bartfab (slow) 2013/04/17 11:51:12 Nit: Add a comment that explains this.
Daniel Erat 2013/04/18 03:59:16 Done.
+ (delays->screen_lock_ms() <= 0 ||
+ delays->screen_off_ms() < delays->screen_lock_ms())) {
+ delays->set_screen_lock_ms(delays->screen_off_ms());
+ }
+
+ delays = policy.mutable_battery_delays();
+ delays->set_screen_dim_ms(battery_screen_dim_delay_ms_pref_.GetValue());
+ delays->set_screen_off_ms(battery_screen_off_delay_ms_pref_.GetValue());
+ delays->set_screen_lock_ms(battery_screen_lock_delay_ms_pref_.GetValue());
+ delays->set_idle_warning_ms(battery_idle_warning_delay_ms_pref_.GetValue());
+ delays->set_idle_ms(battery_idle_delay_ms_pref_.GetValue());
+ if (enable_screen_lock_pref_.GetValue() && delays->screen_off_ms() > 0 &&
bartfab (slow) 2013/04/17 11:51:12 Nit: Add a comment that explains this.
Daniel Erat 2013/04/18 03:59:16 skipped this since i added the earlier comment
+ (delays->screen_lock_ms() <= 0 ||
+ delays->screen_off_ms() < delays->screen_lock_ms())) {
+ delays->set_screen_lock_ms(delays->screen_off_ms());
+ }
- power_manager::PowerManagementPolicy policy = prefs_policy_;
- if (prefs_were_set_)
- reason = "Prefs";
+ policy.set_idle_action(GetProtoActionFromPref(idle_action_pref_));
+ policy.set_lid_closed_action(
+ GetProtoActionFromPref(lid_closed_action_pref_));
+ policy.set_use_audio_activity(use_audio_activity_pref_.GetValue());
+ policy.set_use_video_activity(use_video_activity_pref_.GetValue());
+ policy.set_presentation_idle_delay_factor(
+ presentation_idle_delay_factor_pref_.GetValue());
+ }
if (!screen_blocks_.empty()) {
policy.mutable_ac_delays()->set_screen_dim_ms(0);
@@ -226,12 +177,13 @@ void PowerPolicyController::SendCurrentPolicy() {
}
if ((!screen_blocks_.empty() || !suspend_blocks_.empty()) &&
- (!policy.has_idle_action() || policy.idle_action() ==
+ (policy.idle_action() ==
power_manager::PowerManagementPolicy_Action_SUSPEND)) {
policy.set_idle_action(
power_manager::PowerManagementPolicy_Action_DO_NOTHING);
}
+ std::string reason;
for (BlockMap::const_iterator it = screen_blocks_.begin();
it != screen_blocks_.end(); ++it) {
reason += (reason.empty() ? "" : ", ") + it->second;
@@ -243,6 +195,7 @@ void PowerPolicyController::SendCurrentPolicy() {
if (!reason.empty())
policy.set_reason(reason);
+
client_->SetPolicy(policy);
}

Powered by Google App Engine
This is Rietveld 408576698