blob: b0bcfd93ac11f4e245dfa449a0110e27b23b425b [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
Vitaly Buka207c1cb2015-05-14 17:06:18 -070013#include "buffet/utils.h"
14
Christopher Wiley006e94e2014-05-02 13:44:48 -070015namespace buffet {
16
17FileStorage::FileStorage(const base::FilePath& file_path)
18 : file_path_(file_path) { }
19
Vitaly Buka207c1cb2015-05-14 17:06:18 -070020std::unique_ptr<base::DictionaryValue> FileStorage::Load() {
Christopher Wiley006e94e2014-05-02 13:44:48 -070021 std::string json;
22 if (!base::ReadFileToString(file_path_, &json))
Vitaly Buka207c1cb2015-05-14 17:06:18 -070023 return std::unique_ptr<base::DictionaryValue>();
Christopher Wiley006e94e2014-05-02 13:44:48 -070024
Vitaly Buka207c1cb2015-05-14 17:06:18 -070025 return LoadJsonDict(json, nullptr);
Christopher Wiley006e94e2014-05-02 13:44:48 -070026}
27
Vitaly Buka207c1cb2015-05-14 17:06:18 -070028bool FileStorage::Save(const base::DictionaryValue& config) {
Christopher Wiley006e94e2014-05-02 13:44:48 -070029 std::string json;
30 base::JSONWriter::WriteWithOptions(
Vitaly Buka56363222015-05-13 15:09:21 -070031 &config, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
Christopher Wiley006e94e2014-05-02 13:44:48 -070032 return base::ImportantFileWriter::WriteFileAtomically(file_path_, json);
33}
34
Vitaly Buka207c1cb2015-05-14 17:06:18 -070035std::unique_ptr<base::DictionaryValue> MemStorage::Load() {
36 return std::unique_ptr<base::DictionaryValue>(cache_.DeepCopy());
Christopher Wiley006e94e2014-05-02 13:44:48 -070037}
38
Vitaly Buka207c1cb2015-05-14 17:06:18 -070039bool MemStorage::Save(const base::DictionaryValue& config) {
40 cache_.Clear();
41 cache_.MergeDictionary(&config);
Christopher Wiley006e94e2014-05-02 13:44:48 -070042 return true;
43}
44
45} // namespace buffet