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