[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" |
Sebastien Marchand | efda77e53 | 2019-01-25 22:53:52 | [diff] [blame] | 21 | #include "base/timer/timer.h" |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 22 | #include "components/gcm_driver/gcm_client.h" |
| 23 | #include "components/gcm_driver/gcm_stats_recorder_impl.h" |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 24 | #include "google_apis/gcm/base/mcs_message.h" |
| 25 | #include "google_apis/gcm/engine/gcm_store.h" |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 26 | #include "google_apis/gcm/engine/gservices_settings.h" |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 27 | #include "google_apis/gcm/engine/mcs_client.h" |
[email protected] | b4dd023 | 2014-02-08 02:37:31 | [diff] [blame] | 28 | #include "google_apis/gcm/engine/registration_request.h" |
[email protected] | 0e88e1d1 | 2014-03-19 06:53:08 | [diff] [blame] | 29 | #include "google_apis/gcm/engine/unregistration_request.h" |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 30 | #include "google_apis/gcm/protocol/android_checkin.pb.h" |
[email protected] | 2c4d4cd | 2014-04-10 21:10:22 | [diff] [blame] | 31 | #include "google_apis/gcm/protocol/checkin.pb.h" |
Julie Jeongeun Kim | 17b2adf1 | 2019-10-29 10:24:48 | [diff] [blame] | 32 | #include "mojo/public/cpp/bindings/pending_receiver.h" |
zea | 76342abf | 2016-11-01 17:26:04 | [diff] [blame] | 33 | #include "net/http/http_status_code.h" |
Helen Li | 5f3d96a | 2018-08-10 20:37:24 | [diff] [blame] | 34 | #include "services/network/public/mojom/proxy_resolving_socket.mojom.h" |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 35 | |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 36 | class GURL; |
| 37 | |
[email protected] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 38 | namespace base { |
[email protected] | 955e0ff | 2014-01-31 20:42:12 | [diff] [blame] | 39 | class Clock; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 40 | class Time; |
[email protected] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 41 | } // namespace base |
| 42 | |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 43 | namespace mcs_proto { |
| 44 | class DataMessageStanza; |
| 45 | } // namespace mcs_proto |
| 46 | |
Maks Orlovich | c70c93c | 2018-07-12 02:45:44 | [diff] [blame] | 47 | namespace network { |
Robbie McElrath | b0149933 | 2018-09-25 00:53:13 | [diff] [blame] | 48 | class NetworkConnectionTracker; |
Maks Orlovich | c70c93c | 2018-07-12 02:45:44 | [diff] [blame] | 49 | class SharedURLLoaderFactory; |
| 50 | } // namespace network |
| 51 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 52 | namespace gcm { |
| 53 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 54 | class CheckinRequest; |
| 55 | class ConnectionFactory; |
| 56 | class GCMClientImplTest; |
[email protected] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 57 | |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 58 | // Helper class for building GCM internals. Allows tests to inject fake versions |
| 59 | // as necessary. |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 60 | class GCMInternalsBuilder { |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 61 | public: |
| 62 | GCMInternalsBuilder(); |
| 63 | virtual ~GCMInternalsBuilder(); |
| 64 | |
tzik | 48479d9 | 2018-03-20 15:20:42 | [diff] [blame] | 65 | virtual base::Clock* GetClock(); |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 66 | virtual std::unique_ptr<MCSClient> BuildMCSClient( |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 67 | const std::string& version, |
| 68 | base::Clock* clock, |
| 69 | ConnectionFactory* connection_factory, |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 70 | GCMStore* gcm_store, |
Steven Zhu | b36394c | 2019-06-04 16:10:46 | [diff] [blame] | 71 | scoped_refptr<base::SequencedTaskRunner> io_task_runner, |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 72 | GCMStatsRecorder* recorder); |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 73 | virtual std::unique_ptr<ConnectionFactory> BuildConnectionFactory( |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 74 | const std::vector<GURL>& endpoints, |
| 75 | const net::BackoffEntry::Policy& backoff_policy, |
Julie Jeongeun Kim | 17b2adf1 | 2019-10-29 10:24:48 | [diff] [blame] | 76 | base::RepeatingCallback<void( |
| 77 | mojo::PendingReceiver<network::mojom::ProxyResolvingSocketFactory>)> |
Helen Li | 5f3d96a | 2018-08-10 20:37:24 | [diff] [blame] | 78 | get_socket_factory_callback, |
Steven Zhu | b36394c | 2019-06-04 16:10:46 | [diff] [blame] | 79 | scoped_refptr<base::SequencedTaskRunner> io_task_runner, |
Robbie McElrath | b0149933 | 2018-09-25 00:53:13 | [diff] [blame] | 80 | GCMStatsRecorder* recorder, |
| 81 | network::NetworkConnectionTracker* network_connection_tracker); |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 82 | }; |
| 83 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 84 | // Implements the GCM Client. It is used to coordinate MCS Client (communication |
| 85 | // with MCS) and other pieces of GCM infrastructure like Registration and |
| 86 | // Checkins. It also allows for registering user delegates that host |
| 87 | // applications that send and receive messages. |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 88 | class GCMClientImpl |
[email protected] | c31e1b5 | 2014-06-12 21:00:47 | [diff] [blame] | 89 | : public GCMClient, public GCMStatsRecorder::Delegate, |
| 90 | public ConnectionFactory::ConnectionListener { |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 91 | public: |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 92 | // State representation of the GCMClient. |
| 93 | // Any change made to this enum should have corresponding change in the |
| 94 | // GetStateString(...) function. |
| 95 | enum State { |
| 96 | // Uninitialized. |
| 97 | UNINITIALIZED, |
| 98 | // Initialized, |
| 99 | INITIALIZED, |
| 100 | // GCM store loading is in progress. |
| 101 | LOADING, |
| 102 | // GCM store is loaded. |
| 103 | LOADED, |
| 104 | // Initial device checkin is in progress. |
| 105 | INITIAL_DEVICE_CHECKIN, |
| 106 | // Ready to accept requests. |
| 107 | READY, |
| 108 | }; |
| 109 | |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 110 | explicit GCMClientImpl( |
| 111 | std::unique_ptr<GCMInternalsBuilder> internals_builder); |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 112 | ~GCMClientImpl() override; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 113 | |
[email protected] | c31e1b5 | 2014-06-12 21:00:47 | [diff] [blame] | 114 | // GCMClient implementation. |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 115 | void Initialize( |
[email protected] | 8ad8051 | 2014-05-23 09:40:47 | [diff] [blame] | 116 | const ChromeBuildInfo& chrome_build_info, |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 117 | const base::FilePath& store_path, |
| 118 | const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner, |
Steven Zhu | b36394c | 2019-06-04 16:10:46 | [diff] [blame] | 119 | scoped_refptr<base::SequencedTaskRunner> io_task_runner, |
Julie Jeongeun Kim | 17b2adf1 | 2019-10-29 10:24:48 | [diff] [blame] | 120 | base::RepeatingCallback<void( |
| 121 | mojo::PendingReceiver<network::mojom::ProxyResolvingSocketFactory>)> |
Helen Li | 5f3d96a | 2018-08-10 20:37:24 | [diff] [blame] | 122 | get_socket_factory_callback, |
Maks Orlovich | c70c93c | 2018-07-12 02:45:44 | [diff] [blame] | 123 | const scoped_refptr<network::SharedURLLoaderFactory>& url_loader_factory, |
Robbie McElrath | b0149933 | 2018-09-25 00:53:13 | [diff] [blame] | 124 | network::NetworkConnectionTracker* network_connection_tracker, |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 125 | std::unique_ptr<Encryptor> encryptor, |
mostynb | fe59f48 | 2014-10-06 15:04:46 | [diff] [blame] | 126 | GCMClient::Delegate* delegate) override; |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 127 | void Start(StartMode start_mode) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 128 | void Stop() override; |
Rayan Kanso | 3da434f | 2018-12-19 17:09:39 | [diff] [blame] | 129 | void Register(scoped_refptr<RegistrationInfo> registration_info) override; |
| 130 | bool ValidateRegistration(scoped_refptr<RegistrationInfo> registration_info, |
| 131 | const std::string& registration_id) override; |
| 132 | void Unregister(scoped_refptr<RegistrationInfo> registration_info) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 133 | void Send(const std::string& app_id, |
| 134 | const std::string& receiver_id, |
| 135 | const OutgoingMessage& message) override; |
peter | ee284ba5 | 2016-02-01 11:53:28 | [diff] [blame] | 136 | void RecordDecryptionFailure(const std::string& app_id, |
Peter Beverloo | a376e98c | 2017-06-27 15:55:37 | [diff] [blame] | 137 | GCMDecryptionResult result) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 138 | void SetRecording(bool recording) override; |
| 139 | void ClearActivityLogs() override; |
| 140 | GCMStatistics GetStatistics() const override; |
| 141 | void SetAccountTokens( |
mostynb | fe59f48 | 2014-10-06 15:04:46 | [diff] [blame] | 142 | const std::vector<AccountTokenInfo>& account_tokens) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 143 | void UpdateAccountMapping(const AccountMapping& account_mapping) override; |
Tanmoy Mollik | f6ec6b9 | 2019-08-12 16:19:55 | [diff] [blame] | 144 | void RemoveAccountMapping(const CoreAccountId& account_id) override; |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 145 | void SetLastTokenFetchTime(const base::Time& time) override; |
tzik | b6c66b2 | 2018-07-06 12:14:16 | [diff] [blame] | 146 | void UpdateHeartbeatTimer( |
| 147 | std::unique_ptr<base::RetainingOneShotTimer> timer) override; |
jianli | 10018b2d | 2015-05-11 21:14:13 | [diff] [blame] | 148 | void AddInstanceIDData(const std::string& app_id, |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 149 | const std::string& instance_id, |
| 150 | const std::string& extra_data) override; |
jianli | 10018b2d | 2015-05-11 21:14:13 | [diff] [blame] | 151 | void RemoveInstanceIDData(const std::string& app_id) override; |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 152 | void GetInstanceIDData(const std::string& app_id, |
| 153 | std::string* instance_id, |
| 154 | std::string* extra_data) override; |
fgorski | 2275446 | 2015-05-14 00:05:22 | [diff] [blame] | 155 | void AddHeartbeatInterval(const std::string& scope, int interval_ms) override; |
| 156 | void RemoveHeartbeatInterval(const std::string& scope) override; |
[email protected] | c31e1b5 | 2014-06-12 21:00:47 | [diff] [blame] | 157 | |
| 158 | // GCMStatsRecorder::Delegate implemenation. |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 159 | void OnActivityRecorded() override; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 160 | |
[email protected] | c31e1b5 | 2014-06-12 21:00:47 | [diff] [blame] | 161 | // ConnectionFactory::ConnectionListener implementation. |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 162 | void OnConnected(const GURL& current_server, |
| 163 | const net::IPEndPoint& ip_endpoint) override; |
| 164 | void OnDisconnected() override; |
[email protected] | c31e1b5 | 2014-06-12 21:00:47 | [diff] [blame] | 165 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 166 | private: |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 167 | // The check-in info for the device. |
| 168 | // TODO(fgorski): Convert to a class with explicit getters/setters. |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 169 | struct CheckinInfo { |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 170 | CheckinInfo(); |
| 171 | ~CheckinInfo(); |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 172 | bool IsValid() const { return android_id != 0 && secret != 0; } |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 173 | void SnapshotCheckinAccounts(); |
| 174 | void Reset(); |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 175 | |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 176 | // Android ID of the device as assigned by the server. |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 177 | uint64_t android_id; |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 178 | // Security token of the device as assigned by the server. |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 179 | uint64_t secret; |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 180 | // True if accounts were already provided through SetAccountsForCheckin(), |
| 181 | // or when |last_checkin_accounts| was loaded as empty. |
| 182 | bool accounts_set; |
| 183 | // Map of account email addresses and OAuth2 tokens that will be sent to the |
| 184 | // checkin server on a next checkin. |
| 185 | std::map<std::string, std::string> account_tokens; |
| 186 | // As set of accounts last checkin was completed with. |
| 187 | std::set<std::string> last_checkin_accounts; |
[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 | |
[email protected] | 7de7880 | 2014-05-10 19:49:40 | [diff] [blame] | 346 | 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. |
| 358 | 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 | |
Robbie McElrath | b0149933 | 2018-09-25 00:53:13 | [diff] [blame] | 382 | network::NetworkConnectionTracker* network_connection_tracker_; |
| 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] | 955e0ff | 2014-01-31 20:42:12 | [diff] [blame] | 421 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 422 | DISALLOW_COPY_AND_ASSIGN(GCMClientImpl); |
| 423 | }; |
| 424 | |
| 425 | } // namespace gcm |
| 426 | |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 427 | #endif // COMPONENTS_GCM_DRIVER_GCM_CLIENT_IMPL_H_ |