Index: chromeos/dbus/power_policy_controller.cc |
diff --git a/chromeos/dbus/power_policy_controller.cc b/chromeos/dbus/power_policy_controller.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..57808cfb25b76fd080f16d60fd2bc9f951a2d5f9 |
--- /dev/null |
+++ b/chromeos/dbus/power_policy_controller.cc |
@@ -0,0 +1,145 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chromeos/dbus/power_policy_controller.h" |
+ |
+#include "base/logging.h" |
+#include "base/values.h" |
+#include "chromeos/dbus/dbus_thread_manager.h" |
+#include "chromeos/dbus/power_manager_client.h" |
+ |
+namespace chromeos { |
+ |
+namespace { |
+ |
+// If |pref|, a PrefServiceBase::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*. |
+#define SET_INT_FROM_PREF(pref, proto_field, proto) \ |
+ if (int value = GetIntPrefValue(pref) >= 0) \ |
+ (proto)->set_##proto_field(value); |
+ |
+// Similar to SET_INT_FROM_PREF() but sets a |
+// power_manager::PowerManagementPolicy_Action field instead. |
+#define SET_ACTION_FROM_PREF(pref, proto_field, proto) \ |
+ if (int value = GetIntPrefValue(pref) >= 0) \ |
+ (proto)->set_##proto_field( \ |
+ static_cast<power_manager::PowerManagementPolicy_Action>(value)); |
+ |
+// If |pref|, a PrefServiceBase::Preference containing an bool, has been |
bartfab (slow)
2013/02/04 17:20:05
Nit: "a bool"
Daniel Erat
2013/02/04 17:31:59
Done.
|
+// set, assigns it to |proto_field|, a bool field in |proto|, a |
+// google::protobuf::MessageLite*. |
+#define SET_BOOL_FROM_PREF(pref, proto_field, proto) \ |
+ if (!pref.IsDefaultValue()) { \ |
+ bool value = false; \ |
+ if (pref.GetValue()->GetAsBoolean(&value)) \ |
+ (proto)->set_##proto_field(value); \ |
+ 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 PrefServiceBase::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; |
+} |
+ |
+// Returns the power_manager::PowerManagementPolicy_Action value |
+// corresponding to |action|. |
+power_manager::PowerManagementPolicy_Action GetProtoAction( |
+ PowerPolicyController::Action action) { |
+ switch (action) { |
+ case PowerPolicyController::ACTION_SUSPEND: |
+ return power_manager::PowerManagementPolicy_Action_SUSPEND; |
+ case PowerPolicyController::ACTION_STOP_SESSION: |
+ return power_manager::PowerManagementPolicy_Action_STOP_SESSION; |
+ case PowerPolicyController::ACTION_SHUT_DOWN: |
+ return power_manager::PowerManagementPolicy_Action_SHUT_DOWN; |
+ case PowerPolicyController::ACTION_DO_NOTHING: |
+ return power_manager::PowerManagementPolicy_Action_DO_NOTHING; |
+ default: |
+ NOTREACHED() << "Unhandled action " << action; |
+ return power_manager::PowerManagementPolicy_Action_DO_NOTHING; |
+ } |
+} |
+ |
+} // namespace |
+ |
+PowerPolicyController::PowerPolicyController(PowerManagerClient* client) |
+ : client_(client) { |
+ client_->SetPolicy(power_manager::PowerManagementPolicy()); |
+ ResetPolicy(); |
+} |
+ |
+PowerPolicyController::~PowerPolicyController() { |
+ client_->SetPolicy(power_manager::PowerManagementPolicy()); |
+ client_ = NULL; |
+} |
+ |
+void PowerPolicyController::UpdatePolicyFromPrefs( |
+ const PrefServiceBase::Preference& ac_screen_dim_delay_ms_pref, |
+ const PrefServiceBase::Preference& ac_screen_off_delay_ms_pref, |
+ const PrefServiceBase::Preference& ac_screen_lock_delay_ms_pref, |
+ const PrefServiceBase::Preference& ac_idle_delay_ms_pref, |
+ const PrefServiceBase::Preference& battery_screen_dim_delay_ms_pref, |
+ const PrefServiceBase::Preference& battery_screen_off_delay_ms_pref, |
+ const PrefServiceBase::Preference& battery_screen_lock_delay_ms_pref, |
+ const PrefServiceBase::Preference& battery_idle_delay_ms_pref, |
+ const PrefServiceBase::Preference& idle_action_pref, |
+ const PrefServiceBase::Preference& lid_closed_action_pref, |
+ const PrefServiceBase::Preference& use_audio_activity_pref, |
+ const PrefServiceBase::Preference& use_video_activity_pref, |
+ const PrefServiceBase::Preference& presentation_idle_delay_factor_pref) { |
+ prefs_policy_.Clear(); |
+ |
+ power_manager::PowerManagementPolicy::Delays* delays = |
+ prefs_policy_.mutable_ac_delays(); |
+ SET_DELAY_FROM_PREF(ac_screen_dim_delay_ms_pref, screen_dim_ms, delays); |
+ SET_DELAY_FROM_PREF(ac_screen_off_delay_ms_pref, screen_off_ms, delays); |
+ SET_DELAY_FROM_PREF(ac_screen_lock_delay_ms_pref, screen_lock_ms, delays); |
+ SET_DELAY_FROM_PREF(ac_idle_delay_ms_pref, idle_ms, delays); |
+ |
+ delays = prefs_policy_.mutable_battery_delays(); |
+ SET_DELAY_FROM_PREF(battery_screen_dim_delay_ms_pref, screen_dim_ms, delays); |
+ SET_DELAY_FROM_PREF(battery_screen_off_delay_ms_pref, screen_off_ms, delays); |
+ SET_DELAY_FROM_PREF( |
+ battery_screen_lock_delay_ms_pref, screen_lock_ms, delays); |
+ SET_DELAY_FROM_PREF(battery_idle_delay_ms_pref, idle_ms, delays); |
+ |
+ SET_ACTION_FROM_PREF(idle_action_pref, idle_action, &prefs_policy_); |
+ SET_ACTION_FROM_PREF( |
+ lid_closed_action_pref, lid_closed_action, &prefs_policy_); |
+ |
+ SET_BOOL_FROM_PREF( |
+ use_audio_activity_pref, use_audio_activity, &prefs_policy_); |
+ SET_BOOL_FROM_PREF( |
+ use_video_activity_pref, use_video_activity, &prefs_policy_); |
+ |
+ 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); |
+ } else { |
+ LOG(DFATAL) << presentation_idle_delay_factor_pref.name() |
+ << " pref has non-double value"; |
+ } |
+ } |
+ |
+ // TODO(derat): Incorporate other information into the policy that is |
+ // sent, e.g. from content::PowerSaveBlocker. |
+ client_->SetPolicy(prefs_policy_); |
+} |
+ |
+} // namespace chromeos |