blob: d95538a57b1c8b90b6689e8edbbb484b9887a5a3 [file] [log] [blame]
Christopher Wiley006e94e2014-05-02 13:44:48 -07001// Copyright 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "buffet/storage_impls.h"
6
Alex Vakulenko33797062014-05-12 15:55:25 -07007#include <string>
8
Christopher Wiley006e94e2014-05-02 13:44:48 -07009#include <base/files/important_file_writer.h>
10#include <base/json/json_reader.h>
11#include <base/json/json_writer.h>
12
13namespace buffet {
14
15FileStorage::FileStorage(const base::FilePath& file_path)
16 : file_path_(file_path) { }
17
18std::unique_ptr<base::Value> FileStorage::Load() {
19 std::string json;
20 if (!base::ReadFileToString(file_path_, &json))
21 return std::unique_ptr<base::Value>();
22
23 return std::unique_ptr<base::Value>(base::JSONReader::Read(json));
24}
25
26bool FileStorage::Save(const base::Value* config) {
27 std::string json;
28 base::JSONWriter::WriteWithOptions(
29 config, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
30 return base::ImportantFileWriter::WriteFileAtomically(file_path_, json);
31}
32
33
34std::unique_ptr<base::Value> MemStorage::Load() {
35 return std::unique_ptr<base::Value>(cache_->DeepCopy());
36}
37
38bool MemStorage::Save(const base::Value* config) {
39 cache_.reset(config->DeepCopy());
40 ++save_count_;
41 return true;
42}
43
44} // namespace buffet