blob: e9f2f141ad04f7c28e3f926a64153ae849b39e94 [file] [log] [blame]
[email protected]cd57f372014-06-09 17:13:061// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]e4097c82013-11-08 00:16:122// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]cd57f372014-06-09 17:13:065#ifndef COMPONENTS_GCM_DRIVER_GCM_CLIENT_IMPL_H_
6#define COMPONENTS_GCM_DRIVER_GCM_CLIENT_IMPL_H_
[email protected]e4097c82013-11-08 00:16:127
[email protected]b83122a92014-01-22 21:29:298#include <map>
[email protected]7df5ef22014-07-17 07:35:589#include <set>
[email protected]b83122a92014-01-22 21:29:2910#include <string>
[email protected]848b1b62014-01-30 23:51:0411#include <vector>
[email protected]b83122a92014-01-22 21:29:2912
[email protected]e4097c82013-11-08 00:16:1213#include "base/compiler_specific.h"
[email protected]79994f42014-01-16 16:05:3614#include "base/memory/ref_counted.h"
[email protected]955e0ff2014-01-31 20:42:1215#include "base/memory/weak_ptr.h"
[email protected]b83122a92014-01-22 21:29:2916#include "base/stl_util.h"
[email protected]cd57f372014-06-09 17:13:0617#include "components/gcm_driver/gcm_client.h"
18#include "components/gcm_driver/gcm_stats_recorder_impl.h"
[email protected]b83122a92014-01-22 21:29:2919#include "google_apis/gcm/base/mcs_message.h"
20#include "google_apis/gcm/engine/gcm_store.h"
[email protected]06e45272014-05-06 03:41:3421#include "google_apis/gcm/engine/gservices_settings.h"
[email protected]b83122a92014-01-22 21:29:2922#include "google_apis/gcm/engine/mcs_client.h"
[email protected]b4dd0232014-02-08 02:37:3123#include "google_apis/gcm/engine/registration_request.h"
[email protected]0e88e1d12014-03-19 06:53:0824#include "google_apis/gcm/engine/unregistration_request.h"
[email protected]436bcb82014-04-18 00:40:5725#include "google_apis/gcm/protocol/android_checkin.pb.h"
[email protected]2c4d4cd2014-04-10 21:10:2226#include "google_apis/gcm/protocol/checkin.pb.h"
eroman58e913a2015-04-02 17:23:3127#include "net/log/net_log.h"
[email protected]b83122a92014-01-22 21:29:2928#include "net/url_request/url_request_context_getter.h"
[email protected]e4097c82013-11-08 00:16:1229
[email protected]2bbe0a682014-03-26 00:08:3130class GURL;
31
[email protected]79994f42014-01-16 16:05:3632namespace base {
[email protected]955e0ff2014-01-31 20:42:1233class Clock;
[email protected]764c0442014-05-01 04:30:5534class Time;
[email protected]79994f42014-01-16 16:05:3635} // namespace base
36
[email protected]436bcb82014-04-18 00:40:5737namespace mcs_proto {
38class DataMessageStanza;
39} // namespace mcs_proto
40
[email protected]b83122a92014-01-22 21:29:2941namespace net {
42class HttpNetworkSession;
[email protected]955e0ff2014-01-31 20:42:1243} // namespace net
[email protected]b83122a92014-01-22 21:29:2944
[email protected]e4097c82013-11-08 00:16:1245namespace gcm {
46
[email protected]b83122a92014-01-22 21:29:2947class CheckinRequest;
48class ConnectionFactory;
49class GCMClientImplTest;
[email protected]79994f42014-01-16 16:05:3650
[email protected]2bbe0a682014-03-26 00:08:3151// Helper class for building GCM internals. Allows tests to inject fake versions
52// as necessary.
[email protected]cd57f372014-06-09 17:13:0653class GCMInternalsBuilder {
[email protected]2bbe0a682014-03-26 00:08:3154 public:
55 GCMInternalsBuilder();
56 virtual ~GCMInternalsBuilder();
57
58 virtual scoped_ptr<base::Clock> BuildClock();
59 virtual scoped_ptr<MCSClient> BuildMCSClient(
60 const std::string& version,
61 base::Clock* clock,
62 ConnectionFactory* connection_factory,
[email protected]436bcb82014-04-18 00:40:5763 GCMStore* gcm_store,
64 GCMStatsRecorder* recorder);
[email protected]2bbe0a682014-03-26 00:08:3165 virtual scoped_ptr<ConnectionFactory> BuildConnectionFactory(
66 const std::vector<GURL>& endpoints,
67 const net::BackoffEntry::Policy& backoff_policy,
[email protected]6f63606a2014-07-18 02:18:5568 const scoped_refptr<net::HttpNetworkSession>& gcm_network_session,
69 const scoped_refptr<net::HttpNetworkSession>& http_network_session,
[email protected]9df5b932014-04-30 00:39:0670 net::NetLog* net_log,
71 GCMStatsRecorder* recorder);
[email protected]2bbe0a682014-03-26 00:08:3172};
73
[email protected]b83122a92014-01-22 21:29:2974// Implements the GCM Client. It is used to coordinate MCS Client (communication
75// with MCS) and other pieces of GCM infrastructure like Registration and
76// Checkins. It also allows for registering user delegates that host
77// applications that send and receive messages.
[email protected]cd57f372014-06-09 17:13:0678class GCMClientImpl
[email protected]c31e1b52014-06-12 21:00:4779 : public GCMClient, public GCMStatsRecorder::Delegate,
80 public ConnectionFactory::ConnectionListener {
[email protected]e4097c82013-11-08 00:16:1281 public:
jianlif3e52af42015-01-21 23:18:4782 // State representation of the GCMClient.
83 // Any change made to this enum should have corresponding change in the
84 // GetStateString(...) function.
85 enum State {
86 // Uninitialized.
87 UNINITIALIZED,
88 // Initialized,
89 INITIALIZED,
90 // GCM store loading is in progress.
91 LOADING,
92 // GCM store is loaded.
93 LOADED,
94 // Initial device checkin is in progress.
95 INITIAL_DEVICE_CHECKIN,
96 // Ready to accept requests.
97 READY,
98 };
99
[email protected]2bbe0a682014-03-26 00:08:31100 explicit GCMClientImpl(scoped_ptr<GCMInternalsBuilder> internals_builder);
dcheng00ea022b2014-10-21 11:24:56101 ~GCMClientImpl() override;
[email protected]e4097c82013-11-08 00:16:12102
[email protected]c31e1b52014-06-12 21:00:47103 // GCMClient implementation.
dcheng00ea022b2014-10-21 11:24:56104 void Initialize(
[email protected]8ad80512014-05-23 09:40:47105 const ChromeBuildInfo& chrome_build_info,
[email protected]e2a4a8012014-02-07 22:32:52106 const base::FilePath& store_path,
107 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
108 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:39109 url_request_context_getter,
[email protected]446f73c22014-05-14 20:47:18110 scoped_ptr<Encryptor> encryptor,
mostynbfe59f482014-10-06 15:04:46111 GCMClient::Delegate* delegate) override;
jianlif3e52af42015-01-21 23:18:47112 void Start(StartMode start_mode) override;
dcheng00ea022b2014-10-21 11:24:56113 void Stop() override;
dcheng00ea022b2014-10-21 11:24:56114 void Register(const std::string& app_id,
115 const std::vector<std::string>& sender_ids) override;
116 void Unregister(const std::string& app_id) override;
117 void Send(const std::string& app_id,
118 const std::string& receiver_id,
119 const OutgoingMessage& message) override;
120 void SetRecording(bool recording) override;
121 void ClearActivityLogs() override;
122 GCMStatistics GetStatistics() const override;
123 void SetAccountTokens(
mostynbfe59f482014-10-06 15:04:46124 const std::vector<AccountTokenInfo>& account_tokens) override;
dcheng00ea022b2014-10-21 11:24:56125 void UpdateAccountMapping(const AccountMapping& account_mapping) override;
126 void RemoveAccountMapping(const std::string& account_id) override;
fgorski5df101702014-10-28 02:09:31127 void SetLastTokenFetchTime(const base::Time& time) override;
chirantan192a9212014-12-06 03:30:45128 void UpdateHeartbeatTimer(scoped_ptr<base::Timer> timer) override;
jianli10018b2d2015-05-11 21:14:13129 void AddInstanceIDData(const std::string& app_id,
130 const std::string& instance_id_data) override;
131 void RemoveInstanceIDData(const std::string& app_id) override;
132 std::string GetInstanceIDData(const std::string& app_id) override;
fgorski22754462015-05-14 00:05:22133 void AddHeartbeatInterval(const std::string& scope, int interval_ms) override;
134 void RemoveHeartbeatInterval(const std::string& scope) override;
[email protected]c31e1b52014-06-12 21:00:47135
136 // GCMStatsRecorder::Delegate implemenation.
dcheng00ea022b2014-10-21 11:24:56137 void OnActivityRecorded() override;
[email protected]e4097c82013-11-08 00:16:12138
[email protected]c31e1b52014-06-12 21:00:47139 // ConnectionFactory::ConnectionListener implementation.
dcheng00ea022b2014-10-21 11:24:56140 void OnConnected(const GURL& current_server,
141 const net::IPEndPoint& ip_endpoint) override;
142 void OnDisconnected() override;
[email protected]c31e1b52014-06-12 21:00:47143
[email protected]e4097c82013-11-08 00:16:12144 private:
[email protected]7df5ef22014-07-17 07:35:58145 // The check-in info for the device.
146 // TODO(fgorski): Convert to a class with explicit getters/setters.
[email protected]cd57f372014-06-09 17:13:06147 struct CheckinInfo {
[email protected]7df5ef22014-07-17 07:35:58148 CheckinInfo();
149 ~CheckinInfo();
[email protected]5799d052014-02-12 20:47:39150 bool IsValid() const { return android_id != 0 && secret != 0; }
[email protected]7df5ef22014-07-17 07:35:58151 void SnapshotCheckinAccounts();
152 void Reset();
[email protected]b83122a92014-01-22 21:29:29153
[email protected]7df5ef22014-07-17 07:35:58154 // Android ID of the device as assigned by the server.
[email protected]5799d052014-02-12 20:47:39155 uint64 android_id;
[email protected]7df5ef22014-07-17 07:35:58156 // Security token of the device as assigned by the server.
[email protected]5799d052014-02-12 20:47:39157 uint64 secret;
[email protected]7df5ef22014-07-17 07:35:58158 // True if accounts were already provided through SetAccountsForCheckin(),
159 // or when |last_checkin_accounts| was loaded as empty.
160 bool accounts_set;
161 // Map of account email addresses and OAuth2 tokens that will be sent to the
162 // checkin server on a next checkin.
163 std::map<std::string, std::string> account_tokens;
164 // As set of accounts last checkin was completed with.
165 std::set<std::string> last_checkin_accounts;
[email protected]848b1b62014-01-30 23:51:04166 };
167
[email protected]e4007042014-02-15 20:34:28168 // Collection of pending registration requests. Keys are app IDs, while values
[email protected]5799d052014-02-12 20:47:39169 // are pending registration requests to obtain a registration ID for
170 // requesting application.
171 typedef std::map<std::string, RegistrationRequest*>
[email protected]3a20a4d2014-03-21 22:54:21172 PendingRegistrationRequests;
[email protected]848b1b62014-01-30 23:51:04173
[email protected]e4007042014-02-15 20:34:28174 // Collection of pending unregistration requests. Keys are app IDs, while
175 // values are pending unregistration requests to disable the registration ID
176 // currently assigned to the application.
177 typedef std::map<std::string, UnregistrationRequest*>
[email protected]3a20a4d2014-03-21 22:54:21178 PendingUnregistrationRequests;
[email protected]e4007042014-02-15 20:34:28179
[email protected]b83122a92014-01-22 21:29:29180 friend class GCMClientImplTest;
181
[email protected]35601812014-03-07 19:52:43182 // Returns text representation of the enum State.
183 std::string GetStateString() const;
184
[email protected]b83122a92014-01-22 21:29:29185 // Callbacks for the MCSClient.
186 // Receives messages and dispatches them to relevant user delegates.
187 void OnMessageReceivedFromMCS(const gcm::MCSMessage& message);
188 // Receives confirmation of sent messages or information about errors.
189 void OnMessageSentToMCS(int64 user_serial_number,
190 const std::string& app_id,
191 const std::string& message_id,
192 MCSClient::MessageSendStatus status);
193 // Receives information about mcs_client_ errors.
194 void OnMCSError();
195
196 // Runs after GCM Store load is done to trigger continuation of the
197 // initialization.
[email protected]2809af72014-01-25 09:23:57198 void OnLoadCompleted(scoped_ptr<GCMStore::LoadResult> result);
jianlif3e52af42015-01-21 23:18:47199 // Starts the GCM.
200 void StartGCM();
[email protected]b83122a92014-01-22 21:29:29201 // Initializes mcs_client_, which handles the connection to MCS.
jianlif3e52af42015-01-21 23:18:47202 void InitializeMCSClient();
[email protected]b83122a92014-01-22 21:29:29203 // Complets the first time device checkin.
204 void OnFirstTimeDeviceCheckinCompleted(const CheckinInfo& checkin_info);
205 // Starts a login on mcs_client_.
206 void StartMCSLogin();
jianli00b4600f2015-02-10 23:32:49207 // Resets the GCM store when it is corrupted.
208 void ResetStore();
[email protected]86625df2014-01-31 03:47:58209 // Sets state to ready. This will initiate the MCS login and notify the
210 // delegates.
fgorski5df101702014-10-28 02:09:31211 void OnReady(const std::vector<AccountMapping>& account_mappings,
212 const base::Time& last_token_fetch_time);
[email protected]b83122a92014-01-22 21:29:29213
[email protected]5799d052014-02-12 20:47:39214 // Starts a first time device checkin.
[email protected]25b5f50e2014-04-03 08:27:23215 void StartCheckin();
[email protected]2c4d4cd2014-04-10 21:10:22216 // Completes the device checkin request by parsing the |checkin_response|.
217 // Function also cleans up the pending checkin.
218 void OnCheckinCompleted(
219 const checkin_proto::AndroidCheckinResponse& checkin_response);
[email protected]06e45272014-05-06 03:41:34220
221 // Callback passed to GCMStore::SetGServicesSettings.
222 void SetGServicesSettingsCallback(bool success);
223
[email protected]764c0442014-05-01 04:30:55224 // Schedules next periodic device checkin and makes sure there is at most one
225 // pending checkin at a time. This function is meant to be called after a
226 // successful checkin.
227 void SchedulePeriodicCheckin();
228 // Gets the time until next checkin.
229 base::TimeDelta GetTimeToNextCheckin() const;
[email protected]7df5ef22014-07-17 07:35:58230 // Callback for setting last checkin information in the |gcm_store_|.
231 void SetLastCheckinInfoCallback(bool success);
[email protected]b83122a92014-01-22 21:29:29232
233 // Callback for persisting device credentials in the |gcm_store_|.
234 void SetDeviceCredentialsCallback(bool success);
235
[email protected]3a20a4d2014-03-21 22:54:21236 // Callback for persisting registration info in the |gcm_store_|.
237 void UpdateRegistrationCallback(bool success);
238
[email protected]72d4f252014-08-20 22:34:28239 // Callback for all store operations that do not try to recover, if write in
240 // |gcm_store_| fails.
241 void DefaultStoreCallback(bool success);
242
fgorski9a405102014-11-19 01:25:16243 // Callback for store operation where result does not matter.
244 void IgnoreWriteResultCallback(bool success);
245
jianli00b4600f2015-02-10 23:32:49246 // Callback for resetting the GCM store.
247 void ResetStoreCallback(bool success);
248
[email protected]848b1b62014-01-30 23:51:04249 // Completes the registration request.
[email protected]5799d052014-02-12 20:47:39250 void OnRegisterCompleted(const std::string& app_id,
[email protected]3a20a4d2014-03-21 22:54:21251 const std::vector<std::string>& sender_ids,
[email protected]b4dd0232014-02-08 02:37:31252 RegistrationRequest::Status status,
[email protected]848b1b62014-01-30 23:51:04253 const std::string& registration_id);
254
[email protected]e4007042014-02-15 20:34:28255 // Completes the unregistration request.
[email protected]0e88e1d12014-03-19 06:53:08256 void OnUnregisterCompleted(const std::string& app_id,
257 UnregistrationRequest::Status status);
[email protected]e4007042014-02-15 20:34:28258
[email protected]d3a4b2e2014-02-27 13:46:54259 // Completes the GCM store destroy request.
260 void OnGCMStoreDestroyed(bool success);
261
[email protected]c6fe36b2014-03-11 10:58:12262 // Handles incoming data message and dispatches it the delegate of this class.
[email protected]b83122a92014-01-22 21:29:29263 void HandleIncomingMessage(const gcm::MCSMessage& message);
264
[email protected]c6fe36b2014-03-11 10:58:12265 // Fires OnMessageReceived event on the delegate of this class, based on the
266 // details in |data_message_stanza| and |message_data|.
267 void HandleIncomingDataMessage(
268 const mcs_proto::DataMessageStanza& data_message_stanza,
269 MessageData& message_data);
270
271 // Fires OnMessageSendError event on the delegate of this calss, based on the
272 // details in |data_message_stanza| and |message_data|.
273 void HandleIncomingSendError(
274 const mcs_proto::DataMessageStanza& data_message_stanza,
275 MessageData& message_data);
[email protected]848b1b62014-01-30 23:51:04276
jianlif3e52af42015-01-21 23:18:47277 // Is there any standalone app being registered for GCM?
278 bool HasStandaloneRegisteredApp() const;
279
[email protected]2bbe0a682014-03-26 00:08:31280 // Builder for the GCM internals (mcs client, etc.).
281 scoped_ptr<GCMInternalsBuilder> internals_builder_;
[email protected]b83122a92014-01-22 21:29:29282
[email protected]436bcb82014-04-18 00:40:57283 // Recorder that logs GCM activities.
[email protected]025adfa2014-06-03 21:51:12284 GCMStatsRecorderImpl recorder_;
[email protected]436bcb82014-04-18 00:40:57285
[email protected]b83122a92014-01-22 21:29:29286 // State of the GCM Client Implementation.
287 State state_;
288
[email protected]7de78802014-05-10 19:49:40289 GCMClient::Delegate* delegate_;
[email protected]5799d052014-02-12 20:47:39290
jianlif3e52af42015-01-21 23:18:47291 // Flag to indicate if the GCM should be delay started until it is actually
292 // used in either of the following cases:
293 // 1) The GCM store contains the registration records.
294 // 2) GCM functionailities are explicitly called.
295 StartMode start_mode_;
296
[email protected]b83122a92014-01-22 21:29:29297 // Device checkin info (android ID and security token used by device).
298 CheckinInfo device_checkin_info_;
299
[email protected]848b1b62014-01-30 23:51:04300 // Clock used for timing of retry logic. Passed in for testing. Owned by
301 // GCMClientImpl.
302 scoped_ptr<base::Clock> clock_;
[email protected]b83122a92014-01-22 21:29:29303
304 // Information about the chrome build.
305 // TODO(fgorski): Check if it can be passed in constructor and made const.
[email protected]8ad80512014-05-23 09:40:47306 ChromeBuildInfo chrome_build_info_;
[email protected]b83122a92014-01-22 21:29:29307
308 // Persistent data store for keeping device credentials, messages and user to
309 // serial number mappings.
[email protected]79994f42014-01-16 16:05:36310 scoped_ptr<GCMStore> gcm_store_;
[email protected]b83122a92014-01-22 21:29:29311
jianlif3e52af42015-01-21 23:18:47312 // Data loaded from the GCM store.
313 scoped_ptr<GCMStore::LoadResult> load_result_;
314
jianli00b4600f2015-02-10 23:32:49315 // Tracks if the GCM store has been reset. This is used to prevent from
316 // resetting and loading from the store again and again.
317 bool gcm_store_reset_;
318
[email protected]b83122a92014-01-22 21:29:29319 scoped_refptr<net::HttpNetworkSession> network_session_;
320 net::BoundNetLog net_log_;
321 scoped_ptr<ConnectionFactory> connection_factory_;
322 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
323
324 // Controls receiving and sending of packets and reliable message queueing.
325 scoped_ptr<MCSClient> mcs_client_;
326
[email protected]5799d052014-02-12 20:47:39327 scoped_ptr<CheckinRequest> checkin_request_;
[email protected]b83122a92014-01-22 21:29:29328
[email protected]3a20a4d2014-03-21 22:54:21329 // Cached registration info.
330 RegistrationInfoMap registrations_;
[email protected]848b1b62014-01-30 23:51:04331
[email protected]3a20a4d2014-03-21 22:54:21332 // Currently pending registration requests. GCMClientImpl owns the
333 // RegistrationRequests.
334 PendingRegistrationRequests pending_registration_requests_;
335 STLValueDeleter<PendingRegistrationRequests>
336 pending_registration_requests_deleter_;
337
338 // Currently pending unregistration requests. GCMClientImpl owns the
[email protected]e4007042014-02-15 20:34:28339 // UnregistrationRequests.
[email protected]3a20a4d2014-03-21 22:54:21340 PendingUnregistrationRequests pending_unregistration_requests_;
341 STLValueDeleter<PendingUnregistrationRequests>
342 pending_unregistration_requests_deleter_;
[email protected]e4007042014-02-15 20:34:28343
[email protected]764c0442014-05-01 04:30:55344 // G-services settings that were provided by MCS.
[email protected]06e45272014-05-06 03:41:34345 GServicesSettings gservices_settings_;
[email protected]764c0442014-05-01 04:30:55346
347 // Time of the last successful checkin.
348 base::Time last_checkin_time_;
349
jianli10018b2d2015-05-11 21:14:13350 // Cached instance ID data, key is app id.
351 std::map<std::string, std::string> instance_id_data_;
352
[email protected]764c0442014-05-01 04:30:55353 // Factory for creating references when scheduling periodic checkin.
354 base::WeakPtrFactory<GCMClientImpl> periodic_checkin_ptr_factory_;
355
[email protected]955e0ff2014-01-31 20:42:12356 // Factory for creating references in callbacks.
357 base::WeakPtrFactory<GCMClientImpl> weak_ptr_factory_;
358
[email protected]e4097c82013-11-08 00:16:12359 DISALLOW_COPY_AND_ASSIGN(GCMClientImpl);
360};
361
362} // namespace gcm
363
[email protected]cd57f372014-06-09 17:13:06364#endif // COMPONENTS_GCM_DRIVER_GCM_CLIENT_IMPL_H_