1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_
#define COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_
#include <map>
#include <string>
#include <vector>
#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "components/gcm_driver/gcm_activity.h"
template <class T> class scoped_refptr;
class GURL;
namespace base {
class FilePath;
class SequencedTaskRunner;
class Timer;
}
namespace net {
class IPEndPoint;
class URLRequestContextGetter;
}
namespace gcm {
class Encryptor;
struct AccountMapping;
// Interface that encapsulates the network communications with the Google Cloud
// Messaging server. This interface is not supposed to be thread-safe.
class GCMClient {
public:
// Controls how GCM is being started. At first, GCMClient will be initialized
// and GCM store will be loaded. Then GCM connection may or may not be
// initiated depending on this enum value.
enum StartMode {
// GCM should be started only when it is being actually used. If no
// registration record is found, GCM will not kick off.
DELAYED_START,
// GCM should be started immediately.
IMMEDIATE_START
};
enum Result {
// Successful operation.
SUCCESS,
// Invalid parameter.
INVALID_PARAMETER,
// GCM is disabled.
GCM_DISABLED,
// Previous asynchronous operation is still pending to finish. Certain
// operation, like register, is only allowed one at a time.
ASYNC_OPERATION_PENDING,
// Network socket error.
NETWORK_ERROR,
// Problem at the server.
SERVER_ERROR,
// Exceeded the specified TTL during message sending.
TTL_EXCEEDED,
// Other errors.
UNKNOWN_ERROR
};
enum ChromePlatform {
PLATFORM_WIN,
PLATFORM_MAC,
PLATFORM_LINUX,
PLATFORM_CROS,
PLATFORM_IOS,
PLATFORM_ANDROID,
PLATFORM_UNKNOWN
};
enum ChromeChannel {
CHANNEL_STABLE,
CHANNEL_BETA,
CHANNEL_DEV,
CHANNEL_CANARY,
CHANNEL_UNKNOWN
};
struct ChromeBuildInfo {
ChromeBuildInfo();
~ChromeBuildInfo();
ChromePlatform platform;
ChromeChannel channel;
std::string version;
};
// Message data consisting of key-value pairs.
typedef std::map<std::string, std::string> MessageData;
// Message to be delivered to the other party.
struct OutgoingMessage {
OutgoingMessage();
~OutgoingMessage();
// Message ID.
std::string id;
// In seconds.
int time_to_live;
MessageData data;
static const int kMaximumTTL;
};
// Message being received from the other party.
struct IncomingMessage {
IncomingMessage();
~IncomingMessage();
MessageData data;
std::string collapse_key;
std::string sender_id;
};
// Detailed information of the Send Error event.
struct SendErrorDetails {
SendErrorDetails();
~SendErrorDetails();
std::string message_id;
MessageData additional_data;
Result result;
};
// Internal states and activity statistics of a GCM client.
struct GCMStatistics {
public:
GCMStatistics();
~GCMStatistics();
bool is_recording;
bool gcm_client_created;
std::string gcm_client_state;
bool connection_client_created;
std::string connection_state;
uint64 android_id;
std::vector<std::string> registered_app_ids;
int send_queue_size;
int resend_queue_size;
RecordedActivities recorded_activities;
};
// Information about account.
struct AccountTokenInfo {
std::string account_id;
std::string email;
std::string access_token;
};
// A delegate interface that allows the GCMClient instance to interact with
// its caller, i.e. notifying asynchronous event.
class Delegate {
public:
// Called when the registration completed successfully or an error occurs.
// |app_id|: application ID.
// |registration_id|: non-empty if the registration completed successfully.
// |result|: the type of the error if an error occured, success otherwise.
virtual void OnRegisterFinished(const std::string& app_id,
const std::string& registration_id,
Result result) = 0;
// Called when the unregistration completed.
// |app_id|: application ID.
// |result|: result of the unregistration.
virtual void OnUnregisterFinished(const std::string& app_id,
GCMClient::Result result) = 0;
// Called when the message is scheduled to send successfully or an error
// occurs.
// |app_id|: application ID.
// |message_id|: ID of the message being sent.
// |result|: the type of the error if an error occured, success otherwise.
virtual void OnSendFinished(const std::string& app_id,
const std::string& message_id,
Result result) = 0;
// Called when a message has been received.
// |app_id|: application ID.
// |message|: message received.
virtual void OnMessageReceived(const std::string& app_id,
const IncomingMessage& message) = 0;
// Called when some messages have been deleted from the server.
// |app_id|: application ID.
virtual void OnMessagesDeleted(const std::string& app_id) = 0;
// Called when a message failed to send to the server.
// |app_id|: application ID.
// |send_error_detials|: Details of the send error event, like mesasge ID.
virtual void OnMessageSendError(
const std::string& app_id,
const SendErrorDetails& send_error_details) = 0;
// Called when a message was acknowledged by the GCM server.
// |app_id|: application ID.
// |message_id|: ID of the acknowledged message.
virtual void OnSendAcknowledged(const std::string& app_id,
const std::string& message_id) = 0;
// Called when the GCM becomes ready. To get to this state, GCMClient
// finished loading from the GCM store and retrieved the device check-in
// from the server if it hadn't yet.
// |account_mappings|: a persisted list of accounts mapped to this GCM
// client.
// |last_token_fetch_time|: time of a last successful token fetch.
virtual void OnGCMReady(const std::vector<AccountMapping>& account_mappings,
const base::Time& last_token_fetch_time) = 0;
// Called when activities are being recorded and a new activity has just
// been recorded.
virtual void OnActivityRecorded() = 0;
// Called when a new connection is established and a successful handshake
// has been performed.
virtual void OnConnected(const net::IPEndPoint& ip_endpoint) = 0;
// Called when the connection is interrupted.
virtual void OnDisconnected() = 0;
};
GCMClient();
virtual ~GCMClient();
// Begins initialization of the GCM Client. This will not trigger a
// connection.
// |chrome_build_info|: chrome info, i.e., version, channel and etc.
// |store_path|: path to the GCM store.
// |blocking_task_runner|: for running blocking file tasks.
// |url_request_context_getter|: for url requests.
// |delegate|: the delegate whose methods will be called asynchronously in
// response to events and messages.
virtual void Initialize(
const ChromeBuildInfo& chrome_build_info,
const base::FilePath& store_path,
const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
const scoped_refptr<net::URLRequestContextGetter>&
url_request_context_getter,
scoped_ptr<Encryptor> encryptor,
Delegate* delegate) = 0;
// This will initiate the GCM connection only if |start_mode| means to start
// the GCM immediately or the GCM registration records are found in the store.
// Note that it is OK to call Start multiple times and the implementation
// should handle it gracefully.
virtual void Start(StartMode start_mode) = 0;
// Stops using the GCM service. This will not erase the persisted data.
virtual void Stop() = 0;
// Registers the application for GCM. Delegate::OnRegisterFinished will be
// called asynchronously upon completion.
// |app_id|: application ID.
// |sender_ids|: list of IDs of the servers that are allowed to send the
// messages to the application. These IDs are assigned by the
// Google API Console.
virtual void Register(const std::string& app_id,
const std::vector<std::string>& sender_ids) = 0;
// Unregisters the application from GCM when it is uninstalled.
// Delegate::OnUnregisterFinished will be called asynchronously upon
// completion.
// |app_id|: application ID.
virtual void Unregister(const std::string& app_id) = 0;
// Sends a message to a given receiver. Delegate::OnSendFinished will be
// called asynchronously upon completion.
// |app_id|: application ID.
// |receiver_id|: registration ID of the receiver party.
// |message|: message to be sent.
virtual void Send(const std::string& app_id,
const std::string& receiver_id,
const OutgoingMessage& message) = 0;
// Enables or disables internal activity recording.
virtual void SetRecording(bool recording) = 0;
// Clear all recorded GCM activity logs.
virtual void ClearActivityLogs() = 0;
// Gets internal states and statistics.
virtual GCMStatistics GetStatistics() const = 0;
// Sets a list of accounts with OAuth2 tokens for the next checkin.
// |account_tokens|: list of email addresses, account IDs and OAuth2 access
// tokens.
virtual void SetAccountTokens(
const std::vector<AccountTokenInfo>& account_tokens) = 0;
// Persists the |account_mapping| in the store.
virtual void UpdateAccountMapping(const AccountMapping& account_mapping) = 0;
// Removes the account mapping related to |account_id| from the persistent
// store.
virtual void RemoveAccountMapping(const std::string& account_id) = 0;
// Sets last token fetch time in persistent store.
virtual void SetLastTokenFetchTime(const base::Time& time) = 0;
// Updates the timer used by the HeartbeatManager for sending heartbeats.
virtual void UpdateHeartbeatTimer(scoped_ptr<base::Timer> timer) = 0;
};
} // namespace gcm
#endif // COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_
|