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

Unified Diff: chromeos/dbus/power_policy_controller.cc

Issue 12186010: chromeos: Add power management policy prefs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 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 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
new file mode 100644
index 0000000000000000000000000000000000000000..fd478a252df747e7ec905812519c208c895a47be
--- /dev/null
+++ b/chromeos/dbus/power_policy_controller.cc
@@ -0,0 +1,115 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
bartfab (slow) 2013/02/04 10:04:35 I won't repeat the comments about magic constants
+// 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 "chromeos/dbus/dbus_thread_manager.h"
+#include "chromeos/dbus/power_manager/policy.pb.h"
+#include "chromeos/dbus/power_manager_client.h"
+
+namespace chromeos {
+
+namespace {
+
+// If |pref_field|, a member of |prefs_|, is a zero or positive delay,
+// assigns it to |proto_field|, a millisecond-based delay in |proto|, a
+// power_manager::PowerManagementPolicy::Delays message. Does nothing if
+// the pref contains a negative delay.
+#define SET_DELAY_FROM_PREFS(pref_field, proto_field, proto) \
+ if (prefs_.pref_field >= base::TimeDelta()) \
+ proto->set_##proto_field(prefs_.pref_field.InMilliseconds());
+
+// Returns the power_manager::PowerManagementPolicy_Action value
+// corresponding to |action|. Cannot be called with ACTION_UNSET.
+power_manager::PowerManagementPolicy_Action GetProtoAction(
+ PowerPolicyController::OptionalAction 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
+
+const int PowerPolicyController::kUnsetDelayMs = -1;
+const double PowerPolicyController::kUnsetPresentationIdleDelayFactor = 0.0;
+
+PowerPolicyController::PolicyPrefs::PolicyPrefs()
+ : ac_screen_dim_delay(base::TimeDelta::FromMilliseconds(kUnsetDelayMs)),
+ ac_screen_off_delay(base::TimeDelta::FromMilliseconds(kUnsetDelayMs)),
+ ac_screen_lock_delay(base::TimeDelta::FromMilliseconds(kUnsetDelayMs)),
+ ac_idle_delay(base::TimeDelta::FromMilliseconds(kUnsetDelayMs)),
+ battery_screen_dim_delay(
+ base::TimeDelta::FromMilliseconds(kUnsetDelayMs)),
+ battery_screen_off_delay(
+ base::TimeDelta::FromMilliseconds(kUnsetDelayMs)),
+ battery_screen_lock_delay(
+ base::TimeDelta::FromMilliseconds(kUnsetDelayMs)),
+ battery_idle_delay(base::TimeDelta::FromMilliseconds(kUnsetDelayMs)),
+ idle_action(ACTION_UNSET),
+ lid_closed_action(ACTION_UNSET),
+ use_audio_activity(BOOL_UNSET),
+ use_video_activity(BOOL_UNSET),
+ presentation_idle_delay_factor(kUnsetPresentationIdleDelayFactor) {
+}
+
+PowerPolicyController::PowerPolicyController(PowerManagerClient* client)
+ : client_(client) {
+ ResetPolicy();
+}
+
+PowerPolicyController::~PowerPolicyController() {
+ ResetPolicy();
+ client_ = NULL;
+}
+
+void PowerPolicyController::UpdatePolicyFromPrefs(const PolicyPrefs& prefs) {
+ prefs_ = prefs;
+ SendPolicy();
+}
+
+void PowerPolicyController::ResetPolicy() {
+ prefs_ = PolicyPrefs();
+ SendPolicy();
+}
+
+void PowerPolicyController::SendPolicy() {
+ power_manager::PowerManagementPolicy policy;
+
+ power_manager::PowerManagementPolicy::Delays* delays =
+ policy.mutable_ac_delays();
+ SET_DELAY_FROM_PREFS(ac_screen_dim_delay, screen_dim_ms, delays);
+ SET_DELAY_FROM_PREFS(ac_screen_off_delay, screen_off_ms, delays);
+ SET_DELAY_FROM_PREFS(ac_screen_lock_delay, screen_lock_ms, delays);
+ SET_DELAY_FROM_PREFS(ac_idle_delay, idle_ms, delays);
+
+ delays = policy.mutable_battery_delays();
+ SET_DELAY_FROM_PREFS(battery_screen_dim_delay, screen_dim_ms, delays);
+ SET_DELAY_FROM_PREFS(battery_screen_off_delay, screen_off_ms, delays);
+ SET_DELAY_FROM_PREFS(battery_screen_lock_delay, screen_lock_ms, delays);
+ SET_DELAY_FROM_PREFS(battery_idle_delay, idle_ms, delays);
+
+ if (prefs_.idle_action != ACTION_UNSET)
+ policy.set_idle_action(GetProtoAction(prefs_.idle_action));
+ if (prefs_.lid_closed_action != ACTION_UNSET)
+ policy.set_lid_closed_action(GetProtoAction(prefs_.lid_closed_action));
+ if (prefs_.presentation_idle_delay_factor >= 1.0) {
+ policy.set_presentation_idle_delay_factor(
+ prefs_.presentation_idle_delay_factor);
+ }
+
+ client_->SetPolicy(policy);
+}
+
+} // namespace chromeos
« chromeos/dbus/power_policy_controller.h ('K') | « chromeos/dbus/power_policy_controller.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698