From 56d7ca1c2e1a3eccedbf77b0a6a491d9f7823382 Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Fri, 6 Jun 2025 10:51:57 -0700 Subject: [PATCH] Remove unnecessary conversion from FilePath to string. PiperOrigin-RevId: 768137824 --- .../windows/preferences_manager.cc | 3 +-- .../windows/preferences_repository.cc | 17 +++++++---------- .../windows/preferences_repository.h | 7 +++---- .../windows/preferences_repository_test.cc | 11 ++++++----- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/internal/platform/implementation/windows/preferences_manager.cc b/internal/platform/implementation/windows/preferences_manager.cc index 00488997..082206bc 100644 --- a/internal/platform/implementation/windows/preferences_manager.cc +++ b/internal/platform/implementation/windows/preferences_manager.cc @@ -48,8 +48,7 @@ PreferencesManager::PreferencesManager(FilePath file_path) { } path->append(file_path); - preferences_repository_ = - std::make_unique(path->ToString()); + preferences_repository_ = std::make_unique(*path); value_ = preferences_repository_->LoadPreferences(); } diff --git a/internal/platform/implementation/windows/preferences_repository.cc b/internal/platform/implementation/windows/preferences_repository.cc index b7ce4d1f..d904abff 100644 --- a/internal/platform/implementation/windows/preferences_repository.cc +++ b/internal/platform/implementation/windows/preferences_repository.cc @@ -69,15 +69,14 @@ json PreferencesRepository::LoadPreferences() { bool PreferencesRepository::SavePreferences(json preferences) { absl::MutexLock lock(&mutex_); try { - FilePath path{path_}; - if (!Files::FileExists(path) && !Files::CreateDirectories(path)) { + if (!Files::FileExists(path_) && !Files::CreateDirectories(path_)) { LOG(ERROR) << "Failed to create preferences path."; return false; } - FilePath full_name = path; + FilePath full_name = path_; full_name.append(FilePath(kPreferencesFileName)); - FilePath full_name_backup = path; + FilePath full_name_backup = path_; full_name_backup.append(FilePath(kPreferencesBackupFileName)); // Create a backup without moving the bytes on disk @@ -114,10 +113,9 @@ bool PreferencesRepository::SavePreferences(json preferences) { } std::optional PreferencesRepository::AttemptLoad() { - FilePath path{path_}; - FilePath full_name = path; + FilePath full_name = path_; full_name.append(FilePath(kPreferencesFileName)); - if (!Files::DirectoryExists(path) || !Files::FileExists(full_name)) { + if (!Files::DirectoryExists(path_) || !Files::FileExists(full_name)) { return std::nullopt; } @@ -146,10 +144,9 @@ std::optional PreferencesRepository::AttemptLoad() { } std::optional PreferencesRepository::RestoreFromBackup() { - FilePath path{path_}; - FilePath full_name = path; + FilePath full_name = path_; full_name.append(FilePath(kPreferencesFileName)); - FilePath full_name_backup = path; + FilePath full_name_backup = path_; full_name_backup.append(FilePath(kPreferencesBackupFileName)); if (!Files::FileExists(full_name_backup)) { diff --git a/internal/platform/implementation/windows/preferences_repository.h b/internal/platform/implementation/windows/preferences_repository.h index dfa915cb..d26186c8 100644 --- a/internal/platform/implementation/windows/preferences_repository.h +++ b/internal/platform/implementation/windows/preferences_repository.h @@ -16,20 +16,19 @@ #define PLATFORM_IMPLEMENTATION_WINDOWS_PREFERENCES_REPOSITORY_H_ #include -#include #include "absl/base/thread_annotations.h" -#include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" #include "nlohmann/json.hpp" #include "nlohmann/json_fwd.hpp" +#include "internal/base/file_path.h" namespace nearby { namespace windows { class PreferencesRepository { public: - explicit PreferencesRepository(absl::string_view path) : path_(path) {} + explicit PreferencesRepository(const FilePath& path) : path_(path) {} nlohmann::json LoadPreferences() ABSL_LOCKS_EXCLUDED(&mutex_); bool SavePreferences(nlohmann::json preferences) ABSL_LOCKS_EXCLUDED(&mutex_); @@ -39,7 +38,7 @@ class PreferencesRepository { private: absl::Mutex mutex_; - const std::string path_; + const FilePath path_; }; } // namespace windows diff --git a/internal/platform/implementation/windows/preferences_repository_test.cc b/internal/platform/implementation/windows/preferences_repository_test.cc index 4e375d03..0ef8de90 100644 --- a/internal/platform/implementation/windows/preferences_repository_test.cc +++ b/internal/platform/implementation/windows/preferences_repository_test.cc @@ -36,7 +36,8 @@ constexpr char kPreferencesBackupFileName[] = "preferences_bak.json"; constexpr char kPreferencesPath[] = "Google/Nearby/Sharing"; TEST(PreferencesRepository, LoadWithBadPath) { - PreferencesRepository preferences_repository{"c:\\users\\a\\b\\c\\d\\e\\f"}; + PreferencesRepository preferences_repository{ + FilePath{"c:\\users\\a\\b\\c\\d\\e\\f"}}; json result = preferences_repository.LoadPreferences(); EXPECT_TRUE(result.empty()); } @@ -56,7 +57,7 @@ TEST(PreferencesRepository, RecoverFromBadPreferences) { pref_file << "\"Bad top level object\""; pref_file.close(); - PreferencesRepository preferences_repository{full_path.ToString()}; + PreferencesRepository preferences_repository{full_path}; EXPECT_EQ(preferences_repository.LoadPreferences(), json::object()); } @@ -71,7 +72,7 @@ TEST(PreferencesRepository, SaveAndLoadPreferences) { Files::RemoveFile(full_name); } - PreferencesRepository preferences_repository{full_path.ToString()}; + PreferencesRepository preferences_repository{full_path}; json data; data["key1"] = "value1"; data["key2"] = "value2"; @@ -100,7 +101,7 @@ TEST(PreferencesRepository, LoadFromBackup) { Files::RemoveFile(full_name_backup); } - PreferencesRepository preferences_repository{full_path.ToString()}; + PreferencesRepository preferences_repository{full_path}; json data; data["key1"] = "value1"; data["key2"] = "value2"; @@ -137,7 +138,7 @@ TEST(PreferencesRepository, RecoverFromCorruption) { Files::RemoveFile(full_name_backup); } - PreferencesRepository preferences_repository{full_path.ToString()}; + PreferencesRepository preferences_repository{full_path}; json data; data["key1"] = "value1"; data["key2"] = "value2";