blob: fa4175ae661e793b104aeeb19d335d1c89f5d02a [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
avi26062922015-12-26 00:14:188#include <stdint.h>
9
[email protected]e4097c82013-11-08 00:16:1210#include <map>
11#include <string>
12#include <vector>
13
jianli7a0c9b62015-05-26 23:24:4714#include "base/memory/linked_ptr.h"
[email protected]5da93802014-05-24 01:35:0115#include "base/memory/scoped_ptr.h"
mvanouwerkerkf8633deb2015-07-13 11:04:0616#include "components/gcm_driver/common/gcm_messages.h"
[email protected]cd57f372014-06-09 17:13:0617#include "components/gcm_driver/gcm_activity.h"
jianli7a0c9b62015-05-26 23:24:4718#include "components/gcm_driver/registration_info.h"
[email protected]e4097c82013-11-08 00:16:1219
[email protected]e2a4a8012014-02-07 22:32:5220template <class T> class scoped_refptr;
21
[email protected]fc6078a2014-06-14 08:28:4322class GURL;
23
[email protected]e2a4a8012014-02-07 22:32:5224namespace base {
25class FilePath;
26class SequencedTaskRunner;
chirantan192a9212014-12-06 03:30:4527class Timer;
[email protected]e2a4a8012014-02-07 22:32:5228}
29
[email protected]e2a4a8012014-02-07 22:32:5230namespace net {
[email protected]fc6078a2014-06-14 08:28:4331class IPEndPoint;
[email protected]e2a4a8012014-02-07 22:32:5232class URLRequestContextGetter;
33}
34
[email protected]e4097c82013-11-08 00:16:1235namespace gcm {
36
[email protected]446f73c22014-05-14 20:47:1837class Encryptor;
[email protected]72d4f252014-08-20 22:34:2838struct AccountMapping;
[email protected]446f73c22014-05-14 20:47:1839
[email protected]e4097c82013-11-08 00:16:1240// Interface that encapsulates the network communications with the Google Cloud
41// Messaging server. This interface is not supposed to be thread-safe.
[email protected]cd57f372014-06-09 17:13:0642class GCMClient {
[email protected]e4097c82013-11-08 00:16:1243 public:
jianlif3e52af42015-01-21 23:18:4744 // Controls how GCM is being started. At first, GCMClient will be initialized
45 // and GCM store will be loaded. Then GCM connection may or may not be
46 // initiated depending on this enum value.
47 enum StartMode {
48 // GCM should be started only when it is being actually used. If no
49 // registration record is found, GCM will not kick off.
50 DELAYED_START,
51 // GCM should be started immediately.
52 IMMEDIATE_START
53 };
54
[email protected]e4097c82013-11-08 00:16:1255 enum Result {
56 // Successful operation.
57 SUCCESS,
[email protected]b16a7c52013-11-20 01:18:5958 // Invalid parameter.
59 INVALID_PARAMETER,
[email protected]9d7e5c02014-05-21 03:09:0360 // GCM is disabled.
61 GCM_DISABLED,
[email protected]b16a7c52013-11-20 01:18:5962 // Previous asynchronous operation is still pending to finish. Certain
63 // operation, like register, is only allowed one at a time.
64 ASYNC_OPERATION_PENDING,
[email protected]e4097c82013-11-08 00:16:1265 // Network socket error.
66 NETWORK_ERROR,
67 // Problem at the server.
68 SERVER_ERROR,
69 // Exceeded the specified TTL during message sending.
70 TTL_EXCEEDED,
71 // Other errors.
72 UNKNOWN_ERROR
73 };
74
[email protected]8ad80512014-05-23 09:40:4775 enum ChromePlatform {
76 PLATFORM_WIN,
77 PLATFORM_MAC,
78 PLATFORM_LINUX,
79 PLATFORM_CROS,
80 PLATFORM_IOS,
81 PLATFORM_ANDROID,
82 PLATFORM_UNKNOWN
83 };
84
85 enum ChromeChannel {
86 CHANNEL_STABLE,
87 CHANNEL_BETA,
88 CHANNEL_DEV,
89 CHANNEL_CANARY,
90 CHANNEL_UNKNOWN
91 };
92
[email protected]cd57f372014-06-09 17:13:0693 struct ChromeBuildInfo {
[email protected]8ad80512014-05-23 09:40:4794 ChromeBuildInfo();
95 ~ChromeBuildInfo();
96
97 ChromePlatform platform;
98 ChromeChannel channel;
99 std::string version;
100 };
101
[email protected]c6fe36b2014-03-11 10:58:12102 // Detailed information of the Send Error event.
[email protected]cd57f372014-06-09 17:13:06103 struct SendErrorDetails {
[email protected]c6fe36b2014-03-11 10:58:12104 SendErrorDetails();
105 ~SendErrorDetails();
106
107 std::string message_id;
108 MessageData additional_data;
109 Result result;
110 };
111
[email protected]35601812014-03-07 19:52:43112 // Internal states and activity statistics of a GCM client.
[email protected]cd57f372014-06-09 17:13:06113 struct GCMStatistics {
[email protected]35601812014-03-07 19:52:43114 public:
115 GCMStatistics();
116 ~GCMStatistics();
117
[email protected]436bcb82014-04-18 00:40:57118 bool is_recording;
[email protected]35601812014-03-07 19:52:43119 bool gcm_client_created;
120 std::string gcm_client_state;
121 bool connection_client_created;
122 std::string connection_state;
avi26062922015-12-26 00:14:18123 uint64_t android_id;
[email protected]436bcb82014-04-18 00:40:57124 std::vector<std::string> registered_app_ids;
125 int send_queue_size;
126 int resend_queue_size;
127
[email protected]5da93802014-05-24 01:35:01128 RecordedActivities recorded_activities;
[email protected]35601812014-03-07 19:52:43129 };
130
fgorskic1047312014-09-04 16:48:54131 // Information about account.
132 struct AccountTokenInfo {
133 std::string account_id;
134 std::string email;
135 std::string access_token;
136 };
137
[email protected]e4097c82013-11-08 00:16:12138 // A delegate interface that allows the GCMClient instance to interact with
139 // its caller, i.e. notifying asynchronous event.
140 class Delegate {
141 public:
[email protected]e4097c82013-11-08 00:16:12142 // Called when the registration completed successfully or an error occurs.
jianli7a0c9b62015-05-26 23:24:47143 // |registration_info|: the specific information required for the
144 // registration.
[email protected]e4097c82013-11-08 00:16:12145 // |registration_id|: non-empty if the registration completed successfully.
146 // |result|: the type of the error if an error occured, success otherwise.
jianli7a0c9b62015-05-26 23:24:47147 virtual void OnRegisterFinished(
148 const linked_ptr<RegistrationInfo>& registration_info,
149 const std::string& registration_id,
150 Result result) = 0;
[email protected]e4097c82013-11-08 00:16:12151
[email protected]e4007042014-02-15 20:34:28152 // Called when the unregistration completed.
jianli7a0c9b62015-05-26 23:24:47153 // |registration_info|: the specific information required for the
154 // registration.
[email protected]0e88e1d12014-03-19 06:53:08155 // |result|: result of the unregistration.
jianli7a0c9b62015-05-26 23:24:47156 virtual void OnUnregisterFinished(
157 const linked_ptr<RegistrationInfo>& registration_info,
158 GCMClient::Result result) = 0;
[email protected]e4007042014-02-15 20:34:28159
[email protected]e4097c82013-11-08 00:16:12160 // Called when the message is scheduled to send successfully or an error
161 // occurs.
162 // |app_id|: application ID.
163 // |message_id|: ID of the message being sent.
164 // |result|: the type of the error if an error occured, success otherwise.
165 virtual void OnSendFinished(const std::string& app_id,
166 const std::string& message_id,
167 Result result) = 0;
168
169 // Called when a message has been received.
170 // |app_id|: application ID.
[email protected]e4097c82013-11-08 00:16:12171 // |message|: message received.
172 virtual void OnMessageReceived(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12173 const IncomingMessage& message) = 0;
174
175 // Called when some messages have been deleted from the server.
176 // |app_id|: application ID.
177 virtual void OnMessagesDeleted(const std::string& app_id) = 0;
178
179 // Called when a message failed to send to the server.
180 // |app_id|: application ID.
[email protected]c6fe36b2014-03-11 10:58:12181 // |send_error_detials|: Details of the send error event, like mesasge ID.
182 virtual void OnMessageSendError(
183 const std::string& app_id,
184 const SendErrorDetails& send_error_details) = 0;
[email protected]e4097c82013-11-08 00:16:12185
[email protected]292af2b22014-08-06 19:42:45186 // Called when a message was acknowledged by the GCM server.
187 // |app_id|: application ID.
188 // |message_id|: ID of the acknowledged message.
189 virtual void OnSendAcknowledged(const std::string& app_id,
190 const std::string& message_id) = 0;
191
[email protected]86625df2014-01-31 03:47:58192 // Called when the GCM becomes ready. To get to this state, GCMClient
193 // finished loading from the GCM store and retrieved the device check-in
194 // from the server if it hadn't yet.
fgorskid578c18b2014-09-24 23:40:17195 // |account_mappings|: a persisted list of accounts mapped to this GCM
196 // client.
fgorski5df101702014-10-28 02:09:31197 // |last_token_fetch_time|: time of a last successful token fetch.
198 virtual void OnGCMReady(const std::vector<AccountMapping>& account_mappings,
199 const base::Time& last_token_fetch_time) = 0;
[email protected]7de78802014-05-10 19:49:40200
201 // Called when activities are being recorded and a new activity has just
202 // been recorded.
203 virtual void OnActivityRecorded() = 0;
[email protected]fc6078a2014-06-14 08:28:43204
205 // Called when a new connection is established and a successful handshake
206 // has been performed.
207 virtual void OnConnected(const net::IPEndPoint& ip_endpoint) = 0;
208
209 // Called when the connection is interrupted.
210 virtual void OnDisconnected() = 0;
[email protected]e4097c82013-11-08 00:16:12211 };
212
[email protected]0db118222014-01-22 01:37:59213 GCMClient();
214 virtual ~GCMClient();
[email protected]1b1c3cd2013-12-17 18:40:04215
[email protected]d3a4b2e2014-02-27 13:46:54216 // Begins initialization of the GCM Client. This will not trigger a
217 // connection.
[email protected]8ad80512014-05-23 09:40:47218 // |chrome_build_info|: chrome info, i.e., version, channel and etc.
[email protected]5799d052014-02-12 20:47:39219 // |store_path|: path to the GCM store.
220 // |blocking_task_runner|: for running blocking file tasks.
mmenkee65e7af2015-10-13 17:16:42221 // |url_request_context_getter|: for url requests. The GCMClient must be
222 // deleted before the Getter's underlying URLRequestContext.
[email protected]5799d052014-02-12 20:47:39223 // |delegate|: the delegate whose methods will be called asynchronously in
mmenkee65e7af2015-10-13 17:16:42224 // response to events and messages.
[email protected]e2a4a8012014-02-07 22:32:52225 virtual void Initialize(
[email protected]8ad80512014-05-23 09:40:47226 const ChromeBuildInfo& chrome_build_info,
[email protected]e2a4a8012014-02-07 22:32:52227 const base::FilePath& store_path,
228 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
229 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:39230 url_request_context_getter,
[email protected]446f73c22014-05-14 20:47:18231 scoped_ptr<Encryptor> encryptor,
[email protected]5799d052014-02-12 20:47:39232 Delegate* delegate) = 0;
[email protected]e2a4a8012014-02-07 22:32:52233
jianlif3e52af42015-01-21 23:18:47234 // This will initiate the GCM connection only if |start_mode| means to start
235 // the GCM immediately or the GCM registration records are found in the store.
236 // Note that it is OK to call Start multiple times and the implementation
237 // should handle it gracefully.
238 virtual void Start(StartMode start_mode) = 0;
[email protected]d3a4b2e2014-02-27 13:46:54239
[email protected]21fee5482014-03-05 00:57:15240 // Stops using the GCM service. This will not erase the persisted data.
241 virtual void Stop() = 0;
242
jianli7a0c9b62015-05-26 23:24:47243 // Registers with the server to access the provided service.
244 // Delegate::OnRegisterFinished will be called asynchronously upon completion.
245 // |registration_info|: the specific information required for the
246 // registration. For GCM, it will contain app id and
247 // sender IDs. For InstanceID, it will contain app_id,
248 // authorized entity and scope.
249 virtual void Register(
250 const linked_ptr<RegistrationInfo>& registration_info) = 0;
[email protected]e4097c82013-11-08 00:16:12251
jianli7a0c9b62015-05-26 23:24:47252 // Unregisters from the server to stop accessing the provided service.
[email protected]e4097c82013-11-08 00:16:12253 // Delegate::OnUnregisterFinished will be called asynchronously upon
254 // completion.
jianli7a0c9b62015-05-26 23:24:47255 // |registration_info|: the specific information required for the
256 // registration. For GCM, it will contain app id (sender
257 // IDs can be ingored). For InstanceID, it will contain
258 // app id, authorized entity and scope.
259 virtual void Unregister(
260 const linked_ptr<RegistrationInfo>& registration_info) = 0;
[email protected]e4097c82013-11-08 00:16:12261
262 // Sends a message to a given receiver. Delegate::OnSendFinished will be
263 // called asynchronously upon completion.
[email protected]e4097c82013-11-08 00:16:12264 // |app_id|: application ID.
265 // |receiver_id|: registration ID of the receiver party.
266 // |message|: message to be sent.
[email protected]5799d052014-02-12 20:47:39267 virtual void Send(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12268 const std::string& receiver_id,
269 const OutgoingMessage& message) = 0;
[email protected]35601812014-03-07 19:52:43270
[email protected]436bcb82014-04-18 00:40:57271 // Enables or disables internal activity recording.
272 virtual void SetRecording(bool recording) = 0;
273
274 // Clear all recorded GCM activity logs.
275 virtual void ClearActivityLogs() = 0;
276
[email protected]35601812014-03-07 19:52:43277 // Gets internal states and statistics.
278 virtual GCMStatistics GetStatistics() const = 0;
[email protected]7df5ef22014-07-17 07:35:58279
280 // Sets a list of accounts with OAuth2 tokens for the next checkin.
fgorski58b9dfc2014-09-29 16:46:18281 // |account_tokens|: list of email addresses, account IDs and OAuth2 access
282 // tokens.
283 virtual void SetAccountTokens(
284 const std::vector<AccountTokenInfo>& account_tokens) = 0;
[email protected]72d4f252014-08-20 22:34:28285
286 // Persists the |account_mapping| in the store.
287 virtual void UpdateAccountMapping(const AccountMapping& account_mapping) = 0;
288
289 // Removes the account mapping related to |account_id| from the persistent
290 // store.
291 virtual void RemoveAccountMapping(const std::string& account_id) = 0;
fgorski5df101702014-10-28 02:09:31292
293 // Sets last token fetch time in persistent store.
294 virtual void SetLastTokenFetchTime(const base::Time& time) = 0;
chirantan192a9212014-12-06 03:30:45295
296 // Updates the timer used by the HeartbeatManager for sending heartbeats.
297 virtual void UpdateHeartbeatTimer(scoped_ptr<base::Timer> timer) = 0;
jianli10018b2d2015-05-11 21:14:13298
299 // Adds the Instance ID data for a specific app to the persistent store.
300 virtual void AddInstanceIDData(const std::string& app_id,
jianli7a0c9b62015-05-26 23:24:47301 const std::string& instance_id,
302 const std::string& extra_data) = 0;
jianli10018b2d2015-05-11 21:14:13303
304 // Removes the Instance ID data for a specific app from the persistent store.
305 virtual void RemoveInstanceIDData(const std::string& app_id) = 0;
306
307 // Retrieves the Instance ID data for a specific app from the persistent
308 // store.
jianli7a0c9b62015-05-26 23:24:47309 virtual void GetInstanceIDData(const std::string& app_id,
310 std::string* instance_id,
311 std::string* extra_data) = 0;
fgorski22754462015-05-14 00:05:22312
313 // Gets and sets custom heartbeat interval for the MCS connection.
314 // |scope| is used to identify the component that requests a custom interval
315 // to be set, and allows that component to later revoke the setting. It should
316 // be unique.
317 virtual void AddHeartbeatInterval(const std::string& scope,
318 int interval_ms) = 0;
319 virtual void RemoveHeartbeatInterval(const std::string& scope) = 0;
[email protected]e4097c82013-11-08 00:16:12320};
321
322} // namespace gcm
323
[email protected]cd57f372014-06-09 17:13:06324#endif // COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_