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