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 | #include "buffet/buffet_config.h" |
| 6 | |
Alex Vakulenko | f028174 | 2015-05-18 14:31:10 -0700 | [diff] [blame] | 7 | #include <base/files/file_util.h> |
Christopher Wiley | 34eae04 | 2015-03-18 10:25:08 -0700 | [diff] [blame] | 8 | #include <base/logging.h> |
| 9 | #include <base/strings/string_number_conversions.h> |
| 10 | |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 11 | #include "buffet/storage_impls.h" |
| 12 | #include "buffet/storage_interface.h" |
| 13 | |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 14 | namespace { |
| 15 | |
| 16 | // TODO(vitalybuka): Remove this when deviceKind is gone from server. |
| 17 | std::string GetDeviceKind(const std::string& manifest_id) { |
| 18 | CHECK_EQ(5u, manifest_id.size()); |
| 19 | std::string kind = manifest_id.substr(0, 2); |
| 20 | if (kind == "AC") |
| 21 | return "accessPoint"; |
| 22 | if (kind == "AK") |
| 23 | return "aggregator"; |
| 24 | if (kind == "AM") |
| 25 | return "camera"; |
| 26 | if (kind == "AB") |
| 27 | return "developmentBoard"; |
| 28 | if (kind == "AE") |
| 29 | return "printer"; |
| 30 | if (kind == "AF") |
| 31 | return "scanner"; |
| 32 | if (kind == "AD") |
| 33 | return "speaker"; |
| 34 | if (kind == "AL") |
| 35 | return "storage"; |
| 36 | if (kind == "AJ") |
| 37 | return "toy"; |
| 38 | if (kind == "AA") |
| 39 | return "vendor"; |
| 40 | if (kind == "AN") |
| 41 | return "video"; |
| 42 | LOG(FATAL) << "Invalid model id: " << manifest_id; |
| 43 | return std::string(); |
| 44 | } |
| 45 | |
Vitaly Buka | 2c1029f | 2015-04-30 22:47:18 -0700 | [diff] [blame] | 46 | bool IsValidAccessRole(const std::string& role) { |
Vitaly Buka | 9467520 | 2015-05-13 11:48:54 -0700 | [diff] [blame] | 47 | return role == "none" || role == "viewer" || role == "user"; |
Vitaly Buka | 2c1029f | 2015-04-30 22:47:18 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 50 | } // namespace |
| 51 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 52 | namespace buffet { |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 53 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 54 | namespace config_keys { |
| 55 | |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 56 | const char kClientId[] = "client_id"; |
| 57 | const char kClientSecret[] = "client_secret"; |
| 58 | const char kApiKey[] = "api_key"; |
| 59 | const char kOAuthURL[] = "oauth_url"; |
| 60 | const char kServiceURL[] = "service_url"; |
| 61 | const char kName[] = "name"; |
| 62 | const char kDescription[] = "description"; |
| 63 | const char kLocation[] = "location"; |
Vitaly Buka | 4a3a9a2 | 2015-05-13 16:06:01 -0700 | [diff] [blame] | 64 | const char kLocalAnonymousAccessRole[] = "local_anonymous_access_role"; |
| 65 | const char kLocalDiscoveryEnabled[] = "local_discovery_enabled"; |
| 66 | const char kLocalPairingEnabled[] = "local_pairing_enabled"; |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 67 | const char kOemName[] = "oem_name"; |
| 68 | const char kModelName[] = "model_name"; |
| 69 | const char kModelId[] = "model_id"; |
Vitaly Buka | 2c1029f | 2015-04-30 22:47:18 -0700 | [diff] [blame] | 70 | const char kPollingPeriodMs[] = "polling_period_ms"; |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 71 | const char kBackupPollingPeriodMs[] = "backup_polling_period_ms"; |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 72 | const char kRefreshToken[] = "refresh_token"; |
| 73 | const char kDeviceId[] = "device_id"; |
| 74 | const char kRobotAccount[] = "robot_account"; |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 75 | |
| 76 | } // namespace config_keys |
| 77 | |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 78 | BuffetConfig::BuffetConfig(std::unique_ptr<StorageInterface> storage) |
| 79 | : storage_{std::move(storage)} { |
| 80 | } |
| 81 | |
| 82 | BuffetConfig::BuffetConfig(const base::FilePath& state_path) |
| 83 | : BuffetConfig{ |
| 84 | std::unique_ptr<StorageInterface>{new FileStorage{state_path}}} { |
| 85 | } |
| 86 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 87 | void BuffetConfig::Load(const base::FilePath& config_path) { |
| 88 | chromeos::KeyValueStore store; |
Alex Vakulenko | f028174 | 2015-05-18 14:31:10 -0700 | [diff] [blame] | 89 | if (base::PathExists(config_path)) { |
| 90 | CHECK(store.Load(config_path)) << "Unable to read or parse config file at" |
| 91 | << config_path.value(); |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 92 | } |
Alex Vakulenko | f028174 | 2015-05-18 14:31:10 -0700 | [diff] [blame] | 93 | Load(store); |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void BuffetConfig::Load(const chromeos::KeyValueStore& store) { |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 97 | Transaction change{this}; |
| 98 | change.save_ = false; |
| 99 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 100 | store.GetString(config_keys::kClientId, &client_id_); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 101 | CHECK(!client_id_.empty()); |
| 102 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 103 | store.GetString(config_keys::kClientSecret, &client_secret_); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 104 | CHECK(!client_secret_.empty()); |
| 105 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 106 | store.GetString(config_keys::kApiKey, &api_key_); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 107 | CHECK(!api_key_.empty()); |
| 108 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 109 | store.GetString(config_keys::kOAuthURL, &oauth_url_); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 110 | CHECK(!oauth_url_.empty()); |
| 111 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 112 | store.GetString(config_keys::kServiceURL, &service_url_); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 113 | CHECK(!service_url_.empty()); |
| 114 | |
| 115 | store.GetString(config_keys::kOemName, &oem_name_); |
| 116 | CHECK(!oem_name_.empty()); |
| 117 | |
| 118 | store.GetString(config_keys::kModelName, &model_name_); |
| 119 | CHECK(!model_name_.empty()); |
| 120 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 121 | store.GetString(config_keys::kModelId, &model_id_); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 122 | device_kind_ = GetDeviceKind(model_id_); |
| 123 | |
Christopher Wiley | 34eae04 | 2015-03-18 10:25:08 -0700 | [diff] [blame] | 124 | std::string polling_period_str; |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 125 | if (store.GetString(config_keys::kPollingPeriodMs, &polling_period_str)) |
| 126 | CHECK(base::StringToUint64(polling_period_str, &polling_period_ms_)); |
| 127 | |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 128 | if (store.GetString(config_keys::kBackupPollingPeriodMs, &polling_period_str)) |
| 129 | CHECK(base::StringToUint64(polling_period_str, &backup_polling_period_ms_)); |
| 130 | |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 131 | store.GetString(config_keys::kName, &name_); |
| 132 | CHECK(!name_.empty()); |
| 133 | |
| 134 | store.GetString(config_keys::kDescription, &description_); |
| 135 | store.GetString(config_keys::kLocation, &location_); |
Vitaly Buka | 2c1029f | 2015-04-30 22:47:18 -0700 | [diff] [blame] | 136 | |
Vitaly Buka | 4a3a9a2 | 2015-05-13 16:06:01 -0700 | [diff] [blame] | 137 | store.GetString(config_keys::kLocalAnonymousAccessRole, |
| 138 | &local_anonymous_access_role_); |
| 139 | CHECK(IsValidAccessRole(local_anonymous_access_role_)) |
| 140 | << "Invalid role: " << local_anonymous_access_role_; |
| 141 | |
| 142 | store.GetBoolean(config_keys::kLocalDiscoveryEnabled, |
| 143 | &local_discovery_enabled_); |
| 144 | store.GetBoolean(config_keys::kLocalPairingEnabled, &local_pairing_enabled_); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 145 | |
| 146 | change.LoadState(); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 149 | void BuffetConfig::Transaction::LoadState() { |
| 150 | if (!config_->storage_) |
| 151 | return; |
| 152 | auto value = config_->storage_->Load(); |
| 153 | const base::DictionaryValue* dict = nullptr; |
| 154 | if (!value || !value->GetAsDictionary(&dict)) |
| 155 | return; |
| 156 | |
Vitaly Buka | ff81db6 | 2015-05-14 21:25:45 -0700 | [diff] [blame] | 157 | std::string tmp; |
| 158 | bool tmp_bool{false}; |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 159 | |
Vitaly Buka | ff81db6 | 2015-05-14 21:25:45 -0700 | [diff] [blame] | 160 | if (dict->GetString(config_keys::kClientId, &tmp)) |
| 161 | set_client_id(tmp); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 162 | |
Vitaly Buka | ff81db6 | 2015-05-14 21:25:45 -0700 | [diff] [blame] | 163 | if (dict->GetString(config_keys::kClientSecret, &tmp)) |
| 164 | set_client_secret(tmp); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 165 | |
Vitaly Buka | ff81db6 | 2015-05-14 21:25:45 -0700 | [diff] [blame] | 166 | if (dict->GetString(config_keys::kApiKey, &tmp)) |
| 167 | set_api_key(tmp); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 168 | |
Vitaly Buka | ff81db6 | 2015-05-14 21:25:45 -0700 | [diff] [blame] | 169 | if (dict->GetString(config_keys::kOAuthURL, &tmp)) |
| 170 | set_oauth_url(tmp); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 171 | |
Vitaly Buka | ff81db6 | 2015-05-14 21:25:45 -0700 | [diff] [blame] | 172 | if (dict->GetString(config_keys::kServiceURL, &tmp)) |
| 173 | set_service_url(tmp); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 174 | |
Vitaly Buka | ff81db6 | 2015-05-14 21:25:45 -0700 | [diff] [blame] | 175 | if (dict->GetString(config_keys::kName, &tmp)) |
| 176 | set_name(tmp); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 177 | |
Vitaly Buka | ff81db6 | 2015-05-14 21:25:45 -0700 | [diff] [blame] | 178 | if (dict->GetString(config_keys::kDescription, &tmp)) |
| 179 | set_description(tmp); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 180 | |
Vitaly Buka | ff81db6 | 2015-05-14 21:25:45 -0700 | [diff] [blame] | 181 | if (dict->GetString(config_keys::kLocation, &tmp)) |
| 182 | set_location(tmp); |
| 183 | |
| 184 | if (dict->GetString(config_keys::kLocalAnonymousAccessRole, &tmp)) |
| 185 | set_local_anonymous_access_role(tmp); |
| 186 | |
| 187 | if (dict->GetBoolean(config_keys::kLocalDiscoveryEnabled, &tmp_bool)) |
| 188 | set_local_discovery_enabled(tmp_bool); |
| 189 | |
| 190 | if (dict->GetBoolean(config_keys::kLocalPairingEnabled, &tmp_bool)) |
| 191 | set_local_pairing_enabled(tmp_bool); |
| 192 | |
| 193 | if (dict->GetString(config_keys::kRefreshToken, &tmp)) |
| 194 | set_refresh_token(tmp); |
| 195 | |
| 196 | if (dict->GetString(config_keys::kRobotAccount, &tmp)) |
| 197 | set_robot_account(tmp); |
| 198 | |
| 199 | if (dict->GetString(config_keys::kDeviceId, &tmp)) |
| 200 | set_device_id(tmp); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | bool BuffetConfig::Save() { |
| 204 | if (!storage_) |
| 205 | return false; |
| 206 | base::DictionaryValue dict; |
Vitaly Buka | ff81db6 | 2015-05-14 21:25:45 -0700 | [diff] [blame] | 207 | dict.SetString(config_keys::kClientId, client_id_); |
| 208 | dict.SetString(config_keys::kClientSecret, client_secret_); |
| 209 | dict.SetString(config_keys::kApiKey, api_key_); |
| 210 | dict.SetString(config_keys::kOAuthURL, oauth_url_); |
| 211 | dict.SetString(config_keys::kServiceURL, service_url_); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 212 | dict.SetString(config_keys::kRefreshToken, refresh_token_); |
| 213 | dict.SetString(config_keys::kDeviceId, device_id_); |
| 214 | dict.SetString(config_keys::kRobotAccount, robot_account_); |
| 215 | dict.SetString(config_keys::kName, name_); |
| 216 | dict.SetString(config_keys::kDescription, description_); |
| 217 | dict.SetString(config_keys::kLocation, location_); |
| 218 | dict.SetString(config_keys::kLocalAnonymousAccessRole, |
| 219 | local_anonymous_access_role_); |
| 220 | dict.SetBoolean(config_keys::kLocalDiscoveryEnabled, |
| 221 | local_discovery_enabled_); |
| 222 | dict.SetBoolean(config_keys::kLocalPairingEnabled, local_pairing_enabled_); |
| 223 | |
| 224 | return storage_->Save(dict); |
| 225 | } |
| 226 | |
| 227 | BuffetConfig::Transaction::~Transaction() { |
| 228 | Commit(); |
| 229 | } |
| 230 | |
| 231 | bool BuffetConfig::Transaction::set_name(const std::string& name) { |
Vitaly Buka | 4a3a9a2 | 2015-05-13 16:06:01 -0700 | [diff] [blame] | 232 | if (name.empty()) { |
| 233 | LOG(ERROR) << "Invalid name: " << name; |
| 234 | return false; |
Vitaly Buka | 2c1029f | 2015-04-30 22:47:18 -0700 | [diff] [blame] | 235 | } |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 236 | config_->name_ = name; |
Vitaly Buka | 4a3a9a2 | 2015-05-13 16:06:01 -0700 | [diff] [blame] | 237 | return true; |
| 238 | } |
| 239 | |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 240 | bool BuffetConfig::Transaction::set_local_anonymous_access_role( |
| 241 | const std::string& role) { |
Vitaly Buka | 4a3a9a2 | 2015-05-13 16:06:01 -0700 | [diff] [blame] | 242 | if (!IsValidAccessRole(role)) { |
| 243 | LOG(ERROR) << "Invalid role: " << role; |
| 244 | return false; |
| 245 | } |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 246 | config_->local_anonymous_access_role_ = role; |
Vitaly Buka | 4a3a9a2 | 2015-05-13 16:06:01 -0700 | [diff] [blame] | 247 | return true; |
Vitaly Buka | 2c1029f | 2015-04-30 22:47:18 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 250 | void BuffetConfig::Transaction::Commit() { |
| 251 | if (!config_) |
| 252 | return; |
| 253 | if (save_) |
| 254 | config_->Save(); |
| 255 | for (const auto& cb : config_->on_changed_) |
| 256 | cb.Run(*config_); |
| 257 | config_ = nullptr; |
| 258 | } |
| 259 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 260 | } // namespace buffet |