Chromium Code Reviews| 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(const CalculateIdleTimeCallback& callback) | |
| 157 OVERRIDE { | |
| 158 dbus::MethodCall method_call(power_manager::kPowerManagerInterface, | |
| 159 power_manager::kGetIdleTime); | |
| 160 power_manager_proxy_->CallMethod( | |
| 161 &method_call, | |
| 162 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 163 base::Bind(&PowerManagerClientImpl::OnGetIdleTime, | |
| 164 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 165 } | |
| 156 | 166 |
| 157 private: | 167 private: |
| 158 // Called when a dbus signal is initially connected. | 168 // Called when a dbus signal is initially connected. |
| 159 void SignalConnected(const std::string& interface_name, | 169 void SignalConnected(const std::string& interface_name, |
| 160 const std::string& signal_name, | 170 const std::string& signal_name, |
| 161 bool success) { | 171 bool success) { |
| 162 LOG_IF(WARNING, !success) << "Failed to connect to signal " | 172 LOG_IF(WARNING, !success) << "Failed to connect to signal " |
| 163 << signal_name << "."; | 173 << signal_name << "."; |
| 164 } | 174 } |
| 165 | 175 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 226 !reader.PopBool(&status.battery_is_full)) { | 236 !reader.PopBool(&status.battery_is_full)) { |
| 227 LOG(ERROR) << "Error reading response from powerd: " | 237 LOG(ERROR) << "Error reading response from powerd: " |
| 228 << response->ToString(); | 238 << response->ToString(); |
| 229 return; | 239 return; |
| 230 } | 240 } |
| 231 | 241 |
| 232 VLOG(1) << "Power status: " << status.ToString(); | 242 VLOG(1) << "Power status: " << status.ToString(); |
| 233 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); | 243 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); |
| 234 } | 244 } |
| 235 | 245 |
| 246 void OnGetIdleTime(const CalculateIdleTimeCallback& callback, | |
| 247 dbus::Response* response) { | |
|
satorux1
2011/11/15 02:07:12
Please add something like:
if (!response) {
LOG
Simon Que
2011/11/15 02:47:44
Done.
| |
| 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 } | |
| 262 | |
| 236 dbus::ObjectProxy* power_manager_proxy_; | 263 dbus::ObjectProxy* power_manager_proxy_; |
| 237 ObserverList<Observer> observers_; | 264 ObserverList<Observer> observers_; |
| 238 base::WeakPtrFactory<PowerManagerClientImpl> weak_ptr_factory_; | 265 base::WeakPtrFactory<PowerManagerClientImpl> weak_ptr_factory_; |
| 239 | 266 |
| 240 DISALLOW_COPY_AND_ASSIGN(PowerManagerClientImpl); | 267 DISALLOW_COPY_AND_ASSIGN(PowerManagerClientImpl); |
| 241 }; | 268 }; |
| 242 | 269 |
| 243 // The PowerManagerClient implementation used on Linux desktop, | 270 // The PowerManagerClient implementation used on Linux desktop, |
| 244 // which does nothing. | 271 // which does nothing. |
| 245 class PowerManagerClientStubImpl : public PowerManagerClient { | 272 class PowerManagerClientStubImpl : public PowerManagerClient { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 281 &PowerManagerClientStubImpl::Update); | 308 &PowerManagerClientStubImpl::Update); |
| 282 } else { | 309 } else { |
| 283 timer_.Stop(); | 310 timer_.Stop(); |
| 284 } | 311 } |
| 285 } | 312 } |
| 286 | 313 |
| 287 virtual void RequestRestart() OVERRIDE {} | 314 virtual void RequestRestart() OVERRIDE {} |
| 288 | 315 |
| 289 virtual void RequestShutdown() OVERRIDE {} | 316 virtual void RequestShutdown() OVERRIDE {} |
| 290 | 317 |
| 318 virtual void CalculateIdleTime(const CalculateIdleTimeCallback& callback) | |
| 319 OVERRIDE { | |
|
satorux1
2011/11/15 02:07:12
Please call callback.Run(0) to keep compatibility
Simon Que
2011/11/15 02:47:44
Done.
| |
| 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 |