blob: 6445d55a0a4fedb0d9ac40dc7ead392e2c70c06c [file] [log] [blame]
Vitaly Bukacad20f02015-10-16 17:27:15 -07001// 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 Buka1175a9b2015-08-15 10:42:17 -070014
15#ifndef BUFFET_BUFFET_CONFIG_H_
16#define BUFFET_BUFFET_CONFIG_H_
17
Vitaly Bukabecd4612015-08-16 23:31:55 -070018#include <map>
Vitaly Buka1175a9b2015-08-15 10:42:17 -070019#include <set>
20#include <string>
21#include <vector>
22
23#include <base/callback.h>
24#include <base/files/file_path.h>
Alex Vakulenko41705852015-10-13 10:12:06 -070025#include <brillo/errors/error.h>
26#include <brillo/key_value_store.h>
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070027#include <weave/provider/config_store.h>
Vitaly Buka1175a9b2015-08-15 10:42:17 -070028
Alex Vakulenkodf381642015-10-08 07:34:23 -070029#include "buffet/encryptor.h"
30
Vitaly Buka1175a9b2015-08-15 10:42:17 -070031namespace buffet {
32
33class StorageInterface;
34
35// Handles reading buffet config and state files.
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070036class BuffetConfig final : public weave::provider::ConfigStore {
Vitaly Buka1175a9b2015-08-15 10:42:17 -070037 public:
Alex Vakulenko0022b752015-10-02 11:09:59 -070038 struct Options {
Alex Vakulenko2915a7b2015-10-07 17:04:00 -070039 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 Vakulenko0022b752015-10-02 11:09:59 -070045 base::FilePath defaults;
46 base::FilePath settings;
47
48 base::FilePath definitions;
49 base::FilePath test_definitions;
50
Alex Vakulenko0022b752015-10-02 11:09:59 -070051 std::string test_privet_ssid;
52 };
53
Alex Vakulenkodf381642015-10-08 07:34:23 -070054 // 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 Buka1175a9b2015-08-15 10:42:17 -070062 ~BuffetConfig() override = default;
63
Alex Vakulenko0022b752015-10-02 11:09:59 -070064 explicit BuffetConfig(const Options& options);
Vitaly Buka1175a9b2015-08-15 10:42:17 -070065
66 // Config overrides.
67 bool LoadDefaults(weave::Settings* settings) override;
Vitaly Bukae709fa72016-01-29 16:24:31 -080068 std::string LoadSettings(const std::string& name) override;
Vitaly Buka1175a9b2015-08-15 10:42:17 -070069 std::string LoadSettings() override;
Vitaly Bukae709fa72016-01-29 16:24:31 -080070 void SaveSettings(const std::string& name,
71 const std::string& settings,
72 const weave::DoneCallback& callback) override;
Vitaly Buka1175a9b2015-08-15 10:42:17 -070073
Alex Vakulenko41705852015-10-13 10:12:06 -070074 bool LoadDefaults(const brillo::KeyValueStore& store,
Vitaly Buka1175a9b2015-08-15 10:42:17 -070075 weave::Settings* settings);
76
Alex Vakulenkodf381642015-10-08 07:34:23 -070077 // 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 Buka1175a9b2015-08-15 10:42:17 -070089 private:
Vitaly Bukae709fa72016-01-29 16:24:31 -080090 base::FilePath CreatePath(const std::string& name) const;
Alex Vakulenkodf381642015-10-08 07:34:23 -070091 bool LoadFile(const base::FilePath& file_path,
92 std::string* data,
Alex Vakulenko41705852015-10-13 10:12:06 -070093 brillo::ErrorPtr* error);
Alex Vakulenkodf381642015-10-08 07:34:23 -070094
Alex Vakulenko0022b752015-10-02 11:09:59 -070095 Options options_;
Alex Vakulenkodf381642015-10-08 07:34:23 -070096 std::unique_ptr<Encryptor> default_encryptor_;
97 Encryptor* encryptor_{nullptr};
98 std::unique_ptr<FileIO> default_file_io_;
99 FileIO* file_io_{nullptr};
Vitaly Buka1175a9b2015-08-15 10:42:17 -0700100
101 DISALLOW_COPY_AND_ASSIGN(BuffetConfig);
102};
103
104} // namespace buffet
105
106#endif // BUFFET_BUFFET_CONFIG_H_