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

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: fix observer registration and bool prefs 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
« no previous file with comments | « chromeos/dbus/power_policy_controller.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
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 "base/values.h"
9 #include "chromeos/dbus/dbus_thread_manager.h"
10
11 namespace chromeos {
12
13 namespace {
14
15 // If |pref|, a PrefServiceBase::Preference containing an integer, has been
16 // explicitly set to 0 or a positive value, assigns it to |proto_field|, a
17 // int32 field in |proto|, a google::protobuf::MessageLite*.
18 #define SET_DELAY_FROM_PREF(pref, proto_field, proto) \
19 { \
20 int value = GetIntPrefValue(pref); \
21 if (value >= 0) \
22 (proto)->set_##proto_field(value); \
23 }
24
25 // Similar to SET_DELAY_FROM_PREF() but sets a
26 // power_manager::PowerManagementPolicy_Action field instead.
27 #define SET_ACTION_FROM_PREF(pref, proto_field, proto) \
28 { \
29 int value = GetIntPrefValue(pref); \
30 if (value >= 0) { \
31 (proto)->set_##proto_field( \
32 static_cast<power_manager::PowerManagementPolicy_Action>(value)); \
33 } \
34 }
35
36 // If |pref|, a PrefServiceBase::Preference containing a bool, has been
37 // set, assigns it to |proto_field|, a bool field in |proto|, a
38 // google::protobuf::MessageLite*.
39 #define SET_BOOL_FROM_PREF(pref, proto_field, proto) \
40 if (!pref.IsDefaultValue()) { \
41 bool value = false; \
42 if (pref.GetValue()->GetAsBoolean(&value)) \
43 (proto)->set_##proto_field(value); \
44 else \
45 LOG(DFATAL) << pref.name() << " pref has non-boolean value"; \
46 }
47
48 // Returns a zero or positive integer value from |pref|. Returns -1 if the
49 // pref is unset and logs an error if it's set to a negative value.
50 int GetIntPrefValue(const PrefServiceBase::Preference& pref) {
51 if (pref.IsDefaultValue())
52 return -1;
53
54 int value = -1;
55 if (!pref.GetValue()->GetAsInteger(&value)) {
56 LOG(DFATAL) << pref.name() << " pref has non-integer value";
57 return -1;
58 }
59
60 if (value < 0)
61 LOG(WARNING) << pref.name() << " pref has negative value";
62 return value;
63 }
64
65 // Returns the power_manager::PowerManagementPolicy_Action value
66 // corresponding to |action|.
67 power_manager::PowerManagementPolicy_Action GetProtoAction(
68 PowerPolicyController::Action action) {
69 switch (action) {
70 case PowerPolicyController::ACTION_SUSPEND:
71 return power_manager::PowerManagementPolicy_Action_SUSPEND;
72 case PowerPolicyController::ACTION_STOP_SESSION:
73 return power_manager::PowerManagementPolicy_Action_STOP_SESSION;
74 case PowerPolicyController::ACTION_SHUT_DOWN:
75 return power_manager::PowerManagementPolicy_Action_SHUT_DOWN;
76 case PowerPolicyController::ACTION_DO_NOTHING:
77 return power_manager::PowerManagementPolicy_Action_DO_NOTHING;
78 default:
79 NOTREACHED() << "Unhandled action " << action;
80 return power_manager::PowerManagementPolicy_Action_DO_NOTHING;
81 }
82 }
83
84 } // namespace
85
86 PowerPolicyController::PowerPolicyController(DBusThreadManager* manager,
87 PowerManagerClient* client)
88 : manager_(manager),
89 client_(client) {
90 manager_->AddObserver(this);
91 client_->AddObserver(this);
92 SendEmptyPolicy();
93 }
94
95 PowerPolicyController::~PowerPolicyController() {
96 // The power manager's policy is reset before this point, in
97 // OnDBusThreadManagerDestroying(). At the time that
98 // PowerPolicyController is destroyed, PowerManagerClient's D-Bus proxy
99 // to the power manager is already gone.
100 client_->RemoveObserver(this);
101 client_ = NULL;
102 manager_->RemoveObserver(this);
103 manager_ = NULL;
104 }
105
106 void PowerPolicyController::UpdatePolicyFromPrefs(
107 const PrefServiceBase::Preference& ac_screen_dim_delay_ms_pref,
108 const PrefServiceBase::Preference& ac_screen_off_delay_ms_pref,
109 const PrefServiceBase::Preference& ac_screen_lock_delay_ms_pref,
110 const PrefServiceBase::Preference& ac_idle_delay_ms_pref,
111 const PrefServiceBase::Preference& battery_screen_dim_delay_ms_pref,
112 const PrefServiceBase::Preference& battery_screen_off_delay_ms_pref,
113 const PrefServiceBase::Preference& battery_screen_lock_delay_ms_pref,
114 const PrefServiceBase::Preference& battery_idle_delay_ms_pref,
115 const PrefServiceBase::Preference& idle_action_pref,
116 const PrefServiceBase::Preference& lid_closed_action_pref,
117 const PrefServiceBase::Preference& use_audio_activity_pref,
118 const PrefServiceBase::Preference& use_video_activity_pref,
119 const PrefServiceBase::Preference& presentation_idle_delay_factor_pref) {
120 prefs_policy_.Clear();
121
122 power_manager::PowerManagementPolicy::Delays* delays =
123 prefs_policy_.mutable_ac_delays();
124 SET_DELAY_FROM_PREF(ac_screen_dim_delay_ms_pref, screen_dim_ms, delays);
125 SET_DELAY_FROM_PREF(ac_screen_off_delay_ms_pref, screen_off_ms, delays);
126 SET_DELAY_FROM_PREF(ac_screen_lock_delay_ms_pref, screen_lock_ms, delays);
127 SET_DELAY_FROM_PREF(ac_idle_delay_ms_pref, idle_ms, delays);
128
129 delays = prefs_policy_.mutable_battery_delays();
130 SET_DELAY_FROM_PREF(battery_screen_dim_delay_ms_pref, screen_dim_ms, delays);
131 SET_DELAY_FROM_PREF(battery_screen_off_delay_ms_pref, screen_off_ms, delays);
132 SET_DELAY_FROM_PREF(
133 battery_screen_lock_delay_ms_pref, screen_lock_ms, delays);
134 SET_DELAY_FROM_PREF(battery_idle_delay_ms_pref, idle_ms, delays);
135
136 SET_ACTION_FROM_PREF(idle_action_pref, idle_action, &prefs_policy_);
137 SET_ACTION_FROM_PREF(
138 lid_closed_action_pref, lid_closed_action, &prefs_policy_);
139
140 SET_BOOL_FROM_PREF(
141 use_audio_activity_pref, use_audio_activity, &prefs_policy_);
142 SET_BOOL_FROM_PREF(
143 use_video_activity_pref, use_video_activity, &prefs_policy_);
144
145 if (!presentation_idle_delay_factor_pref.IsDefaultValue()) {
146 double value = 0.0;
147 if (presentation_idle_delay_factor_pref.GetValue()->GetAsDouble(&value)) {
148 prefs_policy_.set_presentation_idle_delay_factor(value);
149 } else {
150 LOG(DFATAL) << presentation_idle_delay_factor_pref.name()
151 << " pref has non-double value";
152 }
153 }
154
155 SendCurrentPolicy();
156 }
157
158 void PowerPolicyController::OnDBusThreadManagerDestroying(
159 DBusThreadManager* manager) {
160 DCHECK_EQ(manager, manager_);
161 SendEmptyPolicy();
162 }
163
164 void PowerPolicyController::PowerManagerRestarted() {
165 SendCurrentPolicy();
166 }
167
168 void PowerPolicyController::SendCurrentPolicy() {
169 // TODO(derat): Incorporate other information into the policy that is
170 // sent, e.g. from content::PowerSaveBlocker.
171 client_->SetPolicy(prefs_policy_);
172 }
173
174 void PowerPolicyController::SendEmptyPolicy() {
175 client_->SetPolicy(power_manager::PowerManagementPolicy());
176 }
177
178 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/power_policy_controller.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698