buffet: Use DictionaryValue in StorageInterface
It is used to store dictionaries only.
BUG=none
TEST='FEATURES=test emerge-gizmo buffet'
Change-Id: Ic85f77a3d31853bf8ed244d7f58264e9c809f666
Reviewed-on: https://chromium-review.googlesource.com/271301
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Vitaly Buka <vitalybuka@chromium.org>
Commit-Queue: Vitaly Buka <vitalybuka@chromium.org>
Tested-by: Vitaly Buka <vitalybuka@chromium.org>
diff --git a/buffet/storage_impls.cc b/buffet/storage_impls.cc
index e45ddbf..ea29d69 100644
--- a/buffet/storage_impls.cc
+++ b/buffet/storage_impls.cc
@@ -10,33 +10,35 @@
#include <base/json/json_reader.h>
#include <base/json/json_writer.h>
+#include "buffet/utils.h"
+
namespace buffet {
FileStorage::FileStorage(const base::FilePath& file_path)
: file_path_(file_path) { }
-std::unique_ptr<base::Value> FileStorage::Load() {
+std::unique_ptr<base::DictionaryValue> FileStorage::Load() {
std::string json;
if (!base::ReadFileToString(file_path_, &json))
- return std::unique_ptr<base::Value>();
+ return std::unique_ptr<base::DictionaryValue>();
- return std::unique_ptr<base::Value>(base::JSONReader::Read(json));
+ return LoadJsonDict(json, nullptr);
}
-bool FileStorage::Save(const base::Value& config) {
+bool FileStorage::Save(const base::DictionaryValue& config) {
std::string json;
base::JSONWriter::WriteWithOptions(
&config, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
return base::ImportantFileWriter::WriteFileAtomically(file_path_, json);
}
-
-std::unique_ptr<base::Value> MemStorage::Load() {
- return std::unique_ptr<base::Value>(cache_->DeepCopy());
+std::unique_ptr<base::DictionaryValue> MemStorage::Load() {
+ return std::unique_ptr<base::DictionaryValue>(cache_.DeepCopy());
}
-bool MemStorage::Save(const base::Value& config) {
- cache_.reset(config.DeepCopy());
+bool MemStorage::Save(const base::DictionaryValue& config) {
+ cache_.Clear();
+ cache_.MergeDictionary(&config);
++save_count_;
return true;
}
diff --git a/buffet/storage_impls.h b/buffet/storage_impls.h
index f4034be..bacedd0 100644
--- a/buffet/storage_impls.h
+++ b/buffet/storage_impls.h
@@ -18,8 +18,8 @@
public:
explicit FileStorage(const base::FilePath& file_path);
virtual ~FileStorage() = default;
- std::unique_ptr<base::Value> Load() override;
- bool Save(const base::Value& config) override;
+ std::unique_ptr<base::DictionaryValue> Load() override;
+ bool Save(const base::DictionaryValue& config) override;
private:
base::FilePath file_path_;
@@ -31,14 +31,14 @@
public:
MemStorage() = default;
virtual ~MemStorage() = default;
- std::unique_ptr<base::Value> Load() override;
- bool Save(const base::Value& config) override;
+ std::unique_ptr<base::DictionaryValue> Load() override;
+ bool Save(const base::DictionaryValue& config) override;
int save_count() { return save_count_; }
void reset_save_count() { save_count_ = 0; }
private:
int save_count_ = 0;
- std::unique_ptr<base::Value> cache_;
+ base::DictionaryValue cache_;
DISALLOW_COPY_AND_ASSIGN(MemStorage);
};
diff --git a/buffet/storage_interface.h b/buffet/storage_interface.h
index 4f05235..d36e56d 100644
--- a/buffet/storage_interface.h
+++ b/buffet/storage_interface.h
@@ -15,16 +15,14 @@
// the details of this storage behind an interface for test purposes.
class StorageInterface {
public:
- // Load the device registration configuration from storage.
- // If it fails (e.g. the storage container [file?] doesn't exist), then
- // it returns empty unique_ptr (aka nullptr).
- virtual std::unique_ptr<base::Value> Load() = 0;
+ // Load the dictionary from storage. If it fails (e.g. the storage container
+ // [file?] doesn't exist), then it returns empty unique_ptr (aka nullptr).
+ virtual std::unique_ptr<base::DictionaryValue> Load() = 0;
- // Save the device registration configuration to storage.
- // If saved successfully, returns true. Could fail when writing to
- // physical storage like file system for various reasons (out of disk space,
- // access permissions, etc).
- virtual bool Save(const base::Value& config) = 0;
+ // Save the dictionary to storage. If saved successfully, returns true. Could
+ // fail when writing to physical storage like file system for various reasons
+ // (out of disk space,access permissions, etc).
+ virtual bool Save(const base::DictionaryValue& config) = 0;
};
} // namespace buffet
diff --git a/buffet/utils.cc b/buffet/utils.cc
index 7e50d0f..707b19d 100644
--- a/buffet/utils.cc
+++ b/buffet/utils.cc
@@ -36,8 +36,9 @@
const char kInvalidCategoryError[] = "invalid_category";
const char kInvalidPackageError[] = "invalid_package";
-std::unique_ptr<const base::DictionaryValue> LoadJsonDict(
- const base::FilePath& json_file_path, chromeos::ErrorPtr* error) {
+std::unique_ptr<base::DictionaryValue> LoadJsonDict(
+ const base::FilePath& json_file_path,
+ chromeos::ErrorPtr* error) {
std::string json_string;
if (!base::ReadFileToString(json_file_path, &json_string)) {
chromeos::errors::system::AddSystemError(error, FROM_HERE, errno);
@@ -50,9 +51,10 @@
return LoadJsonDict(json_string, error);
}
-std::unique_ptr<const base::DictionaryValue> LoadJsonDict(
- const std::string& json_string, chromeos::ErrorPtr* error) {
- std::unique_ptr<const base::DictionaryValue> result;
+std::unique_ptr<base::DictionaryValue> LoadJsonDict(
+ const std::string& json_string,
+ chromeos::ErrorPtr* error) {
+ std::unique_ptr<base::DictionaryValue> result;
std::string error_message;
base::Value* value = base::JSONReader::ReadAndReturnError(
json_string, base::JSON_PARSE_RFC, nullptr, &error_message);
@@ -65,7 +67,7 @@
error_message.c_str());
return result;
}
- const base::DictionaryValue* dict_value = nullptr;
+ base::DictionaryValue* dict_value = nullptr;
if (!value->GetAsDictionary(&dict_value)) {
delete value;
chromeos::Error::AddToPrintf(error, FROM_HERE,
diff --git a/buffet/utils.h b/buffet/utils.h
index 0325171..8e4bfd6 100644
--- a/buffet/utils.h
+++ b/buffet/utils.h
@@ -30,12 +30,14 @@
// Helper function to load a JSON file that is expected to be
// an object/dictionary. In case of error, returns empty unique ptr and fills
// in error details in |error|.
-std::unique_ptr<const base::DictionaryValue> LoadJsonDict(
- const base::FilePath& json_file_path, chromeos::ErrorPtr* error);
+std::unique_ptr<base::DictionaryValue> LoadJsonDict(
+ const base::FilePath& json_file_path,
+ chromeos::ErrorPtr* error);
// Helper function to load a JSON dictionary from a string.
-std::unique_ptr<const base::DictionaryValue> LoadJsonDict(
- const std::string& json_string, chromeos::ErrorPtr* error);
+std::unique_ptr<base::DictionaryValue> LoadJsonDict(
+ const std::string& json_string,
+ chromeos::ErrorPtr* error);
// Synchronously resolves the |host| and connects a socket to the resolved
// address/port.