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