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