OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/dbus/power_policy_controller.h" | 5 #include "chromeos/dbus/power_policy_controller.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chromeos/dbus/dbus_thread_manager.h" | 9 #include "chromeos/dbus/dbus_thread_manager.h" |
10 | 10 |
11 namespace chromeos { | 11 namespace chromeos { |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // If |pref|, a PrefService::Preference containing an integer, has been | 15 // If |pref|, a PrefService::Preference containing an integer, has been |
16 // explicitly set to 0 or a positive value, assigns it to |proto_field|, a | 16 // explicitly set to 0 or a positive value, assigns it to |proto_field|, a |
17 // int32 field in |proto|, a google::protobuf::MessageLite*. | 17 // int32 field in |proto|, a google::protobuf::MessageLite*. If |proto| |
18 #define SET_DELAY_FROM_PREF(pref, proto_field, proto) \ | 18 // was updated, |got_prefs|, a bool*, is set to true; otherwise it is left |
| 19 // unchanged. |
| 20 #define SET_DELAY_FROM_PREF(pref, proto_field, proto, got_prefs) \ |
19 { \ | 21 { \ |
20 int value = GetIntPrefValue(pref); \ | 22 int value = GetIntPrefValue(pref); \ |
21 if (value >= 0) \ | 23 if (value >= 0) { \ |
22 (proto)->set_##proto_field(value); \ | 24 (proto)->set_##proto_field(value); \ |
| 25 *got_prefs = true; \ |
| 26 } \ |
23 } | 27 } |
24 | 28 |
25 // Similar to SET_DELAY_FROM_PREF() but sets a | 29 // Similar to SET_DELAY_FROM_PREF() but sets a |
26 // power_manager::PowerManagementPolicy_Action field instead. | 30 // power_manager::PowerManagementPolicy_Action field instead. |
27 #define SET_ACTION_FROM_PREF(pref, proto_field, proto) \ | 31 #define SET_ACTION_FROM_PREF(pref, proto_field, proto, got_prefs) \ |
28 { \ | 32 { \ |
29 int value = GetIntPrefValue(pref); \ | 33 int value = GetIntPrefValue(pref); \ |
30 if (value >= 0) { \ | 34 if (value >= 0) { \ |
31 (proto)->set_##proto_field( \ | 35 (proto)->set_##proto_field( \ |
32 static_cast<power_manager::PowerManagementPolicy_Action>(value)); \ | 36 static_cast<power_manager::PowerManagementPolicy_Action>(value)); \ |
| 37 *got_prefs = true; \ |
33 } \ | 38 } \ |
34 } | 39 } |
35 | 40 |
36 // If |pref|, a PrefService::Preference containing a bool, has been | 41 // If |pref|, a PrefService::Preference containing a bool, has been set, |
37 // set, assigns it to |proto_field|, a bool field in |proto|, a | 42 // assigns it to |proto_field|, a bool field in |proto|, a |
38 // google::protobuf::MessageLite*. | 43 // google::protobuf::MessageLite*. If |proto| was updated, |got_prefs|, a |
39 #define SET_BOOL_FROM_PREF(pref, proto_field, proto) \ | 44 // bool*, is set to true; otherwise it is left unchanged. |
| 45 #define SET_BOOL_FROM_PREF(pref, proto_field, proto, got_prefs) \ |
40 if (!pref.IsDefaultValue()) { \ | 46 if (!pref.IsDefaultValue()) { \ |
41 bool value = false; \ | 47 bool value = false; \ |
42 if (pref.GetValue()->GetAsBoolean(&value)) \ | 48 if (pref.GetValue()->GetAsBoolean(&value)) { \ |
43 (proto)->set_##proto_field(value); \ | 49 (proto)->set_##proto_field(value); \ |
44 else \ | 50 *got_prefs = true; \ |
| 51 } else { \ |
45 LOG(DFATAL) << pref.name() << " pref has non-boolean value"; \ | 52 LOG(DFATAL) << pref.name() << " pref has non-boolean value"; \ |
| 53 } \ |
46 } | 54 } |
47 | 55 |
48 // Returns a zero or positive integer value from |pref|. Returns -1 if the | 56 // 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. | 57 // pref is unset and logs an error if it's set to a negative value. |
50 int GetIntPrefValue(const PrefService::Preference& pref) { | 58 int GetIntPrefValue(const PrefService::Preference& pref) { |
51 if (pref.IsDefaultValue()) | 59 if (pref.IsDefaultValue()) |
52 return -1; | 60 return -1; |
53 | 61 |
54 int value = -1; | 62 int value = -1; |
55 if (!pref.GetValue()->GetAsInteger(&value)) { | 63 if (!pref.GetValue()->GetAsInteger(&value)) { |
(...skipping 23 matching lines...) Expand all Loading... |
79 NOTREACHED() << "Unhandled action " << action; | 87 NOTREACHED() << "Unhandled action " << action; |
80 return power_manager::PowerManagementPolicy_Action_DO_NOTHING; | 88 return power_manager::PowerManagementPolicy_Action_DO_NOTHING; |
81 } | 89 } |
82 } | 90 } |
83 | 91 |
84 } // namespace | 92 } // namespace |
85 | 93 |
86 PowerPolicyController::PowerPolicyController(DBusThreadManager* manager, | 94 PowerPolicyController::PowerPolicyController(DBusThreadManager* manager, |
87 PowerManagerClient* client) | 95 PowerManagerClient* client) |
88 : manager_(manager), | 96 : manager_(manager), |
89 client_(client) { | 97 client_(client), |
| 98 prefs_were_set_(false), |
| 99 next_block_id_(1) { |
90 manager_->AddObserver(this); | 100 manager_->AddObserver(this); |
91 client_->AddObserver(this); | 101 client_->AddObserver(this); |
92 SendEmptyPolicy(); | 102 SendEmptyPolicy(); |
93 } | 103 } |
94 | 104 |
95 PowerPolicyController::~PowerPolicyController() { | 105 PowerPolicyController::~PowerPolicyController() { |
96 // The power manager's policy is reset before this point, in | 106 // The power manager's policy is reset before this point, in |
97 // OnDBusThreadManagerDestroying(). At the time that | 107 // OnDBusThreadManagerDestroying(). At the time that |
98 // PowerPolicyController is destroyed, PowerManagerClient's D-Bus proxy | 108 // PowerPolicyController is destroyed, PowerManagerClient's D-Bus proxy |
99 // to the power manager is already gone. | 109 // to the power manager is already gone. |
(...skipping 13 matching lines...) Expand all Loading... |
113 const PrefService::Preference& battery_screen_off_delay_ms_pref, | 123 const PrefService::Preference& battery_screen_off_delay_ms_pref, |
114 const PrefService::Preference& battery_screen_lock_delay_ms_pref, | 124 const PrefService::Preference& battery_screen_lock_delay_ms_pref, |
115 const PrefService::Preference& battery_idle_warning_delay_ms_pref, | 125 const PrefService::Preference& battery_idle_warning_delay_ms_pref, |
116 const PrefService::Preference& battery_idle_delay_ms_pref, | 126 const PrefService::Preference& battery_idle_delay_ms_pref, |
117 const PrefService::Preference& idle_action_pref, | 127 const PrefService::Preference& idle_action_pref, |
118 const PrefService::Preference& lid_closed_action_pref, | 128 const PrefService::Preference& lid_closed_action_pref, |
119 const PrefService::Preference& use_audio_activity_pref, | 129 const PrefService::Preference& use_audio_activity_pref, |
120 const PrefService::Preference& use_video_activity_pref, | 130 const PrefService::Preference& use_video_activity_pref, |
121 const PrefService::Preference& presentation_idle_delay_factor_pref) { | 131 const PrefService::Preference& presentation_idle_delay_factor_pref) { |
122 prefs_policy_.Clear(); | 132 prefs_policy_.Clear(); |
| 133 bool got_prefs = false; |
123 | 134 |
124 power_manager::PowerManagementPolicy::Delays* delays = | 135 power_manager::PowerManagementPolicy::Delays* delays = |
125 prefs_policy_.mutable_ac_delays(); | 136 prefs_policy_.mutable_ac_delays(); |
126 SET_DELAY_FROM_PREF(ac_screen_dim_delay_ms_pref, screen_dim_ms, delays); | 137 SET_DELAY_FROM_PREF( |
127 SET_DELAY_FROM_PREF(ac_screen_off_delay_ms_pref, screen_off_ms, delays); | 138 ac_screen_dim_delay_ms_pref, screen_dim_ms, delays, &got_prefs); |
128 SET_DELAY_FROM_PREF(ac_screen_lock_delay_ms_pref, screen_lock_ms, delays); | 139 SET_DELAY_FROM_PREF( |
129 SET_DELAY_FROM_PREF(ac_idle_warning_delay_ms_pref, idle_warning_ms, delays); | 140 ac_screen_off_delay_ms_pref, screen_off_ms, delays, &got_prefs); |
130 SET_DELAY_FROM_PREF(ac_idle_delay_ms_pref, idle_ms, delays); | 141 SET_DELAY_FROM_PREF( |
| 142 ac_screen_lock_delay_ms_pref, screen_lock_ms, delays, &got_prefs); |
| 143 SET_DELAY_FROM_PREF( |
| 144 ac_idle_warning_delay_ms_pref, idle_warning_ms, delays, &got_prefs); |
| 145 SET_DELAY_FROM_PREF(ac_idle_delay_ms_pref, idle_ms, delays, &got_prefs); |
131 | 146 |
132 delays = prefs_policy_.mutable_battery_delays(); | 147 delays = prefs_policy_.mutable_battery_delays(); |
133 SET_DELAY_FROM_PREF(battery_screen_dim_delay_ms_pref, screen_dim_ms, delays); | |
134 SET_DELAY_FROM_PREF(battery_screen_off_delay_ms_pref, screen_off_ms, delays); | |
135 SET_DELAY_FROM_PREF( | 148 SET_DELAY_FROM_PREF( |
136 battery_screen_lock_delay_ms_pref, screen_lock_ms, delays); | 149 battery_screen_dim_delay_ms_pref, screen_dim_ms, delays, &got_prefs); |
137 SET_DELAY_FROM_PREF( | 150 SET_DELAY_FROM_PREF( |
138 battery_idle_warning_delay_ms_pref, idle_warning_ms, delays); | 151 battery_screen_off_delay_ms_pref, screen_off_ms, delays, &got_prefs); |
139 SET_DELAY_FROM_PREF(battery_idle_delay_ms_pref, idle_ms, delays); | 152 SET_DELAY_FROM_PREF( |
| 153 battery_screen_lock_delay_ms_pref, screen_lock_ms, delays, &got_prefs); |
| 154 SET_DELAY_FROM_PREF( |
| 155 battery_idle_warning_delay_ms_pref, idle_warning_ms, delays, &got_prefs); |
| 156 SET_DELAY_FROM_PREF(battery_idle_delay_ms_pref, idle_ms, delays, &got_prefs); |
140 | 157 |
141 SET_ACTION_FROM_PREF(idle_action_pref, idle_action, &prefs_policy_); | |
142 SET_ACTION_FROM_PREF( | 158 SET_ACTION_FROM_PREF( |
143 lid_closed_action_pref, lid_closed_action, &prefs_policy_); | 159 idle_action_pref, idle_action, &prefs_policy_, &got_prefs); |
| 160 SET_ACTION_FROM_PREF( |
| 161 lid_closed_action_pref, lid_closed_action, &prefs_policy_, &got_prefs); |
144 | 162 |
145 SET_BOOL_FROM_PREF( | 163 SET_BOOL_FROM_PREF( |
146 use_audio_activity_pref, use_audio_activity, &prefs_policy_); | 164 use_audio_activity_pref, use_audio_activity, &prefs_policy_, &got_prefs); |
147 SET_BOOL_FROM_PREF( | 165 SET_BOOL_FROM_PREF( |
148 use_video_activity_pref, use_video_activity, &prefs_policy_); | 166 use_video_activity_pref, use_video_activity, &prefs_policy_, &got_prefs); |
149 | 167 |
150 if (!presentation_idle_delay_factor_pref.IsDefaultValue()) { | 168 if (!presentation_idle_delay_factor_pref.IsDefaultValue()) { |
151 double value = 0.0; | 169 double value = 0.0; |
152 if (presentation_idle_delay_factor_pref.GetValue()->GetAsDouble(&value)) { | 170 if (presentation_idle_delay_factor_pref.GetValue()->GetAsDouble(&value)) { |
153 prefs_policy_.set_presentation_idle_delay_factor(value); | 171 prefs_policy_.set_presentation_idle_delay_factor(value); |
| 172 got_prefs = true; |
154 } else { | 173 } else { |
155 LOG(DFATAL) << presentation_idle_delay_factor_pref.name() | 174 LOG(DFATAL) << presentation_idle_delay_factor_pref.name() |
156 << " pref has non-double value"; | 175 << " pref has non-double value"; |
157 } | 176 } |
158 } | 177 } |
159 | 178 |
| 179 prefs_were_set_ = got_prefs; |
160 SendCurrentPolicy(); | 180 SendCurrentPolicy(); |
161 } | 181 } |
162 | 182 |
| 183 int PowerPolicyController::AddScreenBlock(const std::string& reason) { |
| 184 int id = next_block_id_++; |
| 185 screen_blocks_[id] = reason; |
| 186 SendCurrentPolicy(); |
| 187 return id; |
| 188 } |
| 189 |
| 190 int PowerPolicyController::AddSuspendBlock(const std::string& reason) { |
| 191 int id = next_block_id_++; |
| 192 suspend_blocks_[id] = reason; |
| 193 SendCurrentPolicy(); |
| 194 return id; |
| 195 } |
| 196 |
| 197 void PowerPolicyController::RemoveBlock(int id) { |
| 198 if (!screen_blocks_.erase(id) && !suspend_blocks_.erase(id)) |
| 199 LOG(WARNING) << "Ignoring request to remove nonexistent block " << id; |
| 200 else |
| 201 SendCurrentPolicy(); |
| 202 } |
| 203 |
163 void PowerPolicyController::OnDBusThreadManagerDestroying( | 204 void PowerPolicyController::OnDBusThreadManagerDestroying( |
164 DBusThreadManager* manager) { | 205 DBusThreadManager* manager) { |
165 DCHECK_EQ(manager, manager_); | 206 DCHECK_EQ(manager, manager_); |
166 SendEmptyPolicy(); | 207 SendEmptyPolicy(); |
167 } | 208 } |
168 | 209 |
169 void PowerPolicyController::PowerManagerRestarted() { | 210 void PowerPolicyController::PowerManagerRestarted() { |
170 SendCurrentPolicy(); | 211 SendCurrentPolicy(); |
171 } | 212 } |
172 | 213 |
173 void PowerPolicyController::SendCurrentPolicy() { | 214 void PowerPolicyController::SendCurrentPolicy() { |
174 // TODO(derat): Incorporate other information into the policy that is | 215 std::string reason; |
175 // sent, e.g. from content::PowerSaveBlocker. | 216 |
176 client_->SetPolicy(prefs_policy_); | 217 power_manager::PowerManagementPolicy policy = prefs_policy_; |
| 218 if (prefs_were_set_) |
| 219 reason = "Prefs"; |
| 220 |
| 221 if (!screen_blocks_.empty()) { |
| 222 policy.mutable_ac_delays()->set_screen_dim_ms(0); |
| 223 policy.mutable_ac_delays()->set_screen_off_ms(0); |
| 224 policy.mutable_battery_delays()->set_screen_dim_ms(0); |
| 225 policy.mutable_battery_delays()->set_screen_off_ms(0); |
| 226 } |
| 227 |
| 228 if ((!screen_blocks_.empty() || !suspend_blocks_.empty()) && |
| 229 (!policy.has_idle_action() || policy.idle_action() == |
| 230 power_manager::PowerManagementPolicy_Action_SUSPEND)) { |
| 231 policy.set_idle_action( |
| 232 power_manager::PowerManagementPolicy_Action_DO_NOTHING); |
| 233 } |
| 234 |
| 235 for (BlockMap::const_iterator it = screen_blocks_.begin(); |
| 236 it != screen_blocks_.end(); ++it) { |
| 237 reason += (reason.empty() ? "" : ", ") + it->second; |
| 238 } |
| 239 for (BlockMap::const_iterator it = suspend_blocks_.begin(); |
| 240 it != suspend_blocks_.end(); ++it) { |
| 241 reason += (reason.empty() ? "" : ", ") + it->second; |
| 242 } |
| 243 |
| 244 if (!reason.empty()) |
| 245 policy.set_reason(reason); |
| 246 client_->SetPolicy(policy); |
177 } | 247 } |
178 | 248 |
179 void PowerPolicyController::SendEmptyPolicy() { | 249 void PowerPolicyController::SendEmptyPolicy() { |
180 client_->SetPolicy(power_manager::PowerManagementPolicy()); | 250 client_->SetPolicy(power_manager::PowerManagementPolicy()); |
181 } | 251 } |
182 | 252 |
183 } // namespace chromeos | 253 } // namespace chromeos |
OLD | NEW |