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/format_macros.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 str += base::StringPrintf("wait_for_initial_user_activity=%d ", | 127 str += base::StringPrintf("wait_for_initial_user_activity=%d ", |
128 policy.wait_for_initial_user_activity()); | 128 policy.wait_for_initial_user_activity()); |
129 } | 129 } |
130 if (policy.has_reason()) | 130 if (policy.has_reason()) |
131 str += base::StringPrintf("reason=\"%s\" ", policy.reason().c_str()); | 131 str += base::StringPrintf("reason=\"%s\" ", policy.reason().c_str()); |
132 TrimWhitespace(str, TRIM_TRAILING, &str); | 132 TrimWhitespace(str, TRIM_TRAILING, &str); |
133 return str; | 133 return str; |
134 } | 134 } |
135 | 135 |
136 PowerPolicyController::PowerPolicyController() | 136 PowerPolicyController::PowerPolicyController() |
137 : client_(NULL), | 137 : manager_(NULL), |
| 138 client_(NULL), |
138 prefs_were_set_(false), | 139 prefs_were_set_(false), |
139 honor_screen_wake_locks_(true), | 140 honor_screen_wake_locks_(true), |
140 next_wake_lock_id_(1) { | 141 next_wake_lock_id_(1) { |
141 } | 142 } |
142 | 143 |
143 PowerPolicyController::~PowerPolicyController() { | 144 PowerPolicyController::~PowerPolicyController() { |
144 if (client_) { | 145 DCHECK(manager_); |
145 client_->RemoveObserver(this); | 146 // The power manager's policy is reset before this point, in |
146 client_ = NULL; | 147 // OnDBusThreadManagerDestroying(). At the time that |
147 } | 148 // PowerPolicyController is destroyed, PowerManagerClient's D-Bus proxy |
| 149 // to the power manager is already gone. |
| 150 client_->RemoveObserver(this); |
| 151 client_ = NULL; |
| 152 manager_->RemoveObserver(this); |
| 153 manager_ = NULL; |
148 } | 154 } |
149 | 155 |
150 void PowerPolicyController::Init(DBusThreadManager* manager) { | 156 void PowerPolicyController::Init(DBusThreadManager* manager) { |
151 client_ = manager->GetPowerManagerClient(); | 157 manager_ = manager; |
| 158 manager_->AddObserver(this); |
| 159 client_ = manager_->GetPowerManagerClient(); |
152 client_->AddObserver(this); | 160 client_->AddObserver(this); |
| 161 SendCurrentPolicy(); |
153 } | 162 } |
154 | 163 |
155 void PowerPolicyController::ApplyPrefs(const PrefValues& values) { | 164 void PowerPolicyController::ApplyPrefs(const PrefValues& values) { |
156 prefs_policy_.Clear(); | 165 prefs_policy_.Clear(); |
157 | 166 |
158 power_manager::PowerManagementPolicy::Delays* delays = | 167 power_manager::PowerManagementPolicy::Delays* delays = |
159 prefs_policy_.mutable_ac_delays(); | 168 prefs_policy_.mutable_ac_delays(); |
160 delays->set_screen_dim_ms(values.ac_screen_dim_delay_ms); | 169 delays->set_screen_dim_ms(values.ac_screen_dim_delay_ms); |
161 delays->set_screen_off_ms(values.ac_screen_off_delay_ms); | 170 delays->set_screen_off_ms(values.ac_screen_off_delay_ms); |
162 delays->set_screen_lock_ms(values.ac_screen_lock_delay_ms); | 171 delays->set_screen_lock_ms(values.ac_screen_lock_delay_ms); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 values.user_activity_screen_dim_delay_factor); | 213 values.user_activity_screen_dim_delay_factor); |
205 prefs_policy_.set_wait_for_initial_user_activity( | 214 prefs_policy_.set_wait_for_initial_user_activity( |
206 values.wait_for_initial_user_activity); | 215 values.wait_for_initial_user_activity); |
207 | 216 |
208 honor_screen_wake_locks_ = values.allow_screen_wake_locks; | 217 honor_screen_wake_locks_ = values.allow_screen_wake_locks; |
209 | 218 |
210 prefs_were_set_ = true; | 219 prefs_were_set_ = true; |
211 SendCurrentPolicy(); | 220 SendCurrentPolicy(); |
212 } | 221 } |
213 | 222 |
| 223 void PowerPolicyController::ClearPrefs() { |
| 224 prefs_policy_.Clear(); |
| 225 honor_screen_wake_locks_ = true; |
| 226 prefs_were_set_ = false; |
| 227 SendCurrentPolicy(); |
| 228 } |
| 229 |
214 int PowerPolicyController::AddScreenWakeLock(const std::string& reason) { | 230 int PowerPolicyController::AddScreenWakeLock(const std::string& reason) { |
215 int id = next_wake_lock_id_++; | 231 int id = next_wake_lock_id_++; |
216 screen_wake_locks_[id] = reason; | 232 screen_wake_locks_[id] = reason; |
217 SendCurrentPolicy(); | 233 SendCurrentPolicy(); |
218 return id; | 234 return id; |
219 } | 235 } |
220 | 236 |
221 int PowerPolicyController::AddSystemWakeLock(const std::string& reason) { | 237 int PowerPolicyController::AddSystemWakeLock(const std::string& reason) { |
222 int id = next_wake_lock_id_++; | 238 int id = next_wake_lock_id_++; |
223 system_wake_locks_[id] = reason; | 239 system_wake_locks_[id] = reason; |
224 SendCurrentPolicy(); | 240 SendCurrentPolicy(); |
225 return id; | 241 return id; |
226 } | 242 } |
227 | 243 |
228 void PowerPolicyController::RemoveWakeLock(int id) { | 244 void PowerPolicyController::RemoveWakeLock(int id) { |
229 if (!screen_wake_locks_.erase(id) && !system_wake_locks_.erase(id)) | 245 if (!screen_wake_locks_.erase(id) && !system_wake_locks_.erase(id)) |
230 LOG(WARNING) << "Ignoring request to remove nonexistent wake lock " << id; | 246 LOG(WARNING) << "Ignoring request to remove nonexistent wake lock " << id; |
231 else | 247 else |
232 SendCurrentPolicy(); | 248 SendCurrentPolicy(); |
233 } | 249 } |
234 | 250 |
| 251 void PowerPolicyController::OnDBusThreadManagerDestroying( |
| 252 DBusThreadManager* manager) { |
| 253 DCHECK_EQ(manager, manager_); |
| 254 SendEmptyPolicy(); |
| 255 } |
| 256 |
235 void PowerPolicyController::PowerManagerRestarted() { | 257 void PowerPolicyController::PowerManagerRestarted() { |
236 SendCurrentPolicy(); | 258 SendCurrentPolicy(); |
237 } | 259 } |
238 | 260 |
239 void PowerPolicyController::SendCurrentPolicy() { | 261 void PowerPolicyController::SendCurrentPolicy() { |
240 std::string reason; | 262 std::string reason; |
241 | 263 |
242 power_manager::PowerManagementPolicy policy = prefs_policy_; | 264 power_manager::PowerManagementPolicy policy = prefs_policy_; |
243 if (prefs_were_set_) | 265 if (prefs_were_set_) |
244 reason = "Prefs"; | 266 reason = "Prefs"; |
(...skipping 27 matching lines...) Expand all Loading... |
272 for (WakeLockMap::const_iterator it = system_wake_locks_.begin(); | 294 for (WakeLockMap::const_iterator it = system_wake_locks_.begin(); |
273 it != system_wake_locks_.end(); ++it) { | 295 it != system_wake_locks_.end(); ++it) { |
274 reason += (reason.empty() ? "" : ", ") + it->second; | 296 reason += (reason.empty() ? "" : ", ") + it->second; |
275 } | 297 } |
276 | 298 |
277 if (!reason.empty()) | 299 if (!reason.empty()) |
278 policy.set_reason(reason); | 300 policy.set_reason(reason); |
279 client_->SetPolicy(policy); | 301 client_->SetPolicy(policy); |
280 } | 302 } |
281 | 303 |
| 304 void PowerPolicyController::SendEmptyPolicy() { |
| 305 client_->SetPolicy(power_manager::PowerManagementPolicy()); |
| 306 } |
| 307 |
282 } // namespace chromeos | 308 } // namespace chromeos |
OLD | NEW |