blob: d030ecb73940e4151299d7b3c85bc789313316a9 [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>
Christopher Wiley006e94e2014-05-02 13:44:48 -070010#include <base/json/json_writer.h>
11
Vitaly Buka207c1cb2015-05-14 17:06:18 -070012#include "buffet/utils.h"
13
Christopher Wiley006e94e2014-05-02 13:44:48 -070014namespace buffet {
15
16FileStorage::FileStorage(const base::FilePath& file_path)
17 : file_path_(file_path) { }
18
Vitaly Buka207c1cb2015-05-14 17:06:18 -070019std::unique_ptr<base::DictionaryValue> FileStorage::Load() {
Christopher Wiley006e94e2014-05-02 13:44:48 -070020 std::string json;
21 if (!base::ReadFileToString(file_path_, &json))
Vitaly Buka207c1cb2015-05-14 17:06:18 -070022 return std::unique_ptr<base::DictionaryValue>();
Christopher Wiley006e94e2014-05-02 13:44:48 -070023
Vitaly Buka207c1cb2015-05-14 17:06:18 -070024 return LoadJsonDict(json, nullptr);
Christopher Wiley006e94e2014-05-02 13:44:48 -070025}
26
Vitaly Buka207c1cb2015-05-14 17:06:18 -070027bool FileStorage::Save(const base::DictionaryValue& config) {
Christopher Wiley006e94e2014-05-02 13:44:48 -070028 std::string json;
29 base::JSONWriter::WriteWithOptions(
Vitaly Buka56363222015-05-13 15:09:21 -070030 &config, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
Christopher Wiley006e94e2014-05-02 13:44:48 -070031 return base::ImportantFileWriter::WriteFileAtomically(file_path_, json);
32}
33
Vitaly Buka207c1cb2015-05-14 17:06:18 -070034std::unique_ptr<base::DictionaryValue> MemStorage::Load() {
35 return std::unique_ptr<base::DictionaryValue>(cache_.DeepCopy());
Christopher Wiley006e94e2014-05-02 13:44:48 -070036}
37
Vitaly Buka207c1cb2015-05-14 17:06:18 -070038bool MemStorage::Save(const base::DictionaryValue& config) {
39 cache_.Clear();
40 cache_.MergeDictionary(&config);
Christopher Wiley006e94e2014-05-02 13:44:48 -070041 return true;
42}
43
44} // namespace buffet