[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 5 | #ifndef COMPONENTS_GCM_DRIVER_GCM_CLIENT_IMPL_H_ |
| 6 | #define COMPONENTS_GCM_DRIVER_GCM_CLIENT_IMPL_H_ |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 7 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 10 | #include <map> |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 11 | #include <memory> |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 12 | #include <set> |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 13 | #include <string> |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 14 | #include <utility> |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 15 | #include <vector> |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 16 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 17 | #include "base/compiler_specific.h" |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 18 | #include "base/macros.h" |
[email protected] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 19 | #include "base/memory/ref_counted.h" |
[email protected] | 955e0ff | 2014-01-31 20:42:12 | [diff] [blame] | 20 | #include "base/memory/weak_ptr.h" |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 21 | #include "components/gcm_driver/gcm_client.h" |
| 22 | #include "components/gcm_driver/gcm_stats_recorder_impl.h" |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 23 | #include "google_apis/gcm/base/mcs_message.h" |
| 24 | #include "google_apis/gcm/engine/gcm_store.h" |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 25 | #include "google_apis/gcm/engine/gservices_settings.h" |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 26 | #include "google_apis/gcm/engine/mcs_client.h" |
[email protected] | b4dd023 | 2014-02-08 02:37:31 | [diff] [blame] | 27 | #include "google_apis/gcm/engine/registration_request.h" |
[email protected] | 0e88e1d1 | 2014-03-19 06:53:08 | [diff] [blame] | 28 | #include "google_apis/gcm/engine/unregistration_request.h" |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 29 | #include "google_apis/gcm/protocol/android_checkin.pb.h" |
[email protected] | 2c4d4cd | 2014-04-10 21:10:22 | [diff] [blame] | 30 | #include "google_apis/gcm/protocol/checkin.pb.h" |
zea | 76342abf | 2016-11-01 17:26:04 | [diff] [blame] | 31 | #include "net/http/http_status_code.h" |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 32 | #include "net/url_request/url_request_context_getter.h" |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 33 | |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 34 | class GURL; |
| 35 | |
[email protected] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 36 | namespace base { |
[email protected] | 955e0ff | 2014-01-31 20:42:12 | [diff] [blame] | 37 | class Clock; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 38 | class Time; |
[email protected] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 39 | } // namespace base |
| 40 | |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 41 | namespace mcs_proto { |
| 42 | class DataMessageStanza; |
| 43 | } // namespace mcs_proto |
| 44 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 45 | namespace net { |
Helen Li | b5b864d | 2018-06-15 16:10:26 | [diff] [blame] | 46 | class URLRequestContext; |
[email protected] | 955e0ff | 2014-01-31 20:42:12 | [diff] [blame] | 47 | } // namespace net |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 48 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 49 | namespace gcm { |
| 50 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 51 | class CheckinRequest; |
| 52 | class ConnectionFactory; |
| 53 | class GCMClientImplTest; |
[email protected] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 54 | |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 55 | // Helper class for building GCM internals. Allows tests to inject fake versions |
| 56 | // as necessary. |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 57 | class GCMInternalsBuilder { |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 58 | public: |
| 59 | GCMInternalsBuilder(); |
| 60 | virtual ~GCMInternalsBuilder(); |
| 61 | |
tzik | 48479d9 | 2018-03-20 15:20:42 | [diff] [blame] | 62 | virtual base::Clock* GetClock(); |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 63 | virtual std::unique_ptr<MCSClient> BuildMCSClient( |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 64 | const std::string& version, |
| 65 | base::Clock* clock, |
| 66 | ConnectionFactory* connection_factory, |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 67 | GCMStore* gcm_store, |
| 68 | GCMStatsRecorder* recorder); |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 69 | virtual std::unique_ptr<ConnectionFactory> BuildConnectionFactory( |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 70 | const std::vector<GURL>& endpoints, |
| 71 | const net::BackoffEntry::Policy& backoff_policy, |
Helen Li | b5b864d | 2018-06-15 16:10:26 | [diff] [blame] | 72 | net::URLRequestContext* url_request_context, |
[email protected] | 9df5b93 | 2014-04-30 00:39:06 | [diff] [blame] | 73 | GCMStatsRecorder* recorder); |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 74 | }; |
| 75 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 76 | // Implements the GCM Client. It is used to coordinate MCS Client (communication |
| 77 | // with MCS) and other pieces of GCM infrastructure like Registration and |
| 78 | // Checkins. It also allows for registering user delegates that host |
| 79 | // applications that send and receive messages. |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 80 | class GCMClientImpl |
[email protected] | c31e1b5 | 2014-06-12 21:00:47 | [diff] [blame] | 81 | : public GCMClient, public GCMStatsRecorder::Delegate, |
| 82 | public ConnectionFactory::ConnectionListener { |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 83 | public: |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 84 | // State representation of the GCMClient. |
| 85 | // Any change made to this enum should have corresponding change in the |
| 86 | // GetStateString(...) function. |
| 87 | enum State { |
| 88 | // Uninitialized. |
| 89 | UNINITIALIZED, |
| 90 | // Initialized, |
| 91 | INITIALIZED, |
| 92 | // GCM store loading is in progress. |
| 93 | LOADING, |
| 94 | // GCM store is loaded. |
| 95 | LOADED, |
| 96 | // Initial device checkin is in progress. |
| 97 | INITIAL_DEVICE_CHECKIN, |
| 98 | // Ready to accept requests. |
| 99 | READY, |
| 100 | }; |
| 101 | |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 102 | explicit GCMClientImpl( |
| 103 | std::unique_ptr<GCMInternalsBuilder> internals_builder); |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 104 | ~GCMClientImpl() override; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 105 | |
[email protected] | c31e1b5 | 2014-06-12 21:00:47 | [diff] [blame] | 106 | // GCMClient implementation. |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 107 | void Initialize( |
[email protected] | 8ad8051 | 2014-05-23 09:40:47 | [diff] [blame] | 108 | const ChromeBuildInfo& chrome_build_info, |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 109 | const base::FilePath& store_path, |
| 110 | const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner, |
| 111 | const scoped_refptr<net::URLRequestContextGetter>& |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 112 | url_request_context_getter, |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 113 | std::unique_ptr<Encryptor> encryptor, |
mostynb | fe59f48 | 2014-10-06 15:04:46 | [diff] [blame] | 114 | GCMClient::Delegate* delegate) override; |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 115 | void Start(StartMode start_mode) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 116 | void Stop() override; |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 117 | void Register(const linked_ptr<RegistrationInfo>& registration_info) override; |
johnme | 6576ecf | 2017-04-03 19:26:28 | [diff] [blame] | 118 | bool ValidateRegistration( |
| 119 | const linked_ptr<RegistrationInfo>& registration_info, |
| 120 | const std::string& registration_id) override; |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 121 | void Unregister( |
| 122 | const linked_ptr<RegistrationInfo>& registration_info) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 123 | void Send(const std::string& app_id, |
| 124 | const std::string& receiver_id, |
| 125 | const OutgoingMessage& message) override; |
peter | ee284ba5 | 2016-02-01 11:53:28 | [diff] [blame] | 126 | void RecordDecryptionFailure(const std::string& app_id, |
Peter Beverloo | a376e98c | 2017-06-27 15:55:37 | [diff] [blame] | 127 | GCMDecryptionResult result) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 128 | void SetRecording(bool recording) override; |
| 129 | void ClearActivityLogs() override; |
| 130 | GCMStatistics GetStatistics() const override; |
| 131 | void SetAccountTokens( |
mostynb | fe59f48 | 2014-10-06 15:04:46 | [diff] [blame] | 132 | const std::vector<AccountTokenInfo>& account_tokens) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 133 | void UpdateAccountMapping(const AccountMapping& account_mapping) override; |
| 134 | void RemoveAccountMapping(const std::string& account_id) override; |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 135 | void SetLastTokenFetchTime(const base::Time& time) override; |
tzik | b6c66b2 | 2018-07-06 12:14:16 | [diff] [blame^] | 136 | void UpdateHeartbeatTimer( |
| 137 | std::unique_ptr<base::RetainingOneShotTimer> timer) override; |
jianli | 10018b2d | 2015-05-11 21:14:13 | [diff] [blame] | 138 | void AddInstanceIDData(const std::string& app_id, |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 139 | const std::string& instance_id, |
| 140 | const std::string& extra_data) override; |
jianli | 10018b2d | 2015-05-11 21:14:13 | [diff] [blame] | 141 | void RemoveInstanceIDData(const std::string& app_id) override; |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 142 | void GetInstanceIDData(const std::string& app_id, |
| 143 | std::string* instance_id, |
| 144 | std::string* extra_data) override; |
fgorski | 2275446 | 2015-05-14 00:05:22 | [diff] [blame] | 145 | void AddHeartbeatInterval(const std::string& scope, int interval_ms) override; |
| 146 | void RemoveHeartbeatInterval(const std::string& scope) override; |
[email protected] | c31e1b5 | 2014-06-12 21:00:47 | [diff] [blame] | 147 | |
| 148 | // GCMStatsRecorder::Delegate implemenation. |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 149 | void OnActivityRecorded() override; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 150 | |
[email protected] | c31e1b5 | 2014-06-12 21:00:47 | [diff] [blame] | 151 | // ConnectionFactory::ConnectionListener implementation. |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 152 | void OnConnected(const GURL& current_server, |
| 153 | const net::IPEndPoint& ip_endpoint) override; |
| 154 | void OnDisconnected() override; |
[email protected] | c31e1b5 | 2014-06-12 21:00:47 | [diff] [blame] | 155 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 156 | private: |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 157 | // The check-in info for the device. |
| 158 | // TODO(fgorski): Convert to a class with explicit getters/setters. |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 159 | struct CheckinInfo { |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 160 | CheckinInfo(); |
| 161 | ~CheckinInfo(); |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 162 | bool IsValid() const { return android_id != 0 && secret != 0; } |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 163 | void SnapshotCheckinAccounts(); |
| 164 | void Reset(); |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 165 | |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 166 | // Android ID of the device as assigned by the server. |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 167 | uint64_t android_id; |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 168 | // Security token of the device as assigned by the server. |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 169 | uint64_t secret; |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 170 | // True if accounts were already provided through SetAccountsForCheckin(), |
| 171 | // or when |last_checkin_accounts| was loaded as empty. |
| 172 | bool accounts_set; |
| 173 | // Map of account email addresses and OAuth2 tokens that will be sent to the |
| 174 | // checkin server on a next checkin. |
| 175 | std::map<std::string, std::string> account_tokens; |
| 176 | // As set of accounts last checkin was completed with. |
| 177 | std::set<std::string> last_checkin_accounts; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 178 | }; |
| 179 | |
zea | e6e40418 | 2016-11-04 23:05:45 | [diff] [blame] | 180 | // Reasons for resetting the GCM Store. |
| 181 | // Note: this enum is recorded into a histogram. Do not change enum value |
| 182 | // or order. |
| 183 | enum ResetReason { |
| 184 | LOAD_FAILURE, // GCM store failed to load, but the store exists. |
| 185 | CHECKIN_REJECTED, // Checkin was rejected by server. |
| 186 | |
| 187 | RESET_REASON_COUNT, |
| 188 | }; |
| 189 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 190 | // Collection of pending registration requests. Keys are RegistrationInfo |
| 191 | // instance, while values are pending registration requests to obtain a |
| 192 | // registration ID for requesting application. |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 193 | using PendingRegistrationRequests = |
| 194 | std::map<linked_ptr<RegistrationInfo>, |
| 195 | std::unique_ptr<RegistrationRequest>, |
| 196 | RegistrationInfoComparer>; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 197 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 198 | // Collection of pending unregistration requests. Keys are RegistrationInfo |
| 199 | // instance, while values are pending unregistration requests to disable the |
| 200 | // registration ID currently assigned to the application. |
limasdf | 20c5d7a | 2015-12-01 01:16:19 | [diff] [blame] | 201 | using PendingUnregistrationRequests = |
| 202 | std::map<linked_ptr<RegistrationInfo>, |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 203 | std::unique_ptr<UnregistrationRequest>, |
limasdf | 20c5d7a | 2015-12-01 01:16:19 | [diff] [blame] | 204 | RegistrationInfoComparer>; |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 205 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 206 | friend class GCMClientImplTest; |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 207 | friend class GCMClientInstanceIDTest; |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 208 | |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 209 | // Returns text representation of the enum State. |
| 210 | std::string GetStateString() const; |
| 211 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 212 | // Callbacks for the MCSClient. |
| 213 | // Receives messages and dispatches them to relevant user delegates. |
| 214 | void OnMessageReceivedFromMCS(const gcm::MCSMessage& message); |
| 215 | // Receives confirmation of sent messages or information about errors. |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 216 | void OnMessageSentToMCS(int64_t user_serial_number, |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 217 | const std::string& app_id, |
| 218 | const std::string& message_id, |
| 219 | MCSClient::MessageSendStatus status); |
| 220 | // Receives information about mcs_client_ errors. |
| 221 | void OnMCSError(); |
| 222 | |
| 223 | // Runs after GCM Store load is done to trigger continuation of the |
| 224 | // initialization. |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 225 | void OnLoadCompleted(std::unique_ptr<GCMStore::LoadResult> result); |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 226 | // Starts the GCM. |
| 227 | void StartGCM(); |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 228 | // Initializes mcs_client_, which handles the connection to MCS. |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 229 | void InitializeMCSClient(); |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 230 | // Complets the first time device checkin. |
| 231 | void OnFirstTimeDeviceCheckinCompleted(const CheckinInfo& checkin_info); |
| 232 | // Starts a login on mcs_client_. |
| 233 | void StartMCSLogin(); |
jianli | 00b4600f | 2015-02-10 23:32:49 | [diff] [blame] | 234 | // Resets the GCM store when it is corrupted. |
| 235 | void ResetStore(); |
[email protected] | 86625df | 2014-01-31 03:47:58 | [diff] [blame] | 236 | // Sets state to ready. This will initiate the MCS login and notify the |
| 237 | // delegates. |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 238 | void OnReady(const std::vector<AccountMapping>& account_mappings, |
| 239 | const base::Time& last_token_fetch_time); |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 240 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 241 | // Starts a first time device checkin. |
[email protected] | 25b5f50e | 2014-04-03 08:27:23 | [diff] [blame] | 242 | void StartCheckin(); |
[email protected] | 2c4d4cd | 2014-04-10 21:10:22 | [diff] [blame] | 243 | // Completes the device checkin request by parsing the |checkin_response|. |
| 244 | // Function also cleans up the pending checkin. |
| 245 | void OnCheckinCompleted( |
zea | 76342abf | 2016-11-01 17:26:04 | [diff] [blame] | 246 | net::HttpStatusCode response_code, |
[email protected] | 2c4d4cd | 2014-04-10 21:10:22 | [diff] [blame] | 247 | const checkin_proto::AndroidCheckinResponse& checkin_response); |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 248 | |
| 249 | // Callback passed to GCMStore::SetGServicesSettings. |
| 250 | void SetGServicesSettingsCallback(bool success); |
| 251 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 252 | // Schedules next periodic device checkin and makes sure there is at most one |
| 253 | // pending checkin at a time. This function is meant to be called after a |
| 254 | // successful checkin. |
| 255 | void SchedulePeriodicCheckin(); |
| 256 | // Gets the time until next checkin. |
| 257 | base::TimeDelta GetTimeToNextCheckin() const; |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 258 | // Callback for setting last checkin information in the |gcm_store_|. |
| 259 | void SetLastCheckinInfoCallback(bool success); |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 260 | |
| 261 | // Callback for persisting device credentials in the |gcm_store_|. |
| 262 | void SetDeviceCredentialsCallback(bool success); |
| 263 | |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 264 | // Callback for persisting registration info in the |gcm_store_|. |
| 265 | void UpdateRegistrationCallback(bool success); |
| 266 | |
[email protected] | 72d4f25 | 2014-08-20 22:34:28 | [diff] [blame] | 267 | // Callback for all store operations that do not try to recover, if write in |
| 268 | // |gcm_store_| fails. |
| 269 | void DefaultStoreCallback(bool success); |
| 270 | |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 271 | // Callback for store operation where result does not matter. |
| 272 | void IgnoreWriteResultCallback(bool success); |
| 273 | |
jianli | 78b5604 | 2015-06-17 01:21:22 | [diff] [blame] | 274 | // Callback for destroying the GCM store. |
| 275 | void DestroyStoreCallback(bool success); |
| 276 | |
| 277 | // Callback for resetting the GCM store. The store will be reloaded. |
jianli | 00b4600f | 2015-02-10 23:32:49 | [diff] [blame] | 278 | void ResetStoreCallback(bool success); |
| 279 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 280 | // Completes the registration request. |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 281 | void OnRegisterCompleted( |
| 282 | const linked_ptr<RegistrationInfo>& registration_info, |
| 283 | RegistrationRequest::Status status, |
| 284 | const std::string& registration_id); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 285 | |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 286 | // Completes the unregistration request. |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 287 | void OnUnregisterCompleted( |
| 288 | const linked_ptr<RegistrationInfo>& registration_info, |
| 289 | UnregistrationRequest::Status status); |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 290 | |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 291 | // Completes the GCM store destroy request. |
| 292 | void OnGCMStoreDestroyed(bool success); |
| 293 | |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 294 | // Handles incoming data message and dispatches it the delegate of this class. |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 295 | void HandleIncomingMessage(const gcm::MCSMessage& message); |
| 296 | |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 297 | // Fires OnMessageReceived event on the delegate of this class, based on the |
| 298 | // details in |data_message_stanza| and |message_data|. |
| 299 | void HandleIncomingDataMessage( |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 300 | const std::string& app_id, |
| 301 | bool was_subtype, |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 302 | const mcs_proto::DataMessageStanza& data_message_stanza, |
| 303 | MessageData& message_data); |
| 304 | |
johnme | 409dc53 | 2016-12-13 23:43:42 | [diff] [blame] | 305 | // Fires OnMessagesDeleted event on the delegate of this class, based on the |
| 306 | // details in |data_message_stanza| and |message_data|. |
| 307 | void HandleIncomingDeletedMessages( |
| 308 | const std::string& app_id, |
| 309 | const mcs_proto::DataMessageStanza& data_message_stanza, |
| 310 | MessageData& message_data); |
| 311 | |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 312 | // Fires OnMessageSendError event on the delegate of this class, based on the |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 313 | // details in |data_message_stanza| and |message_data|. |
| 314 | void HandleIncomingSendError( |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 315 | const std::string& app_id, |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 316 | const mcs_proto::DataMessageStanza& data_message_stanza, |
| 317 | MessageData& message_data); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 318 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 319 | // Is there any standalone app being registered for GCM? |
| 320 | bool HasStandaloneRegisteredApp() const; |
| 321 | |
jianli | 78b5604 | 2015-06-17 01:21:22 | [diff] [blame] | 322 | // Destroys the store when it is not needed. |
| 323 | void DestroyStoreWhenNotNeeded(); |
| 324 | |
| 325 | // Reset all cahced values. |
| 326 | void ResetCache(); |
| 327 | |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 328 | // Builder for the GCM internals (mcs client, etc.). |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 329 | std::unique_ptr<GCMInternalsBuilder> internals_builder_; |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 330 | |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 331 | // Recorder that logs GCM activities. |
[email protected] | 025adfa | 2014-06-03 21:51:12 | [diff] [blame] | 332 | GCMStatsRecorderImpl recorder_; |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 333 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 334 | // State of the GCM Client Implementation. |
| 335 | State state_; |
| 336 | |
[email protected] | 7de7880 | 2014-05-10 19:49:40 | [diff] [blame] | 337 | GCMClient::Delegate* delegate_; |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 338 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 339 | // Flag to indicate if the GCM should be delay started until it is actually |
| 340 | // used in either of the following cases: |
| 341 | // 1) The GCM store contains the registration records. |
| 342 | // 2) GCM functionailities are explicitly called. |
| 343 | StartMode start_mode_; |
| 344 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 345 | // Device checkin info (android ID and security token used by device). |
| 346 | CheckinInfo device_checkin_info_; |
| 347 | |
tzik | 48479d9 | 2018-03-20 15:20:42 | [diff] [blame] | 348 | // Clock used for timing of retry logic. Passed in for testing. |
| 349 | base::Clock* clock_; |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 350 | |
| 351 | // Information about the chrome build. |
| 352 | // TODO(fgorski): Check if it can be passed in constructor and made const. |
[email protected] | 8ad8051 | 2014-05-23 09:40:47 | [diff] [blame] | 353 | ChromeBuildInfo chrome_build_info_; |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 354 | |
| 355 | // Persistent data store for keeping device credentials, messages and user to |
| 356 | // serial number mappings. |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 357 | std::unique_ptr<GCMStore> gcm_store_; |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 358 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 359 | // Data loaded from the GCM store. |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 360 | std::unique_ptr<GCMStore::LoadResult> load_result_; |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 361 | |
jianli | 00b4600f | 2015-02-10 23:32:49 | [diff] [blame] | 362 | // Tracks if the GCM store has been reset. This is used to prevent from |
| 363 | // resetting and loading from the store again and again. |
| 364 | bool gcm_store_reset_; |
| 365 | |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 366 | std::unique_ptr<ConnectionFactory> connection_factory_; |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 367 | scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 368 | |
| 369 | // Controls receiving and sending of packets and reliable message queueing. |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 370 | // Must be destroyed before |network_session_|. |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 371 | std::unique_ptr<MCSClient> mcs_client_; |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 372 | |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 373 | std::unique_ptr<CheckinRequest> checkin_request_; |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 374 | |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 375 | // Cached registration info. |
| 376 | RegistrationInfoMap registrations_; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 377 | |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 378 | // Currently pending registration requests. GCMClientImpl owns the |
| 379 | // RegistrationRequests. |
| 380 | PendingRegistrationRequests pending_registration_requests_; |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 381 | |
| 382 | // Currently pending unregistration requests. GCMClientImpl owns the |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 383 | // UnregistrationRequests. |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 384 | PendingUnregistrationRequests pending_unregistration_requests_; |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 385 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 386 | // G-services settings that were provided by MCS. |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 387 | GServicesSettings gservices_settings_; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 388 | |
| 389 | // Time of the last successful checkin. |
| 390 | base::Time last_checkin_time_; |
| 391 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 392 | // Cached instance ID data, key is app ID and value is pair of instance ID |
| 393 | // and extra data. |
| 394 | std::map<std::string, std::pair<std::string, std::string>> instance_id_data_; |
jianli | 10018b2d | 2015-05-11 21:14:13 | [diff] [blame] | 395 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 396 | // Factory for creating references when scheduling periodic checkin. |
| 397 | base::WeakPtrFactory<GCMClientImpl> periodic_checkin_ptr_factory_; |
| 398 | |
jianli | 78b5604 | 2015-06-17 01:21:22 | [diff] [blame] | 399 | // Factory for wiping out GCM store. |
| 400 | base::WeakPtrFactory<GCMClientImpl> destroying_gcm_store_ptr_factory_; |
| 401 | |
[email protected] | 955e0ff | 2014-01-31 20:42:12 | [diff] [blame] | 402 | // Factory for creating references in callbacks. |
| 403 | base::WeakPtrFactory<GCMClientImpl> weak_ptr_factory_; |
| 404 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 405 | DISALLOW_COPY_AND_ASSIGN(GCMClientImpl); |
| 406 | }; |
| 407 | |
| 408 | } // namespace gcm |
| 409 | |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 410 | #endif // COMPONENTS_GCM_DRIVER_GCM_CLIENT_IMPL_H_ |