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/common/gcm_messages.h"
16 #include "components/gcm_driver/gcm_activity.h"
17 #include "components/gcm_driver/registration_info.h"
19 template <class T
> class scoped_refptr
;
25 class SequencedTaskRunner
;
31 class URLRequestContextGetter
;
37 struct AccountMapping
;
39 // Interface that encapsulates the network communications with the Google Cloud
40 // Messaging server. This interface is not supposed to be thread-safe.
43 // Controls how GCM is being started. At first, GCMClient will be initialized
44 // and GCM store will be loaded. Then GCM connection may or may not be
45 // initiated depending on this enum value.
47 // GCM should be started only when it is being actually used. If no
48 // registration record is found, GCM will not kick off.
50 // GCM should be started immediately.
55 // Successful operation.
61 // Previous asynchronous operation is still pending to finish. Certain
62 // operation, like register, is only allowed one at a time.
63 ASYNC_OPERATION_PENDING
,
64 // Network socket error.
66 // Problem at the server.
68 // Exceeded the specified TTL during message sending.
92 struct ChromeBuildInfo
{
96 ChromePlatform platform
;
97 ChromeChannel channel
;
101 // Detailed information of the Send Error event.
102 struct SendErrorDetails
{
106 std::string message_id
;
107 MessageData additional_data
;
111 // Internal states and activity statistics of a GCM client.
112 struct GCMStatistics
{
118 bool gcm_client_created
;
119 std::string gcm_client_state
;
120 bool connection_client_created
;
121 std::string connection_state
;
123 std::vector
<std::string
> registered_app_ids
;
125 int resend_queue_size
;
127 RecordedActivities recorded_activities
;
130 // Information about account.
131 struct AccountTokenInfo
{
132 std::string account_id
;
134 std::string access_token
;
137 // A delegate interface that allows the GCMClient instance to interact with
138 // its caller, i.e. notifying asynchronous event.
141 // Called when the registration completed successfully or an error occurs.
142 // |registration_info|: the specific information required for the
144 // |registration_id|: non-empty if the registration completed successfully.
145 // |result|: the type of the error if an error occured, success otherwise.
146 virtual void OnRegisterFinished(
147 const linked_ptr
<RegistrationInfo
>& registration_info
,
148 const std::string
& registration_id
,
151 // Called when the unregistration completed.
152 // |registration_info|: the specific information required for the
154 // |result|: result of the unregistration.
155 virtual void OnUnregisterFinished(
156 const linked_ptr
<RegistrationInfo
>& registration_info
,
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 a message was acknowledged by the GCM server.
186 // |app_id|: application ID.
187 // |message_id|: ID of the acknowledged message.
188 virtual void OnSendAcknowledged(const std::string
& app_id
,
189 const std::string
& message_id
) = 0;
191 // Called when the GCM becomes ready. To get to this state, GCMClient
192 // finished loading from the GCM store and retrieved the device check-in
193 // from the server if it hadn't yet.
194 // |account_mappings|: a persisted list of accounts mapped to this GCM
196 // |last_token_fetch_time|: time of a last successful token fetch.
197 virtual void OnGCMReady(const std::vector
<AccountMapping
>& account_mappings
,
198 const base::Time
& last_token_fetch_time
) = 0;
200 // Called when activities are being recorded and a new activity has just
202 virtual void OnActivityRecorded() = 0;
204 // Called when a new connection is established and a successful handshake
205 // has been performed.
206 virtual void OnConnected(const net::IPEndPoint
& ip_endpoint
) = 0;
208 // Called when the connection is interrupted.
209 virtual void OnDisconnected() = 0;
213 virtual ~GCMClient();
215 // Begins initialization of the GCM Client. This will not trigger a
217 // |chrome_build_info|: chrome info, i.e., version, channel and etc.
218 // |store_path|: path to the GCM store.
219 // |blocking_task_runner|: for running blocking file tasks.
220 // |url_request_context_getter|: for url requests.
221 // |delegate|: the delegate whose methods will be called asynchronously in
222 // response to events and messages.
223 virtual void Initialize(
224 const ChromeBuildInfo
& chrome_build_info
,
225 const base::FilePath
& store_path
,
226 const scoped_refptr
<base::SequencedTaskRunner
>& blocking_task_runner
,
227 const scoped_refptr
<net::URLRequestContextGetter
>&
228 url_request_context_getter
,
229 scoped_ptr
<Encryptor
> encryptor
,
230 Delegate
* delegate
) = 0;
232 // This will initiate the GCM connection only if |start_mode| means to start
233 // the GCM immediately or the GCM registration records are found in the store.
234 // Note that it is OK to call Start multiple times and the implementation
235 // should handle it gracefully.
236 virtual void Start(StartMode start_mode
) = 0;
238 // Stops using the GCM service. This will not erase the persisted data.
239 virtual void Stop() = 0;
241 // Registers with the server to access the provided service.
242 // Delegate::OnRegisterFinished will be called asynchronously upon completion.
243 // |registration_info|: the specific information required for the
244 // registration. For GCM, it will contain app id and
245 // sender IDs. For InstanceID, it will contain app_id,
246 // authorized entity and scope.
247 virtual void Register(
248 const linked_ptr
<RegistrationInfo
>& registration_info
) = 0;
250 // Unregisters from the server to stop accessing the provided service.
251 // Delegate::OnUnregisterFinished will be called asynchronously upon
253 // |registration_info|: the specific information required for the
254 // registration. For GCM, it will contain app id (sender
255 // IDs can be ingored). For InstanceID, it will contain
256 // app id, authorized entity and scope.
257 virtual void Unregister(
258 const linked_ptr
<RegistrationInfo
>& registration_info
) = 0;
260 // Sends a message to a given receiver. Delegate::OnSendFinished will be
261 // called asynchronously upon completion.
262 // |app_id|: application ID.
263 // |receiver_id|: registration ID of the receiver party.
264 // |message|: message to be sent.
265 virtual void Send(const std::string
& app_id
,
266 const std::string
& receiver_id
,
267 const OutgoingMessage
& message
) = 0;
269 // Enables or disables internal activity recording.
270 virtual void SetRecording(bool recording
) = 0;
272 // Clear all recorded GCM activity logs.
273 virtual void ClearActivityLogs() = 0;
275 // Gets internal states and statistics.
276 virtual GCMStatistics
GetStatistics() const = 0;
278 // Sets a list of accounts with OAuth2 tokens for the next checkin.
279 // |account_tokens|: list of email addresses, account IDs and OAuth2 access
281 virtual void SetAccountTokens(
282 const std::vector
<AccountTokenInfo
>& account_tokens
) = 0;
284 // Persists the |account_mapping| in the store.
285 virtual void UpdateAccountMapping(const AccountMapping
& account_mapping
) = 0;
287 // Removes the account mapping related to |account_id| from the persistent
289 virtual void RemoveAccountMapping(const std::string
& account_id
) = 0;
291 // Sets last token fetch time in persistent store.
292 virtual void SetLastTokenFetchTime(const base::Time
& time
) = 0;
294 // Updates the timer used by the HeartbeatManager for sending heartbeats.
295 virtual void UpdateHeartbeatTimer(scoped_ptr
<base::Timer
> timer
) = 0;
297 // Adds the Instance ID data for a specific app to the persistent store.
298 virtual void AddInstanceIDData(const std::string
& app_id
,
299 const std::string
& instance_id
,
300 const std::string
& extra_data
) = 0;
302 // Removes the Instance ID data for a specific app from the persistent store.
303 virtual void RemoveInstanceIDData(const std::string
& app_id
) = 0;
305 // Retrieves the Instance ID data for a specific app from the persistent
307 virtual void GetInstanceIDData(const std::string
& app_id
,
308 std::string
* instance_id
,
309 std::string
* extra_data
) = 0;
311 // Gets and sets custom heartbeat interval for the MCS connection.
312 // |scope| is used to identify the component that requests a custom interval
313 // to be set, and allows that component to later revoke the setting. It should
315 virtual void AddHeartbeatInterval(const std::string
& scope
,
316 int interval_ms
) = 0;
317 virtual void RemoveHeartbeatInterval(const std::string
& scope
) = 0;
322 #endif // COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_