From a7f6ed3fa726adcd7a97f435214007116f52de60 Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Fri, 24 Jul 2026 09:48:47 -0700 Subject: [PATCH] Remove unused BluetoothClassicMedium::Observer. PiperOrigin-RevId: 953415728 --- internal/platform/BUILD | 2 - internal/platform/bluetooth_classic.cc | 73 ------------------- internal/platform/bluetooth_classic.h | 51 +------------ internal/platform/bluetooth_classic_test.cc | 51 ------------- .../implementation/bluetooth_classic.h | 26 ------- .../implementation/g3/bluetooth_classic.cc | 10 --- .../implementation/g3/bluetooth_classic.h | 3 - .../windows/bluetooth_classic_medium.cc | 10 --- .../windows/bluetooth_classic_medium.h | 11 --- .../platform/implementation/windows/mutex.h | 1 + internal/platform/medium_environment.cc | 31 -------- internal/platform/medium_environment.h | 5 -- 12 files changed, 3 insertions(+), 271 deletions(-) diff --git a/internal/platform/BUILD b/internal/platform/BUILD index 9ee65f96..634a5002 100644 --- a/internal/platform/BUILD +++ b/internal/platform/BUILD @@ -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", diff --git a/internal/platform/bluetooth_classic.cc b/internal/platform/bluetooth_classic.cc index 9ad171eb..6adcd0a9 100644 --- a/internal/platform/bluetooth_classic.cc +++ b/internal/platform/bluetooth_classic.cc @@ -19,7 +19,6 @@ #include #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 diff --git a/internal/platform/bluetooth_classic.h b/internal/platform/bluetooth_classic.h index 35e711e9..e917827c 100644 --- a/internal/platform/bluetooth_classic.h +++ b/internal/platform/bluetooth_classic.h @@ -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 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_list_; }; } // namespace nearby diff --git a/internal/platform/bluetooth_classic_test.cc b/internal/platform/bluetooth_classic_test.cc index f9cdae73..fd244c64 100644 --- a/internal/platform/bluetooth_classic_test.cc +++ b/internal/platform/bluetooth_classic_test.cc @@ -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 { 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. diff --git a/internal/platform/implementation/bluetooth_classic.h b/internal/platform/implementation/bluetooth_classic.h index 5e00c1dc..ff9daf7a 100644 --- a/internal/platform/implementation/bluetooth_classic.h +++ b/internal/platform/implementation/bluetooth_classic.h @@ -211,29 +211,6 @@ class BluetoothClassicMedium { DefaultCallback(); }; - 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 diff --git a/internal/platform/implementation/g3/bluetooth_classic.cc b/internal/platform/implementation/g3/bluetooth_classic.cc index 7e100034..df41fdb6 100644 --- a/internal/platform/implementation/g3/bluetooth_classic.cc +++ b/internal/platform/implementation/g3/bluetooth_classic.cc @@ -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 diff --git a/internal/platform/implementation/g3/bluetooth_classic.h b/internal/platform/implementation/g3/bluetooth_classic.h index aeb4f09f..b707abd0 100644 --- a/internal/platform/implementation/g3/bluetooth_classic.h +++ b/internal/platform/implementation/g3/bluetooth_classic.h @@ -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. diff --git a/internal/platform/implementation/windows/bluetooth_classic_medium.cc b/internal/platform/implementation/windows/bluetooth_classic_medium.cc index 94994e88..89f0644c 100644 --- a/internal/platform/implementation/windows/bluetooth_classic_medium.cc +++ b/internal/platform/implementation/windows/bluetooth_classic_medium.cc @@ -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(); diff --git a/internal/platform/implementation/windows/bluetooth_classic_medium.h b/internal/platform/implementation/windows/bluetooth_classic_medium.h index 14b44c3f..9dae9b50 100644 --- a/internal/platform/implementation/windows/bluetooth_classic_medium.h +++ b/internal/platform/implementation/windows/bluetooth_classic_medium.h @@ -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 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 observers_; }; } // namespace nearby::windows diff --git a/internal/platform/implementation/windows/mutex.h b/internal/platform/implementation/windows/mutex.h index 44488a23..44f88d79 100644 --- a/internal/platform/implementation/windows/mutex.h +++ b/internal/platform/implementation/windows/mutex.h @@ -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 { diff --git a/internal/platform/medium_environment.cc b/internal/platform/medium_environment.cc index 46f8142e..842d2d02 100644 --- a/internal/platform/medium_environment.cc +++ b/internal/platform/medium_environment.cc @@ -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; } diff --git a/internal/platform/medium_environment.h b/internal/platform/medium_environment.h index 4ffce3ad..24feec29 100644 --- a/internal/platform/medium_environment.h +++ b/internal/platform/medium_environment.h @@ -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 simulated_clock_ ABSL_GUARDED_BY(mutex_); - ObserverList observers_; bool ble_extended_advertisements_available_ = false; };