1 // 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.
5 #ifndef GOOGLE_APIS_GCM_GCM_CLIENT_H_
6 #define GOOGLE_APIS_GCM_GCM_CLIENT_H_
12 #include "base/basictypes.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "google_apis/gcm/base/gcm_export.h"
15 #include "google_apis/gcm/gcm_activity.h"
17 template <class T
> class scoped_refptr
;
21 class SequencedTaskRunner
;
25 class URLRequestContextGetter
;
32 // Interface that encapsulates the network communications with the Google Cloud
33 // Messaging server. This interface is not supposed to be thread-safe.
34 class GCM_EXPORT GCMClient
{
37 // Successful operation.
43 // Profile not signed in.
45 // Previous asynchronous operation is still pending to finish. Certain
46 // operation, like register, is only allowed one at a time.
47 ASYNC_OPERATION_PENDING
,
48 // Network socket error.
50 // Problem at the server.
52 // Exceeded the specified TTL during message sending.
76 struct GCM_EXPORT ChromeBuildInfo
{
80 ChromePlatform platform
;
81 ChromeChannel channel
;
85 // Message data consisting of key-value pairs.
86 typedef std::map
<std::string
, std::string
> MessageData
;
88 // Message to be delivered to the other party.
89 struct GCM_EXPORT OutgoingMessage
{
99 static const int kMaximumTTL
= 4 * 7 * 24 * 60 * 60; // 4 weeks.
102 // Message being received from the other party.
103 struct GCM_EXPORT IncomingMessage
{
108 std::string collapse_key
;
109 std::string sender_id
;
112 // Detailed information of the Send Error event.
113 struct GCM_EXPORT SendErrorDetails
{
117 std::string message_id
;
118 MessageData additional_data
;
122 // Internal states and activity statistics of a GCM client.
123 struct GCM_EXPORT GCMStatistics
{
129 bool gcm_client_created
;
130 std::string gcm_client_state
;
131 bool connection_client_created
;
132 std::string connection_state
;
134 std::vector
<std::string
> registered_app_ids
;
136 int resend_queue_size
;
138 RecordedActivities recorded_activities
;
141 // A delegate interface that allows the GCMClient instance to interact with
142 // its caller, i.e. notifying asynchronous event.
145 // Called when the registration completed successfully or an error occurs.
146 // |app_id|: application ID.
147 // |registration_id|: non-empty if the registration completed successfully.
148 // |result|: the type of the error if an error occured, success otherwise.
149 virtual void OnRegisterFinished(const std::string
& app_id
,
150 const std::string
& registration_id
,
153 // Called when the unregistration completed.
154 // |app_id|: application ID.
155 // |result|: result of the unregistration.
156 virtual void OnUnregisterFinished(const std::string
& app_id
,
157 GCMClient::Result result
) = 0;
159 // Called when the message is scheduled to send successfully or an error
161 // |app_id|: application ID.
162 // |message_id|: ID of the message being sent.
163 // |result|: the type of the error if an error occured, success otherwise.
164 virtual void OnSendFinished(const std::string
& app_id
,
165 const std::string
& message_id
,
168 // Called when a message has been received.
169 // |app_id|: application ID.
170 // |message|: message received.
171 virtual void OnMessageReceived(const std::string
& app_id
,
172 const IncomingMessage
& message
) = 0;
174 // Called when some messages have been deleted from the server.
175 // |app_id|: application ID.
176 virtual void OnMessagesDeleted(const std::string
& app_id
) = 0;
178 // Called when a message failed to send to the server.
179 // |app_id|: application ID.
180 // |send_error_detials|: Details of the send error event, like mesasge ID.
181 virtual void OnMessageSendError(
182 const std::string
& app_id
,
183 const SendErrorDetails
& send_error_details
) = 0;
185 // Called when the GCM becomes ready. To get to this state, GCMClient
186 // finished loading from the GCM store and retrieved the device check-in
187 // from the server if it hadn't yet.
188 virtual void OnGCMReady() = 0;
190 // Called when activities are being recorded and a new activity has just
192 virtual void OnActivityRecorded() = 0;
196 virtual ~GCMClient();
198 // Begins initialization of the GCM Client. This will not trigger a
200 // |chrome_build_info|: chrome info, i.e., version, channel and etc.
201 // |store_path|: path to the GCM store.
202 // |account_ids|: account IDs to be related to the device when checking in.
203 // |blocking_task_runner|: for running blocking file tasks.
204 // |url_request_context_getter|: for url requests.
205 // |delegate|: the delegate whose methods will be called asynchronously in
206 // response to events and messages.
207 virtual void Initialize(
208 const ChromeBuildInfo
& chrome_build_info
,
209 const base::FilePath
& store_path
,
210 const std::vector
<std::string
>& account_ids
,
211 const scoped_refptr
<base::SequencedTaskRunner
>& blocking_task_runner
,
212 const scoped_refptr
<net::URLRequestContextGetter
>&
213 url_request_context_getter
,
214 scoped_ptr
<Encryptor
> encryptor
,
215 Delegate
* delegate
) = 0;
217 // Starts the GCM service by first loading the data from the persistent store.
218 // This will then kick off the check-in if the check-in info is not found in
220 virtual void Start() = 0;
222 // Stops using the GCM service. This will not erase the persisted data.
223 virtual void Stop() = 0;
225 // Checks out of the GCM service. This will erase all the cached and persisted
227 virtual void CheckOut() = 0;
229 // Registers the application for GCM. Delegate::OnRegisterFinished will be
230 // called asynchronously upon completion.
231 // |app_id|: application ID.
232 // |sender_ids|: list of IDs of the servers that are allowed to send the
233 // messages to the application. These IDs are assigned by the
234 // Google API Console.
235 virtual void Register(const std::string
& app_id
,
236 const std::vector
<std::string
>& sender_ids
) = 0;
238 // Unregisters the application from GCM when it is uninstalled.
239 // Delegate::OnUnregisterFinished will be called asynchronously upon
241 // |app_id|: application ID.
242 virtual void Unregister(const std::string
& app_id
) = 0;
244 // Sends a message to a given receiver. Delegate::OnSendFinished will be
245 // called asynchronously upon completion.
246 // |app_id|: application ID.
247 // |receiver_id|: registration ID of the receiver party.
248 // |message|: message to be sent.
249 virtual void Send(const std::string
& app_id
,
250 const std::string
& receiver_id
,
251 const OutgoingMessage
& message
) = 0;
253 // Enables or disables internal activity recording.
254 virtual void SetRecording(bool recording
) = 0;
256 // Clear all recorded GCM activity logs.
257 virtual void ClearActivityLogs() = 0;
259 // Gets internal states and statistics.
260 virtual GCMStatistics
GetStatistics() const = 0;
265 #endif // GOOGLE_APIS_GCM_GCM_CLIENT_H_