Commit 8a896dcc authored by Alberto Herrera's avatar Alberto Herrera Committed by Commit Bot

[Bluetooth] Create PeripheralBatteryTracker and GattBatteryController

The PeripheralBatteryTracker is instantiated by ash::Shell and creates
one instance of GattBatteryController. The latter creates a
GattBatteryPoller object for each new connected Bluetooth Device.

Design doc at go/cros-bt-battery.

Bug: 785758
Change-Id: If7cda46a95afb36cc631f88047c6a117f0675894
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1660098
Commit-Queue: Alberto Herrera <ahrfgb@google.com>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarRyan Hansberry <hansberry@chromium.org>
Reviewed-by: default avatarJames Cook <jamescook@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#680632}
parent 9248cf3e
...@@ -508,10 +508,16 @@ component("ash") { ...@@ -508,10 +508,16 @@ component("ash") {
"policy/policy_recommendation_restorer.h", "policy/policy_recommendation_restorer.h",
"power/fake_gatt_battery_percentage_fetcher.cc", "power/fake_gatt_battery_percentage_fetcher.cc",
"power/fake_gatt_battery_percentage_fetcher.h", "power/fake_gatt_battery_percentage_fetcher.h",
"power/fake_gatt_battery_poller.cc",
"power/fake_gatt_battery_poller.h",
"power/gatt_battery_controller.cc",
"power/gatt_battery_controller.h",
"power/gatt_battery_percentage_fetcher.cc", "power/gatt_battery_percentage_fetcher.cc",
"power/gatt_battery_percentage_fetcher.h", "power/gatt_battery_percentage_fetcher.h",
"power/gatt_battery_poller.cc", "power/gatt_battery_poller.cc",
"power/gatt_battery_poller.h", "power/gatt_battery_poller.h",
"power/peripheral_battery_tracker.cc",
"power/peripheral_battery_tracker.h",
"root_window_controller.cc", "root_window_controller.cc",
"root_window_settings.cc", "root_window_settings.cc",
"root_window_settings.h", "root_window_settings.h",
...@@ -1718,6 +1724,7 @@ test("ash_unittests") { ...@@ -1718,6 +1724,7 @@ test("ash_unittests") {
"metrics/user_metrics_recorder_unittest.cc", "metrics/user_metrics_recorder_unittest.cc",
"multi_device_setup/multi_device_notification_presenter_unittest.cc", "multi_device_setup/multi_device_notification_presenter_unittest.cc",
"policy/policy_recommendation_restorer_unittest.cc", "policy/policy_recommendation_restorer_unittest.cc",
"power/gatt_battery_controller_unittest.cc",
"power/gatt_battery_percentage_fetcher_unittest.cc", "power/gatt_battery_percentage_fetcher_unittest.cc",
"power/gatt_battery_poller_unittest.cc", "power/gatt_battery_poller_unittest.cc",
"root_window_controller_unittest.cc", "root_window_controller_unittest.cc",
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/power/fake_gatt_battery_poller.h"
namespace ash {
FakeGattBatteryPoller::FakeGattBatteryPoller(
const std::string& device_address,
base::OnceClosure destructor_callback)
: GattBatteryPoller(device_address),
destructor_callback_(std::move(destructor_callback)) {}
FakeGattBatteryPoller::~FakeGattBatteryPoller() {
std::move(destructor_callback_).Run();
}
} // namespace ash
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ASH_POWER_FAKE_GATT_BATTERY_POLLER_H_
#define ASH_POWER_FAKE_GATT_BATTERY_POLLER_H_
#include <memory>
#include "ash/ash_export.h"
#include "ash/power/gatt_battery_poller.h"
#include "base/callback.h"
#include "base/macros.h"
namespace ash {
// Fake implementation of a GattBatteryPoller to use in tests.
class ASH_EXPORT FakeGattBatteryPoller : public GattBatteryPoller {
public:
FakeGattBatteryPoller(const std::string& device_address,
base::OnceClosure destructor_callback);
~FakeGattBatteryPoller() override;
private:
base::OnceClosure destructor_callback_;
DISALLOW_COPY_AND_ASSIGN(FakeGattBatteryPoller);
};
} // namespace ash
#endif // ASH_POWER_FAKE_GATT_BATTERY_POLLER_H_
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/power/gatt_battery_controller.h"
#include "ash/power/gatt_battery_poller.h"
#include "base/stl_util.h"
#include "base/timer/timer.h"
#include "device/bluetooth/bluetooth_device.h"
namespace ash {
GattBatteryController::GattBatteryController(
scoped_refptr<device::BluetoothAdapter> adapter)
: adapter_(adapter) {
adapter_->AddObserver(this);
}
GattBatteryController::~GattBatteryController() {
adapter_->RemoveObserver(this);
}
void GattBatteryController::DeviceConnectedStateChanged(
device::BluetoothAdapter* adapter,
device::BluetoothDevice* device,
bool is_now_connected) {
if (is_now_connected) {
EnsurePollerExistsForDevice(device);
return;
}
poller_map_.erase(device->GetAddress());
}
void GattBatteryController::DeviceAdded(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) {
if (device->IsConnected())
EnsurePollerExistsForDevice(device);
}
void GattBatteryController::DeviceRemoved(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) {
poller_map_.erase(device->GetAddress());
}
void GattBatteryController::EnsurePollerExistsForDevice(
device::BluetoothDevice* device) {
const std::string device_address = device->GetAddress();
// Don't do anything if the device is already registered.
if (base::Contains(poller_map_, device_address))
return;
poller_map_[device_address] = GattBatteryPoller::Factory::NewInstance(
adapter_, device_address, std::make_unique<base::OneShotTimer>());
}
} // namespace ash
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ASH_POWER_GATT_BATTERY_CONTROLLER_H_
#define ASH_POWER_GATT_BATTERY_CONTROLLER_H_
#include <memory>
#include "ash/ash_export.h"
#include "base/containers/flat_map.h"
#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "device/bluetooth/bluetooth_adapter.h"
namespace device {
class BluetoothDevice;
} // namespace device
namespace ash {
class GattBatteryPoller;
// Creates a GattBatteryPoller for each new connected Bluetooth device.
class ASH_EXPORT GattBatteryController
: public device::BluetoothAdapter::Observer {
public:
GattBatteryController(scoped_refptr<device::BluetoothAdapter> adapter);
~GattBatteryController() override;
private:
friend class GattBatteryControllerTest;
// device::BluetoothAdapter::Observer:
void DeviceConnectedStateChanged(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device,
bool is_now_connected) override;
void DeviceAdded(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) override;
void DeviceRemoved(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) override;
// Creates a GattBatteryPoller for the device if one doesn't exist already.
void EnsurePollerExistsForDevice(device::BluetoothDevice* device);
scoped_refptr<device::BluetoothAdapter> adapter_;
// Map of Pollers indexed by the Bluetooth address of the connected device
// they refer to.
base::flat_map<std::string, std::unique_ptr<GattBatteryPoller>> poller_map_;
DISALLOW_COPY_AND_ASSIGN(GattBatteryController);
};
} // namespace ash
#endif // ASH_POWER_GATT_BATTERY_CONTROLLER_H_
This diff is collapsed.
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/power/peripheral_battery_tracker.h"
#include "ash/power/gatt_battery_controller.h"
#include "base/bind.h"
#include "base/logging.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
namespace ash {
PeripheralBatteryTracker::PeripheralBatteryTracker() {
device::BluetoothAdapterFactory::GetAdapter(
base::BindOnce(&PeripheralBatteryTracker::InitializeOnBluetoothReady,
weak_ptr_factory_.GetWeakPtr()));
}
PeripheralBatteryTracker::~PeripheralBatteryTracker() = default;
void PeripheralBatteryTracker::InitializeOnBluetoothReady(
scoped_refptr<device::BluetoothAdapter> adapter) {
adapter_ = adapter;
DCHECK(adapter_.get());
gatt_battery_controller_ = std::make_unique<GattBatteryController>(adapter_);
}
} // namespace ash
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ASH_POWER_PERIPHERAL_BATTERY_TRACKER_H_
#define ASH_POWER_PERIPHERAL_BATTERY_TRACKER_H_
#include "ash/ash_export.h"
#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
namespace device {
class BluetoothAdapter;
} // namespace device
namespace ash {
class GattBatteryController;
// Creates instances of classes to collect the battery status of peripheral
// devices. Currently only tracks Bluetooth devices that support GATT.
// TODO(https://crbug.com/785758): Add support for other protocols, like HID.
class ASH_EXPORT PeripheralBatteryTracker {
public:
PeripheralBatteryTracker();
~PeripheralBatteryTracker();
private:
void InitializeOnBluetoothReady(
scoped_refptr<device::BluetoothAdapter> adapter);
scoped_refptr<device::BluetoothAdapter> adapter_;
std::unique_ptr<GattBatteryController> gatt_battery_controller_;
base::WeakPtrFactory<PeripheralBatteryTracker> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(PeripheralBatteryTracker);
};
} // namespace ash
#endif // ASH_POWER_PERIPHERAL_BATTERY_TRACKER_H_
...@@ -66,6 +66,7 @@ ...@@ -66,6 +66,7 @@
#include "ash/media/media_notification_controller_impl.h" #include "ash/media/media_notification_controller_impl.h"
#include "ash/multi_device_setup/multi_device_notification_presenter.h" #include "ash/multi_device_setup/multi_device_notification_presenter.h"
#include "ash/policy/policy_recommendation_restorer.h" #include "ash/policy/policy_recommendation_restorer.h"
#include "ash/power/peripheral_battery_tracker.h"
#include "ash/public/cpp/ash_constants.h" #include "ash/public/cpp/ash_constants.h"
#include "ash/public/cpp/ash_features.h" #include "ash/public/cpp/ash_features.h"
#include "ash/public/cpp/ash_prefs.h" #include "ash/public/cpp/ash_prefs.h"
...@@ -1123,6 +1124,10 @@ void Shell::Init( ...@@ -1123,6 +1124,10 @@ void Shell::Init(
cursor_manager_->SetCursor(ui::CursorType::kPointer); cursor_manager_->SetCursor(ui::CursorType::kPointer);
peripheral_battery_notifier_ = std::make_unique<PeripheralBatteryNotifier>(); peripheral_battery_notifier_ = std::make_unique<PeripheralBatteryNotifier>();
if (base::FeatureList::IsEnabled(
chromeos::features::kShowBluetoothDeviceBattery)) {
peripheral_battery_tracker_ = std::make_unique<PeripheralBatteryTracker>();
}
power_event_observer_.reset(new PowerEventObserver()); power_event_observer_.reset(new PowerEventObserver());
user_activity_notifier_ = user_activity_notifier_ =
std::make_unique<ui::UserActivityPowerManagerNotifier>( std::make_unique<ui::UserActivityPowerManagerNotifier>(
......
...@@ -148,6 +148,7 @@ class OverlayEventFilter; ...@@ -148,6 +148,7 @@ class OverlayEventFilter;
class OverviewController; class OverviewController;
class PartialMagnificationController; class PartialMagnificationController;
class PeripheralBatteryNotifier; class PeripheralBatteryNotifier;
class PeripheralBatteryTracker;
class PersistentWindowController; class PersistentWindowController;
class PolicyRecommendationRestorer; class PolicyRecommendationRestorer;
class PowerButtonController; class PowerButtonController;
...@@ -743,6 +744,7 @@ class ASH_EXPORT Shell : public SessionObserver, ...@@ -743,6 +744,7 @@ class ASH_EXPORT Shell : public SessionObserver,
std::unique_ptr<ScreenPinningController> screen_pinning_controller_; std::unique_ptr<ScreenPinningController> screen_pinning_controller_;
std::unique_ptr<PeripheralBatteryNotifier> peripheral_battery_notifier_; std::unique_ptr<PeripheralBatteryNotifier> peripheral_battery_notifier_;
std::unique_ptr<PeripheralBatteryTracker> peripheral_battery_tracker_;
std::unique_ptr<PowerEventObserver> power_event_observer_; std::unique_ptr<PowerEventObserver> power_event_observer_;
std::unique_ptr<PowerPrefs> power_prefs_; std::unique_ptr<PowerPrefs> power_prefs_;
std::unique_ptr<ui::UserActivityPowerManagerNotifier> user_activity_notifier_; std::unique_ptr<ui::UserActivityPowerManagerNotifier> user_activity_notifier_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment