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