| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "device/battery/battery_status_manager_linux.h" | 5 #include "device/battery/battery_status_manager_linux.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 UPowerProperties::~UPowerProperties() {} | 54 UPowerProperties::~UPowerProperties() {} |
| 55 | 55 |
| 56 base::Version UPowerProperties::daemon_version() { | 56 base::Version UPowerProperties::daemon_version() { |
| 57 return (daemon_version_.is_valid() || daemon_version_.GetAndBlock()) | 57 return (daemon_version_.is_valid() || daemon_version_.GetAndBlock()) |
| 58 ? base::Version(daemon_version_.value()) | 58 ? base::Version(daemon_version_.value()) |
| 59 : base::Version(); | 59 : base::Version(); |
| 60 } | 60 } |
| 61 | 61 |
| 62 class UPowerObject { | 62 class UPowerObject { |
| 63 public: | 63 public: |
| 64 typedef dbus::PropertySet::PropertyChangedCallback PropertyChangedCallback; | 64 using PropertyChangedCallback = dbus::PropertySet::PropertyChangedCallback; |
| 65 | 65 |
| 66 UPowerObject(dbus::Bus* dbus, | 66 UPowerObject(dbus::Bus* dbus, |
| 67 const PropertyChangedCallback property_changed_callback); | 67 const PropertyChangedCallback property_changed_callback); |
| 68 ~UPowerObject(); | 68 ~UPowerObject(); |
| 69 | 69 |
| 70 std::vector<dbus::ObjectPath> EnumerateDevices(); | 70 std::vector<dbus::ObjectPath> EnumerateDevices(); |
| 71 dbus::ObjectPath GetDisplayDevice(); | 71 dbus::ObjectPath GetDisplayDevice(); |
| 72 | 72 |
| 73 dbus::ObjectProxy* proxy() { return proxy_; } | 73 dbus::ObjectProxy* proxy() { return proxy_; } |
| 74 UPowerProperties* properties() { return properties_.get(); } | 74 UPowerProperties* properties() { return properties_.get(); } |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 : default_value; | 218 : default_value; |
| 219 } | 219 } |
| 220 | 220 |
| 221 uint32_t BatteryProperties::type(uint32_t default_value) { | 221 uint32_t BatteryProperties::type(uint32_t default_value) { |
| 222 return (type_.is_valid() || type_.GetAndBlock()) ? type_.value() | 222 return (type_.is_valid() || type_.GetAndBlock()) ? type_.value() |
| 223 : default_value; | 223 : default_value; |
| 224 } | 224 } |
| 225 | 225 |
| 226 class BatteryObject { | 226 class BatteryObject { |
| 227 public: | 227 public: |
| 228 typedef dbus::PropertySet::PropertyChangedCallback PropertyChangedCallback; | 228 using PropertyChangedCallback = dbus::PropertySet::PropertyChangedCallback; |
| 229 | 229 |
| 230 BatteryObject(dbus::Bus* dbus, | 230 BatteryObject(dbus::Bus* dbus, |
| 231 const dbus::ObjectPath& device_path, | 231 const dbus::ObjectPath& device_path, |
| 232 const PropertyChangedCallback& property_changed_callback); | 232 const PropertyChangedCallback& property_changed_callback); |
| 233 ~BatteryObject(); | 233 ~BatteryObject(); |
| 234 | 234 |
| 235 bool IsValid(); | 235 bool IsValid(); |
| 236 | 236 |
| 237 dbus::ObjectProxy* proxy() { return proxy_; } | 237 dbus::ObjectProxy* proxy() { return proxy_; } |
| 238 BatteryProperties* properties() { return properties_.get(); } | 238 BatteryProperties* properties() { return properties_.get(); } |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 | 327 |
| 328 void StartListening() { | 328 void StartListening() { |
| 329 DCHECK(OnWatcherThread()); | 329 DCHECK(OnWatcherThread()); |
| 330 | 330 |
| 331 if (upower_) | 331 if (upower_) |
| 332 return; | 332 return; |
| 333 | 333 |
| 334 if (!system_bus_) | 334 if (!system_bus_) |
| 335 InitDBus(); | 335 InitDBus(); |
| 336 | 336 |
| 337 upower_.reset(new UPowerObject(system_bus_.get(), | 337 upower_ = base::MakeUnique<UPowerObject>( |
| 338 UPowerObject::PropertyChangedCallback())); | 338 system_bus_.get(), UPowerObject::PropertyChangedCallback()); |
| 339 upower_->proxy()->ConnectToSignal( | 339 upower_->proxy()->ConnectToSignal( |
| 340 kUPowerServiceName, kUPowerSignalDeviceAdded, | 340 kUPowerServiceName, kUPowerSignalDeviceAdded, |
| 341 base::Bind(&BatteryStatusNotificationThread::DeviceAdded, | 341 base::Bind(&BatteryStatusNotificationThread::DeviceAdded, |
| 342 base::Unretained(this)), | 342 base::Unretained(this)), |
| 343 base::Bind(&BatteryStatusNotificationThread::OnSignalConnected, | 343 base::Bind(&BatteryStatusNotificationThread::OnSignalConnected, |
| 344 base::Unretained(this))); | 344 base::Unretained(this))); |
| 345 upower_->proxy()->ConnectToSignal( | 345 upower_->proxy()->ConnectToSignal( |
| 346 kUPowerServiceName, kUPowerSignalDeviceRemoved, | 346 kUPowerServiceName, kUPowerSignalDeviceRemoved, |
| 347 base::Bind(&BatteryStatusNotificationThread::DeviceRemoved, | 347 base::Bind(&BatteryStatusNotificationThread::DeviceRemoved, |
| 348 base::Unretained(this)), | 348 base::Unretained(this)), |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 // - Or it may be a new device; then the previously monitored BatteryObject | 390 // - Or it may be a new device; then the previously monitored BatteryObject |
| 391 // instance (if any) is released on leaving this function. | 391 // instance (if any) is released on leaving this function. |
| 392 // - Or we may not find a battery device; then on leaving this function | 392 // - Or we may not find a battery device; then on leaving this function |
| 393 // battery_ will be nullptr and the previously monitored BatteryObject | 393 // battery_ will be nullptr and the previously monitored BatteryObject |
| 394 // instance (if any) is no longer a battery and will be released. | 394 // instance (if any) is no longer a battery and will be released. |
| 395 std::unique_ptr<BatteryObject> current = std::move(battery_); | 395 std::unique_ptr<BatteryObject> current = std::move(battery_); |
| 396 auto UseCurrentOrCreateBattery = | 396 auto UseCurrentOrCreateBattery = |
| 397 [¤t, this](const dbus::ObjectPath& device_path) { | 397 [¤t, this](const dbus::ObjectPath& device_path) { |
| 398 if (current && current->proxy()->object_path() == device_path) | 398 if (current && current->proxy()->object_path() == device_path) |
| 399 return std::move(current); | 399 return std::move(current); |
| 400 else | 400 return CreateBattery(device_path); |
| 401 return CreateBattery(device_path); | |
| 402 }; | 401 }; |
| 403 | 402 |
| 404 dbus::ObjectPath display_device_path = upower_->GetDisplayDevice(); | 403 dbus::ObjectPath display_device_path; |
| 404 if (!IsDaemonVersionBelow_0_99()) |
| 405 display_device_path = upower_->GetDisplayDevice(); |
| 405 if (display_device_path.IsValid()) { | 406 if (display_device_path.IsValid()) { |
| 406 std::unique_ptr<BatteryObject> battery = | 407 auto battery = UseCurrentOrCreateBattery(display_device_path); |
| 407 UseCurrentOrCreateBattery(display_device_path); | |
| 408 if (battery->IsValid()) | 408 if (battery->IsValid()) |
| 409 battery_ = std::move(battery); | 409 battery_ = std::move(battery); |
| 410 } | 410 } |
| 411 | 411 |
| 412 if (!battery_) { | 412 if (!battery_) { |
| 413 int num_batteries = 0; | 413 int num_batteries = 0; |
| 414 for (const auto& device_path : upower_->EnumerateDevices()) { | 414 for (const auto& device_path : upower_->EnumerateDevices()) { |
| 415 std::unique_ptr<BatteryObject> battery = | 415 auto battery = UseCurrentOrCreateBattery(device_path); |
| 416 UseCurrentOrCreateBattery(device_path); | |
| 417 | |
| 418 if (!battery->IsValid()) | 416 if (!battery->IsValid()) |
| 419 continue; | 417 continue; |
| 420 | 418 |
| 421 if (battery_) { | 419 if (battery_) { |
| 422 // TODO(timvolodine): add support for multiple batteries. Currently we | 420 // TODO(timvolodine): add support for multiple batteries. Currently we |
| 423 // only collect information from the first battery we encounter | 421 // only collect information from the first battery we encounter |
| 424 // (crbug.com/400780). | 422 // (crbug.com/400780). |
| 425 LOG(WARNING) << "multiple batteries found, " | 423 LOG(WARNING) << "multiple batteries found, " |
| 426 << "using status data of the first battery only."; | 424 << "using status data of the first battery only."; |
| 427 } else { | 425 } else { |
| 428 battery_ = std::move(battery); | 426 battery_ = std::move(battery); |
| 429 } | 427 } |
| 430 num_batteries++; | 428 num_batteries++; |
| 431 } | 429 } |
| 432 | 430 |
| 433 UpdateNumberBatteriesHistogram(num_batteries); | 431 UpdateNumberBatteriesHistogram(num_batteries); |
| 434 } | 432 } |
| 435 | 433 |
| 436 if (battery_) { | 434 if (!battery_) { |
| 437 battery_->properties()->ConnectSignals(); | |
| 438 NotifyBatteryStatus(); | |
| 439 } else { | |
| 440 callback_.Run(mojom::BatteryStatus()); | 435 callback_.Run(mojom::BatteryStatus()); |
| 441 return; | 436 return; |
| 442 } | 437 } |
| 443 | 438 |
| 439 battery_->properties()->ConnectSignals(); |
| 440 NotifyBatteryStatus(); |
| 441 |
| 444 if (IsDaemonVersionBelow_0_99()) { | 442 if (IsDaemonVersionBelow_0_99()) { |
| 445 // UPower Version 0.99 replaced the Changed signal with the | 443 // UPower Version 0.99 replaced the Changed signal with the |
| 446 // PropertyChanged signal. For older versions we need to listen | 444 // PropertyChanged signal. For older versions we need to listen |
| 447 // to the Changed signal. | 445 // to the Changed signal. |
| 448 battery_->proxy()->ConnectToSignal( | 446 battery_->proxy()->ConnectToSignal( |
| 449 kUPowerDeviceInterfaceName, kUPowerDeviceSignalChanged, | 447 kUPowerDeviceInterfaceName, kUPowerDeviceSignalChanged, |
| 450 base::Bind(&BatteryStatusNotificationThread::BatteryChanged, | 448 base::Bind(&BatteryStatusNotificationThread::BatteryChanged, |
| 451 base::Unretained(this)), | 449 base::Unretained(this)), |
| 452 base::Bind(&BatteryStatusNotificationThread::OnSignalConnected, | 450 base::Bind(&BatteryStatusNotificationThread::OnSignalConnected, |
| 453 base::Unretained(this))); | 451 base::Unretained(this))); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 469 FROM_HERE, base::Bind(&dbus::Bus::ShutdownAndBlock, system_bus_)); | 467 FROM_HERE, base::Bind(&dbus::Bus::ShutdownAndBlock, system_bus_)); |
| 470 system_bus_ = NULL; | 468 system_bus_ = NULL; |
| 471 } | 469 } |
| 472 | 470 |
| 473 void OnSignalConnected(const std::string& interface_name, | 471 void OnSignalConnected(const std::string& interface_name, |
| 474 const std::string& signal_name, | 472 const std::string& signal_name, |
| 475 bool success) {} | 473 bool success) {} |
| 476 | 474 |
| 477 std::unique_ptr<BatteryObject> CreateBattery( | 475 std::unique_ptr<BatteryObject> CreateBattery( |
| 478 const dbus::ObjectPath& device_path) { | 476 const dbus::ObjectPath& device_path) { |
| 479 std::unique_ptr<BatteryObject> battery(new BatteryObject( | 477 return base::MakeUnique<BatteryObject>( |
| 480 system_bus_.get(), device_path, | 478 system_bus_.get(), device_path, |
| 481 base::Bind(&BatteryStatusNotificationThread::BatteryPropertyChanged, | 479 base::Bind(&BatteryStatusNotificationThread::BatteryPropertyChanged, |
| 482 base::Unretained(this)))); | 480 base::Unretained(this))); |
| 483 return battery; | |
| 484 } | 481 } |
| 485 | 482 |
| 486 void DeviceAdded(dbus::Signal* signal /* unused */) { | 483 void DeviceAdded(dbus::Signal* signal /* unused */) { |
| 487 // Re-iterate all devices to see if we need to monitor the added battery | 484 // Re-iterate all devices to see if we need to monitor the added battery |
| 488 // instead of the currently monitored battery. | 485 // instead of the currently monitored battery. |
| 489 FindBatteryDevice(); | 486 FindBatteryDevice(); |
| 490 } | 487 } |
| 491 | 488 |
| 492 void DeviceRemoved(dbus::Signal* signal) { | 489 void DeviceRemoved(dbus::Signal* signal) { |
| 493 if (!battery_) | 490 if (!battery_) |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 582 notifier_thread_->task_runner()->PostTask( | 579 notifier_thread_->task_runner()->PostTask( |
| 583 FROM_HERE, base::Bind(&BatteryStatusNotificationThread::StopListening, | 580 FROM_HERE, base::Bind(&BatteryStatusNotificationThread::StopListening, |
| 584 base::Unretained(notifier_thread_.get()))); | 581 base::Unretained(notifier_thread_.get()))); |
| 585 } | 582 } |
| 586 | 583 |
| 587 bool BatteryStatusManagerLinux::StartNotifierThreadIfNecessary() { | 584 bool BatteryStatusManagerLinux::StartNotifierThreadIfNecessary() { |
| 588 if (notifier_thread_) | 585 if (notifier_thread_) |
| 589 return true; | 586 return true; |
| 590 | 587 |
| 591 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); | 588 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); |
| 592 notifier_thread_.reset(new BatteryStatusNotificationThread(callback_)); | 589 auto notifier_thread = |
| 593 if (!notifier_thread_->StartWithOptions(thread_options)) { | 590 base::MakeUnique<BatteryStatusNotificationThread>(callback_); |
| 594 notifier_thread_.reset(); | 591 if (!notifier_thread->StartWithOptions(thread_options)) { |
| 595 LOG(ERROR) << "Could not start the " << kBatteryNotifierThreadName | 592 LOG(ERROR) << "Could not start the " << kBatteryNotifierThreadName |
| 596 << " thread"; | 593 << " thread"; |
| 597 return false; | 594 return false; |
| 598 } | 595 } |
| 596 notifier_thread_ = std::move(notifier_thread); |
| 599 return true; | 597 return true; |
| 600 } | 598 } |
| 601 | 599 |
| 602 base::Thread* BatteryStatusManagerLinux::GetNotifierThreadForTesting() { | 600 base::Thread* BatteryStatusManagerLinux::GetNotifierThreadForTesting() { |
| 603 return notifier_thread_.get(); | 601 return notifier_thread_.get(); |
| 604 } | 602 } |
| 605 | 603 |
| 606 // static | 604 // static |
| 607 std::unique_ptr<BatteryStatusManagerLinux> | 605 std::unique_ptr<BatteryStatusManagerLinux> |
| 608 BatteryStatusManagerLinux::CreateForTesting( | 606 BatteryStatusManagerLinux::CreateForTesting( |
| 609 const BatteryStatusService::BatteryUpdateCallback& callback, | 607 const BatteryStatusService::BatteryUpdateCallback& callback, |
| 610 dbus::Bus* bus) { | 608 dbus::Bus* bus) { |
| 611 std::unique_ptr<BatteryStatusManagerLinux> manager( | 609 auto manager = base::MakeUnique<BatteryStatusManagerLinux>(callback); |
| 612 new BatteryStatusManagerLinux(callback)); | 610 if (!manager->StartNotifierThreadIfNecessary()) |
| 613 if (manager->StartNotifierThreadIfNecessary()) | 611 return nullptr; |
| 614 manager->notifier_thread_->SetDBusForTesting(bus); | 612 manager->notifier_thread_->SetDBusForTesting(bus); |
| 615 else | |
| 616 manager.reset(); | |
| 617 return manager; | 613 return manager; |
| 618 } | 614 } |
| 619 | 615 |
| 620 // static | 616 // static |
| 621 std::unique_ptr<BatteryStatusManager> BatteryStatusManager::Create( | 617 std::unique_ptr<BatteryStatusManager> BatteryStatusManager::Create( |
| 622 const BatteryStatusService::BatteryUpdateCallback& callback) { | 618 const BatteryStatusService::BatteryUpdateCallback& callback) { |
| 623 return std::unique_ptr<BatteryStatusManager>( | 619 return base::MakeUnique<BatteryStatusManagerLinux>(callback); |
| 624 new BatteryStatusManagerLinux(callback)); | |
| 625 } | 620 } |
| 626 | 621 |
| 627 } // namespace device | 622 } // namespace device |
| OLD | NEW |