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/linked_ptr.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "components/gcm_driver/gcm_activity.h"
16 #include "components/gcm_driver/registration_info.h"
18 template <class T
> class scoped_refptr
;
24 class SequencedTaskRunner
;
30 class URLRequestContextGetter
;
36 struct AccountMapping
;
38 // Interface that encapsulates the network communications with the Google Cloud
39 // Messaging server. This interface is not supposed to be thread-safe.
42 // Controls how GCM is being started. At first, GCMClient will be initialized
43 // and GCM store will be loaded. Then GCM connection may or may not be
44 // initiated depending on this enum value.
46 // GCM should be started only when it is being actually used. If no
47 // registration record is found, GCM will not kick off.
49 // GCM should be started immediately.
54 // Successful operation.
60 // Previous asynchronous operation is still pending to finish. Certain
61 // operation, like register, is only allowed one at a time.
62 ASYNC_OPERATION_PENDING
,
63 // Network socket error.
65 // Problem at the server.
67 // Exceeded the specified TTL during message sending.
91 struct ChromeBuildInfo
{
95 ChromePlatform platform
;
96 ChromeChannel channel
;
100 // Message data consisting of key-value pairs.
101 typedef std::map
<std::string
, std::string
> MessageData
;
103 // Message to be delivered to the other party.
104 struct OutgoingMessage
{
114 static const int kMaximumTTL
;
117 // Message being received from the other party.
118 struct IncomingMessage
{
123 std::string collapse_key
;
124 std::string sender_id
;
127 // Detailed information of the Send Error event.
128 struct SendErrorDetails
{
132 std::string message_id
;
133 MessageData additional_data
;
137 // Internal states and activity statistics of a GCM client.
138 struct GCMStatistics
{
144 bool gcm_client_created
;
145 std::string gcm_client_state
;
146 bool connection_client_created
;
147 std::string connection_state
;
149 std::vector
<std::string
> registered_app_ids
;
151 int resend_queue_size
;
153 RecordedActivities recorded_activities
;
156 // Information about account.
157 struct AccountTokenInfo
{
158 std::string account_id
;
160 std::string access_token
;
163 // A delegate interface that allows the GCMClient instance to interact with
164 // its caller, i.e. notifying asynchronous event.
167 // Called when the registration completed successfully or an error occurs.
168 // |registration_info|: the specific information required for the
170 // |registration_id|: non-empty if the registration completed successfully.
171 // |result|: the type of the error if an error occured, success otherwise.
172 virtual void OnRegisterFinished(
173 const linked_ptr
<RegistrationInfo
>& registration_info
,
174 const std::string
& registration_id
,
177 // Called when the unregistration completed.
178 // |registration_info|: the specific information required for the
180 // |result|: result of the unregistration.
181 virtual void OnUnregisterFinished(
182 const linked_ptr
<RegistrationInfo
>& registration_info
,
183 GCMClient::Result result
) = 0;
185 // Called when the message is scheduled to send successfully or an error
187 // |app_id|: application ID.
188 // |message_id|: ID of the message being sent.
189 // |result|: the type of the error if an error occured, success otherwise.
190 virtual void OnSendFinished(const std::string
& app_id
,
191 const std::string
& message_id
,
194 // Called when a message has been received.
195 // |app_id|: application ID.
196 // |message|: message received.
197 virtual void OnMessageReceived(const std::string
& app_id
,
198 const IncomingMessage
& message
) = 0;
200 // Called when some messages have been deleted from the server.
201 // |app_id|: application ID.
202 virtual void OnMessagesDeleted(const std::string
& app_id
) = 0;
204 // Called when a message failed to send to the server.
205 // |app_id|: application ID.
206 // |send_error_detials|: Details of the send error event, like mesasge ID.
207 virtual void OnMessageSendError(
208 const std::string
& app_id
,
209 const SendErrorDetails
& send_error_details
) = 0;
211 // Called when a message was acknowledged by the GCM server.
212 // |app_id|: application ID.
213 // |message_id|: ID of the acknowledged message.
214 virtual void OnSendAcknowledged(const std::string
& app_id
,
215 const std::string
& message_id
) = 0;
217 // Called when the GCM becomes ready. To get to this state, GCMClient
218 // finished loading from the GCM store and retrieved the device check-in
219 // from the server if it hadn't yet.
220 // |account_mappings|: a persisted list of accounts mapped to this GCM
222 // |last_token_fetch_time|: time of a last successful token fetch.
223 virtual void OnGCMReady(const std::vector
<AccountMapping
>& account_mappings
,
224 const base::Time
& last_token_fetch_time
) = 0;
226 // Called when activities are being recorded and a new activity has just
228 virtual void OnActivityRecorded() = 0;
230 // Called when a new connection is established and a successful handshake
231 // has been performed.
232 virtual void OnConnected(const net::IPEndPoint
& ip_endpoint
) = 0;
234 // Called when the connection is interrupted.
235 virtual void OnDisconnected() = 0;
239 virtual ~GCMClient();
241 // Begins initialization of the GCM Client. This will not trigger a
243 // |chrome_build_info|: chrome info, i.e., version, channel and etc.
244 // |store_path|: path to the GCM store.
245 // |blocking_task_runner|: for running blocking file tasks.
246 // |url_request_context_getter|: for url requests.
247 // |delegate|: the delegate whose methods will be called asynchronously in
248 // response to events and messages.
249 virtual void Initialize(
250 const ChromeBuildInfo
& chrome_build_info
,
251 const base::FilePath
& store_path
,
252 const scoped_refptr
<base::SequencedTaskRunner
>& blocking_task_runner
,
253 const scoped_refptr
<net::URLRequestContextGetter
>&
254 url_request_context_getter
,
255 scoped_ptr
<Encryptor
> encryptor
,
256 Delegate
* delegate
) = 0;
258 // This will initiate the GCM connection only if |start_mode| means to start
259 // the GCM immediately or the GCM registration records are found in the store.
260 // Note that it is OK to call Start multiple times and the implementation
261 // should handle it gracefully.
262 virtual void Start(StartMode start_mode
) = 0;
264 // Stops using the GCM service. This will not erase the persisted data.
265 virtual void Stop() = 0;
267 // Registers with the server to access the provided service.
268 // Delegate::OnRegisterFinished will be called asynchronously upon completion.
269 // |registration_info|: the specific information required for the
270 // registration. For GCM, it will contain app id and
271 // sender IDs. For InstanceID, it will contain app_id,
272 // authorized entity and scope.
273 virtual void Register(
274 const linked_ptr
<RegistrationInfo
>& registration_info
) = 0;
276 // Unregisters from the server to stop accessing the provided service.
277 // Delegate::OnUnregisterFinished will be called asynchronously upon
279 // |registration_info|: the specific information required for the
280 // registration. For GCM, it will contain app id (sender
281 // IDs can be ingored). For InstanceID, it will contain
282 // app id, authorized entity and scope.
283 virtual void Unregister(
284 const linked_ptr
<RegistrationInfo
>& registration_info
) = 0;
286 // Sends a message to a given receiver. Delegate::OnSendFinished will be
287 // called asynchronously upon completion.
288 // |app_id|: application ID.
289 // |receiver_id|: registration ID of the receiver party.
290 // |message|: message to be sent.
291 virtual void Send(const std::string
& app_id
,
292 const std::string
& receiver_id
,
293 const OutgoingMessage
& message
) = 0;
295 // Enables or disables internal activity recording.
296 virtual void SetRecording(bool recording
) = 0;
298 // Clear all recorded GCM activity logs.
299 virtual void ClearActivityLogs() = 0;
301 // Gets internal states and statistics.
302 virtual GCMStatistics
GetStatistics() const = 0;
304 // Sets a list of accounts with OAuth2 tokens for the next checkin.
305 // |account_tokens|: list of email addresses, account IDs and OAuth2 access
307 virtual void SetAccountTokens(
308 const std::vector
<AccountTokenInfo
>& account_tokens
) = 0;
310 // Persists the |account_mapping| in the store.
311 virtual void UpdateAccountMapping(const AccountMapping
& account_mapping
) = 0;
313 // Removes the account mapping related to |account_id| from the persistent
315 virtual void RemoveAccountMapping(const std::string
& account_id
) = 0;
317 // Sets last token fetch time in persistent store.
318 virtual void SetLastTokenFetchTime(const base::Time
& time
) = 0;
320 // Updates the timer used by the HeartbeatManager for sending heartbeats.
321 virtual void UpdateHeartbeatTimer(scoped_ptr
<base::Timer
> timer
) = 0;
323 // Adds the Instance ID data for a specific app to the persistent store.
324 virtual void AddInstanceIDData(const std::string
& app_id
,
325 const std::string
& instance_id
,
326 const std::string
& extra_data
) = 0;
328 // Removes the Instance ID data for a specific app from the persistent store.
329 virtual void RemoveInstanceIDData(const std::string
& app_id
) = 0;
331 // Retrieves the Instance ID data for a specific app from the persistent
333 virtual void GetInstanceIDData(const std::string
& app_id
,
334 std::string
* instance_id
,
335 std::string
* extra_data
) = 0;
337 // Gets and sets custom heartbeat interval for the MCS connection.
338 // |scope| is used to identify the component that requests a custom interval
339 // to be set, and allows that component to later revoke the setting. It should
341 virtual void AddHeartbeatInterval(const std::string
& scope
,
342 int interval_ms
) = 0;
343 virtual void RemoveHeartbeatInterval(const std::string
& scope
) = 0;
348 #endif // COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_