Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 1 | // 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 Vakulenko | 3379706 | 2014-05-12 15:55:25 -0700 | [diff] [blame] | 7 | #include <string> |
| 8 | |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 9 | #include <base/files/important_file_writer.h> |
| 10 | #include <base/json/json_reader.h> |
| 11 | #include <base/json/json_writer.h> |
| 12 | |
Vitaly Buka | 207c1cb | 2015-05-14 17:06:18 -0700 | [diff] [blame] | 13 | #include "buffet/utils.h" |
| 14 | |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 15 | namespace buffet { |
| 16 | |
| 17 | FileStorage::FileStorage(const base::FilePath& file_path) |
| 18 | : file_path_(file_path) { } |
| 19 | |
Vitaly Buka | 207c1cb | 2015-05-14 17:06:18 -0700 | [diff] [blame] | 20 | std::unique_ptr<base::DictionaryValue> FileStorage::Load() { |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 21 | std::string json; |
| 22 | if (!base::ReadFileToString(file_path_, &json)) |
Vitaly Buka | 207c1cb | 2015-05-14 17:06:18 -0700 | [diff] [blame] | 23 | return std::unique_ptr<base::DictionaryValue>(); |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 24 | |
Vitaly Buka | 207c1cb | 2015-05-14 17:06:18 -0700 | [diff] [blame] | 25 | return LoadJsonDict(json, nullptr); |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Vitaly Buka | 207c1cb | 2015-05-14 17:06:18 -0700 | [diff] [blame] | 28 | bool FileStorage::Save(const base::DictionaryValue& config) { |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 29 | std::string json; |
| 30 | base::JSONWriter::WriteWithOptions( |
Vitaly Buka | 5636322 | 2015-05-13 15:09:21 -0700 | [diff] [blame] | 31 | &config, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 32 | return base::ImportantFileWriter::WriteFileAtomically(file_path_, json); |
| 33 | } |
| 34 | |
Vitaly Buka | 207c1cb | 2015-05-14 17:06:18 -0700 | [diff] [blame] | 35 | std::unique_ptr<base::DictionaryValue> MemStorage::Load() { |
| 36 | return std::unique_ptr<base::DictionaryValue>(cache_.DeepCopy()); |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Vitaly Buka | 207c1cb | 2015-05-14 17:06:18 -0700 | [diff] [blame] | 39 | bool MemStorage::Save(const base::DictionaryValue& config) { |
| 40 | cache_.Clear(); |
| 41 | cache_.MergeDictionary(&config); |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 42 | return true; |
| 43 | } |
| 44 | |
| 45 | } // namespace buffet |