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