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