Skip to content

Commit

Permalink
[floss] Make GetDiscoverableTimeout common
Browse files Browse the repository at this point in the history
Move GetDiscoverableTimeout into device::BluetoothAdapter so that we can
remove the adapter specialization in the arc bridge.

BUG=b:223444521
TEST=autoninja -C out_zork/Release chrome

Change-Id: Ic7b57b5dbbe8db1c3975e59bf0616a1b2272500b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4163919
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Commit-Queue: Abhishek Pandit-Subedi <abhishekpandit@google.com>
Reviewed-by: Kyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1098235}
  • Loading branch information
apandit authored and Chromium LUCI CQ committed Jan 28, 2023
1 parent 7311b8c commit 34bf312
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 17 deletions.
9 changes: 3 additions & 6 deletions chrome/browser/ash/arc/bluetooth/arc_bluetooth_bridge.cc
Expand Up @@ -478,10 +478,6 @@ void ArcBluetoothBridge::OnAdapterInitialized(
arc_bridge_service_->bluetooth()->SetHost(this);
}

bluez::BluetoothAdapterBlueZ* ArcBluetoothBridge::GetBluezAdapter() const {
return static_cast<bluez::BluetoothAdapterBlueZ*>(bluetooth_adapter_.get());
}

void ArcBluetoothBridge::AdapterPoweredChanged(BluetoothAdapter* adapter,
bool powered) {
AdapterPowerState power_change =
Expand Down Expand Up @@ -2664,8 +2660,9 @@ ArcBluetoothBridge::GetAdapterProperties(
}
if (type == mojom::BluetoothPropertyType::ALL ||
type == mojom::BluetoothPropertyType::ADAPTER_DISCOVERY_TIMEOUT) {
properties.push_back(mojom::BluetoothProperty::NewDiscoveryTimeout(
GetBluezAdapter()->GetDiscoverableTimeout()));
properties.push_back(
mojom::BluetoothProperty::NewDiscoveryTimeout(static_cast<uint32_t>(
bluetooth_adapter_->GetDiscoverableTimeout().InSeconds())));
}
if (type == mojom::BluetoothPropertyType::ALL ||
type == mojom::BluetoothPropertyType::LOCAL_LE_FEATURES) {
Expand Down
2 changes: 0 additions & 2 deletions chrome/browser/ash/arc/bluetooth/arc_bluetooth_bridge.h
Expand Up @@ -339,8 +339,6 @@ class ArcBluetoothBridge
ReleaseAdvertisementHandleCallback callback) override;

protected:
bluez::BluetoothAdapterBlueZ* GetBluezAdapter() const;

void ReserveAdvertisementHandleImpl(
ReserveAdvertisementHandleCallback callback);
void EnableAdvertisementImpl(
Expand Down
5 changes: 5 additions & 0 deletions device/bluetooth/bluetooth_adapter.h
Expand Up @@ -498,6 +498,11 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapter
base::OnceClosure callback,
ErrorCallback error_callback) = 0;

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
// Gets the current discoverable time for the adapter radio.
virtual base::TimeDelta GetDiscoverableTimeout() const = 0;
#endif

// Indicates whether the adapter is currently discovering new devices.
virtual bool IsDiscovering() const = 0;

Expand Down
6 changes: 6 additions & 0 deletions device/bluetooth/bluetooth_adapter_unittest.cc
Expand Up @@ -98,6 +98,12 @@ class TestBluetoothAdapter final : public BluetoothAdapter {
base::OnceClosure callback,
ErrorCallback error_callback) override {}

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
base::TimeDelta GetDiscoverableTimeout() const override {
return base::Microseconds(0);
}
#endif

bool IsDiscovering() const override { return false; }

UUIDList GetUUIDs() const override { return UUIDList(); }
Expand Down
6 changes: 3 additions & 3 deletions device/bluetooth/bluez/bluetooth_adapter_bluez.cc
Expand Up @@ -552,16 +552,16 @@ void BluetoothAdapterBlueZ::SetDiscoverable(bool discoverable,
std::move(error_callback)));
}

