blob: 178b18f11d58091dda455f49e6282bfc37f1c0e0 [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]9d7e5c02014-05-21 03:09:0344 // GCM is disabled.
45 GCM_DISABLED,
[email protected]c7f7b532014-01-24 07:24:4546 // Profile not signed in.
47 NOT_SIGNED_IN,
[email protected]b16a7c52013-11-20 01:18:5948 // Previous asynchronous operation is still pending to finish. Certain
49 // operation, like register, is only allowed one at a time.
50 ASYNC_OPERATION_PENDING,
[email protected]e4097c82013-11-08 00:16:1251 // Network socket error.
52 NETWORK_ERROR,
53 // Problem at the server.
54 SERVER_ERROR,
55 // Exceeded the specified TTL during message sending.
56 TTL_EXCEEDED,
57 // Other errors.
58 UNKNOWN_ERROR
59 };
60
61 // Message data consisting of key-value pairs.
[email protected]b16a7c52013-11-20 01:18:5962 typedef std::map<std::string, std::string> MessageData;
[email protected]e4097c82013-11-08 00:16:1263
64 // Message to be delivered to the other party.
[email protected]b16a7c52013-11-20 01:18:5965 struct GCM_EXPORT OutgoingMessage {
[email protected]e4097c82013-11-08 00:16:1266 OutgoingMessage();
67 ~OutgoingMessage();
68
69 // Message ID.
70 std::string id;
71 // In seconds.
72 int time_to_live;
73 MessageData data;
[email protected]b20aece22014-05-09 22:34:0874
75 static const int kMaximumTTL = 4 * 7 * 24 * 60 * 60; // 4 weeks.
[email protected]e4097c82013-11-08 00:16:1276 };
77
78 // Message being received from the other party.
[email protected]b16a7c52013-11-20 01:18:5979 struct GCM_EXPORT IncomingMessage {
[email protected]e4097c82013-11-08 00:16:1280 IncomingMessage();
81 ~IncomingMessage();
82
83 MessageData data;
[email protected]40113aa2014-02-28 21:37:5684 std::string collapse_key;
[email protected]8d3af382014-03-07 00:58:4185 std::string sender_id;
[email protected]e4097c82013-11-08 00:16:1286 };
87
[email protected]c6fe36b2014-03-11 10:58:1288 // Detailed information of the Send Error event.
89 struct GCM_EXPORT SendErrorDetails {
90 SendErrorDetails();
91 ~SendErrorDetails();
92
93 std::string message_id;
94 MessageData additional_data;
95 Result result;
96 };
97
[email protected]35601812014-03-07 19:52:4398 // Internal states and activity statistics of a GCM client.
99 struct GCM_EXPORT GCMStatistics {
100 public:
101 GCMStatistics();
102 ~GCMStatistics();
103
[email protected]436bcb82014-04-18 00:40:57104 bool is_recording;
[email protected]35601812014-03-07 19:52:43105 bool gcm_client_created;
106 std::string gcm_client_state;
107 bool connection_client_created;
108 std::string connection_state;
109 uint64 android_id;
[email protected]436bcb82014-04-18 00:40:57110 std::vector<std::string> registered_app_ids;
111 int send_queue_size;
112 int resend_queue_size;
113
[email protected]9df5b932014-04-30 00:39:06114 GCMStatsRecorder::RecordedActivities recorded_activities;
[email protected]35601812014-03-07 19:52:43115 };
116
[email protected]e4097c82013-11-08 00:16:12117 // A delegate interface that allows the GCMClient instance to interact with
118 // its caller, i.e. notifying asynchronous event.
119 class Delegate {
120 public:
[email protected]e4097c82013-11-08 00:16:12121 // Called when the registration completed successfully or an error occurs.
122 // |app_id|: application ID.
123 // |registration_id|: non-empty if the registration completed successfully.
124 // |result|: the type of the error if an error occured, success otherwise.
125 virtual void OnRegisterFinished(const std::string& app_id,
126 const std::string& registration_id,
127 Result result) = 0;
128
[email protected]e4007042014-02-15 20:34:28129 // Called when the unregistration completed.
130 // |app_id|: application ID.
[email protected]0e88e1d12014-03-19 06:53:08131 // |result|: result of the unregistration.
[email protected]e4007042014-02-15 20:34:28132 virtual void OnUnregisterFinished(const std::string& app_id,
[email protected]0e88e1d12014-03-19 06:53:08133 GCMClient::Result result) = 0;
[email protected]e4007042014-02-15 20:34:28134
[email protected]e4097c82013-11-08 00:16:12135 // Called when the message is scheduled to send successfully or an error
136 // occurs.
137 // |app_id|: application ID.
138 // |message_id|: ID of the message being sent.
139 // |result|: the type of the error if an error occured, success otherwise.
140 virtual void OnSendFinished(const std::string& app_id,
141 const std::string& message_id,
142 Result result) = 0;
143
144 // Called when a message has been received.
145 // |app_id|: application ID.
[email protected]e4097c82013-11-08 00:16:12146 // |message|: message received.
147 virtual void OnMessageReceived(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12148 const IncomingMessage& message) = 0;
149
150 // Called when some messages have been deleted from the server.
151 // |app_id|: application ID.
152 virtual void OnMessagesDeleted(const std::string& app_id) = 0;
153
154 // Called when a message failed to send to the server.
155 // |app_id|: application ID.
[email protected]c6fe36b2014-03-11 10:58:12156 // |send_error_detials|: Details of the send error event, like mesasge ID.
157 virtual void OnMessageSendError(
158 const std::string& app_id,
159 const SendErrorDetails& send_error_details) = 0;
[email protected]e4097c82013-11-08 00:16:12160
[email protected]86625df2014-01-31 03:47:58161 // Called when the GCM becomes ready. To get to this state, GCMClient
162 // finished loading from the GCM store and retrieved the device check-in
163 // from the server if it hadn't yet.
164 virtual void OnGCMReady() = 0;
[email protected]7de78802014-05-10 19:49:40165
166 // Called when activities are being recorded and a new activity has just
167 // been recorded.
168 virtual void OnActivityRecorded() = 0;
[email protected]e4097c82013-11-08 00:16:12169 };
170
[email protected]0db118222014-01-22 01:37:59171 GCMClient();
172 virtual ~GCMClient();
[email protected]1b1c3cd2013-12-17 18:40:04173
[email protected]d3a4b2e2014-02-27 13:46:54174 // Begins initialization of the GCM Client. This will not trigger a
175 // connection.
[email protected]5799d052014-02-12 20:47:39176 // |chrome_build_proto|: chrome info, i.e., version, channel and etc.
177 // |store_path|: path to the GCM store.
[email protected]495a7db92014-02-22 07:49:59178 // |account_ids|: account IDs to be related to the device when checking in.
[email protected]5799d052014-02-12 20:47:39179 // |blocking_task_runner|: for running blocking file tasks.
180 // |url_request_context_getter|: for url requests.
181 // |delegate|: the delegate whose methods will be called asynchronously in
182 // response to events and messages.
[email protected]e2a4a8012014-02-07 22:32:52183 virtual void Initialize(
184 const checkin_proto::ChromeBuildProto& chrome_build_proto,
185 const base::FilePath& store_path,
[email protected]495a7db92014-02-22 07:49:59186 const std::vector<std::string>& account_ids,
[email protected]e2a4a8012014-02-07 22:32:52187 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
188 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:39189 url_request_context_getter,
[email protected]446f73c22014-05-14 20:47:18190 scoped_ptr<Encryptor> encryptor,
[email protected]5799d052014-02-12 20:47:39191 Delegate* delegate) = 0;
[email protected]e2a4a8012014-02-07 22:32:52192
[email protected]fc767e42014-05-13 05:02:14193 // Starts the GCM service by first loading the data from the persistent store.
194 // This will then kick off the check-in if the check-in info is not found in
195 // the store.
196 virtual void Start() = 0;
[email protected]d3a4b2e2014-02-27 13:46:54197
[email protected]21fee5482014-03-05 00:57:15198 // Stops using the GCM service. This will not erase the persisted data.
199 virtual void Stop() = 0;
200
[email protected]5799d052014-02-12 20:47:39201 // Checks out of the GCM service. This will erase all the cached and persisted
202 // data.
203 virtual void CheckOut() = 0;
[email protected]e4097c82013-11-08 00:16:12204
205 // Registers the application for GCM. Delegate::OnRegisterFinished will be
206 // called asynchronously upon completion.
[email protected]e4097c82013-11-08 00:16:12207 // |app_id|: application ID.
[email protected]e4097c82013-11-08 00:16:12208 // |sender_ids|: list of IDs of the servers that are allowed to send the
209 // messages to the application. These IDs are assigned by the
210 // Google API Console.
[email protected]5799d052014-02-12 20:47:39211 virtual void Register(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12212 const std::vector<std::string>& sender_ids) = 0;
213
214 // Unregisters the application from GCM when it is uninstalled.
215 // Delegate::OnUnregisterFinished will be called asynchronously upon
216 // completion.
[email protected]e4097c82013-11-08 00:16:12217 // |app_id|: application ID.
[email protected]5799d052014-02-12 20:47:39218 virtual void Unregister(const std::string& app_id) = 0;
[email protected]e4097c82013-11-08 00:16:12219
220 // Sends a message to a given receiver. Delegate::OnSendFinished will be
221 // called asynchronously upon completion.
[email protected]e4097c82013-11-08 00:16:12222 // |app_id|: application ID.
223 // |receiver_id|: registration ID of the receiver party.
224 // |message|: message to be sent.
[email protected]5799d052014-02-12 20:47:39225 virtual void Send(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12226 const std::string& receiver_id,
227 const OutgoingMessage& message) = 0;
[email protected]35601812014-03-07 19:52:43228
[email protected]436bcb82014-04-18 00:40:57229 // Enables or disables internal activity recording.
230 virtual void SetRecording(bool recording) = 0;
231
232 // Clear all recorded GCM activity logs.
233 virtual void ClearActivityLogs() = 0;
234
[email protected]35601812014-03-07 19:52:43235 // Gets internal states and statistics.
236 virtual GCMStatistics GetStatistics() const = 0;
[email protected]e4097c82013-11-08 00:16:12237};
238
239} // namespace gcm
240
241#endif // GOOGLE_APIS_GCM_GCM_CLIENT_H_