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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // 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
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chromeos/dbus/power_policy_controller.h"
6
7 #include "base/logging.h"
8 #include "chromeos/dbus/dbus_thread_manager.h"
9 #include "chromeos/dbus/power_manager/policy.pb.h"
10 #include "chromeos/dbus/power_manager_client.h"
11
12 namespace chromeos {
13
14 namespace {
15
16 // If |pref_field|, a member of |prefs_|, is a zero or positive delay,
17 // assigns it to |proto_field|, a millisecond-based delay in |proto|, a
18 // power_manager::PowerManagementPolicy::Delays message. Does nothing if
19 // the pref contains a negative delay.
20 #define SET_DELAY_FROM_PREFS(pref_field, proto_field, proto) \
21 if (prefs_.pref_field >= base::TimeDelta()) \
22 proto->set_##proto_field(prefs_.pref_field.InMilliseconds());
23
24 // Returns the power_manager::PowerManagementPolicy_Action value
25 // corresponding to |action|. Cannot be called with ACTION_UNSET.
26 power_manager::PowerManagementPolicy_Action GetProtoAction(
27 PowerPolicyController::OptionalAction action) {
28 switch (action) {
29 case PowerPolicyController::ACTION_SUSPEND:
30 return power_manager::PowerManagementPolicy_Action_SUSPEND;
31 case PowerPolicyController::ACTION_STOP_SESSION:
32 return power_manager::PowerManagementPolicy_Action_STOP_SESSION;
33 case PowerPolicyController::ACTION_SHUT_DOWN:
34 return power_manager::PowerManagementPolicy_Action_SHUT_DOWN;
35 case PowerPolicyController::ACTION_DO_NOTHING:
36 return power_manager::PowerManagementPolicy_Action_DO_NOTHING;
37 default:
38 NOTREACHED() << "Unhandled action " << action;
39 return power_manager::PowerManagementPolicy_Action_DO_NOTHING;
40 }
41 }
42
43 } // namespace
44
45 const int PowerPolicyController::kUnsetDelayMs = -1;
46 const double PowerPolicyController::kUnsetPresentationIdleDelayFactor = 0.0;
47
48 PowerPolicyController::PolicyPrefs::PolicyPrefs()
49 : ac_screen_dim_delay(base::TimeDelta::FromMilliseconds(kUnsetDelayMs)),
50 ac_screen_off_delay(base::TimeDelta::FromMilliseconds(kUnsetDelayMs)),
51 ac_screen_lock_delay(base::TimeDelta::FromMilliseconds(kUnsetDelayMs)),
52 ac_idle_delay(base::TimeDelta::FromMilliseconds(kUnsetDelayMs)),
53 battery_screen_dim_delay(
54 base::TimeDelta::FromMilliseconds(kUnsetDelayMs)),
55 battery_screen_off_delay(
56 base::TimeDelta::FromMilliseconds(kUnsetDelayMs)),
57 battery_screen_lock_delay(
58 base::TimeDelta::FromMilliseconds(kUnsetDelayMs)),
59 battery_idle_delay(base::TimeDelta::FromMilliseconds(kUnsetDelayMs)),
60 idle_action(ACTION_UNSET),
61 lid_closed_action(ACTION_UNSET),
62 use_audio_activity(BOOL_UNSET),
63 use_video_activity(BOOL_UNSET),
64 presentation_idle_delay_factor(kUnsetPresentationIdleDelayFactor) {
65 }
66
67 PowerPolicyController::PowerPolicyController(PowerManagerClient* client)
68 : client_(client) {
69 ResetPolicy();
70 }
71
72 PowerPolicyController::~PowerPolicyController() {
73 ResetPolicy();
74 client_ = NULL;
75 }
76
77 void PowerPolicyController::UpdatePolicyFromPrefs(const PolicyPrefs& prefs) {
78 prefs_ = prefs;
79 SendPolicy();
80 }
81
82 void PowerPolicyController::ResetPolicy() {
83 prefs_ = PolicyPrefs();
84 SendPolicy();
85 }
86
87 void PowerPolicyController::SendPolicy() {
88 power_manager::PowerManagementPolicy policy;
89
90 power_manager::PowerManagementPolicy::Delays* delays =
91 policy.mutable_ac_delays();
92 SET_DELAY_FROM_PREFS(ac_screen_dim_delay, screen_dim_ms, delays);
93 SET_DELAY_FROM_PREFS(ac_screen_off_delay, screen_off_ms, delays);
94 SET_DELAY_FROM_PREFS(ac_screen_lock_delay, screen_lock_ms, delays);
95 SET_DELAY_FROM_PREFS(ac_idle_delay, idle_ms, delays);
96
97 delays = policy.mutable_battery_delays();
98 SET_DELAY_FROM_PREFS(battery_screen_dim_delay, screen_dim_ms, delays);
99 SET_DELAY_FROM_PREFS(battery_screen_off_delay, screen_off_ms, delays);
100 SET_DELAY_FROM_PREFS(battery_screen_lock_delay, screen_lock_ms, delays);
101 SET_DELAY_FROM_PREFS(battery_idle_delay, idle_ms, delays);
102
103 if (prefs_.idle_action != ACTION_UNSET)
104 policy.set_idle_action(GetProtoAction(prefs_.idle_action));
105 if (prefs_.lid_closed_action != ACTION_UNSET)
106 policy.set_lid_closed_action(GetProtoAction(prefs_.lid_closed_action));
107 if (prefs_.presentation_idle_delay_factor >= 1.0) {
108 policy.set_presentation_idle_delay_factor(
109 prefs_.presentation_idle_delay_factor);
110 }
111
112 client_->SetPolicy(policy);
113 }
114
115 } // namespace chromeos
OLDNEW
« 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