Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 1 | // Copyright 2015 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_BUFFET_CONFIG_H_ |
| 6 | #define BUFFET_BUFFET_CONFIG_H_ |
| 7 | |
Vitaly Buka | becd461 | 2015-08-16 23:31:55 -0700 | [diff] [blame] | 8 | #include <map> |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 9 | #include <set> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <base/callback.h> |
| 14 | #include <base/files/file_path.h> |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 15 | #include <brillo/errors/error.h> |
| 16 | #include <brillo/key_value_store.h> |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 17 | #include <weave/provider/config_store.h> |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 18 | |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 19 | #include "buffet/encryptor.h" |
| 20 | |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 21 | namespace buffet { |
| 22 | |
| 23 | class StorageInterface; |
| 24 | |
| 25 | // Handles reading buffet config and state files. |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 26 | class BuffetConfig final : public weave::provider::ConfigStore { |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 27 | public: |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 28 | struct Options { |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 29 | std::string client_id; |
| 30 | std::string client_secret; |
| 31 | std::string api_key; |
| 32 | std::string oauth_url; |
| 33 | std::string service_url; |
| 34 | |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 35 | base::FilePath defaults; |
| 36 | base::FilePath settings; |
| 37 | |
| 38 | base::FilePath definitions; |
| 39 | base::FilePath test_definitions; |
| 40 | |
| 41 | bool disable_security{false}; |
| 42 | std::string test_privet_ssid; |
| 43 | }; |
| 44 | |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 45 | // An IO abstraction to enable testing without using real files. |
| 46 | class FileIO { |
| 47 | public: |
| 48 | virtual bool ReadFile(const base::FilePath& path, std::string* content) = 0; |
| 49 | virtual bool WriteFile(const base::FilePath& path, |
| 50 | const std::string& content) = 0; |
| 51 | }; |
| 52 | |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 53 | ~BuffetConfig() override = default; |
| 54 | |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 55 | explicit BuffetConfig(const Options& options); |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 56 | |
| 57 | // Config overrides. |
| 58 | bool LoadDefaults(weave::Settings* settings) override; |
| 59 | std::string LoadSettings() override; |
| 60 | void SaveSettings(const std::string& settings) override; |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 61 | |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 62 | bool LoadDefaults(const brillo::KeyValueStore& store, |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 63 | weave::Settings* settings); |
| 64 | |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 65 | // Allows injection of a non-default |encryptor| for testing. The caller |
| 66 | // retains ownership of the pointer. |
| 67 | void SetEncryptor(Encryptor* encryptor) { |
| 68 | encryptor_ = encryptor; |
| 69 | } |
| 70 | |
| 71 | // Allows injection of non-default |file_io| for testing. The caller retains |
| 72 | // ownership of the pointer. |
| 73 | void SetFileIO(FileIO* file_io) { |
| 74 | file_io_ = file_io; |
| 75 | } |
| 76 | |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 77 | private: |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 78 | bool LoadFile(const base::FilePath& file_path, |
| 79 | std::string* data, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 80 | brillo::ErrorPtr* error); |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 81 | |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 82 | Options options_; |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 83 | std::unique_ptr<Encryptor> default_encryptor_; |
| 84 | Encryptor* encryptor_{nullptr}; |
| 85 | std::unique_ptr<FileIO> default_file_io_; |
| 86 | FileIO* file_io_{nullptr}; |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 87 | |
| 88 | DISALLOW_COPY_AND_ASSIGN(BuffetConfig); |
| 89 | }; |
| 90 | |
| 91 | } // namespace buffet |
| 92 | |
| 93 | #endif // BUFFET_BUFFET_CONFIG_H_ |