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_
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
;
22 class SequencedTaskRunner
;
27 class URLRequestContextGetter
;
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.
40 // Successful operation.
46 // Profile 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.
53 // Problem at the server.
55 // Exceeded the specified TTL during message sending.
79 struct ChromeBuildInfo
{
83 ChromePlatform platform
;
84 ChromeChannel channel
;
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
{
102 static const int kMaximumTTL
= 4 * 7 * 24 * 60 * 60; // 4 weeks.
105 // Message being received from the other party.
106 struct IncomingMessage
{
111 std::string collapse_key
;
112 std::string sender_id
;
115 // Detailed information of the Send Error event.
116 struct SendErrorDetails
{
120 std::string message_id
;
121 MessageData additional_data
;
125 // Internal states and activity statistics of a GCM client.
126 struct GCMStatistics
{
132 bool gcm_client_created
;
133 std::string gcm_client_state
;
134 bool connection_client_created
;
135 std::string connection_state
;
137 std::vector
<std::string
> registered_app_ids
;
139 int resend_queue_size
;
141 RecordedActivities recorded_activities
;
144 // A delegate interface that allows the GCMClient instance to interact with
145 // its caller, i.e. notifying asynchronous event.
148 // Called when the registration completed successfully or an error occurs.
149 // |app_id|: application ID.
150 // |registration_id|: non-empty if the registration completed successfully.
151 // |result|: the type of the error if an error occured, success otherwise.
152 virtual void OnRegisterFinished(const std::string
& app_id
,
153 const std::string
& registration_id
,
156 // Called when the unregistration completed.
157 // |app_id|: application ID.
158 // |result|: result of the unregistration.
159 virtual void OnUnregisterFinished(const std::string
& app_id
,
160 GCMClient::Result result
) = 0;
162 // Called when the message is scheduled to send successfully or an error
164 // |app_id|: application ID.
165 // |message_id|: ID of the message being sent.
166 // |result|: the type of the error if an error occured, success otherwise.
167 virtual void OnSendFinished(const std::string
& app_id
,
168 const std::string
& message_id
,
171 // Called when a message has been received.
172 // |app_id|: application ID.
173 // |message|: message received.
174 virtual void OnMessageReceived(const std::string
& app_id
,
175 const IncomingMessage
& message
) = 0;
177 // Called when some messages have been deleted from the server.
178 // |app_id|: application ID.
179 virtual void OnMessagesDeleted(const std::string
& app_id
) = 0;
181 // Called when a message failed to send to the server.
182 // |app_id|: application ID.
183 // |send_error_detials|: Details of the send error event, like mesasge ID.
184 virtual void OnMessageSendError(
185 const std::string
& app_id
,
186 const SendErrorDetails
& send_error_details
) = 0;
188 // Called when a message was acknowledged by the GCM server.
189 // |app_id|: application ID.
190 // |message_id|: ID of the acknowledged message.
191 virtual void OnSendAcknowledged(const std::string
& app_id
,
192 const std::string
& message_id
) = 0;
194 // Called when the GCM becomes ready. To get to this state, GCMClient
195 // finished loading from the GCM store and retrieved the device check-in
196 // from the server if it hadn't yet.
197 virtual void OnGCMReady() = 0;
199 // Called when activities are being recorded and a new activity has just
201 virtual void OnActivityRecorded() = 0;
203 // Called when a new connection is established and a successful handshake
204 // has been performed.
205 virtual void OnConnected(const net::IPEndPoint
& ip_endpoint
) = 0;
207 // Called when the connection is interrupted.
208 virtual void OnDisconnected() = 0;
212 virtual ~GCMClient();
214 // Begins initialization of the GCM Client. This will not trigger a
216 // |chrome_build_info|: chrome info, i.e., version, channel and etc.
217 // |store_path|: path to the GCM store.
218 // |blocking_task_runner|: for running blocking file tasks.
219 // |url_request_context_getter|: for url requests.
220 // |delegate|: the delegate whose methods will be called asynchronously in
221 // response to events and messages.
222 virtual void Initialize(
223 const ChromeBuildInfo
& chrome_build_info
,
224 const base::FilePath
& store_path
,
225 const scoped_refptr
<base::SequencedTaskRunner
>& blocking_task_runner
,
226 const scoped_refptr
<net::URLRequestContextGetter
>&
227 url_request_context_getter
,
228 scoped_ptr
<Encryptor
> encryptor
,
229 Delegate
* delegate
) = 0;
231 // Starts the GCM service by first loading the data from the persistent store.
232 // This will then kick off the check-in if the check-in info is not found in
234 virtual void Start() = 0;
236 // Stops using the GCM service. This will not erase the persisted data.
237 virtual void Stop() = 0;
239 // Checks out of the GCM service. This will erase all the cached and persisted
241 virtual void CheckOut() = 0;
243 // Registers the application for GCM. Delegate::OnRegisterFinished will be
244 // called asynchronously upon completion.
245 // |app_id|: application ID.
246 // |sender_ids|: list of IDs of the servers that are allowed to send the
247 // messages to the application. These IDs are assigned by the
248 // Google API Console.
249 virtual void Register(const std::string
& app_id
,
250 const std::vector
<std::string
>& sender_ids
) = 0;
252 // Unregisters the application from GCM when it is uninstalled.
253 // Delegate::OnUnregisterFinished will be called asynchronously upon
255 // |app_id|: application ID.
256 virtual void Unregister(const std::string
& app_id
) = 0;
258 // Sends a message to a given receiver. Delegate::OnSendFinished will be
259 // called asynchronously upon completion.
260 // |app_id|: application ID.
261 // |receiver_id|: registration ID of the receiver party.
262 // |message|: message to be sent.
263 virtual void Send(const std::string
& app_id
,
264 const std::string
& receiver_id
,
265 const OutgoingMessage
& message
) = 0;
267 // Enables or disables internal activity recording.
268 virtual void SetRecording(bool recording
) = 0;
270 // Clear all recorded GCM activity logs.
271 virtual void ClearActivityLogs() = 0;
273 // Gets internal states and statistics.
274 virtual GCMStatistics
GetStatistics() const = 0;
276 // Sets a list of accounts with OAuth2 tokens for the next checkin.
277 // |account_tokens| maps email addresses to OAuth2 access tokens.
278 virtual void SetAccountsForCheckin(
279 const std::map
<std::string
, std::string
>& account_tokens
) = 0;
281 // Persists the |account_mapping| in the store.
282 virtual void UpdateAccountMapping(const AccountMapping
& account_mapping
) = 0;
284 // Removes the account mapping related to |account_id| from the persistent
286 virtual void RemoveAccountMapping(const std::string
& account_id
) = 0;
291 #endif // COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_