buffet: Extract StorageInterface and implementations into separate files
This will make it easy to reuse file storage and memory storage
implementations in the forthcoming state aggregator.
BUG=chromium:369322
TEST=Unittests
Change-Id: Ie0dc0dbbfcc13e038352ecf77e2ad28bd41907d9
Reviewed-on: https://chromium-review.googlesource.com/198045
Tested-by: Christopher Wiley <wiley@chromium.org>
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/storage_impls.cc b/buffet/storage_impls.cc
new file mode 100644
index 0000000..2d2531d
--- /dev/null
+++ b/buffet/storage_impls.cc
@@ -0,0 +1,42 @@
+// Copyright 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "buffet/storage_impls.h"
+
+#include <base/files/important_file_writer.h>
+#include <base/json/json_reader.h>
+#include <base/json/json_writer.h>
+
+namespace buffet {
+
+FileStorage::FileStorage(const base::FilePath& file_path)
+ : file_path_(file_path) { }
+
+std::unique_ptr<base::Value> FileStorage::Load() {
+ std::string json;
+ if (!base::ReadFileToString(file_path_, &json))
+ return std::unique_ptr<base::Value>();
+
+ return std::unique_ptr<base::Value>(base::JSONReader::Read(json));
+}
+
+bool FileStorage::Save(const base::Value* 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());
+}
+
+bool MemStorage::Save(const base::Value* config) {
+ cache_.reset(config->DeepCopy());
+ ++save_count_;
+ return true;
+}
+
+} // namespace buffet