blob: 5996c064c3eba9eb3bbbfc73bbf3736d0549a893 [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;
70 };
71
72 // Message being received from the other party.
[email protected]b16a7c52013-11-20 01:18:5973 struct GCM_EXPORT IncomingMessage {
[email protected]e4097c82013-11-08 00:16:1274 IncomingMessage();
75 ~IncomingMessage();
76
77 MessageData data;
[email protected]40113aa2014-02-28 21:37:5678 std::string collapse_key;
[email protected]8d3af382014-03-07 00:58:4179 std::string sender_id;
[email protected]e4097c82013-11-08 00:16:1280 };
81
[email protected]c6fe36b2014-03-11 10:58:1282 // Detailed information of the Send Error event.
83 struct GCM_EXPORT SendErrorDetails {
84 SendErrorDetails();
85 ~SendErrorDetails();
86
87 std::string message_id;
88 MessageData additional_data;
89 Result result;
90 };
91
[email protected]35601812014-03-07 19:52:4392 // Internal states and activity statistics of a GCM client.
93 struct GCM_EXPORT GCMStatistics {
94 public:
95 GCMStatistics();
96 ~GCMStatistics();
97
[email protected]436bcb82014-04-18 00:40:5798 bool is_recording;
[email protected]35601812014-03-07 19:52:4399 bool gcm_client_created;
100 std::string gcm_client_state;
101 bool connection_client_created;
102 std::string connection_state;
103 uint64 android_id;
[email protected]436bcb82014-04-18 00:40:57104 std::vector<std::string> registered_app_ids;
105 int send_queue_size;
106 int resend_queue_size;
107
108 std::vector<GCMStatsRecorder::SendingActivity> sending_activities;
[email protected]35601812014-03-07 19:52:43109 };
110
[email protected]e4097c82013-11-08 00:16:12111 // A delegate interface that allows the GCMClient instance to interact with
112 // its caller, i.e. notifying asynchronous event.
113 class Delegate {
114 public:
[email protected]e4097c82013-11-08 00:16:12115 // Called when the registration completed successfully or an error occurs.
116 // |app_id|: application ID.
117 // |registration_id|: non-empty if the registration completed successfully.
118 // |result|: the type of the error if an error occured, success otherwise.
119 virtual void OnRegisterFinished(const std::string& app_id,
120 const std::string& registration_id,
121 Result result) = 0;
122
[email protected]e4007042014-02-15 20:34:28123 // Called when the unregistration completed.
124 // |app_id|: application ID.
[email protected]0e88e1d12014-03-19 06:53:08125 // |result|: result of the unregistration.
[email protected]e4007042014-02-15 20:34:28126 virtual void OnUnregisterFinished(const std::string& app_id,
[email protected]0e88e1d12014-03-19 06:53:08127 GCMClient::Result result) = 0;
[email protected]e4007042014-02-15 20:34:28128
[email protected]e4097c82013-11-08 00:16:12129 // Called when the message is scheduled to send successfully or an error
130 // occurs.
131 // |app_id|: application ID.
132 // |message_id|: ID of the message being sent.
133 // |result|: the type of the error if an error occured, success otherwise.
134 virtual void OnSendFinished(const std::string& app_id,
135 const std::string& message_id,
136 Result result) = 0;
137
138 // Called when a message has been received.
139 // |app_id|: application ID.
[email protected]e4097c82013-11-08 00:16:12140 // |message|: message received.
141 virtual void OnMessageReceived(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12142 const IncomingMessage& message) = 0;
143
144 // Called when some messages have been deleted from the server.
145 // |app_id|: application ID.
146 virtual void OnMessagesDeleted(const std::string& app_id) = 0;
147
148 // Called when a message failed to send to the server.
149 // |app_id|: application ID.
[email protected]c6fe36b2014-03-11 10:58:12150 // |send_error_detials|: Details of the send error event, like mesasge ID.
151 virtual void OnMessageSendError(
152 const std::string& app_id,
153 const SendErrorDetails& send_error_details) = 0;
[email protected]e4097c82013-11-08 00:16:12154
[email protected]86625df2014-01-31 03:47:58155 // Called when the GCM becomes ready. To get to this state, GCMClient
156 // finished loading from the GCM store and retrieved the device check-in
157 // from the server if it hadn't yet.
158 virtual void OnGCMReady() = 0;
[email protected]e4097c82013-11-08 00:16:12159 };
160
[email protected]0db118222014-01-22 01:37:59161 GCMClient();
162 virtual ~GCMClient();
[email protected]1b1c3cd2013-12-17 18:40:04163
[email protected]d3a4b2e2014-02-27 13:46:54164 // Begins initialization of the GCM Client. This will not trigger a
165 // connection.
[email protected]5799d052014-02-12 20:47:39166 // |chrome_build_proto|: chrome info, i.e., version, channel and etc.
167 // |store_path|: path to the GCM store.
[email protected]495a7db92014-02-22 07:49:59168 // |account_ids|: account IDs to be related to the device when checking in.
[email protected]5799d052014-02-12 20:47:39169 // |blocking_task_runner|: for running blocking file tasks.
170 // |url_request_context_getter|: for url requests.
171 // |delegate|: the delegate whose methods will be called asynchronously in
172 // response to events and messages.
[email protected]e2a4a8012014-02-07 22:32:52173 virtual void Initialize(
174 const checkin_proto::ChromeBuildProto& chrome_build_proto,
175 const base::FilePath& store_path,
[email protected]495a7db92014-02-22 07:49:59176 const std::vector<std::string>& account_ids,
[email protected]e2a4a8012014-02-07 22:32:52177 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
178 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:39179 url_request_context_getter,
180 Delegate* delegate) = 0;
[email protected]e2a4a8012014-02-07 22:32:52181
[email protected]d3a4b2e2014-02-27 13:46:54182 // Loads the data from the persistent store. This will automatically kick off
183 // the check-in if the check-in info is not found in the store.
[email protected]21fee5482014-03-05 00:57:15184 // TODO(jianli): consider renaming this name to Start.
[email protected]d3a4b2e2014-02-27 13:46:54185 virtual void Load() = 0;
186
[email protected]21fee5482014-03-05 00:57:15187 // Stops using the GCM service. This will not erase the persisted data.
188 virtual void Stop() = 0;
189
[email protected]5799d052014-02-12 20:47:39190 // Checks out of the GCM service. This will erase all the cached and persisted
191 // data.
192 virtual void CheckOut() = 0;
[email protected]e4097c82013-11-08 00:16:12193
194 // Registers the application for GCM. Delegate::OnRegisterFinished will be
195 // called asynchronously upon completion.
[email protected]e4097c82013-11-08 00:16:12196 // |app_id|: application ID.
[email protected]e4097c82013-11-08 00:16:12197 // |sender_ids|: list of IDs of the servers that are allowed to send the
198 // messages to the application. These IDs are assigned by the
199 // Google API Console.
[email protected]5799d052014-02-12 20:47:39200 virtual void Register(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12201 const std::vector<std::string>& sender_ids) = 0;
202
203 // Unregisters the application from GCM when it is uninstalled.
204 // Delegate::OnUnregisterFinished will be called asynchronously upon
205 // completion.
[email protected]e4097c82013-11-08 00:16:12206 // |app_id|: application ID.
[email protected]5799d052014-02-12 20:47:39207 virtual void Unregister(const std::string& app_id) = 0;
[email protected]e4097c82013-11-08 00:16:12208
209 // Sends a message to a given receiver. Delegate::OnSendFinished will be
210 // called asynchronously upon completion.
[email protected]e4097c82013-11-08 00:16:12211 // |app_id|: application ID.
212 // |receiver_id|: registration ID of the receiver party.
213 // |message|: message to be sent.
[email protected]5799d052014-02-12 20:47:39214 virtual void Send(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12215 const std::string& receiver_id,
216 const OutgoingMessage& message) = 0;
[email protected]35601812014-03-07 19:52:43217
[email protected]436bcb82014-04-18 00:40:57218 // Enables or disables internal activity recording.
219 virtual void SetRecording(bool recording) = 0;
220
221 // Clear all recorded GCM activity logs.
222 virtual void ClearActivityLogs() = 0;
223
[email protected]35601812014-03-07 19:52:43224 // Gets internal states and statistics.
225 virtual GCMStatistics GetStatistics() const = 0;
[email protected]e4097c82013-11-08 00:16:12226};
227
228} // namespace gcm
229
230#endif // GOOGLE_APIS_GCM_GCM_CLIENT_H_