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 | |
Christopher Wiley | cd41966 | 2015-02-06 17:51:43 -0800 | [diff] [blame] | 11 | #include <base/bind.h> |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 12 | #include <base/json/json_writer.h> |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 13 | #include <base/message_loop/message_loop.h> |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 14 | #include <base/values.h> |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 15 | #include <chromeos/bind_lambda.h> |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 16 | #include <chromeos/data_encoding.h> |
Anton Muhin | 233d2ee | 2014-10-22 15:16:24 +0400 | [diff] [blame] | 17 | #include <chromeos/errors/error_codes.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 18 | #include <chromeos/http/http_utils.h> |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 19 | #include <chromeos/key_value_store.h> |
Alex Vakulenko | 3aeea1c | 2014-08-20 16:33:12 -0700 | [diff] [blame] | 20 | #include <chromeos/mime_utils.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 21 | #include <chromeos/strings/string_utils.h> |
Alex Vakulenko | bd5b544 | 2014-08-20 16:16:34 -0700 | [diff] [blame] | 22 | #include <chromeos/url_utils.h> |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 23 | |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 24 | #include "buffet/commands/cloud_command_proxy.h" |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 25 | #include "buffet/commands/command_definition.h" |
| 26 | #include "buffet/commands/command_manager.h" |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 27 | #include "buffet/device_registration_storage_keys.h" |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 28 | #include "buffet/states/state_manager.h" |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 29 | #include "buffet/utils.h" |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 30 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 31 | const char buffet::kErrorDomainOAuth2[] = "oauth2"; |
| 32 | const char buffet::kErrorDomainGCD[] = "gcd"; |
| 33 | const char buffet::kErrorDomainGCDServer[] = "gcd_server"; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 34 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 35 | namespace buffet { |
| 36 | namespace storage_keys { |
| 37 | |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 38 | // Persistent keys |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 39 | const char kRefreshToken[] = "refresh_token"; |
| 40 | const char kDeviceId[] = "device_id"; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 41 | const char kRobotAccount[] = "robot_account"; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 42 | const char kDisplayName[] = "display_name"; |
Vitaly Buka | d3eb19c | 2014-11-21 13:39:43 -0800 | [diff] [blame] | 43 | const char kDescription[] = "description"; |
| 44 | const char kLocation[] = "location"; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 45 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 46 | } // namespace storage_keys |
| 47 | } // namespace buffet |
| 48 | |
| 49 | namespace { |
| 50 | |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 51 | const int kMaxStartDeviceRetryDelayMinutes{1}; |
| 52 | const int64_t kMinStartDeviceRetryDelaySeconds{5}; |
| 53 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 54 | std::pair<std::string, std::string> BuildAuthHeader( |
| 55 | const std::string& access_token_type, |
| 56 | const std::string& access_token) { |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 57 | std::string authorization = |
Vitaly Buka | db770e7 | 2015-03-10 19:33:33 -0700 | [diff] [blame] | 58 | chromeos::string_utils::Join(" ", access_token_type, access_token); |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 59 | return {chromeos::http::request_header::kAuthorization, authorization}; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 62 | inline void SetUnexpectedError(chromeos::ErrorPtr* error) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 63 | chromeos::Error::AddTo(error, FROM_HERE, buffet::kErrorDomainGCD, |
| 64 | "unexpected_response", "Unexpected GCD error"); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 67 | void ParseGCDError(const base::DictionaryValue* json, |
| 68 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 69 | const base::Value* list_value = nullptr; |
| 70 | const base::ListValue* error_list = nullptr; |
| 71 | if (!json->Get("error.errors", &list_value) || |
| 72 | !list_value->GetAsList(&error_list)) { |
| 73 | SetUnexpectedError(error); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | for (size_t i = 0; i < error_list->GetSize(); i++) { |
| 78 | const base::Value* error_value = nullptr; |
| 79 | const base::DictionaryValue* error_object = nullptr; |
| 80 | if (!error_list->Get(i, &error_value) || |
| 81 | !error_value->GetAsDictionary(&error_object)) { |
| 82 | SetUnexpectedError(error); |
| 83 | continue; |
| 84 | } |
| 85 | std::string error_code, error_message; |
| 86 | if (error_object->GetString("reason", &error_code) && |
| 87 | error_object->GetString("message", &error_message)) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 88 | chromeos::Error::AddTo(error, FROM_HERE, buffet::kErrorDomainGCDServer, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 89 | error_code, error_message); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 90 | } else { |
| 91 | SetUnexpectedError(error); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
Alex Vakulenko | bda220a | 2014-04-18 15:25:44 -0700 | [diff] [blame] | 96 | std::string BuildURL(const std::string& url, |
| 97 | const std::vector<std::string>& subpaths, |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 98 | const chromeos::data_encoding::WebParamList& params) { |
Alex Vakulenko | bd5b544 | 2014-08-20 16:16:34 -0700 | [diff] [blame] | 99 | std::string result = chromeos::url::CombineMultiple(url, subpaths); |
| 100 | return chromeos::url::AppendQueryParams(result, params); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 103 | void IgnoreCloudError(const chromeos::Error*) { |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 104 | } |
| 105 | |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 106 | void IgnoreCloudErrorWithCallback(const base::Closure& cb, |
| 107 | const chromeos::Error*) { |
| 108 | cb.Run(); |
| 109 | } |
| 110 | |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 111 | void IgnoreCloudResult(const base::DictionaryValue&) { |
| 112 | } |
Anton Muhin | 6d2569e | 2014-10-30 12:32:27 +0400 | [diff] [blame] | 113 | |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 114 | void IgnoreCloudResultWithCallback(const base::Closure& cb, |
| 115 | const base::DictionaryValue&) { |
| 116 | cb.Run(); |
| 117 | } |
| 118 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 119 | } // anonymous namespace |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 120 | |
| 121 | namespace buffet { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 122 | |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 123 | DeviceRegistrationInfo::DeviceRegistrationInfo( |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 124 | const std::shared_ptr<CommandManager>& command_manager, |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 125 | const std::shared_ptr<StateManager>& state_manager, |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame^] | 126 | std::unique_ptr<const BuffetConfig> config, |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 127 | const std::shared_ptr<chromeos::http::Transport>& transport, |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 128 | const std::shared_ptr<StorageInterface>& state_store, |
Vitaly Buka | 7ad8ffb | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 129 | const base::Closure& on_status_changed) |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 130 | : transport_{transport}, |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 131 | storage_{state_store}, |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 132 | command_manager_{command_manager}, |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 133 | state_manager_{state_manager}, |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame^] | 134 | config_{std::move(config)}, |
Vitaly Buka | 7ad8ffb | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 135 | on_status_changed_{on_status_changed} { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 136 | } |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 137 | |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 138 | DeviceRegistrationInfo::~DeviceRegistrationInfo() = default; |
| 139 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 140 | std::pair<std::string, std::string> |
| 141 | DeviceRegistrationInfo::GetAuthorizationHeader() const { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 142 | return BuildAuthHeader("Bearer", access_token_); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | std::string DeviceRegistrationInfo::GetServiceURL( |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 146 | const std::string& subpath, |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 147 | const chromeos::data_encoding::WebParamList& params) const { |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame^] | 148 | return BuildURL(config_->service_url(), {subpath}, params); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | std::string DeviceRegistrationInfo::GetDeviceURL( |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 152 | const std::string& subpath, |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 153 | const chromeos::data_encoding::WebParamList& params) const { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 154 | CHECK(!device_id_.empty()) << "Must have a valid device ID"; |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame^] | 155 | return BuildURL(config_->service_url(), |
| 156 | {"devices", device_id_, subpath}, params); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 159 | std::string DeviceRegistrationInfo::GetOAuthURL( |
| 160 | const std::string& subpath, |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 161 | const chromeos::data_encoding::WebParamList& params) const { |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame^] | 162 | return BuildURL(config_->oauth_url(), {subpath}, params); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Vitaly Buka | 620bd7e | 2015-03-16 01:07:01 -0700 | [diff] [blame] | 165 | const std::string& DeviceRegistrationInfo::GetDeviceId() const { |
| 166 | return device_id_; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | bool DeviceRegistrationInfo::Load() { |
Vitaly Buka | 7ad8ffb | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 170 | // Set kInvalidCredentials to trigger on_status_changed_ callback. |
| 171 | registration_status_ = RegistrationStatus::kInvalidCredentials; |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 172 | SetRegistrationStatus(RegistrationStatus::kUnconfigured); |
Vitaly Buka | 7ad8ffb | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 173 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 174 | auto value = storage_->Load(); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 175 | const base::DictionaryValue* dict = nullptr; |
| 176 | if (!value || !value->GetAsDictionary(&dict)) |
| 177 | return false; |
| 178 | |
| 179 | // Get the values into temp variables first to make sure we can get |
| 180 | // all the data correctly before changing the state of this object. |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 181 | std::string refresh_token; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 182 | std::string device_id; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 183 | std::string device_robot_account; |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 184 | std::string display_name; |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 185 | std::string description; |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 186 | std::string location; |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame^] | 187 | if (!dict->GetString(storage_keys::kRefreshToken, &refresh_token) || |
| 188 | !dict->GetString(storage_keys::kDeviceId, &device_id) || |
| 189 | !dict->GetString(storage_keys::kRobotAccount, &device_robot_account) || |
| 190 | !dict->GetString(storage_keys::kDisplayName, &display_name) || |
| 191 | !dict->GetString(storage_keys::kDescription, &description) || |
| 192 | !dict->GetString(storage_keys::kLocation, &location)) { |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 193 | return false; |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame^] | 194 | } |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 195 | refresh_token_ = refresh_token; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 196 | device_robot_account_ = device_robot_account; |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 197 | display_name_ = display_name; |
| 198 | description_ = description; |
| 199 | location_ = location; |
Vitaly Buka | 6522ab6 | 2015-02-19 10:26:31 -0800 | [diff] [blame] | 200 | |
Vitaly Buka | 620bd7e | 2015-03-16 01:07:01 -0700 | [diff] [blame] | 201 | SetDeviceId(device_id); |
| 202 | |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 203 | if (HaveRegistrationCredentials(nullptr)) { |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 204 | // Wait a significant amount of time for local daemons to publish their |
| 205 | // state to Buffet before publishing it to the cloud. |
| 206 | // TODO(wiley) We could do a lot of things here to either expose this |
| 207 | // timeout as a configurable knob or allow local |
| 208 | // daemons to signal that their state is up to date so that |
| 209 | // we need not wait for them. |
| 210 | ScheduleStartDevice(base::TimeDelta::FromSeconds(5)); |
| 211 | } |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 212 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 213 | return true; |
| 214 | } |
| 215 | |
| 216 | bool DeviceRegistrationInfo::Save() const { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 217 | base::DictionaryValue dict; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 218 | dict.SetString(storage_keys::kRefreshToken, refresh_token_); |
| 219 | dict.SetString(storage_keys::kDeviceId, device_id_); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 220 | dict.SetString(storage_keys::kRobotAccount, device_robot_account_); |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 221 | dict.SetString(storage_keys::kDisplayName, display_name_); |
| 222 | dict.SetString(storage_keys::kDescription, description_); |
| 223 | dict.SetString(storage_keys::kLocation, location_); |
| 224 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 225 | return storage_->Save(&dict); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Christopher Wiley | cd41966 | 2015-02-06 17:51:43 -0800 | [diff] [blame] | 228 | void DeviceRegistrationInfo::ScheduleStartDevice(const base::TimeDelta& later) { |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 229 | SetRegistrationStatus(RegistrationStatus::kConnecting); |
Christopher Wiley | cd41966 | 2015-02-06 17:51:43 -0800 | [diff] [blame] | 230 | base::MessageLoop* current = base::MessageLoop::current(); |
| 231 | if (!current) |
| 232 | return; // Assume we're in unittests |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 233 | base::TimeDelta max_delay = |
| 234 | base::TimeDelta::FromMinutes(kMaxStartDeviceRetryDelayMinutes); |
| 235 | base::TimeDelta min_delay = |
| 236 | base::TimeDelta::FromSeconds(kMinStartDeviceRetryDelaySeconds); |
| 237 | base::TimeDelta retry_delay = later * 2; |
| 238 | if (retry_delay > max_delay) { retry_delay = max_delay; } |
| 239 | if (retry_delay < min_delay) { retry_delay = min_delay; } |
Christopher Wiley | cd41966 | 2015-02-06 17:51:43 -0800 | [diff] [blame] | 240 | current->PostDelayedTask( |
| 241 | FROM_HERE, |
| 242 | base::Bind(&DeviceRegistrationInfo::StartDevice, |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 243 | weak_factory_.GetWeakPtr(), nullptr, |
| 244 | retry_delay), |
Christopher Wiley | cd41966 | 2015-02-06 17:51:43 -0800 | [diff] [blame] | 245 | later); |
| 246 | } |
| 247 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 248 | bool DeviceRegistrationInfo::CheckRegistration(chromeos::ErrorPtr* error) { |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 249 | return HaveRegistrationCredentials(error) && |
David Zeuthen | 390d191 | 2015-03-03 14:54:48 -0500 | [diff] [blame] | 250 | MaybeRefreshAccessToken(error); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | bool DeviceRegistrationInfo::HaveRegistrationCredentials( |
| 254 | chromeos::ErrorPtr* error) { |
| 255 | const bool have_credentials = !refresh_token_.empty() && |
| 256 | !device_id_.empty() && |
| 257 | !device_robot_account_.empty(); |
| 258 | |
| 259 | VLOG(1) << "Device registration record " |
| 260 | << ((have_credentials) ? "found" : "not found."); |
| 261 | if (!have_credentials) |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 262 | chromeos::Error::AddTo(error, FROM_HERE, kErrorDomainGCD, |
| 263 | "device_not_registered", |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 264 | "No valid device registration record found"); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 265 | return have_credentials; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 268 | std::unique_ptr<base::DictionaryValue> |
| 269 | DeviceRegistrationInfo::ParseOAuthResponse( |
| 270 | const chromeos::http::Response* response, chromeos::ErrorPtr* error) { |
| 271 | int code = 0; |
| 272 | auto resp = chromeos::http::ParseJsonResponse(response, &code, error); |
| 273 | if (resp && code >= chromeos::http::status_code::BadRequest) { |
| 274 | std::string error_code, error_message; |
| 275 | if (!resp->GetString("error", &error_code)) { |
| 276 | error_code = "unexpected_response"; |
| 277 | } |
| 278 | if (error_code == "invalid_grant") { |
| 279 | LOG(INFO) << "The device's registration has been revoked."; |
| 280 | SetRegistrationStatus(RegistrationStatus::kInvalidCredentials); |
| 281 | } |
| 282 | // I have never actually seen an error_description returned. |
| 283 | if (!resp->GetString("error_description", &error_message)) { |
| 284 | error_message = "Unexpected OAuth error"; |
| 285 | } |
| 286 | chromeos::Error::AddTo(error, FROM_HERE, buffet::kErrorDomainOAuth2, |
| 287 | error_code, error_message); |
| 288 | return std::unique_ptr<base::DictionaryValue>(); |
| 289 | } |
| 290 | return resp; |
| 291 | } |
| 292 | |
David Zeuthen | 390d191 | 2015-03-03 14:54:48 -0500 | [diff] [blame] | 293 | bool DeviceRegistrationInfo::MaybeRefreshAccessToken( |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 294 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 295 | LOG(INFO) << "Checking access token expiration."; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 296 | if (!access_token_.empty() && |
| 297 | !access_token_expiration_.is_null() && |
| 298 | access_token_expiration_ > base::Time::Now()) { |
| 299 | LOG(INFO) << "Access token is still valid."; |
| 300 | return true; |
| 301 | } |
David Zeuthen | 390d191 | 2015-03-03 14:54:48 -0500 | [diff] [blame] | 302 | return RefreshAccessToken(error); |
| 303 | } |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 304 | |
David Zeuthen | 390d191 | 2015-03-03 14:54:48 -0500 | [diff] [blame] | 305 | bool DeviceRegistrationInfo::RefreshAccessToken( |
| 306 | chromeos::ErrorPtr* error) { |
| 307 | LOG(INFO) << "Refreshing access token."; |
Alex Vakulenko | 89dde2f | 2015-01-07 12:05:12 -0800 | [diff] [blame] | 308 | auto response = chromeos::http::PostFormDataAndBlock(GetOAuthURL("token"), { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 309 | {"refresh_token", refresh_token_}, |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame^] | 310 | {"client_id", config_->client_id()}, |
| 311 | {"client_secret", config_->client_secret()}, |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 312 | {"grant_type", "refresh_token"}, |
Alex Vakulenko | 89dde2f | 2015-01-07 12:05:12 -0800 | [diff] [blame] | 313 | }, {}, transport_, error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 314 | if (!response) |
| 315 | return false; |
| 316 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 317 | auto json = ParseOAuthResponse(response.get(), error); |
| 318 | if (!json) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 319 | return false; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 320 | |
| 321 | int expires_in = 0; |
| 322 | if (!json->GetString("access_token", &access_token_) || |
| 323 | !json->GetInteger("expires_in", &expires_in) || |
| 324 | access_token_.empty() || |
| 325 | expires_in <= 0) { |
| 326 | LOG(ERROR) << "Access token unavailable."; |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 327 | chromeos::Error::AddTo(error, FROM_HERE, kErrorDomainOAuth2, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 328 | "unexpected_server_response", |
| 329 | "Access token unavailable"); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 330 | return false; |
| 331 | } |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 332 | access_token_expiration_ = base::Time::Now() + |
| 333 | base::TimeDelta::FromSeconds(expires_in); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 334 | LOG(INFO) << "Access token is refreshed for additional " << expires_in |
| 335 | << " seconds."; |
Nathan Bullock | d9e0bcd | 2015-02-11 11:36:39 -0500 | [diff] [blame] | 336 | |
Nathan Bullock | bea9113 | 2015-02-19 09:13:33 -0500 | [diff] [blame] | 337 | StartXmpp(); |
Nathan Bullock | d9e0bcd | 2015-02-11 11:36:39 -0500 | [diff] [blame] | 338 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 339 | return true; |
| 340 | } |
| 341 | |
Nathan Bullock | bea9113 | 2015-02-19 09:13:33 -0500 | [diff] [blame] | 342 | void DeviceRegistrationInfo::StartXmpp() { |
| 343 | // If no MessageLoop assume we're in unittests. |
| 344 | if (!base::MessageLoop::current()) { |
| 345 | LOG(INFO) << "No MessageLoop, not starting XMPP"; |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | if (!fd_watcher_.StopWatchingFileDescriptor()) { |
| 350 | LOG(WARNING) << "Failed to stop the previous watcher"; |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | std::unique_ptr<XmppConnection> connection(new XmppConnection()); |
| 355 | if (!connection->Initialize()) { |
| 356 | LOG(WARNING) << "Failed to connect to XMPP server"; |
| 357 | return; |
| 358 | } |
| 359 | xmpp_client_.reset(new XmppClient(device_robot_account_, access_token_, |
| 360 | std::move(connection))); |
| 361 | if (!base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 362 | xmpp_client_->GetFileDescriptor(), true /* persistent */, |
| 363 | base::MessageLoopForIO::WATCH_READ, &fd_watcher_, this)) { |
| 364 | LOG(WARNING) << "Failed to watch XMPP file descriptor"; |
| 365 | return; |
| 366 | } |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 367 | |
Nathan Bullock | bea9113 | 2015-02-19 09:13:33 -0500 | [diff] [blame] | 368 | xmpp_client_->StartStream(); |
| 369 | } |
| 370 | |
Nathan Bullock | f12f7f0 | 2015-02-20 14:46:53 -0500 | [diff] [blame] | 371 | void DeviceRegistrationInfo::OnFileCanReadWithoutBlocking(int fd) { |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 372 | if (!xmpp_client_ || xmpp_client_->GetFileDescriptor() != fd) |
| 373 | return; |
| 374 | if (!xmpp_client_->Read()) { |
| 375 | // Authentication failed or the socket was closed. |
| 376 | if (!fd_watcher_.StopWatchingFileDescriptor()) |
| 377 | LOG(WARNING) << "Failed to stop the watcher"; |
| 378 | return; |
Nathan Bullock | f12f7f0 | 2015-02-20 14:46:53 -0500 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 382 | std::unique_ptr<base::DictionaryValue> |
| 383 | DeviceRegistrationInfo::BuildDeviceResource(chromeos::ErrorPtr* error) { |
| 384 | std::unique_ptr<base::DictionaryValue> commands = |
| 385 | command_manager_->GetCommandDictionary().GetCommandsAsJson(true, error); |
| 386 | if (!commands) |
| 387 | return nullptr; |
| 388 | |
| 389 | std::unique_ptr<base::DictionaryValue> state = |
| 390 | state_manager_->GetStateValuesAsJson(error); |
| 391 | if (!state) |
| 392 | return nullptr; |
| 393 | |
| 394 | std::unique_ptr<base::DictionaryValue> resource{new base::DictionaryValue}; |
| 395 | if (!device_id_.empty()) |
| 396 | resource->SetString("id", device_id_); |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame^] | 397 | resource->SetString("deviceKind", config_->device_kind()); |
| 398 | resource->SetString("name", config_->name()); |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 399 | if (!display_name_.empty()) |
| 400 | resource->SetString("displayName", display_name_); |
Vitaly Buka | d3eb19c | 2014-11-21 13:39:43 -0800 | [diff] [blame] | 401 | if (!description_.empty()) |
| 402 | resource->SetString("description", description_); |
| 403 | if (!location_.empty()) |
| 404 | resource->SetString("location", location_); |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame^] | 405 | if (!config_->model_id().empty()) |
| 406 | resource->SetString("modelManifestId", config_->model_id()); |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 407 | resource->SetString("channel.supportedType", "xmpp"); |
| 408 | resource->Set("commandDefs", commands.release()); |
| 409 | resource->Set("state", state.release()); |
| 410 | |
| 411 | return resource; |
| 412 | } |
| 413 | |
Vitaly Buka | 6ca3ad6 | 2015-03-11 17:03:23 -0700 | [diff] [blame] | 414 | std::unique_ptr<base::DictionaryValue> DeviceRegistrationInfo::GetDeviceInfo( |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 415 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 416 | if (!CheckRegistration(error)) |
Vitaly Buka | 6ca3ad6 | 2015-03-11 17:03:23 -0700 | [diff] [blame] | 417 | return std::unique_ptr<base::DictionaryValue>(); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 418 | |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 419 | // TODO(antonm): Switch to DoCloudRequest later. |
Alex Vakulenko | 89dde2f | 2015-01-07 12:05:12 -0800 | [diff] [blame] | 420 | auto response = chromeos::http::GetAndBlock( |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 421 | GetDeviceURL(), {GetAuthorizationHeader()}, transport_, error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 422 | int status_code = 0; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 423 | std::unique_ptr<base::DictionaryValue> json = |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 424 | chromeos::http::ParseJsonResponse(response.get(), &status_code, error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 425 | if (json) { |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 426 | if (status_code >= chromeos::http::status_code::BadRequest) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 427 | LOG(WARNING) << "Failed to retrieve the device info. Response code = " |
| 428 | << status_code; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 429 | ParseGCDError(json.get(), error); |
Vitaly Buka | 6ca3ad6 | 2015-03-11 17:03:23 -0700 | [diff] [blame] | 430 | return std::unique_ptr<base::DictionaryValue>(); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
Vitaly Buka | 6ca3ad6 | 2015-03-11 17:03:23 -0700 | [diff] [blame] | 433 | return json; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame^] | 436 | namespace { |
| 437 | |
| 438 | bool GetWithDefault(const std::map<std::string, std::string>& source, |
| 439 | const std::string& key, |
| 440 | const std::string& default_value, |
| 441 | std::string* output) { |
| 442 | auto it = source.find(key); |
| 443 | if (it == source.end()) { |
| 444 | *output = default_value; |
| 445 | return false; |
| 446 | } |
| 447 | *output = it->second; |
| 448 | return true; |
| 449 | } |
| 450 | |
| 451 | } // namespace |
| 452 | |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 453 | std::string DeviceRegistrationInfo::RegisterDevice( |
Alex Vakulenko | a904434 | 2014-08-23 19:31:27 -0700 | [diff] [blame] | 454 | const std::map<std::string, std::string>& params, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 455 | chromeos::ErrorPtr* error) { |
Nathan Bullock | e440848 | 2015-02-19 11:13:21 -0500 | [diff] [blame] | 456 | std::string ticket_id; |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame^] | 457 | if (!GetWithDefault(params, "ticket_id", "", &ticket_id)) { |
| 458 | chromeos::Error::AddTo(error, FROM_HERE, kErrorDomainBuffet, |
| 459 | "missing_parameter", |
| 460 | "Need ticket_id parameter for RegisterDevice()."); |
David Zeuthen | d0db55c | 2015-02-12 14:47:35 -0500 | [diff] [blame] | 461 | return std::string(); |
Vitaly Buka | 6522ab6 | 2015-02-19 10:26:31 -0800 | [diff] [blame] | 462 | } |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame^] | 463 | // These fields are optional, and will default to values from the manufacturer |
| 464 | // supplied config. |
| 465 | GetWithDefault(params, storage_keys::kDisplayName, |
| 466 | config_->default_display_name(), &display_name_); |
| 467 | GetWithDefault(params, storage_keys::kDescription, |
| 468 | config_->default_description(), &description_); |
| 469 | GetWithDefault(params, storage_keys::kLocation, |
| 470 | config_->default_location(), &location_); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 471 | |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 472 | std::unique_ptr<base::DictionaryValue> device_draft = |
| 473 | BuildDeviceResource(error); |
| 474 | if (!device_draft) |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 475 | return std::string(); |
| 476 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 477 | base::DictionaryValue req_json; |
Nathan Bullock | e440848 | 2015-02-19 11:13:21 -0500 | [diff] [blame] | 478 | req_json.SetString("id", ticket_id); |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame^] | 479 | req_json.SetString("oauthClientId", config_->client_id()); |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 480 | req_json.Set("deviceDraft", device_draft.release()); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 481 | |
Nathan Bullock | e440848 | 2015-02-19 11:13:21 -0500 | [diff] [blame] | 482 | auto url = GetServiceURL("registrationTickets/" + ticket_id, |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame^] | 483 | {{"key", config_->api_key()}}); |
Anton Muhin | 532ff7e | 2014-09-29 23:21:21 +0400 | [diff] [blame] | 484 | std::unique_ptr<chromeos::http::Response> response = |
Alex Vakulenko | 89dde2f | 2015-01-07 12:05:12 -0800 | [diff] [blame] | 485 | chromeos::http::PatchJsonAndBlock(url, &req_json, {}, transport_, error); |
Anton Muhin | 532ff7e | 2014-09-29 23:21:21 +0400 | [diff] [blame] | 486 | auto json_resp = chromeos::http::ParseJsonResponse(response.get(), nullptr, |
| 487 | error); |
| 488 | if (!json_resp) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 489 | return std::string(); |
David Zeuthen | 1dbad47 | 2015-02-12 15:24:21 -0500 | [diff] [blame] | 490 | if (!response->IsSuccessful()) { |
| 491 | ParseGCDError(json_resp.get(), error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 492 | return std::string(); |
David Zeuthen | 1dbad47 | 2015-02-12 15:24:21 -0500 | [diff] [blame] | 493 | } |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 494 | |
Nathan Bullock | e440848 | 2015-02-19 11:13:21 -0500 | [diff] [blame] | 495 | url = GetServiceURL("registrationTickets/" + ticket_id + |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame^] | 496 | "/finalize?key=" + config_->api_key()); |
Alex Vakulenko | 89dde2f | 2015-01-07 12:05:12 -0800 | [diff] [blame] | 497 | response = chromeos::http::SendRequestWithNoDataAndBlock( |
| 498 | chromeos::http::request_type::kPost, url, {}, transport_, error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 499 | if (!response) |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 500 | return std::string(); |
| 501 | json_resp = chromeos::http::ParseJsonResponse(response.get(), nullptr, error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 502 | if (!json_resp) |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 503 | return std::string(); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 504 | if (!response->IsSuccessful()) { |
| 505 | ParseGCDError(json_resp.get(), error); |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 506 | return std::string(); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 507 | } |
Anton Muhin | 532ff7e | 2014-09-29 23:21:21 +0400 | [diff] [blame] | 508 | |
| 509 | std::string auth_code; |
Vitaly Buka | 620bd7e | 2015-03-16 01:07:01 -0700 | [diff] [blame] | 510 | std::string device_id; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 511 | if (!json_resp->GetString("robotAccountEmail", &device_robot_account_) || |
| 512 | !json_resp->GetString("robotAccountAuthorizationCode", &auth_code) || |
Vitaly Buka | 620bd7e | 2015-03-16 01:07:01 -0700 | [diff] [blame] | 513 | !json_resp->GetString("deviceDraft.id", &device_id)) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 514 | chromeos::Error::AddTo(error, FROM_HERE, kErrorDomainGCD, |
| 515 | "unexpected_response", |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 516 | "Device account missing in response"); |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 517 | return std::string(); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 518 | } |
Vitaly Buka | 620bd7e | 2015-03-16 01:07:01 -0700 | [diff] [blame] | 519 | SetDeviceId(device_id); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 520 | |
| 521 | // Now get access_token and refresh_token |
Alex Vakulenko | 89dde2f | 2015-01-07 12:05:12 -0800 | [diff] [blame] | 522 | response = chromeos::http::PostFormDataAndBlock(GetOAuthURL("token"), { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 523 | {"code", auth_code}, |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame^] | 524 | {"client_id", config_->client_id()}, |
| 525 | {"client_secret", config_->client_secret()}, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 526 | {"redirect_uri", "oob"}, |
| 527 | {"scope", "https://www.googleapis.com/auth/clouddevices"}, |
| 528 | {"grant_type", "authorization_code"} |
Alex Vakulenko | 89dde2f | 2015-01-07 12:05:12 -0800 | [diff] [blame] | 529 | }, {}, transport_, error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 530 | if (!response) |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 531 | return std::string(); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 532 | |
| 533 | json_resp = ParseOAuthResponse(response.get(), error); |
| 534 | int expires_in = 0; |
| 535 | if (!json_resp || |
| 536 | !json_resp->GetString("access_token", &access_token_) || |
| 537 | !json_resp->GetString("refresh_token", &refresh_token_) || |
| 538 | !json_resp->GetInteger("expires_in", &expires_in) || |
| 539 | access_token_.empty() || |
| 540 | refresh_token_.empty() || |
| 541 | expires_in <= 0) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 542 | chromeos::Error::AddTo(error, FROM_HERE, |
| 543 | kErrorDomainGCD, "unexpected_response", |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 544 | "Device access_token missing in response"); |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 545 | return std::string(); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | access_token_expiration_ = base::Time::Now() + |
| 549 | base::TimeDelta::FromSeconds(expires_in); |
| 550 | |
| 551 | Save(); |
Nathan Bullock | bea9113 | 2015-02-19 09:13:33 -0500 | [diff] [blame] | 552 | StartXmpp(); |
Christopher Wiley | cd41966 | 2015-02-06 17:51:43 -0800 | [diff] [blame] | 553 | |
| 554 | // We're going to respond with our success immediately and we'll StartDevice |
| 555 | // shortly after. |
| 556 | ScheduleStartDevice(base::TimeDelta::FromSeconds(0)); |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 557 | return device_id_; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 560 | namespace { |
| 561 | |
| 562 | template <class T> |
| 563 | void PostToCallback(base::Callback<void(const T&)> callback, |
| 564 | std::unique_ptr<T> value) { |
| 565 | auto cb = [callback] (T* result) { |
| 566 | callback.Run(*result); |
| 567 | }; |
| 568 | base::MessageLoop::current()->PostTask( |
| 569 | FROM_HERE, base::Bind(cb, base::Owned(value.release()))); |
| 570 | } |
| 571 | |
Anton Muhin | 0ae1fba | 2014-10-22 19:30:40 +0400 | [diff] [blame] | 572 | void PostRepeatingTask(const tracked_objects::Location& from_here, |
| 573 | base::Closure task, |
| 574 | base::TimeDelta delay) { |
| 575 | task.Run(); |
| 576 | base::MessageLoop::current()->PostDelayedTask( |
| 577 | from_here, base::Bind(&PostRepeatingTask, from_here, task, delay), delay); |
| 578 | } |
| 579 | |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 580 | using ResponsePtr = scoped_ptr<chromeos::http::Response>; |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 581 | |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 582 | void SendRequestWithRetries( |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 583 | const std::string& method, |
| 584 | const std::string& url, |
| 585 | const std::string& data, |
| 586 | const std::string& mime_type, |
| 587 | const chromeos::http::HeaderList& headers, |
| 588 | std::shared_ptr<chromeos::http::Transport> transport, |
| 589 | int num_retries, |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 590 | const chromeos::http::SuccessCallback& success_callback, |
| 591 | const chromeos::http::ErrorCallback& error_callback) { |
| 592 | auto on_failure = |
| 593 | [method, url, data, mime_type, headers, transport, num_retries, |
Alex Vakulenko | 6401d01 | 2015-01-16 07:40:43 -0800 | [diff] [blame] | 594 | success_callback, error_callback](int request_id, |
| 595 | const chromeos::Error* error) { |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 596 | if (num_retries > 0) { |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 597 | SendRequestWithRetries(method, url, data, mime_type, |
| 598 | headers, transport, num_retries - 1, |
| 599 | success_callback, error_callback); |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 600 | } else { |
Alex Vakulenko | 6401d01 | 2015-01-16 07:40:43 -0800 | [diff] [blame] | 601 | error_callback.Run(request_id, error); |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 602 | } |
| 603 | }; |
| 604 | |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 605 | auto on_success = |
Alex Vakulenko | 6401d01 | 2015-01-16 07:40:43 -0800 | [diff] [blame] | 606 | [on_failure, success_callback, error_callback](int request_id, |
| 607 | ResponsePtr response) { |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 608 | int status_code = response->GetStatusCode(); |
| 609 | if (status_code >= chromeos::http::status_code::Continue && |
| 610 | status_code < chromeos::http::status_code::BadRequest) { |
Alex Vakulenko | 6401d01 | 2015-01-16 07:40:43 -0800 | [diff] [blame] | 611 | success_callback.Run(request_id, response.Pass()); |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 612 | return; |
| 613 | } |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 614 | |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 615 | // TODO(antonm): Should add some useful information to error. |
| 616 | LOG(WARNING) << "Request failed. Response code = " << status_code; |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 617 | |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 618 | chromeos::ErrorPtr error; |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 619 | chromeos::Error::AddTo(&error, FROM_HERE, chromeos::errors::http::kDomain, |
Anton Muhin | 233d2ee | 2014-10-22 15:16:24 +0400 | [diff] [blame] | 620 | std::to_string(status_code), |
| 621 | response->GetStatusText()); |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 622 | if (status_code >= chromeos::http::status_code::InternalServerError && |
| 623 | status_code < 600) { |
| 624 | // Request was valid, but server failed, retry. |
| 625 | // TODO(antonm): Implement exponential backoff. |
| 626 | // TODO(antonm): Reconsider status codes, maybe only some require |
| 627 | // retry. |
| 628 | // TODO(antonm): Support Retry-After header. |
Alex Vakulenko | 6401d01 | 2015-01-16 07:40:43 -0800 | [diff] [blame] | 629 | on_failure(request_id, error.get()); |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 630 | } else { |
Alex Vakulenko | 6401d01 | 2015-01-16 07:40:43 -0800 | [diff] [blame] | 631 | error_callback.Run(request_id, error.get()); |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 632 | } |
| 633 | }; |
| 634 | |
| 635 | chromeos::http::SendRequest(method, url, data.c_str(), data.size(), |
| 636 | mime_type, headers, transport, |
| 637 | base::Bind(on_success), |
| 638 | base::Bind(on_failure)); |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | } // namespace |
| 642 | |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 643 | void DeviceRegistrationInfo::DoCloudRequest( |
Anton Muhin | 233d2ee | 2014-10-22 15:16:24 +0400 | [diff] [blame] | 644 | const std::string& method, |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 645 | const std::string& url, |
| 646 | const base::DictionaryValue* body, |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 647 | const CloudRequestCallback& success_callback, |
| 648 | const CloudRequestErrorCallback& error_callback) { |
| 649 | // TODO(antonm): Add reauthorization on access token expiration (do not |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 650 | // forget about 5xx when fetching new access token). |
| 651 | // TODO(antonm): Add support for device removal. |
| 652 | |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 653 | std::string data; |
| 654 | if (body) |
| 655 | base::JSONWriter::Write(body, &data); |
| 656 | |
| 657 | const std::string mime_type{chromeos::mime::AppendParameter( |
| 658 | chromeos::mime::application::kJson, |
| 659 | chromeos::mime::parameters::kCharset, |
| 660 | "utf-8")}; |
| 661 | |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 662 | auto status_cb = base::Bind(&DeviceRegistrationInfo::SetRegistrationStatus, |
| 663 | weak_factory_.GetWeakPtr()); |
| 664 | |
| 665 | auto request_cb = [success_callback, error_callback, status_cb]( |
| 666 | int request_id, ResponsePtr response) { |
| 667 | status_cb.Run(RegistrationStatus::kConnected); |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 668 | chromeos::ErrorPtr error; |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 669 | |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 670 | std::unique_ptr<base::DictionaryValue> json_resp{ |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 671 | chromeos::http::ParseJsonResponse(response.get(), nullptr, &error)}; |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 672 | if (!json_resp) { |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 673 | error_callback.Run(error.get()); |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 674 | return; |
| 675 | } |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 676 | |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 677 | success_callback.Run(*json_resp); |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 678 | }; |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 679 | |
Alex Vakulenko | 6401d01 | 2015-01-16 07:40:43 -0800 | [diff] [blame] | 680 | auto error_cb = |
| 681 | [error_callback](int request_id, const chromeos::Error* error) { |
| 682 | error_callback.Run(error); |
| 683 | }; |
| 684 | |
Anton Muhin | 233d2ee | 2014-10-22 15:16:24 +0400 | [diff] [blame] | 685 | auto transport = transport_; |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 686 | auto error_callackback_with_reauthorization = base::Bind( |
| 687 | [method, url, data, mime_type, transport, request_cb, error_cb, |
| 688 | status_cb](DeviceRegistrationInfo* self, int request_id, |
| 689 | const chromeos::Error* error) { |
| 690 | status_cb.Run(RegistrationStatus::kConnecting); |
| 691 | if (error->HasError( |
| 692 | chromeos::errors::http::kDomain, |
| 693 | std::to_string(chromeos::http::status_code::Denied))) { |
| 694 | chromeos::ErrorPtr reauthorization_error; |
| 695 | // Forcibly refresh the access token. |
| 696 | if (!self->RefreshAccessToken(&reauthorization_error)) { |
| 697 | // TODO(antonm): Check if the device has been actually removed. |
| 698 | error_cb(request_id, reauthorization_error.get()); |
| 699 | return; |
| 700 | } |
| 701 | SendRequestWithRetries(method, url, data, mime_type, |
| 702 | {self->GetAuthorizationHeader()}, transport, 7, |
| 703 | base::Bind(request_cb), base::Bind(error_cb)); |
| 704 | } else { |
| 705 | error_cb(request_id, error); |
| 706 | } |
| 707 | }, |
| 708 | base::Unretained(this)); |
Anton Muhin | 233d2ee | 2014-10-22 15:16:24 +0400 | [diff] [blame] | 709 | |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 710 | SendRequestWithRetries(method, url, |
| 711 | data, mime_type, |
| 712 | {GetAuthorizationHeader()}, |
| 713 | transport, |
| 714 | 7, |
| 715 | base::Bind(request_cb), |
| 716 | error_callackback_with_reauthorization); |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 717 | } |
| 718 | |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 719 | void DeviceRegistrationInfo::StartDevice( |
| 720 | chromeos::ErrorPtr* error, |
| 721 | const base::TimeDelta& retry_delay) { |
Nathan Bullock | 8fb6de7 | 2015-02-24 11:58:39 -0500 | [diff] [blame] | 722 | if (!HaveRegistrationCredentials(error)) |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 723 | return; |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 724 | auto handle_start_device_failure_cb = base::Bind( |
| 725 | &IgnoreCloudErrorWithCallback, |
| 726 | base::Bind(&DeviceRegistrationInfo::ScheduleStartDevice, |
| 727 | weak_factory_.GetWeakPtr(), |
| 728 | retry_delay)); |
| 729 | // "Starting" a device just means that we: |
| 730 | // 1) push an updated device resource |
| 731 | // 2) fetch an initial set of outstanding commands |
| 732 | // 3) abort any commands that we've previously marked as "in progress" |
| 733 | // or as being in an error state. |
| 734 | // 4) Initiate periodic polling for commands. |
| 735 | auto periodically_poll_commands_cb = base::Bind( |
| 736 | &DeviceRegistrationInfo::PeriodicallyPollCommands, |
| 737 | weak_factory_.GetWeakPtr()); |
| 738 | auto abort_commands_cb = base::Bind( |
| 739 | &DeviceRegistrationInfo::AbortLimboCommands, |
| 740 | weak_factory_.GetWeakPtr(), |
| 741 | periodically_poll_commands_cb); |
| 742 | auto fetch_commands_cb = base::Bind( |
| 743 | &DeviceRegistrationInfo::FetchCommands, |
| 744 | weak_factory_.GetWeakPtr(), |
| 745 | abort_commands_cb, |
| 746 | handle_start_device_failure_cb); |
| 747 | UpdateDeviceResource(fetch_commands_cb, handle_start_device_failure_cb); |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 748 | } |
| 749 | |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 750 | void DeviceRegistrationInfo::UpdateCommand( |
| 751 | const std::string& command_id, |
| 752 | const base::DictionaryValue& command_patch) { |
| 753 | DoCloudRequest( |
| 754 | chromeos::http::request_type::kPatch, |
| 755 | GetServiceURL("commands/" + command_id), |
| 756 | &command_patch, |
| 757 | base::Bind(&IgnoreCloudResult), base::Bind(&IgnoreCloudError)); |
| 758 | } |
| 759 | |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 760 | void DeviceRegistrationInfo::UpdateDeviceResource( |
| 761 | const base::Closure& on_success, |
| 762 | const CloudRequestErrorCallback& on_failure) { |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 763 | std::unique_ptr<base::DictionaryValue> device_resource = |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 764 | BuildDeviceResource(nullptr); |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 765 | if (!device_resource) |
| 766 | return; |
| 767 | |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 768 | DoCloudRequest( |
| 769 | chromeos::http::request_type::kPut, |
| 770 | GetDeviceURL(), |
| 771 | device_resource.get(), |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 772 | base::Bind(&IgnoreCloudResultWithCallback, on_success), |
| 773 | on_failure); |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 774 | } |
Anton Muhin | a34f0d9 | 2014-10-03 21:09:40 +0400 | [diff] [blame] | 775 | |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 776 | namespace { |
| 777 | |
| 778 | void HandleFetchCommandsResult( |
| 779 | const base::Callback<void(const base::ListValue&)>& callback, |
| 780 | const base::DictionaryValue& json) { |
| 781 | const base::ListValue* commands{nullptr}; |
| 782 | if (!json.GetList("commands", &commands)) { |
| 783 | VLOG(1) << "No commands in the response."; |
| 784 | } |
| 785 | const base::ListValue empty; |
| 786 | callback.Run(commands ? *commands : empty); |
| 787 | } |
| 788 | |
| 789 | } // namespace |
| 790 | |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 791 | void DeviceRegistrationInfo::FetchCommands( |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 792 | const base::Callback<void(const base::ListValue&)>& on_success, |
| 793 | const CloudRequestErrorCallback& on_failure) { |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 794 | DoCloudRequest( |
| 795 | chromeos::http::request_type::kGet, |
| 796 | GetServiceURL("commands/queue", {{"deviceId", device_id_}}), |
| 797 | nullptr, |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 798 | base::Bind(&HandleFetchCommandsResult, on_success), |
| 799 | on_failure); |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 800 | } |
Anton Muhin | a34f0d9 | 2014-10-03 21:09:40 +0400 | [diff] [blame] | 801 | |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 802 | void DeviceRegistrationInfo::AbortLimboCommands( |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 803 | const base::Closure& callback, const base::ListValue& commands) { |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 804 | const size_t size{commands.GetSize()}; |
| 805 | for (size_t i = 0; i < size; ++i) { |
| 806 | const base::DictionaryValue* command{nullptr}; |
| 807 | if (!commands.GetDictionary(i, &command)) { |
| 808 | LOG(WARNING) << "No command resource at " << i; |
| 809 | continue; |
Anton Muhin | a34f0d9 | 2014-10-03 21:09:40 +0400 | [diff] [blame] | 810 | } |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 811 | std::string command_state; |
| 812 | if (!command->GetString("state", &command_state)) { |
| 813 | LOG(WARNING) << "Command with no state at " << i; |
| 814 | continue; |
| 815 | } |
| 816 | if (command_state != "error" && |
| 817 | command_state != "inProgress" && |
| 818 | command_state != "paused") { |
| 819 | // It's not a limbo command, ignore. |
| 820 | continue; |
| 821 | } |
| 822 | std::string command_id; |
| 823 | if (!command->GetString("id", &command_id)) { |
| 824 | LOG(WARNING) << "Command with no ID at " << i; |
| 825 | continue; |
| 826 | } |
Anton Muhin | 6d2569e | 2014-10-30 12:32:27 +0400 | [diff] [blame] | 827 | |
| 828 | std::unique_ptr<base::DictionaryValue> command_copy{command->DeepCopy()}; |
| 829 | command_copy->SetString("state", "aborted"); |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 830 | // TODO(wiley) We could consider handling this error case more gracefully. |
Anton Muhin | 6d2569e | 2014-10-30 12:32:27 +0400 | [diff] [blame] | 831 | DoCloudRequest( |
| 832 | chromeos::http::request_type::kPut, |
| 833 | GetServiceURL("commands/" + command_id), |
| 834 | command_copy.get(), |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 835 | base::Bind(&IgnoreCloudResult), base::Bind(&IgnoreCloudError)); |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 836 | } |
Anton Muhin | 0ae1fba | 2014-10-22 19:30:40 +0400 | [diff] [blame] | 837 | |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 838 | base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 839 | } |
| 840 | |
| 841 | void DeviceRegistrationInfo::PeriodicallyPollCommands() { |
Anton Muhin | d07e206 | 2014-10-27 10:53:29 +0400 | [diff] [blame] | 842 | VLOG(1) << "Poll commands"; |
| 843 | PostRepeatingTask( |
| 844 | FROM_HERE, |
| 845 | base::Bind( |
| 846 | &DeviceRegistrationInfo::FetchCommands, |
| 847 | base::Unretained(this), |
| 848 | base::Bind(&DeviceRegistrationInfo::PublishCommands, |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 849 | base::Unretained(this)), |
| 850 | base::Bind(&IgnoreCloudError)), |
Anton Muhin | d07e206 | 2014-10-27 10:53:29 +0400 | [diff] [blame] | 851 | base::TimeDelta::FromSeconds(7)); |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 852 | // TODO(antonm): Use better trigger: when StateManager registers new updates, |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 853 | // it should call closure which will post a task, probably with some |
| 854 | // throttling, to publish state updates. |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 855 | PostRepeatingTask( |
| 856 | FROM_HERE, |
| 857 | base::Bind(&DeviceRegistrationInfo::PublishStateUpdates, |
| 858 | base::Unretained(this)), |
| 859 | base::TimeDelta::FromSeconds(7)); |
Anton Muhin | d07e206 | 2014-10-27 10:53:29 +0400 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | void DeviceRegistrationInfo::PublishCommands(const base::ListValue& commands) { |
| 863 | const CommandDictionary& command_dictionary = |
| 864 | command_manager_->GetCommandDictionary(); |
| 865 | |
| 866 | const size_t size{commands.GetSize()}; |
| 867 | for (size_t i = 0; i < size; ++i) { |
| 868 | const base::DictionaryValue* command{nullptr}; |
| 869 | if (!commands.GetDictionary(i, &command)) { |
| 870 | LOG(WARNING) << "No command resource at " << i; |
| 871 | continue; |
| 872 | } |
| 873 | |
| 874 | std::unique_ptr<CommandInstance> command_instance = |
| 875 | CommandInstance::FromJson(command, command_dictionary, nullptr); |
| 876 | if (!command_instance) { |
| 877 | LOG(WARNING) << "Failed to parse a command"; |
| 878 | continue; |
| 879 | } |
| 880 | |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 881 | // TODO(antonm): Properly process cancellation of commands. |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 882 | if (!command_manager_->FindCommand(command_instance->GetID())) { |
| 883 | std::unique_ptr<CommandProxyInterface> cloud_proxy{ |
| 884 | new CloudCommandProxy(command_instance.get(), this)}; |
| 885 | command_instance->AddProxy(std::move(cloud_proxy)); |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 886 | command_manager_->AddCommand(std::move(command_instance)); |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 887 | } |
Anton Muhin | d07e206 | 2014-10-27 10:53:29 +0400 | [diff] [blame] | 888 | } |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 889 | } |
| 890 | |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 891 | void DeviceRegistrationInfo::PublishStateUpdates() { |
| 892 | VLOG(1) << "PublishStateUpdates"; |
| 893 | const std::vector<StateChange> state_changes{ |
| 894 | state_manager_->GetAndClearRecordedStateChanges()}; |
| 895 | if (state_changes.empty()) |
| 896 | return; |
| 897 | |
| 898 | std::unique_ptr<base::ListValue> patches{new base::ListValue}; |
| 899 | for (const auto& state_change : state_changes) { |
| 900 | std::unique_ptr<base::DictionaryValue> patch{new base::DictionaryValue}; |
Anton Muhin | 76933fd | 2014-11-21 21:25:18 +0400 | [diff] [blame] | 901 | patch->SetString("timeMs", |
| 902 | std::to_string(state_change.timestamp.ToJavaTime())); |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 903 | |
| 904 | std::unique_ptr<base::DictionaryValue> changes{new base::DictionaryValue}; |
| 905 | for (const auto& pair : state_change.changed_properties) { |
| 906 | auto value = pair.second->ToJson(nullptr); |
| 907 | if (!value) { |
| 908 | return; |
| 909 | } |
Alex Vakulenko | 61ad4db | 2015-01-20 10:50:04 -0800 | [diff] [blame] | 910 | // The key in |pair.first| is the full property name in format |
| 911 | // "package.property_name", so must use DictionaryValue::Set() instead of |
| 912 | // DictionaryValue::SetWithoutPathExpansion to recreate the JSON |
| 913 | // property tree properly. |
| 914 | changes->Set(pair.first, value.release()); |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 915 | } |
| 916 | patch->Set("patch", changes.release()); |
| 917 | |
| 918 | patches->Append(patch.release()); |
| 919 | } |
| 920 | |
| 921 | base::DictionaryValue body; |
Anton Muhin | 76933fd | 2014-11-21 21:25:18 +0400 | [diff] [blame] | 922 | body.SetString("requestTimeMs", |
| 923 | std::to_string(base::Time::Now().ToJavaTime())); |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 924 | body.Set("patches", patches.release()); |
| 925 | |
| 926 | DoCloudRequest( |
| 927 | chromeos::http::request_type::kPost, |
| 928 | GetDeviceURL("patchState"), |
| 929 | &body, |
| 930 | base::Bind(&IgnoreCloudResult), base::Bind(&IgnoreCloudError)); |
| 931 | } |
| 932 | |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 933 | void DeviceRegistrationInfo::SetRegistrationStatus( |
| 934 | RegistrationStatus new_status) { |
| 935 | if (new_status == registration_status_) |
| 936 | return; |
| 937 | VLOG(1) << "Changing registration status to " << StatusToString(new_status); |
| 938 | registration_status_ = new_status; |
Vitaly Buka | 7ad8ffb | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 939 | if (!on_status_changed_.is_null()) |
| 940 | on_status_changed_.Run(); |
Vitaly Buka | 620bd7e | 2015-03-16 01:07:01 -0700 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | void DeviceRegistrationInfo::SetDeviceId(const std::string& device_id) { |
| 944 | if (device_id == device_id_) |
| 945 | return; |
| 946 | device_id_ = device_id; |
Vitaly Buka | 7ad8ffb | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 947 | if (!on_status_changed_.is_null()) |
| 948 | on_status_changed_.Run(); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 949 | } |
| 950 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 951 | } // namespace buffet |