blob: a15643da33ffa07df99c471f0d104382a8c69618 [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>
jianli7a0c9b62015-05-26 23:24:4711#include <utility>
[email protected]848b1b62014-01-30 23:51:0412#include <vector>
[email protected]b83122a92014-01-22 21:29:2913
[email protected]e4097c82013-11-08 00:16:1214#include "base/compiler_specific.h"
[email protected]79994f42014-01-16 16:05:3615#include "base/memory/ref_counted.h"
mgiucaf59c7a82015-06-25 09:11:5716#include "base/memory/scoped_ptr.h"
[email protected]955e0ff2014-01-31 20:42:1217#include "base/memory/weak_ptr.h"
[email protected]cd57f372014-06-09 17:13:0618#include "components/gcm_driver/gcm_client.h"
19#include "components/gcm_driver/gcm_stats_recorder_impl.h"
[email protected]b83122a92014-01-22 21:29:2920#include "google_apis/gcm/base/mcs_message.h"
21#include "google_apis/gcm/engine/gcm_store.h"
[email protected]06e45272014-05-06 03:41:3422#include "google_apis/gcm/engine/gservices_settings.h"
[email protected]b83122a92014-01-22 21:29:2923#include "google_apis/gcm/engine/mcs_client.h"
[email protected]b4dd0232014-02-08 02:37:3124#include "google_apis/gcm/engine/registration_request.h"
[email protected]0e88e1d12014-03-19 06:53:0825#include "google_apis/gcm/engine/unregistration_request.h"
[email protected]436bcb82014-04-18 00:40:5726#include "google_apis/gcm/protocol/android_checkin.pb.h"
[email protected]2c4d4cd2014-04-10 21:10:2227#include "google_apis/gcm/protocol/checkin.pb.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,
mmenkee65e7af2015-10-13 17:16:4268 net::HttpNetworkSession* gcm_network_session,
69 net::HttpNetworkSession* http_network_session,
[email protected]9df5b932014-04-30 00:39:0670 GCMStatsRecorder* recorder);
[email protected]2bbe0a682014-03-26 00:08:3171};
72
[email protected]b83122a92014-01-22 21:29:2973// Implements the GCM Client. It is used to coordinate MCS Client (communication
74// with MCS) and other pieces of GCM infrastructure like Registration and
75// Checkins. It also allows for registering user delegates that host
76// applications that send and receive messages.
[email protected]cd57f372014-06-09 17:13:0677class GCMClientImpl
[email protected]c31e1b52014-06-12 21:00:4778 : public GCMClient, public GCMStatsRecorder::Delegate,
79 public ConnectionFactory::ConnectionListener {
[email protected]e4097c82013-11-08 00:16:1280 public:
jianlif3e52af42015-01-21 23:18:4781 // State representation of the GCMClient.
82 // Any change made to this enum should have corresponding change in the
83 // GetStateString(...) function.
84 enum State {
85 // Uninitialized.
86 UNINITIALIZED,
87 // Initialized,
88 INITIALIZED,
89 // GCM store loading is in progress.
90 LOADING,
91 // GCM store is loaded.
92 LOADED,
93 // Initial device checkin is in progress.
94 INITIAL_DEVICE_CHECKIN,
95 // Ready to accept requests.
96 READY,
97 };
98
[email protected]2bbe0a682014-03-26 00:08:3199 explicit GCMClientImpl(scoped_ptr<GCMInternalsBuilder> internals_builder);
dcheng00ea022b2014-10-21 11:24:56100 ~GCMClientImpl() override;
[email protected]e4097c82013-11-08 00:16:12101
[email protected]c31e1b52014-06-12 21:00:47102 // GCMClient implementation.
dcheng00ea022b2014-10-21 11:24:56103 void Initialize(
[email protected]8ad80512014-05-23 09:40:47104 const ChromeBuildInfo& chrome_build_info,
[email protected]e2a4a8012014-02-07 22:32:52105 const base::FilePath& store_path,
106 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
107 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:39108 url_request_context_getter,
[email protected]446f73c22014-05-14 20:47:18109 scoped_ptr<Encryptor> encryptor,
mostynbfe59f482014-10-06 15:04:46110 GCMClient::Delegate* delegate) override;
jianlif3e52af42015-01-21 23:18:47111 void Start(StartMode start_mode) override;
dcheng00ea022b2014-10-21 11:24:56112 void Stop() override;
jianli7a0c9b62015-05-26 23:24:47113 void Register(const linked_ptr<RegistrationInfo>& registration_info) override;
114 void Unregister(
115 const linked_ptr<RegistrationInfo>& registration_info) override;
dcheng00ea022b2014-10-21 11:24:56116 void Send(const std::string& app_id,
117 const std::string& receiver_id,
118 const OutgoingMessage& message) override;
119 void SetRecording(bool recording) override;
120 void ClearActivityLogs() override;
121 GCMStatistics GetStatistics() const override;
122 void SetAccountTokens(
mostynbfe59f482014-10-06 15:04:46123 const std::vector<AccountTokenInfo>& account_tokens) override;
dcheng00ea022b2014-10-21 11:24:56124 void UpdateAccountMapping(const AccountMapping& account_mapping) override;
125 void RemoveAccountMapping(const std::string& account_id) override;
fgorski5df101702014-10-28 02:09:31126 void SetLastTokenFetchTime(const base::Time& time) override;
chirantan192a9212014-12-06 03:30:45127 void UpdateHeartbeatTimer(scoped_ptr<base::Timer> timer) override;
jianli10018b2d2015-05-11 21:14:13128 void AddInstanceIDData(const std::string& app_id,
jianli7a0c9b62015-05-26 23:24:47129 const std::string& instance_id,
130 const std::string& extra_data) override;
jianli10018b2d2015-05-11 21:14:13131 void RemoveInstanceIDData(const std::string& app_id) override;
jianli7a0c9b62015-05-26 23:24:47132 void GetInstanceIDData(const std::string& app_id,
133 std::string* instance_id,
134 std::string* extra_data) override;
fgorski22754462015-05-14 00:05:22135 void AddHeartbeatInterval(const std::string& scope, int interval_ms) override;
136 void RemoveHeartbeatInterval(const std::string& scope) override;
[email protected]c31e1b52014-06-12 21:00:47137
138 // GCMStatsRecorder::Delegate implemenation.
dcheng00ea022b2014-10-21 11:24:56139 void OnActivityRecorded() override;
[email protected]e4097c82013-11-08 00:16:12140
[email protected]c31e1b52014-06-12 21:00:47141 // ConnectionFactory::ConnectionListener implementation.
dcheng00ea022b2014-10-21 11:24:56142 void OnConnected(const GURL& current_server,
143 const net::IPEndPoint& ip_endpoint) override;
144 void OnDisconnected() override;
[email protected]c31e1b52014-06-12 21:00:47145
[email protected]e4097c82013-11-08 00:16:12146 private:
[email protected]7df5ef22014-07-17 07:35:58147 // The check-in info for the device.
148 // TODO(fgorski): Convert to a class with explicit getters/setters.
[email protected]cd57f372014-06-09 17:13:06149 struct CheckinInfo {
[email protected]7df5ef22014-07-17 07:35:58150 CheckinInfo();
151 ~CheckinInfo();
[email protected]5799d052014-02-12 20:47:39152 bool IsValid() const { return android_id != 0 && secret != 0; }
[email protected]7df5ef22014-07-17 07:35:58153 void SnapshotCheckinAccounts();
154 void Reset();
[email protected]b83122a92014-01-22 21:29:29155
[email protected]7df5ef22014-07-17 07:35:58156 // Android ID of the device as assigned by the server.
[email protected]5799d052014-02-12 20:47:39157 uint64 android_id;
[email protected]7df5ef22014-07-17 07:35:58158 // Security token of the device as assigned by the server.
[email protected]5799d052014-02-12 20:47:39159 uint64 secret;
[email protected]7df5ef22014-07-17 07:35:58160 // True if accounts were already provided through SetAccountsForCheckin(),
161 // or when |last_checkin_accounts| was loaded as empty.
162 bool accounts_set;
163 // Map of account email addresses and OAuth2 tokens that will be sent to the
164 // checkin server on a next checkin.
165 std::map<std::string, std::string> account_tokens;
166 // As set of accounts last checkin was completed with.
167 std::set<std::string> last_checkin_accounts;
[email protected]848b1b62014-01-30 23:51:04168 };
169
jianli7a0c9b62015-05-26 23:24:47170 // Collection of pending registration requests. Keys are RegistrationInfo
171 // instance, while values are pending registration requests to obtain a
172 // registration ID for requesting application.
limasdf20c5d7a2015-12-01 01:16:19173 using PendingRegistrationRequests = std::map<linked_ptr<RegistrationInfo>,
174 scoped_ptr<RegistrationRequest>,
175 RegistrationInfoComparer>;
[email protected]848b1b62014-01-30 23:51:04176
jianli7a0c9b62015-05-26 23:24:47177 // Collection of pending unregistration requests. Keys are RegistrationInfo
178 // instance, while values are pending unregistration requests to disable the
179 // registration ID currently assigned to the application.
limasdf20c5d7a2015-12-01 01:16:19180 using PendingUnregistrationRequests =
181 std::map<linked_ptr<RegistrationInfo>,
182 scoped_ptr<UnregistrationRequest>,
183 RegistrationInfoComparer>;
[email protected]e4007042014-02-15 20:34:28184
[email protected]b83122a92014-01-22 21:29:29185 friend class GCMClientImplTest;
jianlic02d25e2015-05-27 22:24:31186 friend class GCMClientInstanceIDTest;
[email protected]b83122a92014-01-22 21:29:29187
[email protected]35601812014-03-07 19:52:43188 // Returns text representation of the enum State.
189 std::string GetStateString() const;
190
[email protected]b83122a92014-01-22 21:29:29191 // Callbacks for the MCSClient.
192 // Receives messages and dispatches them to relevant user delegates.
193 void OnMessageReceivedFromMCS(const gcm::MCSMessage& message);
194 // Receives confirmation of sent messages or information about errors.
195 void OnMessageSentToMCS(int64 user_serial_number,
196 const std::string& app_id,
197 const std::string& message_id,
198 MCSClient::MessageSendStatus status);
199 // Receives information about mcs_client_ errors.
200 void OnMCSError();
201
202 // Runs after GCM Store load is done to trigger continuation of the
203 // initialization.
[email protected]2809af72014-01-25 09:23:57204 void OnLoadCompleted(scoped_ptr<GCMStore::LoadResult> result);
jianlif3e52af42015-01-21 23:18:47205 // Starts the GCM.
206 void StartGCM();
[email protected]b83122a92014-01-22 21:29:29207 // Initializes mcs_client_, which handles the connection to MCS.
jianlif3e52af42015-01-21 23:18:47208 void InitializeMCSClient();
[email protected]b83122a92014-01-22 21:29:29209 // Complets the first time device checkin.
210 void OnFirstTimeDeviceCheckinCompleted(const CheckinInfo& checkin_info);
211 // Starts a login on mcs_client_.
212 void StartMCSLogin();
jianli00b4600f2015-02-10 23:32:49213 // Resets the GCM store when it is corrupted.
214 void ResetStore();
[email protected]86625df2014-01-31 03:47:58215 // Sets state to ready. This will initiate the MCS login and notify the
216 // delegates.
fgorski5df101702014-10-28 02:09:31217 void OnReady(const std::vector<AccountMapping>& account_mappings,
218 const base::Time& last_token_fetch_time);
[email protected]b83122a92014-01-22 21:29:29219
[email protected]5799d052014-02-12 20:47:39220 // Starts a first time device checkin.
[email protected]25b5f50e2014-04-03 08:27:23221 void StartCheckin();
[email protected]2c4d4cd2014-04-10 21:10:22222 // Completes the device checkin request by parsing the |checkin_response|.
223 // Function also cleans up the pending checkin.
224 void OnCheckinCompleted(
225 const checkin_proto::AndroidCheckinResponse& checkin_response);
[email protected]06e45272014-05-06 03:41:34226
227 // Callback passed to GCMStore::SetGServicesSettings.
228 void SetGServicesSettingsCallback(bool success);
229
[email protected]764c0442014-05-01 04:30:55230 // Schedules next periodic device checkin and makes sure there is at most one
231 // pending checkin at a time. This function is meant to be called after a
232 // successful checkin.
233 void SchedulePeriodicCheckin();
234 // Gets the time until next checkin.
235 base::TimeDelta GetTimeToNextCheckin() const;
[email protected]7df5ef22014-07-17 07:35:58236 // Callback for setting last checkin information in the |gcm_store_|.
237 void SetLastCheckinInfoCallback(bool success);
[email protected]b83122a92014-01-22 21:29:29238
239 // Callback for persisting device credentials in the |gcm_store_|.
240 void SetDeviceCredentialsCallback(bool success);
241
[email protected]3a20a4d2014-03-21 22:54:21242 // Callback for persisting registration info in the |gcm_store_|.
243 void UpdateRegistrationCallback(bool success);
244
[email protected]72d4f252014-08-20 22:34:28245 // Callback for all store operations that do not try to recover, if write in
246 // |gcm_store_| fails.
247 void DefaultStoreCallback(bool success);
248
fgorski9a405102014-11-19 01:25:16249 // Callback for store operation where result does not matter.
250 void IgnoreWriteResultCallback(bool success);
251
jianli78b56042015-06-17 01:21:22252 // Callback for destroying the GCM store.
253 void DestroyStoreCallback(bool success);
254
255 // Callback for resetting the GCM store. The store will be reloaded.
jianli00b4600f2015-02-10 23:32:49256 void ResetStoreCallback(bool success);
257
[email protected]848b1b62014-01-30 23:51:04258 // Completes the registration request.
jianli7a0c9b62015-05-26 23:24:47259 void OnRegisterCompleted(
260 const linked_ptr<RegistrationInfo>& registration_info,
261 RegistrationRequest::Status status,
262 const std::string& registration_id);
[email protected]848b1b62014-01-30 23:51:04263
[email protected]e4007042014-02-15 20:34:28264 // Completes the unregistration request.
jianli7a0c9b62015-05-26 23:24:47265 void OnUnregisterCompleted(
266 const linked_ptr<RegistrationInfo>& registration_info,
267 UnregistrationRequest::Status status);
[email protected]e4007042014-02-15 20:34:28268
[email protected]d3a4b2e2014-02-27 13:46:54269 // Completes the GCM store destroy request.
270 void OnGCMStoreDestroyed(bool success);
271
[email protected]c6fe36b2014-03-11 10:58:12272 // Handles incoming data message and dispatches it the delegate of this class.
[email protected]b83122a92014-01-22 21:29:29273 void HandleIncomingMessage(const gcm::MCSMessage& message);
274
[email protected]c6fe36b2014-03-11 10:58:12275 // Fires OnMessageReceived event on the delegate of this class, based on the
276 // details in |data_message_stanza| and |message_data|.
277 void HandleIncomingDataMessage(
278 const mcs_proto::DataMessageStanza& data_message_stanza,
279 MessageData& message_data);
280
281 // Fires OnMessageSendError event on the delegate of this calss, based on the
282 // details in |data_message_stanza| and |message_data|.
283 void HandleIncomingSendError(
284 const mcs_proto::DataMessageStanza& data_message_stanza,
285 MessageData& message_data);
[email protected]848b1b62014-01-30 23:51:04286
jianlif3e52af42015-01-21 23:18:47287 // Is there any standalone app being registered for GCM?
288 bool HasStandaloneRegisteredApp() const;
289
jianli78b56042015-06-17 01:21:22290 // Destroys the store when it is not needed.
291 void DestroyStoreWhenNotNeeded();
292
293 // Reset all cahced values.
294 void ResetCache();
295
[email protected]2bbe0a682014-03-26 00:08:31296 // Builder for the GCM internals (mcs client, etc.).
297 scoped_ptr<GCMInternalsBuilder> internals_builder_;
[email protected]b83122a92014-01-22 21:29:29298
[email protected]436bcb82014-04-18 00:40:57299 // Recorder that logs GCM activities.
[email protected]025adfa2014-06-03 21:51:12300 GCMStatsRecorderImpl recorder_;
[email protected]436bcb82014-04-18 00:40:57301
[email protected]b83122a92014-01-22 21:29:29302 // State of the GCM Client Implementation.
303 State state_;
304
[email protected]7de78802014-05-10 19:49:40305 GCMClient::Delegate* delegate_;
[email protected]5799d052014-02-12 20:47:39306
jianlif3e52af42015-01-21 23:18:47307 // Flag to indicate if the GCM should be delay started until it is actually
308 // used in either of the following cases:
309 // 1) The GCM store contains the registration records.
310 // 2) GCM functionailities are explicitly called.
311 StartMode start_mode_;
312
[email protected]b83122a92014-01-22 21:29:29313 // Device checkin info (android ID and security token used by device).
314 CheckinInfo device_checkin_info_;
315
[email protected]848b1b62014-01-30 23:51:04316 // Clock used for timing of retry logic. Passed in for testing. Owned by
317 // GCMClientImpl.
318 scoped_ptr<base::Clock> clock_;
[email protected]b83122a92014-01-22 21:29:29319
320 // Information about the chrome build.
321 // TODO(fgorski): Check if it can be passed in constructor and made const.
[email protected]8ad80512014-05-23 09:40:47322 ChromeBuildInfo chrome_build_info_;
[email protected]b83122a92014-01-22 21:29:29323
324 // Persistent data store for keeping device credentials, messages and user to
325 // serial number mappings.
[email protected]79994f42014-01-16 16:05:36326 scoped_ptr<GCMStore> gcm_store_;
[email protected]b83122a92014-01-22 21:29:29327
jianlif3e52af42015-01-21 23:18:47328 // Data loaded from the GCM store.
329 scoped_ptr<GCMStore::LoadResult> load_result_;
330
jianli00b4600f2015-02-10 23:32:49331 // Tracks if the GCM store has been reset. This is used to prevent from
332 // resetting and loading from the store again and again.
333 bool gcm_store_reset_;
334
mmenkee65e7af2015-10-13 17:16:42335 scoped_ptr<net::HttpNetworkSession> network_session_;
[email protected]b83122a92014-01-22 21:29:29336 scoped_ptr<ConnectionFactory> connection_factory_;
337 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
338
339 // Controls receiving and sending of packets and reliable message queueing.
mmenkee65e7af2015-10-13 17:16:42340 // Must be destroyed before |network_session_|.
[email protected]b83122a92014-01-22 21:29:29341 scoped_ptr<MCSClient> mcs_client_;
342
[email protected]5799d052014-02-12 20:47:39343 scoped_ptr<CheckinRequest> checkin_request_;
[email protected]b83122a92014-01-22 21:29:29344
[email protected]3a20a4d2014-03-21 22:54:21345 // Cached registration info.
346 RegistrationInfoMap registrations_;
[email protected]848b1b62014-01-30 23:51:04347
[email protected]3a20a4d2014-03-21 22:54:21348 // Currently pending registration requests. GCMClientImpl owns the
349 // RegistrationRequests.
350 PendingRegistrationRequests pending_registration_requests_;
[email protected]3a20a4d2014-03-21 22:54:21351
352 // Currently pending unregistration requests. GCMClientImpl owns the
[email protected]e4007042014-02-15 20:34:28353 // UnregistrationRequests.
[email protected]3a20a4d2014-03-21 22:54:21354 PendingUnregistrationRequests pending_unregistration_requests_;
[email protected]e4007042014-02-15 20:34:28355
[email protected]764c0442014-05-01 04:30:55356 // G-services settings that were provided by MCS.
[email protected]06e45272014-05-06 03:41:34357 GServicesSettings gservices_settings_;
[email protected]764c0442014-05-01 04:30:55358
359 // Time of the last successful checkin.
360 base::Time last_checkin_time_;
361
jianli7a0c9b62015-05-26 23:24:47362 // Cached instance ID data, key is app ID and value is pair of instance ID
363 // and extra data.
364 std::map<std::string, std::pair<std::string, std::string>> instance_id_data_;
jianli10018b2d2015-05-11 21:14:13365
[email protected]764c0442014-05-01 04:30:55366 // Factory for creating references when scheduling periodic checkin.
367 base::WeakPtrFactory<GCMClientImpl> periodic_checkin_ptr_factory_;
368
jianli78b56042015-06-17 01:21:22369 // Factory for wiping out GCM store.
370 base::WeakPtrFactory<GCMClientImpl> destroying_gcm_store_ptr_factory_;
371
[email protected]955e0ff2014-01-31 20:42:12372 // Factory for creating references in callbacks.
373 base::WeakPtrFactory<GCMClientImpl> weak_ptr_factory_;
374
[email protected]e4097c82013-11-08 00:16:12375 DISALLOW_COPY_AND_ASSIGN(GCMClientImpl);
376};
377
378} // namespace gcm
379
[email protected]cd57f372014-06-09 17:13:06380#endif // COMPONENTS_GCM_DRIVER_GCM_CLIENT_IMPL_H_