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.
4
5#ifndef COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_
6#define COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
12#include "base/basictypes.h"
13#include "base/memory/scoped_ptr.h"
14#include "components/gcm_driver/gcm_activity.h"
15
16template <class T> class scoped_refptr;
17
18class GURL;
19
20namespace base {
21class FilePath;
22class SequencedTaskRunner;
23}
24
25namespace net {
26class IPEndPoint;
27class URLRequestContextGetter;
28}
29
30namespace gcm {
31
32class Encryptor;
33struct AccountMapping;
34
35// Interface that encapsulates the network communications with the Google Cloud
36// Messaging server. This interface is not supposed to be thread-safe.
37class 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
59  };
60
61  enum ChromePlatform {
62    PLATFORM_WIN,
63    PLATFORM_MAC,
64    PLATFORM_LINUX,
65    PLATFORM_CROS,
66    PLATFORM_IOS,
67    PLATFORM_ANDROID,
68    PLATFORM_UNKNOWN
69  };
70
71  enum ChromeChannel {
72    CHANNEL_STABLE,
73    CHANNEL_BETA,
74    CHANNEL_DEV,
75    CHANNEL_CANARY,
76    CHANNEL_UNKNOWN
77  };
78
79  struct ChromeBuildInfo {
80    ChromeBuildInfo();
81    ~ChromeBuildInfo();
82
83    ChromePlatform platform;
84    ChromeChannel channel;
85    std::string version;
86  };
87
88  // Message data consisting of key-value pairs.
89  typedef std::map<std::string, std::string> MessageData;
90
91  // Message to be delivered to the other party.
92  struct OutgoingMessage {
93    OutgoingMessage();
94    ~OutgoingMessage();
95
96    // Message ID.
97    std::string id;
98    // In seconds.
99    int time_to_live;
100    MessageData data;
101
102    static const int kMaximumTTL;
103  };
104
105  // Message being received from the other party.
106  struct IncomingMessage {
107    IncomingMessage();
108    ~IncomingMessage();
109
110    MessageData data;
111    std::string collapse_key;
112    std::string sender_id;
113  };
114
115  // Detailed information of the Send Error event.
116  struct SendErrorDetails {
117    SendErrorDetails();
118    ~SendErrorDetails();
119
120    std::string message_id;
121    MessageData additional_data;
122    Result result;
123  };
124
125  // Internal states and activity statistics of a GCM client.
126  struct GCMStatistics {
127   public:
128    GCMStatistics();
129    ~GCMStatistics();
130
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;
140
141    RecordedActivities recorded_activities;
142  };
143
144  // Information about account.
145  struct AccountTokenInfo {
146    std::string account_id;
147    std::string email;
148    std::string access_token;
149  };
150
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;
162
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;
168
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;
177
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;
183
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;
187
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;
194
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;
200
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    virtual void OnGCMReady(
207        const std::vector<AccountMapping>& account_mappings) = 0;
208
209    // Called when activities are being recorded and a new activity has just
210    // been recorded.
211    virtual void OnActivityRecorded() = 0;
212
213    // Called when a new connection is established and a successful handshake
214    // has been performed.
215    virtual void OnConnected(const net::IPEndPoint& ip_endpoint) = 0;
216
217    // Called when the connection is interrupted.
218    virtual void OnDisconnected() = 0;
219  };
220
221  GCMClient();
222  virtual ~GCMClient();
223
224  // Begins initialization of the GCM Client. This will not trigger a
225  // connection.
226  // |chrome_build_info|: chrome info, i.e., version, channel and etc.
227  // |store_path|: path to the GCM store.
228  // |blocking_task_runner|: for running blocking file tasks.
229  // |url_request_context_getter|: for url requests.
230  // |delegate|: the delegate whose methods will be called asynchronously in
231  //             response to events and messages.
232  virtual void Initialize(
233      const ChromeBuildInfo& chrome_build_info,
234      const base::FilePath& store_path,
235      const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
236      const scoped_refptr<net::URLRequestContextGetter>&
237          url_request_context_getter,
238      scoped_ptr<Encryptor> encryptor,
239      Delegate* delegate) = 0;
240
241  // Starts the GCM service by first loading the data from the persistent store.
242  // This will then kick off the check-in if the check-in info is not found in
243  // the store.
244  virtual void Start() = 0;
245
246  // Stops using the GCM service. This will not erase the persisted data.
247  virtual void Stop() = 0;
248
249  // Checks out of the GCM service. This will erase all the cached and persisted
250  // data.
251  virtual void CheckOut() = 0;
252
253  // Registers the application for GCM. Delegate::OnRegisterFinished will be
254  // called asynchronously upon completion.
255  // |app_id|: application ID.
256  // |sender_ids|: list of IDs of the servers that are allowed to send the
257  //               messages to the application. These IDs are assigned by the
258  //               Google API Console.
259  virtual void Register(const std::string& app_id,
260                        const std::vector<std::string>& sender_ids) = 0;
261
262  // Unregisters the application from GCM when it is uninstalled.
263  // Delegate::OnUnregisterFinished will be called asynchronously upon
264  // completion.
265  // |app_id|: application ID.
266  virtual void Unregister(const std::string& app_id) = 0;
267
268  // Sends a message to a given receiver. Delegate::OnSendFinished will be
269  // called asynchronously upon completion.
270  // |app_id|: application ID.
271  // |receiver_id|: registration ID of the receiver party.
272  // |message|: message to be sent.
273  virtual void Send(const std::string& app_id,
274                    const std::string& receiver_id,
275                    const OutgoingMessage& message) = 0;
276
277  // Enables or disables internal activity recording.
278  virtual void SetRecording(bool recording) = 0;
279
280  // Clear all recorded GCM activity logs.
281  virtual void ClearActivityLogs() = 0;
282
283  // Gets internal states and statistics.
284  virtual GCMStatistics GetStatistics() const = 0;
285
286  // Sets a list of accounts with OAuth2 tokens for the next checkin.
287  // |account_tokens| maps email addresses to OAuth2 access tokens.
288  virtual void SetAccountsForCheckin(
289      const std::map<std::string, std::string>& account_tokens) = 0;
290
291  // Persists the |account_mapping| in the store.
292  virtual void UpdateAccountMapping(const AccountMapping& account_mapping) = 0;
293
294  // Removes the account mapping related to |account_id| from the persistent
295  // store.
296  virtual void RemoveAccountMapping(const std::string& account_id) = 0;
297};
298
299}  // namespace gcm
300
301#endif  // COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_
302