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 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 5 | #ifndef LIBWEAVE_SRC_DEVICE_REGISTRATION_INFO_H_ |
| 6 | #define LIBWEAVE_SRC_DEVICE_REGISTRATION_INFO_H_ |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 7 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 8 | #include <map> |
| 9 | #include <memory> |
Alex Vakulenko | 5841c30 | 2014-07-23 10:49:49 -0700 | [diff] [blame] | 10 | #include <string> |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 11 | #include <utility> |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 12 | #include <vector> |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 13 | |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 14 | #include <base/callback.h> |
Alex Vakulenko | 132617a | 2014-09-04 08:59:43 -0700 | [diff] [blame] | 15 | #include <base/macros.h> |
Christopher Wiley | cd41966 | 2015-02-06 17:51:43 -0800 | [diff] [blame] | 16 | #include <base/memory/weak_ptr.h> |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 17 | #include <base/time/time.h> |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 18 | #include <weave/device.h> |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 19 | #include <weave/error.h> |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 20 | #include <weave/provider/http_client.h> |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 21 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 22 | #include "src/backoff_entry.h" |
| 23 | #include "src/commands/cloud_command_update_interface.h" |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 24 | #include "src/component_manager.h" |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 25 | #include "src/config.h" |
| 26 | #include "src/data_encoding.h" |
| 27 | #include "src/notification/notification_channel.h" |
| 28 | #include "src/notification/notification_delegate.h" |
| 29 | #include "src/notification/pull_channel.h" |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 30 | |
| 31 | namespace base { |
Vitaly Buka | 6ca3ad6 | 2015-03-11 17:03:23 -0700 | [diff] [blame] | 32 | class DictionaryValue; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 33 | } // namespace base |
| 34 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 35 | namespace weave { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 36 | |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 37 | class StateManager; |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 38 | |
| 39 | namespace provider { |
| 40 | class Network; |
Vitaly Buka | 823fdda | 2015-08-13 00:33:00 -0700 | [diff] [blame] | 41 | class TaskRunner; |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 42 | } |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 43 | |
Vitaly Buka | 3d851b4 | 2015-12-08 16:17:01 -0800 | [diff] [blame] | 44 | namespace privet { |
| 45 | class AuthManager; |
| 46 | } |
| 47 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 48 | extern const char kErrorDomainOAuth2[]; |
| 49 | extern const char kErrorDomainGCD[]; |
| 50 | extern const char kErrorDomainGCDServer[]; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 51 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 52 | // The DeviceRegistrationInfo class represents device registration information. |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 53 | class DeviceRegistrationInfo : public NotificationDelegate, |
Alex Vakulenko | fe53f61 | 2015-06-26 09:05:15 -0700 | [diff] [blame] | 54 | public CloudCommandUpdateInterface { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 55 | public: |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 56 | using CloudRequestDoneCallback = |
| 57 | base::Callback<void(const base::DictionaryValue& response, |
| 58 | ErrorPtr error)>; |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 59 | |
Vitaly Buka | 666b43e | 2015-12-08 16:35:47 -0800 | [diff] [blame] | 60 | DeviceRegistrationInfo(Config* config, |
| 61 | ComponentManager* component_manager, |
Vitaly Buka | 3d851b4 | 2015-12-08 16:17:01 -0800 | [diff] [blame] | 62 | provider::TaskRunner* task_runner, |
| 63 | provider::HttpClient* http_client, |
| 64 | provider::Network* network, |
| 65 | privet::AuthManager* auth_manager); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 66 | |
Alex Vakulenko | 534a312 | 2015-05-22 15:48:53 -0700 | [diff] [blame] | 67 | ~DeviceRegistrationInfo() override; |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 68 | |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 69 | void AddGcdStateChangedCallback( |
| 70 | const Device::GcdStateChangedCallback& callback); |
Vitaly Buka | 12870bd | 2015-10-08 23:49:39 -0700 | [diff] [blame] | 71 | void RegisterDevice(const std::string& ticket_id, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 72 | const DoneCallback& callback); |
Vitaly Buka | 9aa15c7 | 2015-10-01 19:14:44 -0700 | [diff] [blame] | 73 | |
Vitaly Buka | b624bc4 | 2015-09-29 19:13:55 -0700 | [diff] [blame] | 74 | void UpdateDeviceInfo(const std::string& name, |
Vitaly Buka | 8b4ab96 | 2015-07-14 19:19:39 -0700 | [diff] [blame] | 75 | const std::string& description, |
Vitaly Buka | 9aa15c7 | 2015-10-01 19:14:44 -0700 | [diff] [blame] | 76 | const std::string& location); |
Vitaly Buka | b624bc4 | 2015-09-29 19:13:55 -0700 | [diff] [blame] | 77 | void UpdateBaseConfig(AuthScope anonymous_access_role, |
Vitaly Buka | 8b4ab96 | 2015-07-14 19:19:39 -0700 | [diff] [blame] | 78 | bool local_discovery_enabled, |
Vitaly Buka | 9aa15c7 | 2015-10-01 19:14:44 -0700 | [diff] [blame] | 79 | bool local_pairing_enabled); |
Vitaly Buka | 8b4ab96 | 2015-07-14 19:19:39 -0700 | [diff] [blame] | 80 | bool UpdateServiceConfig(const std::string& client_id, |
| 81 | const std::string& client_secret, |
| 82 | const std::string& api_key, |
| 83 | const std::string& oauth_url, |
| 84 | const std::string& service_url, |
Vitaly Buka | 9aa15c7 | 2015-10-01 19:14:44 -0700 | [diff] [blame] | 85 | ErrorPtr* error); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 86 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 87 | void GetDeviceInfo(const CloudRequestDoneCallback& callback); |
Vitaly Buka | ee3e36d | 2015-10-01 18:18:58 -0700 | [diff] [blame] | 88 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 89 | // Returns the GCD service request URL. If |subpath| is specified, it is |
| 90 | // appended to the base URL which is normally |
Vitaly Buka | 88272d5 | 2015-11-17 17:04:01 -0800 | [diff] [blame] | 91 | // https://www.googleapis.com/weave/v1/". |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 92 | // If |params| are specified, each key-value pair is formatted using |
Vitaly Buka | 7d55639 | 2015-08-13 20:06:48 -0700 | [diff] [blame] | 93 | // WebParamsEncode() and appended to URL as a query |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 94 | // string. |
| 95 | // So, calling: |
| 96 | // GetServiceURL("ticket", {{"key","apiKey"}}) |
| 97 | // will return something like: |
Vitaly Buka | 88272d5 | 2015-11-17 17:04:01 -0800 | [diff] [blame] | 98 | // https://www.googleapis.com/weave/v1/ticket?key=apiKey |
Vitaly Buka | 7d55639 | 2015-08-13 20:06:48 -0700 | [diff] [blame] | 99 | std::string GetServiceURL(const std::string& subpath = {}, |
| 100 | const WebParamList& params = {}) const; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 101 | |
| 102 | // Returns a service URL to access the registered device on GCD server. |
| 103 | // The base URL used to construct the full URL looks like this: |
Vitaly Buka | 88272d5 | 2015-11-17 17:04:01 -0800 | [diff] [blame] | 104 | // https://www.googleapis.com/weave/v1/devices/<cloud_id>/ |
Vitaly Buka | 7d55639 | 2015-08-13 20:06:48 -0700 | [diff] [blame] | 105 | std::string GetDeviceURL(const std::string& subpath = {}, |
| 106 | const WebParamList& params = {}) const; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 107 | |
| 108 | // Similar to GetServiceURL, GetOAuthURL() returns a URL of OAuth 2.0 server. |
| 109 | // The base URL used is https://accounts.google.com/o/oauth2/. |
Vitaly Buka | 7d55639 | 2015-08-13 20:06:48 -0700 | [diff] [blame] | 110 | std::string GetOAuthURL(const std::string& subpath = {}, |
| 111 | const WebParamList& params = {}) const; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 112 | |
Alex Vakulenko | 6e3c30e | 2015-05-21 17:39:25 -0700 | [diff] [blame] | 113 | // Starts GCD device if credentials available. |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 114 | void Start(); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 115 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 116 | // Updates a command (override from CloudCommandUpdateInterface). |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 117 | void UpdateCommand(const std::string& command_id, |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 118 | const base::DictionaryValue& command_patch, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 119 | const DoneCallback& callback) override; |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 120 | |
Vitaly Buka | b56872f | 2015-06-21 18:39:34 -0700 | [diff] [blame] | 121 | // TODO(vitalybuka): remove getters and pass config to dependent code. |
Vitaly Buka | 238db69 | 2015-09-29 16:58:39 -0700 | [diff] [blame] | 122 | const Config::Settings& GetSettings() const { return config_->GetSettings(); } |
Vitaly Buka | 666b43e | 2015-12-08 16:35:47 -0800 | [diff] [blame] | 123 | Config* GetMutableConfig() { return config_; } |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 124 | |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 125 | GcdState GetGcdState() const { return gcd_state_; } |
| 126 | |
Vitaly Buka | 4c808b2 | 2015-08-02 13:44:52 -0700 | [diff] [blame] | 127 | private: |
| 128 | friend class DeviceRegistrationInfoTest; |
| 129 | |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 130 | base::WeakPtr<DeviceRegistrationInfo> AsWeakPtr() { |
| 131 | return weak_factory_.GetWeakPtr(); |
| 132 | } |
| 133 | |
Vitaly Buka | 672634b | 2015-11-20 09:49:30 -0800 | [diff] [blame] | 134 | // Checks whether we have credentials generated during registration. |
| 135 | bool HaveRegistrationCredentials() const; |
| 136 | // Calls HaveRegistrationCredentials() and logs an error if no credentials |
| 137 | // are available. |
| 138 | bool VerifyRegistrationCredentials(ErrorPtr* error) const; |
| 139 | |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 140 | // Cause DeviceRegistrationInfo to attempt to connect to cloud server on |
| 141 | // its own later. |
| 142 | void ScheduleCloudConnection(const base::TimeDelta& delay); |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 143 | |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 144 | // Initiates the connection to the cloud server. |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 145 | // Device will do required start up chores and then start to listen |
| 146 | // to new commands. |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 147 | void ConnectToCloud(ErrorPtr error); |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 148 | // Notification called when ConnectToCloud() succeeds. |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 149 | void OnConnectedToCloud(ErrorPtr error); |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 150 | |
David Zeuthen | 390d191 | 2015-03-03 14:54:48 -0500 | [diff] [blame] | 151 | // Forcibly refreshes the access token. |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 152 | void RefreshAccessToken(const DoneCallback& callback); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 153 | |
Alex Vakulenko | 0f91be8 | 2015-07-27 10:11:53 -0700 | [diff] [blame] | 154 | // Callbacks for RefreshAccessToken(). |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 155 | void OnRefreshAccessTokenDone( |
| 156 | const DoneCallback& callback, |
| 157 | std::unique_ptr<provider::HttpClient::Response> response, |
Vitaly Buka | f7f52d4 | 2015-10-10 22:43:55 -0700 | [diff] [blame] | 158 | ErrorPtr error); |
Alex Vakulenko | 6b028ae | 2015-05-29 09:38:59 -0700 | [diff] [blame] | 159 | |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 160 | // Parse the OAuth response, and sets registration status to |
| 161 | // kInvalidCredentials if our registration is no longer valid. |
| 162 | std::unique_ptr<base::DictionaryValue> ParseOAuthResponse( |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 163 | const provider::HttpClient::Response& response, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 164 | ErrorPtr* error); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 165 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 166 | // This attempts to open a notification channel. The channel needs to be |
Nathan Bullock | bea9113 | 2015-02-19 09:13:33 -0500 | [diff] [blame] | 167 | // restarted anytime the access_token is refreshed. |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 168 | void StartNotificationChannel(); |
Nathan Bullock | bea9113 | 2015-02-19 09:13:33 -0500 | [diff] [blame] | 169 | |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 170 | // Do a HTTPS request to cloud services. |
| 171 | // Handles many cases like reauthorization, 5xx HTTP response codes |
| 172 | // and device removal. It is a recommended way to do cloud API |
| 173 | // requests. |
| 174 | // TODO(antonm): Consider moving into some other class. |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 175 | void DoCloudRequest(provider::HttpClient::Method method, |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 176 | const std::string& url, |
| 177 | const base::DictionaryValue* body, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 178 | const CloudRequestDoneCallback& callback); |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 179 | |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 180 | // Helper for DoCloudRequest(). |
| 181 | struct CloudRequestData { |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 182 | provider::HttpClient::Method method; |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 183 | std::string url; |
| 184 | std::string body; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 185 | CloudRequestDoneCallback callback; |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 186 | }; |
| 187 | void SendCloudRequest(const std::shared_ptr<const CloudRequestData>& data); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 188 | void OnCloudRequestDone( |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 189 | const std::shared_ptr<const CloudRequestData>& data, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 190 | std::unique_ptr<provider::HttpClient::Response> response, |
| 191 | ErrorPtr error); |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 192 | void RetryCloudRequest(const std::shared_ptr<const CloudRequestData>& data); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 193 | void OnAccessTokenRefreshed( |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 194 | const std::shared_ptr<const CloudRequestData>& data, |
| 195 | ErrorPtr error); |
Vitaly Buka | f7f52d4 | 2015-10-10 22:43:55 -0700 | [diff] [blame] | 196 | void CheckAccessTokenError(ErrorPtr error); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 197 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 198 | void UpdateDeviceResource(const DoneCallback& callback); |
Alex Vakulenko | f3a95bf | 2015-07-01 07:52:13 -0700 | [diff] [blame] | 199 | void StartQueuedUpdateDeviceResource(); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 200 | void OnUpdateDeviceResourceDone(const base::DictionaryValue& device_info, |
| 201 | ErrorPtr error); |
Vitaly Buka | f7f52d4 | 2015-10-10 22:43:55 -0700 | [diff] [blame] | 202 | void OnUpdateDeviceResourceError(ErrorPtr error); |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 203 | |
Vitaly Buka | 72d8d16 | 2015-12-09 18:39:36 -0800 | [diff] [blame] | 204 | void SendAuthInfo(); |
Vitaly Buka | 0c190b3 | 2015-12-14 15:22:10 -0800 | [diff] [blame^] | 205 | void OnSendAuthInfoDone(const std::vector<uint8_t>& token, |
| 206 | const base::DictionaryValue& body, |
| 207 | ErrorPtr error); |
Vitaly Buka | 72d8d16 | 2015-12-09 18:39:36 -0800 | [diff] [blame] | 208 | |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 209 | // Callback from GetDeviceInfo() to retrieve the device resource timestamp |
| 210 | // and retry UpdateDeviceResource() call. |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 211 | void OnDeviceInfoRetrieved(const base::DictionaryValue& device_info, |
| 212 | ErrorPtr error); |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 213 | |
| 214 | // Extracts the timestamp from the device resource and sets it to |
| 215 | // |last_device_resource_updated_timestamp_|. |
| 216 | // Returns false if the "lastUpdateTimeMs" field is not found in the device |
| 217 | // resource or it is invalid. |
| 218 | bool UpdateDeviceInfoTimestamp(const base::DictionaryValue& device_info); |
| 219 | |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 220 | void FetchCommands( |
Alex Vakulenko | e07c29d | 2015-10-22 10:31:12 -0700 | [diff] [blame] | 221 | const base::Callback<void(const base::ListValue&, ErrorPtr)>& callback, |
| 222 | const std::string& reason); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 223 | void OnFetchCommandsDone( |
| 224 | const base::Callback<void(const base::ListValue&, ErrorPtr)>& callback, |
| 225 | const base::DictionaryValue& json, |
| 226 | ErrorPtr); |
Alex Vakulenko | 64bc9ea | 2015-09-14 09:24:58 -0700 | [diff] [blame] | 227 | // Called when FetchCommands completes (with either success or error). |
| 228 | // This method reschedules any pending/queued fetch requests. |
| 229 | void OnFetchCommandsReturned(); |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 230 | |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 231 | // Processes the command list that is fetched from the server on connection. |
| 232 | // Aborts commands which are in transitional states and publishes queued |
| 233 | // commands which are queued. |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 234 | void ProcessInitialCommandList(const base::ListValue& commands, |
| 235 | ErrorPtr error); |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 236 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 237 | void PublishCommands(const base::ListValue& commands, ErrorPtr error); |
Alex Vakulenko | 6e3c30e | 2015-05-21 17:39:25 -0700 | [diff] [blame] | 238 | void PublishCommand(const base::DictionaryValue& command); |
Anton Muhin | d07e206 | 2014-10-27 10:53:29 +0400 | [diff] [blame] | 239 | |
Alex Vakulenko | 1038ec1 | 2015-07-15 14:36:13 -0700 | [diff] [blame] | 240 | // Helper function to pull the pending command list from the server using |
| 241 | // FetchCommands() and make them available on D-Bus with PublishCommands(). |
Alex Vakulenko | e07c29d | 2015-10-22 10:31:12 -0700 | [diff] [blame] | 242 | // |backup_fetch| is set to true when performing backup ("just-in-case") |
| 243 | // command fetch while XMPP channel is up and running. |
| 244 | void FetchAndPublishCommands(const std::string& reason); |
Alex Vakulenko | 1038ec1 | 2015-07-15 14:36:13 -0700 | [diff] [blame] | 245 | |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 246 | void PublishStateUpdates(); |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 247 | void OnPublishStateDone(ComponentManager::UpdateID update_id, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 248 | const base::DictionaryValue& reply, |
| 249 | ErrorPtr error); |
Vitaly Buka | f7f52d4 | 2015-10-10 22:43:55 -0700 | [diff] [blame] | 250 | void OnPublishStateError(ErrorPtr error); |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 251 | |
Alex Vakulenko | d1978d3 | 2015-04-29 17:33:26 -0700 | [diff] [blame] | 252 | // If unrecoverable error occurred (e.g. error parsing command instance), |
| 253 | // notify the server that the command is aborted by the device. |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 254 | void NotifyCommandAborted(const std::string& command_id, ErrorPtr error); |
Alex Vakulenko | d1978d3 | 2015-04-29 17:33:26 -0700 | [diff] [blame] | 255 | |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 256 | // Builds Cloud API devices collection REST resource which matches |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 257 | // current state of the device including command definitions |
| 258 | // for all supported commands and current device state. |
Vitaly Buka | 1a10871 | 2015-12-06 17:41:07 -0800 | [diff] [blame] | 259 | std::unique_ptr<base::DictionaryValue> BuildDeviceResource() const; |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 260 | |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 261 | void SetGcdState(GcdState new_state); |
Johan Euphrosine | 312c2f5 | 2015-09-29 00:04:29 -0700 | [diff] [blame] | 262 | void SetDeviceId(const std::string& cloud_id); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 263 | |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 264 | // Callback called when command definitions are changed to re-publish new CDD. |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 265 | void OnTraitDefsChanged(); |
| 266 | void OnComponentTreeChanged(); |
Vitaly Buka | c903d28 | 2015-05-26 17:03:08 -0700 | [diff] [blame] | 267 | void OnStateChanged(); |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 268 | |
Alex Vakulenko | 6b40d8f | 2015-06-24 11:44:22 -0700 | [diff] [blame] | 269 | // Overrides from NotificationDelegate. |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 270 | void OnConnected(const std::string& channel_name) override; |
| 271 | void OnDisconnected() override; |
| 272 | void OnPermanentFailure() override; |
Alex Vakulenko | e07c29d | 2015-10-22 10:31:12 -0700 | [diff] [blame] | 273 | void OnCommandCreated(const base::DictionaryValue& command, |
| 274 | const std::string& channel_name) override; |
Johan Euphrosine | 312c2f5 | 2015-09-29 00:04:29 -0700 | [diff] [blame] | 275 | void OnDeviceDeleted(const std::string& cloud_id) override; |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 276 | |
Alex Vakulenko | 3fa42ae | 2015-06-23 15:12:22 -0700 | [diff] [blame] | 277 | // Wipes out the device registration information and stops server connections. |
Vitaly Buka | 672634b | 2015-11-20 09:49:30 -0800 | [diff] [blame] | 278 | void RemoveCredentials(); |
Alex Vakulenko | 3fa42ae | 2015-06-23 15:12:22 -0700 | [diff] [blame] | 279 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 280 | void RegisterDeviceError(const DoneCallback& callback, ErrorPtr error); |
Vitaly Buka | 4774df2 | 2015-10-09 12:36:22 -0700 | [diff] [blame] | 281 | void RegisterDeviceOnTicketSent( |
| 282 | const std::string& ticket_id, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 283 | const DoneCallback& callback, |
| 284 | std::unique_ptr<provider::HttpClient::Response> response, |
| 285 | ErrorPtr error); |
Vitaly Buka | 4774df2 | 2015-10-09 12:36:22 -0700 | [diff] [blame] | 286 | void RegisterDeviceOnTicketFinalized( |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 287 | const DoneCallback& callback, |
| 288 | std::unique_ptr<provider::HttpClient::Response> response, |
| 289 | ErrorPtr error); |
Vitaly Buka | 4774df2 | 2015-10-09 12:36:22 -0700 | [diff] [blame] | 290 | void RegisterDeviceOnAuthCodeSent( |
| 291 | const std::string& cloud_id, |
| 292 | const std::string& robot_account, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 293 | const DoneCallback& callback, |
| 294 | std::unique_ptr<provider::HttpClient::Response> response, |
| 295 | ErrorPtr error); |
Vitaly Buka | 4774df2 | 2015-10-09 12:36:22 -0700 | [diff] [blame] | 296 | |
Nathan Bullock | d9e0bcd | 2015-02-11 11:36:39 -0500 | [diff] [blame] | 297 | // Transient data |
| 298 | std::string access_token_; |
| 299 | base::Time access_token_expiration_; |
Alex Vakulenko | fb331ac | 2015-07-22 15:10:11 -0700 | [diff] [blame] | 300 | // The time stamp of last device resource update on the server. |
| 301 | std::string last_device_resource_updated_timestamp_; |
| 302 | // Set to true if the device has connected to the cloud server correctly. |
| 303 | // At this point, normal state and command updates can be dispatched to the |
| 304 | // server. |
| 305 | bool connected_to_cloud_{false}; |
Nathan Bullock | d9e0bcd | 2015-02-11 11:36:39 -0500 | [diff] [blame] | 306 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 307 | // HTTP transport used for communications. |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 308 | provider::HttpClient* http_client_{nullptr}; |
Vitaly Buka | 6da9425 | 2015-08-04 15:45:14 -0700 | [diff] [blame] | 309 | |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 310 | provider::TaskRunner* task_runner_{nullptr}; |
Vitaly Buka | 666b43e | 2015-12-08 16:35:47 -0800 | [diff] [blame] | 311 | |
| 312 | Config* config_{nullptr}; |
| 313 | |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 314 | // Global component manager. |
| 315 | ComponentManager* component_manager_{nullptr}; |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 316 | |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 317 | // Backoff manager for DoCloudRequest() method. |
Vitaly Buka | 0f80f7c | 2015-08-13 00:57:25 -0700 | [diff] [blame] | 318 | std::unique_ptr<BackoffEntry::Policy> cloud_backoff_policy_; |
| 319 | std::unique_ptr<BackoffEntry> cloud_backoff_entry_; |
| 320 | std::unique_ptr<BackoffEntry> oauth2_backoff_entry_; |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 321 | |
Alex Vakulenko | 93ba0bd | 2015-06-19 14:06:46 -0700 | [diff] [blame] | 322 | // Flag set to true while a device state update patch request is in flight |
| 323 | // to the cloud server. |
| 324 | bool device_state_update_pending_{false}; |
| 325 | |
Alex Vakulenko | 64bc9ea | 2015-09-14 09:24:58 -0700 | [diff] [blame] | 326 | // Set to true when command queue fetch request is in flight to the server. |
| 327 | bool fetch_commands_request_sent_{false}; |
| 328 | // Set to true when another command queue fetch request is queued while |
| 329 | // another one was in flight. |
| 330 | bool fetch_commands_request_queued_{false}; |
Alex Vakulenko | e07c29d | 2015-10-22 10:31:12 -0700 | [diff] [blame] | 331 | // Specifies the reason for queued command fetch request. |
| 332 | std::string queued_fetch_reason_; |
Alex Vakulenko | 64bc9ea | 2015-09-14 09:24:58 -0700 | [diff] [blame] | 333 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 334 | using ResourceUpdateCallbackList = std::vector<DoneCallback>; |
| 335 | // Callbacks for device resource update request currently in flight to the |
| 336 | // cloud server. |
Alex Vakulenko | f3a95bf | 2015-07-01 07:52:13 -0700 | [diff] [blame] | 337 | ResourceUpdateCallbackList in_progress_resource_update_callbacks_; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 338 | // Callbacks for device resource update requests queued while another request |
| 339 | // is in flight to the cloud server. |
Alex Vakulenko | f3a95bf | 2015-07-01 07:52:13 -0700 | [diff] [blame] | 340 | ResourceUpdateCallbackList queued_resource_update_callbacks_; |
| 341 | |
Vitaly Buka | 72d8d16 | 2015-12-09 18:39:36 -0800 | [diff] [blame] | 342 | bool auth_info_update_inprogress_{false}; |
| 343 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 344 | std::unique_ptr<NotificationChannel> primary_notification_channel_; |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 345 | std::unique_ptr<PullChannel> pull_channel_; |
| 346 | NotificationChannel* current_notification_channel_{nullptr}; |
Alex Vakulenko | 6b028ae | 2015-05-29 09:38:59 -0700 | [diff] [blame] | 347 | bool notification_channel_starting_{false}; |
Christopher Wiley | d732bd0 | 2015-04-07 11:11:18 -0700 | [diff] [blame] | 348 | |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 349 | provider::Network* network_{nullptr}; |
Vitaly Buka | 3d851b4 | 2015-12-08 16:17:01 -0800 | [diff] [blame] | 350 | privet::AuthManager* auth_manager_{nullptr}; |
Vitaly Buka | 63cc3d2 | 2015-06-23 20:11:36 -0700 | [diff] [blame] | 351 | |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 352 | // Tracks our GCD state. |
| 353 | GcdState gcd_state_{GcdState::kUnconfigured}; |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 354 | |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 355 | std::vector<Device::GcdStateChangedCallback> gcd_state_changed_callbacks_; |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 356 | |
Christopher Wiley | cd41966 | 2015-02-06 17:51:43 -0800 | [diff] [blame] | 357 | base::WeakPtrFactory<DeviceRegistrationInfo> weak_factory_{this}; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 358 | DISALLOW_COPY_AND_ASSIGN(DeviceRegistrationInfo); |
| 359 | }; |
| 360 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 361 | } // namespace weave |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 362 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 363 | #endif // LIBWEAVE_SRC_DEVICE_REGISTRATION_INFO_H_ |