blob: 85d3af71eb7debe9c97a8c652ec83b98e755bf1d [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"
14
15namespace base {
16class TaskRunner;
17}
18
19namespace gcm {
20
21// Interface that encapsulates the network communications with the Google Cloud
22// Messaging server. This interface is not supposed to be thread-safe.
23class GCM_EXPORT GCMClient {
24 public:
25 enum Result {
26 // Successful operation.
27 SUCCESS,
[email protected]b16a7c52013-11-20 01:18:5928 // Invalid parameter.
29 INVALID_PARAMETER,
30 // Previous asynchronous operation is still pending to finish. Certain
31 // operation, like register, is only allowed one at a time.
32 ASYNC_OPERATION_PENDING,
[email protected]e4097c82013-11-08 00:16:1233 // Network socket error.
34 NETWORK_ERROR,
35 // Problem at the server.
36 SERVER_ERROR,
37 // Exceeded the specified TTL during message sending.
38 TTL_EXCEEDED,
39 // Other errors.
40 UNKNOWN_ERROR
41 };
42
43 // Message data consisting of key-value pairs.
[email protected]b16a7c52013-11-20 01:18:5944 typedef std::map<std::string, std::string> MessageData;
[email protected]e4097c82013-11-08 00:16:1245
46 // Message to be delivered to the other party.
[email protected]b16a7c52013-11-20 01:18:5947 struct GCM_EXPORT OutgoingMessage {
[email protected]e4097c82013-11-08 00:16:1248 OutgoingMessage();
49 ~OutgoingMessage();
50
51 // Message ID.
52 std::string id;
53 // In seconds.
54 int time_to_live;
55 MessageData data;
56 };
57
58 // Message being received from the other party.
[email protected]b16a7c52013-11-20 01:18:5959 struct GCM_EXPORT IncomingMessage {
[email protected]e4097c82013-11-08 00:16:1260 IncomingMessage();
61 ~IncomingMessage();
62
63 MessageData data;
64 };
65
66 // The check-in info for the user. Returned by the server.
[email protected]b16a7c52013-11-20 01:18:5967 struct GCM_EXPORT CheckInInfo {
[email protected]e4097c82013-11-08 00:16:1268 CheckInInfo() : android_id(0), secret(0) {}
69 bool IsValid() const { return android_id != 0 && secret != 0; }
[email protected]eb44a8a2013-11-26 19:55:5370 void Reset() {
71 android_id = 0;
72 secret = 0;
73 }
[email protected]e4097c82013-11-08 00:16:1274
75 uint64 android_id;
76 uint64 secret;
77 };
78
79 // A delegate interface that allows the GCMClient instance to interact with
80 // its caller, i.e. notifying asynchronous event.
81 class Delegate {
82 public:
83 // Called when the user has been checked in successfully or an error occurs.
84 // |checkin_info|: valid if the checkin completed successfully.
85 // |result|: the type of the error if an error occured, success otherwise.
[email protected]eb44a8a2013-11-26 19:55:5386 virtual void OnCheckInFinished(const CheckInInfo& checkin_info,
[email protected]e4097c82013-11-08 00:16:1287 Result result) = 0;
88
89 // Called when the registration completed successfully or an error occurs.
90 // |app_id|: application ID.
91 // |registration_id|: non-empty if the registration completed successfully.
92 // |result|: the type of the error if an error occured, success otherwise.
93 virtual void OnRegisterFinished(const std::string& app_id,
94 const std::string& registration_id,
95 Result result) = 0;
96
97 // Called when the unregistration completed successfully or an error occurs.
98 // |app_id|: application ID.
99 // |result|: the type of the error if an error occured, success otherwise.
100 virtual void OnUnregisterFinished(const std::string& app_id,
101 Result result) = 0;
102
103 // Called when the message is scheduled to send successfully or an error
104 // occurs.
105 // |app_id|: application ID.
106 // |message_id|: ID of the message being sent.
107 // |result|: the type of the error if an error occured, success otherwise.
108 virtual void OnSendFinished(const std::string& app_id,
109 const std::string& message_id,
110 Result result) = 0;
111
112 // Called when a message has been received.
113 // |app_id|: application ID.
[email protected]e4097c82013-11-08 00:16:12114 // |message|: message received.
115 virtual void OnMessageReceived(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12116 const IncomingMessage& message) = 0;
117
118 // Called when some messages have been deleted from the server.
119 // |app_id|: application ID.
120 virtual void OnMessagesDeleted(const std::string& app_id) = 0;
121
122 // Called when a message failed to send to the server.
123 // |app_id|: application ID.
124 // |message_id|: ID of the message being sent.
125 // |result|: the type of the error if an error occured, success otherwise.
126 virtual void OnMessageSendError(const std::string& app_id,
127 const std::string& message_id,
128 Result result) = 0;
129
130 // Returns the checkin info associated with this user. The delegate class
131 // is expected to persist the checkin info that is provided by
[email protected]eb44a8a2013-11-26 19:55:53132 // OnCheckInFinished.
[email protected]e4097c82013-11-08 00:16:12133 virtual CheckInInfo GetCheckInInfo() const = 0;
134
135 // Called when the loading from the persistent store is done. The loading
136 // is triggered asynchronously when GCMClient is created.
137 virtual void OnLoadingCompleted() = 0;
138
139 // Returns a task runner for file operations that may block. This is used
140 // in writing to or reading from the persistent store.
141 virtual base::TaskRunner* GetFileTaskRunner() = 0;
142 };
143
144 // Returns the single instance. Multiple profiles share the same client
145 // that makes use of the same MCS connection.
146 static GCMClient* Get();
147
148 // Passes a mocked instance for testing purpose.
149 static void SetForTesting(GCMClient* client);
150
151 // Checks in the user to use GCM. If the device has not been checked in, it
152 // will be done first.
153 // |username|: the username (email address) used to check in with the server.
154 // |delegate|: the delegate whose methods will be called asynchronously in
155 // response to events and messages.
[email protected]eb44a8a2013-11-26 19:55:53156 virtual void CheckIn(const std::string& username, Delegate* delegate) = 0;
[email protected]e4097c82013-11-08 00:16:12157
158 // Registers the application for GCM. Delegate::OnRegisterFinished will be
159 // called asynchronously upon completion.
[email protected]eb44a8a2013-11-26 19:55:53160 // |username|: the username (email address) passed in CheckIn.
[email protected]e4097c82013-11-08 00:16:12161 // |app_id|: application ID.
162 // |cert|: SHA-1 of public key of the application, in base16 format.
163 // |sender_ids|: list of IDs of the servers that are allowed to send the
164 // messages to the application. These IDs are assigned by the
165 // Google API Console.
166 virtual void Register(const std::string& username,
167 const std::string& app_id,
168 const std::string& cert,
169 const std::vector<std::string>& sender_ids) = 0;
170
171 // Unregisters the application from GCM when it is uninstalled.
172 // Delegate::OnUnregisterFinished will be called asynchronously upon
173 // completion.
[email protected]eb44a8a2013-11-26 19:55:53174 // |username|: the username (email address) passed in CheckIn.
[email protected]e4097c82013-11-08 00:16:12175 // |app_id|: application ID.
176 virtual void Unregister(const std::string& username,
177 const std::string& app_id) = 0;
178
179 // Sends a message to a given receiver. Delegate::OnSendFinished will be
180 // called asynchronously upon completion.
[email protected]eb44a8a2013-11-26 19:55:53181 // |username|: the username (email address) passed in CheckIn.
[email protected]e4097c82013-11-08 00:16:12182 // |app_id|: application ID.
183 // |receiver_id|: registration ID of the receiver party.
184 // |message|: message to be sent.
185 virtual void Send(const std::string& username,
186 const std::string& app_id,
187 const std::string& receiver_id,
188 const OutgoingMessage& message) = 0;
189
190 // Returns true if the loading from the persistent store is still in progress.
191 virtual bool IsLoading() const = 0;
192
193 protected:
194 virtual ~GCMClient() {}
195};
196
197} // namespace gcm
198
199#endif // GOOGLE_APIS_GCM_GCM_CLIENT_H_