blob: 8a7f7b21747892932828090f27f3821ad40bc44d [file] [log] [blame]
[email protected]cd57f372014-06-09 17:13:061// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]e4097c82013-11-08 00:16:122// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]cd57f372014-06-09 17:13:065#ifndef COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_
6#define COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_
[email protected]e4097c82013-11-08 00:16:127
8#include <map>
9#include <string>
10#include <vector>
11
12#include "base/basictypes.h"
[email protected]5da93802014-05-24 01:35:0113#include "base/memory/scoped_ptr.h"
[email protected]cd57f372014-06-09 17:13:0614#include "components/gcm_driver/gcm_activity.h"
[email protected]e4097c82013-11-08 00:16:1215
[email protected]e2a4a8012014-02-07 22:32:5216template <class T> class scoped_refptr;
17
[email protected]fc6078a2014-06-14 08:28:4318class GURL;
19
[email protected]e2a4a8012014-02-07 22:32:5220namespace base {
21class FilePath;
22class SequencedTaskRunner;
23}
24
[email protected]e2a4a8012014-02-07 22:32:5225namespace net {
[email protected]fc6078a2014-06-14 08:28:4326class IPEndPoint;
[email protected]e2a4a8012014-02-07 22:32:5227class URLRequestContextGetter;
28}
29
[email protected]e4097c82013-11-08 00:16:1230namespace gcm {
31
[email protected]446f73c22014-05-14 20:47:1832class Encryptor;
[email protected]72d4f252014-08-20 22:34:2833struct AccountMapping;
[email protected]446f73c22014-05-14 20:47:1834
[email protected]e4097c82013-11-08 00:16:1235// Interface that encapsulates the network communications with the Google Cloud
36// Messaging server. This interface is not supposed to be thread-safe.
[email protected]cd57f372014-06-09 17:13:0637class GCMClient {
[email protected]e4097c82013-11-08 00:16:1238 public:
39 enum Result {
40 // Successful operation.
41 SUCCESS,
[email protected]b16a7c52013-11-20 01:18:5942 // Invalid parameter.
43 INVALID_PARAMETER,
[email protected]9d7e5c02014-05-21 03:09:0344 // GCM is disabled.
45 GCM_DISABLED,
[email protected]c7f7b532014-01-24 07:24:4546 // Profile not signed in.
47 NOT_SIGNED_IN,
[email protected]b16a7c52013-11-20 01:18:5948 // Previous asynchronous operation is still pending to finish. Certain
49 // operation, like register, is only allowed one at a time.
50 ASYNC_OPERATION_PENDING,
[email protected]e4097c82013-11-08 00:16:1251 // Network socket error.
52 NETWORK_ERROR,
53 // Problem at the server.
54 SERVER_ERROR,
55 // Exceeded the specified TTL during message sending.
56 TTL_EXCEEDED,
57 // Other errors.
58 UNKNOWN_ERROR
59 };
60
[email protected]8ad80512014-05-23 09:40:4761 enum ChromePlatform {
62 PLATFORM_WIN,
63 PLATFORM_MAC,
64 PLATFORM_LINUX,
65 PLATFORM_CROS,
66 PLATFORM_IOS,
67 PLATFORM_ANDROID,
68 PLATFORM_UNKNOWN
69 };
70
71 enum ChromeChannel {
72 CHANNEL_STABLE,
73 CHANNEL_BETA,
74 CHANNEL_DEV,
75 CHANNEL_CANARY,
76 CHANNEL_UNKNOWN
77 };
78
[email protected]cd57f372014-06-09 17:13:0679 struct ChromeBuildInfo {
[email protected]8ad80512014-05-23 09:40:4780 ChromeBuildInfo();
81 ~ChromeBuildInfo();
82
83 ChromePlatform platform;
84 ChromeChannel channel;
85 std::string version;
86 };
87
[email protected]e4097c82013-11-08 00:16:1288 // Message data consisting of key-value pairs.
[email protected]b16a7c52013-11-20 01:18:5989 typedef std::map<std::string, std::string> MessageData;
[email protected]e4097c82013-11-08 00:16:1290
91 // Message to be delivered to the other party.
[email protected]cd57f372014-06-09 17:13:0692 struct OutgoingMessage {
[email protected]e4097c82013-11-08 00:16:1293 OutgoingMessage();
94 ~OutgoingMessage();
95
96 // Message ID.
97 std::string id;
98 // In seconds.
99 int time_to_live;
100 MessageData data;
[email protected]b20aece22014-05-09 22:34:08101
102 static const int kMaximumTTL = 4 * 7 * 24 * 60 * 60; // 4 weeks.
[email protected]e4097c82013-11-08 00:16:12103 };
104
105 // Message being received from the other party.
[email protected]cd57f372014-06-09 17:13:06106 struct IncomingMessage {
[email protected]e4097c82013-11-08 00:16:12107 IncomingMessage();
108 ~IncomingMessage();
109
110 MessageData data;
[email protected]40113aa2014-02-28 21:37:56111 std::string collapse_key;
[email protected]8d3af382014-03-07 00:58:41112 std::string sender_id;
[email protected]e4097c82013-11-08 00:16:12113 };
114
[email protected]c6fe36b2014-03-11 10:58:12115 // Detailed information of the Send Error event.
[email protected]cd57f372014-06-09 17:13:06116 struct SendErrorDetails {
[email protected]c6fe36b2014-03-11 10:58:12117 SendErrorDetails();
118 ~SendErrorDetails();
119
120 std::string message_id;
121 MessageData additional_data;
122 Result result;
123 };
124
[email protected]35601812014-03-07 19:52:43125 // Internal states and activity statistics of a GCM client.
[email protected]cd57f372014-06-09 17:13:06126 struct GCMStatistics {
[email protected]35601812014-03-07 19:52:43127 public:
128 GCMStatistics();
129 ~GCMStatistics();
130
[email protected]436bcb82014-04-18 00:40:57131 bool is_recording;
[email protected]35601812014-03-07 19:52:43132 bool gcm_client_created;
133 std::string gcm_client_state;
134 bool connection_client_created;
135 std::string connection_state;
136 uint64 android_id;
[email protected]436bcb82014-04-18 00:40:57137 std::vector<std::string> registered_app_ids;
138 int send_queue_size;
139 int resend_queue_size;
140
[email protected]5da93802014-05-24 01:35:01141 RecordedActivities recorded_activities;
[email protected]35601812014-03-07 19:52:43142 };
143
[email protected]e4097c82013-11-08 00:16:12144 // A delegate interface that allows the GCMClient instance to interact with
145 // its caller, i.e. notifying asynchronous event.
146 class Delegate {
147 public:
[email protected]e4097c82013-11-08 00:16:12148 // Called when the registration completed successfully or an error occurs.
149 // |app_id|: application ID.
150 // |registration_id|: non-empty if the registration completed successfully.
151 // |result|: the type of the error if an error occured, success otherwise.
152 virtual void OnRegisterFinished(const std::string& app_id,
153 const std::string& registration_id,
154 Result result) = 0;
155
[email protected]e4007042014-02-15 20:34:28156 // Called when the unregistration completed.
157 // |app_id|: application ID.
[email protected]0e88e1d12014-03-19 06:53:08158 // |result|: result of the unregistration.
[email protected]e4007042014-02-15 20:34:28159 virtual void OnUnregisterFinished(const std::string& app_id,
[email protected]0e88e1d12014-03-19 06:53:08160 GCMClient::Result result) = 0;
[email protected]e4007042014-02-15 20:34:28161
[email protected]e4097c82013-11-08 00:16:12162 // Called when the message is scheduled to send successfully or an error
163 // occurs.
164 // |app_id|: application ID.
165 // |message_id|: ID of the message being sent.
166 // |result|: the type of the error if an error occured, success otherwise.
167 virtual void OnSendFinished(const std::string& app_id,
168 const std::string& message_id,
169 Result result) = 0;
170
171 // Called when a message has been received.
172 // |app_id|: application ID.
[email protected]e4097c82013-11-08 00:16:12173 // |message|: message received.
174 virtual void OnMessageReceived(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12175 const IncomingMessage& message) = 0;
176
177 // Called when some messages have been deleted from the server.
178 // |app_id|: application ID.
179 virtual void OnMessagesDeleted(const std::string& app_id) = 0;
180
181 // Called when a message failed to send to the server.
182 // |app_id|: application ID.
[email protected]c6fe36b2014-03-11 10:58:12183 // |send_error_detials|: Details of the send error event, like mesasge ID.
184 virtual void OnMessageSendError(
185 const std::string& app_id,
186 const SendErrorDetails& send_error_details) = 0;
[email protected]e4097c82013-11-08 00:16:12187
[email protected]292af2b22014-08-06 19:42:45188 // Called when a message was acknowledged by the GCM server.
189 // |app_id|: application ID.
190 // |message_id|: ID of the acknowledged message.
191 virtual void OnSendAcknowledged(const std::string& app_id,
192 const std::string& message_id) = 0;
193
[email protected]86625df2014-01-31 03:47:58194 // Called when the GCM becomes ready. To get to this state, GCMClient
195 // finished loading from the GCM store and retrieved the device check-in
196 // from the server if it hadn't yet.
197 virtual void OnGCMReady() = 0;
[email protected]7de78802014-05-10 19:49:40198
199 // Called when activities are being recorded and a new activity has just
200 // been recorded.
201 virtual void OnActivityRecorded() = 0;
[email protected]fc6078a2014-06-14 08:28:43202
203 // Called when a new connection is established and a successful handshake
204 // has been performed.
205 virtual void OnConnected(const net::IPEndPoint& ip_endpoint) = 0;
206
207 // Called when the connection is interrupted.
208 virtual void OnDisconnected() = 0;
[email protected]e4097c82013-11-08 00:16:12209 };
210
[email protected]0db118222014-01-22 01:37:59211 GCMClient();
212 virtual ~GCMClient();
[email protected]1b1c3cd2013-12-17 18:40:04213
[email protected]d3a4b2e2014-02-27 13:46:54214 // Begins initialization of the GCM Client. This will not trigger a
215 // connection.
[email protected]8ad80512014-05-23 09:40:47216 // |chrome_build_info|: chrome info, i.e., version, channel and etc.
[email protected]5799d052014-02-12 20:47:39217 // |store_path|: path to the GCM store.
218 // |blocking_task_runner|: for running blocking file tasks.
219 // |url_request_context_getter|: for url requests.
220 // |delegate|: the delegate whose methods will be called asynchronously in
221 // response to events and messages.
[email protected]e2a4a8012014-02-07 22:32:52222 virtual void Initialize(
[email protected]8ad80512014-05-23 09:40:47223 const ChromeBuildInfo& chrome_build_info,
[email protected]e2a4a8012014-02-07 22:32:52224 const base::FilePath& store_path,
225 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
226 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:39227 url_request_context_getter,
[email protected]446f73c22014-05-14 20:47:18228 scoped_ptr<Encryptor> encryptor,
[email protected]5799d052014-02-12 20:47:39229 Delegate* delegate) = 0;
[email protected]e2a4a8012014-02-07 22:32:52230
[email protected]fc767e42014-05-13 05:02:14231 // Starts the GCM service by first loading the data from the persistent store.
232 // This will then kick off the check-in if the check-in info is not found in
233 // the store.
234 virtual void Start() = 0;
[email protected]d3a4b2e2014-02-27 13:46:54235
[email protected]21fee5482014-03-05 00:57:15236 // Stops using the GCM service. This will not erase the persisted data.
237 virtual void Stop() = 0;
238
[email protected]5799d052014-02-12 20:47:39239 // Checks out of the GCM service. This will erase all the cached and persisted
240 // data.
241 virtual void CheckOut() = 0;
[email protected]e4097c82013-11-08 00:16:12242
243 // Registers the application for GCM. Delegate::OnRegisterFinished will be
244 // called asynchronously upon completion.
[email protected]e4097c82013-11-08 00:16:12245 // |app_id|: application ID.
[email protected]e4097c82013-11-08 00:16:12246 // |sender_ids|: list of IDs of the servers that are allowed to send the
247 // messages to the application. These IDs are assigned by the
248 // Google API Console.
[email protected]5799d052014-02-12 20:47:39249 virtual void Register(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12250 const std::vector<std::string>& sender_ids) = 0;
251
252 // Unregisters the application from GCM when it is uninstalled.
253 // Delegate::OnUnregisterFinished will be called asynchronously upon
254 // completion.
[email protected]e4097c82013-11-08 00:16:12255 // |app_id|: application ID.
[email protected]5799d052014-02-12 20:47:39256 virtual void Unregister(const std::string& app_id) = 0;
[email protected]e4097c82013-11-08 00:16:12257
258 // Sends a message to a given receiver. Delegate::OnSendFinished will be
259 // called asynchronously upon completion.
[email protected]e4097c82013-11-08 00:16:12260 // |app_id|: application ID.
261 // |receiver_id|: registration ID of the receiver party.
262 // |message|: message to be sent.
[email protected]5799d052014-02-12 20:47:39263 virtual void Send(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12264 const std::string& receiver_id,
265 const OutgoingMessage& message) = 0;
[email protected]35601812014-03-07 19:52:43266
[email protected]436bcb82014-04-18 00:40:57267 // Enables or disables internal activity recording.
268 virtual void SetRecording(bool recording) = 0;
269
270 // Clear all recorded GCM activity logs.
271 virtual void ClearActivityLogs() = 0;
272
[email protected]35601812014-03-07 19:52:43273 // Gets internal states and statistics.
274 virtual GCMStatistics GetStatistics() const = 0;
[email protected]7df5ef22014-07-17 07:35:58275
276 // Sets a list of accounts with OAuth2 tokens for the next checkin.
277 // |account_tokens| maps email addresses to OAuth2 access tokens.
278 virtual void SetAccountsForCheckin(
279 const std::map<std::string, std::string>& account_tokens) = 0;
[email protected]72d4f252014-08-20 22:34:28280
281 // Persists the |account_mapping| in the store.
282 virtual void UpdateAccountMapping(const AccountMapping& account_mapping) = 0;
283
284 // Removes the account mapping related to |account_id| from the persistent
285 // store.
286 virtual void RemoveAccountMapping(const std::string& account_id) = 0;
[email protected]e4097c82013-11-08 00:16:12287};
288
289} // namespace gcm
290
[email protected]cd57f372014-06-09 17:13:06291#endif // COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_