blob: 95a56d9d0e0f667fc24228fcfaaf535447794f88 [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>
dchenga77e28eb2016-04-21 21:34:3711#include <memory>
[email protected]e4097c82013-11-08 00:16:1212#include <string>
13#include <vector>
14
jianli7a0c9b62015-05-26 23:24:4715#include "base/memory/linked_ptr.h"
mvanouwerkerkf8633deb2015-07-13 11:04:0616#include "components/gcm_driver/common/gcm_messages.h"
peteree284ba52016-02-01 11:53:2817#include "components/gcm_driver/crypto/gcm_encryption_provider.h"
[email protected]cd57f372014-06-09 17:13:0618#include "components/gcm_driver/gcm_activity.h"
jianli7a0c9b62015-05-26 23:24:4719#include "components/gcm_driver/registration_info.h"
[email protected]e4097c82013-11-08 00:16:1220
[email protected]e2a4a8012014-02-07 22:32:5221template <class T> class scoped_refptr;
22
[email protected]e2a4a8012014-02-07 22:32:5223namespace base {
24class FilePath;
25class SequencedTaskRunner;
chirantan192a9212014-12-06 03:30:4526class Timer;
[email protected]e2a4a8012014-02-07 22:32:5227}
28
[email protected]e2a4a8012014-02-07 22:32:5229namespace net {
[email protected]fc6078a2014-06-14 08:28:4330class IPEndPoint;
[email protected]e2a4a8012014-02-07 22:32:5231class URLRequestContextGetter;
32}
33
[email protected]e4097c82013-11-08 00:16:1234namespace gcm {
35
[email protected]446f73c22014-05-14 20:47:1836class Encryptor;
[email protected]72d4f252014-08-20 22:34:2837struct AccountMapping;
[email protected]446f73c22014-05-14 20:47:1838
[email protected]e4097c82013-11-08 00:16:1239// Interface that encapsulates the network communications with the Google Cloud
40// Messaging server. This interface is not supposed to be thread-safe.
[email protected]cd57f372014-06-09 17:13:0641class GCMClient {
[email protected]e4097c82013-11-08 00:16:1242 public:
jianlif3e52af42015-01-21 23:18:4743 // Controls how GCM is being started. At first, GCMClient will be initialized
44 // and GCM store will be loaded. Then GCM connection may or may not be
45 // initiated depending on this enum value.
46 enum StartMode {
47 // GCM should be started only when it is being actually used. If no
48 // registration record is found, GCM will not kick off.
49 DELAYED_START,
50 // GCM should be started immediately.
51 IMMEDIATE_START
52 };
53
johnmeef71bd02017-02-09 17:45:5654 // Used for UMA. Can add enum values, but never renumber or delete and reuse.
[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.
johnmeef71bd02017-02-09 17:45:5672 UNKNOWN_ERROR,
73
74 // Used for UMA. Keep LAST_RESULT up to date and sync with histograms.xml.
75 LAST_RESULT = UNKNOWN_ERROR
[email protected]e4097c82013-11-08 00:16:1276 };
77
[email protected]8ad80512014-05-23 09:40:4778 enum ChromePlatform {
79 PLATFORM_WIN,
80 PLATFORM_MAC,
81 PLATFORM_LINUX,
82 PLATFORM_CROS,
83 PLATFORM_IOS,
84 PLATFORM_ANDROID,
85 PLATFORM_UNKNOWN
86 };
87
88 enum ChromeChannel {
89 CHANNEL_STABLE,
90 CHANNEL_BETA,
91 CHANNEL_DEV,
92 CHANNEL_CANARY,
93 CHANNEL_UNKNOWN
94 };
95
[email protected]cd57f372014-06-09 17:13:0696 struct ChromeBuildInfo {
[email protected]8ad80512014-05-23 09:40:4797 ChromeBuildInfo();
98 ~ChromeBuildInfo();
99
100 ChromePlatform platform;
101 ChromeChannel channel;
102 std::string version;
johnme627dc8c72016-08-19 21:49:39103 std::string product_category_for_subtypes;
[email protected]8ad80512014-05-23 09:40:47104 };
105
[email protected]c6fe36b2014-03-11 10:58:12106 // Detailed information of the Send Error event.
[email protected]cd57f372014-06-09 17:13:06107 struct SendErrorDetails {
[email protected]c6fe36b2014-03-11 10:58:12108 SendErrorDetails();
vmpstrb6449d512016-02-25 23:55:40109 SendErrorDetails(const SendErrorDetails& other);
[email protected]c6fe36b2014-03-11 10:58:12110 ~SendErrorDetails();
111
112 std::string message_id;
113 MessageData additional_data;
114 Result result;
115 };
116
[email protected]35601812014-03-07 19:52:43117 // Internal states and activity statistics of a GCM client.
[email protected]cd57f372014-06-09 17:13:06118 struct GCMStatistics {
[email protected]35601812014-03-07 19:52:43119 public:
120 GCMStatistics();
vmpstrb6449d512016-02-25 23:55:40121 GCMStatistics(const GCMStatistics& other);
[email protected]35601812014-03-07 19:52:43122 ~GCMStatistics();
123
[email protected]436bcb82014-04-18 00:40:57124 bool is_recording;
[email protected]35601812014-03-07 19:52:43125 bool gcm_client_created;
126 std::string gcm_client_state;
127 bool connection_client_created;
128 std::string connection_state;
zea76342abf2016-11-01 17:26:04129 base::Time last_checkin;
130 base::Time next_checkin;
avi26062922015-12-26 00:14:18131 uint64_t android_id;
[email protected]436bcb82014-04-18 00:40:57132 std::vector<std::string> registered_app_ids;
133 int send_queue_size;
134 int resend_queue_size;
135
[email protected]5da93802014-05-24 01:35:01136 RecordedActivities recorded_activities;
[email protected]35601812014-03-07 19:52:43137 };
138
fgorskic1047312014-09-04 16:48:54139 // Information about account.
140 struct AccountTokenInfo {
141 std::string account_id;
142 std::string email;
143 std::string access_token;
144 };
145
[email protected]e4097c82013-11-08 00:16:12146 // A delegate interface that allows the GCMClient instance to interact with
147 // its caller, i.e. notifying asynchronous event.
148 class Delegate {
149 public:
[email protected]e4097c82013-11-08 00:16:12150 // Called when the registration completed successfully or an error occurs.
jianli7a0c9b62015-05-26 23:24:47151 // |registration_info|: the specific information required for the
152 // registration.
[email protected]e4097c82013-11-08 00:16:12153 // |registration_id|: non-empty if the registration completed successfully.
154 // |result|: the type of the error if an error occured, success otherwise.
jianli7a0c9b62015-05-26 23:24:47155 virtual void OnRegisterFinished(
156 const linked_ptr<RegistrationInfo>& registration_info,
157 const std::string& registration_id,
158 Result result) = 0;
[email protected]e4097c82013-11-08 00:16:12159
[email protected]e4007042014-02-15 20:34:28160 // Called when the unregistration completed.
jianli7a0c9b62015-05-26 23:24:47161 // |registration_info|: the specific information required for the
162 // registration.
[email protected]0e88e1d12014-03-19 06:53:08163 // |result|: result of the unregistration.
jianli7a0c9b62015-05-26 23:24:47164 virtual void OnUnregisterFinished(
165 const linked_ptr<RegistrationInfo>& registration_info,
166 GCMClient::Result result) = 0;
[email protected]e4007042014-02-15 20:34:28167
[email protected]e4097c82013-11-08 00:16:12168 // Called when the message is scheduled to send successfully or an error
169 // occurs.
170 // |app_id|: application ID.
171 // |message_id|: ID of the message being sent.
172 // |result|: the type of the error if an error occured, success otherwise.
173 virtual void OnSendFinished(const std::string& app_id,
174 const std::string& message_id,
175 Result result) = 0;
176
177 // Called when a message has been received.
178 // |app_id|: application ID.
[email protected]e4097c82013-11-08 00:16:12179 // |message|: message received.
180 virtual void OnMessageReceived(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12181 const IncomingMessage& message) = 0;
182
183 // Called when some messages have been deleted from the server.
184 // |app_id|: application ID.
185 virtual void OnMessagesDeleted(const std::string& app_id) = 0;
186
187 // Called when a message failed to send to the server.
188 // |app_id|: application ID.
[email protected]c6fe36b2014-03-11 10:58:12189 // |send_error_detials|: Details of the send error event, like mesasge ID.
190 virtual void OnMessageSendError(
191 const std::string& app_id,
192 const SendErrorDetails& send_error_details) = 0;
[email protected]e4097c82013-11-08 00:16:12193
[email protected]292af2b22014-08-06 19:42:45194 // Called when a message was acknowledged by the GCM server.
195 // |app_id|: application ID.
196 // |message_id|: ID of the acknowledged message.
197 virtual void OnSendAcknowledged(const std::string& app_id,
198 const std::string& message_id) = 0;
199
[email protected]86625df2014-01-31 03:47:58200 // Called when the GCM becomes ready. To get to this state, GCMClient
201 // finished loading from the GCM store and retrieved the device check-in
202 // from the server if it hadn't yet.
fgorskid578c18b2014-09-24 23:40:17203 // |account_mappings|: a persisted list of accounts mapped to this GCM
204 // client.
fgorski5df101702014-10-28 02:09:31205 // |last_token_fetch_time|: time of a last successful token fetch.
206 virtual void OnGCMReady(const std::vector<AccountMapping>& account_mappings,
207 const base::Time& last_token_fetch_time) = 0;
[email protected]7de78802014-05-10 19:49:40208
209 // Called when activities are being recorded and a new activity has just
210 // been recorded.
211 virtual void OnActivityRecorded() = 0;
[email protected]fc6078a2014-06-14 08:28:43212
213 // Called when a new connection is established and a successful handshake
214 // has been performed.
215 virtual void OnConnected(const net::IPEndPoint& ip_endpoint) = 0;
216
217 // Called when the connection is interrupted.
218 virtual void OnDisconnected() = 0;
johnme93ec7932016-11-17 14:26:58219
220 // Called when the GCM store is reset (e.g. due to corruption), which
221 // changes the device ID, invalidating all prior registrations.
222 virtual void OnStoreReset() = 0;
[email protected]e4097c82013-11-08 00:16:12223 };
224
[email protected]0db118222014-01-22 01:37:59225 GCMClient();
226 virtual ~GCMClient();
[email protected]1b1c3cd2013-12-17 18:40:04227
[email protected]d3a4b2e2014-02-27 13:46:54228 // Begins initialization of the GCM Client. This will not trigger a
229 // connection.
[email protected]8ad80512014-05-23 09:40:47230 // |chrome_build_info|: chrome info, i.e., version, channel and etc.
[email protected]5799d052014-02-12 20:47:39231 // |store_path|: path to the GCM store.
232 // |blocking_task_runner|: for running blocking file tasks.
mmenkee65e7af2015-10-13 17:16:42233 // |url_request_context_getter|: for url requests. The GCMClient must be
234 // deleted before the Getter's underlying URLRequestContext.
[email protected]5799d052014-02-12 20:47:39235 // |delegate|: the delegate whose methods will be called asynchronously in
mmenkee65e7af2015-10-13 17:16:42236 // response to events and messages.
[email protected]e2a4a8012014-02-07 22:32:52237 virtual void Initialize(
[email protected]8ad80512014-05-23 09:40:47238 const ChromeBuildInfo& chrome_build_info,
[email protected]e2a4a8012014-02-07 22:32:52239 const base::FilePath& store_path,
240 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
241 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:39242 url_request_context_getter,
dchenga77e28eb2016-04-21 21:34:37243 std::unique_ptr<Encryptor> encryptor,
[email protected]5799d052014-02-12 20:47:39244 Delegate* delegate) = 0;
[email protected]e2a4a8012014-02-07 22:32:52245
jianlif3e52af42015-01-21 23:18:47246 // This will initiate the GCM connection only if |start_mode| means to start
247 // the GCM immediately or the GCM registration records are found in the store.
248 // Note that it is OK to call Start multiple times and the implementation
249 // should handle it gracefully.
250 virtual void Start(StartMode start_mode) = 0;
[email protected]d3a4b2e2014-02-27 13:46:54251
[email protected]21fee5482014-03-05 00:57:15252 // Stops using the GCM service. This will not erase the persisted data.
253 virtual void Stop() = 0;
254
jianli7a0c9b62015-05-26 23:24:47255 // Registers with the server to access the provided service.
256 // Delegate::OnRegisterFinished will be called asynchronously upon completion.
257 // |registration_info|: the specific information required for the
258 // registration. For GCM, it will contain app id and
259 // sender IDs. For InstanceID, it will contain app_id,
260 // authorized entity and scope.
261 virtual void Register(
262 const linked_ptr<RegistrationInfo>& registration_info) = 0;
[email protected]e4097c82013-11-08 00:16:12263
jianli7a0c9b62015-05-26 23:24:47264 // Unregisters from the server to stop accessing the provided service.
[email protected]e4097c82013-11-08 00:16:12265 // Delegate::OnUnregisterFinished will be called asynchronously upon
266 // completion.
jianli7a0c9b62015-05-26 23:24:47267 // |registration_info|: the specific information required for the
268 // registration. For GCM, it will contain app id (sender
269 // IDs can be ingored). For InstanceID, it will contain
270 // app id, authorized entity and scope.
271 virtual void Unregister(
272 const linked_ptr<RegistrationInfo>& registration_info) = 0;
[email protected]e4097c82013-11-08 00:16:12273
274 // Sends a message to a given receiver. Delegate::OnSendFinished will be
275 // called asynchronously upon completion.
[email protected]e4097c82013-11-08 00:16:12276 // |app_id|: application ID.
277 // |receiver_id|: registration ID of the receiver party.
278 // |message|: message to be sent.
[email protected]5799d052014-02-12 20:47:39279 virtual void Send(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12280 const std::string& receiver_id,
281 const OutgoingMessage& message) = 0;
[email protected]35601812014-03-07 19:52:43282
peter266a2aa42016-02-19 18:51:39283 // Records a decryption failure due to |result| for the |app_id|.
peteree284ba52016-02-01 11:53:28284 virtual void RecordDecryptionFailure(
285 const std::string& app_id,
peter266a2aa42016-02-19 18:51:39286 GCMEncryptionProvider::DecryptionResult result) = 0;
peteree284ba52016-02-01 11:53:28287
[email protected]436bcb82014-04-18 00:40:57288 // Enables or disables internal activity recording.
289 virtual void SetRecording(bool recording) = 0;
290
291 // Clear all recorded GCM activity logs.
292 virtual void ClearActivityLogs() = 0;
293
[email protected]35601812014-03-07 19:52:43294 // Gets internal states and statistics.
295 virtual GCMStatistics GetStatistics() const = 0;
[email protected]7df5ef22014-07-17 07:35:58296
297 // Sets a list of accounts with OAuth2 tokens for the next checkin.
fgorski58b9dfc2014-09-29 16:46:18298 // |account_tokens|: list of email addresses, account IDs and OAuth2 access
299 // tokens.
300 virtual void SetAccountTokens(
301 const std::vector<AccountTokenInfo>& account_tokens) = 0;
[email protected]72d4f252014-08-20 22:34:28302
303 // Persists the |account_mapping| in the store.
304 virtual void UpdateAccountMapping(const AccountMapping& account_mapping) = 0;
305
306 // Removes the account mapping related to |account_id| from the persistent
307 // store.
308 virtual void RemoveAccountMapping(const std::string& account_id) = 0;
fgorski5df101702014-10-28 02:09:31309
310 // Sets last token fetch time in persistent store.
311 virtual void SetLastTokenFetchTime(const base::Time& time) = 0;
chirantan192a9212014-12-06 03:30:45312
313 // Updates the timer used by the HeartbeatManager for sending heartbeats.
dchenga77e28eb2016-04-21 21:34:37314 virtual void UpdateHeartbeatTimer(std::unique_ptr<base::Timer> timer) = 0;
jianli10018b2d2015-05-11 21:14:13315
316 // Adds the Instance ID data for a specific app to the persistent store.
317 virtual void AddInstanceIDData(const std::string& app_id,
jianli7a0c9b62015-05-26 23:24:47318 const std::string& instance_id,
319 const std::string& extra_data) = 0;
jianli10018b2d2015-05-11 21:14:13320
321 // Removes the Instance ID data for a specific app from the persistent store.
322 virtual void RemoveInstanceIDData(const std::string& app_id) = 0;
323
324 // Retrieves the Instance ID data for a specific app from the persistent
325 // store.
jianli7a0c9b62015-05-26 23:24:47326 virtual void GetInstanceIDData(const std::string& app_id,
327 std::string* instance_id,
328 std::string* extra_data) = 0;
fgorski22754462015-05-14 00:05:22329
330 // Gets and sets custom heartbeat interval for the MCS connection.
331 // |scope| is used to identify the component that requests a custom interval
332 // to be set, and allows that component to later revoke the setting. It should
333 // be unique.
334 virtual void AddHeartbeatInterval(const std::string& scope,
335 int interval_ms) = 0;
336 virtual void RemoveHeartbeatInterval(const std::string& scope) = 0;
[email protected]e4097c82013-11-08 00:16:12337};
338
339} // namespace gcm
340
[email protected]cd57f372014-06-09 17:13:06341#endif // COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_