Commit 0dc78f92 authored by Eli Ribble's avatar Eli Ribble Committed by Commit Bot

Add GattClientManager::GetConnectedDevices

This will eventually take the place of
GattClientManager::GetNumConnected. This moves bluetooth closer to being
fully async

Bug: 76155468
Test: chromecast/device/bluetooth/le/gatt_client_manager_impl_test.cc
Change-Id: I53d7eb0b2d5743165c1df5fe64903b86012a0346
Reviewed-on: https://chromium-review.googlesource.com/1147879Reviewed-by: default avatarBailey Forrest <bcf@chromium.org>
Reviewed-by: default avatarStephen Lanham <slan@chromium.org>
Commit-Queue: Eli Ribble <eliribble@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577748}
parent 56525d2a
......@@ -54,9 +54,6 @@ class GattClientManager {
virtual void AddObserver(Observer* o) = 0;
virtual void RemoveObserver(Observer* o) = 0;
// TODO(bcf/slan): Add new method:
// void GetDevices(Callback<void(vector<scoped_refptr<RemoteDevice>>)> cb);
// Get a RemoteDevice object corresponding to |addr| for performing GATT
// operations. |cb| will be run on the callers thread. Callbacks passed into
// methods on RemoteDevice and its subobjects (RemoteService,
......@@ -70,7 +67,13 @@ class GattClientManager {
virtual scoped_refptr<RemoteDevice> GetDeviceSync(
const bluetooth_v2_shlib::Addr& addr) = 0;
// Returns the currently connected devices.
using GetConnectDevicesCallback =
base::OnceCallback<void(std::vector<scoped_refptr<RemoteDevice>>)>;
virtual void GetConnectedDevices(GetConnectDevicesCallback cb) = 0;
// Returns the number of devices which are currently connected.
// TODO(bcf): Deprecated in favor of |GetConnectedDevices|.
virtual void GetNumConnected(base::OnceCallback<void(size_t)> cb) const = 0;
virtual void NotifyConnect(const bluetooth_v2_shlib::Addr& addr) = 0;
......
......@@ -94,6 +94,19 @@ scoped_refptr<RemoteDevice> GattClientManagerImpl::GetDeviceSync(
return new_device;
}
void GattClientManagerImpl::GetConnectedDevices(GetConnectDevicesCallback cb) {
MAKE_SURE_IO_THREAD(GetConnectedDevices,
BindToCurrentSequence(std::move(cb)));
std::vector<scoped_refptr<RemoteDevice>> devices;
for (const auto& device : addr_to_device_) {
if (device.second->IsConnected()) {
devices.push_back(device.second);
}
}
std::move(cb).Run(std::move(devices));
}
void GattClientManagerImpl::GetNumConnected(
base::OnceCallback<void(size_t)> cb) const {
MAKE_SURE_IO_THREAD(GetNumConnected, BindToCurrentSequence(std::move(cb)));
......
......@@ -37,6 +37,7 @@ class GattClientManagerImpl
base::OnceCallback<void(scoped_refptr<RemoteDevice>)> cb) override;
scoped_refptr<RemoteDevice> GetDeviceSync(
const bluetooth_v2_shlib::Addr& addr) override;
void GetConnectedDevices(GetConnectDevicesCallback cb) override;
void GetNumConnected(base::OnceCallback<void(size_t)> cb) const override;
void NotifyConnect(const bluetooth_v2_shlib::Addr& addr) override;
scoped_refptr<base::SingleThreadTaskRunner> task_runner() override;
......
......@@ -214,10 +214,12 @@ TEST_F(GattClientManagerTest, RemoteDeviceConnect) {
EXPECT_TRUE(device->IsConnected());
base::MockCallback<base::OnceCallback<void(size_t)>>
get_num_connected_callback;
EXPECT_CALL(get_num_connected_callback, Run(1));
gatt_client_manager_->GetNumConnected(get_num_connected_callback.Get());
base::MockCallback<
base::OnceCallback<void(std::vector<scoped_refptr<RemoteDevice>>)>>
get_connected_callback;
const std::vector<scoped_refptr<RemoteDevice>> kExpectedDevices({device});
EXPECT_CALL(get_connected_callback, Run(kExpectedDevices));
gatt_client_manager_->GetConnectedDevices(get_connected_callback.Get());
base::RunLoop().RunUntilIdle();
......
......@@ -5,6 +5,8 @@
#ifndef CHROMECAST_DEVICE_BLUETOOTH_LE_MOCK_GATT_CLIENT_MANAGER_H_
#define CHROMECAST_DEVICE_BLUETOOTH_LE_MOCK_GATT_CLIENT_MANAGER_H_
#include <vector>
#include "base/observer_list.h"
#include "chromecast/device/bluetooth/le/gatt_client_manager.h"
#include "chromecast/device/bluetooth/le/mock_remote_device.h"
......@@ -29,9 +31,16 @@ class MockGattClientManager : public GattClientManager {
base::OnceCallback<void(scoped_refptr<RemoteDevice>)> cb) override {
std::move(cb).Run(GetDevice(addr));
}
MOCK_METHOD1(
GetDeviceSync,
scoped_refptr<RemoteDevice>(const bluetooth_v2_shlib::Addr& addr));
MOCK_METHOD0(GetConnectedDevices, std::vector<scoped_refptr<RemoteDevice>>());
void GetConnectedDevices(GetConnectDevicesCallback cb) {
std::move(cb).Run(GetConnectedDevices());
}
MOCK_CONST_METHOD1(GetNumConnected,
void(base::OnceCallback<void(size_t)> cb));
MOCK_METHOD1(NotifyConnect, void(const bluetooth_v2_shlib::Addr& addr));
......
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