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