From a5b377c770ca55cd2ba55db45a893dda0e781893 Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Fri, 26 Jul 2024 13:14:30 -0700 Subject: [PATCH] Cleanup NearbySharingSettings. PiperOrigin-RevId: 656505916 --- sharing/BUILD | 1 - sharing/nearby_sharing_settings.cc | 144 ++++++++---------------- sharing/nearby_sharing_settings.h | 92 +++++---------- sharing/nearby_sharing_settings_test.cc | 61 ++-------- 4 files changed, 85 insertions(+), 213 deletions(-) diff --git a/sharing/BUILD b/sharing/BUILD index 41b1ff68..4cda16ab 100644 --- a/sharing/BUILD +++ b/sharing/BUILD @@ -641,7 +641,6 @@ cc_test( cc_test( name = "nearby_sharing_settings_test", srcs = ["nearby_sharing_settings_test.cc"], - shard_count = 5, deps = [ ":nearby_sharing_service", "//internal/platform/implementation/g3", # fixdeps: keep diff --git a/sharing/nearby_sharing_settings.cc b/sharing/nearby_sharing_settings.cc index 201e877b..320ee44c 100644 --- a/sharing/nearby_sharing_settings.cc +++ b/sharing/nearby_sharing_settings.cc @@ -18,17 +18,16 @@ #include // NOLINT(build/c++17) #include #include -#include #include #include #include #include #include "absl/strings/string_view.h" +#include "absl/synchronization/mutex.h" #include "absl/time/time.h" #include "internal/platform/clock.h" #include "internal/platform/device_info.h" -#include "internal/platform/mutex_lock.h" #include "proto/sharing_enums.pb.h" #include "sharing/analytics/analytics_recorder.h" #include "sharing/common/compatible_u8_string.h" @@ -80,7 +79,8 @@ NearbyShareSettings::NearbyShareSettings( device_info_(device_info), preference_manager_(preference_manager), local_device_data_manager_(local_device_data_manager), - analytics_recorder_(analytics_recorder) { + analytics_recorder_(analytics_recorder), + fallback_visibility_(prefs::kDefaultFallbackVisibility) { is_desctructing_ = std::make_shared(false); RestoreFallbackVisibility(); preference_manager_.AddObserver( @@ -99,16 +99,14 @@ NearbyShareSettings::NearbyShareSettings( } NearbyShareSettings::~NearbyShareSettings() { - MutexLock lock(&mutex_); + absl::MutexLock lock(&mutex_); is_desctructing_ = nullptr; preference_manager_.RemoveObserver(kPreferencesObserverName); local_device_data_manager_->RemoveObserver(this); - visibility_expiration_timer_.reset(); } FastInitiationNotificationState NearbyShareSettings::GetFastInitiationNotificationState() const { - MutexLock lock(&mutex_); return static_cast( preference_manager_.GetInteger( prefs::kNearbySharingFastInitiationNotificationStateName, @@ -118,15 +116,14 @@ NearbyShareSettings::GetFastInitiationNotificationState() const { void NearbyShareSettings::SetIsFastInitiationHardwareSupported( bool is_supported) { - MutexLock lock(&mutex_); - - // If the new value is the same as the old value, don't notify observers. - if (is_fast_initiation_hardware_supported_ == is_supported) { - return; + { + absl::MutexLock lock(&mutex_); + // If the new value is the same as the old value, don't notify observers. + if (is_fast_initiation_hardware_supported_ == is_supported) { + return; + } + is_fast_initiation_hardware_supported_ = is_supported; } - - is_fast_initiation_hardware_supported_ = is_supported; - for (Observer* observer : observers_set_.GetObservers()) { observer->OnIsFastInitiationHardwareSupportedChanged(is_supported); } @@ -137,13 +134,12 @@ std::string NearbyShareSettings::GetDeviceName() const { } DataUsage NearbyShareSettings::GetDataUsage() const { - MutexLock lock(&mutex_); return static_cast( preference_manager_.GetInteger(prefs::kNearbySharingDataUsageName, 0)); } void NearbyShareSettings::StartVisibilityTimer( - absl::Duration expiration) const { + absl::Duration expiration) { NL_LOG(INFO) << __func__ << ": start visibility timer. expiration=" << expiration; visibility_expiration_timer_ = std::make_unique( @@ -152,11 +148,8 @@ void NearbyShareSettings::StartVisibilityTimer( NL_LOG(INFO) << __func__ << ": visibility timer expired."; proto::DeviceVisibility visibility; { - MutexLock lock(&mutex_); - // We stop the timer after reading the fallback visibility, so - // GetFallbackVisibility() will return the persisted fallback - // visibility value instead of UNSPECIFIED. - visibility = GetFallbackVisibility().visibility; + absl::MutexLock lock(&mutex_); + visibility = GetRawFallbackVisibility().visibility; visibility_expiration_timer_.reset(); } SetVisibility(visibility); @@ -164,8 +157,6 @@ void NearbyShareSettings::StartVisibilityTimer( } void NearbyShareSettings::RestoreFallbackVisibility() { - MutexLock lock(&mutex_); - int64_t expiration_seconds = preference_manager_.GetInteger( prefs::kNearbySharingBackgroundVisibilityExpirationSeconds, 0); int64_t fallback_visibility = preference_manager_.GetInteger( @@ -192,7 +183,6 @@ void NearbyShareSettings::RestoreFallbackVisibility() { } std::string NearbyShareSettings::GetCustomSavePath() const { - MutexLock lock(&mutex_); return preference_manager_.GetString( prefs::kNearbySharingCustomSavePath, GetCompatibleU8String(device_info_.GetDownloadPath().u8string())); @@ -201,29 +191,15 @@ std::string NearbyShareSettings::GetCustomSavePath() const { bool NearbyShareSettings::IsDisabledByPolicy() const { return false; } void NearbyShareSettings::AddSettingsObserver(Observer* observer) { - MutexLock lock(&mutex_); observers_set_.AddObserver(observer); } void NearbyShareSettings::RemoveSettingsObserver(Observer* observer) { - MutexLock lock(&mutex_); observers_set_.RemoveObserver(observer); } -void NearbyShareSettings::GetFastInitiationNotificationState( - std::function callback) { - std::move(callback)(GetFastInitiationNotificationState()); -} - -void NearbyShareSettings::GetIsFastInitiationHardwareSupported( - std::function callback) { - MutexLock lock(&mutex_); - std::move(callback)(is_fast_initiation_hardware_supported_); -} - void NearbyShareSettings::SetFastInitiationNotificationState( FastInitiationNotificationState state) { - MutexLock lock(&mutex_); if (analytics_recorder_ != nullptr) { analytics_recorder_->NewToggleShowNotification( GetNotificationStatus(GetFastInitiationNotificationState()), @@ -235,11 +211,6 @@ void NearbyShareSettings::SetFastInitiationNotificationState( static_cast(state)); } -void NearbyShareSettings::GetDeviceName( - std::function callback) { - std::move(callback)(GetDeviceName()); -} - void NearbyShareSettings::ValidateDeviceName( absl::string_view device_name, std::function callback) { @@ -256,13 +227,7 @@ void NearbyShareSettings::SetDeviceName( std::move(callback)(local_device_data_manager_->SetDeviceName(device_name)); } -void NearbyShareSettings::GetDataUsage( - std::function callback) { - std::move(callback)(GetDataUsage()); -} - void NearbyShareSettings::SetDataUsage(DataUsage data_usage) { - MutexLock lock(&mutex_); if (analytics_recorder_ != nullptr) { analytics_recorder_->NewSetDataUsage(GetDataUsage(), data_usage); } @@ -270,13 +235,7 @@ void NearbyShareSettings::SetDataUsage(DataUsage data_usage) { static_cast(data_usage)); } -void NearbyShareSettings::GetVisibility( - std::function callback) { - std::move(callback)(GetVisibility()); -} - DeviceVisibility NearbyShareSettings::GetVisibility() const { - MutexLock lock(&mutex_); DeviceVisibility visibility = static_cast(preference_manager_.GetInteger( prefs::kNearbySharingBackgroundVisibilityName, @@ -284,19 +243,20 @@ DeviceVisibility NearbyShareSettings::GetVisibility() const { if (visibility == DeviceVisibility::DEVICE_VISIBILITY_SELECTED_CONTACTS) { // Set the visibility to self share if it's only visible to selected // contacts, as part of QuickShare rebrand work. - SetVisibility(DeviceVisibility::DEVICE_VISIBILITY_SELF_SHARE); - return DeviceVisibility::DEVICE_VISIBILITY_SELF_SHARE; + visibility = DeviceVisibility::DEVICE_VISIBILITY_SELF_SHARE; } return visibility; } void NearbyShareSettings::SetVisibility(DeviceVisibility visibility, - absl::Duration expiration) const { - MutexLock lock(&mutex_); - DeviceVisibility last_visibility = - static_cast(preference_manager_.GetInteger( - prefs::kNearbySharingBackgroundVisibilityName, - static_cast(prefs::kDefaultVisibility))); + absl::Duration expiration) { + absl::MutexLock lock(&mutex_); + if (visibility == DeviceVisibility::DEVICE_VISIBILITY_SELECTED_CONTACTS) { + // This should really be an error, but this function does not return errors, + // so change it to self share as in GetVisibility(). + visibility = DeviceVisibility::DEVICE_VISIBILITY_SELF_SHARE; + } + DeviceVisibility last_visibility = GetVisibility(); if (analytics_recorder_ != nullptr) { analytics_recorder_->NewSetVisibility( last_visibility, visibility, absl::ToInt64Milliseconds(expiration)); @@ -305,12 +265,7 @@ void NearbyShareSettings::SetVisibility(DeviceVisibility visibility, NL_VLOG(1) << __func__ << ": set visibility. visibility=" << static_cast(visibility) << ", expiration=" << expiration; - if (visibility_expiration_timer_ != nullptr && - visibility_expiration_timer_->IsRunning()) { - NL_VLOG(1) << __func__ - << ": temporary visibility timer is running. stopped."; - visibility_expiration_timer_.reset(); - } + visibility_expiration_timer_.reset(); absl::Time now = clock_->Now(); if (expiration != absl::ZeroDuration()) { @@ -340,27 +295,35 @@ void NearbyShareSettings::SetVisibility(DeviceVisibility visibility, } absl::Time NearbyShareSettings::GetLastVisibilityTimestamp() const { - MutexLock lock(&mutex_); + absl::MutexLock lock(&mutex_); return last_visibility_timestamp_; } proto::DeviceVisibility NearbyShareSettings::GetLastVisibility() const { - MutexLock lock(&mutex_); + absl::MutexLock lock(&mutex_); return static_cast(last_visibility_); } +NearbyShareSettings::FallbackVisibilityInfo +NearbyShareSettings::GetRawFallbackVisibility() const { + return { + .visibility = fallback_visibility_, + .fallback_time = absl::FromUnixSeconds(preference_manager_.GetInteger( + prefs::kNearbySharingBackgroundVisibilityExpirationSeconds, 0)) + }; +} + NearbyShareSettings::FallbackVisibilityInfo NearbyShareSettings::GetFallbackVisibility() const { - MutexLock lock(&mutex_); + absl::MutexLock lock(&mutex_); FallbackVisibilityInfo result{ .visibility = DeviceVisibility::DEVICE_VISIBILITY_UNSPECIFIED, .fallback_time = absl::UnixEpoch(), }; - if (GetIsTemporarilyVisible()) { - result.visibility = - fallback_visibility_.value_or(prefs::kDefaultFallbackVisibility); - result.fallback_time = absl::FromUnixSeconds(preference_manager_.GetInteger( - prefs::kNearbySharingBackgroundVisibilityExpirationSeconds, 0)); + // Check if visibility is temporary. + if (visibility_expiration_timer_ != nullptr && + visibility_expiration_timer_->IsRunning()) { + result = GetRawFallbackVisibility(); } NL_VLOG(1) << __func__ << ": get fallback visibility " << static_cast(result.visibility) @@ -368,9 +331,7 @@ NearbyShareSettings::GetFallbackVisibility() const { return result; } -void NearbyShareSettings::SetFallbackVisibility( - DeviceVisibility visibility) const { - MutexLock lock(&mutex_); +void NearbyShareSettings::SetFallbackVisibility(DeviceVisibility visibility) { NL_VLOG(1) << __func__ << ": set fallback visibility. visibility=" << static_cast(visibility); if (visibility == DeviceVisibility::DEVICE_VISIBILITY_EVERYONE) { @@ -384,25 +345,14 @@ void NearbyShareSettings::SetFallbackVisibility( static_cast(visibility)); } -bool NearbyShareSettings::GetIsTemporarilyVisible() const { - MutexLock lock(&mutex_); - return visibility_expiration_timer_ != nullptr; -} - -void NearbyShareSettings::GetCustomSavePathAsync( - const std::function& callback) const { - callback(GetCustomSavePath()); -} - void NearbyShareSettings::SetCustomSavePathAsync( absl::string_view save_path, const std::function& callback) { - MutexLock lock(&mutex_); + absl::MutexLock lock(&mutex_); preference_manager_.SetString(prefs::kNearbySharingCustomSavePath, save_path); callback(); } void NearbyShareSettings::OnPreferenceChanged(absl::string_view key) { - MutexLock lock(&mutex_); if (key == prefs::kNearbySharingFastInitiationNotificationStateName) { NotifyAllObservers(key, Observer::Data(static_cast( GetFastInitiationNotificationState()))); @@ -423,7 +373,6 @@ void NearbyShareSettings::OnPreferenceChanged(absl::string_view key) { void NearbyShareSettings::OnLocalDeviceDataChanged(bool did_device_name_change, bool did_full_name_change, bool did_icon_url_change) { - MutexLock lock(&mutex_); if (!did_device_name_change) return; std::string device_name = GetDeviceName(); @@ -438,15 +387,12 @@ void NearbyShareSettings::NotifyAllObservers(absl::string_view key, } } -bool NearbyShareSettings::GetIsAnalyticsEnabled() { - MutexLock lock(&mutex_); +bool NearbyShareSettings::GetIsAnalyticsEnabled() const { return preference_manager_.GetBoolean( prefs::kNearbySharingIsAnalyticsEnabledName, true); } -void NearbyShareSettings::SetIsAnalyticsEnabled( - bool is_analytics_enabled) const { - MutexLock lock(&mutex_); +void NearbyShareSettings::SetIsAnalyticsEnabled(bool is_analytics_enabled) { preference_manager_.SetBoolean(prefs::kNearbySharingIsAnalyticsEnabledName, is_analytics_enabled); } @@ -468,7 +414,7 @@ std::string NearbyShareSettings::Dump() const { } bool NearbyShareSettings::is_fast_initiation_hardware_supported() { - MutexLock lock(&mutex_); + absl::MutexLock lock(&mutex_); return is_fast_initiation_hardware_supported_; } diff --git a/sharing/nearby_sharing_settings.h b/sharing/nearby_sharing_settings.h index 3503ee71..9a17fc19 100644 --- a/sharing/nearby_sharing_settings.h +++ b/sharing/nearby_sharing_settings.h @@ -19,18 +19,16 @@ #include #include #include -#include #include #include #include "absl/base/thread_annotations.h" #include "absl/strings/string_view.h" +#include "absl/synchronization/mutex.h" #include "absl/time/time.h" #include "internal/base/observer_list.h" #include "internal/platform/clock.h" #include "internal/platform/device_info.h" -#include "internal/platform/mutex.h" -#include "internal/platform/timer.h" #include "proto/sharing_enums.pb.h" #include "sharing/analytics/analytics_recorder.h" #include "sharing/common/nearby_share_enums.h" @@ -44,28 +42,6 @@ namespace nearby { namespace sharing { -// Provides a type safe wrapper/abstraction over prefs for both C++ and -// Javascript (over mojo) to interact with Nearby user settings. This class -// always reads directly from prefs and relies on preference's memory cache. -// It is designed to be contained within the Nearby Sharing Service with an -// instance per user profile. This class also helps to keep some prefs -// logic out of |NearbyShareServiceImpl|. -// -// This class is also used to expose device properties that affect the settings -// UI, but cannot be added at load time because they need to be re-computed. See -// GetIsFastInitiationHardwareSupported() as an example. -// -// The mojo interface is intended to be exposed in settings, os_settings, and -// the nearby WebUI. -// -// NOTE: The pref-change registrar only notifies observers of pref value -// changes; observers are not notified if the pref value is set but does not -// change. This class inherits this behavior. -// -// NOTE: Because the observer interface is over mojo, setting a value directly -// will not synchronously trigger the observer event. Generally this is not a -// problem because these settings should only be changed by user interaction, -// but this is necessary to know when writing unit-tests. class NearbyShareSettings : nearby::sharing::NearbyShareLocalDeviceDataManager::Observer { public: @@ -171,15 +147,16 @@ class NearbyShareSettings // Internal synchronous getters for C++ clients proto::FastInitiationNotificationState GetFastInitiationNotificationState() const; - bool is_fast_initiation_hardware_supported(); - void SetIsFastInitiationHardwareSupported(bool is_supported); + bool is_fast_initiation_hardware_supported() ABSL_LOCKS_EXCLUDED(mutex_); + void SetIsFastInitiationHardwareSupported(bool is_supported) + ABSL_LOCKS_EXCLUDED(mutex_); std::string GetDeviceName() const; proto::DataUsage GetDataUsage() const; proto::DeviceVisibility GetVisibility() const; // Gets the timestamp of last visibility change. Need the timestamp to decide // whether need to send optional signature data during key pairing. - absl::Time GetLastVisibilityTimestamp() const; - proto::DeviceVisibility GetLastVisibility() const; + absl::Time GetLastVisibilityTimestamp() const ABSL_LOCKS_EXCLUDED(mutex_); + proto::DeviceVisibility GetLastVisibility() const ABSL_LOCKS_EXCLUDED(mutex_); std::string GetCustomSavePath() const; @@ -189,23 +166,18 @@ class NearbyShareSettings // Asynchronous APIs exposed by NearbyShareSettings void AddSettingsObserver(Observer* observer); void RemoveSettingsObserver(Observer* observer); - void GetFastInitiationNotificationState( - std::function callback); - void GetIsFastInitiationHardwareSupported(std::function callback); void SetFastInitiationNotificationState( proto::FastInitiationNotificationState state); - void GetDeviceName(std::function callback); void ValidateDeviceName( absl::string_view device_name, std::function callback); void SetDeviceName(absl::string_view device_name, std::function callback); - void GetDataUsage(std::function callback); void SetDataUsage(proto::DataUsage data_usage); // Returns the fallback visibility if the current visibility is temporary, // otherwise returning |DEVICE_VISIBILITY_UNSPECIFIED|. - FallbackVisibilityInfo GetFallbackVisibility() const; - void GetVisibility(std::function callback); + FallbackVisibilityInfo GetFallbackVisibility() const + ABSL_LOCKS_EXCLUDED(mutex_); // Sets the visibility for the Nearby Sharing service. If the expiration is // not zero, the visibility will be set temporarily and a fallback will be // set. If the expiration is zero, the visibility will be set permanently. @@ -214,16 +186,14 @@ class NearbyShareSettings // temporary timer expires, the fallback visibility will be restored and // cleared. void SetVisibility(proto::DeviceVisibility visibility, - absl::Duration expiration = absl::ZeroDuration()) const; - bool GetIsReceiving(); - void SetIsReceiving(bool is_receiving) const; - bool GetIsAnalyticsEnabled(); - void SetIsAnalyticsEnabled(bool is_analytics_enabled) const; + absl::Duration expiration = absl::ZeroDuration()) + ABSL_LOCKS_EXCLUDED(mutex_); + bool GetIsAnalyticsEnabled() const; + void SetIsAnalyticsEnabled(bool is_analytics_enabled); - void GetCustomSavePathAsync( - const std::function& callback) const; void SetCustomSavePathAsync(absl::string_view save_path, - const std::function& callback); + const std::function& callback) + ABSL_LOCKS_EXCLUDED(mutex_); // NearbyShareLocalDeviceDataManager::Observer: void OnLocalDeviceDataChanged(bool did_device_name_change, @@ -233,29 +203,24 @@ class NearbyShareSettings std::string Dump() const; private: - bool GetIsTemporarilyVisible() const; - void SetFallbackVisibility(proto::DeviceVisibility visibility) const; - void OnEnabledPrefChanged(); - void OnFastInitiationNotificationStatePrefChanged(); - void OnDataUsagePrefChanged(); - void OnVisibilityPrefChanged(); - void OnIsReceivingPrefChanged(); - void OnAllowedContactsPrefChanged(); - void OnIsOnboardingCompletePrefChanged(); - void OnCustomSavePathChanged(); + void SetFallbackVisibility(proto::DeviceVisibility visibility) + ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); void OnPreferenceChanged(absl::string_view key); - void NotifyAllObservers(absl::string_view key, Observer::Data value) + void NotifyAllObservers(absl::string_view key, Observer::Data value); + + void StartVisibilityTimer(absl::Duration expiration) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); - void StartVisibilityTimer(absl::Duration expiration) const + FallbackVisibilityInfo GetRawFallbackVisibility() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); // Restore/Save fallback visibility - void RestoreFallbackVisibility(); + void RestoreFallbackVisibility() + ABSL_NO_THREAD_SAFETY_ANALYSIS; // called from c'tor only // Make sure thread safe to access Nearby settings - mutable RecursiveMutex mutex_; + mutable absl::Mutex mutex_; Context* context_; nearby::Clock* const clock_; nearby::DeviceInfo& device_info_; @@ -266,16 +231,15 @@ class NearbyShareSettings std::shared_ptr is_desctructing_ = nullptr; bool is_fast_initiation_hardware_supported_ ABSL_GUARDED_BY(mutex_) = false; - ObserverList observers_set_ ABSL_GUARDED_BY(mutex_); - mutable std::unique_ptr visibility_expiration_timer_ - ABSL_GUARDED_BY(mutex_); - mutable std::optional fallback_visibility_ + ObserverList observers_set_; + std::unique_ptr visibility_expiration_timer_ ABSL_GUARDED_BY(mutex_); + proto::DeviceVisibility fallback_visibility_ ABSL_GUARDED_BY(mutex_); // Used to track the timestamp of visibility change. - mutable absl::Time last_visibility_timestamp_ ABSL_GUARDED_BY(mutex_) = + absl::Time last_visibility_timestamp_ ABSL_GUARDED_BY(mutex_) = absl::InfinitePast(); - mutable proto::DeviceVisibility last_visibility_ ABSL_GUARDED_BY(mutex_) = + proto::DeviceVisibility last_visibility_ ABSL_GUARDED_BY(mutex_) = proto::DeviceVisibility::DEVICE_VISIBILITY_UNSPECIFIED; }; diff --git a/sharing/nearby_sharing_settings_test.cc b/sharing/nearby_sharing_settings_test.cc index 416c424e..5df18fbd 100644 --- a/sharing/nearby_sharing_settings_test.cc +++ b/sharing/nearby_sharing_settings_test.cc @@ -169,41 +169,16 @@ class NearbyShareSettingsTest : public ::testing::Test { std::unique_ptr nearby_share_settings_; }; -TEST_F(NearbyShareSettingsTest, GetAndSetFastInitiationNotificationState) { - // Fast init notifications are enabled by default. - EXPECT_EQ(observer_.fast_initiation_notification_state(), - FastInitiationNotificationState::ENABLED_FAST_INIT); - settings()->SetFastInitiationNotificationState( - FastInitiationNotificationState::DISABLED_BY_USER_FAST_INIT); - EXPECT_EQ(FastInitiationNotificationState::DISABLED_BY_USER_FAST_INIT, - settings()->GetFastInitiationNotificationState()); - Flush(); - EXPECT_EQ(observer_.fast_initiation_notification_state(), - FastInitiationNotificationState::DISABLED_BY_USER_FAST_INIT); - - FastInitiationNotificationState state = - FastInitiationNotificationState::ENABLED_FAST_INIT; - settings()->GetFastInitiationNotificationState( - [&state](FastInitiationNotificationState result) { state = result; }); - EXPECT_EQ(state, FastInitiationNotificationState::DISABLED_BY_USER_FAST_INIT); -} - TEST_F(NearbyShareSettingsTest, GetAndSetCustomSavePath) { absl::Notification notification; - settings()->SetCustomSavePathAsync( - GetCompatibleU8String(std::filesystem::temp_directory_path().u8string()), - [&]() { notification.Notify(); }); + std::string save_path = + GetCompatibleU8String(std::filesystem::temp_directory_path().u8string()); + settings()->SetCustomSavePathAsync(save_path, + [&]() { notification.Notify(); }); Flush(); EXPECT_TRUE(notification.HasBeenNotified()); - settings()->GetCustomSavePathAsync([&](absl::string_view path) { - observer_.OnSettingChanged( - prefs::kNearbySharingCustomSavePath, - NearbyShareSettings::Observer::Data(std::string(path))); - }); - Flush(); - EXPECT_EQ( - observer_.custom_save_path(), - GetCompatibleU8String(std::filesystem::temp_directory_path().u8string())); + + EXPECT_EQ(settings()->GetCustomSavePath(), save_path); } TEST_F(NearbyShareSettingsTest, GetAndSetIsFastInitiationHardwareSupported) { @@ -213,10 +188,7 @@ TEST_F(NearbyShareSettingsTest, GetAndSetIsFastInitiationHardwareSupported) { Flush(); EXPECT_TRUE(observer_.is_fast_initiation_notification_hardware_supported()); - bool is_supported = false; - settings()->GetIsFastInitiationHardwareSupported( - [&is_supported](bool result) { is_supported = result; }); - EXPECT_TRUE(is_supported); + EXPECT_TRUE(settings()->is_fast_initiation_hardware_supported()); } TEST_F(NearbyShareSettingsTest, ValidateDeviceName) { @@ -237,9 +209,7 @@ TEST_F(NearbyShareSettingsTest, ValidateDeviceName) { TEST_F(NearbyShareSettingsTest, GetAndSetDeviceName) { std::string name = "not_the_default"; - settings()->GetDeviceName( - [&name](absl::string_view result) { name = std::string(result); }); - EXPECT_EQ(kDefaultDeviceName, name); + EXPECT_EQ(kDefaultDeviceName, settings()->GetDeviceName()); // When we get a validation error, setting the name should not succeed. EXPECT_EQ(observer_.device_name(), "uncalled"); @@ -264,9 +234,7 @@ TEST_F(NearbyShareSettingsTest, GetAndSetDeviceName) { Flush(); EXPECT_EQ(observer_.device_name(), "d"); - settings()->GetDeviceName( - [&name](absl::string_view result) { name = std::string(result); }); - EXPECT_EQ(name, "d"); + EXPECT_EQ(settings()->GetDeviceName(), "d"); } TEST_F(NearbyShareSettingsTest, GetAndSetDataUsage) { @@ -276,10 +244,7 @@ TEST_F(NearbyShareSettingsTest, GetAndSetDataUsage) { Flush(); EXPECT_EQ(observer_.data_usage(), DataUsage::OFFLINE_DATA_USAGE); - DataUsage data_usage = DataUsage::UNKNOWN_DATA_USAGE; - settings()->GetDataUsage( - [&data_usage](DataUsage usage) { data_usage = usage; }); - EXPECT_EQ(data_usage, DataUsage::OFFLINE_DATA_USAGE); + EXPECT_EQ(settings()->GetDataUsage(), DataUsage::OFFLINE_DATA_USAGE); } TEST_F(NearbyShareSettingsTest, GetAndSetVisibility) { @@ -292,10 +257,8 @@ TEST_F(NearbyShareSettingsTest, GetAndSetVisibility) { EXPECT_EQ(observer_.visibility(), DeviceVisibility::DEVICE_VISIBILITY_EVERYONE); - DeviceVisibility visibility = DeviceVisibility::DEVICE_VISIBILITY_UNSPECIFIED; - settings()->GetVisibility( - [&visibility](DeviceVisibility result) { visibility = result; }); - EXPECT_EQ(visibility, DeviceVisibility::DEVICE_VISIBILITY_EVERYONE); + EXPECT_EQ(settings()->GetVisibility(), + DeviceVisibility::DEVICE_VISIBILITY_EVERYONE); } TEST_F(NearbyShareSettingsTest,