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