[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [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 | |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 5 | #ifndef COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_ |
| 6 | #define COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_ |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/basictypes.h" |
[email protected] | 5da9380 | 2014-05-24 01:35:01 | [diff] [blame] | 13 | #include "base/memory/scoped_ptr.h" |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 14 | #include "components/gcm_driver/gcm_activity.h" |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 15 | |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 16 | template <class T> class scoped_refptr; |
| 17 | |
[email protected] | fc6078a | 2014-06-14 08:28:43 | [diff] [blame] | 18 | class GURL; |
| 19 | |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 20 | namespace base { |
| 21 | class FilePath; |
| 22 | class SequencedTaskRunner; |
chirantan | 192a921 | 2014-12-06 03:30:45 | [diff] [blame^] | 23 | class Timer; |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 24 | } |
| 25 | |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 26 | namespace net { |
[email protected] | fc6078a | 2014-06-14 08:28:43 | [diff] [blame] | 27 | class IPEndPoint; |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 28 | class URLRequestContextGetter; |
| 29 | } |
| 30 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 31 | namespace gcm { |
| 32 | |
[email protected] | 446f73c2 | 2014-05-14 20:47:18 | [diff] [blame] | 33 | class Encryptor; |
[email protected] | 72d4f25 | 2014-08-20 22:34:28 | [diff] [blame] | 34 | struct AccountMapping; |
[email protected] | 446f73c2 | 2014-05-14 20:47:18 | [diff] [blame] | 35 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 36 | // Interface that encapsulates the network communications with the Google Cloud |
| 37 | // Messaging server. This interface is not supposed to be thread-safe. |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 38 | class GCMClient { |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 39 | public: |
| 40 | enum Result { |
| 41 | // Successful operation. |
| 42 | SUCCESS, |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 43 | // Invalid parameter. |
| 44 | INVALID_PARAMETER, |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 45 | // GCM is disabled. |
| 46 | GCM_DISABLED, |
[email protected] | c7f7b53 | 2014-01-24 07:24:45 | [diff] [blame] | 47 | // Profile not signed in. |
| 48 | NOT_SIGNED_IN, |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 49 | // Previous asynchronous operation is still pending to finish. Certain |
| 50 | // operation, like register, is only allowed one at a time. |
| 51 | ASYNC_OPERATION_PENDING, |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 52 | // Network socket error. |
| 53 | NETWORK_ERROR, |
| 54 | // Problem at the server. |
| 55 | SERVER_ERROR, |
| 56 | // Exceeded the specified TTL during message sending. |
| 57 | TTL_EXCEEDED, |
| 58 | // Other errors. |
| 59 | UNKNOWN_ERROR |
| 60 | }; |
| 61 | |
[email protected] | 8ad8051 | 2014-05-23 09:40:47 | [diff] [blame] | 62 | enum ChromePlatform { |
| 63 | PLATFORM_WIN, |
| 64 | PLATFORM_MAC, |
| 65 | PLATFORM_LINUX, |
| 66 | PLATFORM_CROS, |
| 67 | PLATFORM_IOS, |
| 68 | PLATFORM_ANDROID, |
| 69 | PLATFORM_UNKNOWN |
| 70 | }; |
| 71 | |
| 72 | enum ChromeChannel { |
| 73 | CHANNEL_STABLE, |
| 74 | CHANNEL_BETA, |
| 75 | CHANNEL_DEV, |
| 76 | CHANNEL_CANARY, |
| 77 | CHANNEL_UNKNOWN |
| 78 | }; |
| 79 | |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 80 | struct ChromeBuildInfo { |
[email protected] | 8ad8051 | 2014-05-23 09:40:47 | [diff] [blame] | 81 | ChromeBuildInfo(); |
| 82 | ~ChromeBuildInfo(); |
| 83 | |
| 84 | ChromePlatform platform; |
| 85 | ChromeChannel channel; |
| 86 | std::string version; |
| 87 | }; |
| 88 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 89 | // Message data consisting of key-value pairs. |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 90 | typedef std::map<std::string, std::string> MessageData; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 91 | |
| 92 | // Message to be delivered to the other party. |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 93 | struct OutgoingMessage { |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 94 | OutgoingMessage(); |
| 95 | ~OutgoingMessage(); |
| 96 | |
| 97 | // Message ID. |
| 98 | std::string id; |
| 99 | // In seconds. |
| 100 | int time_to_live; |
| 101 | MessageData data; |
[email protected] | b20aece2 | 2014-05-09 22:34:08 | [diff] [blame] | 102 | |
fgorski | 0dfb6cd | 2014-09-22 23:05:47 | [diff] [blame] | 103 | static const int kMaximumTTL; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | // Message being received from the other party. |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 107 | struct IncomingMessage { |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 108 | IncomingMessage(); |
| 109 | ~IncomingMessage(); |
| 110 | |
| 111 | MessageData data; |
[email protected] | 40113aa | 2014-02-28 21:37:56 | [diff] [blame] | 112 | std::string collapse_key; |
[email protected] | 8d3af38 | 2014-03-07 00:58:41 | [diff] [blame] | 113 | std::string sender_id; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 114 | }; |
| 115 | |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 116 | // Detailed information of the Send Error event. |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 117 | struct SendErrorDetails { |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 118 | SendErrorDetails(); |
| 119 | ~SendErrorDetails(); |
| 120 | |
| 121 | std::string message_id; |
| 122 | MessageData additional_data; |
| 123 | Result result; |
| 124 | }; |
| 125 | |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 126 | // Internal states and activity statistics of a GCM client. |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 127 | struct GCMStatistics { |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 128 | public: |
| 129 | GCMStatistics(); |
| 130 | ~GCMStatistics(); |
| 131 | |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 132 | bool is_recording; |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 133 | bool gcm_client_created; |
| 134 | std::string gcm_client_state; |
| 135 | bool connection_client_created; |
| 136 | std::string connection_state; |
| 137 | uint64 android_id; |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 138 | std::vector<std::string> registered_app_ids; |
| 139 | int send_queue_size; |
| 140 | int resend_queue_size; |
| 141 | |
[email protected] | 5da9380 | 2014-05-24 01:35:01 | [diff] [blame] | 142 | RecordedActivities recorded_activities; |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 143 | }; |
| 144 | |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 145 | // Information about account. |
| 146 | struct AccountTokenInfo { |
| 147 | std::string account_id; |
| 148 | std::string email; |
| 149 | std::string access_token; |
| 150 | }; |
| 151 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 152 | // A delegate interface that allows the GCMClient instance to interact with |
| 153 | // its caller, i.e. notifying asynchronous event. |
| 154 | class Delegate { |
| 155 | public: |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 156 | // Called when the registration completed successfully or an error occurs. |
| 157 | // |app_id|: application ID. |
| 158 | // |registration_id|: non-empty if the registration completed successfully. |
| 159 | // |result|: the type of the error if an error occured, success otherwise. |
| 160 | virtual void OnRegisterFinished(const std::string& app_id, |
| 161 | const std::string& registration_id, |
| 162 | Result result) = 0; |
| 163 | |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 164 | // Called when the unregistration completed. |
| 165 | // |app_id|: application ID. |
[email protected] | 0e88e1d1 | 2014-03-19 06:53:08 | [diff] [blame] | 166 | // |result|: result of the unregistration. |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 167 | virtual void OnUnregisterFinished(const std::string& app_id, |
[email protected] | 0e88e1d1 | 2014-03-19 06:53:08 | [diff] [blame] | 168 | GCMClient::Result result) = 0; |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 169 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 170 | // Called when the message is scheduled to send successfully or an error |
| 171 | // occurs. |
| 172 | // |app_id|: application ID. |
| 173 | // |message_id|: ID of the message being sent. |
| 174 | // |result|: the type of the error if an error occured, success otherwise. |
| 175 | virtual void OnSendFinished(const std::string& app_id, |
| 176 | const std::string& message_id, |
| 177 | Result result) = 0; |
| 178 | |
| 179 | // Called when a message has been received. |
| 180 | // |app_id|: application ID. |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 181 | // |message|: message received. |
| 182 | virtual void OnMessageReceived(const std::string& app_id, |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 183 | const IncomingMessage& message) = 0; |
| 184 | |
| 185 | // Called when some messages have been deleted from the server. |
| 186 | // |app_id|: application ID. |
| 187 | virtual void OnMessagesDeleted(const std::string& app_id) = 0; |
| 188 | |
| 189 | // Called when a message failed to send to the server. |
| 190 | // |app_id|: application ID. |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 191 | // |send_error_detials|: Details of the send error event, like mesasge ID. |
| 192 | virtual void OnMessageSendError( |
| 193 | const std::string& app_id, |
| 194 | const SendErrorDetails& send_error_details) = 0; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 195 | |
[email protected] | 292af2b2 | 2014-08-06 19:42:45 | [diff] [blame] | 196 | // Called when a message was acknowledged by the GCM server. |
| 197 | // |app_id|: application ID. |
| 198 | // |message_id|: ID of the acknowledged message. |
| 199 | virtual void OnSendAcknowledged(const std::string& app_id, |
| 200 | const std::string& message_id) = 0; |
| 201 | |
[email protected] | 86625df | 2014-01-31 03:47:58 | [diff] [blame] | 202 | // Called when the GCM becomes ready. To get to this state, GCMClient |
| 203 | // finished loading from the GCM store and retrieved the device check-in |
| 204 | // from the server if it hadn't yet. |
fgorski | d578c18b | 2014-09-24 23:40:17 | [diff] [blame] | 205 | // |account_mappings|: a persisted list of accounts mapped to this GCM |
| 206 | // client. |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 207 | // |last_token_fetch_time|: time of a last successful token fetch. |
| 208 | virtual void OnGCMReady(const std::vector<AccountMapping>& account_mappings, |
| 209 | const base::Time& last_token_fetch_time) = 0; |
[email protected] | 7de7880 | 2014-05-10 19:49:40 | [diff] [blame] | 210 | |
| 211 | // Called when activities are being recorded and a new activity has just |
| 212 | // been recorded. |
| 213 | virtual void OnActivityRecorded() = 0; |
[email protected] | fc6078a | 2014-06-14 08:28:43 | [diff] [blame] | 214 | |
| 215 | // Called when a new connection is established and a successful handshake |
| 216 | // has been performed. |
| 217 | virtual void OnConnected(const net::IPEndPoint& ip_endpoint) = 0; |
| 218 | |
| 219 | // Called when the connection is interrupted. |
| 220 | virtual void OnDisconnected() = 0; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 221 | }; |
| 222 | |
[email protected] | 0db11822 | 2014-01-22 01:37:59 | [diff] [blame] | 223 | GCMClient(); |
| 224 | virtual ~GCMClient(); |
[email protected] | 1b1c3cd | 2013-12-17 18:40:04 | [diff] [blame] | 225 | |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 226 | // Begins initialization of the GCM Client. This will not trigger a |
| 227 | // connection. |
[email protected] | 8ad8051 | 2014-05-23 09:40:47 | [diff] [blame] | 228 | // |chrome_build_info|: chrome info, i.e., version, channel and etc. |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 229 | // |store_path|: path to the GCM store. |
| 230 | // |blocking_task_runner|: for running blocking file tasks. |
| 231 | // |url_request_context_getter|: for url requests. |
| 232 | // |delegate|: the delegate whose methods will be called asynchronously in |
| 233 | // response to events and messages. |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 234 | virtual void Initialize( |
[email protected] | 8ad8051 | 2014-05-23 09:40:47 | [diff] [blame] | 235 | const ChromeBuildInfo& chrome_build_info, |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 236 | const base::FilePath& store_path, |
| 237 | const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner, |
| 238 | const scoped_refptr<net::URLRequestContextGetter>& |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 239 | url_request_context_getter, |
[email protected] | 446f73c2 | 2014-05-14 20:47:18 | [diff] [blame] | 240 | scoped_ptr<Encryptor> encryptor, |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 241 | Delegate* delegate) = 0; |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 242 | |
[email protected] | fc767e4 | 2014-05-13 05:02:14 | [diff] [blame] | 243 | // Starts the GCM service by first loading the data from the persistent store. |
| 244 | // This will then kick off the check-in if the check-in info is not found in |
| 245 | // the store. |
| 246 | virtual void Start() = 0; |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 247 | |
[email protected] | 21fee548 | 2014-03-05 00:57:15 | [diff] [blame] | 248 | // Stops using the GCM service. This will not erase the persisted data. |
| 249 | virtual void Stop() = 0; |
| 250 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 251 | // Checks out of the GCM service. This will erase all the cached and persisted |
| 252 | // data. |
| 253 | virtual void CheckOut() = 0; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 254 | |
| 255 | // Registers the application for GCM. Delegate::OnRegisterFinished will be |
| 256 | // called asynchronously upon completion. |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 257 | // |app_id|: application ID. |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 258 | // |sender_ids|: list of IDs of the servers that are allowed to send the |
| 259 | // messages to the application. These IDs are assigned by the |
| 260 | // Google API Console. |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 261 | virtual void Register(const std::string& app_id, |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 262 | const std::vector<std::string>& sender_ids) = 0; |
| 263 | |
| 264 | // Unregisters the application from GCM when it is uninstalled. |
| 265 | // Delegate::OnUnregisterFinished will be called asynchronously upon |
| 266 | // completion. |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 267 | // |app_id|: application ID. |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 268 | virtual void Unregister(const std::string& app_id) = 0; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 269 | |
| 270 | // Sends a message to a given receiver. Delegate::OnSendFinished will be |
| 271 | // called asynchronously upon completion. |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 272 | // |app_id|: application ID. |
| 273 | // |receiver_id|: registration ID of the receiver party. |
| 274 | // |message|: message to be sent. |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 275 | virtual void Send(const std::string& app_id, |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 276 | const std::string& receiver_id, |
| 277 | const OutgoingMessage& message) = 0; |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 278 | |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 279 | // Enables or disables internal activity recording. |
| 280 | virtual void SetRecording(bool recording) = 0; |
| 281 | |
| 282 | // Clear all recorded GCM activity logs. |
| 283 | virtual void ClearActivityLogs() = 0; |
| 284 | |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 285 | // Gets internal states and statistics. |
| 286 | virtual GCMStatistics GetStatistics() const = 0; |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 287 | |
| 288 | // Sets a list of accounts with OAuth2 tokens for the next checkin. |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 289 | // |account_tokens|: list of email addresses, account IDs and OAuth2 access |
| 290 | // tokens. |
| 291 | virtual void SetAccountTokens( |
| 292 | const std::vector<AccountTokenInfo>& account_tokens) = 0; |
[email protected] | 72d4f25 | 2014-08-20 22:34:28 | [diff] [blame] | 293 | |
| 294 | // Persists the |account_mapping| in the store. |
| 295 | virtual void UpdateAccountMapping(const AccountMapping& account_mapping) = 0; |
| 296 | |
| 297 | // Removes the account mapping related to |account_id| from the persistent |
| 298 | // store. |
| 299 | virtual void RemoveAccountMapping(const std::string& account_id) = 0; |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 300 | |
| 301 | // Sets last token fetch time in persistent store. |
| 302 | virtual void SetLastTokenFetchTime(const base::Time& time) = 0; |
chirantan | 192a921 | 2014-12-06 03:30:45 | [diff] [blame^] | 303 | |
| 304 | // Updates the timer used by the HeartbeatManager for sending heartbeats. |
| 305 | virtual void UpdateHeartbeatTimer(scoped_ptr<base::Timer> timer) = 0; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 306 | }; |
| 307 | |
| 308 | } // namespace gcm |
| 309 | |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 310 | #endif // COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_ |