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