blob: 1a24f0932e3cf3d24a42dc89ea77c23da098b42a [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
33// Interface that encapsulates the network communications with the Google Cloud
34// Messaging server. This interface is not supposed to be thread-safe.
35class GCM_EXPORT GCMClient {
36 public:
37 enum Result {
38 // Successful operation.
39 SUCCESS,
[email protected]b16a7c52013-11-20 01:18:5940 // Invalid parameter.
41 INVALID_PARAMETER,
[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
57 // Message data consisting of key-value pairs.
[email protected]b16a7c52013-11-20 01:18:5958 typedef std::map<std::string, std::string> MessageData;
[email protected]e4097c82013-11-08 00:16:1259
60 // Message to be delivered to the other party.
[email protected]b16a7c52013-11-20 01:18:5961 struct GCM_EXPORT OutgoingMessage {
[email protected]e4097c82013-11-08 00:16:1262 OutgoingMessage();
63 ~OutgoingMessage();
64
65 // Message ID.
66 std::string id;
67 // In seconds.
68 int time_to_live;
69 MessageData data;
[email protected]b20aece22014-05-09 22:34:0870
71 static const int kMaximumTTL = 4 * 7 * 24 * 60 * 60; // 4 weeks.
[email protected]e4097c82013-11-08 00:16:1272 };
73
74 // Message being received from the other party.
[email protected]b16a7c52013-11-20 01:18:5975 struct GCM_EXPORT IncomingMessage {
[email protected]e4097c82013-11-08 00:16:1276 IncomingMessage();
77 ~IncomingMessage();
78
79 MessageData data;
[email protected]40113aa2014-02-28 21:37:5680 std::string collapse_key;
[email protected]8d3af382014-03-07 00:58:4181 std::string sender_id;
[email protected]e4097c82013-11-08 00:16:1282 };
83
[email protected]c6fe36b2014-03-11 10:58:1284 // Detailed information of the Send Error event.
85 struct GCM_EXPORT SendErrorDetails {
86 SendErrorDetails();
87 ~SendErrorDetails();
88
89 std::string message_id;
90 MessageData additional_data;
91 Result result;
92 };
93
[email protected]35601812014-03-07 19:52:4394 // Internal states and activity statistics of a GCM client.
95 struct GCM_EXPORT GCMStatistics {
96 public:
97 GCMStatistics();
98 ~GCMStatistics();
99
[email protected]436bcb82014-04-18 00:40:57100 bool is_recording;
[email protected]35601812014-03-07 19:52:43101 bool gcm_client_created;
102 std::string gcm_client_state;
103 bool connection_client_created;
104 std::string connection_state;
105 uint64 android_id;
[email protected]436bcb82014-04-18 00:40:57106 std::vector<std::string> registered_app_ids;
107 int send_queue_size;
108 int resend_queue_size;
109
[email protected]9df5b932014-04-30 00:39:06110 GCMStatsRecorder::RecordedActivities recorded_activities;
[email protected]35601812014-03-07 19:52:43111 };
112
[email protected]e4097c82013-11-08 00:16:12113 // A delegate interface that allows the GCMClient instance to interact with
114 // its caller, i.e. notifying asynchronous event.
115 class Delegate {
116 public:
[email protected]e4097c82013-11-08 00:16:12117 // Called when the registration completed successfully or an error occurs.
118 // |app_id|: application ID.
119 // |registration_id|: non-empty if the registration completed successfully.
120 // |result|: the type of the error if an error occured, success otherwise.
121 virtual void OnRegisterFinished(const std::string& app_id,
122 const std::string& registration_id,
123 Result result) = 0;
124
[email protected]e4007042014-02-15 20:34:28125 // Called when the unregistration completed.
126 // |app_id|: application ID.
[email protected]0e88e1d12014-03-19 06:53:08127 // |result|: result of the unregistration.
[email protected]e4007042014-02-15 20:34:28128 virtual void OnUnregisterFinished(const std::string& app_id,
[email protected]0e88e1d12014-03-19 06:53:08129 GCMClient::Result result) = 0;
[email protected]e4007042014-02-15 20:34:28130
[email protected]e4097c82013-11-08 00:16:12131 // Called when the message is scheduled to send successfully or an error
132 // occurs.
133 // |app_id|: application ID.
134 // |message_id|: ID of the message being sent.
135 // |result|: the type of the error if an error occured, success otherwise.
136 virtual void OnSendFinished(const std::string& app_id,
137 const std::string& message_id,
138 Result result) = 0;
139
140 // Called when a message has been received.
141 // |app_id|: application ID.
[email protected]e4097c82013-11-08 00:16:12142 // |message|: message received.
143 virtual void OnMessageReceived(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12144 const IncomingMessage& message) = 0;
145
146 // Called when some messages have been deleted from the server.
147 // |app_id|: application ID.
148 virtual void OnMessagesDeleted(const std::string& app_id) = 0;
149
150 // Called when a message failed to send to the server.
151 // |app_id|: application ID.
[email protected]c6fe36b2014-03-11 10:58:12152 // |send_error_detials|: Details of the send error event, like mesasge ID.
153 virtual void OnMessageSendError(
154 const std::string& app_id,
155 const SendErrorDetails& send_error_details) = 0;
[email protected]e4097c82013-11-08 00:16:12156
[email protected]86625df2014-01-31 03:47:58157 // Called when the GCM becomes ready. To get to this state, GCMClient
158 // finished loading from the GCM store and retrieved the device check-in
159 // from the server if it hadn't yet.
160 virtual void OnGCMReady() = 0;
[email protected]e4097c82013-11-08 00:16:12161 };
162
[email protected]0db118222014-01-22 01:37:59163 GCMClient();
164 virtual ~GCMClient();
[email protected]1b1c3cd2013-12-17 18:40:04165
[email protected]d3a4b2e2014-02-27 13:46:54166 // Begins initialization of the GCM Client. This will not trigger a
167 // connection.
[email protected]5799d052014-02-12 20:47:39168 // |chrome_build_proto|: chrome info, i.e., version, channel and etc.
169 // |store_path|: path to the GCM store.
[email protected]495a7db92014-02-22 07:49:59170 // |account_ids|: account IDs to be related to the device when checking in.
[email protected]5799d052014-02-12 20:47:39171 // |blocking_task_runner|: for running blocking file tasks.
172 // |url_request_context_getter|: for url requests.
173 // |delegate|: the delegate whose methods will be called asynchronously in
174 // response to events and messages.
[email protected]e2a4a8012014-02-07 22:32:52175 virtual void Initialize(
176 const checkin_proto::ChromeBuildProto& chrome_build_proto,
177 const base::FilePath& store_path,
[email protected]495a7db92014-02-22 07:49:59178 const std::vector<std::string>& account_ids,
[email protected]e2a4a8012014-02-07 22:32:52179 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
180 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:39181 url_request_context_getter,
182 Delegate* delegate) = 0;
[email protected]e2a4a8012014-02-07 22:32:52183
[email protected]d3a4b2e2014-02-27 13:46:54184 // Loads the data from the persistent store. This will automatically kick off
185 // the check-in if the check-in info is not found in the store.
[email protected]21fee5482014-03-05 00:57:15186 // TODO(jianli): consider renaming this name to Start.
[email protected]d3a4b2e2014-02-27 13:46:54187 virtual void Load() = 0;
188
[email protected]21fee5482014-03-05 00:57:15189 // Stops using the GCM service. This will not erase the persisted data.
190 virtual void Stop() = 0;
191
[email protected]5799d052014-02-12 20:47:39192 // Checks out of the GCM service. This will erase all the cached and persisted
193 // data.
194 virtual void CheckOut() = 0;
[email protected]e4097c82013-11-08 00:16:12195
196 // Registers the application for GCM. Delegate::OnRegisterFinished will be
197 // called asynchronously upon completion.
[email protected]e4097c82013-11-08 00:16:12198 // |app_id|: application ID.
[email protected]e4097c82013-11-08 00:16:12199 // |sender_ids|: list of IDs of the servers that are allowed to send the
200 // messages to the application. These IDs are assigned by the
201 // Google API Console.
[email protected]5799d052014-02-12 20:47:39202 virtual void Register(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12203 const std::vector<std::string>& sender_ids) = 0;
204
205 // Unregisters the application from GCM when it is uninstalled.
206 // Delegate::OnUnregisterFinished will be called asynchronously upon
207 // completion.
[email protected]e4097c82013-11-08 00:16:12208 // |app_id|: application ID.
[email protected]5799d052014-02-12 20:47:39209 virtual void Unregister(const std::string& app_id) = 0;
[email protected]e4097c82013-11-08 00:16:12210
211 // Sends a message to a given receiver. Delegate::OnSendFinished will be
212 // called asynchronously upon completion.
[email protected]e4097c82013-11-08 00:16:12213 // |app_id|: application ID.
214 // |receiver_id|: registration ID of the receiver party.
215 // |message|: message to be sent.
[email protected]5799d052014-02-12 20:47:39216 virtual void Send(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12217 const std::string& receiver_id,
218 const OutgoingMessage& message) = 0;
[email protected]35601812014-03-07 19:52:43219
[email protected]436bcb82014-04-18 00:40:57220 // Enables or disables internal activity recording.
221 virtual void SetRecording(bool recording) = 0;
222
223 // Clear all recorded GCM activity logs.
224 virtual void ClearActivityLogs() = 0;
225
[email protected]35601812014-03-07 19:52:43226 // Gets internal states and statistics.
227 virtual GCMStatistics GetStatistics() const = 0;
[email protected]e4097c82013-11-08 00:16:12228};
229
230} // namespace gcm
231
232#endif // GOOGLE_APIS_GCM_GCM_CLIENT_H_