From 34bf312377f356761705e99482a021bf04201ebf Mon Sep 17 00:00:00 2001 From: Abhishek Pandit-Subedi Date: Sat, 28 Jan 2023 02:29:35 +0000 Subject: [PATCH] [floss] Make GetDiscoverableTimeout common 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 Commit-Queue: Abhishek Pandit-Subedi Reviewed-by: Kyle Horimoto Cr-Commit-Position: refs/heads/main@{#1098235} --- .../ash/arc/bluetooth/arc_bluetooth_bridge.cc | 9 +++------ .../ash/arc/bluetooth/arc_bluetooth_bridge.h | 2 -- device/bluetooth/bluetooth_adapter.h | 5 +++++ device/bluetooth/bluetooth_adapter_unittest.cc | 6 ++++++ .../bluetooth/bluez/bluetooth_adapter_bluez.cc | 6 +++--- .../bluetooth/bluez/bluetooth_adapter_bluez.h | 3 ++- .../bluez/bluetooth_bluez_unittest.cc | 4 ++-- .../dbus/fake_bluetooth_adapter_client.cc | 5 +++-- .../dbus/fake_bluetooth_adapter_client.h | 2 +- .../bluetooth/floss/bluetooth_adapter_floss.cc | 9 +++++++++ .../bluetooth/floss/bluetooth_adapter_floss.h | 2 ++ device/bluetooth/floss/floss_adapter_client.cc | 18 ++++++++++++++++++ device/bluetooth/floss/floss_adapter_client.h | 14 ++++++++++++++ device/bluetooth/floss/floss_dbus_client.cc | 1 + device/bluetooth/floss/floss_dbus_client.h | 1 + device/bluetooth/test/fake_central.cc | 7 +++++++ device/bluetooth/test/fake_central.h | 3 +++ device/bluetooth/test/mock_bluetooth_adapter.h | 4 ++++ 18 files changed, 84 insertions(+), 17 deletions(-) diff --git a/chrome/browser/ash/arc/bluetooth/arc_bluetooth_bridge.cc b/chrome/browser/ash/arc/bluetooth/arc_bluetooth_bridge.cc index f0bc71eee81a9..c10334a601631 100644 --- a/chrome/browser/ash/arc/bluetooth/arc_bluetooth_bridge.cc +++ b/chrome/browser/ash/arc/bluetooth/arc_bluetooth_bridge.cc @@ -478,10 +478,6 @@ void ArcBluetoothBridge::OnAdapterInitialized( arc_bridge_service_->bluetooth()->SetHost(this); } -bluez::BluetoothAdapterBlueZ* ArcBluetoothBridge::GetBluezAdapter() const { - return static_cast(bluetooth_adapter_.get()); -} - void ArcBluetoothBridge::AdapterPoweredChanged(BluetoothAdapter* adapter, bool powered) { AdapterPowerState power_change = @@ -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( + bluetooth_adapter_->GetDiscoverableTimeout().InSeconds()))); } if (type == mojom::BluetoothPropertyType::ALL || type == mojom::BluetoothPropertyType::LOCAL_LE_FEATURES) { diff --git a/chrome/browser/ash/arc/bluetooth/arc_bluetooth_bridge.h b/chrome/browser/ash/arc/bluetooth/arc_bluetooth_bridge.h index 2ae078b781c9c..b88f1a4a33f1f 100644 --- a/chrome/browser/ash/arc/bluetooth/arc_bluetooth_bridge.h +++ b/chrome/browser/ash/arc/bluetooth/arc_bluetooth_bridge.h @@ -339,8 +339,6 @@ class ArcBluetoothBridge ReleaseAdvertisementHandleCallback callback) override; protected: - bluez::BluetoothAdapterBlueZ* GetBluezAdapter() const; - void ReserveAdvertisementHandleImpl( ReserveAdvertisementHandleCallback callback); void EnableAdvertisementImpl( diff --git a/device/bluetooth/bluetooth_adapter.h b/device/bluetooth/bluetooth_adapter.h index 9505f0af3add0..dc016fba022cf 100644 --- a/device/bluetooth/bluetooth_adapter.h +++ b/device/bluetooth/bluetooth_adapter.h @@ -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; diff --git a/device/bluetooth/bluetooth_adapter_unittest.cc b/device/bluetooth/bluetooth_adapter_unittest.cc index 1da5c99e15f68..a3ced80b0ea6e 100644 --- a/device/bluetooth/bluetooth_adapter_unittest.cc +++ b/device/bluetooth/bluetooth_adapter_unittest.cc @@ -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(); } diff --git a/device/bluetooth/bluez/bluetooth_adapter_bluez.cc b/device/bluetooth/bluez/bluetooth_adapter_bluez.cc index 496d6ee338222..fc836aae10f06 100644 --- a/device/bluetooth/bluez/bluetooth_adapter_bluez.cc +++ b/device/bluetooth/bluez/bluetooth_adapter_bluez.cc @@ -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 { diff --git a/device/bluetooth/bluez/bluetooth_adapter_bluez.h b/device/bluetooth/bluez/bluetooth_adapter_bluez.h index 33b70d7fd1e72..4637d696e4637 100644 --- a/device/bluetooth/bluez/bluetooth_adapter_bluez.h +++ b/device/bluetooth/bluez/bluetooth_adapter_bluez.h @@ -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" @@ -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 diff --git a/device/bluetooth/bluez/bluetooth_bluez_unittest.cc b/device/bluetooth/bluez/bluetooth_bluez_unittest.cc index 2c1f69f98bcb8..fa62e19a4f81e 100644 --- a/device/bluetooth/bluez/bluetooth_bluez_unittest.cc +++ b/device/bluetooth/bluez/bluetooth_bluez_unittest.cc @@ -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(adapter_.get()); diff --git a/device/bluetooth/dbus/fake_bluetooth_adapter_client.cc b/device/bluetooth/dbus/fake_bluetooth_adapter_client.cc index 66c45d5705b7e..f997c17d71e34 100644 --- a/device/bluetooth/dbus/fake_bluetooth_adapter_client.cc +++ b/device/bluetooth/dbus/fake_bluetooth_adapter_client.cc @@ -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( diff --git a/device/bluetooth/dbus/fake_bluetooth_adapter_client.h b/device/bluetooth/dbus/fake_bluetooth_adapter_client.h index 148396c3d7f8c..97db46e602309 100644 --- a/device/bluetooth/dbus/fake_bluetooth_adapter_client.h +++ b/device/bluetooth/dbus/fake_bluetooth_adapter_client.h @@ -97,7 +97,7 @@ class DEVICE_BLUETOOTH_EXPORT FakeBluetoothAdapterClient void SetSecondUUIDs(const std::vector& 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[]; diff --git a/device/bluetooth/floss/bluetooth_adapter_floss.cc b/device/bluetooth/floss/bluetooth_adapter_floss.cc index 554154085e465..7184007fc991b 100644 --- a/device/bluetooth/floss/bluetooth_adapter_floss.cc +++ b/device/bluetooth/floss/bluetooth_adapter_floss.cc @@ -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; diff --git a/device/bluetooth/floss/bluetooth_adapter_floss.h b/device/bluetooth/floss/bluetooth_adapter_floss.h index 9a0b9925334d9..549ff14a9cba2 100644 --- a/device/bluetooth/floss/bluetooth_adapter_floss.h +++ b/device/bluetooth/floss/bluetooth_adapter_floss.h @@ -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" @@ -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 diff --git a/device/bluetooth/floss/floss_adapter_client.cc b/device/bluetooth/floss/floss_adapter_client.cc index 0e462b6e39fe0..b4df6f65f8f05 100644 --- a/device/bluetooth/floss/floss_adapter_client.cc +++ b/device/bluetooth/floss/floss_adapter_client.cc @@ -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); @@ -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( @@ -497,6 +502,19 @@ void FlossAdapterClient::OnDeviceDisconnected( std::move(response_sender).Run(dbus::Response::FromMethodCall(method_call)); } +void FlossAdapterClient::UpdateDiscoverableTimeout() { + CallAdapterMethod( + base::BindOnce(&FlossAdapterClient::OnDiscoverableTimeout, + weak_ptr_factory_.GetWeakPtr()), + adapter::kGetDiscoverableTimeout); +} + +void FlossAdapterClient::OnDiscoverableTimeout(DBusResult ret) { + if (ret.has_value()) { + discoverable_timeout_ = *ret; + } +} + FlossAdapterClient::FlossAdapterClient() = default; FlossAdapterClient::~FlossAdapterClient() { if (bus_) { diff --git a/device/bluetooth/floss/floss_adapter_client.h b/device/bluetooth/floss/floss_adapter_client.h index fe7130891a787..75e33a496e446 100644 --- a/device/bluetooth/floss/floss_adapter_client.h +++ b/device/bluetooth/floss/floss_adapter_client.h @@ -164,6 +164,10 @@ class DEVICE_BLUETOOTH_EXPORT FlossAdapterClient : public FlossDBusClient { virtual void SetDiscoverable(ResponseCallback 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 callback); @@ -302,6 +306,12 @@ class DEVICE_BLUETOOTH_EXPORT FlossAdapterClient : public FlossDBusClient { // Handle GetConnectedDevices. void OnGetConnectedDevices(DBusResult> ret); + // Calls GetDiscoverableTimeout. + void UpdateDiscoverableTimeout(); + + // Handle GetDiscoverableTimeout and cache the returned value. + void OnDiscoverableTimeout(DBusResult ret); + // List of observers interested in event notifications from this client. base::ObserverList observers_; @@ -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); diff --git a/device/bluetooth/floss/floss_dbus_client.cc b/device/bluetooth/floss/floss_dbus_client.cc index 4de0bb414e4c2..9da00d1872abf 100644 --- a/device/bluetooth/floss/floss_dbus_client.cc +++ b/device/bluetooth/floss/floss_dbus_client.cc @@ -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"; diff --git a/device/bluetooth/floss/floss_dbus_client.h b/device/bluetooth/floss/floss_dbus_client.h index da552cc0751bc..811c1a6c6d298 100644 --- a/device/bluetooth/floss/floss_dbus_client.h +++ b/device/bluetooth/floss/floss_dbus_client.h @@ -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[]; diff --git a/device/bluetooth/test/fake_central.cc b/device/bluetooth/test/fake_central.cc index 8cc9df7372b45..1d7060a4f7af0 100644 --- a/device/bluetooth/test/fake_central.cc +++ b/device/bluetooth/test/fake_central.cc @@ -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; diff --git a/device/bluetooth/test/fake_central.h b/device/bluetooth/test/fake_central.h index 3db7808b2e307..bc2633444023c 100644 --- a/device/bluetooth/test/fake_central.h +++ b/device/bluetooth/test/fake_central.h @@ -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, diff --git a/device/bluetooth/test/mock_bluetooth_adapter.h b/device/bluetooth/test/mock_bluetooth_adapter.h index 1fd7c3ee23aab..8337a19d31f48 100644 --- a/device/bluetooth/test/mock_bluetooth_adapter.h +++ b/device/bluetooth/test/mock_bluetooth_adapter.h @@ -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_,