uint32_t BluetoothAdapterBlueZ::GetDiscoverableTimeout() const {
base::TimeDelta BluetoothAdapterBlueZ::GetDiscoverableTimeout() const {
if (!IsPresent())
return 0;
return base::Seconds(0);

bluez::BluetoothAdapterClient::Properties* properties =
bluez::BluezDBusManager::Get()
->GetBluetoothAdapterClient()
->GetProperties(object_path_);

return properties->discoverable_timeout.value();
return base::Seconds(properties->discoverable_timeout.value());
}

bool BluetoothAdapterBlueZ::IsDiscovering() const {
Expand Down
3 changes: 2 additions & 1 deletion device/bluetooth/bluez/bluetooth_adapter_bluez.h
Expand Up @@ -17,6 +17,7 @@
#include "base/containers/queue.h"
#include "base/gtest_prod_util.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "build/chromeos_buildflags.h"
#include "dbus/object_path.h"
Expand Down Expand Up @@ -136,7 +137,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterBlueZ final
void SetDiscoverable(bool discoverable,
base::OnceClosure callback,
ErrorCallback error_callback) override;
uint32_t GetDiscoverableTimeout() const;
base::TimeDelta GetDiscoverableTimeout() const override;
bool IsDiscovering() const override;
bool IsDiscoveringForTesting() const;
std::unordered_map<device::BluetoothDevice*, device::BluetoothDevice::UUIDSet>
Expand Down
4 changes: 2 additions & 2 deletions device/bluetooth/bluez/bluetooth_bluez_unittest.cc
Expand Up @@ -4268,8 +4268,8 @@ TEST_P(BluetoothBlueZTestP, GetConnectionInfoForConnectedDevice) {
}

TEST_F(BluetoothBlueZTest, GetDiscoverableTimeout) {
constexpr uint32_t kShortDiscoverableTimeout = 30;
constexpr uint32_t kLongDiscoverableTimeout = 240;
constexpr base::TimeDelta kShortDiscoverableTimeout = base::Seconds(30);
constexpr base::TimeDelta kLongDiscoverableTimeout = base::Seconds(240);
GetAdapter();
BluetoothAdapterBlueZ* adapter_bluez =
static_cast<BluetoothAdapterBlueZ*>(adapter_.get());
Expand Down
5 changes: 3 additions & 2 deletions device/bluetooth/dbus/fake_bluetooth_adapter_client.cc
Expand Up @@ -347,8 +347,9 @@ void FakeBluetoothAdapterClient::SetSecondUUIDs(
second_properties_->uuids.ReplaceValue(uuids);
}

void FakeBluetoothAdapterClient::SetDiscoverableTimeout(uint32_t timeout) {
properties_->discoverable_timeout.ReplaceValue(timeout);
void FakeBluetoothAdapterClient::SetDiscoverableTimeout(
base::TimeDelta timeout) {
properties_->discoverable_timeout.ReplaceValue(timeout.InSeconds());
}

void FakeBluetoothAdapterClient::OnPropertyChanged(
Expand Down
2 changes: 1 addition & 1 deletion device/bluetooth/dbus/fake_bluetooth_adapter_client.h
Expand Up @@ -97,7 +97,7 @@ class DEVICE_BLUETOOTH_EXPORT FakeBluetoothAdapterClient
void SetSecondUUIDs(const std::vector<std::string>& uuids);

// Set discoverable timeout
void SetDiscoverableTimeout(uint32_t timeout);
void SetDiscoverableTimeout(base::TimeDelta timeout);

// Object path, name and addresses of the adapters we emulate.
static const char kAdapterPath[];
Expand Down
9 changes: 9 additions & 0 deletions device/bluetooth/floss/bluetooth_adapter_floss.cc
Expand Up @@ -343,6 +343,15 @@ void BluetoothAdapterFloss::SetDiscoverable(bool discoverable,
discoverable);
}

base::TimeDelta BluetoothAdapterFloss::GetDiscoverableTimeout() const {
if (!IsPresent()) {
return base::Seconds(0);
}

return base::Seconds(
FlossDBusManager::Get()->GetAdapterClient()->GetDiscoverableTimeout());
}

bool BluetoothAdapterFloss::IsDiscovering() const {
if (!IsPresent())
return false;
Expand Down
2 changes: 2 additions & 0 deletions device/bluetooth/floss/bluetooth_adapter_floss.h
Expand Up @@ -11,6 +11,7 @@

#include "base/functional/callback.h"
#include "base/memory/scoped_refptr.h"
#include "base/time/time.h"
#include "build/chromeos_buildflags.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_device.h"
Expand Down Expand Up @@ -83,6 +84,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterFloss final
void SetDiscoverable(bool discoverable,
base::OnceClosure callback,
ErrorCallback error_callback) override;
base::TimeDelta GetDiscoverableTimeout() const override;
bool IsDiscovering() const override;

std::unordered_map<device::BluetoothDevice*, device::BluetoothDevice::UUIDSet>
Expand Down
18 changes: 18 additions & 0 deletions device/bluetooth/floss/floss_adapter_client.cc
Expand Up @@ -249,6 +249,8 @@ void FlossAdapterClient::Init(dbus::Bus* bus,
base::BindRepeating(&FlossAdapterClient::OnDiscoverableChanged,
weak_ptr_factory_.GetWeakPtr()));

UpdateDiscoverableTimeout();

dbus::MethodCall register_callback(kAdapterInterface,
adapter::kRegisterCallback);

Expand Down Expand Up @@ -313,6 +315,9 @@ void FlossAdapterClient::OnDiscoverableChanged(const bool& discoverable) {
for (auto& observer : observers_) {
observer.DiscoverableChanged(discoverable);
}

// Also update the discoverable timeout.
UpdateDiscoverableTimeout();
}

void FlossAdapterClient::OnDiscoveringChanged(
Expand Down Expand Up @@ -497,6 +502,19 @@ void FlossAdapterClient::OnDeviceDisconnected(
std::move(response_sender).Run(dbus::Response::FromMethodCall(method_call));
}

void FlossAdapterClient::UpdateDiscoverableTimeout() {
CallAdapterMethod<uint32_t>(
base::BindOnce(&FlossAdapterClient::OnDiscoverableTimeout,
weak_ptr_factory_.GetWeakPtr()),
adapter::kGetDiscoverableTimeout);
}

void FlossAdapterClient::OnDiscoverableTimeout(DBusResult<uint32_t> ret) {
if (ret.has_value()) {
discoverable_timeout_ = *ret;
}
}

FlossAdapterClient::FlossAdapterClient() = default;
FlossAdapterClient::~FlossAdapterClient() {
if (bus_) {
Expand Down
14 changes: 14 additions & 0 deletions device/bluetooth/floss/floss_adapter_client.h
Expand Up @@ -164,6 +164,10 @@ class DEVICE_BLUETOOTH_EXPORT FlossAdapterClient : public FlossDBusClient {
virtual void SetDiscoverable(ResponseCallback<Void> callback,
bool discoverable);

// Get the discoverable timeout for the adapter. Updates whenever the
// discoverable state changes.
uint32_t GetDiscoverableTimeout() const { return discoverable_timeout_; }

// Start a discovery session.
virtual void StartDiscovery(ResponseCallback<Void> callback);

Expand Down Expand Up @@ -302,6 +306,12 @@ class DEVICE_BLUETOOTH_EXPORT FlossAdapterClient : public FlossDBusClient {
// Handle GetConnectedDevices.
void OnGetConnectedDevices(DBusResult<std::vector<FlossDeviceId>> ret);

// Calls GetDiscoverableTimeout.
void UpdateDiscoverableTimeout();

// Handle GetDiscoverableTimeout and cache the returned value.
void OnDiscoverableTimeout(DBusResult<uint32_t> ret);

// List of observers interested in event notifications from this client.
base::ObserverList<Observer> observers_;

Expand All @@ -314,6 +324,10 @@ class DEVICE_BLUETOOTH_EXPORT FlossAdapterClient : public FlossDBusClient {
// Service which implements the adapter interface.
std::string service_name_;

// Cached discoverable timeout value (updates on init and on discoverable
// state changes).
uint32_t discoverable_timeout_ = 0;

private:
FRIEND_TEST_ALL_PREFIXES(FlossAdapterClientTest, CallAdapterMethods);

Expand Down
1 change: 1 addition & 0 deletions device/bluetooth/floss/floss_dbus_client.cc
Expand Up @@ -40,6 +40,7 @@ const char kGetAddress[] = "GetAddress";
const char kGetName[] = "GetName";
const char kSetName[] = "SetName";
const char kGetDiscoverable[] = "GetDiscoverable";
const char kGetDiscoverableTimeout[] = "GetDiscoverableTimeout";
const char kSetDiscoverable[] = "SetDiscoverable";
const char kStartDiscovery[] = "StartDiscovery";
const char kCancelDiscovery[] = "CancelDiscovery";
Expand Down
1 change: 1 addition & 0 deletions device/bluetooth/floss/floss_dbus_client.h
Expand Up @@ -48,6 +48,7 @@ extern DEVICE_BLUETOOTH_EXPORT const char kGetAddress[];
extern DEVICE_BLUETOOTH_EXPORT const char kGetName[];
extern DEVICE_BLUETOOTH_EXPORT const char kSetName[];
extern DEVICE_BLUETOOTH_EXPORT const char kGetDiscoverable[];
extern DEVICE_BLUETOOTH_EXPORT const char kGetDiscoverableTimeout[];
extern DEVICE_BLUETOOTH_EXPORT const char kSetDiscoverable[];
extern DEVICE_BLUETOOTH_EXPORT const char kStartDiscovery[];
extern DEVICE_BLUETOOTH_EXPORT const char kCancelDiscovery[];
Expand Down
7 changes: 7 additions & 0 deletions device/bluetooth/test/fake_central.cc
Expand Up @@ -554,6 +554,13 @@ void FakeCentral::SetDiscoverable(bool discoverable,
NOTREACHED();
}

#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
base::TimeDelta FakeCentral::GetDiscoverableTimeout() const {
NOTREACHED();
return base::Microseconds(0);
}
#endif

bool FakeCentral::IsDiscovering() const {
NOTREACHED();
return false;
Expand Down
3 changes: 3 additions & 0 deletions device/bluetooth/test/fake_central.h
Expand Up @@ -165,6 +165,9 @@ class FakeCentral final : public mojom::FakeCentral,
void SetDiscoverable(bool discoverable,
base::OnceClosure callback,
ErrorCallback error_callback) override;
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
base::TimeDelta GetDiscoverableTimeout() const override;
#endif
bool IsDiscovering() const override;
UUIDList GetUUIDs() const override;
void CreateRfcommService(const device::BluetoothUUID& uuid,
Expand Down
4 changes: 4 additions & 0 deletions device/bluetooth/test/mock_bluetooth_adapter.h
Expand Up @@ -78,6 +78,10 @@ class MockBluetoothAdapter : public BluetoothAdapter {
void(bool discoverable,
base::OnceClosure callback,
ErrorCallback error_callback));
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
MOCK_CONST_METHOD0(GetDiscoverableTimeout, base::TimeDelta());
#endif

MOCK_CONST_METHOD0(IsDiscovering, bool());
MOCK_METHOD2(
StartScanWithFilter_,
Expand Down

0 comments on commit 34bf312

Please sign in to comment.