blob: 8b7e4e9cbd26468017f9b770e22c654007aa67ca [file] [log] [blame]
Vitaly Buka1175a9b2015-08-15 10:42:17 -07001// 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 Bukabecd4612015-08-16 23:31:55 -07008#include <map>
Vitaly Buka1175a9b2015-08-15 10:42:17 -07009#include <set>
10#include <string>
11#include <vector>
12
13#include <base/callback.h>
14#include <base/files/file_path.h>
Alex Vakulenko41705852015-10-13 10:12:06 -070015#include <brillo/errors/error.h>
16#include <brillo/key_value_store.h>
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070017#include <weave/provider/config_store.h>
Vitaly Buka1175a9b2015-08-15 10:42:17 -070018
Alex Vakulenkodf381642015-10-08 07:34:23 -070019#include "buffet/encryptor.h"
20
Vitaly Buka1175a9b2015-08-15 10:42:17 -070021namespace buffet {
22
23class StorageInterface;
24
25// Handles reading buffet config and state files.
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070026class BuffetConfig final : public weave::provider::ConfigStore {
Vitaly Buka1175a9b2015-08-15 10:42:17 -070027 public:
Alex Vakulenko0022b752015-10-02 11:09:59 -070028 struct Options {
Alex Vakulenko2915a7b2015-10-07 17:04:00 -070029 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 Vakulenko0022b752015-10-02 11:09:59 -070035 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 Vakulenkodf381642015-10-08 07:34:23 -070045 // 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 Buka1175a9b2015-08-15 10:42:17 -070053 ~BuffetConfig() override = default;
54
Alex Vakulenko0022b752015-10-02 11:09:59 -070055 explicit BuffetConfig(const Options& options);
Vitaly Buka1175a9b2015-08-15 10:42:17 -070056
57 // Config overrides.
58 bool LoadDefaults(weave::Settings* settings) override;
59 std::string LoadSettings() override;
60 void SaveSettings(const std::string& settings) override;
Vitaly Buka1175a9b2015-08-15 10:42:17 -070061
Alex Vakulenko41705852015-10-13 10:12:06 -070062 bool LoadDefaults(const brillo::KeyValueStore& store,
Vitaly Buka1175a9b2015-08-15 10:42:17 -070063 weave::Settings* settings);
64
Alex Vakulenkodf381642015-10-08 07:34:23 -070065 // 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 Buka1175a9b2015-08-15 10:42:17 -070077 private:
Alex Vakulenkodf381642015-10-08 07:34:23 -070078 bool LoadFile(const base::FilePath& file_path,
79 std::string* data,
Alex Vakulenko41705852015-10-13 10:12:06 -070080 brillo::ErrorPtr* error);
Alex Vakulenkodf381642015-10-08 07:34:23 -070081
Alex Vakulenko0022b752015-10-02 11:09:59 -070082 Options options_;
Alex Vakulenkodf381642015-10-08 07:34:23 -070083 std::unique_ptr<Encryptor> default_encryptor_;
84 Encryptor* encryptor_{nullptr};
85 std::unique_ptr<FileIO> default_file_io_;
86 FileIO* file_io_{nullptr};
Vitaly Buka1175a9b2015-08-15 10:42:17 -070087
88 DISALLOW_COPY_AND_ASSIGN(BuffetConfig);
89};
90
91} // namespace buffet
92
93#endif // BUFFET_BUFFET_CONFIG_H_