blob: b3f65431debdef1ebbcd4cb62e6d43840ec02df2 [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]fc6078a2014-06-14 08:28:4323class GURL;
24
[email protected]e2a4a8012014-02-07 22:32:5225namespace base {
26class FilePath;
27class SequencedTaskRunner;
chirantan192a9212014-12-06 03:30:4528class Timer;
[email protected]e2a4a8012014-02-07 22:32:5229}
30
[email protected]e2a4a8012014-02-07 22:32:5231namespace net {
[email protected]fc6078a2014-06-14 08:28:4332class IPEndPoint;
[email protected]e2a4a8012014-02-07 22:32:5233class URLRequestContextGetter;
34}
35
[email protected]e4097c82013-11-08 00:16:1236namespace gcm {
37
[email protected]446f73c22014-05-14 20:47:1838class Encryptor;
[email protected]72d4f252014-08-20 22:34:2839struct AccountMapping;
[email protected]446f73c22014-05-14 20:47:1840
[email protected]e4097c82013-11-08 00:16:1241// Interface that encapsulates the network communications with the Google Cloud
42// Messaging server. This interface is not supposed to be thread-safe.
[email protected]cd57f372014-06-09 17:13:0643class GCMClient {
[email protected]e4097c82013-11-08 00:16:1244 public:
jianlif3e52af42015-01-21 23:18:4745 // Controls how GCM is being started. At first, GCMClient will be initialized
46 // and GCM store will be loaded. Then GCM connection may or may not be
47 // initiated depending on this enum value.
48 enum StartMode {
49 // GCM should be started only when it is being actually used. If no
50 // registration record is found, GCM will not kick off.
51 DELAYED_START,
52 // GCM should be started immediately.
53 IMMEDIATE_START
54 };
55
[email protected]e4097c82013-11-08 00:16:1256 enum Result {
57 // Successful operation.
58 SUCCESS,
[email protected]b16a7c52013-11-20 01:18:5959 // Invalid parameter.
60 INVALID_PARAMETER,
[email protected]9d7e5c02014-05-21 03:09:0361 // GCM is disabled.
62 GCM_DISABLED,
[email protected]b16a7c52013-11-20 01:18:5963 // Previous asynchronous operation is still pending to finish. Certain
64 // operation, like register, is only allowed one at a time.
65 ASYNC_OPERATION_PENDING,
[email protected]e4097c82013-11-08 00:16:1266 // Network socket error.
67 NETWORK_ERROR,
68 // Problem at the server.
69 SERVER_ERROR,
70 // Exceeded the specified TTL during message sending.
71 TTL_EXCEEDED,
72 // Other errors.
73 UNKNOWN_ERROR
74 };
75
[email protected]8ad80512014-05-23 09:40:4776 enum ChromePlatform {
77 PLATFORM_WIN,
78 PLATFORM_MAC,
79 PLATFORM_LINUX,
80 PLATFORM_CROS,
81 PLATFORM_IOS,
82 PLATFORM_ANDROID,
83 PLATFORM_UNKNOWN
84 };
85
86 enum ChromeChannel {
87 CHANNEL_STABLE,
88 CHANNEL_BETA,
89 CHANNEL_DEV,
90 CHANNEL_CANARY,
91 CHANNEL_UNKNOWN
92 };
93
[email protected]cd57f372014-06-09 17:13:0694 struct ChromeBuildInfo {
[email protected]8ad80512014-05-23 09:40:4795 ChromeBuildInfo();
96 ~ChromeBuildInfo();
97
98 ChromePlatform platform;
99 ChromeChannel channel;
100 std::string version;
johnme627dc8c72016-08-19 21:49:39101 std::string product_category_for_subtypes;
[email protected]8ad80512014-05-23 09:40:47102 };
103
[email protected]c6fe36b2014-03-11 10:58:12104 // Detailed information of the Send Error event.
[email protected]cd57f372014-06-09 17:13:06105 struct SendErrorDetails {
[email protected]c6fe36b2014-03-11 10:58:12106 SendErrorDetails();
vmpstrb6449d512016-02-25 23:55:40107 SendErrorDetails(const SendErrorDetails& other);
[email protected]c6fe36b2014-03-11 10:58:12108 ~SendErrorDetails();
109
110 std::string message_id;
111 MessageData additional_data;
112 Result result;
113 };
114
[email protected]35601812014-03-07 19:52:43115 // Internal states and activity statistics of a GCM client.
[email protected]cd57f372014-06-09 17:13:06116 struct GCMStatistics {
[email protected]35601812014-03-07 19:52:43117 public:
118 GCMStatistics();
vmpstrb6449d512016-02-25 23:55:40119 GCMStatistics(const GCMStatistics& other);
[email protected]35601812014-03-07 19:52:43120 ~GCMStatistics();
121
[email protected]436bcb82014-04-18 00:40:57122 bool is_recording;
[email protected]35601812014-03-07 19:52:43123 bool gcm_client_created;
124 std::string gcm_client_state;
125 bool connection_client_created;
126 std::string connection_state;
zea76342abf2016-11-01 17:26:04127 base::Time last_checkin;
128 base::Time next_checkin;
avi26062922015-12-26 00:14:18129 uint64_t android_id;
[email protected]436bcb82014-04-18 00:40:57130 std::vector<std::string> registered_app_ids;
131 int send_queue_size;
132 int resend_queue_size;
133
[email protected]5da93802014-05-24 01:35:01134 RecordedActivities recorded_activities;
[email protected]35601812014-03-07 19:52:43135 };
136
fgorskic1047312014-09-04 16:48:54137 // Information about account.
138 struct AccountTokenInfo {
139 std::string account_id;
140 std::string email;
141 std::string access_token;
142 };
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.
jianli7a0c9b62015-05-26 23:24:47149 // |registration_info|: the specific information required for the
150 // registration.
[email protected]e4097c82013-11-08 00:16:12151 // |registration_id|: non-empty if the registration completed successfully.
152 // |result|: the type of the error if an error occured, success otherwise.
jianli7a0c9b62015-05-26 23:24:47153 virtual void OnRegisterFinished(
154 const linked_ptr<RegistrationInfo>& registration_info,
155 const std::string& registration_id,
156 Result result) = 0;
[email protected]e4097c82013-11-08 00:16:12157
[email protected]e4007042014-02-15 20:34:28158 // Called when the unregistration completed.
jianli7a0c9b62015-05-26 23:24:47159 // |registration_info|: the specific information required for the
160 // registration.
[email protected]0e88e1d12014-03-19 06:53:08161 // |result|: result of the unregistration.
jianli7a0c9b62015-05-26 23:24:47162 virtual void OnUnregisterFinished(
163 const linked_ptr<RegistrationInfo>& registration_info,
164 GCMClient::Result result) = 0;
[email protected]e4007042014-02-15 20:34:28165
[email protected]e4097c82013-11-08 00:16:12166 // Called when the message is scheduled to send successfully or an error
167 // occurs.
168 // |app_id|: application ID.
169 // |message_id|: ID of the message being sent.
170 // |result|: the type of the error if an error occured, success otherwise.
171 virtual void OnSendFinished(const std::string& app_id,
172 const std::string& message_id,
173 Result result) = 0;
174
175 // Called when a message has been received.
176 // |app_id|: application ID.
[email protected]e4097c82013-11-08 00:16:12177 // |message|: message received.
178 virtual void OnMessageReceived(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12179 const IncomingMessage& message) = 0;
180
181 // Called when some messages have been deleted from the server.
182 // |app_id|: application ID.
183 virtual void OnMessagesDeleted(const std::string& app_id) = 0;
184
185 // Called when a message failed to send to the server.
186 // |app_id|: application ID.
[email protected]c6fe36b2014-03-11 10:58:12187 // |send_error_detials|: Details of the send error event, like mesasge ID.
188 virtual void OnMessageSendError(
189 const std::string& app_id,
190 const SendErrorDetails& send_error_details) = 0;
[email protected]e4097c82013-11-08 00:16:12191
[email protected]292af2b22014-08-06 19:42:45192 // Called when a message was acknowledged by the GCM server.
193 // |app_id|: application ID.
194 // |message_id|: ID of the acknowledged message.
195 virtual void OnSendAcknowledged(const std::string& app_id,
196 const std::string& message_id) = 0;
197
[email protected]86625df2014-01-31 03:47:58198 // Called when the GCM becomes ready. To get to this state, GCMClient
199 // finished loading from the GCM store and retrieved the device check-in
200 // from the server if it hadn't yet.
fgorskid578c18b2014-09-24 23:40:17201 // |account_mappings|: a persisted list of accounts mapped to this GCM
202 // client.
fgorski5df101702014-10-28 02:09:31203 // |last_token_fetch_time|: time of a last successful token fetch.
204 virtual void OnGCMReady(const std::vector<AccountMapping>& account_mappings,
205 const base::Time& last_token_fetch_time) = 0;
[email protected]7de78802014-05-10 19:49:40206
207 // Called when activities are being recorded and a new activity has just
208 // been recorded.
209 virtual void OnActivityRecorded() = 0;
[email protected]fc6078a2014-06-14 08:28:43210
211 // Called when a new connection is established and a successful handshake
212 // has been performed.
213 virtual void OnConnected(const net::IPEndPoint& ip_endpoint) = 0;
214
215 // Called when the connection is interrupted.
216 virtual void OnDisconnected() = 0;
[email protected]e4097c82013-11-08 00:16:12217 };
218
[email protected]0db118222014-01-22 01:37:59219 GCMClient();
220 virtual ~GCMClient();
[email protected]1b1c3cd2013-12-17 18:40:04221
[email protected]d3a4b2e2014-02-27 13:46:54222 // Begins initialization of the GCM Client. This will not trigger a
223 // connection.
[email protected]8ad80512014-05-23 09:40:47224 // |chrome_build_info|: chrome info, i.e., version, channel and etc.
[email protected]5799d052014-02-12 20:47:39225 // |store_path|: path to the GCM store.
226 // |blocking_task_runner|: for running blocking file tasks.
mmenkee65e7af2015-10-13 17:16:42227 // |url_request_context_getter|: for url requests. The GCMClient must be
228 // deleted before the Getter's underlying URLRequestContext.
[email protected]5799d052014-02-12 20:47:39229 // |delegate|: the delegate whose methods will be called asynchronously in
mmenkee65e7af2015-10-13 17:16:42230 // response to events and messages.
[email protected]e2a4a8012014-02-07 22:32:52231 virtual void Initialize(
[email protected]8ad80512014-05-23 09:40:47232 const ChromeBuildInfo& chrome_build_info,
[email protected]e2a4a8012014-02-07 22:32:52233 const base::FilePath& store_path,
234 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
235 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:39236 url_request_context_getter,
dchenga77e28eb2016-04-21 21:34:37237 std::unique_ptr<Encryptor> encryptor,
[email protected]5799d052014-02-12 20:47:39238 Delegate* delegate) = 0;
[email protected]e2a4a8012014-02-07 22:32:52239
jianlif3e52af42015-01-21 23:18:47240 // This will initiate the GCM connection only if |start_mode| means to start
241 // the GCM immediately or the GCM registration records are found in the store.
242 // Note that it is OK to call Start multiple times and the implementation
243 // should handle it gracefully.
244 virtual void Start(StartMode start_mode) = 0;
[email protected]d3a4b2e2014-02-27 13:46:54245
[email protected]21fee5482014-03-05 00:57:15246 // Stops using the GCM service. This will not erase the persisted data.
247 virtual void Stop() = 0;
248
jianli7a0c9b62015-05-26 23:24:47249 // Registers with the server to access the provided service.
250 // Delegate::OnRegisterFinished will be called asynchronously upon completion.
251 // |registration_info|: the specific information required for the
252 // registration. For GCM, it will contain app id and
253 // sender IDs. For InstanceID, it will contain app_id,
254 // authorized entity and scope.
255 virtual void Register(
256 const linked_ptr<RegistrationInfo>& registration_info) = 0;
[email protected]e4097c82013-11-08 00:16:12257
jianli7a0c9b62015-05-26 23:24:47258 // Unregisters from the server to stop accessing the provided service.
[email protected]e4097c82013-11-08 00:16:12259 // Delegate::OnUnregisterFinished will be called asynchronously upon
260 // completion.
jianli7a0c9b62015-05-26 23:24:47261 // |registration_info|: the specific information required for the
262 // registration. For GCM, it will contain app id (sender
263 // IDs can be ingored). For InstanceID, it will contain
264 // app id, authorized entity and scope.
265 virtual void Unregister(
266 const linked_ptr<RegistrationInfo>& registration_info) = 0;
[email protected]e4097c82013-11-08 00:16:12267
268 // Sends a message to a given receiver. Delegate::OnSendFinished will be
269 // called asynchronously upon completion.
[email protected]e4097c82013-11-08 00:16:12270 // |app_id|: application ID.
271 // |receiver_id|: registration ID of the receiver party.
272 // |message|: message to be sent.
[email protected]5799d052014-02-12 20:47:39273 virtual void Send(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12274 const std::string& receiver_id,
275 const OutgoingMessage& message) = 0;
[email protected]35601812014-03-07 19:52:43276
peter266a2aa42016-02-19 18:51:39277 // Records a decryption failure due to |result| for the |app_id|.
peteree284ba52016-02-01 11:53:28278 virtual void RecordDecryptionFailure(
279 const std::string& app_id,
peter266a2aa42016-02-19 18:51:39280 GCMEncryptionProvider::DecryptionResult result) = 0;
peteree284ba52016-02-01 11:53:28281
[email protected]436bcb82014-04-18 00:40:57282 // Enables or disables internal activity recording.
283 virtual void SetRecording(bool recording) = 0;
284
285 // Clear all recorded GCM activity logs.
286 virtual void ClearActivityLogs() = 0;
287
[email protected]35601812014-03-07 19:52:43288 // Gets internal states and statistics.
289 virtual GCMStatistics GetStatistics() const = 0;
[email protected]7df5ef22014-07-17 07:35:58290
291 // Sets a list of accounts with OAuth2 tokens for the next checkin.
fgorski58b9dfc2014-09-29 16:46:18292 // |account_tokens|: list of email addresses, account IDs and OAuth2 access
293 // tokens.
294 virtual void SetAccountTokens(
295 const std::vector<AccountTokenInfo>& account_tokens) = 0;
[email protected]72d4f252014-08-20 22:34:28296
297 // Persists the |account_mapping| in the store.
298 virtual void UpdateAccountMapping(const AccountMapping& account_mapping) = 0;
299
300 // Removes the account mapping related to |account_id| from the persistent
301 // store.
302 virtual void RemoveAccountMapping(const std::string& account_id) = 0;
fgorski5df101702014-10-28 02:09:31303
304 // Sets last token fetch time in persistent store.
305 virtual void SetLastTokenFetchTime(const base::Time& time) = 0;
chirantan192a9212014-12-06 03:30:45306
307 // Updates the timer used by the HeartbeatManager for sending heartbeats.
dchenga77e28eb2016-04-21 21:34:37308 virtual void UpdateHeartbeatTimer(std::unique_ptr<base::Timer> timer) = 0;
jianli10018b2d2015-05-11 21:14:13309
310 // Adds the Instance ID data for a specific app to the persistent store.
311 virtual void AddInstanceIDData(const std::string& app_id,
jianli7a0c9b62015-05-26 23:24:47312 const std::string& instance_id,
313 const std::string& extra_data) = 0;
jianli10018b2d2015-05-11 21:14:13314
315 // Removes the Instance ID data for a specific app from the persistent store.
316 virtual void RemoveInstanceIDData(const std::string& app_id) = 0;
317
318 // Retrieves the Instance ID data for a specific app from the persistent
319 // store.
jianli7a0c9b62015-05-26 23:24:47320 virtual void GetInstanceIDData(const std::string& app_id,
321 std::string* instance_id,
322 std::string* extra_data) = 0;
fgorski22754462015-05-14 00:05:22323
324 // Gets and sets custom heartbeat interval for the MCS connection.
325 // |scope| is used to identify the component that requests a custom interval
326 // to be set, and allows that component to later revoke the setting. It should
327 // be unique.
328 virtual void AddHeartbeatInterval(const std::string& scope,
329 int interval_ms) = 0;
330 virtual void RemoveHeartbeatInterval(const std::string& scope) = 0;
[email protected]e4097c82013-11-08 00:16:12331};
332
333} // namespace gcm
334
[email protected]cd57f372014-06-09 17:13:06335#endif // COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_