Revert of Fix CapsLock remapping. (patchset #2 id:20001 of https://codereview.chromiu...
[chromium-blink-merge.git] / components / gcm_driver / gcm_client_impl.h
blob801a2251d735ee63cdd424dfeaf471c9aec6e402
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_IMPL_H_
6 #define COMPONENTS_GCM_DRIVER_GCM_CLIENT_IMPL_H_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/stl_util.h"
17 #include "components/gcm_driver/gcm_client.h"
18 #include "components/gcm_driver/gcm_stats_recorder_impl.h"
19 #include "google_apis/gcm/base/mcs_message.h"
20 #include "google_apis/gcm/engine/gcm_store.h"
21 #include "google_apis/gcm/engine/gservices_settings.h"
22 #include "google_apis/gcm/engine/mcs_client.h"
23 #include "google_apis/gcm/engine/registration_request.h"
24 #include "google_apis/gcm/engine/unregistration_request.h"
25 #include "google_apis/gcm/protocol/android_checkin.pb.h"
26 #include "google_apis/gcm/protocol/checkin.pb.h"
27 #include "net/base/net_log.h"
28 #include "net/url_request/url_request_context_getter.h"
30 class GURL;
32 namespace base {
33 class Clock;
34 class Time;
35 } // namespace base
37 namespace mcs_proto {
38 class DataMessageStanza;
39 } // namespace mcs_proto
41 namespace net {
42 class HttpNetworkSession;
43 } // namespace net
45 namespace gcm {
47 class CheckinRequest;
48 class ConnectionFactory;
49 class GCMClientImplTest;
51 // Helper class for building GCM internals. Allows tests to inject fake versions
52 // as necessary.
53 class GCMInternalsBuilder {
54 public:
55 GCMInternalsBuilder();
56 virtual ~GCMInternalsBuilder();
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,
63 GCMStore* gcm_store,
64 GCMStatsRecorder* recorder);
65 virtual scoped_ptr<ConnectionFactory> BuildConnectionFactory(
66 const std::vector<GURL>& endpoints,
67 const net::BackoffEntry::Policy& backoff_policy,
68 const scoped_refptr<net::HttpNetworkSession>& gcm_network_session,
69 const scoped_refptr<net::HttpNetworkSession>& http_network_session,
70 net::NetLog* net_log,
71 GCMStatsRecorder* recorder);
74 // 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.
78 class GCMClientImpl
79 : public GCMClient, public GCMStatsRecorder::Delegate,
80 public ConnectionFactory::ConnectionListener {
81 public:
82 // 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,
100 explicit GCMClientImpl(scoped_ptr<GCMInternalsBuilder> internals_builder);
101 ~GCMClientImpl() override;
103 // GCMClient implementation.
104 void Initialize(
105 const ChromeBuildInfo& chrome_build_info,
106 const base::FilePath& store_path,
107 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
108 const scoped_refptr<net::URLRequestContextGetter>&
109 url_request_context_getter,
110 scoped_ptr<Encryptor> encryptor,
111 GCMClient::Delegate* delegate) override;
112 void Start(StartMode start_mode) override;
113 void Stop() override;
114 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(
124 const std::vector<AccountTokenInfo>& account_tokens) override;
125 void UpdateAccountMapping(const AccountMapping& account_mapping) override;
126 void RemoveAccountMapping(const std::string& account_id) override;
127 void SetLastTokenFetchTime(const base::Time& time) override;
128 void UpdateHeartbeatTimer(scoped_ptr<base::Timer> timer) override;
130 // GCMStatsRecorder::Delegate implemenation.
131 void OnActivityRecorded() override;
133 // ConnectionFactory::ConnectionListener implementation.
134 void OnConnected(const GURL& current_server,
135 const net::IPEndPoint& ip_endpoint) override;
136 void OnDisconnected() override;
138 private:
139 // The check-in info for the device.
140 // TODO(fgorski): Convert to a class with explicit getters/setters.
141 struct CheckinInfo {
142 CheckinInfo();
143 ~CheckinInfo();
144 bool IsValid() const { return android_id != 0 && secret != 0; }
145 void SnapshotCheckinAccounts();
146 void Reset();
148 // Android ID of the device as assigned by the server.
149 uint64 android_id;
150 // Security token of the device as assigned by the server.
151 uint64 secret;
152 // True if accounts were already provided through SetAccountsForCheckin(),
153 // or when |last_checkin_accounts| was loaded as empty.
154 bool accounts_set;
155 // Map of account email addresses and OAuth2 tokens that will be sent to the
156 // checkin server on a next checkin.
157 std::map<std::string, std::string> account_tokens;
158 // As set of accounts last checkin was completed with.
159 std::set<std::string> last_checkin_accounts;
162 // Collection of pending registration requests. Keys are app IDs, while values
163 // are pending registration requests to obtain a registration ID for
164 // requesting application.
165 typedef std::map<std::string, RegistrationRequest*>
166 PendingRegistrationRequests;
168 // Collection of pending unregistration requests. Keys are app IDs, while
169 // values are pending unregistration requests to disable the registration ID
170 // currently assigned to the application.
171 typedef std::map<std::string, UnregistrationRequest*>
172 PendingUnregistrationRequests;
174 friend class GCMClientImplTest;
176 // Returns text representation of the enum State.
177 std::string GetStateString() const;
179 // Callbacks for the MCSClient.
180 // Receives messages and dispatches them to relevant user delegates.
181 void OnMessageReceivedFromMCS(const gcm::MCSMessage& message);
182 // Receives confirmation of sent messages or information about errors.
183 void OnMessageSentToMCS(int64 user_serial_number,
184 const std::string& app_id,
185 const std::string& message_id,
186 MCSClient::MessageSendStatus status);
187 // Receives information about mcs_client_ errors.
188 void OnMCSError();
190 // Runs after GCM Store load is done to trigger continuation of the
191 // initialization.
192 void OnLoadCompleted(scoped_ptr<GCMStore::LoadResult> result);
193 // Starts the GCM.
194 void StartGCM();
195 // Initializes mcs_client_, which handles the connection to MCS.
196 void InitializeMCSClient();
197 // Complets the first time device checkin.
198 void OnFirstTimeDeviceCheckinCompleted(const CheckinInfo& checkin_info);
199 // Starts a login on mcs_client_.
200 void StartMCSLogin();
201 // Resets the GCM store when it is corrupted.
202 void ResetStore();
203 // Sets state to ready. This will initiate the MCS login and notify the
204 // delegates.
205 void OnReady(const std::vector<AccountMapping>& account_mappings,
206 const base::Time& last_token_fetch_time);
208 // Starts a first time device checkin.
209 void StartCheckin();
210 // Completes the device checkin request by parsing the |checkin_response|.
211 // Function also cleans up the pending checkin.
212 void OnCheckinCompleted(
213 const checkin_proto::AndroidCheckinResponse& checkin_response);
215 // Callback passed to GCMStore::SetGServicesSettings.
216 void SetGServicesSettingsCallback(bool success);
218 // Schedules next periodic device checkin and makes sure there is at most one
219 // pending checkin at a time. This function is meant to be called after a
220 // successful checkin.
221 void SchedulePeriodicCheckin();
222 // Gets the time until next checkin.
223 base::TimeDelta GetTimeToNextCheckin() const;
224 // Callback for setting last checkin information in the |gcm_store_|.
225 void SetLastCheckinInfoCallback(bool success);
227 // Callback for persisting device credentials in the |gcm_store_|.
228 void SetDeviceCredentialsCallback(bool success);
230 // Callback for persisting registration info in the |gcm_store_|.
231 void UpdateRegistrationCallback(bool success);
233 // Callback for all store operations that do not try to recover, if write in
234 // |gcm_store_| fails.
235 void DefaultStoreCallback(bool success);
237 // Callback for store operation where result does not matter.
238 void IgnoreWriteResultCallback(bool success);
240 // Callback for resetting the GCM store.
241 void ResetStoreCallback(bool success);
243 // Completes the registration request.
244 void OnRegisterCompleted(const std::string& app_id,
245 const std::vector<std::string>& sender_ids,
246 RegistrationRequest::Status status,
247 const std::string& registration_id);
249 // Completes the unregistration request.
250 void OnUnregisterCompleted(const std::string& app_id,
251 UnregistrationRequest::Status status);
253 // Completes the GCM store destroy request.
254 void OnGCMStoreDestroyed(bool success);
256 // Handles incoming data message and dispatches it the delegate of this class.
257 void HandleIncomingMessage(const gcm::MCSMessage& message);
259 // Fires OnMessageReceived event on the delegate of this class, based on the
260 // details in |data_message_stanza| and |message_data|.
261 void HandleIncomingDataMessage(
262 const mcs_proto::DataMessageStanza& data_message_stanza,
263 MessageData& message_data);
265 // Fires OnMessageSendError event on the delegate of this calss, based on the
266 // details in |data_message_stanza| and |message_data|.
267 void HandleIncomingSendError(
268 const mcs_proto::DataMessageStanza& data_message_stanza,
269 MessageData& message_data);
271 // Is there any standalone app being registered for GCM?
272 bool HasStandaloneRegisteredApp() const;
274 // Builder for the GCM internals (mcs client, etc.).
275 scoped_ptr<GCMInternalsBuilder> internals_builder_;
277 // Recorder that logs GCM activities.
278 GCMStatsRecorderImpl recorder_;
280 // State of the GCM Client Implementation.
281 State state_;
283 GCMClient::Delegate* delegate_;
285 // Flag to indicate if the GCM should be delay started until it is actually
286 // used in either of the following cases:
287 // 1) The GCM store contains the registration records.
288 // 2) GCM functionailities are explicitly called.
289 StartMode start_mode_;
291 // Device checkin info (android ID and security token used by device).
292 CheckinInfo device_checkin_info_;
294 // Clock used for timing of retry logic. Passed in for testing. Owned by
295 // GCMClientImpl.
296 scoped_ptr<base::Clock> clock_;
298 // Information about the chrome build.
299 // TODO(fgorski): Check if it can be passed in constructor and made const.
300 ChromeBuildInfo chrome_build_info_;
302 // Persistent data store for keeping device credentials, messages and user to
303 // serial number mappings.
304 scoped_ptr<GCMStore> gcm_store_;
306 // Data loaded from the GCM store.
307 scoped_ptr<GCMStore::LoadResult> load_result_;
309 // Tracks if the GCM store has been reset. This is used to prevent from
310 // resetting and loading from the store again and again.
311 bool gcm_store_reset_;
313 scoped_refptr<net::HttpNetworkSession> network_session_;
314 net::BoundNetLog net_log_;
315 scoped_ptr<ConnectionFactory> connection_factory_;
316 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
318 // Controls receiving and sending of packets and reliable message queueing.
319 scoped_ptr<MCSClient> mcs_client_;
321 scoped_ptr<CheckinRequest> checkin_request_;
323 // Cached registration info.
324 RegistrationInfoMap registrations_;
326 // Currently pending registration requests. GCMClientImpl owns the
327 // RegistrationRequests.
328 PendingRegistrationRequests pending_registration_requests_;
329 STLValueDeleter<PendingRegistrationRequests>
330 pending_registration_requests_deleter_;
332 // Currently pending unregistration requests. GCMClientImpl owns the
333 // UnregistrationRequests.
334 PendingUnregistrationRequests pending_unregistration_requests_;
335 STLValueDeleter<PendingUnregistrationRequests>
336 pending_unregistration_requests_deleter_;
338 // G-services settings that were provided by MCS.
339 GServicesSettings gservices_settings_;
341 // Time of the last successful checkin.
342 base::Time last_checkin_time_;
344 // Factory for creating references when scheduling periodic checkin.
345 base::WeakPtrFactory<GCMClientImpl> periodic_checkin_ptr_factory_;
347 // Factory for creating references in callbacks.
348 base::WeakPtrFactory<GCMClientImpl> weak_ptr_factory_;
350 DISALLOW_COPY_AND_ASSIGN(GCMClientImpl);
353 } // namespace gcm
355 #endif // COMPONENTS_GCM_DRIVER_GCM_CLIENT_IMPL_H_