mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Cleanup preferences manager.
PiperOrigin-RevId: 791889165
This commit is contained in:
committed by
Copybara-Service
parent
38827f2b4b
commit
4826e79e0c
@@ -269,7 +269,7 @@ ImplementationPlatform::CreateDeviceInfo() {
|
||||
|
||||
std::unique_ptr<nearby::api::PreferencesManager>
|
||||
ImplementationPlatform::CreatePreferencesManager(absl::string_view path) {
|
||||
return std::make_unique<g3::PreferencesManager>();
|
||||
return std::make_unique<g3::PreferencesManager>(FilePath(path));
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
||||
@@ -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<DeviceInfoImpl>();
|
||||
preferences_repository_ = std::make_unique<PreferencesRepository>();
|
||||
PreferencesManager::PreferencesManager(FilePath preferences_dir) {
|
||||
preferences_repository_ =
|
||||
std::make_unique<PreferencesRepository>(preferences_dir);
|
||||
value_ = preferences_repository_->LoadPreferences();
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "internal/platform/implementation/g3/preferences_repository.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <ios>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -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<FilePath> path =
|
||||
nearby::api::ImplementationPlatform::CreateDeviceInfo()
|
||||
->GetLocalAppDataPath();
|
||||
if (!path.has_value()) {
|
||||
path = Files::GetTemporaryDirectory();
|
||||
}
|
||||
|
||||
path->append(file_path);
|
||||
preferences_repository_ = std::make_unique<PreferencesRepository>(*path);
|
||||
PreferencesManager::PreferencesManager(FilePath preferences_dir) {
|
||||
preferences_repository_ =
|
||||
std::make_unique<PreferencesRepository>(preferences_dir);
|
||||
value_ = preferences_repository_->LoadPreferences();
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user