diff --git a/fastpair/scanning/fastpair/fake_fast_pair_scanner.cc b/fastpair/scanning/fastpair/fake_fast_pair_scanner.cc index 2f003064..1c5eae58 100644 --- a/fastpair/scanning/fastpair/fake_fast_pair_scanner.cc +++ b/fastpair/scanning/fastpair/fake_fast_pair_scanner.cc @@ -26,11 +26,11 @@ void FakeFastPairScanner::RemoveObserver(Observer* observer) { } void FakeFastPairScanner::NotifyDeviceFound(const BlePeripheral& peripheral) { - for (auto& obs : observer_) obs->OnDeviceFound(peripheral); + for (auto& obs : observer_.GetObservers()) obs->OnDeviceFound(peripheral); } void FakeFastPairScanner::NotifyDeviceLost(const BlePeripheral& peripheral) { - for (auto& obs : observer_) obs->OnDeviceLost(peripheral); + for (auto& obs : observer_.GetObservers()) obs->OnDeviceLost(peripheral); } } // namespace fastpair diff --git a/fastpair/scanning/fastpair/fast_pair_scanner_impl.cc b/fastpair/scanning/fastpair/fast_pair_scanner_impl.cc index 6c36df43..4b4efc71 100644 --- a/fastpair/scanning/fastpair/fast_pair_scanner_impl.cc +++ b/fastpair/scanning/fastpair/fast_pair_scanner_impl.cc @@ -134,13 +134,13 @@ void FastPairScannerImpl::OnDeviceLost(const BlePeripheral& peripheral) { << peripheral.GetName(); device_address_advertisement_data_map_.erase(peripheral.GetName()); - for (auto& observer : observer_) { + for (auto& observer : observer_.GetObservers()) { observer->OnDeviceLost(peripheral); } } void FastPairScannerImpl::NotifyDeviceFound(const BlePeripheral& peripheral) { - for (auto& observer : observer_) { + for (auto& observer : observer_.GetObservers()) { observer->OnDeviceFound(peripheral); } } diff --git a/fastpair/scanning/mock_scanner_broker.h b/fastpair/scanning/mock_scanner_broker.h index 59a44bef..0df0bea0 100644 --- a/fastpair/scanning/mock_scanner_broker.h +++ b/fastpair/scanning/mock_scanner_broker.h @@ -15,9 +15,9 @@ #define THIRD_PARTY_NEARBY_FASTPAIR_SCANNING_MOCK_SCANNER_BROKER_H_ #include "gmock/gmock.h" +#include "fastpair/common/fast_pair_device.h" #include "fastpair/scanning/scanner_broker.h" #include "internal/base/observer_list.h" -#include "fastpair/common/fast_pair_device.h" namespace nearby { namespace fastpair { @@ -27,7 +27,6 @@ class MockScannerBroker : public ScannerBroker { MOCK_METHOD(void, StartScanning, (Protocol), (override)); MOCK_METHOD(void, StopScanning, (Protocol), (override)); - void AddObserver(Observer* observer) override { observers_.AddObserver(observer); } @@ -37,13 +36,13 @@ class MockScannerBroker : public ScannerBroker { } void NotifyDeviceFound(const FastPairDevice& device) { - for (auto& observer : observers_){ - observer->OnDeviceFound(device); + for (auto& observer : observers_.GetObservers()) { + observer->OnDeviceFound(device); } } - void NotifyDeviceLost(const FastPairDevice& device) { - for (auto& observer : observers_){ + void NotifyDeviceLost(const FastPairDevice& device) { + for (auto& observer : observers_.GetObservers()) { observer->OnDeviceLost(device); } } diff --git a/fastpair/scanning/scanner_broker_impl.cc b/fastpair/scanning/scanner_broker_impl.cc index 3fd6ebf9..36a60d80 100644 --- a/fastpair/scanning/scanner_broker_impl.cc +++ b/fastpair/scanning/scanner_broker_impl.cc @@ -72,7 +72,7 @@ void ScannerBrokerImpl::StopFastPairScanning() { void ScannerBrokerImpl::NotifyDeviceFound(const FastPairDevice& device) { NEARBY_LOGS(INFO) << __func__ << ": Notifying device found, model id = " << device.GetModelId(); - for (auto& observer : observers_) { + for (auto& observer : observers_.GetObservers()) { observer->OnDeviceFound(device); } } @@ -80,7 +80,7 @@ void ScannerBrokerImpl::NotifyDeviceFound(const FastPairDevice& device) { void ScannerBrokerImpl::NotifyDeviceLost(const FastPairDevice& device) { NEARBY_LOGS(INFO) << __func__ << ": Notifying device lost, model id = " << device.GetModelId(); - for (auto& observer : observers_) { + for (auto& observer : observers_.GetObservers()) { observer->OnDeviceLost(device); } } diff --git a/fastpair/ui/fast_pair/fast_pair_notification_controller.cc b/fastpair/ui/fast_pair/fast_pair_notification_controller.cc index b9851496..35f26de6 100644 --- a/fastpair/ui/fast_pair/fast_pair_notification_controller.cc +++ b/fastpair/ui/fast_pair/fast_pair_notification_controller.cc @@ -27,7 +27,7 @@ void FastPairNotificationController::RemoveObserver(Observer* observer) { void FastPairNotificationController::NotifyShowDiscovery( const DeviceMetadata& device) { - for (Observer* observer : observers_) { + for (Observer* observer : observers_.GetObservers()) { observer->OnUpdateDevice(device); } } diff --git a/internal/base/BUILD b/internal/base/BUILD index 998aec84..69faa567 100644 --- a/internal/base/BUILD +++ b/internal/base/BUILD @@ -17,6 +17,8 @@ cc_library( "//location/nearby/cpp/sharing:__subpackages__", ], deps = [ + "//internal/platform:types", + "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/container:flat_hash_set", ], ) diff --git a/internal/base/observer_list.h b/internal/base/observer_list.h index c3f45616..a4d44a2f 100644 --- a/internal/base/observer_list.h +++ b/internal/base/observer_list.h @@ -15,10 +15,9 @@ #ifndef THIRD_PARTY_NEARBY_INTERNAL_BASE_OBSERVER_LIST_H_ #define THIRD_PARTY_NEARBY_INTERNAL_BASE_OBSERVER_LIST_H_ -#include -#include - +#include "absl/base/thread_annotations.h" #include "absl/container/flat_hash_set.h" +#include "internal/platform/mutex_lock.h" namespace nearby { @@ -29,28 +28,45 @@ class ObserverList { using const_iterator = typename absl::flat_hash_set::const_iterator; - void AddObserver(ObserverType* observer) { observers_.emplace(observer); } + void AddObserver(ObserverType* observer) ABSL_LOCKS_EXCLUDED(mutex_) { + MutexLock lock(&mutex_); + observers_.insert(observer); + } - void RemoveObserver(ObserverType* observer) { observers_.erase(observer); } + void RemoveObserver(ObserverType* observer) ABSL_LOCKS_EXCLUDED(mutex_) { + MutexLock lock(&mutex_); + observers_.erase(observer); + } - bool HasObserver(ObserverType* observer) { + bool HasObserver(ObserverType* observer) ABSL_LOCKS_EXCLUDED(mutex_) { + MutexLock lock(&mutex_); return observers_.contains(observer); } - void Clear() { observers_.clear(); } + void Clear() ABSL_LOCKS_EXCLUDED(mutex_) { + MutexLock lock(&mutex_); + observers_.clear(); + } - bool empty() const { return observers_.empty(); } + bool empty() const ABSL_LOCKS_EXCLUDED(mutex_) { + MutexLock lock(&mutex_); + return observers_.empty(); + } - int size() const { return observers_.size(); } + int size() const ABSL_LOCKS_EXCLUDED(mutex_) { + MutexLock lock(&mutex_); + return observers_.size(); + } - // Supports iterators - iterator begin() { return observers_.begin(); } - iterator end() { return observers_.end(); } - const_iterator begin() const { return observers_.begin(); } - const_iterator end() const { return observers_.end(); } + absl::flat_hash_set GetObservers() + ABSL_LOCKS_EXCLUDED(mutex_) { + MutexLock lock(&mutex_); + return observers_; + } private: - absl::flat_hash_set observers_; + mutable Mutex mutex_; + absl::flat_hash_set observers_ ABSL_GUARDED_BY(mutex_); }; } // namespace nearby diff --git a/internal/platform/BUILD b/internal/platform/BUILD index 368ce7d0..433aca27 100644 --- a/internal/platform/BUILD +++ b/internal/platform/BUILD @@ -340,6 +340,7 @@ cc_library( visibility = [ "//connections:__subpackages__", "//fastpair:__subpackages__", + "//internal/base:__subpackages__", "//internal/flags:__subpackages__", "//internal/platform/implementation/windows:__subpackages__", "//internal/test:__subpackages__", diff --git a/internal/platform/implementation/platform.h b/internal/platform/implementation/platform.h index c26deb36..862bae8f 100644 --- a/internal/platform/implementation/platform.h +++ b/internal/platform/implementation/platform.h @@ -53,6 +53,10 @@ #include "internal/platform/os_name.h" #include "internal/platform/payload_id.h" +#ifdef CreateMutex +#undef CreateMutex +#endif + namespace nearby { namespace api { diff --git a/internal/platform/implementation/windows/mutex.h b/internal/platform/implementation/windows/mutex.h index db30d65a..b9a908be 100644 --- a/internal/platform/implementation/windows/mutex.h +++ b/internal/platform/implementation/windows/mutex.h @@ -13,7 +13,6 @@ // limitations under the License. #ifndef PLATFORM_IMPL_WINDOWS_MUTEX_H_ #define PLATFORM_IMPL_WINDOWS_MUTEX_H_ -#include #include #include diff --git a/internal/platform/implementation/windows/platform.cc b/internal/platform/implementation/windows/platform.cc index 10edeca1..70047776 100644 --- a/internal/platform/implementation/windows/platform.cc +++ b/internal/platform/implementation/windows/platform.cc @@ -65,6 +65,10 @@ #include "internal/platform/implementation/windows/wifi_lan.h" #include "internal/platform/logging.h" +#ifdef CreateMutex +#undef CreateMutex +#endif + namespace nearby { namespace api {