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