blob: e2f1b02f323514bd51356e8fff04d82a28da6304 [file] [log] [blame]
[email protected]e4097c82013-11-08 00:16:121// Copyright 2013 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.
4
5#ifndef GOOGLE_APIS_GCM_GCM_CLIENT_IMPL_H_
6#define GOOGLE_APIS_GCM_GCM_CLIENT_IMPL_H_
7
[email protected]b83122a92014-01-22 21:29:298#include <map>
9#include <string>
[email protected]848b1b62014-01-30 23:51:0410#include <vector>
[email protected]b83122a92014-01-22 21:29:2911
[email protected]e4097c82013-11-08 00:16:1212#include "base/compiler_specific.h"
[email protected]79994f42014-01-16 16:05:3613#include "base/memory/ref_counted.h"
[email protected]955e0ff2014-01-31 20:42:1214#include "base/memory/weak_ptr.h"
[email protected]b83122a92014-01-22 21:29:2915#include "base/stl_util.h"
[email protected]b83122a92014-01-22 21:29:2916#include "google_apis/gcm/base/mcs_message.h"
17#include "google_apis/gcm/engine/gcm_store.h"
[email protected]06e45272014-05-06 03:41:3418#include "google_apis/gcm/engine/gservices_settings.h"
[email protected]b83122a92014-01-22 21:29:2919#include "google_apis/gcm/engine/mcs_client.h"
[email protected]b4dd0232014-02-08 02:37:3120#include "google_apis/gcm/engine/registration_request.h"
[email protected]0e88e1d12014-03-19 06:53:0821#include "google_apis/gcm/engine/unregistration_request.h"
[email protected]e4097c82013-11-08 00:16:1222#include "google_apis/gcm/gcm_client.h"
[email protected]436bcb82014-04-18 00:40:5723#include "google_apis/gcm/monitoring/gcm_stats_recorder.h"
24#include "google_apis/gcm/protocol/android_checkin.pb.h"
[email protected]2c4d4cd2014-04-10 21:10:2225#include "google_apis/gcm/protocol/checkin.pb.h"
[email protected]b83122a92014-01-22 21:29:2926#include "net/base/net_log.h"
27#include "net/url_request/url_request_context_getter.h"
[email protected]e4097c82013-11-08 00:16:1228
[email protected]2bbe0a682014-03-26 00:08:3129class GURL;
30
[email protected]79994f42014-01-16 16:05:3631namespace base {
[email protected]955e0ff2014-01-31 20:42:1232class Clock;
[email protected]764c0442014-05-01 04:30:5533class Time;
[email protected]79994f42014-01-16 16:05:3634} // namespace base
35
[email protected]436bcb82014-04-18 00:40:5736namespace mcs_proto {
37class DataMessageStanza;
38} // namespace mcs_proto
39
[email protected]b83122a92014-01-22 21:29:2940namespace net {
41class HttpNetworkSession;
[email protected]955e0ff2014-01-31 20:42:1242} // namespace net
[email protected]b83122a92014-01-22 21:29:2943
[email protected]e4097c82013-11-08 00:16:1244namespace gcm {
45
[email protected]b83122a92014-01-22 21:29:2946class CheckinRequest;
47class ConnectionFactory;
48class GCMClientImplTest;
[email protected]79994f42014-01-16 16:05:3649
[email protected]2bbe0a682014-03-26 00:08:3150// Helper class for building GCM internals. Allows tests to inject fake versions
51// as necessary.
52class GCM_EXPORT GCMInternalsBuilder {
53 public:
54 GCMInternalsBuilder();
55 virtual ~GCMInternalsBuilder();
56
57 virtual scoped_ptr<base::Clock> BuildClock();
58 virtual scoped_ptr<MCSClient> BuildMCSClient(
59 const std::string& version,
60 base::Clock* clock,
61 ConnectionFactory* connection_factory,
[email protected]436bcb82014-04-18 00:40:5762 GCMStore* gcm_store,
63 GCMStatsRecorder* recorder);
[email protected]2bbe0a682014-03-26 00:08:3164 virtual scoped_ptr<ConnectionFactory> BuildConnectionFactory(
65 const std::vector<GURL>& endpoints,
66 const net::BackoffEntry::Policy& backoff_policy,
67 scoped_refptr<net::HttpNetworkSession> network_session,
[email protected]9df5b932014-04-30 00:39:0668 net::NetLog* net_log,
69 GCMStatsRecorder* recorder);
[email protected]2bbe0a682014-03-26 00:08:3170};
71
[email protected]b83122a92014-01-22 21:29:2972// Implements the GCM Client. It is used to coordinate MCS Client (communication
73// with MCS) and other pieces of GCM infrastructure like Registration and
74// Checkins. It also allows for registering user delegates that host
75// applications that send and receive messages.
[email protected]7de78802014-05-10 19:49:4076class GCM_EXPORT GCMClientImpl
77 : public GCMClient, public GCMStatsRecorder::Delegate {
[email protected]e4097c82013-11-08 00:16:1278 public:
[email protected]2bbe0a682014-03-26 00:08:3179 explicit GCMClientImpl(scoped_ptr<GCMInternalsBuilder> internals_builder);
[email protected]e4097c82013-11-08 00:16:1280 virtual ~GCMClientImpl();
81
82 // Overridden from GCMClient:
[email protected]e2a4a8012014-02-07 22:32:5283 virtual void Initialize(
[email protected]8ad80512014-05-23 09:40:4784 const ChromeBuildInfo& chrome_build_info,
[email protected]e2a4a8012014-02-07 22:32:5285 const base::FilePath& store_path,
[email protected]495a7db92014-02-22 07:49:5986 const std::vector<std::string>& account_ids,
[email protected]e2a4a8012014-02-07 22:32:5287 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
88 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:3989 url_request_context_getter,
[email protected]446f73c22014-05-14 20:47:1890 scoped_ptr<Encryptor> encryptor,
[email protected]7de78802014-05-10 19:49:4091 GCMClient::Delegate* delegate) OVERRIDE;
[email protected]fc767e42014-05-13 05:02:1492 virtual void Start() OVERRIDE;
[email protected]21fee5482014-03-05 00:57:1593 virtual void Stop() OVERRIDE;
[email protected]5799d052014-02-12 20:47:3994 virtual void CheckOut() OVERRIDE;
95 virtual void Register(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:1296 const std::vector<std::string>& sender_ids) OVERRIDE;
[email protected]5799d052014-02-12 20:47:3997 virtual void Unregister(const std::string& app_id) OVERRIDE;
98 virtual void Send(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:1299 const std::string& receiver_id,
100 const OutgoingMessage& message) OVERRIDE;
[email protected]436bcb82014-04-18 00:40:57101 virtual void SetRecording(bool recording) OVERRIDE;
102 virtual void ClearActivityLogs() OVERRIDE;
[email protected]35601812014-03-07 19:52:43103 virtual GCMStatistics GetStatistics() const OVERRIDE;
[email protected]7de78802014-05-10 19:49:40104 virtual void OnActivityRecorded() OVERRIDE;
[email protected]e4097c82013-11-08 00:16:12105
106 private:
[email protected]b83122a92014-01-22 21:29:29107 // State representation of the GCMClient.
[email protected]35601812014-03-07 19:52:43108 // Any change made to this enum should have corresponding change in the
109 // GetStateString(...) function.
[email protected]b83122a92014-01-22 21:29:29110 enum State {
111 // Uninitialized.
112 UNINITIALIZED,
[email protected]d3a4b2e2014-02-27 13:46:54113 // Initialized,
114 INITIALIZED,
[email protected]b83122a92014-01-22 21:29:29115 // GCM store loading is in progress.
116 LOADING,
117 // Initial device checkin is in progress.
118 INITIAL_DEVICE_CHECKIN,
119 // Ready to accept requests.
120 READY,
121 };
122
[email protected]5799d052014-02-12 20:47:39123 // The check-in info for the user. Returned by the server.
124 struct GCM_EXPORT CheckinInfo {
125 CheckinInfo() : android_id(0), secret(0) {}
126 bool IsValid() const { return android_id != 0 && secret != 0; }
127 void Reset() {
128 android_id = 0;
129 secret = 0;
130 }
[email protected]b83122a92014-01-22 21:29:29131
[email protected]5799d052014-02-12 20:47:39132 uint64 android_id;
133 uint64 secret;
[email protected]848b1b62014-01-30 23:51:04134 };
135
[email protected]e4007042014-02-15 20:34:28136 // Collection of pending registration requests. Keys are app IDs, while values
[email protected]5799d052014-02-12 20:47:39137 // are pending registration requests to obtain a registration ID for
138 // requesting application.
139 typedef std::map<std::string, RegistrationRequest*>
[email protected]3a20a4d2014-03-21 22:54:21140 PendingRegistrationRequests;
[email protected]848b1b62014-01-30 23:51:04141
[email protected]e4007042014-02-15 20:34:28142 // Collection of pending unregistration requests. Keys are app IDs, while
143 // values are pending unregistration requests to disable the registration ID
144 // currently assigned to the application.
145 typedef std::map<std::string, UnregistrationRequest*>
[email protected]3a20a4d2014-03-21 22:54:21146 PendingUnregistrationRequests;
[email protected]e4007042014-02-15 20:34:28147
[email protected]b83122a92014-01-22 21:29:29148 friend class GCMClientImplTest;
149
[email protected]35601812014-03-07 19:52:43150 // Returns text representation of the enum State.
151 std::string GetStateString() const;
152
[email protected]b83122a92014-01-22 21:29:29153 // Callbacks for the MCSClient.
154 // Receives messages and dispatches them to relevant user delegates.
155 void OnMessageReceivedFromMCS(const gcm::MCSMessage& message);
156 // Receives confirmation of sent messages or information about errors.
157 void OnMessageSentToMCS(int64 user_serial_number,
158 const std::string& app_id,
159 const std::string& message_id,
160 MCSClient::MessageSendStatus status);
161 // Receives information about mcs_client_ errors.
162 void OnMCSError();
163
164 // Runs after GCM Store load is done to trigger continuation of the
165 // initialization.
[email protected]2809af72014-01-25 09:23:57166 void OnLoadCompleted(scoped_ptr<GCMStore::LoadResult> result);
[email protected]b83122a92014-01-22 21:29:29167 // Initializes mcs_client_, which handles the connection to MCS.
[email protected]2809af72014-01-25 09:23:57168 void InitializeMCSClient(scoped_ptr<GCMStore::LoadResult> result);
[email protected]b83122a92014-01-22 21:29:29169 // Complets the first time device checkin.
170 void OnFirstTimeDeviceCheckinCompleted(const CheckinInfo& checkin_info);
171 // Starts a login on mcs_client_.
172 void StartMCSLogin();
173 // Resets state to before initialization.
174 void ResetState();
[email protected]86625df2014-01-31 03:47:58175 // Sets state to ready. This will initiate the MCS login and notify the
176 // delegates.
177 void OnReady();
[email protected]b83122a92014-01-22 21:29:29178
[email protected]5799d052014-02-12 20:47:39179 // Starts a first time device checkin.
[email protected]25b5f50e2014-04-03 08:27:23180 void StartCheckin();
[email protected]2c4d4cd2014-04-10 21:10:22181 // Completes the device checkin request by parsing the |checkin_response|.
182 // Function also cleans up the pending checkin.
183 void OnCheckinCompleted(
184 const checkin_proto::AndroidCheckinResponse& checkin_response);
[email protected]06e45272014-05-06 03:41:34185
186 // Callback passed to GCMStore::SetGServicesSettings.
187 void SetGServicesSettingsCallback(bool success);
188
[email protected]764c0442014-05-01 04:30:55189 // Schedules next periodic device checkin and makes sure there is at most one
190 // pending checkin at a time. This function is meant to be called after a
191 // successful checkin.
192 void SchedulePeriodicCheckin();
193 // Gets the time until next checkin.
194 base::TimeDelta GetTimeToNextCheckin() const;
[email protected]25b5f50e2014-04-03 08:27:23195 // Callback for setting last checkin time in the |gcm_store_|.
196 void SetLastCheckinTimeCallback(bool success);
[email protected]b83122a92014-01-22 21:29:29197
198 // Callback for persisting device credentials in the |gcm_store_|.
199 void SetDeviceCredentialsCallback(bool success);
200
[email protected]3a20a4d2014-03-21 22:54:21201 // Callback for persisting registration info in the |gcm_store_|.
202 void UpdateRegistrationCallback(bool success);
203
[email protected]848b1b62014-01-30 23:51:04204 // Completes the registration request.
[email protected]5799d052014-02-12 20:47:39205 void OnRegisterCompleted(const std::string& app_id,
[email protected]3a20a4d2014-03-21 22:54:21206 const std::vector<std::string>& sender_ids,
[email protected]b4dd0232014-02-08 02:37:31207 RegistrationRequest::Status status,
[email protected]848b1b62014-01-30 23:51:04208 const std::string& registration_id);
209
[email protected]e4007042014-02-15 20:34:28210 // Completes the unregistration request.
[email protected]0e88e1d12014-03-19 06:53:08211 void OnUnregisterCompleted(const std::string& app_id,
212 UnregistrationRequest::Status status);
[email protected]e4007042014-02-15 20:34:28213
[email protected]d3a4b2e2014-02-27 13:46:54214 // Completes the GCM store destroy request.
215 void OnGCMStoreDestroyed(bool success);
216
[email protected]c6fe36b2014-03-11 10:58:12217 // Handles incoming data message and dispatches it the delegate of this class.
[email protected]b83122a92014-01-22 21:29:29218 void HandleIncomingMessage(const gcm::MCSMessage& message);
219
[email protected]c6fe36b2014-03-11 10:58:12220 // Fires OnMessageReceived event on the delegate of this class, based on the
221 // details in |data_message_stanza| and |message_data|.
222 void HandleIncomingDataMessage(
223 const mcs_proto::DataMessageStanza& data_message_stanza,
224 MessageData& message_data);
225
226 // Fires OnMessageSendError event on the delegate of this calss, based on the
227 // details in |data_message_stanza| and |message_data|.
228 void HandleIncomingSendError(
229 const mcs_proto::DataMessageStanza& data_message_stanza,
230 MessageData& message_data);
[email protected]848b1b62014-01-30 23:51:04231
[email protected]2bbe0a682014-03-26 00:08:31232 // Builder for the GCM internals (mcs client, etc.).
233 scoped_ptr<GCMInternalsBuilder> internals_builder_;
[email protected]b83122a92014-01-22 21:29:29234
[email protected]436bcb82014-04-18 00:40:57235 // Recorder that logs GCM activities.
236 GCMStatsRecorder recorder_;
237
[email protected]b83122a92014-01-22 21:29:29238 // State of the GCM Client Implementation.
239 State state_;
240
[email protected]7de78802014-05-10 19:49:40241 GCMClient::Delegate* delegate_;
[email protected]5799d052014-02-12 20:47:39242
[email protected]b83122a92014-01-22 21:29:29243 // Device checkin info (android ID and security token used by device).
244 CheckinInfo device_checkin_info_;
245
[email protected]848b1b62014-01-30 23:51:04246 // Clock used for timing of retry logic. Passed in for testing. Owned by
247 // GCMClientImpl.
248 scoped_ptr<base::Clock> clock_;
[email protected]b83122a92014-01-22 21:29:29249
250 // Information about the chrome build.
251 // TODO(fgorski): Check if it can be passed in constructor and made const.
[email protected]8ad80512014-05-23 09:40:47252 ChromeBuildInfo chrome_build_info_;
[email protected]b83122a92014-01-22 21:29:29253
254 // Persistent data store for keeping device credentials, messages and user to
255 // serial number mappings.
[email protected]79994f42014-01-16 16:05:36256 scoped_ptr<GCMStore> gcm_store_;
[email protected]b83122a92014-01-22 21:29:29257
[email protected]b83122a92014-01-22 21:29:29258 scoped_refptr<net::HttpNetworkSession> network_session_;
259 net::BoundNetLog net_log_;
260 scoped_ptr<ConnectionFactory> connection_factory_;
261 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
262
263 // Controls receiving and sending of packets and reliable message queueing.
264 scoped_ptr<MCSClient> mcs_client_;
265
[email protected]5799d052014-02-12 20:47:39266 scoped_ptr<CheckinRequest> checkin_request_;
[email protected]495a7db92014-02-22 07:49:59267 std::vector<std::string> account_ids_;
[email protected]b83122a92014-01-22 21:29:29268
[email protected]3a20a4d2014-03-21 22:54:21269 // Cached registration info.
270 RegistrationInfoMap registrations_;
[email protected]848b1b62014-01-30 23:51:04271
[email protected]3a20a4d2014-03-21 22:54:21272 // Currently pending registration requests. GCMClientImpl owns the
273 // RegistrationRequests.
274 PendingRegistrationRequests pending_registration_requests_;
275 STLValueDeleter<PendingRegistrationRequests>
276 pending_registration_requests_deleter_;
277
278 // Currently pending unregistration requests. GCMClientImpl owns the
[email protected]e4007042014-02-15 20:34:28279 // UnregistrationRequests.
[email protected]3a20a4d2014-03-21 22:54:21280 PendingUnregistrationRequests pending_unregistration_requests_;
281 STLValueDeleter<PendingUnregistrationRequests>
282 pending_unregistration_requests_deleter_;
[email protected]e4007042014-02-15 20:34:28283
[email protected]764c0442014-05-01 04:30:55284 // G-services settings that were provided by MCS.
[email protected]06e45272014-05-06 03:41:34285 GServicesSettings gservices_settings_;
[email protected]764c0442014-05-01 04:30:55286
287 // Time of the last successful checkin.
288 base::Time last_checkin_time_;
289
290 // Factory for creating references when scheduling periodic checkin.
291 base::WeakPtrFactory<GCMClientImpl> periodic_checkin_ptr_factory_;
292
[email protected]955e0ff2014-01-31 20:42:12293 // Factory for creating references in callbacks.
294 base::WeakPtrFactory<GCMClientImpl> weak_ptr_factory_;
295
[email protected]e4097c82013-11-08 00:16:12296 DISALLOW_COPY_AND_ASSIGN(GCMClientImpl);
297};
298
299} // namespace gcm
300
301#endif // GOOGLE_APIS_GCM_GCM_CLIENT_IMPL_H_