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 | #ifndef BUFFET_STORAGE_IMPLS_H_ |
| 6 | #define BUFFET_STORAGE_IMPLS_H_ |
| 7 | |
Ben Chan | 1526097 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 8 | #include <base/files/file_util.h> |
Alex Vakulenko | 132617a | 2014-09-04 08:59:43 -0700 | [diff] [blame] | 9 | #include <base/macros.h> |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 10 | #include <base/values.h> |
| 11 | |
| 12 | #include "buffet/storage_interface.h" |
| 13 | |
| 14 | namespace buffet { |
| 15 | |
| 16 | // Persists the given Value to an atomically written file. |
| 17 | class FileStorage : public StorageInterface { |
| 18 | public: |
Alex Vakulenko | 3379706 | 2014-05-12 15:55:25 -0700 | [diff] [blame] | 19 | explicit FileStorage(const base::FilePath& file_path); |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 20 | virtual ~FileStorage() = default; |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 21 | std::unique_ptr<base::Value> Load() override; |
| 22 | bool Save(const base::Value* config) override; |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 23 | |
| 24 | private: |
| 25 | base::FilePath file_path_; |
| 26 | DISALLOW_COPY_AND_ASSIGN(FileStorage); |
| 27 | }; |
| 28 | |
| 29 | // StorageInterface for testing. Just stores the values in memory. |
| 30 | class MemStorage : public StorageInterface { |
| 31 | public: |
| 32 | MemStorage() = default; |
| 33 | virtual ~MemStorage() = default; |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 34 | std::unique_ptr<base::Value> Load() override; |
| 35 | bool Save(const base::Value* config) override; |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 36 | int save_count() { return save_count_; } |
| 37 | void reset_save_count() { save_count_ = 0; } |
| 38 | |
| 39 | private: |
| 40 | int save_count_ = 0; |
| 41 | std::unique_ptr<base::Value> cache_; |
| 42 | DISALLOW_COPY_AND_ASSIGN(MemStorage); |
| 43 | }; |
| 44 | |
| 45 | } // namespace buffet |
| 46 | |
| 47 | #endif // BUFFET_STORAGE_IMPLS_H_ |