Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 5 | #ifndef BUFFET_DEVICE_REGISTRATION_INFO_H_ |
| 6 | #define BUFFET_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> |
Christopher Wiley | 34eae04 | 2015-03-18 10:25:08 -0700 | [diff] [blame] | 18 | #include <base/timer/timer.h> |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 19 | #include <chromeos/backoff_entry.h> |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 20 | #include <chromeos/data_encoding.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 21 | #include <chromeos/errors/error.h> |
| 22 | #include <chromeos/http/http_transport.h> |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 23 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 24 | #include "buffet/buffet_config.h" |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 25 | #include "buffet/commands/command_manager.h" |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 26 | #include "buffet/notification/notification_channel.h" |
| 27 | #include "buffet/notification/notification_delegate.h" |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 28 | #include "buffet/notification/pull_channel.h" |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 29 | #include "buffet/registration_status.h" |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 30 | #include "buffet/storage_interface.h" |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 31 | |
| 32 | namespace base { |
Vitaly Buka | 6ca3ad6 | 2015-03-11 17:03:23 -0700 | [diff] [blame] | 33 | class DictionaryValue; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 34 | } // namespace base |
| 35 | |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 36 | namespace chromeos { |
| 37 | class KeyValueStore; |
| 38 | } // namespace chromeos |
| 39 | |
Vitaly Buka | 63cc3d2 | 2015-06-23 20:11:36 -0700 | [diff] [blame] | 40 | namespace privetd { |
| 41 | class ShillClient; |
| 42 | } |
| 43 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 44 | namespace buffet { |
| 45 | |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 46 | class StateManager; |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 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. |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 53 | class DeviceRegistrationInfo : public NotificationDelegate { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 54 | public: |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 55 | using OnRegistrationChangedCallback = |
| 56 | base::Callback<void(RegistrationStatus)>; |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 57 | using CloudRequestCallback = |
| 58 | base::Callback<void(const base::DictionaryValue&)>; |
| 59 | using CloudRequestErrorCallback = |
| 60 | base::Callback<void(const chromeos::Error* error)>; |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 61 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 62 | DeviceRegistrationInfo( |
| 63 | const std::shared_ptr<CommandManager>& command_manager, |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 64 | const std::shared_ptr<StateManager>& state_manager, |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 65 | std::unique_ptr<BuffetConfig> config, |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 66 | const std::shared_ptr<chromeos::http::Transport>& transport, |
Vitaly Buka | 63cc3d2 | 2015-06-23 20:11:36 -0700 | [diff] [blame] | 67 | bool notifications_enabled, |
| 68 | privetd::ShillClient* shill_client); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 69 | |
Alex Vakulenko | 534a312 | 2015-05-22 15:48:53 -0700 | [diff] [blame] | 70 | ~DeviceRegistrationInfo() override; |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 71 | |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 72 | // Add callback to listen for changes in registration status. |
| 73 | void AddOnRegistrationChangedCallback( |
| 74 | const OnRegistrationChangedCallback& callback); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 75 | |
Vitaly Buka | 7ab89ff | 2015-06-09 22:11:40 -0700 | [diff] [blame] | 76 | // Add callback to listen for changes in config. |
| 77 | void AddOnConfigChangedCallback( |
| 78 | const BuffetConfig::OnChangedCallback& callback); |
| 79 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 80 | // Returns the authorization HTTP header that can be used to talk |
| 81 | // to GCD server for authenticated device communication. |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 82 | // Make sure ValidateAndRefreshAccessToken() is called before this call. |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 83 | std::pair<std::string, std::string> GetAuthorizationHeader() const; |
| 84 | |
| 85 | // Returns the GCD service request URL. If |subpath| is specified, it is |
| 86 | // appended to the base URL which is normally |
| 87 | // https://www.googleapis.com/clouddevices/v1/". |
| 88 | // If |params| are specified, each key-value pair is formatted using |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 89 | // chromeos::data_encoding::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: |
| 94 | // https://www.googleapis.com/clouddevices/v1/ticket?key=apiKey |
| 95 | std::string GetServiceURL( |
| 96 | const std::string& subpath = {}, |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 97 | const chromeos::data_encoding::WebParamList& params = {}) const; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 98 | |
| 99 | // Returns a service URL to access the registered device on GCD server. |
| 100 | // The base URL used to construct the full URL looks like this: |
| 101 | // https://www.googleapis.com/clouddevices/v1/devices/<device_id>/ |
| 102 | std::string GetDeviceURL( |
| 103 | const std::string& subpath = {}, |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 104 | const chromeos::data_encoding::WebParamList& params = {}) const; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 105 | |
| 106 | // Similar to GetServiceURL, GetOAuthURL() returns a URL of OAuth 2.0 server. |
| 107 | // The base URL used is https://accounts.google.com/o/oauth2/. |
| 108 | std::string GetOAuthURL( |
| 109 | const std::string& subpath = {}, |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 110 | const chromeos::data_encoding::WebParamList& params = {}) const; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 111 | |
Alex Vakulenko | 6e3c30e | 2015-05-21 17:39:25 -0700 | [diff] [blame] | 112 | // Starts GCD device if credentials available. |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 113 | void Start(); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 114 | |
Nathan Bullock | 5e022a3 | 2015-04-08 15:13:07 -0400 | [diff] [blame] | 115 | // Checks whether we have credentials generated during registration. |
Alex Vakulenko | 3fa42ae | 2015-06-23 15:12:22 -0700 | [diff] [blame] | 116 | bool HaveRegistrationCredentials() const; |
| 117 | // Calls HaveRegistrationCredentials() and logs an error if no credentials |
| 118 | // are available. |
| 119 | bool VerifyRegistrationCredentials(chromeos::ErrorPtr* error) const; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 120 | |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 121 | // Gets the full device description JSON object asynchronously. |
| 122 | // Passes the device info as the first argument to |callback|, or nullptr if |
| 123 | // the device is not registered or in case of communication failure. |
| 124 | void GetDeviceInfo(const CloudRequestCallback& success_callback, |
| 125 | const CloudRequestErrorCallback& error_callback); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 126 | |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 127 | // Registers the device. |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 128 | // Returns a device ID on success. |
Vitaly Buka | cad2e33 | 2015-05-14 23:33:32 -0700 | [diff] [blame] | 129 | std::string RegisterDevice(const std::string& ticket_id, |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 130 | chromeos::ErrorPtr* error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 131 | |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 132 | // Updates a command. |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 133 | void UpdateCommand(const std::string& command_id, |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 134 | const base::DictionaryValue& command_patch, |
| 135 | const base::Closure& on_success, |
| 136 | const base::Closure& on_error); |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 137 | |
Vitaly Buka | fa94706 | 2015-04-17 00:41:31 -0700 | [diff] [blame] | 138 | // Updates basic device information. |
| 139 | bool UpdateDeviceInfo(const std::string& name, |
| 140 | const std::string& description, |
| 141 | const std::string& location, |
| 142 | chromeos::ErrorPtr* error); |
| 143 | |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 144 | // Updates base device config. |
| 145 | bool UpdateBaseConfig(const std::string& anonymous_access_role, |
| 146 | bool local_discovery_enabled, |
| 147 | bool local_pairing_enabled, |
| 148 | chromeos::ErrorPtr* error); |
| 149 | |
Vitaly Buka | ff81db6 | 2015-05-14 21:25:45 -0700 | [diff] [blame] | 150 | // Updates GCD service configuration. Usually for testing. |
| 151 | bool UpdateServiceConfig(const std::string& client_id, |
| 152 | const std::string& client_secret, |
| 153 | const std::string& api_key, |
| 154 | const std::string& oauth_url, |
| 155 | const std::string& service_url, |
| 156 | chromeos::ErrorPtr* error); |
| 157 | |
Vitaly Buka | b56872f | 2015-06-21 18:39:34 -0700 | [diff] [blame] | 158 | // TODO(vitalybuka): remove getters and pass config to dependent code. |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 159 | const BuffetConfig& GetConfig() const { return *config_; } |
Vitaly Buka | b56872f | 2015-06-21 18:39:34 -0700 | [diff] [blame] | 160 | BuffetConfig* GetMutableConfig() { return config_.get(); } |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 161 | |
| 162 | base::WeakPtr<DeviceRegistrationInfo> AsWeakPtr() { |
| 163 | return weak_factory_.GetWeakPtr(); |
| 164 | } |
| 165 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 166 | private: |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 167 | friend class DeviceRegistrationInfoTest; |
| 168 | |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 169 | // Cause DeviceRegistrationInfo to attempt to StartDevice on its own later. |
| 170 | void ScheduleStartDevice(const base::TimeDelta& later); |
| 171 | |
| 172 | // Starts device execution. |
| 173 | // Device will do required start up chores and then start to listen |
| 174 | // to new commands. |
| 175 | // TODO(antonm): Consider moving into some other class. |
| 176 | void StartDevice(chromeos::ErrorPtr* error, |
| 177 | const base::TimeDelta& retry_delay); |
| 178 | |
David Zeuthen | 390d191 | 2015-03-03 14:54:48 -0500 | [diff] [blame] | 179 | // Forcibly refreshes the access token. |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 180 | void RefreshAccessToken(const base::Closure& success_callback, |
| 181 | const CloudRequestErrorCallback& error_callback); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 182 | |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 183 | // Success callback for RefreshAccessToken(). |
| 184 | void OnRefreshAccessTokenSuccess( |
| 185 | const base::Closure& success_callback, |
| 186 | const std::shared_ptr<CloudRequestErrorCallback>& error_callback, |
| 187 | chromeos::http::RequestID id, |
| 188 | std::unique_ptr<chromeos::http::Response> response); |
Alex Vakulenko | 6b028ae | 2015-05-29 09:38:59 -0700 | [diff] [blame] | 189 | |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 190 | // Parse the OAuth response, and sets registration status to |
| 191 | // kInvalidCredentials if our registration is no longer valid. |
| 192 | std::unique_ptr<base::DictionaryValue> ParseOAuthResponse( |
Alex Vakulenko | f71cca6 | 2015-04-09 15:17:17 -0700 | [diff] [blame] | 193 | chromeos::http::Response* response, chromeos::ErrorPtr* error); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 194 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 195 | // This attempts to open a notification channel. The channel needs to be |
Nathan Bullock | bea9113 | 2015-02-19 09:13:33 -0500 | [diff] [blame] | 196 | // restarted anytime the access_token is refreshed. |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 197 | void StartNotificationChannel(); |
Nathan Bullock | bea9113 | 2015-02-19 09:13:33 -0500 | [diff] [blame] | 198 | |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 199 | // Do a HTTPS request to cloud services. |
| 200 | // Handles many cases like reauthorization, 5xx HTTP response codes |
| 201 | // and device removal. It is a recommended way to do cloud API |
| 202 | // requests. |
| 203 | // TODO(antonm): Consider moving into some other class. |
| 204 | void DoCloudRequest( |
Anton Muhin | 233d2ee | 2014-10-22 15:16:24 +0400 | [diff] [blame] | 205 | const std::string& method, |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 206 | const std::string& url, |
| 207 | const base::DictionaryValue* body, |
Alex Vakulenko | 0357c03 | 2015-01-06 16:32:31 -0800 | [diff] [blame] | 208 | const CloudRequestCallback& success_callback, |
| 209 | const CloudRequestErrorCallback& error_callback); |
Anton Muhin | ac661ab | 2014-10-03 20:29:48 +0400 | [diff] [blame] | 210 | |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 211 | // Helper for DoCloudRequest(). |
| 212 | struct CloudRequestData { |
| 213 | std::string method; |
| 214 | std::string url; |
| 215 | std::string body; |
| 216 | CloudRequestCallback success_callback; |
| 217 | CloudRequestErrorCallback error_callback; |
| 218 | }; |
| 219 | void SendCloudRequest(const std::shared_ptr<const CloudRequestData>& data); |
| 220 | void OnCloudRequestSuccess( |
| 221 | const std::shared_ptr<const CloudRequestData>& data, |
| 222 | chromeos::http::RequestID request_id, |
| 223 | std::unique_ptr<chromeos::http::Response> response); |
| 224 | void OnCloudRequestError( |
| 225 | const std::shared_ptr<const CloudRequestData>& data, |
| 226 | chromeos::http::RequestID request_id, |
| 227 | const chromeos::Error* error); |
| 228 | void RetryCloudRequest( |
| 229 | const std::shared_ptr<const CloudRequestData>& data); |
| 230 | void OnAccessTokenRefreshed( |
| 231 | const std::shared_ptr<const CloudRequestData>& data); |
| 232 | void OnAccessTokenError( |
| 233 | const std::shared_ptr<const CloudRequestData>& data, |
| 234 | const chromeos::Error* error); |
| 235 | |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 236 | void UpdateDeviceResource(const base::Closure& on_success, |
| 237 | const CloudRequestErrorCallback& on_failure); |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 238 | |
Christopher Wiley | ba983c8 | 2015-03-05 16:32:23 -0800 | [diff] [blame] | 239 | void FetchCommands( |
| 240 | const base::Callback<void(const base::ListValue&)>& on_success, |
| 241 | const CloudRequestErrorCallback& on_failure); |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 242 | |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 243 | // Processes the command list that is fetched from the server on connection. |
| 244 | // Aborts commands which are in transitional states and publishes queued |
| 245 | // commands which are queued. |
| 246 | void ProcessInitialCommandList(const base::ListValue& commands); |
Anton Muhin | c635c59 | 2014-10-28 21:48:08 +0400 | [diff] [blame] | 247 | |
Anton Muhin | d07e206 | 2014-10-27 10:53:29 +0400 | [diff] [blame] | 248 | void PublishCommands(const base::ListValue& commands); |
Alex Vakulenko | 6e3c30e | 2015-05-21 17:39:25 -0700 | [diff] [blame] | 249 | void PublishCommand(const base::DictionaryValue& command); |
Anton Muhin | d07e206 | 2014-10-27 10:53:29 +0400 | [diff] [blame] | 250 | |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 251 | void PublishStateUpdates(); |
Alex Vakulenko | 93ba0bd | 2015-06-19 14:06:46 -0700 | [diff] [blame] | 252 | void OnPublishStateSuccess(const base::DictionaryValue& reply); |
| 253 | void OnPublishStateError(const chromeos::Error* error); |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 254 | |
Alex Vakulenko | d1978d3 | 2015-04-29 17:33:26 -0700 | [diff] [blame] | 255 | // If unrecoverable error occurred (e.g. error parsing command instance), |
| 256 | // notify the server that the command is aborted by the device. |
| 257 | void NotifyCommandAborted(const std::string& command_id, |
| 258 | chromeos::ErrorPtr error); |
| 259 | |
| 260 | // When NotifyCommandAborted() fails, RetryNotifyCommandAborted() schedules |
| 261 | // a retry attempt. |
| 262 | void RetryNotifyCommandAborted(const std::string& command_id, |
| 263 | chromeos::ErrorPtr error); |
| 264 | |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 265 | // Builds Cloud API devices collection REST resource which matches |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 266 | // current state of the device including command definitions |
| 267 | // for all supported commands and current device state. |
| 268 | std::unique_ptr<base::DictionaryValue> BuildDeviceResource( |
| 269 | chromeos::ErrorPtr* error); |
| 270 | |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 271 | void SetRegistrationStatus(RegistrationStatus new_status); |
Vitaly Buka | 620bd7e | 2015-03-16 01:07:01 -0700 | [diff] [blame] | 272 | void SetDeviceId(const std::string& device_id); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 273 | |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 274 | // Callback called when command definitions are changed to re-publish new CDD. |
| 275 | void OnCommandDefsChanged(); |
Vitaly Buka | c903d28 | 2015-05-26 17:03:08 -0700 | [diff] [blame] | 276 | void OnStateChanged(); |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 277 | |
Alex Vakulenko | 6b40d8f | 2015-06-24 11:44:22 -0700 | [diff] [blame] | 278 | // Overrides from NotificationDelegate. |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 279 | void OnConnected(const std::string& channel_name) override; |
| 280 | void OnDisconnected() override; |
| 281 | void OnPermanentFailure() override; |
Alex Vakulenko | 6e3c30e | 2015-05-21 17:39:25 -0700 | [diff] [blame] | 282 | void OnCommandCreated(const base::DictionaryValue& command) override; |
Alex Vakulenko | 6b40d8f | 2015-06-24 11:44:22 -0700 | [diff] [blame] | 283 | void OnDeviceDeleted(const std::string& device_id) override; |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 284 | |
Alex Vakulenko | 3fa42ae | 2015-06-23 15:12:22 -0700 | [diff] [blame] | 285 | // Wipes out the device registration information and stops server connections. |
| 286 | void MarkDeviceUnregistered(); |
| 287 | |
Nathan Bullock | d9e0bcd | 2015-02-11 11:36:39 -0500 | [diff] [blame] | 288 | // Transient data |
| 289 | std::string access_token_; |
| 290 | base::Time access_token_expiration_; |
Nathan Bullock | d9e0bcd | 2015-02-11 11:36:39 -0500 | [diff] [blame] | 291 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 292 | // HTTP transport used for communications. |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 293 | std::shared_ptr<chromeos::http::Transport> transport_; |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 294 | // Global command manager. |
| 295 | std::shared_ptr<CommandManager> command_manager_; |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 296 | // Device state manager. |
Anton Muhin | b831562 | 2014-11-20 03:17:05 +0400 | [diff] [blame] | 297 | std::shared_ptr<StateManager> state_manager_; |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 298 | |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 299 | std::unique_ptr<BuffetConfig> config_; |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 300 | |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 301 | // Backoff manager for DoCloudRequest() method. |
| 302 | std::unique_ptr<chromeos::BackoffEntry::Policy> cloud_backoff_policy_; |
| 303 | std::unique_ptr<chromeos::BackoffEntry> cloud_backoff_entry_; |
| 304 | |
Alex Vakulenko | 93ba0bd | 2015-06-19 14:06:46 -0700 | [diff] [blame] | 305 | // Flag set to true while a device state update patch request is in flight |
| 306 | // to the cloud server. |
| 307 | bool device_state_update_pending_{false}; |
| 308 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 309 | const bool notifications_enabled_; |
| 310 | std::unique_ptr<NotificationChannel> primary_notification_channel_; |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 311 | std::unique_ptr<PullChannel> pull_channel_; |
| 312 | NotificationChannel* current_notification_channel_{nullptr}; |
Alex Vakulenko | 6b028ae | 2015-05-29 09:38:59 -0700 | [diff] [blame] | 313 | bool notification_channel_starting_{false}; |
Christopher Wiley | d732bd0 | 2015-04-07 11:11:18 -0700 | [diff] [blame] | 314 | |
Vitaly Buka | 63cc3d2 | 2015-06-23 20:11:36 -0700 | [diff] [blame] | 315 | privetd::ShillClient* shill_client_{nullptr}; |
| 316 | |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 317 | // Tracks our current registration status. |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 318 | RegistrationStatus registration_status_{RegistrationStatus::kUnconfigured}; |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 319 | |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 320 | std::vector<OnRegistrationChangedCallback> on_registration_changed_; |
| 321 | |
Christopher Wiley | cd41966 | 2015-02-06 17:51:43 -0800 | [diff] [blame] | 322 | base::WeakPtrFactory<DeviceRegistrationInfo> weak_factory_{this}; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 323 | DISALLOW_COPY_AND_ASSIGN(DeviceRegistrationInfo); |
| 324 | }; |
| 325 | |
| 326 | } // namespace buffet |
| 327 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 328 | #endif // BUFFET_DEVICE_REGISTRATION_INFO_H_ |