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 | |
| 13 | namespace buffet { |
| 14 | |
| 15 | FileStorage::FileStorage(const base::FilePath& file_path) |
| 16 | : file_path_(file_path) { } |
| 17 | |
| 18 | std::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 | |
| 26 | bool 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 | |
| 34 | std::unique_ptr<base::Value> MemStorage::Load() { |
| 35 | return std::unique_ptr<base::Value>(cache_->DeepCopy()); |
| 36 | } |
| 37 | |
| 38 | bool MemStorage::Save(const base::Value* config) { |
| 39 | cache_.reset(config->DeepCopy()); |
| 40 | ++save_count_; |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | } // namespace buffet |