Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 #include "chromeos/dbus/power_manager_client.h" | |
| 11 | |
| 12 namespace chromeos { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // If |pref|, a PrefServiceBase::Preference containing an integer, has been | |
| 17 // explicitly set to 0 or a positive value, assigns it to |proto_field|, a | |
| 18 // int32 field in |proto|, a google::protobuf::MessageLite*. | |
| 19 #define SET_INT_FROM_PREF(pref, proto_field, proto) \ | |
| 20 if (int value = GetIntPrefValue(pref) >= 0) \ | |
| 21 (proto)->set_##proto_field(value); | |
| 22 | |
| 23 // Similar to SET_INT_FROM_PREF() but sets a | |
| 24 // power_manager::PowerManagementPolicy_Action field instead. | |
| 25 #define SET_ACTION_FROM_PREF(pref, proto_field, proto) \ | |
| 26 if (int value = GetIntPrefValue(pref) >= 0) \ | |
| 27 (proto)->set_##proto_field( \ | |
| 28 static_cast<power_manager::PowerManagementPolicy_Action>(value)); | |
| 29 | |
| 30 // If |pref|, a PrefServiceBase::Preference containing an bool, has been | |
|
bartfab (slow)
2013/02/04 17:20:05
Nit: "a bool"
Daniel Erat
2013/02/04 17:31:59
Done.
| |
| 31 // set, assigns it to |proto_field|, a bool field in |proto|, a | |
| 32 // google::protobuf::MessageLite*. | |
| 33 #define SET_BOOL_FROM_PREF(pref, proto_field, proto) \ | |
| 34 if (!pref.IsDefaultValue()) { \ | |
| 35 bool value = false; \ | |
| 36 if (pref.GetValue()->GetAsBoolean(&value)) \ | |
| 37 (proto)->set_##proto_field(value); \ | |
| 38 else \ | |
| 39 LOG(DFATAL) << pref.name() << " pref has non-boolean value"; \ | |
| 40 } | |
| 41 | |
| 42 // Returns a zero or positive integer value from |pref|. Returns -1 if the | |
| 43 // pref is unset and logs an error if it's set to a negative value. | |
| 44 int GetIntPrefValue(const PrefServiceBase::Preference& pref) { | |
| 45 if (pref.IsDefaultValue()) | |
| 46 return -1; | |
| 47 | |
| 48 int value = -1; | |
| 49 if (!pref.GetValue()->GetAsInteger(&value)) { | |
| 50 LOG(DFATAL) << pref.name() << " pref has non-integer value"; | |
| 51 return -1; | |
| 52 } | |
| 53 | |
| 54 if (value < 0) | |
| 55 LOG(WARNING) << pref.name() << " pref has negative value"; | |
| 56 return value; | |
| 57 } | |
| 58 | |
| 59 // Returns the power_manager::PowerManagementPolicy_Action value | |
| 60 // corresponding to |action|. | |
| 61 power_manager::PowerManagementPolicy_Action GetProtoAction( | |
| 62 PowerPolicyController::Action action) { | |
| 63 switch (action) { | |
| 64 case PowerPolicyController::ACTION_SUSPEND: | |
| 65 return power_manager::PowerManagementPolicy_Action_SUSPEND; | |
| 66 case PowerPolicyController::ACTION_STOP_SESSION: | |
| 67 return power_manager::PowerManagementPolicy_Action_STOP_SESSION; | |
| 68 case PowerPolicyController::ACTION_SHUT_DOWN: | |
| 69 return power_manager::PowerManagementPolicy_Action_SHUT_DOWN; | |
| 70 case PowerPolicyController::ACTION_DO_NOTHING: | |
| 71 return power_manager::PowerManagementPolicy_Action_DO_NOTHING; | |
| 72 default: | |
| 73 NOTREACHED() << "Unhandled action " << action; | |
| 74 return power_manager::PowerManagementPolicy_Action_DO_NOTHING; | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 } // namespace | |
| 79 | |
| 80 PowerPolicyController::PowerPolicyController(PowerManagerClient* client) | |
| 81 : client_(client) { | |
| 82 client_->SetPolicy(power_manager::PowerManagementPolicy()); | |
| 83 ResetPolicy(); | |
| 84 } | |
| 85 | |
| 86 PowerPolicyController::~PowerPolicyController() { | |
| 87 client_->SetPolicy(power_manager::PowerManagementPolicy()); | |
| 88 client_ = NULL; | |
| 89 } | |
| 90 | |
| 91 void PowerPolicyController::UpdatePolicyFromPrefs( | |
| 92 const PrefServiceBase::Preference& ac_screen_dim_delay_ms_pref, | |
| 93 const PrefServiceBase::Preference& ac_screen_off_delay_ms_pref, | |
| 94 const PrefServiceBase::Preference& ac_screen_lock_delay_ms_pref, | |
| 95 const PrefServiceBase::Preference& ac_idle_delay_ms_pref, | |
| 96 const PrefServiceBase::Preference& battery_screen_dim_delay_ms_pref, | |
| 97 const PrefServiceBase::Preference& battery_screen_off_delay_ms_pref, | |
| 98 const PrefServiceBase::Preference& battery_screen_lock_delay_ms_pref, | |
| 99 const PrefServiceBase::Preference& battery_idle_delay_ms_pref, | |
| 100 const PrefServiceBase::Preference& idle_action_pref, | |
| 101 const PrefServiceBase::Preference& lid_closed_action_pref, | |
| 102 const PrefServiceBase::Preference& use_audio_activity_pref, | |
| 103 const PrefServiceBase::Preference& use_video_activity_pref, | |
| 104 const PrefServiceBase::Preference& presentation_idle_delay_factor_pref) { | |
| 105 prefs_policy_.Clear(); | |
| 106 | |
| 107 power_manager::PowerManagementPolicy::Delays* delays = | |
| 108 prefs_policy_.mutable_ac_delays(); | |
| 109 SET_DELAY_FROM_PREF(ac_screen_dim_delay_ms_pref, screen_dim_ms, delays); | |
| 110 SET_DELAY_FROM_PREF(ac_screen_off_delay_ms_pref, screen_off_ms, delays); | |
| 111 SET_DELAY_FROM_PREF(ac_screen_lock_delay_ms_pref, screen_lock_ms, delays); | |
| 112 SET_DELAY_FROM_PREF(ac_idle_delay_ms_pref, idle_ms, delays); | |
| 113 | |
| 114 delays = prefs_policy_.mutable_battery_delays(); | |
| 115 SET_DELAY_FROM_PREF(battery_screen_dim_delay_ms_pref, screen_dim_ms, delays); | |
| 116 SET_DELAY_FROM_PREF(battery_screen_off_delay_ms_pref, screen_off_ms, delays); | |
| 117 SET_DELAY_FROM_PREF( | |
| 118 battery_screen_lock_delay_ms_pref, screen_lock_ms, delays); | |
| 119 SET_DELAY_FROM_PREF(battery_idle_delay_ms_pref, idle_ms, delays); | |
| 120 | |
| 121 SET_ACTION_FROM_PREF(idle_action_pref, idle_action, &prefs_policy_); | |
| 122 SET_ACTION_FROM_PREF( | |
| 123 lid_closed_action_pref, lid_closed_action, &prefs_policy_); | |
| 124 | |
| 125 SET_BOOL_FROM_PREF( | |
| 126 use_audio_activity_pref, use_audio_activity, &prefs_policy_); | |
| 127 SET_BOOL_FROM_PREF( | |
| 128 use_video_activity_pref, use_video_activity, &prefs_policy_); | |
| 129 | |
| 130 if (!presentation_idle_delay_factor_pref.IsDefaultValue()) { | |
| 131 double value = 0.0; | |
| 132 if (presentation_idle_delay_factor_pref.GetValue()->GetAsDouble(&value)) { | |
| 133 prefs_policy_.set_presentation_idle_delay_factor(value); | |
| 134 } else { | |
| 135 LOG(DFATAL) << presentation_idle_delay_factor_pref.name() | |
| 136 << " pref has non-double value"; | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 // TODO(derat): Incorporate other information into the policy that is | |
| 141 // sent, e.g. from content::PowerSaveBlocker. | |
| 142 client_->SetPolicy(prefs_policy_); | |
| 143 } | |
| 144 | |
| 145 } // namespace chromeos | |
| OLD | NEW |