Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / components / gcm_driver / gcm_client.h
blob9956c1003e93e3125866b2f2c0eb5fc8a126af53
1 // Copyright 2014 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.
5 #ifndef COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_
6 #define COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "components/gcm_driver/gcm_activity.h"
16 template <class T> class scoped_refptr;
18 class GURL;
20 namespace base {
21 class FilePath;
22 class SequencedTaskRunner;
25 namespace net {
26 class IPEndPoint;
27 class URLRequestContextGetter;
30 namespace gcm {
32 class Encryptor;
33 struct AccountMapping;
35 // Interface that encapsulates the network communications with the Google Cloud
36 // Messaging server. This interface is not supposed to be thread-safe.
37 class GCMClient {
38 public:
39 enum Result {
40 // Successful operation.
41 SUCCESS,
42 // Invalid parameter.
43 INVALID_PARAMETER,
44 // GCM is disabled.
45 GCM_DISABLED,
46 // Profile not signed in.
47 NOT_SIGNED_IN,
48 // Previous asynchronous operation is still pending to finish. Certain
49 // operation, like register, is only allowed one at a time.
50 ASYNC_OPERATION_PENDING,
51 // 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
61 enum ChromePlatform {
62 PLATFORM_WIN,
63 PLATFORM_MAC,
64 PLATFORM_LINUX,
65 PLATFORM_CROS,
66 PLATFORM_IOS,
67 PLATFORM_ANDROID,
68 PLATFORM_UNKNOWN
71 enum ChromeChannel {
72 CHANNEL_STABLE,
73 CHANNEL_BETA,
74 CHANNEL_DEV,
75 CHANNEL_CANARY,
76 CHANNEL_UNKNOWN
79 struct ChromeBuildInfo {
80 ChromeBuildInfo();
81 ~ChromeBuildInfo();
83 ChromePlatform platform;
84 ChromeChannel channel;
85 std::string version;
88 // Message data consisting of key-value pairs.
89 typedef std::map<std::string, std::string> MessageData;
91 // Message to be delivered to the other party.
92 struct OutgoingMessage {
93 OutgoingMessage();
94 ~OutgoingMessage();
96 // Message ID.
97 std::string id;
98 // In seconds.
99 int time_to_live;
100 MessageData data;
102 static const int kMaximumTTL;
105 // Message being received from the other party.
106 struct IncomingMessage {
107 IncomingMessage();
108 ~IncomingMessage();
110 MessageData data;
111 std::string collapse_key;
112 std::string sender_id;
115 // Detailed information of the Send Error event.
116 struct SendErrorDetails {
117 SendErrorDetails();
118 ~SendErrorDetails();
120 std::string message_id;
121 MessageData additional_data;
122 Result result;
125 // Internal states and activity statistics of a GCM client.
126 struct GCMStatistics {
127 public:
128 GCMStatistics();
129 ~GCMStatistics();
131 bool is_recording;
132 bool gcm_client_created;
133 std::string gcm_client_state;
134 bool connection_client_created;
135 std::string connection_state;
136 uint64 android_id;
137 std::vector<std::string> registered_app_ids;
138 int send_queue_size;
139 int resend_queue_size;
141 RecordedActivities recorded_activities;
144 // Information about account.
145 struct AccountTokenInfo {
146 std::string account_id;
147 std::string email;
148 std::string access_token;
151 // A delegate interface that allows the GCMClient instance to interact with
152 // its caller, i.e. notifying asynchronous event.
153 class Delegate {
154 public:
155 // Called when the registration completed successfully or an error occurs.
156 // |app_id|: application ID.
157 // |registration_id|: non-empty if the registration completed successfully.
158 // |result|: the type of the error if an error occured, success otherwise.
159 virtual void OnRegisterFinished(const std::string& app_id,
160 const std::string& registration_id,
161 Result result) = 0;
163 // Called when the unregistration completed.
164 // |app_id|: application ID.
165 // |result|: result of the unregistration.
166 virtual void OnUnregisterFinished(const std::string& app_id,
167 GCMClient::Result result) = 0;
169 // Called when the message is scheduled to send successfully or an error
170 // occurs.
171 // |app_id|: application ID.
172 // |message_id|: ID of the message being sent.
173 // |result|: the type of the error if an error occured, success otherwise.
174 virtual void OnSendFinished(const std::string& app_id,
175 const std::string& message_id,
176 Result result) = 0;
178 // Called when a message has been received.
179 // |app_id|: application ID.
180 // |message|: message received.
181 virtual void OnMessageReceived(const std::string& app_id,
182 const IncomingMessage& message) = 0;
184 // Called when some messages have been deleted from the server.
185 // |app_id|: application ID.
186 virtual void OnMessagesDeleted(const std::string& app_id) = 0;
188 // Called when a message failed to send to the server.
189 // |app_id|: application ID.
190 // |send_error_detials|: Details of the send error event, like mesasge ID.
191 virtual void OnMessageSendError(
192 const std::string& app_id,
193 const SendErrorDetails& send_error_details) = 0;
195 // Called when a message was acknowledged by the GCM server.
196 // |app_id|: application ID.
197 // |message_id|: ID of the acknowledged message.
198 virtual void OnSendAcknowledged(const std::string& app_id,
199 const std::string& message_id) = 0;
201 // Called when the GCM becomes ready. To get to this state, GCMClient
202 // finished loading from the GCM store and retrieved the device check-in
203 // from the server if it hadn't yet.
204 // |account_mappings|: a persisted list of accounts mapped to this GCM
205 // client.
206 // |last_token_fetch_time|: time of a last successful token fetch.
207 virtual void OnGCMReady(const std::vector<AccountMapping>& account_mappings,
208 const base::Time& last_token_fetch_time) = 0;
210 // Called when activities are being recorded and a new activity has just
211 // been recorded.
212 virtual void OnActivityRecorded() = 0;
214 // Called when a new connection is established and a successful handshake
215 // has been performed.
216 virtual void OnConnected(const net::IPEndPoint& ip_endpoint) = 0;
218 // Called when the connection is interrupted.
219 virtual void OnDisconnected() = 0;
222 GCMClient();
223 virtual ~GCMClient();
225 // Begins initialization of the GCM Client. This will not trigger a
226 // connection.
227 // |chrome_build_info|: chrome info, i.e., version, channel and etc.
228 // |store_path|: path to the GCM store.
229 // |blocking_task_runner|: for running blocking file tasks.
230 // |url_request_context_getter|: for url requests.
231 // |delegate|: the delegate whose methods will be called asynchronously in
232 // response to events and messages.
233 virtual void Initialize(
234 const ChromeBuildInfo& chrome_build_info,
235 const base::FilePath& store_path,
236 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
237 const scoped_refptr<net::URLRequestContextGetter>&
238 url_request_context_getter,
239 scoped_ptr<Encryptor> encryptor,
240 Delegate* delegate) = 0;
242 // Starts the GCM service by first loading the data from the persistent store.
243 // This will then kick off the check-in if the check-in info is not found in
244 // the store.
245 virtual void Start() = 0;
247 // Stops using the GCM service. This will not erase the persisted data.
248 virtual void Stop() = 0;
250 // Checks out of the GCM service. This will erase all the cached and persisted
251 // data.
252 virtual void CheckOut() = 0;
254 // Registers the application for GCM. Delegate::OnRegisterFinished will be
255 // called asynchronously upon completion.
256 // |app_id|: application ID.
257 // |sender_ids|: list of IDs of the servers that are allowed to send the
258 // messages to the application. These IDs are assigned by the
259 // Google API Console.
260 virtual void Register(const std::string& app_id,
261 const std::vector<std::string>& sender_ids) = 0;
263 // Unregisters the application from GCM when it is uninstalled.
264 // Delegate::OnUnregisterFinished will be called asynchronously upon
265 // completion.
266 // |app_id|: application ID.
267 virtual void Unregister(const std::string& app_id) = 0;
269 // Sends a message to a given receiver. Delegate::OnSendFinished will be
270 // called asynchronously upon completion.
271 // |app_id|: application ID.
272 // |receiver_id|: registration ID of the receiver party.
273 // |message|: message to be sent.
274 virtual void Send(const std::string& app_id,
275 const std::string& receiver_id,
276 const OutgoingMessage& message) = 0;
278 // Enables or disables internal activity recording.
279 virtual void SetRecording(bool recording) = 0;
281 // Clear all recorded GCM activity logs.
282 virtual void ClearActivityLogs() = 0;
284 // Gets internal states and statistics.
285 virtual GCMStatistics GetStatistics() const = 0;
287 // Sets a list of accounts with OAuth2 tokens for the next checkin.
288 // |account_tokens|: list of email addresses, account IDs and OAuth2 access
289 // tokens.
290 virtual void SetAccountTokens(
291 const std::vector<AccountTokenInfo>& account_tokens) = 0;
293 // Persists the |account_mapping| in the store.
294 virtual void UpdateAccountMapping(const AccountMapping& account_mapping) = 0;
296 // Removes the account mapping related to |account_id| from the persistent
297 // store.
298 virtual void RemoveAccountMapping(const std::string& account_id) = 0;
300 // Sets last token fetch time in persistent store.
301 virtual void SetLastTokenFetchTime(const base::Time& time) = 0;
304 } // namespace gcm
306 #endif // COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_