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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 // Requests shutdown of the system. | 146 // Requests shutdown of the system. |
147 virtual void RequestShutdown() OVERRIDE { | 147 virtual void RequestShutdown() OVERRIDE { |
148 dbus::MethodCall method_call(power_manager::kPowerManagerInterface, | 148 dbus::MethodCall method_call(power_manager::kPowerManagerInterface, |
149 power_manager::kRequestShutdownMethod); | 149 power_manager::kRequestShutdownMethod); |
150 power_manager_proxy_->CallMethod( | 150 power_manager_proxy_->CallMethod( |
151 &method_call, | 151 &method_call, |
152 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 152 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
153 dbus::ObjectProxy::EmptyResponseCallback()); | 153 dbus::ObjectProxy::EmptyResponseCallback()); |
154 } | 154 } |
155 | 155 |
| 156 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { |
| 157 dbus::MethodCall method_call(power_manager::kPowerManagerInterface, |
| 158 power_manager::kGetIdleTime); |
| 159 power_manager_proxy_->CallMethod( |
| 160 &method_call, |
| 161 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 162 base::Bind(&PowerManagerClientImpl::OnGetIdleTime, |
| 163 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 164 } |
156 | 165 |
157 private: | 166 private: |
158 // Called when a dbus signal is initially connected. | 167 // Called when a dbus signal is initially connected. |
159 void SignalConnected(const std::string& interface_name, | 168 void SignalConnected(const std::string& interface_name, |
160 const std::string& signal_name, | 169 const std::string& signal_name, |
161 bool success) { | 170 bool success) { |
162 LOG_IF(WARNING, !success) << "Failed to connect to signal " | 171 LOG_IF(WARNING, !success) << "Failed to connect to signal " |
163 << signal_name << "."; | 172 << signal_name << "."; |
164 } | 173 } |
165 | 174 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 !reader.PopBool(&status.battery_is_full)) { | 235 !reader.PopBool(&status.battery_is_full)) { |
227 LOG(ERROR) << "Error reading response from powerd: " | 236 LOG(ERROR) << "Error reading response from powerd: " |
228 << response->ToString(); | 237 << response->ToString(); |
229 return; | 238 return; |
230 } | 239 } |
231 | 240 |
232 VLOG(1) << "Power status: " << status.ToString(); | 241 VLOG(1) << "Power status: " << status.ToString(); |
233 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); | 242 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); |
234 } | 243 } |
235 | 244 |
| 245 void OnGetIdleTime(CalculateIdleTimeCallback* callback, |
| 246 dbus::Response* response) { |
| 247 DCHECK(callback); |
| 248 dbus::MessageReader reader(response); |
| 249 int64 idle_time_ms = 0; |
| 250 if (!reader.PopInt64(&idle_time_ms)) { |
| 251 LOG(ERROR) << "Error reading response from powerd: " |
| 252 << response->ToString(); |
| 253 return; |
| 254 } |
| 255 if (idle_time_ms >= 0) { |
| 256 callback->Run(idle_time_ms/1000); |
| 257 } else { |
| 258 LOG(ERROR) << "Power manager failed to calculate idle time."; |
| 259 callback->Run(-1); |
| 260 } |
| 261 delete callback; |
| 262 } |
| 263 |
236 dbus::ObjectProxy* power_manager_proxy_; | 264 dbus::ObjectProxy* power_manager_proxy_; |
237 ObserverList<Observer> observers_; | 265 ObserverList<Observer> observers_; |
238 base::WeakPtrFactory<PowerManagerClientImpl> weak_ptr_factory_; | 266 base::WeakPtrFactory<PowerManagerClientImpl> weak_ptr_factory_; |
239 | 267 |
240 DISALLOW_COPY_AND_ASSIGN(PowerManagerClientImpl); | 268 DISALLOW_COPY_AND_ASSIGN(PowerManagerClientImpl); |
241 }; | 269 }; |
242 | 270 |
243 // The PowerManagerClient implementation used on Linux desktop, | 271 // The PowerManagerClient implementation used on Linux desktop, |
244 // which does nothing. | 272 // which does nothing. |
245 class PowerManagerClientStubImpl : public PowerManagerClient { | 273 class PowerManagerClientStubImpl : public PowerManagerClient { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 &PowerManagerClientStubImpl::Update); | 309 &PowerManagerClientStubImpl::Update); |
282 } else { | 310 } else { |
283 timer_.Stop(); | 311 timer_.Stop(); |
284 } | 312 } |
285 } | 313 } |
286 | 314 |
287 virtual void RequestRestart() OVERRIDE {} | 315 virtual void RequestRestart() OVERRIDE {} |
288 | 316 |
289 virtual void RequestShutdown() OVERRIDE {} | 317 virtual void RequestShutdown() OVERRIDE {} |
290 | 318 |
| 319 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { |
| 320 } |
| 321 |
291 private: | 322 private: |
292 void Update() { | 323 void Update() { |
293 // We pause at 0 and 100% so that it's easier to check those conditions. | 324 // We pause at 0 and 100% so that it's easier to check those conditions. |
294 if (pause_count_ > 1) { | 325 if (pause_count_ > 1) { |
295 pause_count_--; | 326 pause_count_--; |
296 return; | 327 return; |
297 } | 328 } |
298 | 329 |
299 if (battery_percentage_ == 0 || battery_percentage_ == 100) { | 330 if (battery_percentage_ == 0 || battery_percentage_ == 100) { |
300 if (pause_count_) { | 331 if (pause_count_) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 | 365 |
335 PowerManagerClient* PowerManagerClient::Create(dbus::Bus* bus) { | 366 PowerManagerClient* PowerManagerClient::Create(dbus::Bus* bus) { |
336 if (system::runtime_environment::IsRunningOnChromeOS()) { | 367 if (system::runtime_environment::IsRunningOnChromeOS()) { |
337 return new PowerManagerClientImpl(bus); | 368 return new PowerManagerClientImpl(bus); |
338 } else { | 369 } else { |
339 return new PowerManagerClientStubImpl(); | 370 return new PowerManagerClientStubImpl(); |
340 } | 371 } |
341 } | 372 } |
342 | 373 |
343 } // namespace chromeos | 374 } // namespace chromeos |
OLD | NEW |