Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Vitaly Buka | d322a3c | 2015-08-16 01:03:02 -0700 | [diff] [blame] | 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 LIBWEAVE_SRC_CONFIG_H_ |
| 6 | #define LIBWEAVE_SRC_CONFIG_H_ |
| 7 | |
| 8 | #include <set> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <base/callback.h> |
Johan Euphrosine | 0b7bb9f | 2015-09-29 01:11:21 -0700 | [diff] [blame] | 13 | #include <base/gtest_prod_util.h> |
Vitaly Buka | d322a3c | 2015-08-16 01:03:02 -0700 | [diff] [blame] | 14 | #include <weave/error.h> |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 15 | #include <weave/provider/config_store.h> |
Vitaly Buka | d322a3c | 2015-08-16 01:03:02 -0700 | [diff] [blame] | 16 | |
Vitaly Buka | 63c9862 | 2015-12-11 11:06:04 -0800 | [diff] [blame] | 17 | #include "src/privet/privet_types.h" |
Vitaly Buka | d322a3c | 2015-08-16 01:03:02 -0700 | [diff] [blame] | 18 | |
| 19 | namespace weave { |
| 20 | |
| 21 | class StorageInterface; |
| 22 | |
Vitaly Buka | 63c9862 | 2015-12-11 11:06:04 -0800 | [diff] [blame] | 23 | enum class RootClientTokenOwner { |
Vitaly Buka | c3bc82a | 2015-12-14 23:24:13 -0800 | [diff] [blame] | 24 | // Keep order as it's used with order comparison operators. |
Vitaly Buka | 63c9862 | 2015-12-11 11:06:04 -0800 | [diff] [blame] | 25 | kNone, |
| 26 | kClient, |
| 27 | kCloud, |
| 28 | }; |
| 29 | |
Vitaly Buka | d322a3c | 2015-08-16 01:03:02 -0700 | [diff] [blame] | 30 | // Handles reading buffet config and state files. |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 31 | class Config final { |
Vitaly Buka | d322a3c | 2015-08-16 01:03:02 -0700 | [diff] [blame] | 32 | public: |
Vitaly Buka | 238db69 | 2015-09-29 16:58:39 -0700 | [diff] [blame] | 33 | struct Settings : public weave::Settings { |
| 34 | std::string refresh_token; |
| 35 | std::string robot_account; |
| 36 | std::string last_configured_ssid; |
Vitaly Buka | f08caeb | 2015-12-02 13:47:48 -0800 | [diff] [blame] | 37 | std::vector<uint8_t> secret; |
Vitaly Buka | 63c9862 | 2015-12-11 11:06:04 -0800 | [diff] [blame] | 38 | RootClientTokenOwner root_client_token_owner{RootClientTokenOwner::kNone}; |
Vitaly Buka | 238db69 | 2015-09-29 16:58:39 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | using OnChangedCallback = base::Callback<void(const weave::Settings&)>; |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 42 | ~Config() = default; |
Vitaly Buka | d322a3c | 2015-08-16 01:03:02 -0700 | [diff] [blame] | 43 | |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 44 | explicit Config(provider::ConfigStore* config_store); |
Vitaly Buka | d322a3c | 2015-08-16 01:03:02 -0700 | [diff] [blame] | 45 | |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 46 | void AddOnChangedCallback(const OnChangedCallback& callback); |
Vitaly Buka | 238db69 | 2015-09-29 16:58:39 -0700 | [diff] [blame] | 47 | const Config::Settings& GetSettings() const; |
Vitaly Buka | d322a3c | 2015-08-16 01:03:02 -0700 | [diff] [blame] | 48 | |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 49 | void Load(); |
Vitaly Buka | d322a3c | 2015-08-16 01:03:02 -0700 | [diff] [blame] | 50 | |
| 51 | // Allows editing of config. Makes sure that callbacks were called and changes |
| 52 | // were saved. |
| 53 | // User can commit changes by calling Commit method or by destroying the |
| 54 | // object. |
| 55 | class Transaction final { |
| 56 | public: |
| 57 | explicit Transaction(Config* config) |
| 58 | : config_(config), settings_(&config->settings_) { |
| 59 | CHECK(config_); |
| 60 | } |
| 61 | |
| 62 | ~Transaction(); |
| 63 | |
| 64 | void set_client_id(const std::string& id) { settings_->client_id = id; } |
| 65 | void set_client_secret(const std::string& secret) { |
| 66 | settings_->client_secret = secret; |
| 67 | } |
| 68 | void set_api_key(const std::string& key) { settings_->api_key = key; } |
| 69 | void set_oauth_url(const std::string& url) { settings_->oauth_url = url; } |
| 70 | void set_service_url(const std::string& url) { |
| 71 | settings_->service_url = url; |
| 72 | } |
| 73 | void set_name(const std::string& name) { settings_->name = name; } |
| 74 | void set_description(const std::string& description) { |
| 75 | settings_->description = description; |
| 76 | } |
| 77 | void set_location(const std::string& location) { |
| 78 | settings_->location = location; |
| 79 | } |
Vitaly Buka | b624bc4 | 2015-09-29 19:13:55 -0700 | [diff] [blame] | 80 | void set_local_anonymous_access_role(AuthScope role) { |
| 81 | settings_->local_anonymous_access_role = role; |
| 82 | } |
Vitaly Buka | d322a3c | 2015-08-16 01:03:02 -0700 | [diff] [blame] | 83 | void set_local_discovery_enabled(bool enabled) { |
| 84 | settings_->local_discovery_enabled = enabled; |
| 85 | } |
| 86 | void set_local_pairing_enabled(bool enabled) { |
| 87 | settings_->local_pairing_enabled = enabled; |
| 88 | } |
Johan Euphrosine | 312c2f5 | 2015-09-29 00:04:29 -0700 | [diff] [blame] | 89 | void set_cloud_id(const std::string& id) { settings_->cloud_id = id; } |
Vitaly Buka | d322a3c | 2015-08-16 01:03:02 -0700 | [diff] [blame] | 90 | void set_refresh_token(const std::string& token) { |
| 91 | settings_->refresh_token = token; |
| 92 | } |
| 93 | void set_robot_account(const std::string& account) { |
| 94 | settings_->robot_account = account; |
| 95 | } |
| 96 | void set_last_configured_ssid(const std::string& ssid) { |
| 97 | settings_->last_configured_ssid = ssid; |
| 98 | } |
Vitaly Buka | f08caeb | 2015-12-02 13:47:48 -0800 | [diff] [blame] | 99 | void set_secret(const std::vector<uint8_t>& secret) { |
| 100 | settings_->secret = secret; |
| 101 | } |
Vitaly Buka | 63c9862 | 2015-12-11 11:06:04 -0800 | [diff] [blame] | 102 | void set_root_client_token_owner( |
| 103 | RootClientTokenOwner root_client_token_owner) { |
| 104 | settings_->root_client_token_owner = root_client_token_owner; |
Vitaly Buka | a580328 | 2015-12-06 20:11:55 -0800 | [diff] [blame] | 105 | } |
Vitaly Buka | d322a3c | 2015-08-16 01:03:02 -0700 | [diff] [blame] | 106 | |
| 107 | void Commit(); |
| 108 | |
| 109 | private: |
Johan Euphrosine | 0b7bb9f | 2015-09-29 01:11:21 -0700 | [diff] [blame] | 110 | FRIEND_TEST_ALL_PREFIXES(ConfigTest, Setters); |
| 111 | void set_device_id(const std::string& id) { |
| 112 | config_->settings_.device_id = id; |
| 113 | } |
| 114 | |
Vitaly Buka | d322a3c | 2015-08-16 01:03:02 -0700 | [diff] [blame] | 115 | friend class Config; |
| 116 | void LoadState(); |
| 117 | Config* config_; |
| 118 | Settings* settings_; |
| 119 | bool save_{true}; |
| 120 | }; |
| 121 | |
Vitaly Buka | d322a3c | 2015-08-16 01:03:02 -0700 | [diff] [blame] | 122 | private: |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 123 | void Save(); |
Vitaly Buka | d322a3c | 2015-08-16 01:03:02 -0700 | [diff] [blame] | 124 | |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 125 | Settings settings_; |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 126 | provider::ConfigStore* config_store_{nullptr}; |
Vitaly Buka | d322a3c | 2015-08-16 01:03:02 -0700 | [diff] [blame] | 127 | std::vector<OnChangedCallback> on_changed_; |
| 128 | |
| 129 | DISALLOW_COPY_AND_ASSIGN(Config); |
| 130 | }; |
| 131 | |
| 132 | } // namespace weave |
| 133 | |
| 134 | #endif // LIBWEAVE_SRC_CONFIG_H_ |