blob: dc659673f567aa51fdec5206ddb0f79fe81fa7cc [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
23namespace checkin_proto {
24class ChromeBuildProto;
25}
26
27namespace net {
28class URLRequestContextGetter;
29}
30
[email protected]e4097c82013-11-08 00:16:1231namespace gcm {
32
[email protected]446f73c22014-05-14 20:47:1833class Encryptor;
34
[email protected]e4097c82013-11-08 00:16:1235// Interface that encapsulates the network communications with the Google Cloud
36// Messaging server. This interface is not supposed to be thread-safe.
37class GCM_EXPORT GCMClient {
38 public:
39 enum Result {
40 // Successful operation.
41 SUCCESS,
[email protected]b16a7c52013-11-20 01:18:5942 // Invalid parameter.
43 INVALID_PARAMETER,
[email protected]c7f7b532014-01-24 07:24:4544 // Profile not signed in.
45 NOT_SIGNED_IN,
[email protected]b16a7c52013-11-20 01:18:5946 // Previous asynchronous operation is still pending to finish. Certain
47 // operation, like register, is only allowed one at a time.
48 ASYNC_OPERATION_PENDING,
[email protected]e4097c82013-11-08 00:16:1249 // Network socket error.
50 NETWORK_ERROR,
51 // Problem at the server.
52 SERVER_ERROR,
53 // Exceeded the specified TTL during message sending.
54 TTL_EXCEEDED,
55 // Other errors.
56 UNKNOWN_ERROR
57 };
58
59 // Message data consisting of key-value pairs.
[email protected]b16a7c52013-11-20 01:18:5960 typedef std::map<std::string, std::string> MessageData;
[email protected]e4097c82013-11-08 00:16:1261
62 // Message to be delivered to the other party.
[email protected]b16a7c52013-11-20 01:18:5963 struct GCM_EXPORT OutgoingMessage {
[email protected]e4097c82013-11-08 00:16:1264 OutgoingMessage();
65 ~OutgoingMessage();
66
67 // Message ID.
68 std::string id;
69 // In seconds.
70 int time_to_live;
71 MessageData data;
[email protected]b20aece22014-05-09 22:34:0872
73 static const int kMaximumTTL = 4 * 7 * 24 * 60 * 60; // 4 weeks.
[email protected]e4097c82013-11-08 00:16:1274 };
75
76 // Message being received from the other party.
[email protected]b16a7c52013-11-20 01:18:5977 struct GCM_EXPORT IncomingMessage {
[email protected]e4097c82013-11-08 00:16:1278 IncomingMessage();
79 ~IncomingMessage();
80
81 MessageData data;
[email protected]40113aa2014-02-28 21:37:5682 std::string collapse_key;
[email protected]8d3af382014-03-07 00:58:4183 std::string sender_id;
[email protected]e4097c82013-11-08 00:16:1284 };
85
[email protected]c6fe36b2014-03-11 10:58:1286 // Detailed information of the Send Error event.
87 struct GCM_EXPORT SendErrorDetails {
88 SendErrorDetails();
89 ~SendErrorDetails();
90
91 std::string message_id;
92 MessageData additional_data;
93 Result result;
94 };
95
[email protected]35601812014-03-07 19:52:4396 // Internal states and activity statistics of a GCM client.
97 struct GCM_EXPORT GCMStatistics {
98 public:
99 GCMStatistics();
100 ~GCMStatistics();
101
[email protected]436bcb82014-04-18 00:40:57102 bool is_recording;
[email protected]35601812014-03-07 19:52:43103 bool gcm_client_created;
104 std::string gcm_client_state;
105 bool connection_client_created;
106 std::string connection_state;
107 uint64 android_id;
[email protected]436bcb82014-04-18 00:40:57108 std::vector<std::string> registered_app_ids;
109 int send_queue_size;
110 int resend_queue_size;
111
[email protected]9df5b932014-04-30 00:39:06112 GCMStatsRecorder::RecordedActivities recorded_activities;
[email protected]35601812014-03-07 19:52:43113 };
114
[email protected]e4097c82013-11-08 00:16:12115 // A delegate interface that allows the GCMClient instance to interact with
116 // its caller, i.e. notifying asynchronous event.
117 class Delegate {
118 public:
[email protected]e4097c82013-11-08 00:16:12119 // Called when the registration completed successfully or an error occurs.
120 // |app_id|: application ID.
121 // |registration_id|: non-empty if the registration completed successfully.
122 // |result|: the type of the error if an error occured, success otherwise.
123 virtual void OnRegisterFinished(const std::string& app_id,
124 const std::string& registration_id,
125 Result result) = 0;
126
[email protected]e4007042014-02-15 20:34:28127 // Called when the unregistration completed.
128 // |app_id|: application ID.
[email protected]0e88e1d12014-03-19 06:53:08129 // |result|: result of the unregistration.
[email protected]e4007042014-02-15 20:34:28130 virtual void OnUnregisterFinished(const std::string& app_id,
[email protected]0e88e1d12014-03-19 06:53:08131 GCMClient::Result result) = 0;
[email protected]e4007042014-02-15 20:34:28132
[email protected]e4097c82013-11-08 00:16:12133 // Called when the message is scheduled to send successfully or an error
134 // occurs.
135 // |app_id|: application ID.
136 // |message_id|: ID of the message being sent.
137 // |result|: the type of the error if an error occured, success otherwise.
138 virtual void OnSendFinished(const std::string& app_id,
139 const std::string& message_id,
140 Result result) = 0;
141
142 // Called when a message has been received.
143 // |app_id|: application ID.
[email protected]e4097c82013-11-08 00:16:12144 // |message|: message received.
145 virtual void OnMessageReceived(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12146 const IncomingMessage& message) = 0;
147
148 // Called when some messages have been deleted from the server.
149 // |app_id|: application ID.
150 virtual void OnMessagesDeleted(const std::string& app_id) = 0;
151
152 // Called when a message failed to send to the server.
153 // |app_id|: application ID.
[email protected]c6fe36b2014-03-11 10:58:12154 // |send_error_detials|: Details of the send error event, like mesasge ID.
155 virtual void OnMessageSendError(
156 const std::string& app_id,
157 const SendErrorDetails& send_error_details) = 0;
[email protected]e4097c82013-11-08 00:16:12158
[email protected]86625df2014-01-31 03:47:58159 // Called when the GCM becomes ready. To get to this state, GCMClient
160 // finished loading from the GCM store and retrieved the device check-in
161 // from the server if it hadn't yet.
162 virtual void OnGCMReady() = 0;
[email protected]7de78802014-05-10 19:49:40163
164 // Called when activities are being recorded and a new activity has just
165 // been recorded.
166 virtual void OnActivityRecorded() = 0;
[email protected]e4097c82013-11-08 00:16:12167 };
168
[email protected]0db118222014-01-22 01:37:59169 GCMClient();
170 virtual ~GCMClient();
[email protected]1b1c3cd2013-12-17 18:40:04171
[email protected]d3a4b2e2014-02-27 13:46:54172 // Begins initialization of the GCM Client. This will not trigger a
173 // connection.
[email protected]5799d052014-02-12 20:47:39174 // |chrome_build_proto|: chrome info, i.e., version, channel and etc.
175 // |store_path|: path to the GCM store.
[email protected]495a7db92014-02-22 07:49:59176 // |account_ids|: account IDs to be related to the device when checking in.
[email protected]5799d052014-02-12 20:47:39177 // |blocking_task_runner|: for running blocking file tasks.
178 // |url_request_context_getter|: for url requests.
179 // |delegate|: the delegate whose methods will be called asynchronously in
180 // response to events and messages.
[email protected]e2a4a8012014-02-07 22:32:52181 virtual void Initialize(
182 const checkin_proto::ChromeBuildProto& chrome_build_proto,
183 const base::FilePath& store_path,
[email protected]495a7db92014-02-22 07:49:59184 const std::vector<std::string>& account_ids,
[email protected]e2a4a8012014-02-07 22:32:52185 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
186 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:39187 url_request_context_getter,
[email protected]446f73c22014-05-14 20:47:18188 scoped_ptr<Encryptor> encryptor,
[email protected]5799d052014-02-12 20:47:39189 Delegate* delegate) = 0;
[email protected]e2a4a8012014-02-07 22:32:52190
[email protected]fc767e42014-05-13 05:02:14191 // Starts the GCM service by first loading the data from the persistent store.
192 // This will then kick off the check-in if the check-in info is not found in
193 // the store.
194 virtual void Start() = 0;
[email protected]d3a4b2e2014-02-27 13:46:54195
[email protected]21fee5482014-03-05 00:57:15196 // Stops using the GCM service. This will not erase the persisted data.
197 virtual void Stop() = 0;
198
[email protected]5799d052014-02-12 20:47:39199 // Checks out of the GCM service. This will erase all the cached and persisted
200 // data.
201 virtual void CheckOut() = 0;
[email protected]e4097c82013-11-08 00:16:12202
203 // Registers the application for GCM. Delegate::OnRegisterFinished will be
204 // called asynchronously upon completion.
[email protected]e4097c82013-11-08 00:16:12205 // |app_id|: application ID.
[email protected]e4097c82013-11-08 00:16:12206 // |sender_ids|: list of IDs of the servers that are allowed to send the
207 // messages to the application. These IDs are assigned by the
208 // Google API Console.
[email protected]5799d052014-02-12 20:47:39209 virtual void Register(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12210 const std::vector<std::string>& sender_ids) = 0;
211
212 // Unregisters the application from GCM when it is uninstalled.
213 // Delegate::OnUnregisterFinished will be called asynchronously upon
214 // completion.
[email protected]e4097c82013-11-08 00:16:12215 // |app_id|: application ID.
[email protected]5799d052014-02-12 20:47:39216 virtual void Unregister(const std::string& app_id) = 0;
[email protected]e4097c82013-11-08 00:16:12217
218 // Sends a message to a given receiver. Delegate::OnSendFinished will be
219 // called asynchronously upon completion.
[email protected]e4097c82013-11-08 00:16:12220 // |app_id|: application ID.
221 // |receiver_id|: registration ID of the receiver party.
222 // |message|: message to be sent.
[email protected]5799d052014-02-12 20:47:39223 virtual void Send(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12224 const std::string& receiver_id,
225 const OutgoingMessage& message) = 0;
[email protected]35601812014-03-07 19:52:43226
[email protected]436bcb82014-04-18 00:40:57227 // Enables or disables internal activity recording.
228 virtual void SetRecording(bool recording) = 0;
229
230 // Clear all recorded GCM activity logs.
231 virtual void ClearActivityLogs() = 0;
232
[email protected]35601812014-03-07 19:52:43233 // Gets internal states and statistics.
234 virtual GCMStatistics GetStatistics() const = 0;
[email protected]e4097c82013-11-08 00:16:12235};
236
237} // namespace gcm
238
239#endif // GOOGLE_APIS_GCM_GCM_CLIENT_H_