implemented new methods from upstream for pref manager

This commit is contained in:
Lasan Mahaliyana
2026-03-07 01:13:58 +05:30
parent ea513a34aa
commit 0c162a56d2
2 changed files with 46 additions and 0 deletions
@@ -29,6 +29,8 @@
#include "internal/platform/implementation/platform.h"
#include "nlohmann/json.hpp"
#include "nlohmann/json_fwd.hpp"
#include "google/protobuf/json/json.h"
#include "google/protobuf/message.h"
namespace nearby {
namespace linux {
@@ -113,6 +115,16 @@ bool PreferencesManager::SetTime(absl::string_view key, absl::Time value) {
return Commit();
}
bool PreferencesManager::SetProtoMessage(
absl::string_view key, const google::protobuf::Message& value) {
std::string json_string;
if (!google::protobuf::json::MessageToJsonString(value, &json_string).ok()) {
return false;
}
absl::MutexLock lock(&mutex_);
return SetValue(key, json::parse(json_string));
}
// Get JSON value.
json PreferencesManager::Get(absl::string_view key,
const json& default_value) const {
@@ -179,12 +191,35 @@ absl::Time PreferencesManager::GetTime(absl::string_view key,
return absl::FromUnixNanos(result->get<int64_t>());
}
bool PreferencesManager::GetProtoMessage(absl::string_view key,
google::protobuf::Message* value) const {
absl::MutexLock lock(&mutex_);
auto result = value_.find(absl::StrCat(key));
if (result == value_.end()) {
return false;
}
return google::protobuf::json::JsonStringToMessage(result->dump(), value).ok();
}
// Removes preferences
void PreferencesManager::Remove(absl::string_view key) {
absl::MutexLock lock(&mutex_);
value_.erase(absl::StrCat(key));
}
bool PreferencesManager::RemoveKeyPrefix(absl::string_view prefix) {
absl::MutexLock lock(&mutex_);
auto it = value_.begin();
while (it != value_.end()) {
if (it.key().starts_with(prefix)) {
it = value_.erase(it);
} else {
++it;
}
}
return true;
}
// Private methods
// Writes data to storage.
@@ -30,6 +30,7 @@
#include "internal/platform/implementation/preferences_manager.h"
#include "nlohmann/json.hpp"
#include "nlohmann/json_fwd.hpp"
#include "google/protobuf/message.h"
namespace nearby {
namespace linux {
@@ -72,6 +73,10 @@ class PreferencesManager : public api::PreferencesManager {
bool SetTime(absl::string_view key, absl::Time value) override
ABSL_LOCKS_EXCLUDED(mutex_);
bool SetProtoMessage(absl::string_view key,
const google::protobuf::Message& value) override
ABSL_LOCKS_EXCLUDED(mutex_);
// Gets values
nlohmann::json Get(absl::string_view key,
const nlohmann::json& default_value) const override
@@ -105,8 +110,14 @@ class PreferencesManager : public api::PreferencesManager {
absl::Time default_value) const override
ABSL_LOCKS_EXCLUDED(mutex_);
bool GetProtoMessage(absl::string_view key,
google::protobuf::Message* value) const override
ABSL_LOCKS_EXCLUDED(mutex_);
// Removes preferences
void Remove(absl::string_view key) override ABSL_LOCKS_EXCLUDED(mutex_);
bool RemoveKeyPrefix(absl::string_view prefix) override
ABSL_LOCKS_EXCLUDED(mutex_);
private:
// Writes data to storage.