OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/chromeos/dbus/power_manager_client.h" | 5 #include "chrome/browser/chromeos/dbus/power_manager_client.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 // Requests shutdown of the system. | 149 // Requests shutdown of the system. |
150 virtual void RequestShutdown() OVERRIDE { | 150 virtual void RequestShutdown() OVERRIDE { |
151 dbus::MethodCall method_call(power_manager::kPowerManagerInterface, | 151 dbus::MethodCall method_call(power_manager::kPowerManagerInterface, |
152 power_manager::kRequestShutdownMethod); | 152 power_manager::kRequestShutdownMethod); |
153 power_manager_proxy_->CallMethod( | 153 power_manager_proxy_->CallMethod( |
154 &method_call, | 154 &method_call, |
155 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 155 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
156 dbus::ObjectProxy::EmptyResponseCallback()); | 156 dbus::ObjectProxy::EmptyResponseCallback()); |
157 } | 157 } |
158 | 158 |
| 159 virtual void CalculateIdleTime(const CalculateIdleTimeCallback& callback) |
| 160 OVERRIDE { |
| 161 dbus::MethodCall method_call(power_manager::kPowerManagerInterface, |
| 162 power_manager::kGetIdleTime); |
| 163 power_manager_proxy_->CallMethod( |
| 164 &method_call, |
| 165 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 166 base::Bind(&PowerManagerClientImpl::OnGetIdleTime, |
| 167 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 168 } |
159 | 169 |
160 private: | 170 private: |
161 // Called when a dbus signal is initially connected. | 171 // Called when a dbus signal is initially connected. |
162 void SignalConnected(const std::string& interface_name, | 172 void SignalConnected(const std::string& interface_name, |
163 const std::string& signal_name, | 173 const std::string& signal_name, |
164 bool success) { | 174 bool success) { |
165 LOG_IF(WARNING, !success) << "Failed to connect to signal " | 175 LOG_IF(WARNING, !success) << "Failed to connect to signal " |
166 << signal_name << "."; | 176 << signal_name << "."; |
167 } | 177 } |
168 | 178 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 !reader.PopBool(&status.battery_is_full)) { | 239 !reader.PopBool(&status.battery_is_full)) { |
230 LOG(ERROR) << "Error reading response from powerd: " | 240 LOG(ERROR) << "Error reading response from powerd: " |
231 << response->ToString(); | 241 << response->ToString(); |
232 return; | 242 return; |
233 } | 243 } |
234 | 244 |
235 VLOG(1) << "Power status: " << status.ToString(); | 245 VLOG(1) << "Power status: " << status.ToString(); |
236 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); | 246 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); |
237 } | 247 } |
238 | 248 |
| 249 void OnGetIdleTime(const CalculateIdleTimeCallback& callback, |
| 250 dbus::Response* response) { |
| 251 dbus::MessageReader reader(response); |
| 252 int64 idle_time_ms = 0; |
| 253 if (!reader.PopInt64(&idle_time_ms)) { |
| 254 LOG(ERROR) << "Error reading response from powerd: " |
| 255 << response->ToString(); |
| 256 callback.Run(-1); |
| 257 return; |
| 258 } |
| 259 if (idle_time_ms < 0) { |
| 260 LOG(ERROR) << "Power manager failed to calculate idle time."; |
| 261 callback.Run(-1); |
| 262 return; |
| 263 } |
| 264 callback.Run(idle_time_ms/1000); |
| 265 } |
| 266 |
239 dbus::ObjectProxy* power_manager_proxy_; | 267 dbus::ObjectProxy* power_manager_proxy_; |
240 ObserverList<Observer> observers_; | 268 ObserverList<Observer> observers_; |
241 base::WeakPtrFactory<PowerManagerClientImpl> weak_ptr_factory_; | 269 base::WeakPtrFactory<PowerManagerClientImpl> weak_ptr_factory_; |
242 | 270 |
243 DISALLOW_COPY_AND_ASSIGN(PowerManagerClientImpl); | 271 DISALLOW_COPY_AND_ASSIGN(PowerManagerClientImpl); |
244 }; | 272 }; |
245 | 273 |
246 // The PowerManagerClient implementation used on Linux desktop, | 274 // The PowerManagerClient implementation used on Linux desktop, |
247 // which does nothing. | 275 // which does nothing. |
248 class PowerManagerClientStubImpl : public PowerManagerClient { | 276 class PowerManagerClientStubImpl : public PowerManagerClient { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 &PowerManagerClientStubImpl::Update); | 312 &PowerManagerClientStubImpl::Update); |
285 } else { | 313 } else { |
286 timer_.Stop(); | 314 timer_.Stop(); |
287 } | 315 } |
288 } | 316 } |
289 | 317 |
290 virtual void RequestRestart() OVERRIDE {} | 318 virtual void RequestRestart() OVERRIDE {} |
291 | 319 |
292 virtual void RequestShutdown() OVERRIDE {} | 320 virtual void RequestShutdown() OVERRIDE {} |
293 | 321 |
| 322 virtual void CalculateIdleTime(const CalculateIdleTimeCallback& callback) |
| 323 OVERRIDE { |
| 324 callback.Run(0); |
| 325 } |
| 326 |
294 private: | 327 private: |
295 void Update() { | 328 void Update() { |
296 // We pause at 0 and 100% so that it's easier to check those conditions. | 329 // We pause at 0 and 100% so that it's easier to check those conditions. |
297 if (pause_count_ > 1) { | 330 if (pause_count_ > 1) { |
298 pause_count_--; | 331 pause_count_--; |
299 return; | 332 return; |
300 } | 333 } |
301 | 334 |
302 if (battery_percentage_ == 0 || battery_percentage_ == 100) { | 335 if (battery_percentage_ == 0 || battery_percentage_ == 100) { |
303 if (pause_count_) { | 336 if (pause_count_) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 | 375 |
343 PowerManagerClient* PowerManagerClient::Create(dbus::Bus* bus) { | 376 PowerManagerClient* PowerManagerClient::Create(dbus::Bus* bus) { |
344 if (system::runtime_environment::IsRunningOnChromeOS()) { | 377 if (system::runtime_environment::IsRunningOnChromeOS()) { |
345 return new PowerManagerClientImpl(bus); | 378 return new PowerManagerClientImpl(bus); |
346 } else { | 379 } else { |
347 return new PowerManagerClientStubImpl(); | 380 return new PowerManagerClientStubImpl(); |
348 } | 381 } |
349 } | 382 } |
350 | 383 |
351 } // namespace chromeos | 384 } // namespace chromeos |
OLD | NEW |