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