Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 2508c65 | 2013-11-27 01:49:50 | [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 | |
Peter Beverloo | 3413946 | 2018-04-10 14:18:06 | [diff] [blame] | 5 | #include "components/gcm_driver/fake_gcm_profile_service.h" |
[email protected] | 2508c65 | 2013-11-27 01:49:50 | [diff] [blame] | 6 | |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
johnme | 29022c5 | 2014-12-11 16:26:30 | [diff] [blame] | 9 | #include "base/format_macros.h" |
Avi Drissman | 12be031 | 2023-01-11 09:16:09 | [diff] [blame] | 10 | #include "base/functional/bind.h" |
skyostil | 0259835 | 2015-06-12 12:37:25 | [diff] [blame] | 11 | #include "base/location.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 12 | #include "base/memory/raw_ptr.h" |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
johnme | 29022c5 | 2014-12-11 16:26:30 | [diff] [blame] | 14 | #include "base/strings/stringprintf.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 15 | #include "base/task/single_thread_task_runner.h" |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 16 | #include "base/time/time.h" |
Alex Chau | da2073d6 | 2020-01-29 11:26:08 | [diff] [blame] | 17 | #include "components/gcm_driver/crypto/gcm_encryption_result.h" |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 18 | #include "components/gcm_driver/gcm_driver.h" |
johnme | a504573 | 2016-09-08 17:23:29 | [diff] [blame] | 19 | #include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h" |
[email protected] | 2508c65 | 2013-11-27 01:49:50 | [diff] [blame] | 20 | |
| 21 | namespace gcm { |
| 22 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 23 | class FakeGCMProfileService::CustomFakeGCMDriver |
| 24 | : public instance_id::FakeGCMDriverForInstanceID { |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 25 | public: |
Rushan Suleymanov | 79aba4e | 2023-01-23 18:14:16 | [diff] [blame] | 26 | CustomFakeGCMDriver(); |
| 27 | |
| 28 | // Must be called before any other methods. |
| 29 | void SetService(FakeGCMProfileService* service); |
Peter Boström | 09c0182 | 2021-09-20 22:43:27 | [diff] [blame] | 30 | |
| 31 | CustomFakeGCMDriver(const CustomFakeGCMDriver&) = delete; |
| 32 | CustomFakeGCMDriver& operator=(const CustomFakeGCMDriver&) = delete; |
| 33 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 34 | ~CustomFakeGCMDriver() override; |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 35 | |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 36 | void OnRegisterFinished(const std::string& app_id, |
| 37 | const std::string& registration_id, |
| 38 | GCMClient::Result result); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 39 | void OnSendFinished(const std::string& app_id, |
| 40 | const std::string& message_id, |
| 41 | GCMClient::Result result); |
| 42 | |
Alex Chau | da2073d6 | 2020-01-29 11:26:08 | [diff] [blame] | 43 | // GCMDriver overrides: |
| 44 | void EncryptMessage(const std::string& app_id, |
| 45 | const std::string& authorized_entity, |
| 46 | const std::string& p256dh, |
| 47 | const std::string& auth_secret, |
| 48 | const std::string& message, |
| 49 | EncryptMessageCallback callback) override; |
Himanshu Jaju | 3dc84e8 | 2019-08-01 16:46:01 | [diff] [blame] | 50 | |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 51 | protected: |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 52 | // FakeGCMDriver overrides: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 53 | void RegisterImpl(const std::string& app_id, |
| 54 | const std::vector<std::string>& sender_ids) override; |
| 55 | void UnregisterImpl(const std::string& app_id) override; |
peter | c5634e8f | 2015-12-02 15:34:59 | [diff] [blame] | 56 | void UnregisterWithSenderIdImpl(const std::string& app_id, |
| 57 | const std::string& sender_id) override; |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 58 | void SendImpl(const std::string& app_id, |
| 59 | const std::string& receiver_id, |
mvanouwerkerk | f8633deb | 2015-07-13 11:04:06 | [diff] [blame] | 60 | const OutgoingMessage& message) override; |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 61 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 62 | // FakeGCMDriverForInstanceID overrides: |
| 63 | void GetToken(const std::string& app_id, |
| 64 | const std::string& authorized_entity, |
| 65 | const std::string& scope, |
Maksim Moskvitin | 8b44898 | 2020-02-06 13:23:19 | [diff] [blame] | 66 | base::TimeDelta time_to_live, |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 67 | GetTokenCallback callback) override; |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 68 | void DeleteToken(const std::string& app_id, |
| 69 | const std::string& authorized_entity, |
| 70 | const std::string& scope, |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 71 | DeleteTokenCallback callback) override; |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 72 | |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 73 | private: |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 74 | void DoRegister(const std::string& app_id, |
| 75 | const std::vector<std::string>& sender_ids, |
| 76 | const std::string& registration_id); |
| 77 | void DoSend(const std::string& app_id, |
| 78 | const std::string& receiver_id, |
| 79 | const OutgoingMessage& message); |
| 80 | |
Rushan Suleymanov | 79aba4e | 2023-01-23 18:14:16 | [diff] [blame] | 81 | raw_ptr<FakeGCMProfileService> service_ = nullptr; |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 82 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 83 | // Used to give each registration a unique registration id. Does not decrease |
| 84 | // when unregister is called. |
| 85 | int registration_count_ = 0; |
| 86 | |
Jeremy Roman | 5c341f6d | 2019-07-15 15:56:10 | [diff] [blame] | 87 | base::WeakPtrFactory<CustomFakeGCMDriver> weak_factory_{ |
| 88 | this}; // Must be last. |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 89 | }; |
| 90 | |
Rushan Suleymanov | 79aba4e | 2023-01-23 18:14:16 | [diff] [blame] | 91 | FakeGCMProfileService::CustomFakeGCMDriver::CustomFakeGCMDriver() = default; |
[email protected] | 2508c65 | 2013-11-27 01:49:50 | [diff] [blame] | 92 | |
Rushan Suleymanov | 79aba4e | 2023-01-23 18:14:16 | [diff] [blame] | 93 | FakeGCMProfileService::CustomFakeGCMDriver::~CustomFakeGCMDriver() = default; |
[email protected] | 2508c65 | 2013-11-27 01:49:50 | [diff] [blame] | 94 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 95 | void FakeGCMProfileService::CustomFakeGCMDriver::RegisterImpl( |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 96 | const std::string& app_id, |
| 97 | const std::vector<std::string>& sender_ids) { |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 98 | if (service_->is_offline_) |
| 99 | return; // Drop request. |
[email protected] | 2508c65 | 2013-11-27 01:49:50 | [diff] [blame] | 100 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 101 | // Generate fake registration IDs, encoding the number of sender IDs (used by |
| 102 | // GcmApiTest.RegisterValidation), then an incrementing count (even for the |
| 103 | // same app_id - there's no caching) so tests can distinguish registrations. |
| 104 | std::string registration_id = base::StringPrintf( |
| 105 | "%" PRIuS "-%d", sender_ids.size(), registration_count_); |
| 106 | ++registration_count_; |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 107 | |
Sean Maher | 5b9af51f | 2022-11-21 15:32:47 | [diff] [blame] | 108 | base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
tzik | 29ea5c7 | 2017-04-20 02:16:51 | [diff] [blame] | 109 | FROM_HERE, base::BindOnce(&CustomFakeGCMDriver::DoRegister, |
| 110 | weak_factory_.GetWeakPtr(), app_id, sender_ids, |
| 111 | registration_id)); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 112 | } |
| 113 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 114 | void FakeGCMProfileService::CustomFakeGCMDriver::DoRegister( |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 115 | const std::string& app_id, |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 116 | const std::vector<std::string>& sender_ids, |
| 117 | const std::string& registration_id) { |
| 118 | if (service_->collect_) { |
| 119 | service_->last_registered_app_id_ = app_id; |
| 120 | service_->last_registered_sender_ids_ = sender_ids; |
| 121 | } |
| 122 | RegisterFinished(app_id, registration_id, GCMClient::SUCCESS); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 123 | } |
peter | c5634e8f | 2015-12-02 15:34:59 | [diff] [blame] | 124 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 125 | void FakeGCMProfileService::CustomFakeGCMDriver::UnregisterImpl( |
| 126 | const std::string& app_id) { |
| 127 | if (service_->is_offline_) |
| 128 | return; // Drop request. |
| 129 | |
| 130 | GCMClient::Result result = GCMClient::SUCCESS; |
| 131 | if (!service_->unregister_responses_.empty()) { |
| 132 | result = service_->unregister_responses_.front(); |
| 133 | service_->unregister_responses_.pop_front(); |
| 134 | } |
Sean Maher | 5b9af51f | 2022-11-21 15:32:47 | [diff] [blame] | 135 | base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
tzik | 29ea5c7 | 2017-04-20 02:16:51 | [diff] [blame] | 136 | FROM_HERE, base::BindOnce(&CustomFakeGCMDriver::UnregisterFinished, |
| 137 | weak_factory_.GetWeakPtr(), app_id, result)); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 138 | } |
peter | c5634e8f | 2015-12-02 15:34:59 | [diff] [blame] | 139 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 140 | void FakeGCMProfileService::CustomFakeGCMDriver::UnregisterWithSenderIdImpl( |
| 141 | const std::string& app_id, |
| 142 | const std::string& sender_id) { |
| 143 | NOTREACHED() << "This Android-specific method is not yet faked."; |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 144 | } |
| 145 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 146 | void FakeGCMProfileService::CustomFakeGCMDriver::SendImpl( |
| 147 | const std::string& app_id, |
| 148 | const std::string& receiver_id, |
| 149 | const OutgoingMessage& message) { |
| 150 | if (service_->is_offline_) |
| 151 | return; // Drop request. |
| 152 | |
Sean Maher | 5b9af51f | 2022-11-21 15:32:47 | [diff] [blame] | 153 | base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 154 | FROM_HERE, |
tzik | 29ea5c7 | 2017-04-20 02:16:51 | [diff] [blame] | 155 | base::BindOnce(&CustomFakeGCMDriver::DoSend, weak_factory_.GetWeakPtr(), |
| 156 | app_id, receiver_id, message)); |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 157 | } |
| 158 | |
Alex Chau | da2073d6 | 2020-01-29 11:26:08 | [diff] [blame] | 159 | void FakeGCMProfileService::CustomFakeGCMDriver::EncryptMessage( |
Himanshu Jaju | 3dc84e8 | 2019-08-01 16:46:01 | [diff] [blame] | 160 | const std::string& app_id, |
| 161 | const std::string& authorized_entity, |
| 162 | const std::string& p256dh, |
| 163 | const std::string& auth_secret, |
Alex Chau | da2073d6 | 2020-01-29 11:26:08 | [diff] [blame] | 164 | const std::string& message, |
| 165 | EncryptMessageCallback callback) { |
| 166 | // Pretend that message has been encrypted. |
| 167 | std::move(callback).Run(GCMEncryptionResult::ENCRYPTED_DRAFT_08, message); |
Himanshu Jaju | 3dc84e8 | 2019-08-01 16:46:01 | [diff] [blame] | 168 | } |
| 169 | |
Rushan Suleymanov | 79aba4e | 2023-01-23 18:14:16 | [diff] [blame] | 170 | void FakeGCMProfileService::CustomFakeGCMDriver::SetService( |
| 171 | FakeGCMProfileService* service) { |
| 172 | service_ = service; |
| 173 | } |
| 174 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 175 | void FakeGCMProfileService::CustomFakeGCMDriver::DoSend( |
| 176 | const std::string& app_id, |
| 177 | const std::string& receiver_id, |
| 178 | const OutgoingMessage& message) { |
| 179 | if (service_->collect_) { |
| 180 | service_->last_sent_message_ = message; |
| 181 | service_->last_receiver_id_ = receiver_id; |
| 182 | } |
| 183 | SendFinished(app_id, message.id, GCMClient::SUCCESS); |
| 184 | } |
| 185 | |
| 186 | void FakeGCMProfileService::CustomFakeGCMDriver::GetToken( |
| 187 | const std::string& app_id, |
| 188 | const std::string& authorized_entity, |
| 189 | const std::string& scope, |
Maksim Moskvitin | 8b44898 | 2020-02-06 13:23:19 | [diff] [blame] | 190 | base::TimeDelta time_to_live, |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 191 | GetTokenCallback callback) { |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 192 | if (service_->is_offline_) |
| 193 | return; // Drop request. |
| 194 | |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 195 | instance_id::FakeGCMDriverForInstanceID::GetToken( |
Richard Knoll | 943b81a | 2020-12-10 12:07:28 | [diff] [blame] | 196 | app_id, authorized_entity, scope, time_to_live, std::move(callback)); |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void FakeGCMProfileService::CustomFakeGCMDriver::DeleteToken( |
| 200 | const std::string& app_id, |
| 201 | const std::string& authorized_entity, |
| 202 | const std::string& scope, |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 203 | DeleteTokenCallback callback) { |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 204 | if (service_->is_offline_) |
| 205 | return; // Drop request. |
| 206 | |
| 207 | instance_id::FakeGCMDriverForInstanceID::DeleteToken( |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 208 | app_id, authorized_entity, scope, std::move(callback)); |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 209 | } |
| 210 | |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 211 | // static |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 212 | std::unique_ptr<KeyedService> FakeGCMProfileService::Build( |
isherman | 30fa851a | 2015-06-09 23:32:10 | [diff] [blame] | 213 | content::BrowserContext* context) { |
Rushan Suleymanov | 79aba4e | 2023-01-23 18:14:16 | [diff] [blame] | 214 | auto custom_driver = std::make_unique<CustomFakeGCMDriver>(); |
| 215 | CustomFakeGCMDriver* custom_driver_ptr = custom_driver.get(); |
Rushan Suleymanov | 9b9b88b6 | 2023-01-20 13:40:45 | [diff] [blame] | 216 | |
Rushan Suleymanov | 79aba4e | 2023-01-23 18:14:16 | [diff] [blame] | 217 | std::unique_ptr<FakeGCMProfileService> service = |
| 218 | std::make_unique<FakeGCMProfileService>(std::move(custom_driver)); |
| 219 | |
| 220 | custom_driver_ptr->SetService(service.get()); |
Peter Beverloo | 3413946 | 2018-04-10 14:18:06 | [diff] [blame] | 221 | return service; |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 222 | } |
| 223 | |
Rushan Suleymanov | 79aba4e | 2023-01-23 18:14:16 | [diff] [blame] | 224 | FakeGCMProfileService::FakeGCMProfileService( |
| 225 | std::unique_ptr<instance_id::FakeGCMDriverForInstanceID> fake_gcm_driver) |
| 226 | : GCMProfileService(std::move(fake_gcm_driver)) {} |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 227 | |
Peter Beverloo | 3413946 | 2018-04-10 14:18:06 | [diff] [blame] | 228 | FakeGCMProfileService::~FakeGCMProfileService() = default; |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 229 | |
[email protected] | 0e88e1d1 | 2014-03-19 06:53:08 | [diff] [blame] | 230 | void FakeGCMProfileService::AddExpectedUnregisterResponse( |
| 231 | GCMClient::Result result) { |
| 232 | unregister_responses_.push_back(result); |
| 233 | } |
| 234 | |
peter | c5634e8f | 2015-12-02 15:34:59 | [diff] [blame] | 235 | void FakeGCMProfileService::DispatchMessage(const std::string& app_id, |
| 236 | const IncomingMessage& message) { |
Rushan Suleymanov | 79aba4e | 2023-01-23 18:14:16 | [diff] [blame] | 237 | GetFakeGCMDriver()->DispatchMessage(app_id, message); |
| 238 | } |
| 239 | |
| 240 | instance_id::FakeGCMDriverForInstanceID* |
| 241 | FakeGCMProfileService::GetFakeGCMDriver() { |
| 242 | return static_cast<instance_id::FakeGCMDriverForInstanceID*>(driver()); |
peter | c5634e8f | 2015-12-02 15:34:59 | [diff] [blame] | 243 | } |
| 244 | |
[email protected] | 2508c65 | 2013-11-27 01:49:50 | [diff] [blame] | 245 | } // namespace gcm |