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
;
28 class URLRequestContextGetter
;
34 struct AccountMapping
;
36 // Interface that encapsulates the network communications with the Google Cloud
37 // Messaging server. This interface is not supposed to be thread-safe.
40 // Controls how GCM is being started. At first, GCMClient will be initialized
41 // and GCM store will be loaded. Then GCM connection may or may not be
42 // initiated depending on this enum value.
44 // GCM should be started only when it is being actually used. If no
45 // registration record is found, GCM will not kick off.
47 // GCM should be started immediately.
52 // Successful operation.
58 // Previous asynchronous operation is still pending to finish. Certain
59 // operation, like register, is only allowed one at a time.
60 ASYNC_OPERATION_PENDING
,
61 // Network socket error.
63 // Problem at the server.
65 // Exceeded the specified TTL during message sending.
89 struct ChromeBuildInfo
{
93 ChromePlatform platform
;
94 ChromeChannel channel
;
98 // Message data consisting of key-value pairs.
99 typedef std::map
<std::string
, std::string
> MessageData
;
101 // Message to be delivered to the other party.
102 struct OutgoingMessage
{
112 static const int kMaximumTTL
;
115 // Message being received from the other party.
116 struct IncomingMessage
{
121 std::string collapse_key
;
122 std::string sender_id
;
125 // Detailed information of the Send Error event.
126 struct SendErrorDetails
{
130 std::string message_id
;
131 MessageData additional_data
;
135 // Internal states and activity statistics of a GCM client.
136 struct GCMStatistics
{
142 bool gcm_client_created
;
143 std::string gcm_client_state
;
144 bool connection_client_created
;
145 std::string connection_state
;
147 std::vector
<std::string
> registered_app_ids
;
149 int resend_queue_size
;
151 RecordedActivities recorded_activities
;
154 // Information about account.
155 struct AccountTokenInfo
{
156 std::string account_id
;
158 std::string access_token
;
161 // A delegate interface that allows the GCMClient instance to interact with
162 // its caller, i.e. notifying asynchronous event.
165 // Called when the registration completed successfully or an error occurs.
166 // |app_id|: application ID.
167 // |registration_id|: non-empty if the registration completed successfully.
168 // |result|: the type of the error if an error occured, success otherwise.
169 virtual void OnRegisterFinished(const std::string
& app_id
,
170 const std::string
& registration_id
,
173 // Called when the unregistration completed.
174 // |app_id|: application ID.
175 // |result|: result of the unregistration.
176 virtual void OnUnregisterFinished(const std::string
& app_id
,
177 GCMClient::Result result
) = 0;
179 // Called when the message is scheduled to send successfully or an error
181 // |app_id|: application ID.
182 // |message_id|: ID of the message being sent.
183 // |result|: the type of the error if an error occured, success otherwise.
184 virtual void OnSendFinished(const std::string
& app_id
,
185 const std::string
& message_id
,
188 // Called when a message has been received.
189 // |app_id|: application ID.
190 // |message|: message received.
191 virtual void OnMessageReceived(const std::string
& app_id
,
192 const IncomingMessage
& message
) = 0;
194 // Called when some messages have been deleted from the server.
195 // |app_id|: application ID.
196 virtual void OnMessagesDeleted(const std::string
& app_id
) = 0;
198 // Called when a message failed to send to the server.
199 // |app_id|: application ID.
200 // |send_error_detials|: Details of the send error event, like mesasge ID.
201 virtual void OnMessageSendError(
202 const std::string
& app_id
,
203 const SendErrorDetails
& send_error_details
) = 0;
205 // Called when a message was acknowledged by the GCM server.
206 // |app_id|: application ID.
207 // |message_id|: ID of the acknowledged message.
208 virtual void OnSendAcknowledged(const std::string
& app_id
,
209 const std::string
& message_id
) = 0;
211 // Called when the GCM becomes ready. To get to this state, GCMClient
212 // finished loading from the GCM store and retrieved the device check-in
213 // from the server if it hadn't yet.
214 // |account_mappings|: a persisted list of accounts mapped to this GCM
216 // |last_token_fetch_time|: time of a last successful token fetch.
217 virtual void OnGCMReady(const std::vector
<AccountMapping
>& account_mappings
,
218 const base::Time
& last_token_fetch_time
) = 0;
220 // Called when activities are being recorded and a new activity has just
222 virtual void OnActivityRecorded() = 0;
224 // Called when a new connection is established and a successful handshake
225 // has been performed.
226 virtual void OnConnected(const net::IPEndPoint
& ip_endpoint
) = 0;
228 // Called when the connection is interrupted.
229 virtual void OnDisconnected() = 0;
233 virtual ~GCMClient();
235 // Begins initialization of the GCM Client. This will not trigger a
237 // |chrome_build_info|: chrome info, i.e., version, channel and etc.
238 // |store_path|: path to the GCM store.
239 // |blocking_task_runner|: for running blocking file tasks.
240 // |url_request_context_getter|: for url requests.
241 // |delegate|: the delegate whose methods will be called asynchronously in
242 // response to events and messages.
243 virtual void Initialize(
244 const ChromeBuildInfo
& chrome_build_info
,
245 const base::FilePath
& store_path
,
246 const scoped_refptr
<base::SequencedTaskRunner
>& blocking_task_runner
,
247 const scoped_refptr
<net::URLRequestContextGetter
>&
248 url_request_context_getter
,
249 scoped_ptr
<Encryptor
> encryptor
,
250 Delegate
* delegate
) = 0;
252 // This will initiate the GCM connection only if |start_mode| means to start
253 // the GCM immediately or the GCM registration records are found in the store.
254 // Note that it is OK to call Start multiple times and the implementation
255 // should handle it gracefully.
256 virtual void Start(StartMode start_mode
) = 0;
258 // Stops using the GCM service. This will not erase the persisted data.
259 virtual void Stop() = 0;
261 // Registers the application for GCM. Delegate::OnRegisterFinished will be
262 // called asynchronously upon completion.
263 // |app_id|: application ID.
264 // |sender_ids|: list of IDs of the servers that are allowed to send the
265 // messages to the application. These IDs are assigned by the
266 // Google API Console.
267 virtual void Register(const std::string
& app_id
,
268 const std::vector
<std::string
>& sender_ids
) = 0;
270 // Unregisters the application from GCM when it is uninstalled.
271 // Delegate::OnUnregisterFinished will be called asynchronously upon
273 // |app_id|: application ID.
274 virtual void Unregister(const std::string
& app_id
) = 0;
276 // Sends a message to a given receiver. Delegate::OnSendFinished will be
277 // called asynchronously upon completion.
278 // |app_id|: application ID.
279 // |receiver_id|: registration ID of the receiver party.
280 // |message|: message to be sent.
281 virtual void Send(const std::string
& app_id
,
282 const std::string
& receiver_id
,
283 const OutgoingMessage
& message
) = 0;
285 // Enables or disables internal activity recording.
286 virtual void SetRecording(bool recording
) = 0;
288 // Clear all recorded GCM activity logs.
289 virtual void ClearActivityLogs() = 0;
291 // Gets internal states and statistics.
292 virtual GCMStatistics
GetStatistics() const = 0;
294 // Sets a list of accounts with OAuth2 tokens for the next checkin.
295 // |account_tokens|: list of email addresses, account IDs and OAuth2 access
297 virtual void SetAccountTokens(
298 const std::vector
<AccountTokenInfo
>& account_tokens
) = 0;
300 // Persists the |account_mapping| in the store.
301 virtual void UpdateAccountMapping(const AccountMapping
& account_mapping
) = 0;
303 // Removes the account mapping related to |account_id| from the persistent
305 virtual void RemoveAccountMapping(const std::string
& account_id
) = 0;
307 // Sets last token fetch time in persistent store.
308 virtual void SetLastTokenFetchTime(const base::Time
& time
) = 0;
310 // Updates the timer used by the HeartbeatManager for sending heartbeats.
311 virtual void UpdateHeartbeatTimer(scoped_ptr
<base::Timer
> timer
) = 0;
316 #endif // COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_