Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 5 | #include "src/device_registration_info.h" |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 6 | |
Alex Vakulenko | f3a95bf | 2015-07-01 07:52:13 -0700 | [diff] [blame] | 7 | #include <algorithm> |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 8 | #include <memory> |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 9 | #include <set> |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 10 | #include <utility> |
| 11 | #include <vector> |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 12 | |
Christopher Wiley | cd41966 | 2015-02-06 17:51:43 -0800 | [diff] [blame] | 13 | #include <base/bind.h> |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 14 | #include <base/json/json_reader.h> |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 15 | #include <base/json/json_writer.h> |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 16 | #include <base/strings/string_number_conversions.h> |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 17 | #include <base/values.h> |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 18 | #include <weave/provider/http_client.h> |
| 19 | #include <weave/provider/network.h> |
| 20 | #include <weave/provider/task_runner.h> |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 21 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 22 | #include "src/bind_lambda.h" |
| 23 | #include "src/commands/cloud_command_proxy.h" |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 24 | #include "src/commands/command_manager.h" |
| 25 | #include "src/commands/schema_constants.h" |
| 26 | #include "src/data_encoding.h" |
| 27 | #include "src/http_constants.h" |
| 28 | #include "src/json_error_codes.h" |
| 29 | #include "src/notification/xmpp_channel.h" |
| 30 | #include "src/states/state_manager.h" |
| 31 | #include "src/string_utils.h" |
| 32 | #include "src/utils.h" |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 33 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 34 | namespace weave { |
| 35 | |
| 36 | const char kErrorDomainOAuth2[] = "oauth2"; |
| 37 | const char kErrorDomainGCD[] = "gcd"; |
| 38 | const char kErrorDomainGCDServer[] = "gcd_server"; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 39 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 40 | namespace { |
| 41 | |
Vitaly Buka | 41a90d6 | 2015-09-29 16:58:39 -0700 | [diff] [blame] | 42 | const int kPollingPeriodSeconds = 7; |
Alex Vakulenko | c1fc90c | 2015-10-22 08:00:43 -0700 | [diff] [blame] | 43 | const int kBackupPollingPeriodMinutes = 30; |
Vitaly Buka | 41a90d6 | 2015-09-29 16:58:39 -0700 | [diff] [blame] | 44 | |
Alex Vakulenko | e07c29d | 2015-10-22 10:31:12 -0700 | [diff] [blame] | 45 | namespace fetch_reason { |
| 46 | |
| 47 | const char kDeviceStart[] = "device_start"; // Initial queue fetch at startup. |
| 48 | const char kRegularPull[] = "regular_pull"; // Regular fetch before XMPP is up. |
| 49 | const char kNewCommand[] = "new_command"; // A new command is available. |
| 50 | const char kJustInCase[] = "just_in_case"; // Backup fetch when XMPP is live. |
| 51 | |
| 52 | } // namespace fetch_reason |
| 53 | |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 54 | using provider::HttpClient; |
| 55 | |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 56 | inline void SetUnexpectedError(ErrorPtr* error) { |
| 57 | Error::AddTo(error, FROM_HERE, kErrorDomainGCD, "unexpected_response", |
| 58 | "Unexpected GCD error"); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 61 | void ParseGCDError(const base::DictionaryValue* json, ErrorPtr* error) { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 62 | const base::Value* list_value = nullptr; |
| 63 | const base::ListValue* error_list = nullptr; |
| 64 | if (!json->Get("error.errors", &list_value) || |
| 65 | !list_value->GetAsList(&error_list)) { |
| 66 | SetUnexpectedError(error); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | for (size_t i = 0; i < error_list->GetSize(); i++) { |
| 71 | const base::Value* error_value = nullptr; |
| 72 | const base::DictionaryValue* error_object = nullptr; |
| 73 | if (!error_list->Get(i, &error_value) || |
| 74 | !error_value->GetAsDictionary(&error_object)) { |
| 75 | SetUnexpectedError(error); |
| 76 | continue; |
| 77 | } |
| 78 | std::string error_code, error_message; |
| 79 | if (error_object->GetString("reason", &error_code) && |
| 80 | error_object->GetString("message", &error_message)) { |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 81 | Error::AddTo(error, FROM_HERE, kErrorDomainGCDServer, error_code, |
| 82 | error_message); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 83 | } else { |
| 84 | SetUnexpectedError(error); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
Vitaly Buka | 7d55639 | 2015-08-13 20:06:48 -0700 | [diff] [blame] | 89 | std::string AppendQueryParams(const std::string& url, |
| 90 | const WebParamList& params) { |
Vitaly Buka | b001f28 | 2015-08-13 17:07:59 -0700 | [diff] [blame] | 91 | CHECK_EQ(std::string::npos, url.find_first_of("?#")); |
| 92 | if (params.empty()) |
| 93 | return url; |
Vitaly Buka | 7d55639 | 2015-08-13 20:06:48 -0700 | [diff] [blame] | 94 | return url + '?' + WebParamsEncode(params); |
Vitaly Buka | b001f28 | 2015-08-13 17:07:59 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Alex Vakulenko | bda220a | 2014-04-18 15:25:44 -0700 | [diff] [blame] | 97 | std::string BuildURL(const std::string& url, |
Vitaly Buka | b001f28 | 2015-08-13 17:07:59 -0700 | [diff] [blame] | 98 | const std::string& subpath, |
Vitaly Buka | 7d55639 | 2015-08-13 20:06:48 -0700 | [diff] [blame] | 99 | const WebParamList& params) { |
Vitaly Buka | b001f28 | 2015-08-13 17:07:59 -0700 | [diff] [blame] | 100 | std::string result = url; |
| 101 | if (!result.empty() && result.back() != '/' && !subpath.empty()) { |
| 102 | CHECK_NE('/', subpath.front()); |
| 103 | result += '/'; |
| 104 | } |
| 105 | result += subpath; |
| 106 | return AppendQueryParams(result, params); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Vitaly Buka | f7f52d4 | 2015-10-10 22:43:55 -0700 | [diff] [blame] | 109 | void IgnoreCloudErrorWithCallback(const base::Closure& cb, ErrorPtr) { |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 110 | cb.Run(); |
| 111 | } |
| 112 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 113 | void IgnoreCloudError(ErrorPtr) {} |
Anton Muhin | 6d2569e | 2014-10-30 12:32:27 +0400 | [diff] [blame] | 114 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 115 | void IgnoreCloudResult(const base::DictionaryValue&, ErrorPtr error) {} |
| 116 | |
| 117 | void IgnoreCloudResultWithCallback(const DoneCallback& cb, |
| 118 | const base::DictionaryValue&, |
| 119 | ErrorPtr error) { |
| 120 | cb.Run(std::move(error)); |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 123 | class RequestSender final { |
| 124 | public: |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 125 | RequestSender(HttpClient::Method method, |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 126 | const std::string& url, |
| 127 | HttpClient* transport) |
| 128 | : method_{method}, url_{url}, transport_{transport} {} |
| 129 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 130 | void Send(const HttpClient::SendRequestCallback& callback) { |
Vitaly Buka | 866b60a | 2015-10-09 14:24:55 -0700 | [diff] [blame] | 131 | static int debug_id = 0; |
| 132 | ++debug_id; |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 133 | VLOG(1) << "Sending request. id:" << debug_id |
| 134 | << " method:" << EnumToString(method_) << " url:" << url_; |
Vitaly Buka | 866b60a | 2015-10-09 14:24:55 -0700 | [diff] [blame] | 135 | VLOG(2) << "Request data: " << data_; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 136 | auto on_done = []( |
| 137 | int debug_id, const HttpClient::SendRequestCallback& callback, |
| 138 | std::unique_ptr<HttpClient::Response> response, ErrorPtr error) { |
| 139 | if (error) { |
| 140 | VLOG(1) << "Request failed, id=" << debug_id |
| 141 | << ", reason: " << error->GetCode() |
| 142 | << ", message: " << error->GetMessage(); |
| 143 | return callback.Run({}, std::move(error)); |
| 144 | } |
| 145 | VLOG(1) << "Request succeeded. id:" << debug_id |
| 146 | << " status:" << response->GetStatusCode(); |
| 147 | VLOG(2) << "Response data: " << response->GetData(); |
| 148 | callback.Run(std::move(response), nullptr); |
Vitaly Buka | 866b60a | 2015-10-09 14:24:55 -0700 | [diff] [blame] | 149 | }; |
| 150 | transport_->SendRequest(method_, url_, GetFullHeaders(), data_, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 151 | base::Bind(on_done, debug_id, callback)); |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Vitaly Buka | 815b603 | 2015-08-06 11:06:25 -0700 | [diff] [blame] | 154 | void SetAccessToken(const std::string& access_token) { |
| 155 | access_token_ = access_token; |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void SetData(const std::string& data, const std::string& mime_type) { |
| 159 | data_ = data; |
| 160 | mime_type_ = mime_type; |
| 161 | } |
| 162 | |
| 163 | void SetFormData( |
| 164 | const std::vector<std::pair<std::string, std::string>>& data) { |
Vitaly Buka | 7d55639 | 2015-08-13 20:06:48 -0700 | [diff] [blame] | 165 | SetData(WebParamsEncode(data), http::kWwwFormUrlEncoded); |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void SetJsonData(const base::Value& json) { |
| 169 | std::string data; |
Vitaly Buka | 815b603 | 2015-08-06 11:06:25 -0700 | [diff] [blame] | 170 | CHECK(base::JSONWriter::Write(json, &data)); |
| 171 | SetData(data, http::kJsonUtf8); |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | private: |
Vitaly Buka | 815b603 | 2015-08-06 11:06:25 -0700 | [diff] [blame] | 175 | HttpClient::Headers GetFullHeaders() const { |
| 176 | HttpClient::Headers headers; |
| 177 | if (!access_token_.empty()) |
| 178 | headers.emplace_back(http::kAuthorization, "Bearer " + access_token_); |
| 179 | if (!mime_type_.empty()) |
| 180 | headers.emplace_back(http::kContentType, mime_type_); |
| 181 | return headers; |
| 182 | } |
| 183 | |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 184 | HttpClient::Method method_; |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 185 | std::string url_; |
| 186 | std::string data_; |
| 187 | std::string mime_type_; |
Vitaly Buka | 815b603 | 2015-08-06 11:06:25 -0700 | [diff] [blame] | 188 | std::string access_token_; |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 189 | HttpClient* transport_{nullptr}; |
| 190 | |
| 191 | DISALLOW_COPY_AND_ASSIGN(RequestSender); |
| 192 | }; |
| 193 | |
| 194 | std::unique_ptr<base::DictionaryValue> ParseJsonResponse( |
| 195 | const HttpClient::Response& response, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 196 | ErrorPtr* error) { |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 197 | // Make sure we have a correct content type. Do not try to parse |
| 198 | // binary files, or HTML output. Limit to application/json and text/plain. |
Vitaly Buka | 24d6fd5 | 2015-08-13 23:22:48 -0700 | [diff] [blame] | 199 | std::string content_type = |
| 200 | SplitAtFirst(response.GetContentType(), ";", true).first; |
Vitaly Buka | 1da6599 | 2015-08-06 01:38:57 -0700 | [diff] [blame] | 201 | |
| 202 | if (content_type != http::kJson && content_type != http::kPlain) { |
Vitaly Buka | c27390d | 2015-11-19 14:42:35 -0800 | [diff] [blame] | 203 | Error::AddTo( |
| 204 | error, FROM_HERE, errors::json::kDomain, "non_json_content_type", |
| 205 | "Unexpected content type: \'" + response.GetContentType() + "\'"); |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 206 | return std::unique_ptr<base::DictionaryValue>(); |
| 207 | } |
| 208 | |
| 209 | const std::string& json = response.GetData(); |
| 210 | std::string error_message; |
| 211 | auto value = base::JSONReader::ReadAndReturnError(json, base::JSON_PARSE_RFC, |
| 212 | nullptr, &error_message); |
| 213 | if (!value) { |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 214 | Error::AddToPrintf(error, FROM_HERE, errors::json::kDomain, |
| 215 | errors::json::kParseError, |
| 216 | "Error '%s' occurred parsing JSON string '%s'", |
| 217 | error_message.c_str(), json.c_str()); |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 218 | return std::unique_ptr<base::DictionaryValue>(); |
| 219 | } |
| 220 | base::DictionaryValue* dict_value = nullptr; |
| 221 | if (!value->GetAsDictionary(&dict_value)) { |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 222 | Error::AddToPrintf( |
Vitaly Buka | ea2f15f | 2015-08-13 15:26:20 -0700 | [diff] [blame] | 223 | error, FROM_HERE, errors::json::kDomain, errors::json::kObjectExpected, |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 224 | "Response is not a valid JSON object: '%s'", json.c_str()); |
| 225 | return std::unique_ptr<base::DictionaryValue>(); |
| 226 | } else { |
| 227 | // |value| is now owned by |dict_value|, so release the scoped_ptr now. |
| 228 | base::IgnoreResult(value.release()); |
| 229 | } |
| 230 | return std::unique_ptr<base::DictionaryValue>(dict_value); |
| 231 | } |
| 232 | |
| 233 | bool IsSuccessful(const HttpClient::Response& response) { |
| 234 | int code = response.GetStatusCode(); |
Vitaly Buka | 1da6599 | 2015-08-06 01:38:57 -0700 | [diff] [blame] | 235 | return code >= http::kContinue && code < http::kBadRequest; |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 238 | } // anonymous namespace |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 239 | |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 240 | DeviceRegistrationInfo::DeviceRegistrationInfo( |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 241 | const std::shared_ptr<CommandManager>& command_manager, |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 242 | const std::shared_ptr<StateManager>& state_manager, |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 243 | std::unique_ptr<Config> config, |
| 244 | provider::TaskRunner* task_runner, |
| 245 | provider::HttpClient* http_client, |
| 246 | provider::Network* network) |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 247 | : http_client_{http_client}, |
Alex Vakulenko | a56a7e6 | 2015-06-26 12:59:50 -0700 | [diff] [blame] | 248 | task_runner_{task_runner}, |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 249 | command_manager_{command_manager}, |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 250 | state_manager_{state_manager}, |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 251 | config_{std::move(config)}, |
Vitaly Buka | ddb2e38 | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 252 | network_{network} { |
Vitaly Buka | 0f80f7c | 2015-08-13 00:57:25 -0700 | [diff] [blame] | 253 | cloud_backoff_policy_.reset(new BackoffEntry::Policy{}); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 254 | cloud_backoff_policy_->num_errors_to_ignore = 0; |
Alex Vakulenko | f61ee01 | 2015-06-24 14:21:35 -0700 | [diff] [blame] | 255 | cloud_backoff_policy_->initial_delay_ms = 1000; |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 256 | cloud_backoff_policy_->multiply_factor = 2.0; |
| 257 | cloud_backoff_policy_->jitter_factor = 0.1; |
| 258 | cloud_backoff_policy_->maximum_backoff_ms = 30000; |
| 259 | cloud_backoff_policy_->entry_lifetime_ms = -1; |
| 260 | cloud_backoff_policy_->always_use_initial_delay = false; |
Vitaly Buka | 0f80f7c | 2015-08-13 00:57:25 -0700 | [diff] [blame] | 261 | cloud_backoff_entry_.reset(new BackoffEntry{cloud_backoff_policy_.get()}); |
| 262 | oauth2_backoff_entry_.reset(new BackoffEntry{cloud_backoff_policy_.get()}); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 263 | |
Vitaly Buka | 672634b | 2015-11-20 09:49:30 -0800 | [diff] [blame] | 264 | bool revoked = |
| 265 | !GetSettings().cloud_id.empty() && !HaveRegistrationCredentials(); |
| 266 | gcd_state_ = |
| 267 | revoked ? GcdState::kInvalidCredentials : GcdState::kUnconfigured; |
| 268 | |
Vitaly Buka | 553a762 | 2015-10-05 13:53:20 -0700 | [diff] [blame] | 269 | command_manager_->AddCommandDefChanged( |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 270 | base::Bind(&DeviceRegistrationInfo::OnCommandDefsChanged, |
| 271 | weak_factory_.GetWeakPtr())); |
Vitaly Buka | 4c98135 | 2015-10-01 23:04:24 -0700 | [diff] [blame] | 272 | state_manager_->AddChangedCallback(base::Bind( |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 273 | &DeviceRegistrationInfo::OnStateChanged, weak_factory_.GetWeakPtr())); |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 274 | } |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 275 | |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 276 | DeviceRegistrationInfo::~DeviceRegistrationInfo() = default; |
| 277 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 278 | std::string DeviceRegistrationInfo::GetServiceURL( |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 279 | const std::string& subpath, |
Vitaly Buka | 7d55639 | 2015-08-13 20:06:48 -0700 | [diff] [blame] | 280 | const WebParamList& params) const { |
Vitaly Buka | 5cf12a3 | 2015-09-15 21:25:48 -0700 | [diff] [blame] | 281 | return BuildURL(GetSettings().service_url, subpath, params); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | std::string DeviceRegistrationInfo::GetDeviceURL( |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 285 | const std::string& subpath, |
Vitaly Buka | 7d55639 | 2015-08-13 20:06:48 -0700 | [diff] [blame] | 286 | const WebParamList& params) const { |
Johan Euphrosine | 312c2f5 | 2015-09-29 00:04:29 -0700 | [diff] [blame] | 287 | CHECK(!GetSettings().cloud_id.empty()) << "Must have a valid device ID"; |
Vitaly Buka | 5cf12a3 | 2015-09-15 21:25:48 -0700 | [diff] [blame] | 288 | return BuildURL(GetSettings().service_url, |
Johan Euphrosine | 312c2f5 | 2015-09-29 00:04:29 -0700 | [diff] [blame] | 289 | "devices/" + GetSettings().cloud_id + "/" + subpath, params); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 292 | std::string DeviceRegistrationInfo::GetOAuthURL( |
| 293 | const std::string& subpath, |
Vitaly Buka | 7d55639 | 2015-08-13 20:06:48 -0700 | [diff] [blame] | 294 | const WebParamList& params) const { |
Vitaly Buka | 5cf12a3 | 2015-09-15 21:25:48 -0700 | [diff] [blame] | 295 | return BuildURL(GetSettings().oauth_url, subpath, params); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 298 | void DeviceRegistrationInfo::Start() { |
Alex Vakulenko | 3fa42ae | 2015-06-23 15:12:22 -0700 | [diff] [blame] | 299 | if (HaveRegistrationCredentials()) { |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 300 | StartNotificationChannel(); |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 301 | // Wait a significant amount of time for local daemons to publish their |
| 302 | // state to Buffet before publishing it to the cloud. |
| 303 | // TODO(wiley) We could do a lot of things here to either expose this |
| 304 | // timeout as a configurable knob or allow local |
| 305 | // daemons to signal that their state is up to date so that |
| 306 | // we need not wait for them. |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 307 | ScheduleCloudConnection(base::TimeDelta::FromSeconds(5)); |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 308 | } |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 311 | void DeviceRegistrationInfo::ScheduleCloudConnection( |
| 312 | const base::TimeDelta& delay) { |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 313 | SetGcdState(GcdState::kConnecting); |
Alex Vakulenko | a56a7e6 | 2015-06-26 12:59:50 -0700 | [diff] [blame] | 314 | if (!task_runner_) |
Vitaly Buka | 0f6b2ec | 2015-08-20 15:35:19 -0700 | [diff] [blame] | 315 | return; // Assume we're in test |
Alex Vakulenko | a56a7e6 | 2015-06-26 12:59:50 -0700 | [diff] [blame] | 316 | task_runner_->PostDelayedTask( |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 317 | FROM_HERE, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 318 | base::Bind(&DeviceRegistrationInfo::ConnectToCloud, AsWeakPtr(), nullptr), |
| 319 | delay); |
Christopher Wiley | cd41966 | 2015-02-06 17:51:43 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Alex Vakulenko | 3fa42ae | 2015-06-23 15:12:22 -0700 | [diff] [blame] | 322 | bool DeviceRegistrationInfo::HaveRegistrationCredentials() const { |
Vitaly Buka | 5cf12a3 | 2015-09-15 21:25:48 -0700 | [diff] [blame] | 323 | return !GetSettings().refresh_token.empty() && |
Johan Euphrosine | 312c2f5 | 2015-09-29 00:04:29 -0700 | [diff] [blame] | 324 | !GetSettings().cloud_id.empty() && |
Vitaly Buka | 5cf12a3 | 2015-09-15 21:25:48 -0700 | [diff] [blame] | 325 | !GetSettings().robot_account.empty(); |
Alex Vakulenko | 3fa42ae | 2015-06-23 15:12:22 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | bool DeviceRegistrationInfo::VerifyRegistrationCredentials( |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 329 | ErrorPtr* error) const { |
Alex Vakulenko | 3fa42ae | 2015-06-23 15:12:22 -0700 | [diff] [blame] | 330 | const bool have_credentials = HaveRegistrationCredentials(); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 331 | |
Alex Vakulenko | ed77a57 | 2015-07-15 10:55:43 -0700 | [diff] [blame] | 332 | VLOG(2) << "Device registration record " |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 333 | << ((have_credentials) ? "found" : "not found."); |
| 334 | if (!have_credentials) |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 335 | Error::AddTo(error, FROM_HERE, kErrorDomainGCD, "device_not_registered", |
| 336 | "No valid device registration record found"); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 337 | return have_credentials; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 340 | std::unique_ptr<base::DictionaryValue> |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 341 | DeviceRegistrationInfo::ParseOAuthResponse(const HttpClient::Response& response, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 342 | ErrorPtr* error) { |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 343 | int code = response.GetStatusCode(); |
| 344 | auto resp = ParseJsonResponse(response, error); |
Vitaly Buka | 1da6599 | 2015-08-06 01:38:57 -0700 | [diff] [blame] | 345 | if (resp && code >= http::kBadRequest) { |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 346 | std::string error_code, error_message; |
| 347 | if (!resp->GetString("error", &error_code)) { |
| 348 | error_code = "unexpected_response"; |
| 349 | } |
| 350 | if (error_code == "invalid_grant") { |
| 351 | LOG(INFO) << "The device's registration has been revoked."; |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 352 | SetGcdState(GcdState::kInvalidCredentials); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 353 | } |
| 354 | // I have never actually seen an error_description returned. |
| 355 | if (!resp->GetString("error_description", &error_message)) { |
| 356 | error_message = "Unexpected OAuth error"; |
| 357 | } |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 358 | Error::AddTo(error, FROM_HERE, kErrorDomainOAuth2, error_code, |
| 359 | error_message); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 360 | return std::unique_ptr<base::DictionaryValue>(); |
| 361 | } |
| 362 | return resp; |
| 363 | } |
| 364 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 365 | void DeviceRegistrationInfo::RefreshAccessToken(const DoneCallback& callback) { |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 366 | LOG(INFO) << "Refreshing access token."; |
Alex Vakulenko | 3fa42ae | 2015-06-23 15:12:22 -0700 | [diff] [blame] | 367 | |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 368 | ErrorPtr error; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 369 | if (!VerifyRegistrationCredentials(&error)) |
| 370 | return callback.Run(std::move(error)); |
Alex Vakulenko | 3fa42ae | 2015-06-23 15:12:22 -0700 | [diff] [blame] | 371 | |
Alex Vakulenko | 0f91be8 | 2015-07-27 10:11:53 -0700 | [diff] [blame] | 372 | if (oauth2_backoff_entry_->ShouldRejectRequest()) { |
| 373 | VLOG(1) << "RefreshToken request delayed for " |
| 374 | << oauth2_backoff_entry_->GetTimeUntilRelease() |
| 375 | << " due to backoff policy"; |
| 376 | task_runner_->PostDelayedTask( |
| 377 | FROM_HERE, base::Bind(&DeviceRegistrationInfo::RefreshAccessToken, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 378 | AsWeakPtr(), callback), |
Alex Vakulenko | 0f91be8 | 2015-07-27 10:11:53 -0700 | [diff] [blame] | 379 | oauth2_backoff_entry_->GetTimeUntilRelease()); |
| 380 | return; |
| 381 | } |
| 382 | |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 383 | RequestSender sender{HttpClient::Method::kPost, GetOAuthURL("token"), |
| 384 | http_client_}; |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 385 | sender.SetFormData({ |
Vitaly Buka | 5cf12a3 | 2015-09-15 21:25:48 -0700 | [diff] [blame] | 386 | {"refresh_token", GetSettings().refresh_token}, |
| 387 | {"client_id", GetSettings().client_id}, |
| 388 | {"client_secret", GetSettings().client_secret}, |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 389 | {"grant_type", "refresh_token"}, |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 390 | }); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 391 | sender.Send(base::Bind(&DeviceRegistrationInfo::OnRefreshAccessTokenDone, |
| 392 | weak_factory_.GetWeakPtr(), callback)); |
Vitaly Buka | 866b60a | 2015-10-09 14:24:55 -0700 | [diff] [blame] | 393 | VLOG(1) << "Refresh access token request dispatched"; |
David Zeuthen | 390d191 | 2015-03-03 14:54:48 -0500 | [diff] [blame] | 394 | } |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 395 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 396 | void DeviceRegistrationInfo::OnRefreshAccessTokenDone( |
| 397 | const DoneCallback& callback, |
| 398 | std::unique_ptr<HttpClient::Response> response, |
| 399 | ErrorPtr error) { |
| 400 | if (error) { |
| 401 | VLOG(1) << "Refresh access token failed"; |
| 402 | oauth2_backoff_entry_->InformOfRequest(false); |
| 403 | return RefreshAccessToken(callback); |
| 404 | } |
Vitaly Buka | 866b60a | 2015-10-09 14:24:55 -0700 | [diff] [blame] | 405 | VLOG(1) << "Refresh access token request completed"; |
Alex Vakulenko | 0f91be8 | 2015-07-27 10:11:53 -0700 | [diff] [blame] | 406 | oauth2_backoff_entry_->InformOfRequest(true); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 407 | auto json = ParseOAuthResponse(*response, &error); |
Vitaly Buka | f7f52d4 | 2015-10-10 22:43:55 -0700 | [diff] [blame] | 408 | if (!json) |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 409 | return callback.Run(std::move(error)); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 410 | |
| 411 | int expires_in = 0; |
| 412 | if (!json->GetString("access_token", &access_token_) || |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 413 | !json->GetInteger("expires_in", &expires_in) || access_token_.empty() || |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 414 | expires_in <= 0) { |
| 415 | LOG(ERROR) << "Access token unavailable."; |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 416 | Error::AddTo(&error, FROM_HERE, kErrorDomainOAuth2, |
| 417 | "unexpected_server_response", "Access token unavailable"); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 418 | return callback.Run(std::move(error)); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 419 | } |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 420 | access_token_expiration_ = |
| 421 | base::Time::Now() + base::TimeDelta::FromSeconds(expires_in); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 422 | LOG(INFO) << "Access token is refreshed for additional " << expires_in |
| 423 | << " seconds."; |
Nathan Bullock | d9e0bcd | 2015-02-11 11:36:39 -0500 | [diff] [blame] | 424 | |
Alex Vakulenko | 6b028ae | 2015-05-29 09:38:59 -0700 | [diff] [blame] | 425 | if (primary_notification_channel_ && |
| 426 | !primary_notification_channel_->IsConnected()) { |
| 427 | // If we have disconnected channel, it is due to failed credentials. |
| 428 | // Now that we have a new access token, retry the connection. |
| 429 | StartNotificationChannel(); |
| 430 | } |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 431 | callback.Run(nullptr); |
Alex Vakulenko | 6b028ae | 2015-05-29 09:38:59 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 434 | void DeviceRegistrationInfo::StartNotificationChannel() { |
Alex Vakulenko | 6b028ae | 2015-05-29 09:38:59 -0700 | [diff] [blame] | 435 | if (notification_channel_starting_) |
| 436 | return; |
| 437 | |
Alex Vakulenko | 3fa42ae | 2015-06-23 15:12:22 -0700 | [diff] [blame] | 438 | LOG(INFO) << "Starting notification channel"; |
| 439 | |
Vitaly Buka | 0f6b2ec | 2015-08-20 15:35:19 -0700 | [diff] [blame] | 440 | // If no TaskRunner assume we're in test. |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 441 | if (!network_) { |
| 442 | LOG(INFO) << "No Network, not starting notification channel"; |
Nathan Bullock | bea9113 | 2015-02-19 09:13:33 -0500 | [diff] [blame] | 443 | return; |
| 444 | } |
| 445 | |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 446 | if (primary_notification_channel_) { |
Alex Vakulenko | 26f557b | 2015-05-26 16:47:40 -0700 | [diff] [blame] | 447 | primary_notification_channel_->Stop(); |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 448 | primary_notification_channel_.reset(); |
| 449 | current_notification_channel_ = nullptr; |
| 450 | } |
| 451 | |
| 452 | // Start with just regular polling at the pre-configured polling interval. |
| 453 | // Once the primary notification channel is connected successfully, it will |
| 454 | // call back to OnConnected() and at that time we'll switch to use the |
| 455 | // primary channel and switch periodic poll into much more infrequent backup |
| 456 | // poll mode. |
Vitaly Buka | 41a90d6 | 2015-09-29 16:58:39 -0700 | [diff] [blame] | 457 | const base::TimeDelta pull_interval = |
| 458 | base::TimeDelta::FromSeconds(kPollingPeriodSeconds); |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 459 | if (!pull_channel_) { |
Alex Vakulenko | a56a7e6 | 2015-06-26 12:59:50 -0700 | [diff] [blame] | 460 | pull_channel_.reset(new PullChannel{pull_interval, task_runner_}); |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 461 | pull_channel_->Start(this); |
| 462 | } else { |
| 463 | pull_channel_->UpdatePullInterval(pull_interval); |
| 464 | } |
| 465 | current_notification_channel_ = pull_channel_.get(); |
| 466 | |
Alex Vakulenko | 6b028ae | 2015-05-29 09:38:59 -0700 | [diff] [blame] | 467 | notification_channel_starting_ = true; |
Vitaly Buka | 63cc3d2 | 2015-06-23 20:11:36 -0700 | [diff] [blame] | 468 | primary_notification_channel_.reset(new XmppChannel{ |
Vitaly Buka | 5cf12a3 | 2015-09-15 21:25:48 -0700 | [diff] [blame] | 469 | GetSettings().robot_account, access_token_, task_runner_, network_}); |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 470 | primary_notification_channel_->Start(this); |
Nathan Bullock | f12f7f0 | 2015-02-20 14:46:53 -0500 | [diff] [blame] | 471 | } |
| 472 | |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 473 | void DeviceRegistrationInfo::AddGcdStateChangedCallback( |
| 474 | const Device::GcdStateChangedCallback& callback) { |
| 475 | gcd_state_changed_callbacks_.push_back(callback); |
| 476 | callback.Run(gcd_state_); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 479 | std::unique_ptr<base::DictionaryValue> |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 480 | DeviceRegistrationInfo::BuildDeviceResource(ErrorPtr* error) { |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 481 | // Limit only to commands that are visible to the cloud. |
Alex Vakulenko | 2c7740a | 2015-11-30 08:51:29 -0800 | [diff] [blame] | 482 | const base::DictionaryValue& commands = |
| 483 | command_manager_->GetCommandDictionary().GetCommandsAsJson(); |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 484 | |
Vitaly Buka | c430560 | 2015-11-24 23:33:09 -0800 | [diff] [blame] | 485 | const base::DictionaryValue& state = state_manager_->GetState(); |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 486 | |
| 487 | std::unique_ptr<base::DictionaryValue> resource{new base::DictionaryValue}; |
Johan Euphrosine | 312c2f5 | 2015-09-29 00:04:29 -0700 | [diff] [blame] | 488 | if (!GetSettings().cloud_id.empty()) |
| 489 | resource->SetString("id", GetSettings().cloud_id); |
Vitaly Buka | 5cf12a3 | 2015-09-15 21:25:48 -0700 | [diff] [blame] | 490 | resource->SetString("name", GetSettings().name); |
| 491 | if (!GetSettings().description.empty()) |
| 492 | resource->SetString("description", GetSettings().description); |
| 493 | if (!GetSettings().location.empty()) |
| 494 | resource->SetString("location", GetSettings().location); |
| 495 | resource->SetString("modelManifestId", GetSettings().model_id); |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 496 | std::unique_ptr<base::DictionaryValue> channel{new base::DictionaryValue}; |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 497 | if (current_notification_channel_) { |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 498 | channel->SetString("supportedType", |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 499 | current_notification_channel_->GetName()); |
| 500 | current_notification_channel_->AddChannelParameters(channel.get()); |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 501 | } else { |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 502 | channel->SetString("supportedType", "pull"); |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 503 | } |
| 504 | resource->Set("channel", channel.release()); |
Alex Vakulenko | 2c7740a | 2015-11-30 08:51:29 -0800 | [diff] [blame] | 505 | resource->Set("commandDefs", commands.DeepCopy()); |
Vitaly Buka | c430560 | 2015-11-24 23:33:09 -0800 | [diff] [blame] | 506 | resource->Set("state", state.DeepCopy()); |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 507 | |
| 508 | return resource; |
| 509 | } |
| 510 | |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 511 | void DeviceRegistrationInfo::GetDeviceInfo( |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 512 | const CloudRequestDoneCallback& callback) { |
Vitaly Buka | 11b2f23 | 2015-08-20 13:55:41 -0700 | [diff] [blame] | 513 | ErrorPtr error; |
Vitaly Buka | 672634b | 2015-11-20 09:49:30 -0800 | [diff] [blame] | 514 | if (!VerifyRegistrationCredentials(&error)) |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 515 | return callback.Run({}, std::move(error)); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 516 | DoCloudRequest(HttpClient::Method::kGet, GetDeviceURL(), nullptr, callback); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 519 | void DeviceRegistrationInfo::RegisterDeviceError(const DoneCallback& callback, |
| 520 | ErrorPtr error) { |
| 521 | task_runner_->PostDelayedTask(FROM_HERE, |
| 522 | base::Bind(callback, base::Passed(&error)), {}); |
Vitaly Buka | 4774df2 | 2015-10-09 12:36:22 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 525 | void DeviceRegistrationInfo::RegisterDevice(const std::string& ticket_id, |
| 526 | const DoneCallback& callback) { |
Vitaly Buka | 12870bd | 2015-10-08 23:49:39 -0700 | [diff] [blame] | 527 | ErrorPtr error; |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 528 | std::unique_ptr<base::DictionaryValue> device_draft = |
Vitaly Buka | 12870bd | 2015-10-08 23:49:39 -0700 | [diff] [blame] | 529 | BuildDeviceResource(&error); |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 530 | if (!device_draft) |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 531 | return RegisterDeviceError(callback, std::move(error)); |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 532 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 533 | base::DictionaryValue req_json; |
Nathan Bullock | e440848 | 2015-02-19 11:13:21 -0500 | [diff] [blame] | 534 | req_json.SetString("id", ticket_id); |
Vitaly Buka | 5cf12a3 | 2015-09-15 21:25:48 -0700 | [diff] [blame] | 535 | req_json.SetString("oauthClientId", GetSettings().client_id); |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 536 | req_json.Set("deviceDraft", device_draft.release()); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 537 | |
Nathan Bullock | e440848 | 2015-02-19 11:13:21 -0500 | [diff] [blame] | 538 | auto url = GetServiceURL("registrationTickets/" + ticket_id, |
Vitaly Buka | 5cf12a3 | 2015-09-15 21:25:48 -0700 | [diff] [blame] | 539 | {{"key", GetSettings().api_key}}); |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 540 | |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 541 | RequestSender sender{HttpClient::Method::kPatch, url, http_client_}; |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 542 | sender.SetJsonData(req_json); |
Vitaly Buka | 4774df2 | 2015-10-09 12:36:22 -0700 | [diff] [blame] | 543 | sender.Send(base::Bind(&DeviceRegistrationInfo::RegisterDeviceOnTicketSent, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 544 | weak_factory_.GetWeakPtr(), ticket_id, callback)); |
Vitaly Buka | 4774df2 | 2015-10-09 12:36:22 -0700 | [diff] [blame] | 545 | } |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 546 | |
Vitaly Buka | 4774df2 | 2015-10-09 12:36:22 -0700 | [diff] [blame] | 547 | void DeviceRegistrationInfo::RegisterDeviceOnTicketSent( |
| 548 | const std::string& ticket_id, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 549 | const DoneCallback& callback, |
| 550 | std::unique_ptr<provider::HttpClient::Response> response, |
| 551 | ErrorPtr error) { |
| 552 | if (error) |
| 553 | return RegisterDeviceError(callback, std::move(error)); |
| 554 | auto json_resp = ParseJsonResponse(*response, &error); |
Anton Muhin | 532ff7e | 2014-09-29 23:21:21 +0400 | [diff] [blame] | 555 | if (!json_resp) |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 556 | return RegisterDeviceError(callback, std::move(error)); |
Vitaly Buka | 4774df2 | 2015-10-09 12:36:22 -0700 | [diff] [blame] | 557 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 558 | if (!IsSuccessful(*response)) { |
Vitaly Buka | 12870bd | 2015-10-08 23:49:39 -0700 | [diff] [blame] | 559 | ParseGCDError(json_resp.get(), &error); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 560 | return RegisterDeviceError(callback, std::move(error)); |
David Zeuthen | 1dbad47 | 2015-02-12 15:24:21 -0500 | [diff] [blame] | 561 | } |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 562 | |
Vitaly Buka | 4774df2 | 2015-10-09 12:36:22 -0700 | [diff] [blame] | 563 | std::string url = |
| 564 | GetServiceURL("registrationTickets/" + ticket_id + "/finalize", |
| 565 | {{"key", GetSettings().api_key}}); |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 566 | RequestSender{HttpClient::Method::kPost, url, http_client_}.Send( |
Vitaly Buka | 4774df2 | 2015-10-09 12:36:22 -0700 | [diff] [blame] | 567 | base::Bind(&DeviceRegistrationInfo::RegisterDeviceOnTicketFinalized, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 568 | weak_factory_.GetWeakPtr(), callback)); |
Vitaly Buka | 4774df2 | 2015-10-09 12:36:22 -0700 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | void DeviceRegistrationInfo::RegisterDeviceOnTicketFinalized( |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 572 | const DoneCallback& callback, |
| 573 | std::unique_ptr<provider::HttpClient::Response> response, |
| 574 | ErrorPtr error) { |
| 575 | if (error) |
| 576 | return RegisterDeviceError(callback, std::move(error)); |
| 577 | auto json_resp = ParseJsonResponse(*response, &error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 578 | if (!json_resp) |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 579 | return RegisterDeviceError(callback, std::move(error)); |
| 580 | if (!IsSuccessful(*response)) { |
Vitaly Buka | 12870bd | 2015-10-08 23:49:39 -0700 | [diff] [blame] | 581 | ParseGCDError(json_resp.get(), &error); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 582 | return RegisterDeviceError(callback, std::move(error)); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 583 | } |
Anton Muhin | 532ff7e | 2014-09-29 23:21:21 +0400 | [diff] [blame] | 584 | |
| 585 | std::string auth_code; |
Johan Euphrosine | 312c2f5 | 2015-09-29 00:04:29 -0700 | [diff] [blame] | 586 | std::string cloud_id; |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 587 | std::string robot_account; |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 588 | const base::DictionaryValue* device_draft_response = nullptr; |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 589 | if (!json_resp->GetString("robotAccountEmail", &robot_account) || |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 590 | !json_resp->GetString("robotAccountAuthorizationCode", &auth_code) || |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 591 | !json_resp->GetDictionary("deviceDraft", &device_draft_response) || |
Johan Euphrosine | 312c2f5 | 2015-09-29 00:04:29 -0700 | [diff] [blame] | 592 | !device_draft_response->GetString("id", &cloud_id)) { |
Vitaly Buka | 12870bd | 2015-10-08 23:49:39 -0700 | [diff] [blame] | 593 | Error::AddTo(&error, FROM_HERE, kErrorDomainGCD, "unexpected_response", |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 594 | "Device account missing in response"); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 595 | return RegisterDeviceError(callback, std::move(error)); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 598 | UpdateDeviceInfoTimestamp(*device_draft_response); |
| 599 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 600 | // Now get access_token and refresh_token |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 601 | RequestSender sender2{HttpClient::Method::kPost, GetOAuthURL("token"), |
| 602 | http_client_}; |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 603 | sender2.SetFormData( |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 604 | {{"code", auth_code}, |
Vitaly Buka | 5cf12a3 | 2015-09-15 21:25:48 -0700 | [diff] [blame] | 605 | {"client_id", GetSettings().client_id}, |
| 606 | {"client_secret", GetSettings().client_secret}, |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 607 | {"redirect_uri", "oob"}, |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 608 | {"grant_type", "authorization_code"}}); |
Vitaly Buka | 4774df2 | 2015-10-09 12:36:22 -0700 | [diff] [blame] | 609 | sender2.Send(base::Bind(&DeviceRegistrationInfo::RegisterDeviceOnAuthCodeSent, |
| 610 | weak_factory_.GetWeakPtr(), cloud_id, robot_account, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 611 | callback)); |
Vitaly Buka | 4774df2 | 2015-10-09 12:36:22 -0700 | [diff] [blame] | 612 | } |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 613 | |
Vitaly Buka | 4774df2 | 2015-10-09 12:36:22 -0700 | [diff] [blame] | 614 | void DeviceRegistrationInfo::RegisterDeviceOnAuthCodeSent( |
| 615 | const std::string& cloud_id, |
| 616 | const std::string& robot_account, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 617 | const DoneCallback& callback, |
| 618 | std::unique_ptr<provider::HttpClient::Response> response, |
| 619 | ErrorPtr error) { |
| 620 | if (error) |
| 621 | return RegisterDeviceError(callback, std::move(error)); |
| 622 | auto json_resp = ParseOAuthResponse(*response, &error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 623 | int expires_in = 0; |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 624 | std::string refresh_token; |
| 625 | if (!json_resp || !json_resp->GetString("access_token", &access_token_) || |
| 626 | !json_resp->GetString("refresh_token", &refresh_token) || |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 627 | !json_resp->GetInteger("expires_in", &expires_in) || |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 628 | access_token_.empty() || refresh_token.empty() || expires_in <= 0) { |
Vitaly Buka | 12870bd | 2015-10-08 23:49:39 -0700 | [diff] [blame] | 629 | Error::AddTo(&error, FROM_HERE, kErrorDomainGCD, "unexpected_response", |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 630 | "Device access_token missing in response"); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 631 | return RegisterDeviceError(callback, std::move(error)); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 634 | access_token_expiration_ = |
| 635 | base::Time::Now() + base::TimeDelta::FromSeconds(expires_in); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 636 | |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 637 | Config::Transaction change{config_.get()}; |
Johan Euphrosine | 312c2f5 | 2015-09-29 00:04:29 -0700 | [diff] [blame] | 638 | change.set_cloud_id(cloud_id); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 639 | change.set_robot_account(robot_account); |
| 640 | change.set_refresh_token(refresh_token); |
| 641 | change.Commit(); |
| 642 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 643 | task_runner_->PostDelayedTask(FROM_HERE, base::Bind(callback, nullptr), {}); |
Vitaly Buka | 12870bd | 2015-10-08 23:49:39 -0700 | [diff] [blame] | 644 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 645 | StartNotificationChannel(); |
Christopher Wiley | cd41966 | 2015-02-06 17:51:43 -0800 | [diff] [blame] | 646 | |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 647 | // We're going to respond with our success immediately and we'll connect to |
| 648 | // cloud shortly after. |
Vitaly Buka | 12870bd | 2015-10-08 23:49:39 -0700 | [diff] [blame] | 649 | ScheduleCloudConnection({}); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 652 | void DeviceRegistrationInfo::DoCloudRequest( |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 653 | HttpClient::Method method, |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 654 | const std::string& url, |
| 655 | const base::DictionaryValue* body, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 656 | const CloudRequestDoneCallback& callback) { |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 657 | // We make CloudRequestData shared here because we want to make sure |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 658 | // there is only one instance of callback and error_calback since |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 659 | // those may have move-only types and making a copy of the callback with |
| 660 | // move-only types curried-in will invalidate the source callback. |
| 661 | auto data = std::make_shared<CloudRequestData>(); |
| 662 | data->method = method; |
| 663 | data->url = url; |
| 664 | if (body) |
| 665 | base::JSONWriter::Write(*body, &data->body); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 666 | data->callback = callback; |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 667 | SendCloudRequest(data); |
| 668 | } |
| 669 | |
| 670 | void DeviceRegistrationInfo::SendCloudRequest( |
| 671 | const std::shared_ptr<const CloudRequestData>& data) { |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 672 | // TODO(antonm): Add reauthorization on access token expiration (do not |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 673 | // forget about 5xx when fetching new access token). |
| 674 | // TODO(antonm): Add support for device removal. |
| 675 | |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 676 | ErrorPtr error; |
Vitaly Buka | 672634b | 2015-11-20 09:49:30 -0800 | [diff] [blame] | 677 | if (!VerifyRegistrationCredentials(&error)) |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 678 | return data->callback.Run({}, std::move(error)); |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 679 | |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 680 | if (cloud_backoff_entry_->ShouldRejectRequest()) { |
| 681 | VLOG(1) << "Cloud request delayed for " |
| 682 | << cloud_backoff_entry_->GetTimeUntilRelease() |
| 683 | << " due to backoff policy"; |
Vitaly Buka | f7f52d4 | 2015-10-10 22:43:55 -0700 | [diff] [blame] | 684 | return task_runner_->PostDelayedTask( |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 685 | FROM_HERE, base::Bind(&DeviceRegistrationInfo::SendCloudRequest, |
| 686 | AsWeakPtr(), data), |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 687 | cloud_backoff_entry_->GetTimeUntilRelease()); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 688 | } |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 689 | |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 690 | RequestSender sender{data->method, data->url, http_client_}; |
Vitaly Buka | 1da6599 | 2015-08-06 01:38:57 -0700 | [diff] [blame] | 691 | sender.SetData(data->body, http::kJsonUtf8); |
Vitaly Buka | 815b603 | 2015-08-06 11:06:25 -0700 | [diff] [blame] | 692 | sender.SetAccessToken(access_token_); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 693 | sender.Send(base::Bind(&DeviceRegistrationInfo::OnCloudRequestDone, |
Vitaly Buka | 866b60a | 2015-10-09 14:24:55 -0700 | [diff] [blame] | 694 | AsWeakPtr(), data)); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 695 | } |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 696 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 697 | void DeviceRegistrationInfo::OnCloudRequestDone( |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 698 | const std::shared_ptr<const CloudRequestData>& data, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 699 | std::unique_ptr<provider::HttpClient::Response> response, |
| 700 | ErrorPtr error) { |
| 701 | if (error) |
| 702 | return RetryCloudRequest(data); |
| 703 | int status_code = response->GetStatusCode(); |
Vitaly Buka | 1da6599 | 2015-08-06 01:38:57 -0700 | [diff] [blame] | 704 | if (status_code == http::kDenied) { |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 705 | cloud_backoff_entry_->InformOfRequest(true); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 706 | RefreshAccessToken( |
| 707 | base::Bind(&DeviceRegistrationInfo::OnAccessTokenRefreshed, AsWeakPtr(), |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 708 | data)); |
| 709 | return; |
| 710 | } |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 711 | |
Vitaly Buka | 1da6599 | 2015-08-06 01:38:57 -0700 | [diff] [blame] | 712 | if (status_code >= http::kInternalServerError) { |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 713 | // Request was valid, but server failed, retry. |
| 714 | // TODO(antonm): Reconsider status codes, maybe only some require |
| 715 | // retry. |
| 716 | // TODO(antonm): Support Retry-After header. |
| 717 | RetryCloudRequest(data); |
| 718 | return; |
| 719 | } |
Anton Muhin | 633eded | 2014-10-03 20:40:10 +0400 | [diff] [blame] | 720 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 721 | auto json_resp = ParseJsonResponse(*response, &error); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 722 | if (!json_resp) { |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 723 | cloud_backoff_entry_->InformOfRequest(true); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 724 | return data->callback.Run({}, std::move(error)); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 725 | } |
Anton Muhin | 233d2ee | 2014-10-22 15:16:24 +0400 | [diff] [blame] | 726 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 727 | if (!IsSuccessful(*response)) { |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 728 | ParseGCDError(json_resp.get(), &error); |
Vitaly Buka | 1da6599 | 2015-08-06 01:38:57 -0700 | [diff] [blame] | 729 | if (status_code == http::kForbidden && |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 730 | error->HasError(kErrorDomainGCDServer, "rateLimitExceeded")) { |
Alex Vakulenko | f61ee01 | 2015-06-24 14:21:35 -0700 | [diff] [blame] | 731 | // If we exceeded server quota, retry the request later. |
Vitaly Buka | f7f52d4 | 2015-10-10 22:43:55 -0700 | [diff] [blame] | 732 | return RetryCloudRequest(data); |
Alex Vakulenko | f61ee01 | 2015-06-24 14:21:35 -0700 | [diff] [blame] | 733 | } |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 734 | cloud_backoff_entry_->InformOfRequest(true); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 735 | return data->callback.Run({}, std::move(error)); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Alex Vakulenko | f61ee01 | 2015-06-24 14:21:35 -0700 | [diff] [blame] | 738 | cloud_backoff_entry_->InformOfRequest(true); |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 739 | SetGcdState(GcdState::kConnected); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 740 | data->callback.Run(*json_resp, nullptr); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | void DeviceRegistrationInfo::RetryCloudRequest( |
| 744 | const std::shared_ptr<const CloudRequestData>& data) { |
Alex Vakulenko | f61ee01 | 2015-06-24 14:21:35 -0700 | [diff] [blame] | 745 | // TODO(avakulenko): Tie connecting/connected status to XMPP channel instead. |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 746 | SetGcdState(GcdState::kConnecting); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 747 | cloud_backoff_entry_->InformOfRequest(false); |
| 748 | SendCloudRequest(data); |
| 749 | } |
| 750 | |
| 751 | void DeviceRegistrationInfo::OnAccessTokenRefreshed( |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 752 | const std::shared_ptr<const CloudRequestData>& data, |
| 753 | ErrorPtr error) { |
| 754 | if (error) { |
| 755 | CheckAccessTokenError(error->Clone()); |
| 756 | return data->callback.Run({}, std::move(error)); |
| 757 | } |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 758 | SendCloudRequest(data); |
| 759 | } |
| 760 | |
Vitaly Buka | f7f52d4 | 2015-10-10 22:43:55 -0700 | [diff] [blame] | 761 | void DeviceRegistrationInfo::CheckAccessTokenError(ErrorPtr error) { |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 762 | if (error && error->HasError(kErrorDomainOAuth2, "invalid_grant")) |
Vitaly Buka | 672634b | 2015-11-20 09:49:30 -0800 | [diff] [blame] | 763 | RemoveCredentials(); |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 766 | void DeviceRegistrationInfo::ConnectToCloud(ErrorPtr error) { |
| 767 | if (error) { |
| 768 | if (error->HasError(kErrorDomainOAuth2, "invalid_grant")) |
Vitaly Buka | 672634b | 2015-11-20 09:49:30 -0800 | [diff] [blame] | 769 | RemoveCredentials(); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 770 | return; |
| 771 | } |
| 772 | |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 773 | connected_to_cloud_ = false; |
| 774 | if (!VerifyRegistrationCredentials(nullptr)) |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 775 | return; |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 776 | |
| 777 | if (access_token_.empty()) { |
| 778 | RefreshAccessToken( |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 779 | base::Bind(&DeviceRegistrationInfo::ConnectToCloud, AsWeakPtr())); |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 780 | return; |
| 781 | } |
| 782 | |
| 783 | // Connecting a device to cloud just means that we: |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 784 | // 1) push an updated device resource |
| 785 | // 2) fetch an initial set of outstanding commands |
| 786 | // 3) abort any commands that we've previously marked as "in progress" |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 787 | // or as being in an error state; publish queued commands |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 788 | UpdateDeviceResource( |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 789 | base::Bind(&DeviceRegistrationInfo::OnConnectedToCloud, AsWeakPtr())); |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 792 | void DeviceRegistrationInfo::OnConnectedToCloud(ErrorPtr error) { |
| 793 | if (error) |
| 794 | return; |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 795 | LOG(INFO) << "Device connected to cloud server"; |
| 796 | connected_to_cloud_ = true; |
| 797 | FetchCommands(base::Bind(&DeviceRegistrationInfo::ProcessInitialCommandList, |
Alex Vakulenko | e07c29d | 2015-10-22 10:31:12 -0700 | [diff] [blame] | 798 | AsWeakPtr()), fetch_reason::kDeviceStart); |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 799 | // In case there are any pending state updates since we sent off the initial |
| 800 | // UpdateDeviceResource() request, update the server with any state changes. |
| 801 | PublishStateUpdates(); |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 802 | } |
| 803 | |
Vitaly Buka | b624bc4 | 2015-09-29 19:13:55 -0700 | [diff] [blame] | 804 | void DeviceRegistrationInfo::UpdateDeviceInfo(const std::string& name, |
Vitaly Buka | fa94706 | 2015-04-17 00:41:31 -0700 | [diff] [blame] | 805 | const std::string& description, |
Vitaly Buka | b624bc4 | 2015-09-29 19:13:55 -0700 | [diff] [blame] | 806 | const std::string& location) { |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 807 | Config::Transaction change{config_.get()}; |
Vitaly Buka | 798a0e7 | 2015-06-02 15:37:51 -0700 | [diff] [blame] | 808 | change.set_name(name); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 809 | change.set_description(description); |
| 810 | change.set_location(location); |
Vitaly Buka | ff81db6 | 2015-05-14 21:25:45 -0700 | [diff] [blame] | 811 | change.Commit(); |
Vitaly Buka | fa94706 | 2015-04-17 00:41:31 -0700 | [diff] [blame] | 812 | |
Alex Vakulenko | 3fa42ae | 2015-06-23 15:12:22 -0700 | [diff] [blame] | 813 | if (HaveRegistrationCredentials()) { |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 814 | UpdateDeviceResource(base::Bind(&IgnoreCloudError)); |
Vitaly Buka | fa94706 | 2015-04-17 00:41:31 -0700 | [diff] [blame] | 815 | } |
Vitaly Buka | fa94706 | 2015-04-17 00:41:31 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Vitaly Buka | b624bc4 | 2015-09-29 19:13:55 -0700 | [diff] [blame] | 818 | void DeviceRegistrationInfo::UpdateBaseConfig(AuthScope anonymous_access_role, |
| 819 | bool local_discovery_enabled, |
| 820 | bool local_pairing_enabled) { |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 821 | Config::Transaction change(config_.get()); |
Vitaly Buka | b624bc4 | 2015-09-29 19:13:55 -0700 | [diff] [blame] | 822 | change.set_local_anonymous_access_role(anonymous_access_role); |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 823 | change.set_local_discovery_enabled(local_discovery_enabled); |
| 824 | change.set_local_pairing_enabled(local_pairing_enabled); |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Vitaly Buka | ff81db6 | 2015-05-14 21:25:45 -0700 | [diff] [blame] | 827 | bool DeviceRegistrationInfo::UpdateServiceConfig( |
| 828 | const std::string& client_id, |
| 829 | const std::string& client_secret, |
| 830 | const std::string& api_key, |
| 831 | const std::string& oauth_url, |
| 832 | const std::string& service_url, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 833 | ErrorPtr* error) { |
Alex Vakulenko | 3fa42ae | 2015-06-23 15:12:22 -0700 | [diff] [blame] | 834 | if (HaveRegistrationCredentials()) { |
Vitaly Buka | e2810e0 | 2015-08-16 23:31:55 -0700 | [diff] [blame] | 835 | Error::AddTo(error, FROM_HERE, errors::kErrorDomain, "already_registered", |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 836 | "Unable to change config for registered device"); |
Vitaly Buka | ff81db6 | 2015-05-14 21:25:45 -0700 | [diff] [blame] | 837 | return false; |
| 838 | } |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 839 | Config::Transaction change{config_.get()}; |
Vitaly Buka | ff81db6 | 2015-05-14 21:25:45 -0700 | [diff] [blame] | 840 | change.set_client_id(client_id); |
| 841 | change.set_client_secret(client_secret); |
| 842 | change.set_api_key(api_key); |
| 843 | change.set_oauth_url(oauth_url); |
| 844 | change.set_service_url(service_url); |
| 845 | return true; |
| 846 | } |
| 847 | |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 848 | void DeviceRegistrationInfo::UpdateCommand( |
| 849 | const std::string& command_id, |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 850 | const base::DictionaryValue& command_patch, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 851 | const DoneCallback& callback) { |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 852 | DoCloudRequest(HttpClient::Method::kPatch, |
| 853 | GetServiceURL("commands/" + command_id), &command_patch, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 854 | base::Bind(&IgnoreCloudResultWithCallback, callback)); |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 855 | } |
| 856 | |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 857 | void DeviceRegistrationInfo::NotifyCommandAborted(const std::string& command_id, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 858 | ErrorPtr error) { |
Alex Vakulenko | d1978d3 | 2015-04-29 17:33:26 -0700 | [diff] [blame] | 859 | base::DictionaryValue command_patch; |
| 860 | command_patch.SetString(commands::attributes::kCommand_State, |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 861 | EnumToString(Command::State::kAborted)); |
Alex Vakulenko | d1978d3 | 2015-04-29 17:33:26 -0700 | [diff] [blame] | 862 | if (error) { |
Vitaly Buka | 70f77d9 | 2015-10-07 15:42:40 -0700 | [diff] [blame] | 863 | command_patch.Set(commands::attributes::kCommand_Error, |
| 864 | ErrorInfoToJson(*error).release()); |
Alex Vakulenko | d1978d3 | 2015-04-29 17:33:26 -0700 | [diff] [blame] | 865 | } |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 866 | UpdateCommand(command_id, command_patch, base::Bind(&IgnoreCloudError)); |
Alex Vakulenko | d1978d3 | 2015-04-29 17:33:26 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 869 | void DeviceRegistrationInfo::UpdateDeviceResource( |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 870 | const DoneCallback& callback) { |
| 871 | queued_resource_update_callbacks_.emplace_back(callback); |
Alex Vakulenko | f3a95bf | 2015-07-01 07:52:13 -0700 | [diff] [blame] | 872 | if (!in_progress_resource_update_callbacks_.empty()) { |
| 873 | VLOG(1) << "Another request is already pending."; |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 874 | return; |
Alex Vakulenko | f3a95bf | 2015-07-01 07:52:13 -0700 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | StartQueuedUpdateDeviceResource(); |
| 878 | } |
| 879 | |
| 880 | void DeviceRegistrationInfo::StartQueuedUpdateDeviceResource() { |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 881 | if (in_progress_resource_update_callbacks_.empty() && |
| 882 | queued_resource_update_callbacks_.empty()) |
Alex Vakulenko | f3a95bf | 2015-07-01 07:52:13 -0700 | [diff] [blame] | 883 | return; |
| 884 | |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 885 | if (last_device_resource_updated_timestamp_.empty()) { |
| 886 | // We don't know the current time stamp of the device resource from the |
| 887 | // server side. We need to provide the time stamp to the server as part of |
| 888 | // the request to guard against out-of-order requests overwriting settings |
| 889 | // specified by later requests. |
| 890 | VLOG(1) << "Getting the last device resource timestamp from server..."; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 891 | GetDeviceInfo(base::Bind(&DeviceRegistrationInfo::OnDeviceInfoRetrieved, |
| 892 | AsWeakPtr())); |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 893 | return; |
| 894 | } |
| 895 | |
| 896 | in_progress_resource_update_callbacks_.insert( |
| 897 | in_progress_resource_update_callbacks_.end(), |
| 898 | queued_resource_update_callbacks_.begin(), |
| 899 | queued_resource_update_callbacks_.end()); |
| 900 | queued_resource_update_callbacks_.clear(); |
Alex Vakulenko | f3a95bf | 2015-07-01 07:52:13 -0700 | [diff] [blame] | 901 | |
| 902 | VLOG(1) << "Updating GCD server with CDD..."; |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 903 | ErrorPtr error; |
Alex Vakulenko | f3a95bf | 2015-07-01 07:52:13 -0700 | [diff] [blame] | 904 | std::unique_ptr<base::DictionaryValue> device_resource = |
| 905 | BuildDeviceResource(&error); |
| 906 | if (!device_resource) { |
Vitaly Buka | f7f52d4 | 2015-10-10 22:43:55 -0700 | [diff] [blame] | 907 | return OnUpdateDeviceResourceError(std::move(error)); |
Alex Vakulenko | f3a95bf | 2015-07-01 07:52:13 -0700 | [diff] [blame] | 908 | } |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 909 | |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 910 | std::string url = GetDeviceURL( |
| 911 | {}, {{"lastUpdateTimeMs", last_device_resource_updated_timestamp_}}); |
| 912 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 913 | DoCloudRequest(HttpClient::Method::kPut, url, device_resource.get(), |
| 914 | base::Bind(&DeviceRegistrationInfo::OnUpdateDeviceResourceDone, |
| 915 | AsWeakPtr())); |
Alex Vakulenko | f3a95bf | 2015-07-01 07:52:13 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 918 | void DeviceRegistrationInfo::OnDeviceInfoRetrieved( |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 919 | const base::DictionaryValue& device_info, |
| 920 | ErrorPtr error) { |
| 921 | if (error) |
| 922 | return OnUpdateDeviceResourceError(std::move(error)); |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 923 | if (UpdateDeviceInfoTimestamp(device_info)) |
| 924 | StartQueuedUpdateDeviceResource(); |
| 925 | } |
| 926 | |
| 927 | bool DeviceRegistrationInfo::UpdateDeviceInfoTimestamp( |
| 928 | const base::DictionaryValue& device_info) { |
| 929 | // For newly created devices, "lastUpdateTimeMs" may not be present, but |
| 930 | // "creationTimeMs" should be there at least. |
| 931 | if (!device_info.GetString("lastUpdateTimeMs", |
| 932 | &last_device_resource_updated_timestamp_) && |
| 933 | !device_info.GetString("creationTimeMs", |
| 934 | &last_device_resource_updated_timestamp_)) { |
| 935 | LOG(WARNING) << "Device resource timestamp is missing"; |
| 936 | return false; |
| 937 | } |
| 938 | return true; |
| 939 | } |
| 940 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 941 | void DeviceRegistrationInfo::OnUpdateDeviceResourceDone( |
| 942 | const base::DictionaryValue& device_info, |
| 943 | ErrorPtr error) { |
| 944 | if (error) |
| 945 | return OnUpdateDeviceResourceError(std::move(error)); |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 946 | UpdateDeviceInfoTimestamp(device_info); |
Alex Vakulenko | f3a95bf | 2015-07-01 07:52:13 -0700 | [diff] [blame] | 947 | // Make a copy of the callback list so that if the callback triggers another |
| 948 | // call to UpdateDeviceResource(), we do not modify the list we are iterating |
| 949 | // over. |
| 950 | auto callback_list = std::move(in_progress_resource_update_callbacks_); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 951 | for (const auto& callback : callback_list) |
| 952 | callback.Run(nullptr); |
Alex Vakulenko | f3a95bf | 2015-07-01 07:52:13 -0700 | [diff] [blame] | 953 | StartQueuedUpdateDeviceResource(); |
| 954 | } |
| 955 | |
Vitaly Buka | f7f52d4 | 2015-10-10 22:43:55 -0700 | [diff] [blame] | 956 | void DeviceRegistrationInfo::OnUpdateDeviceResourceError(ErrorPtr error) { |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 957 | if (error->HasError(kErrorDomainGCDServer, "invalid_last_update_time_ms")) { |
| 958 | // If the server rejected our previous request, retrieve the latest |
| 959 | // timestamp from the server and retry. |
| 960 | VLOG(1) << "Getting the last device resource timestamp from server..."; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 961 | GetDeviceInfo(base::Bind(&DeviceRegistrationInfo::OnDeviceInfoRetrieved, |
| 962 | AsWeakPtr())); |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 963 | return; |
| 964 | } |
| 965 | |
Alex Vakulenko | f3a95bf | 2015-07-01 07:52:13 -0700 | [diff] [blame] | 966 | // Make a copy of the callback list so that if the callback triggers another |
| 967 | // call to UpdateDeviceResource(), we do not modify the list we are iterating |
| 968 | // over. |
| 969 | auto callback_list = std::move(in_progress_resource_update_callbacks_); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 970 | for (const auto& callback : callback_list) |
| 971 | callback.Run(error->Clone()); |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 972 | |
Alex Vakulenko | f3a95bf | 2015-07-01 07:52:13 -0700 | [diff] [blame] | 973 | StartQueuedUpdateDeviceResource(); |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 974 | } |
Anton Muhin | a34f0d9 | 2014-10-03 21:09:40 +0400 | [diff] [blame] | 975 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 976 | void DeviceRegistrationInfo::OnFetchCommandsDone( |
| 977 | const base::Callback<void(const base::ListValue&, ErrorPtr)>& callback, |
| 978 | const base::DictionaryValue& json, |
| 979 | ErrorPtr error) { |
Alex Vakulenko | 64bc9ea | 2015-09-14 09:24:58 -0700 | [diff] [blame] | 980 | OnFetchCommandsReturned(); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 981 | if (error) |
| 982 | return callback.Run({}, std::move(error)); |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 983 | const base::ListValue* commands{nullptr}; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 984 | if (!json.GetList("commands", &commands)) |
Alex Vakulenko | ed77a57 | 2015-07-15 10:55:43 -0700 | [diff] [blame] | 985 | VLOG(2) << "No commands in the response."; |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 986 | const base::ListValue empty; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 987 | callback.Run(commands ? *commands : empty, nullptr); |
Alex Vakulenko | 64bc9ea | 2015-09-14 09:24:58 -0700 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | void DeviceRegistrationInfo::OnFetchCommandsReturned() { |
| 991 | fetch_commands_request_sent_ = false; |
| 992 | // If we have additional requests queued, send them out now. |
| 993 | if (fetch_commands_request_queued_) |
Alex Vakulenko | e07c29d | 2015-10-22 10:31:12 -0700 | [diff] [blame] | 994 | FetchAndPublishCommands(queued_fetch_reason_); |
Alex Vakulenko | 64bc9ea | 2015-09-14 09:24:58 -0700 | [diff] [blame] | 995 | } |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 996 | |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 997 | void DeviceRegistrationInfo::FetchCommands( |
Alex Vakulenko | e07c29d | 2015-10-22 10:31:12 -0700 | [diff] [blame] | 998 | const base::Callback<void(const base::ListValue&, ErrorPtr)>& callback, |
| 999 | const std::string& reason) { |
Alex Vakulenko | 64bc9ea | 2015-09-14 09:24:58 -0700 | [diff] [blame] | 1000 | fetch_commands_request_sent_ = true; |
| 1001 | fetch_commands_request_queued_ = false; |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 1002 | DoCloudRequest( |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 1003 | HttpClient::Method::kGet, |
Alex Vakulenko | e07c29d | 2015-10-22 10:31:12 -0700 | [diff] [blame] | 1004 | GetServiceURL("commands/queue", {{"deviceId", GetSettings().cloud_id}, |
| 1005 | {"reason", reason}}), |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 1006 | nullptr, base::Bind(&DeviceRegistrationInfo::OnFetchCommandsDone, |
| 1007 | AsWeakPtr(), callback)); |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 1008 | } |
Anton Muhin | a34f0d9 | 2014-10-03 21:09:40 +0400 | [diff] [blame] | 1009 | |
Alex Vakulenko | e07c29d | 2015-10-22 10:31:12 -0700 | [diff] [blame] | 1010 | void DeviceRegistrationInfo::FetchAndPublishCommands( |
| 1011 | const std::string& reason) { |
Alex Vakulenko | 64bc9ea | 2015-09-14 09:24:58 -0700 | [diff] [blame] | 1012 | if (fetch_commands_request_sent_) { |
| 1013 | fetch_commands_request_queued_ = true; |
Alex Vakulenko | e07c29d | 2015-10-22 10:31:12 -0700 | [diff] [blame] | 1014 | queued_fetch_reason_ = reason; |
Alex Vakulenko | 64bc9ea | 2015-09-14 09:24:58 -0700 | [diff] [blame] | 1015 | return; |
| 1016 | } |
| 1017 | |
Alex Vakulenko | 1038ec1 | 2015-07-15 14:36:13 -0700 | [diff] [blame] | 1018 | FetchCommands(base::Bind(&DeviceRegistrationInfo::PublishCommands, |
Alex Vakulenko | e07c29d | 2015-10-22 10:31:12 -0700 | [diff] [blame] | 1019 | weak_factory_.GetWeakPtr()), |
| 1020 | reason); |
Alex Vakulenko | 1038ec1 | 2015-07-15 14:36:13 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 1023 | void DeviceRegistrationInfo::ProcessInitialCommandList( |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 1024 | const base::ListValue& commands, |
| 1025 | ErrorPtr error) { |
| 1026 | if (error) |
| 1027 | return; |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 1028 | for (const base::Value* command : commands) { |
| 1029 | const base::DictionaryValue* command_dict{nullptr}; |
| 1030 | if (!command->GetAsDictionary(&command_dict)) { |
| 1031 | LOG(WARNING) << "Not a command dictionary: " << *command; |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 1032 | continue; |
Anton Muhin | a34f0d9 | 2014-10-03 21:09:40 +0400 | [diff] [blame] | 1033 | } |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 1034 | std::string command_state; |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 1035 | if (!command_dict->GetString("state", &command_state)) { |
| 1036 | LOG(WARNING) << "Command with no state at " << *command; |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 1037 | continue; |
| 1038 | } |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 1039 | if (command_state == "error" && command_state == "inProgress" && |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 1040 | command_state == "paused") { |
| 1041 | // It's a limbo command, abort it. |
| 1042 | std::string command_id; |
| 1043 | if (!command_dict->GetString("id", &command_id)) { |
| 1044 | LOG(WARNING) << "Command with no ID at " << *command; |
| 1045 | continue; |
| 1046 | } |
Anton Muhin | 6d2569e | 2014-10-30 12:32:27 +0400 | [diff] [blame] | 1047 | |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 1048 | std::unique_ptr<base::DictionaryValue> cmd_copy{command_dict->DeepCopy()}; |
| 1049 | cmd_copy->SetString("state", "aborted"); |
| 1050 | // TODO(wiley) We could consider handling this error case more gracefully. |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 1051 | DoCloudRequest(HttpClient::Method::kPut, |
| 1052 | GetServiceURL("commands/" + command_id), cmd_copy.get(), |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 1053 | base::Bind(&IgnoreCloudResult)); |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 1054 | } else { |
| 1055 | // Normal command, publish it to local clients. |
| 1056 | PublishCommand(*command_dict); |
| 1057 | } |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 1058 | } |
Anton Muhin | d07e206 | 2014-10-27 10:53:29 +0400 | [diff] [blame] | 1059 | } |
| 1060 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 1061 | void DeviceRegistrationInfo::PublishCommands(const base::ListValue& commands, |
| 1062 | ErrorPtr error) { |
| 1063 | if (error) |
| 1064 | return; |
Alex Vakulenko | 6e3c30e | 2015-05-21 17:39:25 -0700 | [diff] [blame] | 1065 | for (const base::Value* command : commands) { |
| 1066 | const base::DictionaryValue* command_dict{nullptr}; |
| 1067 | if (!command->GetAsDictionary(&command_dict)) { |
| 1068 | LOG(WARNING) << "Not a command dictionary: " << *command; |
Anton Muhin | d07e206 | 2014-10-27 10:53:29 +0400 | [diff] [blame] | 1069 | continue; |
| 1070 | } |
Alex Vakulenko | 6e3c30e | 2015-05-21 17:39:25 -0700 | [diff] [blame] | 1071 | PublishCommand(*command_dict); |
| 1072 | } |
| 1073 | } |
Anton Muhin | d07e206 | 2014-10-27 10:53:29 +0400 | [diff] [blame] | 1074 | |
Alex Vakulenko | 6e3c30e | 2015-05-21 17:39:25 -0700 | [diff] [blame] | 1075 | void DeviceRegistrationInfo::PublishCommand( |
| 1076 | const base::DictionaryValue& command) { |
| 1077 | std::string command_id; |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 1078 | ErrorPtr error; |
Alex Vakulenko | 6e3c30e | 2015-05-21 17:39:25 -0700 | [diff] [blame] | 1079 | auto command_instance = CommandInstance::FromJson( |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 1080 | &command, Command::Origin::kCloud, |
| 1081 | command_manager_->GetCommandDictionary(), &command_id, &error); |
Alex Vakulenko | 6e3c30e | 2015-05-21 17:39:25 -0700 | [diff] [blame] | 1082 | if (!command_instance) { |
| 1083 | LOG(WARNING) << "Failed to parse a command instance: " << command; |
| 1084 | if (!command_id.empty()) |
| 1085 | NotifyCommandAborted(command_id, std::move(error)); |
| 1086 | return; |
| 1087 | } |
Anton Muhin | d07e206 | 2014-10-27 10:53:29 +0400 | [diff] [blame] | 1088 | |
Alex Vakulenko | 6e3c30e | 2015-05-21 17:39:25 -0700 | [diff] [blame] | 1089 | // TODO(antonm): Properly process cancellation of commands. |
| 1090 | if (!command_manager_->FindCommand(command_instance->GetID())) { |
| 1091 | LOG(INFO) << "New command '" << command_instance->GetName() |
| 1092 | << "' arrived, ID: " << command_instance->GetID(); |
Vitaly Buka | 0f80f7c | 2015-08-13 00:57:25 -0700 | [diff] [blame] | 1093 | std::unique_ptr<BackoffEntry> backoff_entry{ |
| 1094 | new BackoffEntry{cloud_backoff_policy_.get()}}; |
Vitaly Buka | 157b16a | 2015-07-31 16:20:48 -0700 | [diff] [blame] | 1095 | std::unique_ptr<CloudCommandProxy> cloud_proxy{new CloudCommandProxy{ |
Vitaly Buka | 0d377a4 | 2015-07-21 10:26:08 -0700 | [diff] [blame] | 1096 | command_instance.get(), this, state_manager_->GetStateChangeQueue(), |
| 1097 | std::move(backoff_entry), task_runner_}}; |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 1098 | // CloudCommandProxy::CloudCommandProxy() subscribe itself to Command |
| 1099 | // notifications. When Command is being destroyed it sends |
Vitaly Buka | 157b16a | 2015-07-31 16:20:48 -0700 | [diff] [blame] | 1100 | // ::OnCommandDestroyed() and CloudCommandProxy deletes itself. |
| 1101 | cloud_proxy.release(); |
Alex Vakulenko | 6e3c30e | 2015-05-21 17:39:25 -0700 | [diff] [blame] | 1102 | command_manager_->AddCommand(std::move(command_instance)); |
Anton Muhin | d07e206 | 2014-10-27 10:53:29 +0400 | [diff] [blame] | 1103 | } |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 1104 | } |
| 1105 | |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 1106 | void DeviceRegistrationInfo::PublishStateUpdates() { |
Alex Vakulenko | 93ba0bd | 2015-06-19 14:06:46 -0700 | [diff] [blame] | 1107 | // If we have pending state update requests, don't send any more for now. |
| 1108 | if (device_state_update_pending_) |
| 1109 | return; |
| 1110 | |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 1111 | StateChangeQueueInterface::UpdateID update_id = 0; |
| 1112 | std::vector<StateChange> state_changes; |
| 1113 | std::tie(update_id, state_changes) = |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 1114 | state_manager_->GetAndClearRecordedStateChanges(); |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 1115 | if (state_changes.empty()) |
| 1116 | return; |
| 1117 | |
| 1118 | std::unique_ptr<base::ListValue> patches{new base::ListValue}; |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 1119 | for (auto& state_change : state_changes) { |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 1120 | std::unique_ptr<base::DictionaryValue> patch{new base::DictionaryValue}; |
Anton Muhin | 76933fd | 2014-11-21 21:25:18 +0400 | [diff] [blame] | 1121 | patch->SetString("timeMs", |
| 1122 | std::to_string(state_change.timestamp.ToJavaTime())); |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 1123 | patch->Set("patch", state_change.changed_properties.release()); |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 1124 | patches->Append(patch.release()); |
| 1125 | } |
| 1126 | |
| 1127 | base::DictionaryValue body; |
Anton Muhin | 76933fd | 2014-11-21 21:25:18 +0400 | [diff] [blame] | 1128 | body.SetString("requestTimeMs", |
| 1129 | std::to_string(base::Time::Now().ToJavaTime())); |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 1130 | body.Set("patches", patches.release()); |
| 1131 | |
Alex Vakulenko | 93ba0bd | 2015-06-19 14:06:46 -0700 | [diff] [blame] | 1132 | device_state_update_pending_ = true; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 1133 | DoCloudRequest(HttpClient::Method::kPost, GetDeviceURL("patchState"), &body, |
| 1134 | base::Bind(&DeviceRegistrationInfo::OnPublishStateDone, |
| 1135 | AsWeakPtr(), update_id)); |
Alex Vakulenko | 93ba0bd | 2015-06-19 14:06:46 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 1138 | void DeviceRegistrationInfo::OnPublishStateDone( |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 1139 | StateChangeQueueInterface::UpdateID update_id, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 1140 | const base::DictionaryValue& reply, |
| 1141 | ErrorPtr error) { |
Alex Vakulenko | 93ba0bd | 2015-06-19 14:06:46 -0700 | [diff] [blame] | 1142 | device_state_update_pending_ = false; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 1143 | if (error) { |
| 1144 | LOG(ERROR) << "Permanent failure while trying to update device state"; |
| 1145 | return; |
| 1146 | } |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 1147 | state_manager_->NotifyStateUpdatedOnServer(update_id); |
Alex Vakulenko | 93ba0bd | 2015-06-19 14:06:46 -0700 | [diff] [blame] | 1148 | // See if there were more pending state updates since the previous request |
| 1149 | // had been sent out. |
| 1150 | PublishStateUpdates(); |
| 1151 | } |
| 1152 | |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 1153 | void DeviceRegistrationInfo::SetGcdState(GcdState new_state) { |
| 1154 | VLOG_IF(1, new_state != gcd_state_) << "Changing registration status to " |
| 1155 | << EnumToString(new_state); |
| 1156 | gcd_state_ = new_state; |
| 1157 | for (const auto& cb : gcd_state_changed_callbacks_) |
| 1158 | cb.Run(gcd_state_); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 1159 | } |
| 1160 | |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 1161 | void DeviceRegistrationInfo::OnCommandDefsChanged() { |
| 1162 | VLOG(1) << "CommandDefinitionChanged notification received"; |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 1163 | if (!HaveRegistrationCredentials() || !connected_to_cloud_) |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 1164 | return; |
| 1165 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 1166 | UpdateDeviceResource(base::Bind(&IgnoreCloudError)); |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
Vitaly Buka | c903d28 | 2015-05-26 17:03:08 -0700 | [diff] [blame] | 1169 | void DeviceRegistrationInfo::OnStateChanged() { |
| 1170 | VLOG(1) << "StateChanged notification received"; |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 1171 | if (!HaveRegistrationCredentials() || !connected_to_cloud_) |
Vitaly Buka | c903d28 | 2015-05-26 17:03:08 -0700 | [diff] [blame] | 1172 | return; |
| 1173 | |
| 1174 | // TODO(vitalybuka): Integrate BackoffEntry. |
| 1175 | PublishStateUpdates(); |
| 1176 | } |
| 1177 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 1178 | void DeviceRegistrationInfo::OnConnected(const std::string& channel_name) { |
| 1179 | LOG(INFO) << "Notification channel successfully established over " |
| 1180 | << channel_name; |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 1181 | CHECK_EQ(primary_notification_channel_->GetName(), channel_name); |
Alex Vakulenko | 6b028ae | 2015-05-29 09:38:59 -0700 | [diff] [blame] | 1182 | notification_channel_starting_ = false; |
Vitaly Buka | 41a90d6 | 2015-09-29 16:58:39 -0700 | [diff] [blame] | 1183 | pull_channel_->UpdatePullInterval( |
Alex Vakulenko | c1fc90c | 2015-10-22 08:00:43 -0700 | [diff] [blame] | 1184 | base::TimeDelta::FromMinutes(kBackupPollingPeriodMinutes)); |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 1185 | current_notification_channel_ = primary_notification_channel_.get(); |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 1186 | |
Alex Vakulenko | 8b096cc | 2015-08-03 10:37:55 -0700 | [diff] [blame] | 1187 | // If we have not successfully connected to the cloud server and we have not |
| 1188 | // initiated the first device resource update, there is nothing we need to |
| 1189 | // do now to update the server of the notification channel change. |
| 1190 | if (!connected_to_cloud_ && in_progress_resource_update_callbacks_.empty()) |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 1191 | return; |
| 1192 | |
Alex Vakulenko | 1038ec1 | 2015-07-15 14:36:13 -0700 | [diff] [blame] | 1193 | // Once we update the device resource with the new notification channel, |
| 1194 | // do the last poll for commands from the server, to make sure we have the |
| 1195 | // latest command baseline and no other commands have been queued between |
| 1196 | // the moment of the last poll and the time we successfully told the server |
| 1197 | // to send new commands over the new notification channel. |
| 1198 | UpdateDeviceResource( |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 1199 | base::Bind(&IgnoreCloudErrorWithCallback, |
| 1200 | base::Bind(&DeviceRegistrationInfo::FetchAndPublishCommands, |
Alex Vakulenko | e07c29d | 2015-10-22 10:31:12 -0700 | [diff] [blame] | 1201 | AsWeakPtr(), fetch_reason::kRegularPull))); |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | void DeviceRegistrationInfo::OnDisconnected() { |
| 1205 | LOG(INFO) << "Notification channel disconnected"; |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 1206 | if (!HaveRegistrationCredentials() || !connected_to_cloud_) |
Alex Vakulenko | 3fa42ae | 2015-06-23 15:12:22 -0700 | [diff] [blame] | 1207 | return; |
| 1208 | |
Vitaly Buka | 41a90d6 | 2015-09-29 16:58:39 -0700 | [diff] [blame] | 1209 | pull_channel_->UpdatePullInterval( |
| 1210 | base::TimeDelta::FromSeconds(kPollingPeriodSeconds)); |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 1211 | current_notification_channel_ = pull_channel_.get(); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 1212 | UpdateDeviceResource(base::Bind(&IgnoreCloudError)); |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | void DeviceRegistrationInfo::OnPermanentFailure() { |
| 1216 | LOG(ERROR) << "Failed to establish notification channel."; |
Alex Vakulenko | 6b028ae | 2015-05-29 09:38:59 -0700 | [diff] [blame] | 1217 | notification_channel_starting_ = false; |
Vitaly Buka | 4ebd329 | 2015-09-23 18:04:17 -0700 | [diff] [blame] | 1218 | RefreshAccessToken( |
Vitaly Buka | 4ebd329 | 2015-09-23 18:04:17 -0700 | [diff] [blame] | 1219 | base::Bind(&DeviceRegistrationInfo::CheckAccessTokenError, AsWeakPtr())); |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
Alex Vakulenko | 6e3c30e | 2015-05-21 17:39:25 -0700 | [diff] [blame] | 1222 | void DeviceRegistrationInfo::OnCommandCreated( |
Alex Vakulenko | e07c29d | 2015-10-22 10:31:12 -0700 | [diff] [blame] | 1223 | const base::DictionaryValue& command, |
| 1224 | const std::string& channel_name) { |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 1225 | if (!connected_to_cloud_) |
| 1226 | return; |
| 1227 | |
Vitaly Buka | 7a35005 | 2015-10-10 23:58:20 -0700 | [diff] [blame] | 1228 | VLOG(1) << "Command notification received: " << command; |
| 1229 | |
Alex Vakulenko | 6e3c30e | 2015-05-21 17:39:25 -0700 | [diff] [blame] | 1230 | if (!command.empty()) { |
| 1231 | // GCD spec indicates that the command parameter in notification object |
| 1232 | // "may be empty if command size is too big". |
| 1233 | PublishCommand(command); |
| 1234 | return; |
| 1235 | } |
Alex Vakulenko | e07c29d | 2015-10-22 10:31:12 -0700 | [diff] [blame] | 1236 | |
| 1237 | // If this request comes from a Pull channel while the primary notification |
| 1238 | // channel (XMPP) is active, we are doing a backup poll, so mark the request |
| 1239 | // appropriately. |
| 1240 | bool just_in_case = |
| 1241 | (channel_name == kPullChannelName) && |
| 1242 | (current_notification_channel_ == primary_notification_channel_.get()); |
| 1243 | |
| 1244 | std::string reason = |
| 1245 | just_in_case ? fetch_reason::kJustInCase : fetch_reason::kNewCommand; |
| 1246 | |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 1247 | // If the command was too big to be delivered over a notification channel, |
| 1248 | // or OnCommandCreated() was initiated from the Pull notification, |
| 1249 | // perform a manual command fetch from the server here. |
Alex Vakulenko | e07c29d | 2015-10-22 10:31:12 -0700 | [diff] [blame] | 1250 | FetchAndPublishCommands(reason); |
Alex Vakulenko | 6e3c30e | 2015-05-21 17:39:25 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
Johan Euphrosine | 312c2f5 | 2015-09-29 00:04:29 -0700 | [diff] [blame] | 1253 | void DeviceRegistrationInfo::OnDeviceDeleted(const std::string& cloud_id) { |
| 1254 | if (cloud_id != GetSettings().cloud_id) { |
| 1255 | LOG(WARNING) << "Unexpected device deletion notification for cloud ID '" |
| 1256 | << cloud_id << "'"; |
Alex Vakulenko | 6b40d8f | 2015-06-24 11:44:22 -0700 | [diff] [blame] | 1257 | return; |
| 1258 | } |
Vitaly Buka | 672634b | 2015-11-20 09:49:30 -0800 | [diff] [blame] | 1259 | RemoveCredentials(); |
Alex Vakulenko | 6b40d8f | 2015-06-24 11:44:22 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
Vitaly Buka | 672634b | 2015-11-20 09:49:30 -0800 | [diff] [blame] | 1262 | void DeviceRegistrationInfo::RemoveCredentials() { |
Alex Vakulenko | 3fa42ae | 2015-06-23 15:12:22 -0700 | [diff] [blame] | 1263 | if (!HaveRegistrationCredentials()) |
| 1264 | return; |
| 1265 | |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 1266 | connected_to_cloud_ = false; |
| 1267 | |
Alex Vakulenko | 3fa42ae | 2015-06-23 15:12:22 -0700 | [diff] [blame] | 1268 | LOG(INFO) << "Device is unregistered from the cloud. Deleting credentials"; |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 1269 | Config::Transaction change{config_.get()}; |
Vitaly Buka | 672634b | 2015-11-20 09:49:30 -0800 | [diff] [blame] | 1270 | // Keep cloud_id to switch to detect kInvalidCredentials after restart. |
Alex Vakulenko | 3fa42ae | 2015-06-23 15:12:22 -0700 | [diff] [blame] | 1271 | change.set_robot_account(""); |
| 1272 | change.set_refresh_token(""); |
| 1273 | change.Commit(); |
| 1274 | |
| 1275 | current_notification_channel_ = nullptr; |
| 1276 | if (primary_notification_channel_) { |
| 1277 | primary_notification_channel_->Stop(); |
| 1278 | primary_notification_channel_.reset(); |
| 1279 | } |
| 1280 | if (pull_channel_) { |
| 1281 | pull_channel_->Stop(); |
| 1282 | pull_channel_.reset(); |
| 1283 | } |
| 1284 | notification_channel_starting_ = false; |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 1285 | SetGcdState(GcdState::kInvalidCredentials); |
Alex Vakulenko | 3fa42ae | 2015-06-23 15:12:22 -0700 | [diff] [blame] | 1286 | } |
Alex Vakulenko | 6e3c30e | 2015-05-21 17:39:25 -0700 | [diff] [blame] | 1287 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 1288 | } // namespace weave |