blob: 0242d9d935b46b5f9bc33aa153f1c1f172ae1354 [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
Rayan Kanso3da434f2018-12-19 17:09:3915#include "base/memory/scoped_refptr.h"
Rayan Kanso7b3b5ea2019-05-08 18:15:0016#include "components/gcm_driver/common/gcm_message.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"
Takuto Ikutaaa3b796c2019-02-06 02:54:5619#include "services/network/public/mojom/proxy_resolving_socket.mojom-forward.h"
[email protected]e4097c82013-11-08 00:16:1220
[email protected]e2a4a8012014-02-07 22:32:5221namespace base {
22class FilePath;
tzikb6c66b22018-07-06 12:14:1623class RetainingOneShotTimer;
[email protected]e2a4a8012014-02-07 22:32:5224class SequencedTaskRunner;
Rayan Kanso3da434f2018-12-19 17:09:3925} // namespace base
[email protected]e2a4a8012014-02-07 22:32:5226
[email protected]e2a4a8012014-02-07 22:32:5227namespace net {
[email protected]fc6078a2014-06-14 08:28:4328class IPEndPoint;
Rayan Kanso3da434f2018-12-19 17:09:3929} // namespace net
[email protected]e2a4a8012014-02-07 22:32:5230
Maks Orlovichc70c93c2018-07-12 02:45:4431namespace network {
Robbie McElrathb01499332018-09-25 00:53:1332class NetworkConnectionTracker;
Maks Orlovichc70c93c2018-07-12 02:45:4433class SharedURLLoaderFactory;
Rayan Kanso3da434f2018-12-19 17:09:3934} // namespace network
Maks Orlovichc70c93c2018-07-12 02:45:4435
[email protected]e4097c82013-11-08 00:16:1236namespace gcm {
37
[email protected]72d4f252014-08-20 22:34:2838struct AccountMapping;
Peter Beverlooa376e98c2017-06-27 15:55:3739class Encryptor;
40enum class GCMDecryptionResult;
[email protected]446f73c22014-05-14 20:47:1841
[email protected]e4097c82013-11-08 00:16:1242// Interface that encapsulates the network communications with the Google Cloud
43// Messaging server. This interface is not supposed to be thread-safe.
[email protected]cd57f372014-06-09 17:13:0644class GCMClient {
[email protected]e4097c82013-11-08 00:16:1245 public:
jianlif3e52af42015-01-21 23:18:4746 // Controls how GCM is being started. At first, GCMClient will be initialized
47 // and GCM store will be loaded. Then GCM connection may or may not be
48 // initiated depending on this enum value.
49 enum StartMode {
50 // GCM should be started only when it is being actually used. If no
51 // registration record is found, GCM will not kick off.
52 DELAYED_START,
53 // GCM should be started immediately.
54 IMMEDIATE_START
55 };
56
johnmeef71bd02017-02-09 17:45:5657 // Used for UMA. Can add enum values, but never renumber or delete and reuse.
[email protected]e4097c82013-11-08 00:16:1258 enum Result {
59 // Successful operation.
60 SUCCESS,
[email protected]b16a7c52013-11-20 01:18:5961 // Invalid parameter.
62 INVALID_PARAMETER,
[email protected]9d7e5c02014-05-21 03:09:0363 // GCM is disabled.
64 GCM_DISABLED,
[email protected]b16a7c52013-11-20 01:18:5965 // Previous asynchronous operation is still pending to finish. Certain
66 // operation, like register, is only allowed one at a time.
67 ASYNC_OPERATION_PENDING,
[email protected]e4097c82013-11-08 00:16:1268 // Network socket error.
69 NETWORK_ERROR,
70 // Problem at the server.
71 SERVER_ERROR,
72 // Exceeded the specified TTL during message sending.
73 TTL_EXCEEDED,
74 // Other errors.
johnmeef71bd02017-02-09 17:45:5675 UNKNOWN_ERROR,
76
77 // Used for UMA. Keep LAST_RESULT up to date and sync with histograms.xml.
78 LAST_RESULT = UNKNOWN_ERROR
[email protected]e4097c82013-11-08 00:16:1279 };
80
[email protected]8ad80512014-05-23 09:40:4781 enum ChromePlatform {
82 PLATFORM_WIN,
83 PLATFORM_MAC,
84 PLATFORM_LINUX,
85 PLATFORM_CROS,
86 PLATFORM_IOS,
87 PLATFORM_ANDROID,
Daniel Bratelle3c10f32018-02-05 15:03:3888 PLATFORM_UNSPECIFIED
[email protected]8ad80512014-05-23 09:40:4789 };
90
91 enum ChromeChannel {
92 CHANNEL_STABLE,
93 CHANNEL_BETA,
94 CHANNEL_DEV,
95 CHANNEL_CANARY,
96 CHANNEL_UNKNOWN
97 };
98
[email protected]cd57f372014-06-09 17:13:0699 struct ChromeBuildInfo {
[email protected]8ad80512014-05-23 09:40:47100 ChromeBuildInfo();
101 ~ChromeBuildInfo();
102
103 ChromePlatform platform;
104 ChromeChannel channel;
105 std::string version;
johnme627dc8c72016-08-19 21:49:39106 std::string product_category_for_subtypes;
[email protected]8ad80512014-05-23 09:40:47107 };
108
[email protected]c6fe36b2014-03-11 10:58:12109 // Detailed information of the Send Error event.
[email protected]cd57f372014-06-09 17:13:06110 struct SendErrorDetails {
[email protected]c6fe36b2014-03-11 10:58:12111 SendErrorDetails();
vmpstrb6449d512016-02-25 23:55:40112 SendErrorDetails(const SendErrorDetails& other);
[email protected]c6fe36b2014-03-11 10:58:12113 ~SendErrorDetails();
114
115 std::string message_id;
116 MessageData additional_data;
117 Result result;
118 };
119
[email protected]35601812014-03-07 19:52:43120 // Internal states and activity statistics of a GCM client.
[email protected]cd57f372014-06-09 17:13:06121 struct GCMStatistics {
[email protected]35601812014-03-07 19:52:43122 public:
123 GCMStatistics();
vmpstrb6449d512016-02-25 23:55:40124 GCMStatistics(const GCMStatistics& other);
[email protected]35601812014-03-07 19:52:43125 ~GCMStatistics();
126
[email protected]436bcb82014-04-18 00:40:57127 bool is_recording;
[email protected]35601812014-03-07 19:52:43128 bool gcm_client_created;
129 std::string gcm_client_state;
130 bool connection_client_created;
131 std::string connection_state;
zea76342abf2016-11-01 17:26:04132 base::Time last_checkin;
133 base::Time next_checkin;
avi26062922015-12-26 00:14:18134 uint64_t android_id;
Peter Beverloof9469182018-05-03 20:46:52135 uint64_t android_secret;
[email protected]436bcb82014-04-18 00:40:57136 std::vector<std::string> registered_app_ids;
137 int send_queue_size;
138 int resend_queue_size;
139
[email protected]5da93802014-05-24 01:35:01140 RecordedActivities recorded_activities;
[email protected]35601812014-03-07 19:52:43141 };
142
fgorskic1047312014-09-04 16:48:54143 // Information about account.
144 struct AccountTokenInfo {
145 std::string account_id;
146 std::string email;
147 std::string access_token;
148 };
149
[email protected]e4097c82013-11-08 00:16:12150 // A delegate interface that allows the GCMClient instance to interact with
151 // its caller, i.e. notifying asynchronous event.
152 class Delegate {
153 public:
[email protected]e4097c82013-11-08 00:16:12154 // Called when the registration completed successfully or an error occurs.
jianli7a0c9b62015-05-26 23:24:47155 // |registration_info|: the specific information required for the
156 // registration.
[email protected]e4097c82013-11-08 00:16:12157 // |registration_id|: non-empty if the registration completed successfully.
158 // |result|: the type of the error if an error occured, success otherwise.
jianli7a0c9b62015-05-26 23:24:47159 virtual void OnRegisterFinished(
Rayan Kanso3da434f2018-12-19 17:09:39160 scoped_refptr<RegistrationInfo> registration_info,
jianli7a0c9b62015-05-26 23:24:47161 const std::string& registration_id,
162 Result result) = 0;
[email protected]e4097c82013-11-08 00:16:12163
[email protected]e4007042014-02-15 20:34:28164 // Called when the unregistration completed.
jianli7a0c9b62015-05-26 23:24:47165 // |registration_info|: the specific information required for the
166 // registration.
[email protected]0e88e1d12014-03-19 06:53:08167 // |result|: result of the unregistration.
jianli7a0c9b62015-05-26 23:24:47168 virtual void OnUnregisterFinished(
Rayan Kanso3da434f2018-12-19 17:09:39169 scoped_refptr<RegistrationInfo> registration_info,
jianli7a0c9b62015-05-26 23:24:47170 GCMClient::Result result) = 0;
[email protected]e4007042014-02-15 20:34:28171
[email protected]e4097c82013-11-08 00:16:12172 // Called when the message is scheduled to send successfully or an error
173 // occurs.
174 // |app_id|: application ID.
175 // |message_id|: ID of the message being sent.
176 // |result|: the type of the error if an error occured, success otherwise.
177 virtual void OnSendFinished(const std::string& app_id,
178 const std::string& message_id,
179 Result result) = 0;
180
181 // Called when a message has been received.
182 // |app_id|: application ID.
[email protected]e4097c82013-11-08 00:16:12183 // |message|: message received.
184 virtual void OnMessageReceived(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12185 const IncomingMessage& message) = 0;
186
187 // Called when some messages have been deleted from the server.
188 // |app_id|: application ID.
189 virtual void OnMessagesDeleted(const std::string& app_id) = 0;
190
191 // Called when a message failed to send to the server.
192 // |app_id|: application ID.
[email protected]c6fe36b2014-03-11 10:58:12193 // |send_error_detials|: Details of the send error event, like mesasge ID.
194 virtual void OnMessageSendError(
195 const std::string& app_id,
196 const SendErrorDetails& send_error_details) = 0;
[email protected]e4097c82013-11-08 00:16:12197
[email protected]292af2b22014-08-06 19:42:45198 // Called when a message was acknowledged by the GCM server.
199 // |app_id|: application ID.
200 // |message_id|: ID of the acknowledged message.
201 virtual void OnSendAcknowledged(const std::string& app_id,
202 const std::string& message_id) = 0;
203
[email protected]86625df2014-01-31 03:47:58204 // Called when the GCM becomes ready. To get to this state, GCMClient
205 // finished loading from the GCM store and retrieved the device check-in
206 // from the server if it hadn't yet.
fgorskid578c18b2014-09-24 23:40:17207 // |account_mappings|: a persisted list of accounts mapped to this GCM
208 // client.
fgorski5df101702014-10-28 02:09:31209 // |last_token_fetch_time|: time of a last successful token fetch.
210 virtual void OnGCMReady(const std::vector<AccountMapping>& account_mappings,
211 const base::Time& last_token_fetch_time) = 0;
[email protected]7de78802014-05-10 19:49:40212
213 // Called when activities are being recorded and a new activity has just
214 // been recorded.
215 virtual void OnActivityRecorded() = 0;
[email protected]fc6078a2014-06-14 08:28:43216
217 // Called when a new connection is established and a successful handshake
218 // has been performed.
219 virtual void OnConnected(const net::IPEndPoint& ip_endpoint) = 0;
220
221 // Called when the connection is interrupted.
222 virtual void OnDisconnected() = 0;
johnme93ec7932016-11-17 14:26:58223
224 // Called when the GCM store is reset (e.g. due to corruption), which
225 // changes the device ID, invalidating all prior registrations.
226 virtual void OnStoreReset() = 0;
[email protected]e4097c82013-11-08 00:16:12227 };
228
[email protected]0db118222014-01-22 01:37:59229 GCMClient();
230 virtual ~GCMClient();
[email protected]1b1c3cd2013-12-17 18:40:04231
[email protected]d3a4b2e2014-02-27 13:46:54232 // Begins initialization of the GCM Client. This will not trigger a
Steven Zhub36394c2019-06-04 16:10:46233 // connection. Must be called on |io_task_runner|.
[email protected]8ad80512014-05-23 09:40:47234 // |chrome_build_info|: chrome info, i.e., version, channel and etc.
[email protected]5799d052014-02-12 20:47:39235 // |store_path|: path to the GCM store.
236 // |blocking_task_runner|: for running blocking file tasks.
Steven Zhub36394c2019-06-04 16:10:46237 // |io_task_runner|: for running IO tasks. When provided, it could be a
238 // wrapper on top of base::ThreadTaskRunnerHandle::Get() to provide power
239 // management featueres so that a delayed task posted to it can wake the
240 // system up from sleep to perform the task.
Helen Li5f3d96a2018-08-10 20:37:24241 // |get_socket_factory_callback|: a callback that can accept a request for a
242 // network::mojom::ProxyResolvingSocketFactoryPtr. It needs to be safe to
243 // run on any thread.
[email protected]5799d052014-02-12 20:47:39244 // |delegate|: the delegate whose methods will be called asynchronously in
mmenkee65e7af2015-10-13 17:16:42245 // response to events and messages.
[email protected]e2a4a8012014-02-07 22:32:52246 virtual void Initialize(
[email protected]8ad80512014-05-23 09:40:47247 const ChromeBuildInfo& chrome_build_info,
[email protected]e2a4a8012014-02-07 22:32:52248 const base::FilePath& store_path,
249 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
Steven Zhub36394c2019-06-04 16:10:46250 scoped_refptr<base::SequencedTaskRunner> io_task_runner,
Helen Li5f3d96a2018-08-10 20:37:24251 base::RepeatingCallback<
252 void(network::mojom::ProxyResolvingSocketFactoryRequest)>
253 get_socket_factory_callback,
Maks Orlovichc70c93c2018-07-12 02:45:44254 const scoped_refptr<network::SharedURLLoaderFactory>& url_loader_factory,
Robbie McElrathb01499332018-09-25 00:53:13255 network::NetworkConnectionTracker* network_connection_tracker_,
dchenga77e28eb2016-04-21 21:34:37256 std::unique_ptr<Encryptor> encryptor,
[email protected]5799d052014-02-12 20:47:39257 Delegate* delegate) = 0;
[email protected]e2a4a8012014-02-07 22:32:52258
jianlif3e52af42015-01-21 23:18:47259 // This will initiate the GCM connection only if |start_mode| means to start
260 // the GCM immediately or the GCM registration records are found in the store.
261 // Note that it is OK to call Start multiple times and the implementation
262 // should handle it gracefully.
263 virtual void Start(StartMode start_mode) = 0;
[email protected]d3a4b2e2014-02-27 13:46:54264
[email protected]21fee5482014-03-05 00:57:15265 // Stops using the GCM service. This will not erase the persisted data.
266 virtual void Stop() = 0;
267
jianli7a0c9b62015-05-26 23:24:47268 // Registers with the server to access the provided service.
269 // Delegate::OnRegisterFinished will be called asynchronously upon completion.
270 // |registration_info|: the specific information required for the
271 // registration. For GCM, it will contain app id and
272 // sender IDs. For InstanceID, it will contain app_id,
273 // authorized entity and scope.
Rayan Kanso3da434f2018-12-19 17:09:39274 virtual void Register(scoped_refptr<RegistrationInfo> registration_info) = 0;
[email protected]e4097c82013-11-08 00:16:12275
johnme6576ecf2017-04-03 19:26:28276 // Checks that the provided |registration_id| (aka token for Instance ID
277 // registrations) matches the stored registration info. Also checks sender IDs
278 // match for GCM registrations.
279 virtual bool ValidateRegistration(
Rayan Kanso3da434f2018-12-19 17:09:39280 scoped_refptr<RegistrationInfo> registration_info,
johnme6576ecf2017-04-03 19:26:28281 const std::string& registration_id) = 0;
282
jianli7a0c9b62015-05-26 23:24:47283 // Unregisters from the server to stop accessing the provided service.
[email protected]e4097c82013-11-08 00:16:12284 // Delegate::OnUnregisterFinished will be called asynchronously upon
285 // completion.
jianli7a0c9b62015-05-26 23:24:47286 // |registration_info|: the specific information required for the
287 // registration. For GCM, it will contain app id (sender
288 // IDs can be ingored). For InstanceID, it will contain
289 // app id, authorized entity and scope.
290 virtual void Unregister(
Rayan Kanso3da434f2018-12-19 17:09:39291 scoped_refptr<RegistrationInfo> registration_info) = 0;
[email protected]e4097c82013-11-08 00:16:12292
293 // Sends a message to a given receiver. Delegate::OnSendFinished will be
294 // called asynchronously upon completion.
[email protected]e4097c82013-11-08 00:16:12295 // |app_id|: application ID.
296 // |receiver_id|: registration ID of the receiver party.
297 // |message|: message to be sent.
[email protected]5799d052014-02-12 20:47:39298 virtual void Send(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12299 const std::string& receiver_id,
300 const OutgoingMessage& message) = 0;
[email protected]35601812014-03-07 19:52:43301
peter266a2aa42016-02-19 18:51:39302 // Records a decryption failure due to |result| for the |app_id|.
Peter Beverlooa376e98c2017-06-27 15:55:37303 virtual void RecordDecryptionFailure(const std::string& app_id,
304 GCMDecryptionResult result) = 0;
peteree284ba52016-02-01 11:53:28305
[email protected]436bcb82014-04-18 00:40:57306 // Enables or disables internal activity recording.
307 virtual void SetRecording(bool recording) = 0;
308
309 // Clear all recorded GCM activity logs.
310 virtual void ClearActivityLogs() = 0;
311
[email protected]35601812014-03-07 19:52:43312 // Gets internal states and statistics.
313 virtual GCMStatistics GetStatistics() const = 0;
[email protected]7df5ef22014-07-17 07:35:58314
315 // Sets a list of accounts with OAuth2 tokens for the next checkin.
fgorski58b9dfc2014-09-29 16:46:18316 // |account_tokens|: list of email addresses, account IDs and OAuth2 access
317 // tokens.
318 virtual void SetAccountTokens(
319 const std::vector<AccountTokenInfo>& account_tokens) = 0;
[email protected]72d4f252014-08-20 22:34:28320
321 // Persists the |account_mapping| in the store.
322 virtual void UpdateAccountMapping(const AccountMapping& account_mapping) = 0;
323
324 // Removes the account mapping related to |account_id| from the persistent
325 // store.
326 virtual void RemoveAccountMapping(const std::string& account_id) = 0;
fgorski5df101702014-10-28 02:09:31327
328 // Sets last token fetch time in persistent store.
329 virtual void SetLastTokenFetchTime(const base::Time& time) = 0;
chirantan192a9212014-12-06 03:30:45330
331 // Updates the timer used by the HeartbeatManager for sending heartbeats.
tzikb6c66b22018-07-06 12:14:16332 virtual void UpdateHeartbeatTimer(
333 std::unique_ptr<base::RetainingOneShotTimer> timer) = 0;
jianli10018b2d2015-05-11 21:14:13334
335 // Adds the Instance ID data for a specific app to the persistent store.
336 virtual void AddInstanceIDData(const std::string& app_id,
jianli7a0c9b62015-05-26 23:24:47337 const std::string& instance_id,
338 const std::string& extra_data) = 0;
jianli10018b2d2015-05-11 21:14:13339
340 // Removes the Instance ID data for a specific app from the persistent store.
341 virtual void RemoveInstanceIDData(const std::string& app_id) = 0;
342
343 // Retrieves the Instance ID data for a specific app from the persistent
344 // store.
jianli7a0c9b62015-05-26 23:24:47345 virtual void GetInstanceIDData(const std::string& app_id,
346 std::string* instance_id,
347 std::string* extra_data) = 0;
fgorski22754462015-05-14 00:05:22348
349 // Gets and sets custom heartbeat interval for the MCS connection.
350 // |scope| is used to identify the component that requests a custom interval
351 // to be set, and allows that component to later revoke the setting. It should
352 // be unique.
353 virtual void AddHeartbeatInterval(const std::string& scope,
354 int interval_ms) = 0;
355 virtual void RemoveHeartbeatInterval(const std::string& scope) = 0;
[email protected]e4097c82013-11-08 00:16:12356};
357
358} // namespace gcm
359
[email protected]cd57f372014-06-09 17:13:06360#endif // COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_