| 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/format_macros.h" |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/values.h" | 9 #include "base/string_util.h" |
| 10 #include "base/stringprintf.h" |
| 9 #include "chromeos/dbus/dbus_thread_manager.h" | 11 #include "chromeos/dbus/dbus_thread_manager.h" |
| 10 | 12 |
| 11 namespace chromeos { | 13 namespace chromeos { |
| 12 | 14 |
| 13 namespace { | 15 namespace { |
| 14 | 16 |
| 15 // If |pref|, a PrefService::Preference containing an integer, has been | 17 // Appends a description of |field|, a field within |delays|, a |
| 16 // explicitly set to 0 or a positive value, assigns it to |proto_field|, a | 18 // power_manager::PowerManagementPolicy::Delays object, to |str|, an |
| 17 // int32 field in |proto|, a google::protobuf::MessageLite*. If |proto| | 19 // std::string, if the field is set. |name| is a char* describing the |
| 18 // was updated, |got_prefs|, a bool*, is set to true; otherwise it is left | 20 // field. |
| 19 // unchanged. | 21 #define APPEND_DELAY(str, delays, field, name) \ |
| 20 #define SET_DELAY_FROM_PREF(pref, proto_field, proto, got_prefs) \ | |
| 21 { \ | 22 { \ |
| 22 int value = GetIntPrefValue(pref); \ | 23 if (delays.has_##field()) \ |
| 23 if (value >= 0) { \ | 24 str += base::StringPrintf(name "=%" PRId64 " ", delays.field()); \ |
| 24 (proto)->set_##proto_field(value); \ | |
| 25 *got_prefs = true; \ | |
| 26 } \ | |
| 27 } | 25 } |
| 28 | 26 |
| 29 // Similar to SET_DELAY_FROM_PREF() but sets a | 27 // Appends descriptions of all of the set delays in |delays|, a |
| 30 // power_manager::PowerManagementPolicy_Action field instead. | 28 // power_manager::PowerManagementPolicy::Delays object, to |str|, an |
| 31 #define SET_ACTION_FROM_PREF(pref, proto_field, proto, got_prefs) \ | 29 // std::string. |prefix| should be a char* containing either "ac" or |
| 30 // "battery". |
| 31 #define APPEND_DELAYS(str, delays, prefix) \ |
| 32 { \ | 32 { \ |
| 33 int value = GetIntPrefValue(pref); \ | 33 APPEND_DELAY(str, delays, screen_dim_ms, prefix "_screen_dim_ms"); \ |
| 34 if (value >= 0) { \ | 34 APPEND_DELAY(str, delays, screen_off_ms, prefix "_screen_off_ms"); \ |
| 35 (proto)->set_##proto_field( \ | 35 APPEND_DELAY(str, delays, screen_lock_ms, prefix "_screen_lock_ms"); \ |
| 36 static_cast<power_manager::PowerManagementPolicy_Action>(value)); \ | 36 APPEND_DELAY(str, delays, idle_warning_ms, prefix "_idle_warning_ms"); \ |
| 37 *got_prefs = true; \ | 37 APPEND_DELAY(str, delays, idle_ms, prefix "_idle_ms"); \ |
| 38 } \ | |
| 39 } | 38 } |
| 40 | 39 |
| 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 | 40 // Returns the power_manager::PowerManagementPolicy_Action value |
| 74 // corresponding to |action|. | 41 // corresponding to |action|. |
| 75 power_manager::PowerManagementPolicy_Action GetProtoAction( | 42 power_manager::PowerManagementPolicy_Action GetProtoAction( |
| 76 PowerPolicyController::Action action) { | 43 PowerPolicyController::Action action) { |
| 77 switch (action) { | 44 switch (action) { |
| 78 case PowerPolicyController::ACTION_SUSPEND: | 45 case PowerPolicyController::ACTION_SUSPEND: |
| 79 return power_manager::PowerManagementPolicy_Action_SUSPEND; | 46 return power_manager::PowerManagementPolicy_Action_SUSPEND; |
| 80 case PowerPolicyController::ACTION_STOP_SESSION: | 47 case PowerPolicyController::ACTION_STOP_SESSION: |
| 81 return power_manager::PowerManagementPolicy_Action_STOP_SESSION; | 48 return power_manager::PowerManagementPolicy_Action_STOP_SESSION; |
| 82 case PowerPolicyController::ACTION_SHUT_DOWN: | 49 case PowerPolicyController::ACTION_SHUT_DOWN: |
| 83 return power_manager::PowerManagementPolicy_Action_SHUT_DOWN; | 50 return power_manager::PowerManagementPolicy_Action_SHUT_DOWN; |
| 84 case PowerPolicyController::ACTION_DO_NOTHING: | 51 case PowerPolicyController::ACTION_DO_NOTHING: |
| 85 return power_manager::PowerManagementPolicy_Action_DO_NOTHING; | 52 return power_manager::PowerManagementPolicy_Action_DO_NOTHING; |
| 86 default: | 53 default: |
| 87 NOTREACHED() << "Unhandled action " << action; | 54 NOTREACHED() << "Unhandled action " << action; |
| 88 return power_manager::PowerManagementPolicy_Action_DO_NOTHING; | 55 return power_manager::PowerManagementPolicy_Action_DO_NOTHING; |
| 89 } | 56 } |
| 90 } | 57 } |
| 91 | 58 |
| 92 } // namespace | 59 } // namespace |
| 93 | 60 |
| 61 // -1 is interpreted as "unset" by powerd, resulting in powerd's default |
| 62 // delays being used instead. There are no similarly-interpreted values |
| 63 // for the other fields, unfortunately (but the constructor-assigned values |
| 64 // will only reach powerd if Chrome messes up and forgets to override them |
| 65 // with the pref-assigned values). |
| 66 PowerPolicyController::PrefValues::PrefValues() |
| 67 : ac_screen_dim_delay_ms(-1), |
| 68 ac_screen_off_delay_ms(-1), |
| 69 ac_screen_lock_delay_ms(-1), |
| 70 ac_idle_warning_delay_ms(-1), |
| 71 ac_idle_delay_ms(-1), |
| 72 battery_screen_dim_delay_ms(-1), |
| 73 battery_screen_off_delay_ms(-1), |
| 74 battery_screen_lock_delay_ms(-1), |
| 75 battery_idle_warning_delay_ms(-1), |
| 76 battery_idle_delay_ms(-1), |
| 77 idle_action(ACTION_SUSPEND), |
| 78 lid_closed_action(ACTION_SUSPEND), |
| 79 use_audio_activity(true), |
| 80 use_video_activity(true), |
| 81 enable_screen_lock(false), |
| 82 presentation_idle_delay_factor(2.0) {} |
| 83 |
| 84 // static |
| 85 std::string PowerPolicyController::GetPolicyDebugString( |
| 86 const power_manager::PowerManagementPolicy& policy) { |
| 87 std::string str; |
| 88 if (policy.has_ac_delays()) |
| 89 APPEND_DELAYS(str, policy.ac_delays(), "ac"); |
| 90 if (policy.has_battery_delays()) |
| 91 APPEND_DELAYS(str, policy.battery_delays(), "battery"); |
| 92 if (policy.has_idle_action()) |
| 93 str += base::StringPrintf("idle=%d ", policy.idle_action()); |
| 94 if (policy.has_lid_closed_action()) |
| 95 str += base::StringPrintf("lid_closed=%d ", policy.lid_closed_action()); |
| 96 if (policy.has_use_audio_activity()) |
| 97 str += base::StringPrintf("use_audio=%d ", policy.use_audio_activity()); |
| 98 if (policy.has_use_video_activity()) |
| 99 str += base::StringPrintf("use_video=%d ", policy.use_audio_activity()); |
| 100 if (policy.has_presentation_idle_delay_factor()) { |
| 101 str += base::StringPrintf("presentation_idle_delay_factor=%f ", |
| 102 policy.presentation_idle_delay_factor()); |
| 103 } |
| 104 if (policy.has_reason()) |
| 105 str += base::StringPrintf("reason=\"%s\" ", policy.reason().c_str()); |
| 106 TrimWhitespace(str, TRIM_TRAILING, &str); |
| 107 return str; |
| 108 } |
| 109 |
| 94 PowerPolicyController::PowerPolicyController(DBusThreadManager* manager, | 110 PowerPolicyController::PowerPolicyController(DBusThreadManager* manager, |
| 95 PowerManagerClient* client) | 111 PowerManagerClient* client) |
| 96 : manager_(manager), | 112 : manager_(manager), |
| 97 client_(client), | 113 client_(client), |
| 98 prefs_were_set_(false), | 114 prefs_were_set_(false), |
| 99 next_block_id_(1) { | 115 next_block_id_(1) { |
| 100 manager_->AddObserver(this); | 116 manager_->AddObserver(this); |
| 101 client_->AddObserver(this); | 117 client_->AddObserver(this); |
| 102 SendEmptyPolicy(); | 118 SendCurrentPolicy(); |
| 103 } | 119 } |
| 104 | 120 |
| 105 PowerPolicyController::~PowerPolicyController() { | 121 PowerPolicyController::~PowerPolicyController() { |
| 106 // The power manager's policy is reset before this point, in | 122 // The power manager's policy is reset before this point, in |
| 107 // OnDBusThreadManagerDestroying(). At the time that | 123 // OnDBusThreadManagerDestroying(). At the time that |
| 108 // PowerPolicyController is destroyed, PowerManagerClient's D-Bus proxy | 124 // PowerPolicyController is destroyed, PowerManagerClient's D-Bus proxy |
| 109 // to the power manager is already gone. | 125 // to the power manager is already gone. |
| 110 client_->RemoveObserver(this); | 126 client_->RemoveObserver(this); |
| 111 client_ = NULL; | 127 client_ = NULL; |
| 112 manager_->RemoveObserver(this); | 128 manager_->RemoveObserver(this); |
| 113 manager_ = NULL; | 129 manager_ = NULL; |
| 114 } | 130 } |
| 115 | 131 |
| 116 void PowerPolicyController::UpdatePolicyFromPrefs( | 132 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(); | 133 prefs_policy_.Clear(); |
| 133 bool got_prefs = false; | |
| 134 | 134 |
| 135 power_manager::PowerManagementPolicy::Delays* delays = | 135 power_manager::PowerManagementPolicy::Delays* delays = |
| 136 prefs_policy_.mutable_ac_delays(); | 136 prefs_policy_.mutable_ac_delays(); |
| 137 SET_DELAY_FROM_PREF( | 137 delays->set_screen_dim_ms(values.ac_screen_dim_delay_ms); |
| 138 ac_screen_dim_delay_ms_pref, screen_dim_ms, delays, &got_prefs); | 138 delays->set_screen_off_ms(values.ac_screen_off_delay_ms); |
| 139 SET_DELAY_FROM_PREF( | 139 delays->set_screen_lock_ms(values.ac_screen_lock_delay_ms); |
| 140 ac_screen_off_delay_ms_pref, screen_off_ms, delays, &got_prefs); | 140 delays->set_idle_warning_ms(values.ac_idle_warning_delay_ms); |
| 141 SET_DELAY_FROM_PREF( | 141 delays->set_idle_ms(values.ac_idle_delay_ms); |
| 142 ac_screen_lock_delay_ms_pref, screen_lock_ms, delays, &got_prefs); | 142 |
| 143 SET_DELAY_FROM_PREF( | 143 // 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); | 144 // it's turned off due to user inactivity. |
| 145 SET_DELAY_FROM_PREF(ac_idle_delay_ms_pref, idle_ms, delays, &got_prefs); | 145 if (values.enable_screen_lock && delays->screen_off_ms() > 0 && |
| 146 (delays->screen_lock_ms() <= 0 || |
| 147 delays->screen_off_ms() < delays->screen_lock_ms())) { |
| 148 delays->set_screen_lock_ms(delays->screen_off_ms()); |
| 149 } |
| 146 | 150 |
| 147 delays = prefs_policy_.mutable_battery_delays(); | 151 delays = prefs_policy_.mutable_battery_delays(); |
| 148 SET_DELAY_FROM_PREF( | 152 delays->set_screen_dim_ms(values.battery_screen_dim_delay_ms); |
| 149 battery_screen_dim_delay_ms_pref, screen_dim_ms, delays, &got_prefs); | 153 delays->set_screen_off_ms(values.battery_screen_off_delay_ms); |
| 150 SET_DELAY_FROM_PREF( | 154 delays->set_screen_lock_ms(values.battery_screen_lock_delay_ms); |
| 151 battery_screen_off_delay_ms_pref, screen_off_ms, delays, &got_prefs); | 155 delays->set_idle_warning_ms(values.battery_idle_warning_delay_ms); |
| 152 SET_DELAY_FROM_PREF( | 156 delays->set_idle_ms(values.battery_idle_delay_ms); |
| 153 battery_screen_lock_delay_ms_pref, screen_lock_ms, delays, &got_prefs); | 157 if (values.enable_screen_lock && delays->screen_off_ms() > 0 && |
| 154 SET_DELAY_FROM_PREF( | 158 (delays->screen_lock_ms() <= 0 || |
| 155 battery_idle_warning_delay_ms_pref, idle_warning_ms, delays, &got_prefs); | 159 delays->screen_off_ms() < delays->screen_lock_ms())) { |
| 156 SET_DELAY_FROM_PREF(battery_idle_delay_ms_pref, idle_ms, delays, &got_prefs); | 160 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 } | 161 } |
| 178 | 162 |
| 179 prefs_were_set_ = got_prefs; | 163 prefs_policy_.set_idle_action(GetProtoAction(values.idle_action)); |
| 164 prefs_policy_.set_lid_closed_action(GetProtoAction(values.lid_closed_action)); |
| 165 prefs_policy_.set_use_audio_activity(values.use_audio_activity); |
| 166 prefs_policy_.set_use_video_activity(values.use_video_activity); |
| 167 prefs_policy_.set_presentation_idle_delay_factor( |
| 168 values.presentation_idle_delay_factor); |
| 169 |
| 170 prefs_were_set_ = true; |
| 180 SendCurrentPolicy(); | 171 SendCurrentPolicy(); |
| 181 } | 172 } |
| 182 | 173 |
| 183 int PowerPolicyController::AddScreenBlock(const std::string& reason) { | 174 int PowerPolicyController::AddScreenBlock(const std::string& reason) { |
| 184 int id = next_block_id_++; | 175 int id = next_block_id_++; |
| 185 screen_blocks_[id] = reason; | 176 screen_blocks_[id] = reason; |
| 186 SendCurrentPolicy(); | 177 SendCurrentPolicy(); |
| 187 return id; | 178 return id; |
| 188 } | 179 } |
| 189 | 180 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 if (!reason.empty()) | 235 if (!reason.empty()) |
| 245 policy.set_reason(reason); | 236 policy.set_reason(reason); |
| 246 client_->SetPolicy(policy); | 237 client_->SetPolicy(policy); |
| 247 } | 238 } |
| 248 | 239 |
| 249 void PowerPolicyController::SendEmptyPolicy() { | 240 void PowerPolicyController::SendEmptyPolicy() { |
| 250 client_->SetPolicy(power_manager::PowerManagementPolicy()); | 241 client_->SetPolicy(power_manager::PowerManagementPolicy()); |
| 251 } | 242 } |
| 252 | 243 |
| 253 } // namespace chromeos | 244 } // namespace chromeos |
| OLD | NEW |