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" |
|
bartfab (slow)
2013/04/18 14:17:22
Nit: No longer used.
Daniel Erat
2013/04/18 14:37:10
Done.
| |
| 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 | |
| 16 // explicitly set to 0 or a positive value, assigns it to |proto_field|, a | |
| 17 // int32 field in |proto|, a google::protobuf::MessageLite*. If |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) \ | |
| 21 { \ | |
| 22 int value = GetIntPrefValue(pref); \ | |
| 23 if (value >= 0) { \ | |
| 24 (proto)->set_##proto_field(value); \ | |
| 25 *got_prefs = true; \ | |
| 26 } \ | |
| 27 } | |
| 28 | |
| 29 // Similar to SET_DELAY_FROM_PREF() but sets a | |
| 30 // power_manager::PowerManagementPolicy_Action field instead. | |
| 31 #define SET_ACTION_FROM_PREF(pref, proto_field, proto, got_prefs) \ | |
| 32 { \ | |
| 33 int value = GetIntPrefValue(pref); \ | |
| 34 if (value >= 0) { \ | |
| 35 (proto)->set_##proto_field( \ | |
| 36 static_cast<power_manager::PowerManagementPolicy_Action>(value)); \ | |
| 37 *got_prefs = true; \ | |
| 38 } \ | |
| 39 } | |
| 40 | |
| 41 // If |pref|, a PrefService::Preference containing a bool, has been set, | |
| 42 // assigns it to |proto_field|, a bool field in |proto|, a | |
| 43 // google::protobuf::MessageLite*. If |proto| was updated, |got_prefs|, a | |
| 44 // bool*, is set to true; otherwise it is left unchanged. | |
| 45 #define SET_BOOL_FROM_PREF(pref, proto_field, proto, got_prefs) \ | |
| 46 if (!pref.IsDefaultValue()) { \ | |
| 47 bool value = false; \ | |
| 48 if (pref.GetValue()->GetAsBoolean(&value)) { \ | |
| 49 (proto)->set_##proto_field(value); \ | |
| 50 *got_prefs = true; \ | |
| 51 } else { \ | |
| 52 LOG(DFATAL) << pref.name() << " pref has non-boolean value"; \ | |
| 53 } \ | |
| 54 } | |
| 55 | |
| 56 // Returns a zero or positive integer value from |pref|. Returns -1 if the | |
| 57 // pref is unset and logs an error if it's set to a negative value. | |
| 58 int GetIntPrefValue(const PrefService::Preference& pref) { | |
| 59 if (pref.IsDefaultValue()) | |
| 60 return -1; | |
| 61 | |
| 62 int value = -1; | |
| 63 if (!pref.GetValue()->GetAsInteger(&value)) { | |
| 64 LOG(DFATAL) << pref.name() << " pref has non-integer value"; | |
| 65 return -1; | |
| 66 } | |
| 67 | |
| 68 if (value < 0) | |
| 69 LOG(WARNING) << pref.name() << " pref has negative value"; | |
| 70 return value; | |
| 71 } | |
| 72 | |
| 73 // Returns the power_manager::PowerManagementPolicy_Action value | 15 // Returns the power_manager::PowerManagementPolicy_Action value |
| 74 // corresponding to |action|. | 16 // corresponding to |action|. |
| 75 power_manager::PowerManagementPolicy_Action GetProtoAction( | 17 power_manager::PowerManagementPolicy_Action GetProtoAction( |
| 76 PowerPolicyController::Action action) { | 18 PowerPolicyController::Action action) { |
| 77 switch (action) { | 19 switch (action) { |
| 78 case PowerPolicyController::ACTION_SUSPEND: | 20 case PowerPolicyController::ACTION_SUSPEND: |
| 79 return power_manager::PowerManagementPolicy_Action_SUSPEND; | 21 return power_manager::PowerManagementPolicy_Action_SUSPEND; |
| 80 case PowerPolicyController::ACTION_STOP_SESSION: | 22 case PowerPolicyController::ACTION_STOP_SESSION: |
| 81 return power_manager::PowerManagementPolicy_Action_STOP_SESSION; | 23 return power_manager::PowerManagementPolicy_Action_STOP_SESSION; |
| 82 case PowerPolicyController::ACTION_SHUT_DOWN: | 24 case PowerPolicyController::ACTION_SHUT_DOWN: |
| 83 return power_manager::PowerManagementPolicy_Action_SHUT_DOWN; | 25 return power_manager::PowerManagementPolicy_Action_SHUT_DOWN; |
| 84 case PowerPolicyController::ACTION_DO_NOTHING: | 26 case PowerPolicyController::ACTION_DO_NOTHING: |
| 85 return power_manager::PowerManagementPolicy_Action_DO_NOTHING; | 27 return power_manager::PowerManagementPolicy_Action_DO_NOTHING; |
| 86 default: | 28 default: |
| 87 NOTREACHED() << "Unhandled action " << action; | 29 NOTREACHED() << "Unhandled action " << action; |
| 88 return power_manager::PowerManagementPolicy_Action_DO_NOTHING; | 30 return power_manager::PowerManagementPolicy_Action_DO_NOTHING; |
| 89 } | 31 } |
| 90 } | 32 } |
| 91 | 33 |
| 92 } // namespace | 34 } // namespace |
| 93 | 35 |
| 94 PowerPolicyController::PowerPolicyController(DBusThreadManager* manager, | 36 PowerPolicyController::PowerPolicyController(DBusThreadManager* manager, |
| 95 PowerManagerClient* client) | 37 PowerManagerClient* client) |
| 96 : manager_(manager), | 38 : manager_(manager), |
| 97 client_(client), | 39 client_(client), |
| 98 prefs_were_set_(false), | 40 prefs_were_set_(false), |
| 99 next_block_id_(1) { | 41 next_block_id_(1) { |
| 100 manager_->AddObserver(this); | 42 manager_->AddObserver(this); |
| 101 client_->AddObserver(this); | 43 client_->AddObserver(this); |
| 102 SendEmptyPolicy(); | 44 SendCurrentPolicy(); |
| 103 } | 45 } |
| 104 | 46 |
| 105 PowerPolicyController::~PowerPolicyController() { | 47 PowerPolicyController::~PowerPolicyController() { |
| 106 // The power manager's policy is reset before this point, in | 48 // The power manager's policy is reset before this point, in |
| 107 // OnDBusThreadManagerDestroying(). At the time that | 49 // OnDBusThreadManagerDestroying(). At the time that |
| 108 // PowerPolicyController is destroyed, PowerManagerClient's D-Bus proxy | 50 // PowerPolicyController is destroyed, PowerManagerClient's D-Bus proxy |
| 109 // to the power manager is already gone. | 51 // to the power manager is already gone. |
| 110 client_->RemoveObserver(this); | 52 client_->RemoveObserver(this); |
| 111 client_ = NULL; | 53 client_ = NULL; |
| 112 manager_->RemoveObserver(this); | 54 manager_->RemoveObserver(this); |
| 113 manager_ = NULL; | 55 manager_ = NULL; |
| 114 } | 56 } |
| 115 | 57 |
| 116 void PowerPolicyController::UpdatePolicyFromPrefs( | 58 void PowerPolicyController::ApplyPrefs(const PrefValues& values) { |
| 117 const PrefService::Preference& ac_screen_dim_delay_ms_pref, | |
| 118 const PrefService::Preference& ac_screen_off_delay_ms_pref, | |
| 119 const PrefService::Preference& ac_screen_lock_delay_ms_pref, | |
| 120 const PrefService::Preference& ac_idle_warning_delay_ms_pref, | |
| 121 const PrefService::Preference& ac_idle_delay_ms_pref, | |
| 122 const PrefService::Preference& battery_screen_dim_delay_ms_pref, | |
| 123 const PrefService::Preference& battery_screen_off_delay_ms_pref, | |
| 124 const PrefService::Preference& battery_screen_lock_delay_ms_pref, | |
| 125 const PrefService::Preference& battery_idle_warning_delay_ms_pref, | |
| 126 const PrefService::Preference& battery_idle_delay_ms_pref, | |
| 127 const PrefService::Preference& idle_action_pref, | |
| 128 const PrefService::Preference& lid_closed_action_pref, | |
| 129 const PrefService::Preference& use_audio_activity_pref, | |
| 130 const PrefService::Preference& use_video_activity_pref, | |
| 131 const PrefService::Preference& presentation_idle_delay_factor_pref) { | |
| 132 prefs_policy_.Clear(); | 59 prefs_policy_.Clear(); |
| 133 bool got_prefs = false; | |
| 134 | 60 |
| 135 power_manager::PowerManagementPolicy::Delays* delays = | 61 power_manager::PowerManagementPolicy::Delays* delays = |
| 136 prefs_policy_.mutable_ac_delays(); | 62 prefs_policy_.mutable_ac_delays(); |
| 137 SET_DELAY_FROM_PREF( | 63 delays->set_screen_dim_ms(values.ac_screen_dim_delay_ms); |
| 138 ac_screen_dim_delay_ms_pref, screen_dim_ms, delays, &got_prefs); | 64 delays->set_screen_off_ms(values.ac_screen_off_delay_ms); |
| 139 SET_DELAY_FROM_PREF( | 65 delays->set_screen_lock_ms(values.ac_screen_lock_delay_ms); |
| 140 ac_screen_off_delay_ms_pref, screen_off_ms, delays, &got_prefs); | 66 delays->set_idle_warning_ms(values.ac_idle_warning_delay_ms); |
| 141 SET_DELAY_FROM_PREF( | 67 delays->set_idle_ms(values.ac_idle_delay_ms); |
| 142 ac_screen_lock_delay_ms_pref, screen_lock_ms, delays, &got_prefs); | 68 |
| 143 SET_DELAY_FROM_PREF( | 69 // If screen-locking is enabled, ensure that the screen is locked when |
| 144 ac_idle_warning_delay_ms_pref, idle_warning_ms, delays, &got_prefs); | 70 // it's turned off due to user inactivity. |
| 145 SET_DELAY_FROM_PREF(ac_idle_delay_ms_pref, idle_ms, delays, &got_prefs); | 71 if (values.enable_screen_lock && delays->screen_off_ms() > 0 && |
| 72 (delays->screen_lock_ms() <= 0 || | |
| 73 delays->screen_off_ms() < delays->screen_lock_ms())) { | |
| 74 delays->set_screen_lock_ms(delays->screen_off_ms()); | |
| 75 } | |
| 146 | 76 |
| 147 delays = prefs_policy_.mutable_battery_delays(); | 77 delays = prefs_policy_.mutable_battery_delays(); |
| 148 SET_DELAY_FROM_PREF( | 78 delays->set_screen_dim_ms(values.battery_screen_dim_delay_ms); |
| 149 battery_screen_dim_delay_ms_pref, screen_dim_ms, delays, &got_prefs); | 79 delays->set_screen_off_ms(values.battery_screen_off_delay_ms); |
| 150 SET_DELAY_FROM_PREF( | 80 delays->set_screen_lock_ms(values.battery_screen_lock_delay_ms); |
| 151 battery_screen_off_delay_ms_pref, screen_off_ms, delays, &got_prefs); | 81 delays->set_idle_warning_ms(values.battery_idle_warning_delay_ms); |
| 152 SET_DELAY_FROM_PREF( | 82 delays->set_idle_ms(values.battery_idle_delay_ms); |
| 153 battery_screen_lock_delay_ms_pref, screen_lock_ms, delays, &got_prefs); | 83 if (values.enable_screen_lock && delays->screen_off_ms() > 0 && |
| 154 SET_DELAY_FROM_PREF( | 84 (delays->screen_lock_ms() <= 0 || |
| 155 battery_idle_warning_delay_ms_pref, idle_warning_ms, delays, &got_prefs); | 85 delays->screen_off_ms() < delays->screen_lock_ms())) { |
| 156 SET_DELAY_FROM_PREF(battery_idle_delay_ms_pref, idle_ms, delays, &got_prefs); | 86 delays->set_screen_lock_ms(delays->screen_off_ms()); |
| 157 | |
| 158 SET_ACTION_FROM_PREF( | |
| 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); | |
| 162 | |
| 163 SET_BOOL_FROM_PREF( | |
| 164 use_audio_activity_pref, use_audio_activity, &prefs_policy_, &got_prefs); | |
| 165 SET_BOOL_FROM_PREF( | |
| 166 use_video_activity_pref, use_video_activity, &prefs_policy_, &got_prefs); | |
| 167 | |
| 168 if (!presentation_idle_delay_factor_pref.IsDefaultValue()) { | |
| 169 double value = 0.0; | |
| 170 if (presentation_idle_delay_factor_pref.GetValue()->GetAsDouble(&value)) { | |
| 171 prefs_policy_.set_presentation_idle_delay_factor(value); | |
| 172 got_prefs = true; | |
| 173 } else { | |
| 174 LOG(DFATAL) << presentation_idle_delay_factor_pref.name() | |
| 175 << " pref has non-double value"; | |
| 176 } | |
| 177 } | 87 } |
| 178 | 88 |
| 179 prefs_were_set_ = got_prefs; | 89 prefs_policy_.set_idle_action(GetProtoAction(values.idle_action)); |
| 90 prefs_policy_.set_lid_closed_action(GetProtoAction(values.lid_closed_action)); | |
| 91 prefs_policy_.set_use_audio_activity(values.use_audio_activity); | |
| 92 prefs_policy_.set_use_video_activity(values.use_video_activity); | |
| 93 prefs_policy_.set_presentation_idle_delay_factor( | |
| 94 values.presentation_idle_delay_factor); | |
| 95 | |
| 96 prefs_were_set_ = true; | |
| 180 SendCurrentPolicy(); | 97 SendCurrentPolicy(); |
| 181 } | 98 } |
| 182 | 99 |
| 183 int PowerPolicyController::AddScreenBlock(const std::string& reason) { | 100 int PowerPolicyController::AddScreenBlock(const std::string& reason) { |
| 184 int id = next_block_id_++; | 101 int id = next_block_id_++; |
| 185 screen_blocks_[id] = reason; | 102 screen_blocks_[id] = reason; |
| 186 SendCurrentPolicy(); | 103 SendCurrentPolicy(); |
| 187 return id; | 104 return id; |
| 188 } | 105 } |
| 189 | 106 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 244 if (!reason.empty()) | 161 if (!reason.empty()) |
| 245 policy.set_reason(reason); | 162 policy.set_reason(reason); |
| 246 client_->SetPolicy(policy); | 163 client_->SetPolicy(policy); |
| 247 } | 164 } |
| 248 | 165 |
| 249 void PowerPolicyController::SendEmptyPolicy() { | 166 void PowerPolicyController::SendEmptyPolicy() { |
| 250 client_->SetPolicy(power_manager::PowerManagementPolicy()); | 167 client_->SetPolicy(power_manager::PowerManagementPolicy()); |
| 251 } | 168 } |
| 252 | 169 |
| 253 } // namespace chromeos | 170 } // namespace chromeos |
| OLD | NEW |