Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 1 | // 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 | |
| 8 | #include <string> |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 9 | #include <vector> |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 10 | |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 11 | #include <base/callback.h> |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 12 | #include <base/files/file_path.h> |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 13 | #include <chromeos/errors/error.h> |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 14 | #include <chromeos/key_value_store.h> |
| 15 | |
| 16 | namespace buffet { |
| 17 | |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 18 | class StorageInterface; |
| 19 | |
| 20 | // Handles reading buffet config and state files. |
| 21 | class BuffetConfig final { |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 22 | public: |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 23 | using OnChangedCallback = base::Callback<void(const BuffetConfig&)>; |
| 24 | |
| 25 | explicit BuffetConfig(std::unique_ptr<StorageInterface> storage); |
| 26 | |
| 27 | explicit BuffetConfig(const base::FilePath& state_path); |
| 28 | |
| 29 | void AddOnChangedCallback(const OnChangedCallback& callback) { |
| 30 | on_changed_.push_back(callback); |
| 31 | // Force to read current state. |
| 32 | callback.Run(*this); |
| 33 | } |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 34 | |
| 35 | void Load(const base::FilePath& config_path); |
| 36 | void Load(const chromeos::KeyValueStore& store); |
| 37 | |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 38 | // Allows editing of config. Makes sure that callbacks were called and changes |
| 39 | // were saved. |
| 40 | // User can commit changes by calling Commit method or by destroying the |
| 41 | // object. |
| 42 | class Transaction final { |
| 43 | public: |
| 44 | explicit Transaction(BuffetConfig* config) : config_(config) { |
| 45 | CHECK(config_); |
| 46 | } |
| 47 | |
| 48 | ~Transaction(); |
| 49 | |
Vitaly Buka | ff81db6 | 2015-05-14 21:25:45 -0700 | [diff] [blame] | 50 | void set_client_id(const std::string& id) { config_->client_id_ = id; } |
| 51 | void set_client_secret(const std::string& secret) { |
| 52 | config_->client_secret_ = secret; |
| 53 | } |
| 54 | void set_api_key(const std::string& key) { config_->api_key_ = key; } |
| 55 | void set_oauth_url(const std::string& url) { config_->oauth_url_ = url; } |
| 56 | void set_service_url(const std::string& url) { |
| 57 | config_->service_url_ = url; |
| 58 | } |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 59 | bool set_name(const std::string& name); |
| 60 | void set_description(const std::string& description) { |
| 61 | config_->description_ = description; |
| 62 | } |
| 63 | void set_location(const std::string& location) { |
| 64 | config_->location_ = location; |
| 65 | } |
| 66 | bool set_local_anonymous_access_role(const std::string& role); |
| 67 | void set_local_discovery_enabled(bool enabled) { |
| 68 | config_->local_discovery_enabled_ = enabled; |
| 69 | } |
| 70 | void set_local_pairing_enabled(bool enabled) { |
| 71 | config_->local_pairing_enabled_ = enabled; |
| 72 | } |
| 73 | void set_device_id(const std::string& id) { config_->device_id_ = id; } |
| 74 | void set_refresh_token(const std::string& token) { |
| 75 | config_->refresh_token_ = token; |
| 76 | } |
| 77 | void set_robot_account(const std::string& account) { |
| 78 | config_->robot_account_ = account; |
| 79 | } |
| 80 | |
| 81 | void Commit(); |
| 82 | |
| 83 | private: |
| 84 | friend class BuffetConfig; |
| 85 | void LoadState(); |
| 86 | BuffetConfig* config_; |
| 87 | bool save_{true}; |
| 88 | }; |
| 89 | |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 90 | const std::string& client_id() const { return client_id_; } |
| 91 | const std::string& client_secret() const { return client_secret_; } |
| 92 | const std::string& api_key() const { return api_key_; } |
| 93 | const std::string& oauth_url() const { return oauth_url_; } |
| 94 | const std::string& service_url() const { return service_url_; } |
| 95 | const std::string& oem_name() const { return oem_name_; } |
| 96 | const std::string& model_name() const { return model_name_; } |
| 97 | const std::string& model_id() const { return model_id_; } |
| 98 | const std::string& device_kind() const { return device_kind_; } |
Christopher Wiley | 34eae04 | 2015-03-18 10:25:08 -0700 | [diff] [blame] | 99 | uint64_t polling_period_ms() const { return polling_period_ms_; } |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 100 | uint64_t backup_polling_period_ms() const { |
| 101 | return backup_polling_period_ms_; |
| 102 | } |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 103 | |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 104 | const std::string& name() const { return name_; } |
| 105 | const std::string& description() const { return description_; } |
| 106 | const std::string& location() const { return location_; } |
Vitaly Buka | 4a3a9a2 | 2015-05-13 16:06:01 -0700 | [diff] [blame] | 107 | std::string local_anonymous_access_role() const { |
| 108 | return local_anonymous_access_role_; |
| 109 | } |
| 110 | bool local_pairing_enabled() const { return local_pairing_enabled_; } |
| 111 | bool local_discovery_enabled() const { return local_discovery_enabled_; } |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 112 | |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 113 | std::string device_id() const { return device_id_; } |
| 114 | std::string refresh_token() const { return refresh_token_; } |
| 115 | std::string robot_account() const { return robot_account_; } |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 116 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 117 | private: |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 118 | bool Save(); |
| 119 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 120 | std::string client_id_{"58855907228.apps.googleusercontent.com"}; |
| 121 | std::string client_secret_{"eHSAREAHrIqPsHBxCE9zPPBi"}; |
| 122 | std::string api_key_{"AIzaSyDSq46gG-AxUnC3zoqD9COIPrjolFsMfMA"}; |
| 123 | std::string oauth_url_{"https://accounts.google.com/o/oauth2/"}; |
| 124 | std::string service_url_{"https://www.googleapis.com/clouddevices/v1/"}; |
Alex Vakulenko | 468f7f3 | 2015-04-08 10:42:45 -0700 | [diff] [blame] | 125 | std::string name_{"Developer device"}; |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 126 | std::string description_; |
| 127 | std::string location_; |
Vitaly Buka | 4a3a9a2 | 2015-05-13 16:06:01 -0700 | [diff] [blame] | 128 | std::string local_anonymous_access_role_{"viewer"}; |
| 129 | bool local_discovery_enabled_{true}; |
| 130 | bool local_pairing_enabled_{true}; |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 131 | std::string oem_name_{"Chromium"}; |
| 132 | std::string model_name_{"Brillo"}; |
| 133 | std::string model_id_{"AAAAA"}; |
Alex Vakulenko | 1fb9217 | 2015-04-20 10:59:20 -0700 | [diff] [blame] | 134 | std::string device_kind_{"vendor"}; |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 135 | uint64_t polling_period_ms_{7000}; // 7 seconds. |
| 136 | uint64_t backup_polling_period_ms_{30 * 60 * 1000}; // 30 minutes. |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 137 | |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 138 | std::string device_id_; |
| 139 | std::string refresh_token_; |
| 140 | std::string robot_account_; |
| 141 | |
| 142 | // Serialization interface to save and load buffet state. |
| 143 | std::unique_ptr<StorageInterface> storage_; |
| 144 | |
| 145 | std::vector<OnChangedCallback> on_changed_; |
| 146 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 147 | DISALLOW_COPY_AND_ASSIGN(BuffetConfig); |
| 148 | }; |
| 149 | |
| 150 | } // namespace buffet |
| 151 | |
| 152 | #endif // BUFFET_BUFFET_CONFIG_H_ |