Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / gcm_driver / gcm_client.h
blob0aa9a1a8f7520a73d885476f56a81e937ee6e3a0
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_
8 #include <map>
9 #include <string>
10 #include <vector>
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;
21 class GURL;
23 namespace base {
24 class FilePath;
25 class SequencedTaskRunner;
26 class Timer;
29 namespace net {
30 class IPEndPoint;
31 class URLRequestContextGetter;
34 namespace gcm {
36 class Encryptor;
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.
41 class GCMClient {
42 public:
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.
46 enum StartMode {
47 // GCM should be started only when it is being actually used. If no
48 // registration record is found, GCM will not kick off.
49 DELAYED_START,
50 // GCM should be started immediately.
51 IMMEDIATE_START
54 enum Result {
55 // Successful operation.
56 SUCCESS,
57 // Invalid parameter.
58 INVALID_PARAMETER,
59 // GCM is disabled.
60 GCM_DISABLED,
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.
65 NETWORK_ERROR,
66 // Problem at the server.
67 SERVER_ERROR,
68 // Exceeded the specified TTL during message sending.
69 TTL_EXCEEDED,
70 // Other errors.
71 UNKNOWN_ERROR
74 enum ChromePlatform {
75 PLATFORM_WIN,
76 PLATFORM_MAC,
77 PLATFORM_LINUX,
78 PLATFORM_CROS,
79 PLATFORM_IOS,
80 PLATFORM_ANDROID,
81 PLATFORM_UNKNOWN
84 enum ChromeChannel {
85 CHANNEL_STABLE,
86 CHANNEL_BETA,
87 CHANNEL_DEV,
88 CHANNEL_CANARY,
89 CHANNEL_UNKNOWN
92 struct ChromeBuildInfo {
93 ChromeBuildInfo();
94 ~ChromeBuildInfo();
96 ChromePlatform platform;
97 ChromeChannel channel;
98 std::string version;
101 // Detailed information of the Send Error event.
102 struct SendErrorDetails {
103 SendErrorDetails();
104 ~SendErrorDetails();
106 std::string message_id;
107 MessageData additional_data;
108 Result result;
111 // Internal states and activity statistics of a GCM client.
112 struct GCMStatistics {
113 public:
114 GCMStatistics();
115 ~GCMStatistics();
117 bool is_recording;
118 bool gcm_client_created;
119 std::string gcm_client_state;
120 bool connection_client_created;
121 std::string connection_state;
122 uint64 android_id;
123 std::vector<std::string> registered_app_ids;
124 int send_queue_size;
125 int resend_queue_size;
127 RecordedActivities recorded_activities;
130 // Information about account.
131 struct AccountTokenInfo {
132 std::string account_id;
133 std::string email;
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.
139 class Delegate {
140 public:
141 // Called when the registration completed successfully or an error occurs.
142 // |registration_info|: the specific information required for the
143 // registration.
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,
149 Result result) = 0;
151 // Called when the unregistration completed.
152 // |registration_info|: the specific information required for the
153 // registration.
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
160 // occurs.
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,
166 Result result) = 0;
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
195 // client.
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
201 // been recorded.
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;
212 GCMClient();
213 virtual ~GCMClient();
215 // Begins initialization of the GCM Client. This will not trigger a
216 // connection.
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
252 // completion.
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
280 // tokens.
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
288 // store.
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
306 // store.
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
314 // be unique.
315 virtual void AddHeartbeatInterval(const std::string& scope,
316 int interval_ms) = 0;
317 virtual void RemoveHeartbeatInterval(const std::string& scope) = 0;
320 } // namespace gcm
322 #endif // COMPONENTS_GCM_DRIVER_GCM_CLIENT_H_