blob: 02111c866858489f029c899de10ca6d10c30900e [file] [log] [blame]
[email protected]e4097c82013-11-08 00:16:121// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef GOOGLE_APIS_GCM_GCM_CLIENT_H_
6#define GOOGLE_APIS_GCM_GCM_CLIENT_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
12#include "base/basictypes.h"
13#include "google_apis/gcm/base/gcm_export.h"
[email protected]436bcb82014-04-18 00:40:5714#include "google_apis/gcm/monitoring/gcm_stats_recorder.h"
[email protected]e4097c82013-11-08 00:16:1215
[email protected]e2a4a8012014-02-07 22:32:5216template <class T> class scoped_refptr;
17
18namespace base {
19class FilePath;
20class SequencedTaskRunner;
21}
22
[email protected]e2a4a8012014-02-07 22:32:5223namespace net {
24class URLRequestContextGetter;
25}
26
[email protected]e4097c82013-11-08 00:16:1227namespace gcm {
28
[email protected]446f73c22014-05-14 20:47:1829class Encryptor;
30
[email protected]e4097c82013-11-08 00:16:1231// Interface that encapsulates the network communications with the Google Cloud
32// Messaging server. This interface is not supposed to be thread-safe.
33class GCM_EXPORT GCMClient {
34 public:
35 enum Result {
36 // Successful operation.
37 SUCCESS,
[email protected]b16a7c52013-11-20 01:18:5938 // Invalid parameter.
39 INVALID_PARAMETER,
[email protected]9d7e5c02014-05-21 03:09:0340 // GCM is disabled.
41 GCM_DISABLED,
[email protected]c7f7b532014-01-24 07:24:4542 // Profile not signed in.
43 NOT_SIGNED_IN,
[email protected]b16a7c52013-11-20 01:18:5944 // Previous asynchronous operation is still pending to finish. Certain
45 // operation, like register, is only allowed one at a time.
46 ASYNC_OPERATION_PENDING,
[email protected]e4097c82013-11-08 00:16:1247 // Network socket error.
48 NETWORK_ERROR,
49 // Problem at the server.
50 SERVER_ERROR,
51 // Exceeded the specified TTL during message sending.
52 TTL_EXCEEDED,
53 // Other errors.
54 UNKNOWN_ERROR
55 };
56
[email protected]8ad80512014-05-23 09:40:4757 enum ChromePlatform {
58 PLATFORM_WIN,
59 PLATFORM_MAC,
60 PLATFORM_LINUX,
61 PLATFORM_CROS,
62 PLATFORM_IOS,
63 PLATFORM_ANDROID,
64 PLATFORM_UNKNOWN
65 };
66
67 enum ChromeChannel {
68 CHANNEL_STABLE,
69 CHANNEL_BETA,
70 CHANNEL_DEV,
71 CHANNEL_CANARY,
72 CHANNEL_UNKNOWN
73 };
74
75 struct GCM_EXPORT ChromeBuildInfo {
76 ChromeBuildInfo();
77 ~ChromeBuildInfo();
78
79 ChromePlatform platform;
80 ChromeChannel channel;
81 std::string version;
82 };
83
[email protected]e4097c82013-11-08 00:16:1284 // Message data consisting of key-value pairs.
[email protected]b16a7c52013-11-20 01:18:5985 typedef std::map<std::string, std::string> MessageData;
[email protected]e4097c82013-11-08 00:16:1286
87 // Message to be delivered to the other party.
[email protected]b16a7c52013-11-20 01:18:5988 struct GCM_EXPORT OutgoingMessage {
[email protected]e4097c82013-11-08 00:16:1289 OutgoingMessage();
90 ~OutgoingMessage();
91
92 // Message ID.
93 std::string id;
94 // In seconds.
95 int time_to_live;
96 MessageData data;
[email protected]b20aece22014-05-09 22:34:0897
98 static const int kMaximumTTL = 4 * 7 * 24 * 60 * 60; // 4 weeks.
[email protected]e4097c82013-11-08 00:16:1299 };
100
101 // Message being received from the other party.
[email protected]b16a7c52013-11-20 01:18:59102 struct GCM_EXPORT IncomingMessage {
[email protected]e4097c82013-11-08 00:16:12103 IncomingMessage();
104 ~IncomingMessage();
105
106 MessageData data;
[email protected]40113aa2014-02-28 21:37:56107 std::string collapse_key;
[email protected]8d3af382014-03-07 00:58:41108 std::string sender_id;
[email protected]e4097c82013-11-08 00:16:12109 };
110
[email protected]c6fe36b2014-03-11 10:58:12111 // Detailed information of the Send Error event.
112 struct GCM_EXPORT SendErrorDetails {
113 SendErrorDetails();
114 ~SendErrorDetails();
115
116 std::string message_id;
117 MessageData additional_data;
118 Result result;
119 };
120
[email protected]35601812014-03-07 19:52:43121 // Internal states and activity statistics of a GCM client.
122 struct GCM_EXPORT GCMStatistics {
123 public:
124 GCMStatistics();
125 ~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;
132 uint64 android_id;
[email protected]436bcb82014-04-18 00:40:57133 std::vector<std::string> registered_app_ids;
134 int send_queue_size;
135 int resend_queue_size;
136
[email protected]9df5b932014-04-30 00:39:06137 GCMStatsRecorder::RecordedActivities recorded_activities;
[email protected]35601812014-03-07 19:52:43138 };
139
[email protected]e4097c82013-11-08 00:16:12140 // A delegate interface that allows the GCMClient instance to interact with
141 // its caller, i.e. notifying asynchronous event.
142 class Delegate {
143 public:
[email protected]e4097c82013-11-08 00:16:12144 // Called when the registration completed successfully or an error occurs.
145 // |app_id|: application ID.
146 // |registration_id|: non-empty if the registration completed successfully.
147 // |result|: the type of the error if an error occured, success otherwise.
148 virtual void OnRegisterFinished(const std::string& app_id,
149 const std::string& registration_id,
150 Result result) = 0;
151
[email protected]e4007042014-02-15 20:34:28152 // Called when the unregistration completed.
153 // |app_id|: application ID.
[email protected]0e88e1d12014-03-19 06:53:08154 // |result|: result of the unregistration.
[email protected]e4007042014-02-15 20:34:28155 virtual void OnUnregisterFinished(const std::string& app_id,
[email protected]0e88e1d12014-03-19 06:53:08156 GCMClient::Result result) = 0;
[email protected]e4007042014-02-15 20:34:28157
[email protected]e4097c82013-11-08 00:16:12158 // Called when the message is scheduled to send successfully or an error
159 // occurs.
160 // |app_id|: application ID.
161 // |message_id|: ID of the message being sent.
162 // |result|: the type of the error if an error occured, success otherwise.
163 virtual void OnSendFinished(const std::string& app_id,
164 const std::string& message_id,
165 Result result) = 0;
166
167 // Called when a message has been received.
168 // |app_id|: application ID.
[email protected]e4097c82013-11-08 00:16:12169 // |message|: message received.
170 virtual void OnMessageReceived(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12171 const IncomingMessage& message) = 0;
172
173 // Called when some messages have been deleted from the server.
174 // |app_id|: application ID.
175 virtual void OnMessagesDeleted(const std::string& app_id) = 0;
176
177 // Called when a message failed to send to the server.
178 // |app_id|: application ID.
[email protected]c6fe36b2014-03-11 10:58:12179 // |send_error_detials|: Details of the send error event, like mesasge ID.
180 virtual void OnMessageSendError(
181 const std::string& app_id,
182 const SendErrorDetails& send_error_details) = 0;
[email protected]e4097c82013-11-08 00:16:12183
[email protected]86625df2014-01-31 03:47:58184 // Called when the GCM becomes ready. To get to this state, GCMClient
185 // finished loading from the GCM store and retrieved the device check-in
186 // from the server if it hadn't yet.
187 virtual void OnGCMReady() = 0;
[email protected]7de78802014-05-10 19:49:40188
189 // Called when activities are being recorded and a new activity has just
190 // been recorded.
191 virtual void OnActivityRecorded() = 0;
[email protected]e4097c82013-11-08 00:16:12192 };
193
[email protected]0db118222014-01-22 01:37:59194 GCMClient();
195 virtual ~GCMClient();
[email protected]1b1c3cd2013-12-17 18:40:04196
[email protected]d3a4b2e2014-02-27 13:46:54197 // Begins initialization of the GCM Client. This will not trigger a
198 // connection.
[email protected]8ad80512014-05-23 09:40:47199 // |chrome_build_info|: chrome info, i.e., version, channel and etc.
[email protected]5799d052014-02-12 20:47:39200 // |store_path|: path to the GCM store.
[email protected]495a7db92014-02-22 07:49:59201 // |account_ids|: account IDs to be related to the device when checking in.
[email protected]5799d052014-02-12 20:47:39202 // |blocking_task_runner|: for running blocking file tasks.
203 // |url_request_context_getter|: for url requests.
204 // |delegate|: the delegate whose methods will be called asynchronously in
205 // response to events and messages.
[email protected]e2a4a8012014-02-07 22:32:52206 virtual void Initialize(
[email protected]8ad80512014-05-23 09:40:47207 const ChromeBuildInfo& chrome_build_info,
[email protected]e2a4a8012014-02-07 22:32:52208 const base::FilePath& store_path,
[email protected]495a7db92014-02-22 07:49:59209 const std::vector<std::string>& account_ids,
[email protected]e2a4a8012014-02-07 22:32:52210 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
211 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:39212 url_request_context_getter,
[email protected]446f73c22014-05-14 20:47:18213 scoped_ptr<Encryptor> encryptor,
[email protected]5799d052014-02-12 20:47:39214 Delegate* delegate) = 0;
[email protected]e2a4a8012014-02-07 22:32:52215
[email protected]fc767e42014-05-13 05:02:14216 // Starts the GCM service by first loading the data from the persistent store.
217 // This will then kick off the check-in if the check-in info is not found in
218 // the store.
219 virtual void Start() = 0;
[email protected]d3a4b2e2014-02-27 13:46:54220
[email protected]21fee5482014-03-05 00:57:15221 // Stops using the GCM service. This will not erase the persisted data.
222 virtual void Stop() = 0;
223
[email protected]5799d052014-02-12 20:47:39224 // Checks out of the GCM service. This will erase all the cached and persisted
225 // data.
226 virtual void CheckOut() = 0;
[email protected]e4097c82013-11-08 00:16:12227
228 // Registers the application for GCM. Delegate::OnRegisterFinished will be
229 // called asynchronously upon completion.
[email protected]e4097c82013-11-08 00:16:12230 // |app_id|: application ID.
[email protected]e4097c82013-11-08 00:16:12231 // |sender_ids|: list of IDs of the servers that are allowed to send the
232 // messages to the application. These IDs are assigned by the
233 // Google API Console.
[email protected]5799d052014-02-12 20:47:39234 virtual void Register(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12235 const std::vector<std::string>& sender_ids) = 0;
236
237 // Unregisters the application from GCM when it is uninstalled.
238 // Delegate::OnUnregisterFinished will be called asynchronously upon
239 // completion.
[email protected]e4097c82013-11-08 00:16:12240 // |app_id|: application ID.
[email protected]5799d052014-02-12 20:47:39241 virtual void Unregister(const std::string& app_id) = 0;
[email protected]e4097c82013-11-08 00:16:12242
243 // Sends a message to a given receiver. Delegate::OnSendFinished will be
244 // called asynchronously upon completion.
[email protected]e4097c82013-11-08 00:16:12245 // |app_id|: application ID.
246 // |receiver_id|: registration ID of the receiver party.
247 // |message|: message to be sent.
[email protected]5799d052014-02-12 20:47:39248 virtual void Send(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12249 const std::string& receiver_id,
250 const OutgoingMessage& message) = 0;
[email protected]35601812014-03-07 19:52:43251
[email protected]436bcb82014-04-18 00:40:57252 // Enables or disables internal activity recording.
253 virtual void SetRecording(bool recording) = 0;
254
255 // Clear all recorded GCM activity logs.
256 virtual void ClearActivityLogs() = 0;
257
[email protected]35601812014-03-07 19:52:43258 // Gets internal states and statistics.
259 virtual GCMStatistics GetStatistics() const = 0;
[email protected]e4097c82013-11-08 00:16:12260};
261
262} // namespace gcm
263
264#endif // GOOGLE_APIS_GCM_GCM_CLIENT_H_