Vitaly Buka | cad20f0 | 2015-10-16 17:27:15 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 14 | |
| 15 | #ifndef BUFFET_BUFFET_CONFIG_H_ |
| 16 | #define BUFFET_BUFFET_CONFIG_H_ |
| 17 | |
Vitaly Buka | becd461 | 2015-08-16 23:31:55 -0700 | [diff] [blame] | 18 | #include <map> |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 19 | #include <set> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include <base/callback.h> |
| 24 | #include <base/files/file_path.h> |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 25 | #include <brillo/errors/error.h> |
| 26 | #include <brillo/key_value_store.h> |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 27 | #include <weave/provider/config_store.h> |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 28 | |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 29 | #include "buffet/encryptor.h" |
| 30 | |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 31 | namespace buffet { |
| 32 | |
| 33 | class StorageInterface; |
| 34 | |
| 35 | // Handles reading buffet config and state files. |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 36 | class BuffetConfig final : public weave::provider::ConfigStore { |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 37 | public: |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 38 | struct Options { |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 39 | std::string client_id; |
| 40 | std::string client_secret; |
| 41 | std::string api_key; |
| 42 | std::string oauth_url; |
| 43 | std::string service_url; |
| 44 | |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 45 | base::FilePath defaults; |
| 46 | base::FilePath settings; |
| 47 | |
| 48 | base::FilePath definitions; |
| 49 | base::FilePath test_definitions; |
| 50 | |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 51 | std::string test_privet_ssid; |
| 52 | }; |
| 53 | |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 54 | // An IO abstraction to enable testing without using real files. |
| 55 | class FileIO { |
| 56 | public: |
| 57 | virtual bool ReadFile(const base::FilePath& path, std::string* content) = 0; |
| 58 | virtual bool WriteFile(const base::FilePath& path, |
| 59 | const std::string& content) = 0; |
| 60 | }; |
| 61 | |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 62 | ~BuffetConfig() override = default; |
| 63 | |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 64 | explicit BuffetConfig(const Options& options); |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 65 | |
| 66 | // Config overrides. |
| 67 | bool LoadDefaults(weave::Settings* settings) override; |
Vitaly Buka | e709fa7 | 2016-01-29 16:24:31 -0800 | [diff] [blame] | 68 | std::string LoadSettings(const std::string& name) override; |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 69 | std::string LoadSettings() override; |
Vitaly Buka | e709fa7 | 2016-01-29 16:24:31 -0800 | [diff] [blame] | 70 | void SaveSettings(const std::string& name, |
| 71 | const std::string& settings, |
| 72 | const weave::DoneCallback& callback) override; |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 73 | |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 74 | bool LoadDefaults(const brillo::KeyValueStore& store, |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 75 | weave::Settings* settings); |
| 76 | |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 77 | // Allows injection of a non-default |encryptor| for testing. The caller |
| 78 | // retains ownership of the pointer. |
| 79 | void SetEncryptor(Encryptor* encryptor) { |
| 80 | encryptor_ = encryptor; |
| 81 | } |
| 82 | |
| 83 | // Allows injection of non-default |file_io| for testing. The caller retains |
| 84 | // ownership of the pointer. |
| 85 | void SetFileIO(FileIO* file_io) { |
| 86 | file_io_ = file_io; |
| 87 | } |
| 88 | |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 89 | private: |
Vitaly Buka | e709fa7 | 2016-01-29 16:24:31 -0800 | [diff] [blame] | 90 | base::FilePath CreatePath(const std::string& name) const; |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 91 | bool LoadFile(const base::FilePath& file_path, |
| 92 | std::string* data, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 93 | brillo::ErrorPtr* error); |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 94 | |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 95 | Options options_; |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 96 | std::unique_ptr<Encryptor> default_encryptor_; |
| 97 | Encryptor* encryptor_{nullptr}; |
| 98 | std::unique_ptr<FileIO> default_file_io_; |
| 99 | FileIO* file_io_{nullptr}; |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 100 | |
| 101 | DISALLOW_COPY_AND_ASSIGN(BuffetConfig); |
| 102 | }; |
| 103 | |
| 104 | } // namespace buffet |
| 105 | |
| 106 | #endif // BUFFET_BUFFET_CONFIG_H_ |