Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 1 | // Copyright 2014 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/device_registration_info.h" |
| 6 | |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 7 | #include <memory> |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 8 | #include <utility> |
| 9 | #include <vector> |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 10 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 11 | #include <base/json/json_writer.h> |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 12 | #include <base/message_loop/message_loop.h> |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 13 | #include <base/values.h> |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 14 | #include <chromeos/bind_lambda.h> |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 15 | #include <chromeos/data_encoding.h> |
Anton Muhin | 233d2ee | 2014-10-22 15:16:24 +0400 | [diff] [blame] | 16 | #include <chromeos/errors/error_codes.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 17 | #include <chromeos/http/http_utils.h> |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 18 | #include <chromeos/key_value_store.h> |
Alex Vakulenko | 3aeea1c | 2014-08-20 16:33:12 -0700 | [diff] [blame] | 19 | #include <chromeos/mime_utils.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 20 | #include <chromeos/strings/string_utils.h> |
Alex Vakulenko | bd5b544 | 2014-08-20 16:16:34 -0700 | [diff] [blame] | 21 | #include <chromeos/url_utils.h> |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 22 | |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 23 | #include "buffet/commands/cloud_command_proxy.h" |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 24 | #include "buffet/commands/command_definition.h" |
| 25 | #include "buffet/commands/command_manager.h" |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 26 | #include "buffet/device_registration_storage_keys.h" |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 27 | #include "buffet/states/state_manager.h" |
Alex Vakulenko | 5841c30 | 2014-07-23 10:49:49 -0700 | [diff] [blame] | 28 | #include "buffet/storage_impls.h" |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 29 | #include "buffet/utils.h" |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 30 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 31 | const char buffet::kErrorDomainOAuth2[] = "oauth2"; |
| 32 | const char buffet::kErrorDomainGCD[] = "gcd"; |
| 33 | const char buffet::kErrorDomainGCDServer[] = "gcd_server"; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 34 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 35 | namespace buffet { |
| 36 | namespace storage_keys { |
| 37 | |
Christopher Wiley | ab8af68 | 2015-02-08 09:25:34 -0800 | [diff] [blame] | 38 | // Statically configured fields |
| 39 | const char kApiKey[] = "api_key"; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 40 | const char kClientId[] = "client_id"; |
| 41 | const char kClientSecret[] = "client_secret"; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 42 | const char kOAuthURL[] = "oauth_url"; |
| 43 | const char kServiceURL[] = "service_url"; |
Christopher Wiley | ab8af68 | 2015-02-08 09:25:34 -0800 | [diff] [blame] | 44 | // Credentials related to a particular registration. |
| 45 | const char kDeviceId[] = "device_id"; |
| 46 | const char kRefreshToken[] = "refresh_token"; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 47 | const char kRobotAccount[] = "robot_account"; |
Christopher Wiley | ab8af68 | 2015-02-08 09:25:34 -0800 | [diff] [blame] | 48 | // Fields in our device metadata. |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 49 | const char kDeviceKind[] = "device_kind"; |
Anton Muhin | 9e19cdd | 2014-10-20 13:55:04 +0400 | [diff] [blame] | 50 | const char kName[] = "name"; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 51 | const char kDisplayName[] = "display_name"; |
Vitaly Buka | d3eb19c | 2014-11-21 13:39:43 -0800 | [diff] [blame] | 52 | const char kDescription[] = "description"; |
| 53 | const char kLocation[] = "location"; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 54 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 55 | } // namespace storage_keys |
| 56 | } // namespace buffet |
| 57 | |
| 58 | namespace { |
| 59 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 60 | const base::FilePath::CharType kDeviceInfoFilePath[] = |
| 61 | FILE_PATH_LITERAL("/var/lib/buffet/device_reg_info"); |
| 62 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 63 | std::pair<std::string, std::string> BuildAuthHeader( |
| 64 | const std::string& access_token_type, |
| 65 | const std::string& access_token) { |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 66 | std::string authorization = |
Alex Vakulenko | b8fc1df | 2014-08-20 15:38:07 -0700 | [diff] [blame] | 67 | chromeos::string_utils::Join(' ', access_token_type, access_token); |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 68 | return {chromeos::http::request_header::kAuthorization, authorization}; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | std::unique_ptr<base::DictionaryValue> ParseOAuthResponse( |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 72 | const chromeos::http::Response* response, chromeos::ErrorPtr* error) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 73 | int code = 0; |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 74 | auto resp = chromeos::http::ParseJsonResponse(response, &code, error); |
| 75 | if (resp && code >= chromeos::http::status_code::BadRequest) { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 76 | if (error) { |
| 77 | std::string error_code, error_message; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 78 | if (resp->GetString("error", &error_code) && |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 79 | resp->GetString("error_description", &error_message)) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 80 | chromeos::Error::AddTo(error, FROM_HERE, buffet::kErrorDomainOAuth2, |
| 81 | error_code, error_message); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 82 | } else { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 83 | chromeos::Error::AddTo(error, FROM_HERE, buffet::kErrorDomainOAuth2, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 84 | "unexpected_response", "Unexpected OAuth error"); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | return std::unique_ptr<base::DictionaryValue>(); |
| 88 | } |
| 89 | return resp; |
| 90 | } |
| 91 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 92 | inline void SetUnexpectedError(chromeos::ErrorPtr* error) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 93 | chromeos::Error::AddTo(error, FROM_HERE, buffet::kErrorDomainGCD, |
| 94 | "unexpected_response", "Unexpected GCD error"); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 97 | void ParseGCDError(const base::DictionaryValue* json, |
| 98 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 99 | if (!error) |
| 100 | return; |
| 101 | |
| 102 | const base::Value* list_value = nullptr; |
| 103 | const base::ListValue* error_list = nullptr; |
| 104 | if (!json->Get("error.errors", &list_value) || |
| 105 | !list_value->GetAsList(&error_list)) { |
| 106 | SetUnexpectedError(error); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | for (size_t i = 0; i < error_list->GetSize(); i++) { |
| 111 | const base::Value* error_value = nullptr; |
| 112 | const base::DictionaryValue* error_object = nullptr; |
| 113 | if (!error_list->Get(i, &error_value) || |
| 114 | !error_value->GetAsDictionary(&error_object)) { |
| 115 | SetUnexpectedError(error); |
| 116 | continue; |
| 117 | } |
| 118 | std::string error_code, error_message; |
| 119 | if (error_object->GetString("reason", &error_code) && |
| 120 | error_object->GetString("message", &error_message)) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 121 | chromeos::Error::AddTo(error, FROM_HERE, buffet::kErrorDomainGCDServer, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 122 | error_code, error_message); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 123 | } else { |
| 124 | SetUnexpectedError(error); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
Alex Vakulenko | bda220a | 2014-04-18 15:25:44 -0700 | [diff] [blame] | 129 | std::string BuildURL(const std::string& url, |
| 130 | const std::vector<std::string>& subpaths, |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 131 | const chromeos::data_encoding::WebParamList& params) { |
Alex Vakulenko | bd5b544 | 2014-08-20 16:16:34 -0700 | [diff] [blame] | 132 | std::string result = chromeos::url::CombineMultiple(url, subpaths); |
| 133 | return chromeos::url::AppendQueryParams(result, params); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 136 | void IgnoreCloudError(const chromeos::Error*) { |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void IgnoreCloudResult(const base::DictionaryValue&) { |
| 140 | } |
Anton Muhin | 6d2569e | 2014-10-30 12:32:27 +0400 | [diff] [blame] | 141 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 142 | } // anonymous namespace |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 143 | |
| 144 | namespace buffet { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 145 | |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 146 | DeviceRegistrationInfo::DeviceRegistrationInfo( |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 147 | const std::shared_ptr<CommandManager>& command_manager, |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 148 | const std::shared_ptr<StateManager>& state_manager, |
| 149 | std::unique_ptr<chromeos::KeyValueStore> config_store) |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 150 | : DeviceRegistrationInfo( |
| 151 | command_manager, |
| 152 | state_manager, |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 153 | std::move(config_store), |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 154 | chromeos::http::Transport::CreateDefault(), |
| 155 | // TODO(avakulenko): Figure out security implications of storing |
| 156 | // this data unencrypted. |
| 157 | std::make_shared<FileStorage>(base::FilePath{kDeviceInfoFilePath})) { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | DeviceRegistrationInfo::DeviceRegistrationInfo( |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 161 | const std::shared_ptr<CommandManager>& command_manager, |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 162 | const std::shared_ptr<StateManager>& state_manager, |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 163 | std::unique_ptr<chromeos::KeyValueStore> config_store, |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 164 | const std::shared_ptr<chromeos::http::Transport>& transport, |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 165 | const std::shared_ptr<StorageInterface>& storage) |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 166 | : transport_{transport}, |
| 167 | storage_{storage}, |
| 168 | command_manager_{command_manager}, |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 169 | state_manager_{state_manager}, |
| 170 | config_store_{std::move(config_store)} { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 171 | } |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 172 | |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 173 | DeviceRegistrationInfo::~DeviceRegistrationInfo() = default; |
| 174 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 175 | std::pair<std::string, std::string> |
| 176 | DeviceRegistrationInfo::GetAuthorizationHeader() const { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 177 | return BuildAuthHeader("Bearer", access_token_); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | std::string DeviceRegistrationInfo::GetServiceURL( |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 181 | const std::string& subpath, |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 182 | const chromeos::data_encoding::WebParamList& params) const { |
Alex Vakulenko | bda220a | 2014-04-18 15:25:44 -0700 | [diff] [blame] | 183 | return BuildURL(service_url_, {subpath}, params); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | std::string DeviceRegistrationInfo::GetDeviceURL( |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 187 | const std::string& subpath, |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 188 | const chromeos::data_encoding::WebParamList& params) const { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 189 | CHECK(!device_id_.empty()) << "Must have a valid device ID"; |
Alex Vakulenko | bda220a | 2014-04-18 15:25:44 -0700 | [diff] [blame] | 190 | return BuildURL(service_url_, {"devices", device_id_, subpath}, params); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 193 | std::string DeviceRegistrationInfo::GetOAuthURL( |
| 194 | const std::string& subpath, |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 195 | const chromeos::data_encoding::WebParamList& params) const { |
Alex Vakulenko | bda220a | 2014-04-18 15:25:44 -0700 | [diff] [blame] | 196 | return BuildURL(oauth_url_, {subpath}, params); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 199 | std::string DeviceRegistrationInfo::GetDeviceId(chromeos::ErrorPtr* error) { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 200 | return CheckRegistration(error) ? device_id_ : std::string(); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | bool DeviceRegistrationInfo::Load() { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 204 | auto value = storage_->Load(); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 205 | const base::DictionaryValue* dict = nullptr; |
| 206 | if (!value || !value->GetAsDictionary(&dict)) |
| 207 | return false; |
| 208 | |
| 209 | // Get the values into temp variables first to make sure we can get |
| 210 | // all the data correctly before changing the state of this object. |
| 211 | std::string client_id; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 212 | if (!dict->GetString(storage_keys::kClientId, &client_id)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 213 | return false; |
| 214 | std::string client_secret; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 215 | if (!dict->GetString(storage_keys::kClientSecret, &client_secret)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 216 | return false; |
| 217 | std::string api_key; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 218 | if (!dict->GetString(storage_keys::kApiKey, &api_key)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 219 | return false; |
| 220 | std::string refresh_token; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 221 | if (!dict->GetString(storage_keys::kRefreshToken, &refresh_token)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 222 | return false; |
| 223 | std::string device_id; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 224 | if (!dict->GetString(storage_keys::kDeviceId, &device_id)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 225 | return false; |
| 226 | std::string oauth_url; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 227 | if (!dict->GetString(storage_keys::kOAuthURL, &oauth_url)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 228 | return false; |
| 229 | std::string service_url; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 230 | if (!dict->GetString(storage_keys::kServiceURL, &service_url)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 231 | return false; |
| 232 | std::string device_robot_account; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 233 | if (!dict->GetString(storage_keys::kRobotAccount, &device_robot_account)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 234 | return false; |
| 235 | |
| 236 | client_id_ = client_id; |
| 237 | client_secret_ = client_secret; |
| 238 | api_key_ = api_key; |
| 239 | refresh_token_ = refresh_token; |
| 240 | device_id_ = device_id; |
| 241 | oauth_url_ = oauth_url; |
| 242 | service_url_ = service_url; |
| 243 | device_robot_account_ = device_robot_account; |
| 244 | return true; |
| 245 | } |
| 246 | |
| 247 | bool DeviceRegistrationInfo::Save() const { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 248 | base::DictionaryValue dict; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 249 | dict.SetString(storage_keys::kClientId, client_id_); |
| 250 | dict.SetString(storage_keys::kClientSecret, client_secret_); |
| 251 | dict.SetString(storage_keys::kApiKey, api_key_); |
| 252 | dict.SetString(storage_keys::kRefreshToken, refresh_token_); |
| 253 | dict.SetString(storage_keys::kDeviceId, device_id_); |
| 254 | dict.SetString(storage_keys::kOAuthURL, oauth_url_); |
| 255 | dict.SetString(storage_keys::kServiceURL, service_url_); |
| 256 | dict.SetString(storage_keys::kRobotAccount, device_robot_account_); |
| 257 | return storage_->Save(&dict); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 260 | bool DeviceRegistrationInfo::CheckRegistration(chromeos::ErrorPtr* error) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 261 | LOG(INFO) << "Checking device registration record."; |
| 262 | if (refresh_token_.empty() || |
| 263 | device_id_.empty() || |
| 264 | device_robot_account_.empty()) { |
| 265 | LOG(INFO) << "No valid device registration record found."; |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 266 | chromeos::Error::AddTo(error, FROM_HERE, kErrorDomainGCD, |
| 267 | "device_not_registered", |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 268 | "No valid device registration record found"); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 269 | return false; |
| 270 | } |
| 271 | |
| 272 | LOG(INFO) << "Device registration record found."; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 273 | return ValidateAndRefreshAccessToken(error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 276 | bool DeviceRegistrationInfo::ValidateAndRefreshAccessToken( |
| 277 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 278 | LOG(INFO) << "Checking access token expiration."; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 279 | if (!access_token_.empty() && |
| 280 | !access_token_expiration_.is_null() && |
| 281 | access_token_expiration_ > base::Time::Now()) { |
| 282 | LOG(INFO) << "Access token is still valid."; |
| 283 | return true; |
| 284 | } |
| 285 | |
Alex Vakulenko | 89dde2f | 2015-01-07 12:05:12 -0800 | [diff] [blame] | 286 | auto response = chromeos::http::PostFormDataAndBlock(GetOAuthURL("token"), { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 287 | {"refresh_token", refresh_token_}, |
| 288 | {"client_id", client_id_}, |
| 289 | {"client_secret", client_secret_}, |
| 290 | {"grant_type", "refresh_token"}, |
Alex Vakulenko | 89dde2f | 2015-01-07 12:05:12 -0800 | [diff] [blame] | 291 | }, {}, transport_, error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 292 | if (!response) |
| 293 | return false; |
| 294 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 295 | auto json = ParseOAuthResponse(response.get(), error); |
| 296 | if (!json) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 297 | return false; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 298 | |
| 299 | int expires_in = 0; |
| 300 | if (!json->GetString("access_token", &access_token_) || |
| 301 | !json->GetInteger("expires_in", &expires_in) || |
| 302 | access_token_.empty() || |
| 303 | expires_in <= 0) { |
| 304 | LOG(ERROR) << "Access token unavailable."; |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 305 | chromeos::Error::AddTo(error, FROM_HERE, kErrorDomainOAuth2, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 306 | "unexpected_server_response", |
| 307 | "Access token unavailable"); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 308 | return false; |
| 309 | } |
| 310 | |
| 311 | access_token_expiration_ = base::Time::Now() + |
| 312 | base::TimeDelta::FromSeconds(expires_in); |
| 313 | |
| 314 | LOG(INFO) << "Access token is refreshed for additional " << expires_in |
| 315 | << " seconds."; |
| 316 | return true; |
| 317 | } |
| 318 | |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 319 | std::unique_ptr<base::DictionaryValue> |
| 320 | DeviceRegistrationInfo::BuildDeviceResource(chromeos::ErrorPtr* error) { |
| 321 | std::unique_ptr<base::DictionaryValue> commands = |
| 322 | command_manager_->GetCommandDictionary().GetCommandsAsJson(true, error); |
| 323 | if (!commands) |
| 324 | return nullptr; |
| 325 | |
| 326 | std::unique_ptr<base::DictionaryValue> state = |
| 327 | state_manager_->GetStateValuesAsJson(error); |
| 328 | if (!state) |
| 329 | return nullptr; |
| 330 | |
| 331 | std::unique_ptr<base::DictionaryValue> resource{new base::DictionaryValue}; |
| 332 | if (!device_id_.empty()) |
| 333 | resource->SetString("id", device_id_); |
| 334 | resource->SetString("deviceKind", device_kind_); |
Anton Muhin | 9e19cdd | 2014-10-20 13:55:04 +0400 | [diff] [blame] | 335 | resource->SetString("name", name_); |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 336 | if (!display_name_.empty()) |
| 337 | resource->SetString("displayName", display_name_); |
Vitaly Buka | d3eb19c | 2014-11-21 13:39:43 -0800 | [diff] [blame] | 338 | if (!description_.empty()) |
| 339 | resource->SetString("description", description_); |
| 340 | if (!location_.empty()) |
| 341 | resource->SetString("location", location_); |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 342 | resource->SetString("channel.supportedType", "xmpp"); |
| 343 | resource->Set("commandDefs", commands.release()); |
| 344 | resource->Set("state", state.release()); |
| 345 | |
| 346 | return resource; |
| 347 | } |
| 348 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 349 | std::unique_ptr<base::Value> DeviceRegistrationInfo::GetDeviceInfo( |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 350 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 351 | if (!CheckRegistration(error)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 352 | return std::unique_ptr<base::Value>(); |
| 353 | |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 354 | // TODO(antonm): Switch to DoCloudRequest later. |
Alex Vakulenko | 89dde2f | 2015-01-07 12:05:12 -0800 | [diff] [blame] | 355 | auto response = chromeos::http::GetAndBlock( |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 356 | GetDeviceURL(), {GetAuthorizationHeader()}, transport_, error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 357 | int status_code = 0; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 358 | std::unique_ptr<base::DictionaryValue> json = |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 359 | chromeos::http::ParseJsonResponse(response.get(), &status_code, error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 360 | if (json) { |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 361 | if (status_code >= chromeos::http::status_code::BadRequest) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 362 | LOG(WARNING) << "Failed to retrieve the device info. Response code = " |
| 363 | << status_code; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 364 | ParseGCDError(json.get(), error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 365 | return std::unique_ptr<base::Value>(); |
| 366 | } |
| 367 | } |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 368 | return std::unique_ptr<base::Value>(json.release()); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | bool CheckParam(const std::string& param_name, |
| 372 | const std::string& param_value, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 373 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 374 | if (!param_value.empty()) |
| 375 | return true; |
| 376 | |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 377 | chromeos::Error::AddToPrintf(error, FROM_HERE, kErrorDomainBuffet, |
| 378 | "missing_parameter", |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 379 | "Parameter %s not specified", |
| 380 | param_name.c_str()); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 381 | return false; |
| 382 | } |
| 383 | |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 384 | std::string DeviceRegistrationInfo::RegisterDevice( |
Alex Vakulenko | a904434 | 2014-08-23 19:31:27 -0700 | [diff] [blame] | 385 | const std::map<std::string, std::string>& params, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 386 | chromeos::ErrorPtr* error) { |
Anton Muhin | a480314 | 2014-09-24 19:30:45 +0400 | [diff] [blame] | 387 | GetParamValue(params, "ticket_id", &ticket_id_); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 388 | GetParamValue(params, storage_keys::kClientId, &client_id_); |
| 389 | GetParamValue(params, storage_keys::kClientSecret, &client_secret_); |
| 390 | GetParamValue(params, storage_keys::kApiKey, &api_key_); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 391 | GetParamValue(params, storage_keys::kDeviceKind, &device_kind_); |
Anton Muhin | 9e19cdd | 2014-10-20 13:55:04 +0400 | [diff] [blame] | 392 | GetParamValue(params, storage_keys::kName, &name_); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 393 | GetParamValue(params, storage_keys::kDisplayName, &display_name_); |
Vitaly Buka | d3eb19c | 2014-11-21 13:39:43 -0800 | [diff] [blame] | 394 | GetParamValue(params, storage_keys::kDescription, &description_); |
| 395 | GetParamValue(params, storage_keys::kLocation, &location_); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 396 | GetParamValue(params, storage_keys::kOAuthURL, &oauth_url_); |
| 397 | GetParamValue(params, storage_keys::kServiceURL, &service_url_); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 398 | |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 399 | std::unique_ptr<base::DictionaryValue> device_draft = |
| 400 | BuildDeviceResource(error); |
| 401 | if (!device_draft) |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 402 | return std::string(); |
| 403 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 404 | base::DictionaryValue req_json; |
Anton Muhin | a480314 | 2014-09-24 19:30:45 +0400 | [diff] [blame] | 405 | req_json.SetString("id", ticket_id_); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 406 | req_json.SetString("oauthClientId", client_id_); |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 407 | req_json.Set("deviceDraft", device_draft.release()); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 408 | |
Anton Muhin | a480314 | 2014-09-24 19:30:45 +0400 | [diff] [blame] | 409 | auto url = GetServiceURL("registrationTickets/" + ticket_id_, |
| 410 | {{"key", api_key_}}); |
Anton Muhin | 532ff7e | 2014-09-29 23:21:21 +0400 | [diff] [blame] | 411 | std::unique_ptr<chromeos::http::Response> response = |
Alex Vakulenko | 89dde2f | 2015-01-07 12:05:12 -0800 | [diff] [blame] | 412 | chromeos::http::PatchJsonAndBlock(url, &req_json, {}, transport_, error); |
Anton Muhin | 532ff7e | 2014-09-29 23:21:21 +0400 | [diff] [blame] | 413 | auto json_resp = chromeos::http::ParseJsonResponse(response.get(), nullptr, |
| 414 | error); |
| 415 | if (!json_resp) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 416 | return std::string(); |
Anton Muhin | 532ff7e | 2014-09-29 23:21:21 +0400 | [diff] [blame] | 417 | if (!response->IsSuccessful()) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 418 | return std::string(); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 419 | |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 420 | url = GetServiceURL("registrationTickets/" + ticket_id_ + |
| 421 | "/finalize?key=" + api_key_); |
Alex Vakulenko | 89dde2f | 2015-01-07 12:05:12 -0800 | [diff] [blame] | 422 | response = chromeos::http::SendRequestWithNoDataAndBlock( |
| 423 | chromeos::http::request_type::kPost, url, {}, transport_, error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 424 | if (!response) |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 425 | return std::string(); |
| 426 | json_resp = chromeos::http::ParseJsonResponse(response.get(), nullptr, error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 427 | if (!json_resp) |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 428 | return std::string(); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 429 | if (!response->IsSuccessful()) { |
| 430 | ParseGCDError(json_resp.get(), error); |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 431 | return std::string(); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 432 | } |
Anton Muhin | 532ff7e | 2014-09-29 23:21:21 +0400 | [diff] [blame] | 433 | |
| 434 | std::string auth_code; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 435 | if (!json_resp->GetString("robotAccountEmail", &device_robot_account_) || |
| 436 | !json_resp->GetString("robotAccountAuthorizationCode", &auth_code) || |
| 437 | !json_resp->GetString("deviceDraft.id", &device_id_)) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 438 | chromeos::Error::AddTo(error, FROM_HERE, kErrorDomainGCD, |
| 439 | "unexpected_response", |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 440 | "Device account missing in response"); |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 441 | return std::string(); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // Now get access_token and refresh_token |
Alex Vakulenko | 89dde2f | 2015-01-07 12:05:12 -0800 | [diff] [blame] | 445 | response = chromeos::http::PostFormDataAndBlock(GetOAuthURL("token"), { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 446 | {"code", auth_code}, |
| 447 | {"client_id", client_id_}, |
| 448 | {"client_secret", client_secret_}, |
| 449 | {"redirect_uri", "oob"}, |
| 450 | {"scope", "https://www.googleapis.com/auth/clouddevices"}, |
| 451 | {"grant_type", "authorization_code"} |
Alex Vakulenko | 89dde2f | 2015-01-07 12:05:12 -0800 | [diff] [blame] | 452 | }, {}, transport_, error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 453 | if (!response) |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 454 | return std::string(); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 455 | |
| 456 | json_resp = ParseOAuthResponse(response.get(), error); |
| 457 | int expires_in = 0; |
| 458 | if (!json_resp || |
| 459 | !json_resp->GetString("access_token", &access_token_) || |
| 460 | !json_resp->GetString("refresh_token", &refresh_token_) || |
| 461 | !json_resp->GetInteger("expires_in", &expires_in) || |
| 462 | access_token_.empty() || |
| 463 | refresh_token_.empty() || |
| 464 | expires_in <= 0) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 465 | chromeos::Error::AddTo(error, FROM_HERE, |
| 466 | kErrorDomainGCD, "unexpected_response", |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 467 | "Device access_token missing in response"); |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 468 | return std::string(); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | access_token_expiration_ = base::Time::Now() + |
| 472 | base::TimeDelta::FromSeconds(expires_in); |
| 473 | |
| 474 | Save(); |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 475 | return device_id_; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 478 | namespace { |
| 479 | |
| 480 | template <class T> |
| 481 | void PostToCallback(base::Callback<void(const T&)> callback, |
| 482 | std::unique_ptr<T> value) { |
| 483 | auto cb = [callback] (T* result) { |
| 484 | callback.Run(*result); |
| 485 | }; |
| 486 | base::MessageLoop::current()->PostTask( |
| 487 | FROM_HERE, base::Bind(cb, base::Owned(value.release()))); |
| 488 | } |
| 489 | |
Anton Muhin | 0ae1fba | 2014-10-22 19:30:40 +0400 | [diff] [blame] | 490 | void PostRepeatingTask(const tracked_objects::Location& from_here, |
| 491 | base::Closure task, |
| 492 | base::TimeDelta delay) { |
| 493 | task.Run(); |
| 494 | base::MessageLoop::current()->PostDelayedTask( |
| 495 | from_here, base::Bind(&PostRepeatingTask, from_here, task, delay), delay); |
| 496 | } |
| 497 | |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 498 | using ResponsePtr = scoped_ptr<chromeos::http::Response>; |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 499 | |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 500 | void SendRequestWithRetries( |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 501 | const std::string& method, |
| 502 | const std::string& url, |
| 503 | const std::string& data, |
| 504 | const std::string& mime_type, |
| 505 | const chromeos::http::HeaderList& headers, |
| 506 | std::shared_ptr<chromeos::http::Transport> transport, |
| 507 | int num_retries, |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 508 | const chromeos::http::SuccessCallback& success_callback, |
| 509 | const chromeos::http::ErrorCallback& error_callback) { |
| 510 | auto on_failure = |
| 511 | [method, url, data, mime_type, headers, transport, num_retries, |
Alex Vakulenko | 6401d01 | 2015-01-16 07:40:43 -0800 | [diff] [blame] | 512 | success_callback, error_callback](int request_id, |
| 513 | const chromeos::Error* error) { |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 514 | if (num_retries > 0) { |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 515 | SendRequestWithRetries(method, url, data, mime_type, |
| 516 | headers, transport, num_retries - 1, |
| 517 | success_callback, error_callback); |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 518 | } else { |
Alex Vakulenko | 6401d01 | 2015-01-16 07:40:43 -0800 | [diff] [blame] | 519 | error_callback.Run(request_id, error); |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 520 | } |
| 521 | }; |
| 522 | |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 523 | auto on_success = |
Alex Vakulenko | 6401d01 | 2015-01-16 07:40:43 -0800 | [diff] [blame] | 524 | [on_failure, success_callback, error_callback](int request_id, |
| 525 | ResponsePtr response) { |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 526 | int status_code = response->GetStatusCode(); |
| 527 | if (status_code >= chromeos::http::status_code::Continue && |
| 528 | status_code < chromeos::http::status_code::BadRequest) { |
Alex Vakulenko | 6401d01 | 2015-01-16 07:40:43 -0800 | [diff] [blame] | 529 | success_callback.Run(request_id, response.Pass()); |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 530 | return; |
| 531 | } |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 532 | |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 533 | // TODO(antonm): Should add some useful information to error. |
| 534 | LOG(WARNING) << "Request failed. Response code = " << status_code; |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 535 | |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 536 | chromeos::ErrorPtr error; |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 537 | chromeos::Error::AddTo(&error, FROM_HERE, chromeos::errors::http::kDomain, |
Anton Muhin | 233d2ee | 2014-10-22 15:16:24 +0400 | [diff] [blame] | 538 | std::to_string(status_code), |
| 539 | response->GetStatusText()); |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 540 | if (status_code >= chromeos::http::status_code::InternalServerError && |
| 541 | status_code < 600) { |
| 542 | // Request was valid, but server failed, retry. |
| 543 | // TODO(antonm): Implement exponential backoff. |
| 544 | // TODO(antonm): Reconsider status codes, maybe only some require |
| 545 | // retry. |
| 546 | // TODO(antonm): Support Retry-After header. |
Alex Vakulenko | 6401d01 | 2015-01-16 07:40:43 -0800 | [diff] [blame] | 547 | on_failure(request_id, error.get()); |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 548 | } else { |
Alex Vakulenko | 6401d01 | 2015-01-16 07:40:43 -0800 | [diff] [blame] | 549 | error_callback.Run(request_id, error.get()); |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 550 | } |
| 551 | }; |
| 552 | |
| 553 | chromeos::http::SendRequest(method, url, data.c_str(), data.size(), |
| 554 | mime_type, headers, transport, |
| 555 | base::Bind(on_success), |
| 556 | base::Bind(on_failure)); |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | } // namespace |
| 560 | |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 561 | void DeviceRegistrationInfo::DoCloudRequest( |
Anton Muhin | 233d2ee | 2014-10-22 15:16:24 +0400 | [diff] [blame] | 562 | const std::string& method, |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 563 | const std::string& url, |
| 564 | const base::DictionaryValue* body, |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 565 | const CloudRequestCallback& success_callback, |
| 566 | const CloudRequestErrorCallback& error_callback) { |
| 567 | // TODO(antonm): Add reauthorization on access token expiration (do not |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 568 | // forget about 5xx when fetching new access token). |
| 569 | // TODO(antonm): Add support for device removal. |
| 570 | |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 571 | std::string data; |
| 572 | if (body) |
| 573 | base::JSONWriter::Write(body, &data); |
| 574 | |
| 575 | const std::string mime_type{chromeos::mime::AppendParameter( |
| 576 | chromeos::mime::application::kJson, |
| 577 | chromeos::mime::parameters::kCharset, |
| 578 | "utf-8")}; |
| 579 | |
Alex Vakulenko | 6401d01 | 2015-01-16 07:40:43 -0800 | [diff] [blame] | 580 | auto request_cb = |
| 581 | [success_callback, error_callback](int request_id, ResponsePtr response) { |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 582 | chromeos::ErrorPtr error; |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 583 | |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 584 | std::unique_ptr<base::DictionaryValue> json_resp{ |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 585 | chromeos::http::ParseJsonResponse(response.get(), nullptr, &error)}; |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 586 | if (!json_resp) { |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 587 | error_callback.Run(error.get()); |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 588 | return; |
| 589 | } |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 590 | |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 591 | success_callback.Run(*json_resp); |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 592 | }; |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 593 | |
Alex Vakulenko | 6401d01 | 2015-01-16 07:40:43 -0800 | [diff] [blame] | 594 | auto error_cb = |
| 595 | [error_callback](int request_id, const chromeos::Error* error) { |
| 596 | error_callback.Run(error); |
| 597 | }; |
| 598 | |
Anton Muhin | 233d2ee | 2014-10-22 15:16:24 +0400 | [diff] [blame] | 599 | auto transport = transport_; |
Alex Vakulenko | 6401d01 | 2015-01-16 07:40:43 -0800 | [diff] [blame] | 600 | auto error_callackback_with_reauthorization = |
| 601 | base::Bind([method, url, data, mime_type, transport, request_cb, error_cb] |
| 602 | (DeviceRegistrationInfo* self, |
| 603 | int request_id, |
| 604 | const chromeos::Error* error) { |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 605 | if (error->HasError(chromeos::errors::http::kDomain, |
| 606 | std::to_string(chromeos::http::status_code::Denied))) { |
Anton Muhin | 233d2ee | 2014-10-22 15:16:24 +0400 | [diff] [blame] | 607 | chromeos::ErrorPtr reauthorization_error; |
| 608 | if (!self->ValidateAndRefreshAccessToken(&reauthorization_error)) { |
| 609 | // TODO(antonm): Check if the device has been actually removed. |
Alex Vakulenko | 6401d01 | 2015-01-16 07:40:43 -0800 | [diff] [blame] | 610 | error_cb(request_id, reauthorization_error.get()); |
Anton Muhin | 233d2ee | 2014-10-22 15:16:24 +0400 | [diff] [blame] | 611 | return; |
| 612 | } |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 613 | SendRequestWithRetries(method, url, |
| 614 | data, mime_type, |
| 615 | {self->GetAuthorizationHeader()}, |
| 616 | transport, |
| 617 | 7, |
Alex Vakulenko | 6401d01 | 2015-01-16 07:40:43 -0800 | [diff] [blame] | 618 | base::Bind(request_cb), base::Bind(error_cb)); |
Anton Muhin | 233d2ee | 2014-10-22 15:16:24 +0400 | [diff] [blame] | 619 | } else { |
Alex Vakulenko | 6401d01 | 2015-01-16 07:40:43 -0800 | [diff] [blame] | 620 | error_cb(request_id, error); |
Anton Muhin | 233d2ee | 2014-10-22 15:16:24 +0400 | [diff] [blame] | 621 | } |
| 622 | }, base::Unretained(this)); |
| 623 | |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 624 | SendRequestWithRetries(method, url, |
| 625 | data, mime_type, |
| 626 | {GetAuthorizationHeader()}, |
| 627 | transport, |
| 628 | 7, |
| 629 | base::Bind(request_cb), |
| 630 | error_callackback_with_reauthorization); |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 631 | } |
| 632 | |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 633 | void DeviceRegistrationInfo::StartDevice(chromeos::ErrorPtr* error) { |
| 634 | if (!CheckRegistration(error)) |
| 635 | return; |
| 636 | |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 637 | base::Bind( |
| 638 | &DeviceRegistrationInfo::UpdateDeviceResource, |
| 639 | base::Unretained(this), |
| 640 | base::Bind( |
| 641 | &DeviceRegistrationInfo::FetchCommands, |
| 642 | base::Unretained(this), |
| 643 | base::Bind( |
| 644 | &DeviceRegistrationInfo::AbortLimboCommands, |
| 645 | base::Unretained(this), |
| 646 | base::Bind( |
| 647 | &DeviceRegistrationInfo::PeriodicallyPollCommands, |
| 648 | base::Unretained(this))))).Run(); |
| 649 | } |
| 650 | |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 651 | void DeviceRegistrationInfo::UpdateCommand( |
| 652 | const std::string& command_id, |
| 653 | const base::DictionaryValue& command_patch) { |
| 654 | DoCloudRequest( |
| 655 | chromeos::http::request_type::kPatch, |
| 656 | GetServiceURL("commands/" + command_id), |
| 657 | &command_patch, |
| 658 | base::Bind(&IgnoreCloudResult), base::Bind(&IgnoreCloudError)); |
| 659 | } |
| 660 | |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 661 | void DeviceRegistrationInfo::UpdateDeviceResource(base::Closure callback) { |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 662 | std::unique_ptr<base::DictionaryValue> device_resource = |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 663 | BuildDeviceResource(nullptr); |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 664 | if (!device_resource) |
| 665 | return; |
| 666 | |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 667 | DoCloudRequest( |
| 668 | chromeos::http::request_type::kPut, |
| 669 | GetDeviceURL(), |
| 670 | device_resource.get(), |
| 671 | base::Bind([callback](const base::DictionaryValue&){ |
| 672 | base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 673 | }), |
| 674 | // TODO(antonm): Failure to update device resource probably deserves |
| 675 | // some additional actions. |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 676 | base::Bind(&IgnoreCloudError)); |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 677 | } |
Anton Muhin | a34f0d9 | 2014-10-03 21:09:40 +0400 | [diff] [blame] | 678 | |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 679 | void DeviceRegistrationInfo::FetchCommands( |
| 680 | base::Callback<void(const base::ListValue&)> callback) { |
| 681 | DoCloudRequest( |
| 682 | chromeos::http::request_type::kGet, |
| 683 | GetServiceURL("commands/queue", {{"deviceId", device_id_}}), |
| 684 | nullptr, |
| 685 | base::Bind([callback](const base::DictionaryValue& json) { |
| 686 | const base::ListValue* commands{nullptr}; |
| 687 | if (!json.GetList("commands", &commands)) { |
| 688 | VLOG(1) << "No commands in the response."; |
| 689 | } |
| 690 | const base::ListValue empty; |
| 691 | callback.Run(commands ? *commands : empty); |
| 692 | }), |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 693 | base::Bind(&IgnoreCloudError)); |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 694 | } |
Anton Muhin | a34f0d9 | 2014-10-03 21:09:40 +0400 | [diff] [blame] | 695 | |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 696 | void DeviceRegistrationInfo::AbortLimboCommands( |
| 697 | base::Closure callback, const base::ListValue& commands) { |
| 698 | const size_t size{commands.GetSize()}; |
| 699 | for (size_t i = 0; i < size; ++i) { |
| 700 | const base::DictionaryValue* command{nullptr}; |
| 701 | if (!commands.GetDictionary(i, &command)) { |
| 702 | LOG(WARNING) << "No command resource at " << i; |
| 703 | continue; |
Anton Muhin | a34f0d9 | 2014-10-03 21:09:40 +0400 | [diff] [blame] | 704 | } |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 705 | std::string command_state; |
| 706 | if (!command->GetString("state", &command_state)) { |
| 707 | LOG(WARNING) << "Command with no state at " << i; |
| 708 | continue; |
| 709 | } |
| 710 | if (command_state != "error" && |
| 711 | command_state != "inProgress" && |
| 712 | command_state != "paused") { |
| 713 | // It's not a limbo command, ignore. |
| 714 | continue; |
| 715 | } |
| 716 | std::string command_id; |
| 717 | if (!command->GetString("id", &command_id)) { |
| 718 | LOG(WARNING) << "Command with no ID at " << i; |
| 719 | continue; |
| 720 | } |
Anton Muhin | 6d2569e | 2014-10-30 12:32:27 +0400 | [diff] [blame] | 721 | |
| 722 | std::unique_ptr<base::DictionaryValue> command_copy{command->DeepCopy()}; |
| 723 | command_copy->SetString("state", "aborted"); |
| 724 | DoCloudRequest( |
| 725 | chromeos::http::request_type::kPut, |
| 726 | GetServiceURL("commands/" + command_id), |
| 727 | command_copy.get(), |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 728 | base::Bind(&IgnoreCloudResult), base::Bind(&IgnoreCloudError)); |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 729 | } |
Anton Muhin | 0ae1fba | 2014-10-22 19:30:40 +0400 | [diff] [blame] | 730 | |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 731 | base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 732 | } |
| 733 | |
| 734 | void DeviceRegistrationInfo::PeriodicallyPollCommands() { |
Anton Muhin | d07e206 | 2014-10-27 10:53:29 +0400 | [diff] [blame] | 735 | VLOG(1) << "Poll commands"; |
| 736 | PostRepeatingTask( |
| 737 | FROM_HERE, |
| 738 | base::Bind( |
| 739 | &DeviceRegistrationInfo::FetchCommands, |
| 740 | base::Unretained(this), |
| 741 | base::Bind(&DeviceRegistrationInfo::PublishCommands, |
| 742 | base::Unretained(this))), |
| 743 | base::TimeDelta::FromSeconds(7)); |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 744 | // TODO(antonm): Use better trigger: when StateManager registers new updates, |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 745 | // it should call closure which will post a task, probably with some |
| 746 | // throttling, to publish state updates. |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 747 | PostRepeatingTask( |
| 748 | FROM_HERE, |
| 749 | base::Bind(&DeviceRegistrationInfo::PublishStateUpdates, |
| 750 | base::Unretained(this)), |
| 751 | base::TimeDelta::FromSeconds(7)); |
Anton Muhin | d07e206 | 2014-10-27 10:53:29 +0400 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | void DeviceRegistrationInfo::PublishCommands(const base::ListValue& commands) { |
| 755 | const CommandDictionary& command_dictionary = |
| 756 | command_manager_->GetCommandDictionary(); |
| 757 | |
| 758 | const size_t size{commands.GetSize()}; |
| 759 | for (size_t i = 0; i < size; ++i) { |
| 760 | const base::DictionaryValue* command{nullptr}; |
| 761 | if (!commands.GetDictionary(i, &command)) { |
| 762 | LOG(WARNING) << "No command resource at " << i; |
| 763 | continue; |
| 764 | } |
| 765 | |
| 766 | std::unique_ptr<CommandInstance> command_instance = |
| 767 | CommandInstance::FromJson(command, command_dictionary, nullptr); |
| 768 | if (!command_instance) { |
| 769 | LOG(WARNING) << "Failed to parse a command"; |
| 770 | continue; |
| 771 | } |
| 772 | |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 773 | // TODO(antonm): Properly process cancellation of commands. |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 774 | if (!command_manager_->FindCommand(command_instance->GetID())) { |
| 775 | std::unique_ptr<CommandProxyInterface> cloud_proxy{ |
| 776 | new CloudCommandProxy(command_instance.get(), this)}; |
| 777 | command_instance->AddProxy(std::move(cloud_proxy)); |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 778 | command_manager_->AddCommand(std::move(command_instance)); |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 779 | } |
Anton Muhin | d07e206 | 2014-10-27 10:53:29 +0400 | [diff] [blame] | 780 | } |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 781 | } |
| 782 | |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 783 | void DeviceRegistrationInfo::PublishStateUpdates() { |
| 784 | VLOG(1) << "PublishStateUpdates"; |
| 785 | const std::vector<StateChange> state_changes{ |
| 786 | state_manager_->GetAndClearRecordedStateChanges()}; |
| 787 | if (state_changes.empty()) |
| 788 | return; |
| 789 | |
| 790 | std::unique_ptr<base::ListValue> patches{new base::ListValue}; |
| 791 | for (const auto& state_change : state_changes) { |
| 792 | std::unique_ptr<base::DictionaryValue> patch{new base::DictionaryValue}; |
Anton Muhin | 76933fd | 2014-11-21 21:25:18 +0400 | [diff] [blame] | 793 | patch->SetString("timeMs", |
| 794 | std::to_string(state_change.timestamp.ToJavaTime())); |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 795 | |
| 796 | std::unique_ptr<base::DictionaryValue> changes{new base::DictionaryValue}; |
| 797 | for (const auto& pair : state_change.changed_properties) { |
| 798 | auto value = pair.second->ToJson(nullptr); |
| 799 | if (!value) { |
| 800 | return; |
| 801 | } |
Alex Vakulenko | 61ad4db | 2015-01-20 10:50:04 -0800 | [diff] [blame] | 802 | // The key in |pair.first| is the full property name in format |
| 803 | // "package.property_name", so must use DictionaryValue::Set() instead of |
| 804 | // DictionaryValue::SetWithoutPathExpansion to recreate the JSON |
| 805 | // property tree properly. |
| 806 | changes->Set(pair.first, value.release()); |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 807 | } |
| 808 | patch->Set("patch", changes.release()); |
| 809 | |
| 810 | patches->Append(patch.release()); |
| 811 | } |
| 812 | |
| 813 | base::DictionaryValue body; |
Anton Muhin | 76933fd | 2014-11-21 21:25:18 +0400 | [diff] [blame] | 814 | body.SetString("requestTimeMs", |
| 815 | std::to_string(base::Time::Now().ToJavaTime())); |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 816 | body.Set("patches", patches.release()); |
| 817 | |
| 818 | DoCloudRequest( |
| 819 | chromeos::http::request_type::kPost, |
| 820 | GetDeviceURL("patchState"), |
| 821 | &body, |
| 822 | base::Bind(&IgnoreCloudResult), base::Bind(&IgnoreCloudError)); |
| 823 | } |
| 824 | |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 825 | void DeviceRegistrationInfo::GetParamValue( |
| 826 | const std::map<std::string, std::string>& params, |
| 827 | const std::string& param_name, |
| 828 | std::string* param_value) { |
| 829 | auto p = params.find(param_name); |
| 830 | if (p != params.end()) { |
| 831 | *param_value = p->second; |
| 832 | return; |
| 833 | } |
| 834 | |
| 835 | bool present = config_store_->GetString(param_name, param_value); |
| 836 | CHECK(present) << "No default for parameter " << param_name; |
| 837 | } |
| 838 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 839 | } // namespace buffet |