Remove unused BluetoothClassicMedium::Observer.

PiperOrigin-RevId: 953415728
This commit is contained in:
Francis Tsui
2026-07-24 09:50:15 -07:00
committed by Copybara-Service
parent 8a1bbd074e
commit a7f6ed3fa7
12 changed files with 3 additions and 271 deletions
-2
View File
@@ -321,7 +321,6 @@ cc_library(
":types",
":uuid",
"//connections/implementation/flags:connections_flags",
"//internal/base",
"//internal/flags:nearby_flags",
"//internal/platform/implementation:comm",
"//internal/platform/implementation:platform",
@@ -372,7 +371,6 @@ cc_library(
":mac_address",
":types",
":uuid",
"//internal/base",
"//internal/platform/implementation:comm",
"//internal/platform/implementation:webrtc_platform",
"//internal/platform/implementation:wifi_utils",
-73
View File
@@ -19,7 +19,6 @@
#include <utility>
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "internal/platform/bluetooth_adapter.h"
#include "internal/platform/cancellation_flag.h"
#include "internal/platform/implementation/bluetooth_classic.h"
@@ -57,11 +56,6 @@ MediumSocket* BluetoothSocket::CreateVirtualSocket(
}
BluetoothClassicMedium::~BluetoothClassicMedium() {
LOG(INFO) << "~BluetoothClassicMedium: observer_list_ size: "
<< observer_list_.size();
if (!observer_list_.empty()) {
impl_->RemoveObserver(this);
}
StopDiscovery();
LOG(INFO) << "eof ~BluetoothClassicMedium";
}
@@ -160,71 +154,4 @@ bool BluetoothClassicMedium::StopDiscovery() {
return impl_->StopDiscovery();
}
void BluetoothClassicMedium::AddObserver(Observer* observer) {
LOG(INFO) << "BT AddObserver; impl=" << &GetImpl();
MutexLock lock(&mutex_);
if (observer_list_.empty()) {
impl_->AddObserver(this);
}
observer_list_.AddObserver(observer);
LOG(INFO) << "BT AddObserver done";
}
void BluetoothClassicMedium::RemoveObserver(Observer* observer) {
LOG(INFO) << "BT RemoveObserver; impl=" << &GetImpl();
MutexLock lock(&mutex_);
observer_list_.RemoveObserver(observer);
if (observer_list_.empty()) {
impl_->RemoveObserver(this);
}
LOG(INFO) << "BT RemoveObserver done";
}
// api::BluetoothClassicMedium::Observer methods
void BluetoothClassicMedium::DeviceAdded(api::BluetoothDevice& device) {
VLOG(1) << "BT DeviceAdded; name=" << device.GetName()
<< ", address=" << device.GetMacAddress().ToString();
BluetoothDevice bt_device(&device);
for (auto* observer : observer_list_.GetObservers()) {
observer->DeviceAdded(bt_device);
}
}
void BluetoothClassicMedium::DeviceRemoved(api::BluetoothDevice& device) {
VLOG(1) << "BT DeviceRemoved; name=" << device.GetName()
<< ", address=" << device.GetMacAddress().ToString();
BluetoothDevice bt_device(&device);
for (auto* observer : observer_list_.GetObservers()) {
observer->DeviceRemoved(bt_device);
}
}
void BluetoothClassicMedium::DeviceAddressChanged(
api::BluetoothDevice& device, absl::string_view old_address) {
VLOG(1) << "BT DeviceAddressChanged; name=" << device.GetName()
<< ", address=" << device.GetMacAddress().ToString()
<< ", old_address=" << old_address;
BluetoothDevice bt_device(&device);
for (auto* observer : observer_list_.GetObservers()) {
observer->DeviceAddressChanged(bt_device, old_address);
}
}
void BluetoothClassicMedium::DevicePairedChanged(api::BluetoothDevice& device,
bool new_paired_status) {
VLOG(1) << "BT DevicePairedChanged; name=" << device.GetName()
<< ", address=" << device.GetMacAddress().ToString()
<< ", status=" << new_paired_status;
BluetoothDevice bt_device(&device);
for (auto* observer : observer_list_.GetObservers()) {
observer->DevicePairedChanged(bt_device, new_paired_status);
}
}
void BluetoothClassicMedium::DeviceConnectedStateChanged(
api::BluetoothDevice& device, bool connected) {
VLOG(1) << "BT DeviceConnectedStateChanged: name=" << device.GetName()
<< ", address=" << device.GetMacAddress().ToString()
<< ", connected=" << connected;
BluetoothDevice bt_device(&device);
for (auto* observer : observer_list_.GetObservers()) {
observer->DeviceConnectedStateChanged(bt_device, connected);
}
}
} // namespace nearby
+2 -49
View File
@@ -26,7 +26,6 @@
#include "absl/container/flat_hash_map.h"
#include "absl/functional/any_invocable.h"
#include "absl/strings/string_view.h"
#include "internal/base/observer_list.h"
#include "internal/platform/blocking_queue_stream.h"
#include "internal/platform/bluetooth_adapter.h"
#include "internal/platform/byte_array.h"
@@ -223,7 +222,7 @@ class BluetoothPairing final {
// Container of operations that can be performed over the Bluetooth Classic
// medium.
class BluetoothClassicMedium : public api::BluetoothClassicMedium::Observer {
class BluetoothClassicMedium {
public:
using Platform = api::ImplementationPlatform;
struct DiscoveryCallback {
@@ -246,39 +245,11 @@ class BluetoothClassicMedium : public api::BluetoothClassicMedium::Observer {
BluetoothDevice device;
};
class Observer {
public:
virtual ~Observer() = default;
// Called when a new `device` is added. The `device` parameter becomes
// invalid after the call.
virtual void DeviceAdded(BluetoothDevice& device) {}
// Called when `device` is removed. The `device` parameter becomes invalid
// after the call.
virtual void DeviceRemoved(BluetoothDevice& device) {}
// Called when the address of `device` changed due to pairing. The
// `device` parameter becomes invalid after the call.
virtual void DeviceAddressChanged(BluetoothDevice& device,
absl::string_view old_address) {}
// Called when the paired property of `device` changed. The `device`
// parameter becomes invalid after the call.
virtual void DevicePairedChanged(BluetoothDevice& device,
bool new_paired_status) {}
// Called when `device` has connected or disconnected. The `device`
// parameter becomes invalid after the call.
virtual void DeviceConnectedStateChanged(BluetoothDevice& device,
bool connected) {}
};
explicit BluetoothClassicMedium(BluetoothAdapter& adapter)
: impl_(Platform::CreateBluetoothClassicMedium(adapter.GetImpl())),
adapter_(adapter) {}
~BluetoothClassicMedium() override;
virtual ~BluetoothClassicMedium();
// NOTE(DiscoveryCallback):
// BluetoothDevice is a proxy object created as a result of BT discovery.
@@ -353,23 +324,6 @@ class BluetoothClassicMedium : public api::BluetoothClassicMedium::Observer {
return BluetoothDevice(impl_->GetRemoteDevice(mac_address));
}
// Adds an observer. `observer` must be valid until RemoveObserver is called,
// or BluetoothClassicMedium is destroyed.
void AddObserver(Observer* observer);
// Removes an observer. It's OK to remove an unregistered observer.
void RemoveObserver(Observer* observer);
// api::BluetoothClassicMedium::Observer methods
void DeviceAdded(api::BluetoothDevice& device) override;
void DeviceRemoved(api::BluetoothDevice& device) override;
void DeviceAddressChanged(api::BluetoothDevice& device,
absl::string_view old_address) override;
void DevicePairedChanged(api::BluetoothDevice& device,
bool new_paired_status) override;
void DeviceConnectedStateChanged(api::BluetoothDevice& device,
bool connected) override;
private:
Mutex mutex_;
std::unique_ptr<api::BluetoothClassicMedium> impl_;
@@ -379,7 +333,6 @@ class BluetoothClassicMedium : public api::BluetoothClassicMedium::Observer {
devices_ ABSL_GUARDED_BY(mutex_);
DiscoveryCallback discovery_callback_ ABSL_GUARDED_BY(mutex_);
bool discovery_enabled_ ABSL_GUARDED_BY(mutex_) = false;
ObserverList<Observer> observer_list_;
};
} // namespace nearby
@@ -49,39 +49,6 @@ constexpr FeatureFlags kTestCases[] = {
},
};
class BluetoothClassicMediumObserver
: public BluetoothClassicMedium ::Observer {
public:
explicit BluetoothClassicMediumObserver(
CountDownLatch* device_added_latch, CountDownLatch* device_removed_latch,
CountDownLatch* device_paired_changed_latch)
: device_added_latch_(device_added_latch),
device_removed_latch_(device_removed_latch),
device_paired_changed_latch_(device_paired_changed_latch) {}
void DeviceAdded(BluetoothDevice& device) override {
if (!device_added_latch_) return;
device_added_latch_->CountDown();
}
void DeviceRemoved(BluetoothDevice& device) override {
if (!device_removed_latch_) return;
device_removed_latch_->CountDown();
}
void DevicePairedChanged(BluetoothDevice& device,
bool new_paired_status) override {
if (!device_paired_changed_latch_) return;
paired_status_ = new_paired_status;
device_paired_changed_latch_->CountDown();
}
CountDownLatch* device_added_latch_;
CountDownLatch* device_removed_latch_;
CountDownLatch* device_paired_changed_latch_;
bool paired_status_ = false;
};
class BluetoothClassicMediumTest
: public ::testing::TestWithParam<FeatureFlags> {
protected:
@@ -341,11 +308,6 @@ TEST_F(BluetoothClassicMediumTest, CanStartDiscovery) {
adapter_a_->SetScanMode(BluetoothAdapter::ScanMode::kConnectable);
CountDownLatch found_latch(1);
CountDownLatch lost_latch(1);
CountDownLatch device_added_latch(1);
CountDownLatch device_removed_latch(1);
BluetoothClassicMediumObserver observer(&device_added_latch,
&device_removed_latch, nullptr);
bt_a_->AddObserver(&observer);
bt_a_->StartDiscovery(DiscoveryCallback{
.device_discovered_cb =
@@ -365,21 +327,15 @@ TEST_F(BluetoothClassicMediumTest, CanStartDiscovery) {
EXPECT_EQ(adapter_b_->GetScanMode(),
BluetoothAdapter::ScanMode::kConnectableDiscoverable);
EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result());
EXPECT_TRUE(device_added_latch.Await(absl::Milliseconds(1000)).result());
adapter_b_->SetStatus(BluetoothAdapter::Status::kDisabled);
EXPECT_FALSE(adapter_b_->IsEnabled());
EXPECT_TRUE(lost_latch.Await(absl::Milliseconds(1000)).result());
EXPECT_TRUE(device_removed_latch.Await(absl::Milliseconds(1000)).result());
}
TEST_F(BluetoothClassicMediumTest, DiscoveryCallbackAfterStopDiscovery) {
SingleThreadExecutor executor;
adapter_a_->SetScanMode(BluetoothAdapter::ScanMode::kConnectable);
CountDownLatch found_latch(1);
CountDownLatch device_added_latch(1);
BluetoothClassicMediumObserver observer(&device_added_latch, nullptr,
nullptr);
bt_a_->AddObserver(&observer);
bt_a_->StartDiscovery(DiscoveryCallback{
.device_discovered_cb =
@@ -397,7 +353,6 @@ TEST_F(BluetoothClassicMediumTest, DiscoveryCallbackAfterStopDiscovery) {
BluetoothAdapter::ScanMode::kConnectableDiscoverable);
bt_a_->StopDiscovery();
EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result());
EXPECT_TRUE(device_added_latch.Await(absl::Milliseconds(1000)).result());
executor.Shutdown();
}
@@ -491,10 +446,6 @@ TEST_F(BluetoothClassicMediumTest, BluetoothPairingSuccess) {
CountDownLatch paired_latch(1);
CountDownLatch initiated_latch(1);
CountDownLatch error_latch(1);
CountDownLatch device_paired_latch(1);
BluetoothClassicMediumObserver observer(nullptr, nullptr,
&device_paired_latch);
bt_a_->AddObserver(&observer);
EXPECT_TRUE(bluetooth_pairing->InitiatePairing({
.on_paired_cb = [&]() { paired_latch.CountDown(); },
.on_pairing_error_cb =
@@ -520,8 +471,6 @@ TEST_F(BluetoothClassicMediumTest, BluetoothPairingSuccess) {
// Finishes pairing with remote device.
EXPECT_TRUE(bluetooth_pairing->FinishPairing(received_passkey));
paired_latch.Await();
device_paired_latch.Await();
EXPECT_TRUE(observer.paired_status_);
EXPECT_TRUE(bluetooth_pairing->IsPaired());
// Unpairs with remote device.
@@ -211,29 +211,6 @@ class BluetoothClassicMedium {
DefaultCallback<BluetoothDevice&>();
};
class Observer {
public:
virtual ~Observer() = default;
// Called when a new `device` is added to the adapter.
virtual void DeviceAdded(BluetoothDevice& device) {}
// Called when `device` is removed from the adapter.
virtual void DeviceRemoved(BluetoothDevice& device) {}
// Called when the address of `device` changed due to pairing.
virtual void DeviceAddressChanged(BluetoothDevice& device,
absl::string_view old_address) {}
// Called when the paired property of `device` changed.
virtual void DevicePairedChanged(BluetoothDevice& device,
bool new_paired_status) {}
// Called when `device` has connected or disconnected.
virtual void DeviceConnectedStateChanged(BluetoothDevice& device,
bool connected) {}
};
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#startDiscovery()
//
// Returns true once the process of discovery has been initiated.
@@ -283,9 +260,6 @@ class BluetoothClassicMedium {
BluetoothDevice& remote_device) = 0;
virtual BluetoothDevice* GetRemoteDevice(MacAddress mac_address) = 0;
virtual void AddObserver(Observer* observer) = 0;
virtual void RemoveObserver(Observer* observer) = 0;
};
} // namespace nearby::api
@@ -264,15 +264,5 @@ api::BluetoothDevice* BluetoothClassicMedium::GetRemoteDevice(
return MediumEnvironment::Instance().FindBluetoothDevice(mac_address);
}
void BluetoothClassicMedium::AddObserver(
api::BluetoothClassicMedium::Observer* observer) {
MediumEnvironment::Instance().AddObserver(observer);
}
void BluetoothClassicMedium::RemoveObserver(
api::BluetoothClassicMedium::Observer* observer) {
MediumEnvironment::Instance().RemoveObserver(observer);
}
} // namespace g3
} // namespace nearby
@@ -206,9 +206,6 @@ class BluetoothClassicMedium : public api::BluetoothClassicMedium {
api::BluetoothDevice* GetRemoteDevice(MacAddress mac_address) override;
void AddObserver(Observer* observer) override;
void RemoveObserver(Observer* observer) override;
private:
absl::Mutex mutex_;
BluetoothAdapter* adapter_; // Our device adapter; read-only.
@@ -701,9 +701,6 @@ winrt::fire_and_forget BluetoothClassicMedium::DeviceWatcher_Added(
if (discovery_callback_.device_discovered_cb != nullptr) {
discovery_callback_.device_discovered_cb(*device);
}
for (auto& observer : observers_.GetObservers()) {
observer->DeviceAdded(*device);
}
return winrt::fire_and_forget();
}
@@ -769,9 +766,6 @@ winrt::fire_and_forget BluetoothClassicMedium::DeviceWatcher_Updated(
LOG(INFO) << __func__
<< ": Notifying device paired changed: " << std::boolalpha
<< new_paired_status;
for (auto& observer : observers_.GetObservers()) {
observer->DevicePairedChanged(*device, new_paired_status);
}
}
return winrt::fire_and_forget();
@@ -819,10 +813,6 @@ winrt::fire_and_forget BluetoothClassicMedium::DeviceWatcher_Removed(
discovery_callback_.device_lost_cb(*device);
}
for (auto& observer : observers_.GetObservers()) {
observer->DeviceRemoved(*device);
}
RemoveRemoteDevice(mac_address);
return winrt::fire_and_forget();
@@ -21,7 +21,6 @@
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_map.h"
#include "absl/synchronization/mutex.h"
#include "internal/base/observer_list.h"
#include "internal/platform/cancellation_flag.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/implementation/bluetooth_classic.h"
@@ -90,15 +89,6 @@ class BluetoothClassicMedium : public api::BluetoothClassicMedium {
std::unique_ptr<api::BluetoothPairing> CreatePairing(
api::BluetoothDevice& remote_device) override;
void AddObserver(Observer* observer) override {
observers_.AddObserver(observer);
}
// Removes an observer. It's OK to remove an unregistered observer.
void RemoveObserver(Observer* observer) override {
observers_.RemoveObserver(observer);
}
private:
bool StartScanning();
bool StopScanning();
@@ -191,7 +181,6 @@ class BluetoothClassicMedium : public api::BluetoothClassicMedium {
// the shared_ptr.
BluetoothServerSocket* raw_server_socket_ = nullptr;
bool is_radio_discoverable_ = false;
ObserverList<Observer> observers_;
};
} // namespace nearby::windows
@@ -19,6 +19,7 @@
#include "absl/synchronization/mutex.h"
#include "internal/platform/implementation/mutex.h"
#include "internal/platform/condition_variable.h"
namespace nearby {
namespace windows {
-31
View File
@@ -192,9 +192,6 @@ void MediumEnvironment::OnBluetoothDeviceStateChanged(
if (enable_notifications_) {
VLOG(1) << "Notify about new discovered device";
info.callback.device_discovered_cb(device);
for (auto& observer : observers_.GetObservers()) {
observer->DeviceAdded(device);
}
}
}
} else {
@@ -215,9 +212,6 @@ void MediumEnvironment::OnBluetoothDeviceStateChanged(
if (enable_notifications_) {
VLOG(1) << "Notify about existing discovered device";
info.callback.device_discovered_cb(device);
for (auto& observer : observers_.GetObservers()) {
observer->DeviceAdded(device);
}
}
}
}
@@ -227,9 +221,6 @@ void MediumEnvironment::OnBluetoothDeviceStateChanged(
if (enable_notifications_) {
VLOG(1) << "Notify about removed device";
info.callback.device_lost_cb(device);
for (auto& observer : observers_.GetObservers()) {
observer->DeviceRemoved(device);
}
}
info.devices.erase(item);
}
@@ -1291,11 +1282,6 @@ bool MediumEnvironment::SetPairingState(api::BluetoothDevice* device,
latch.CountDown();
});
latch.Await();
if (enable_notifications_) {
for (auto& observer : observers_.GetObservers()) {
observer->DevicePairedChanged(*device, true);
}
}
return updated;
}
@@ -1359,11 +1345,6 @@ bool MediumEnvironment::FinishPairing(api::BluetoothDevice* device) {
pairing_context->pairing_error.value());
} else {
pairing_context->is_paired = true;
if (enable_notifications_) {
for (auto& observer : observers_.GetObservers()) {
observer->DevicePairedChanged(*device, true);
}
}
pairing_context->pairing_callback.on_paired_cb();
}
return finshed;
@@ -1409,18 +1390,6 @@ void MediumEnvironment::ClearBluetoothDevicesForPairing() {
RunOnMediumEnvironmentThread([&]() { devices_pairing_contexts_.clear(); });
}
void MediumEnvironment::AddObserver(
api::BluetoothClassicMedium::Observer* observer) {
if (!enabled_) return;
observers_.AddObserver(observer);
}
void MediumEnvironment::RemoveObserver(
api::BluetoothClassicMedium::Observer* observer) {
if (!enabled_) return;
observers_.RemoveObserver(observer);
}
void MediumEnvironment::SetBleExtendedAdvertisementsAvailable(bool enabled) {
ble_extended_advertisements_available_ = enabled;
}
-5
View File
@@ -27,7 +27,6 @@
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "internal/base/observer_list.h"
#include "internal/platform/borrowable.h"
#include "internal/platform/implementation/awdl.h"
#include "internal/platform/implementation/ble.h"
@@ -377,9 +376,6 @@ class MediumEnvironment {
// Clears the map `devices_pairing_contexts_`.
void ClearBluetoothDevicesForPairing();
void AddObserver(api::BluetoothClassicMedium::Observer* observer);
void RemoveObserver(api::BluetoothClassicMedium::Observer* observer);
// Sets the availability of BLE extended advertisements. It is false by
// default.
void SetBleExtendedAdvertisementsAvailable(bool enabled);
@@ -514,7 +510,6 @@ class MediumEnvironment {
bool use_valid_peer_connection_ = true;
absl::Duration peer_connection_latency_ = absl::ZeroDuration();
std::shared_ptr<FakeClock> simulated_clock_ ABSL_GUARDED_BY(mutex_);
ObserverList<api::BluetoothClassicMedium::Observer> observers_;
bool ble_extended_advertisements_available_ = false;
};