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