From 4826e79e0c3519aeced36fcd7b52a09cedf93e4c Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Wed, 6 Aug 2025 16:27:06 -0700 Subject: [PATCH] Cleanup preferences manager. PiperOrigin-RevId: 791889165 --- internal/platform/implementation/g3/platform.cc | 2 +- .../implementation/g3/preferences_manager.cc | 9 ++++----- .../implementation/g3/preferences_manager.h | 3 ++- .../implementation/g3/preferences_repository.cc | 17 ++++++++--------- .../implementation/g3/preferences_repository.h | 5 ++++- .../g3/preferences_repository_test.cc | 8 ++++++-- .../windows/preferences_manager.cc | 16 +++------------- .../windows/preferences_manager.h | 3 +-- sharing/internal/api/sharing_platform.h | 2 -- 9 files changed, 29 insertions(+), 36 deletions(-) diff --git a/internal/platform/implementation/g3/platform.cc b/internal/platform/implementation/g3/platform.cc index e508c606..30d6d4f0 100644 --- a/internal/platform/implementation/g3/platform.cc +++ b/internal/platform/implementation/g3/platform.cc @@ -269,7 +269,7 @@ ImplementationPlatform::CreateDeviceInfo() { std::unique_ptr ImplementationPlatform::CreatePreferencesManager(absl::string_view path) { - return std::make_unique(); + return std::make_unique(FilePath(path)); } } // namespace api diff --git a/internal/platform/implementation/g3/preferences_manager.cc b/internal/platform/implementation/g3/preferences_manager.cc index 150d6a07..74461d27 100644 --- a/internal/platform/implementation/g3/preferences_manager.cc +++ b/internal/platform/implementation/g3/preferences_manager.cc @@ -27,9 +27,8 @@ #include "absl/types/span.h" #include "nlohmann/json.hpp" #include "nlohmann/json_fwd.hpp" -#include "internal/platform/device_info_impl.h" +#include "internal/base/file_path.h" #include "internal/platform/implementation/g3/preferences_repository.h" -#include "internal/platform/implementation/preferences_manager.h" #include "internal/platform/logging.h" namespace nearby { @@ -38,9 +37,9 @@ namespace { using json = ::nlohmann::json; } // namespace -PreferencesManager::PreferencesManager() { - auto device_info = std::make_unique(); - preferences_repository_ = std::make_unique(); +PreferencesManager::PreferencesManager(FilePath preferences_dir) { + preferences_repository_ = + std::make_unique(preferences_dir); value_ = preferences_repository_->LoadPreferences(); } diff --git a/internal/platform/implementation/g3/preferences_manager.h b/internal/platform/implementation/g3/preferences_manager.h index d20c7adc..bde0b853 100644 --- a/internal/platform/implementation/g3/preferences_manager.h +++ b/internal/platform/implementation/g3/preferences_manager.h @@ -28,6 +28,7 @@ #include "absl/types/span.h" #include "nlohmann/json.hpp" #include "nlohmann/json_fwd.hpp" +#include "internal/base/file_path.h" #include "internal/platform/implementation/g3/preferences_repository.h" #include "internal/platform/implementation/preferences_manager.h" @@ -40,7 +41,7 @@ namespace g3 { // change by the observer. class PreferencesManager : public api::PreferencesManager { public: - PreferencesManager(); + explicit PreferencesManager(FilePath preferences_dir); // Sets values diff --git a/internal/platform/implementation/g3/preferences_repository.cc b/internal/platform/implementation/g3/preferences_repository.cc index 5c3a6abb..af0cac77 100644 --- a/internal/platform/implementation/g3/preferences_repository.cc +++ b/internal/platform/implementation/g3/preferences_repository.cc @@ -15,6 +15,7 @@ #include "internal/platform/implementation/g3/preferences_repository.h" #include +#include #include "absl/synchronization/mutex.h" #include "nlohmann/json.hpp" @@ -29,20 +30,15 @@ using json = nlohmann::json; } // namespace json PreferencesRepository::LoadPreferences() { - absl::MutexLock lock(&mutex_); + absl::MutexLock lock(mutex_); // Emulate Windows implementation try { - // settings.json is used for testing, but we should look at having - // an implementation override for G3 in PreferencesManager to override - // the path for testing. - FilePath path = - Files::GetTemporaryDirectory().append(FilePath("settings.json")); - if (!Files::FileExists(path)) { + if (!Files::FileExists(file_path_)) { return value_; } - std::ifstream preferences_file(path.GetPath()); + std::ifstream preferences_file(file_path_.GetPath()); if (!preferences_file.good()) { return value_; } @@ -62,8 +58,11 @@ json PreferencesRepository::LoadPreferences() { } bool PreferencesRepository::SavePreferences(json preferences) { - absl::MutexLock lock(&mutex_); + absl::MutexLock lock(mutex_); value_ = preferences; + std::ofstream preferences_file(file_path_.GetPath(), std::ios_base::trunc); + preferences_file << preferences; + preferences_file.close(); return true; } diff --git a/internal/platform/implementation/g3/preferences_repository.h b/internal/platform/implementation/g3/preferences_repository.h index 5615346c..69bbceb2 100644 --- a/internal/platform/implementation/g3/preferences_repository.h +++ b/internal/platform/implementation/g3/preferences_repository.h @@ -19,17 +19,20 @@ #include "absl/synchronization/mutex.h" #include "nlohmann/json.hpp" #include "nlohmann/json_fwd.hpp" +#include "internal/base/file_path.h" namespace nearby::g3 { class PreferencesRepository { public: - PreferencesRepository() = default; + explicit PreferencesRepository(FilePath base_path) + : file_path_(base_path.append(FilePath("settings.json"))) {} nlohmann::json LoadPreferences() ABSL_LOCKS_EXCLUDED(&mutex_); bool SavePreferences(nlohmann::json preferences) ABSL_LOCKS_EXCLUDED(&mutex_); private: + const FilePath file_path_; // Avoid to write in google3, just create a memory value to simulate a // preferences storage nlohmann::json value_ = nlohmann::json::object(); diff --git a/internal/platform/implementation/g3/preferences_repository_test.cc b/internal/platform/implementation/g3/preferences_repository_test.cc index bac55073..acf21958 100644 --- a/internal/platform/implementation/g3/preferences_repository_test.cc +++ b/internal/platform/implementation/g3/preferences_repository_test.cc @@ -19,6 +19,8 @@ #include "gtest/gtest.h" #include "nlohmann/json.hpp" #include "nlohmann/json_fwd.hpp" +#include "internal/base/file_path.h" +#include "internal/base/files.h" namespace nearby::g3 { namespace { @@ -26,7 +28,8 @@ using json = ::nlohmann::json; } // namespace TEST(Preferences, TestSaveAndGetPreferences) { - PreferencesRepository preferences_repository; + PreferencesRepository preferences_repository( + Files::GetTemporaryDirectory().append(FilePath("test1.json"))); std::string string_key = "string_value"; std::string string_value = "hello world"; std::string int_key = "int_value"; @@ -40,7 +43,8 @@ TEST(Preferences, TestSaveAndGetPreferences) { } TEST(Preferences, TestMultipleSaveAndGetPreferences) { - PreferencesRepository preferences_repository; + PreferencesRepository preferences_repository( + Files::GetTemporaryDirectory().append(FilePath("test2.json"))); std::string string_key = "string_value"; std::string string_value = "hello world"; std::string string_new_value = "again"; diff --git a/internal/platform/implementation/windows/preferences_manager.cc b/internal/platform/implementation/windows/preferences_manager.cc index 082206bc..bb0dac9c 100644 --- a/internal/platform/implementation/windows/preferences_manager.cc +++ b/internal/platform/implementation/windows/preferences_manager.cc @@ -16,7 +16,6 @@ #include #include -#include #include #include #include @@ -28,9 +27,7 @@ #include "absl/types/span.h" #include "nlohmann/json.hpp" #include "nlohmann/json_fwd.hpp" -#include "internal/base/files.h" #include "internal/base/file_path.h" -#include "internal/platform/implementation/platform.h" #include "internal/platform/implementation/windows/preferences_repository.h" #include "internal/platform/logging.h" @@ -39,16 +36,9 @@ namespace { using json = ::nlohmann::json; } // namespace -PreferencesManager::PreferencesManager(FilePath file_path) { - std::optional path = - nearby::api::ImplementationPlatform::CreateDeviceInfo() - ->GetLocalAppDataPath(); - if (!path.has_value()) { - path = Files::GetTemporaryDirectory(); - } - - path->append(file_path); - preferences_repository_ = std::make_unique(*path); +PreferencesManager::PreferencesManager(FilePath preferences_dir) { + preferences_repository_ = + std::make_unique(preferences_dir); value_ = preferences_repository_->LoadPreferences(); } diff --git a/internal/platform/implementation/windows/preferences_manager.h b/internal/platform/implementation/windows/preferences_manager.h index 25083502..363c07cd 100644 --- a/internal/platform/implementation/windows/preferences_manager.h +++ b/internal/platform/implementation/windows/preferences_manager.h @@ -41,8 +41,7 @@ namespace windows { // change by the observer. class PreferencesManager : public api::PreferencesManager { public: - // `path` is relative to the user's local app data directory. - explicit PreferencesManager(nearby::FilePath path); + explicit PreferencesManager(nearby::FilePath preferences_dir); // Sets values diff --git a/sharing/internal/api/sharing_platform.h b/sharing/internal/api/sharing_platform.h index 5a98a13f..fd629354 100644 --- a/sharing/internal/api/sharing_platform.h +++ b/sharing/internal/api/sharing_platform.h @@ -39,8 +39,6 @@ namespace nearby::sharing::api { -constexpr char kSharingPreferencesFilePath[] = "Google/Nearby/Sharing"; - // Platform abstraction interface for NearbyShare cross-platform compatibility. class SharingPlatform { public: