blob: 920bd872353109be0d2ca3115098164ac0390df9 [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"
[email protected]b83122a92014-01-22 21:29:2927#include "net/base/net_log.h"
28#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,
68 scoped_refptr<net::HttpNetworkSession> network_session,
[email protected]9df5b932014-04-30 00:39:0669 net::NetLog* net_log,
70 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:
[email protected]2bbe0a682014-03-26 00:08:3181 explicit GCMClientImpl(scoped_ptr<GCMInternalsBuilder> internals_builder);
[email protected]e4097c82013-11-08 00:16:1282 virtual ~GCMClientImpl();
83
[email protected]c31e1b52014-06-12 21:00:4784 // GCMClient implementation.
[email protected]e2a4a8012014-02-07 22:32:5285 virtual void Initialize(
[email protected]8ad80512014-05-23 09:40:4786 const ChromeBuildInfo& chrome_build_info,
[email protected]e2a4a8012014-02-07 22:32:5287 const base::FilePath& store_path,
[email protected]e2a4a8012014-02-07 22:32:5288 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
89 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:3990 url_request_context_getter,
[email protected]446f73c22014-05-14 20:47:1891 scoped_ptr<Encryptor> encryptor,
[email protected]7de78802014-05-10 19:49:4092 GCMClient::Delegate* delegate) OVERRIDE;
[email protected]fc767e42014-05-13 05:02:1493 virtual void Start() OVERRIDE;
[email protected]21fee5482014-03-05 00:57:1594 virtual void Stop() OVERRIDE;
[email protected]5799d052014-02-12 20:47:3995 virtual void CheckOut() OVERRIDE;
96 virtual void Register(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:1297 const std::vector<std::string>& sender_ids) OVERRIDE;
[email protected]5799d052014-02-12 20:47:3998 virtual void Unregister(const std::string& app_id) OVERRIDE;
99 virtual void Send(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12100 const std::string& receiver_id,
101 const OutgoingMessage& message) OVERRIDE;
[email protected]436bcb82014-04-18 00:40:57102 virtual void SetRecording(bool recording) OVERRIDE;
103 virtual void ClearActivityLogs() OVERRIDE;
[email protected]35601812014-03-07 19:52:43104 virtual GCMStatistics GetStatistics() const OVERRIDE;
[email protected]7df5ef22014-07-17 07:35:58105 virtual void SetAccountsForCheckin(
106 const std::map<std::string, std::string>& account_tokens) OVERRIDE;
[email protected]c31e1b52014-06-12 21:00:47107
108 // GCMStatsRecorder::Delegate implemenation.
[email protected]7de78802014-05-10 19:49:40109 virtual void OnActivityRecorded() OVERRIDE;
[email protected]e4097c82013-11-08 00:16:12110
[email protected]c31e1b52014-06-12 21:00:47111 // ConnectionFactory::ConnectionListener implementation.
112 virtual void OnConnected(const GURL& current_server,
113 const net::IPEndPoint& ip_endpoint) OVERRIDE;
114 virtual void OnDisconnected() OVERRIDE;
115
[email protected]e4097c82013-11-08 00:16:12116 private:
[email protected]b83122a92014-01-22 21:29:29117 // State representation of the GCMClient.
[email protected]35601812014-03-07 19:52:43118 // Any change made to this enum should have corresponding change in the
119 // GetStateString(...) function.
[email protected]b83122a92014-01-22 21:29:29120 enum State {
121 // Uninitialized.
122 UNINITIALIZED,
[email protected]d3a4b2e2014-02-27 13:46:54123 // Initialized,
124 INITIALIZED,
[email protected]b83122a92014-01-22 21:29:29125 // GCM store loading is in progress.
126 LOADING,
127 // Initial device checkin is in progress.
128 INITIAL_DEVICE_CHECKIN,
129 // Ready to accept requests.
130 READY,
131 };
132
[email protected]7df5ef22014-07-17 07:35:58133 // The check-in info for the device.
134 // TODO(fgorski): Convert to a class with explicit getters/setters.
[email protected]cd57f372014-06-09 17:13:06135 struct CheckinInfo {
[email protected]7df5ef22014-07-17 07:35:58136 CheckinInfo();
137 ~CheckinInfo();
[email protected]5799d052014-02-12 20:47:39138 bool IsValid() const { return android_id != 0 && secret != 0; }
[email protected]7df5ef22014-07-17 07:35:58139 void SnapshotCheckinAccounts();
140 void Reset();
[email protected]b83122a92014-01-22 21:29:29141
[email protected]7df5ef22014-07-17 07:35:58142 // Android ID of the device as assigned by the server.
[email protected]5799d052014-02-12 20:47:39143 uint64 android_id;
[email protected]7df5ef22014-07-17 07:35:58144 // Security token of the device as assigned by the server.
[email protected]5799d052014-02-12 20:47:39145 uint64 secret;
[email protected]7df5ef22014-07-17 07:35:58146 // True if accounts were already provided through SetAccountsForCheckin(),
147 // or when |last_checkin_accounts| was loaded as empty.
148 bool accounts_set;
149 // Map of account email addresses and OAuth2 tokens that will be sent to the
150 // checkin server on a next checkin.
151 std::map<std::string, std::string> account_tokens;
152 // As set of accounts last checkin was completed with.
153 std::set<std::string> last_checkin_accounts;
[email protected]848b1b62014-01-30 23:51:04154 };
155
[email protected]e4007042014-02-15 20:34:28156 // Collection of pending registration requests. Keys are app IDs, while values
[email protected]5799d052014-02-12 20:47:39157 // are pending registration requests to obtain a registration ID for
158 // requesting application.
159 typedef std::map<std::string, RegistrationRequest*>
[email protected]3a20a4d2014-03-21 22:54:21160 PendingRegistrationRequests;
[email protected]848b1b62014-01-30 23:51:04161
[email protected]e4007042014-02-15 20:34:28162 // Collection of pending unregistration requests. Keys are app IDs, while
163 // values are pending unregistration requests to disable the registration ID
164 // currently assigned to the application.
165 typedef std::map<std::string, UnregistrationRequest*>
[email protected]3a20a4d2014-03-21 22:54:21166 PendingUnregistrationRequests;
[email protected]e4007042014-02-15 20:34:28167
[email protected]b83122a92014-01-22 21:29:29168 friend class GCMClientImplTest;
169
[email protected]35601812014-03-07 19:52:43170 // Returns text representation of the enum State.
171 std::string GetStateString() const;
172
[email protected]b83122a92014-01-22 21:29:29173 // Callbacks for the MCSClient.
174 // Receives messages and dispatches them to relevant user delegates.
175 void OnMessageReceivedFromMCS(const gcm::MCSMessage& message);
176 // Receives confirmation of sent messages or information about errors.
177 void OnMessageSentToMCS(int64 user_serial_number,
178 const std::string& app_id,
179 const std::string& message_id,
180 MCSClient::MessageSendStatus status);
181 // Receives information about mcs_client_ errors.
182 void OnMCSError();
183
184 // Runs after GCM Store load is done to trigger continuation of the
185 // initialization.
[email protected]2809af72014-01-25 09:23:57186 void OnLoadCompleted(scoped_ptr<GCMStore::LoadResult> result);
[email protected]b83122a92014-01-22 21:29:29187 // Initializes mcs_client_, which handles the connection to MCS.
[email protected]2809af72014-01-25 09:23:57188 void InitializeMCSClient(scoped_ptr<GCMStore::LoadResult> result);
[email protected]b83122a92014-01-22 21:29:29189 // Complets the first time device checkin.
190 void OnFirstTimeDeviceCheckinCompleted(const CheckinInfo& checkin_info);
191 // Starts a login on mcs_client_.
192 void StartMCSLogin();
193 // Resets state to before initialization.
194 void ResetState();
[email protected]86625df2014-01-31 03:47:58195 // Sets state to ready. This will initiate the MCS login and notify the
196 // delegates.
197 void OnReady();
[email protected]b83122a92014-01-22 21:29:29198
[email protected]5799d052014-02-12 20:47:39199 // Starts a first time device checkin.
[email protected]25b5f50e2014-04-03 08:27:23200 void StartCheckin();
[email protected]2c4d4cd2014-04-10 21:10:22201 // Completes the device checkin request by parsing the |checkin_response|.
202 // Function also cleans up the pending checkin.
203 void OnCheckinCompleted(
204 const checkin_proto::AndroidCheckinResponse& checkin_response);
[email protected]06e45272014-05-06 03:41:34205
206 // Callback passed to GCMStore::SetGServicesSettings.
207 void SetGServicesSettingsCallback(bool success);
208
[email protected]764c0442014-05-01 04:30:55209 // Schedules next periodic device checkin and makes sure there is at most one
210 // pending checkin at a time. This function is meant to be called after a
211 // successful checkin.
212 void SchedulePeriodicCheckin();
213 // Gets the time until next checkin.
214 base::TimeDelta GetTimeToNextCheckin() const;
[email protected]7df5ef22014-07-17 07:35:58215 // Callback for setting last checkin information in the |gcm_store_|.
216 void SetLastCheckinInfoCallback(bool success);
[email protected]b83122a92014-01-22 21:29:29217
218 // Callback for persisting device credentials in the |gcm_store_|.
219 void SetDeviceCredentialsCallback(bool success);
220
[email protected]3a20a4d2014-03-21 22:54:21221 // Callback for persisting registration info in the |gcm_store_|.
222 void UpdateRegistrationCallback(bool success);
223
[email protected]848b1b62014-01-30 23:51:04224 // Completes the registration request.
[email protected]5799d052014-02-12 20:47:39225 void OnRegisterCompleted(const std::string& app_id,
[email protected]3a20a4d2014-03-21 22:54:21226 const std::vector<std::string>& sender_ids,
[email protected]b4dd0232014-02-08 02:37:31227 RegistrationRequest::Status status,
[email protected]848b1b62014-01-30 23:51:04228 const std::string& registration_id);
229
[email protected]e4007042014-02-15 20:34:28230 // Completes the unregistration request.
[email protected]0e88e1d12014-03-19 06:53:08231 void OnUnregisterCompleted(const std::string& app_id,
232 UnregistrationRequest::Status status);
[email protected]e4007042014-02-15 20:34:28233
[email protected]d3a4b2e2014-02-27 13:46:54234 // Completes the GCM store destroy request.
235 void OnGCMStoreDestroyed(bool success);
236
[email protected]c6fe36b2014-03-11 10:58:12237 // Handles incoming data message and dispatches it the delegate of this class.
[email protected]b83122a92014-01-22 21:29:29238 void HandleIncomingMessage(const gcm::MCSMessage& message);
239
[email protected]c6fe36b2014-03-11 10:58:12240 // Fires OnMessageReceived event on the delegate of this class, based on the
241 // details in |data_message_stanza| and |message_data|.
242 void HandleIncomingDataMessage(
243 const mcs_proto::DataMessageStanza& data_message_stanza,
244 MessageData& message_data);
245
246 // Fires OnMessageSendError event on the delegate of this calss, based on the
247 // details in |data_message_stanza| and |message_data|.
248 void HandleIncomingSendError(
249 const mcs_proto::DataMessageStanza& data_message_stanza,
250 MessageData& message_data);
[email protected]848b1b62014-01-30 23:51:04251
[email protected]2bbe0a682014-03-26 00:08:31252 // Builder for the GCM internals (mcs client, etc.).
253 scoped_ptr<GCMInternalsBuilder> internals_builder_;
[email protected]b83122a92014-01-22 21:29:29254
[email protected]436bcb82014-04-18 00:40:57255 // Recorder that logs GCM activities.
[email protected]025adfa2014-06-03 21:51:12256 GCMStatsRecorderImpl recorder_;
[email protected]436bcb82014-04-18 00:40:57257
[email protected]b83122a92014-01-22 21:29:29258 // State of the GCM Client Implementation.
259 State state_;
260
[email protected]7de78802014-05-10 19:49:40261 GCMClient::Delegate* delegate_;
[email protected]5799d052014-02-12 20:47:39262
[email protected]b83122a92014-01-22 21:29:29263 // Device checkin info (android ID and security token used by device).
264 CheckinInfo device_checkin_info_;
265
[email protected]848b1b62014-01-30 23:51:04266 // Clock used for timing of retry logic. Passed in for testing. Owned by
267 // GCMClientImpl.
268 scoped_ptr<base::Clock> clock_;
[email protected]b83122a92014-01-22 21:29:29269
270 // Information about the chrome build.
271 // TODO(fgorski): Check if it can be passed in constructor and made const.
[email protected]8ad80512014-05-23 09:40:47272 ChromeBuildInfo chrome_build_info_;
[email protected]b83122a92014-01-22 21:29:29273
274 // Persistent data store for keeping device credentials, messages and user to
275 // serial number mappings.
[email protected]79994f42014-01-16 16:05:36276 scoped_ptr<GCMStore> gcm_store_;
[email protected]b83122a92014-01-22 21:29:29277
[email protected]b83122a92014-01-22 21:29:29278 scoped_refptr<net::HttpNetworkSession> network_session_;
279 net::BoundNetLog net_log_;
280 scoped_ptr<ConnectionFactory> connection_factory_;
281 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
282
283 // Controls receiving and sending of packets and reliable message queueing.
284 scoped_ptr<MCSClient> mcs_client_;
285
[email protected]5799d052014-02-12 20:47:39286 scoped_ptr<CheckinRequest> checkin_request_;
[email protected]b83122a92014-01-22 21:29:29287
[email protected]3a20a4d2014-03-21 22:54:21288 // Cached registration info.
289 RegistrationInfoMap registrations_;
[email protected]848b1b62014-01-30 23:51:04290
[email protected]3a20a4d2014-03-21 22:54:21291 // Currently pending registration requests. GCMClientImpl owns the
292 // RegistrationRequests.
293 PendingRegistrationRequests pending_registration_requests_;
294 STLValueDeleter<PendingRegistrationRequests>
295 pending_registration_requests_deleter_;
296
297 // Currently pending unregistration requests. GCMClientImpl owns the
[email protected]e4007042014-02-15 20:34:28298 // UnregistrationRequests.
[email protected]3a20a4d2014-03-21 22:54:21299 PendingUnregistrationRequests pending_unregistration_requests_;
300 STLValueDeleter<PendingUnregistrationRequests>
301 pending_unregistration_requests_deleter_;
[email protected]e4007042014-02-15 20:34:28302
[email protected]764c0442014-05-01 04:30:55303 // G-services settings that were provided by MCS.
[email protected]06e45272014-05-06 03:41:34304 GServicesSettings gservices_settings_;
[email protected]764c0442014-05-01 04:30:55305
306 // Time of the last successful checkin.
307 base::Time last_checkin_time_;
308
309 // Factory for creating references when scheduling periodic checkin.
310 base::WeakPtrFactory<GCMClientImpl> periodic_checkin_ptr_factory_;
311
[email protected]955e0ff2014-01-31 20:42:12312 // Factory for creating references in callbacks.
313 base::WeakPtrFactory<GCMClientImpl> weak_ptr_factory_;
314
[email protected]e4097c82013-11-08 00:16:12315 DISALLOW_COPY_AND_ASSIGN(GCMClientImpl);
316};
317
318} // namespace gcm
319
[email protected]cd57f372014-06-09 17:13:06320#endif // COMPONENTS_GCM_DRIVER_GCM_CLIENT_IMPL_H_