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