[email protected] | 2508c65 | 2013-11-27 01:49:50 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 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 | |
[email protected] | 2508c65 | 2013-11-27 01:49:50 | [diff] [blame] | 9 | #include "base/bind.h" |
johnme | 29022c5 | 2014-12-11 16:26:30 | [diff] [blame] | 10 | #include "base/format_macros.h" |
skyostil | 0259835 | 2015-06-12 12:37:25 | [diff] [blame] | 11 | #include "base/location.h" |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 12 | #include "base/macros.h" |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
skyostil | 0259835 | 2015-06-12 12:37:25 | [diff] [blame] | 14 | #include "base/single_thread_task_runner.h" |
[email protected] | 2508c65 | 2013-11-27 01:49:50 | [diff] [blame] | 15 | #include "base/strings/string_number_conversions.h" |
johnme | 29022c5 | 2014-12-11 16:26:30 | [diff] [blame] | 16 | #include "base/strings/stringprintf.h" |
gab | b15e1907 | 2016-05-11 20:45:41 | [diff] [blame] | 17 | #include "base/threading/thread_task_runner_handle.h" |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 18 | #include "base/time/time.h" |
[email protected] | 8d41e5a | 2014-05-28 03:18:49 | [diff] [blame] | 19 | #include "components/gcm_driver/fake_gcm_client_factory.h" |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 20 | #include "components/gcm_driver/gcm_driver.h" |
johnme | a504573 | 2016-09-08 17:23:29 | [diff] [blame] | 21 | #include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h" |
[email protected] | 2508c65 | 2013-11-27 01:49:50 | [diff] [blame] | 22 | |
| 23 | namespace gcm { |
| 24 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 25 | class FakeGCMProfileService::CustomFakeGCMDriver |
| 26 | : public instance_id::FakeGCMDriverForInstanceID { |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 27 | public: |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 28 | explicit CustomFakeGCMDriver(FakeGCMProfileService* service); |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 29 | ~CustomFakeGCMDriver() override; |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 30 | |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 31 | void OnRegisterFinished(const std::string& app_id, |
| 32 | const std::string& registration_id, |
| 33 | GCMClient::Result result); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 34 | void OnSendFinished(const std::string& app_id, |
| 35 | const std::string& message_id, |
| 36 | GCMClient::Result result); |
| 37 | |
peter | c5634e8f | 2015-12-02 15:34:59 | [diff] [blame] | 38 | void OnDispatchMessage(const std::string& app_id, |
| 39 | const IncomingMessage& message); |
| 40 | |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 41 | protected: |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 42 | // FakeGCMDriver overrides: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 43 | void RegisterImpl(const std::string& app_id, |
| 44 | const std::vector<std::string>& sender_ids) override; |
| 45 | void UnregisterImpl(const std::string& app_id) override; |
peter | c5634e8f | 2015-12-02 15:34:59 | [diff] [blame] | 46 | void UnregisterWithSenderIdImpl(const std::string& app_id, |
| 47 | const std::string& sender_id) override; |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 48 | void SendImpl(const std::string& app_id, |
| 49 | const std::string& receiver_id, |
mvanouwerkerk | f8633deb | 2015-07-13 11:04:06 | [diff] [blame] | 50 | const OutgoingMessage& message) override; |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 51 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 52 | // FakeGCMDriverForInstanceID overrides: |
| 53 | void GetToken(const std::string& app_id, |
| 54 | const std::string& authorized_entity, |
| 55 | const std::string& scope, |
| 56 | const std::map<std::string, std::string>& options, |
| 57 | const GetTokenCallback& callback) override; |
| 58 | void DeleteToken(const std::string& app_id, |
| 59 | const std::string& authorized_entity, |
| 60 | const std::string& scope, |
| 61 | const DeleteTokenCallback& callback) override; |
| 62 | |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 63 | private: |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 64 | void DoRegister(const std::string& app_id, |
| 65 | const std::vector<std::string>& sender_ids, |
| 66 | const std::string& registration_id); |
| 67 | void DoSend(const std::string& app_id, |
| 68 | const std::string& receiver_id, |
| 69 | const OutgoingMessage& message); |
| 70 | |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 71 | FakeGCMProfileService* service_; |
| 72 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 73 | // Used to give each registration a unique registration id. Does not decrease |
| 74 | // when unregister is called. |
| 75 | int registration_count_ = 0; |
| 76 | |
| 77 | base::WeakPtrFactory<CustomFakeGCMDriver> weak_factory_; // Must be last. |
| 78 | |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 79 | DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver); |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 80 | }; |
| 81 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 82 | FakeGCMProfileService::CustomFakeGCMDriver::CustomFakeGCMDriver( |
| 83 | FakeGCMProfileService* service) |
johnme | a504573 | 2016-09-08 17:23:29 | [diff] [blame] | 84 | : instance_id::FakeGCMDriverForInstanceID( |
| 85 | base::ThreadTaskRunnerHandle::Get()), |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 86 | service_(service), |
| 87 | weak_factory_(this) {} |
[email protected] | 2508c65 | 2013-11-27 01:49:50 | [diff] [blame] | 88 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 89 | FakeGCMProfileService::CustomFakeGCMDriver::~CustomFakeGCMDriver() {} |
[email protected] | 2508c65 | 2013-11-27 01:49:50 | [diff] [blame] | 90 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 91 | void FakeGCMProfileService::CustomFakeGCMDriver::RegisterImpl( |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 92 | const std::string& app_id, |
| 93 | const std::vector<std::string>& sender_ids) { |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 94 | if (service_->is_offline_) |
| 95 | return; // Drop request. |
[email protected] | 2508c65 | 2013-11-27 01:49:50 | [diff] [blame] | 96 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 97 | // Generate fake registration IDs, encoding the number of sender IDs (used by |
| 98 | // GcmApiTest.RegisterValidation), then an incrementing count (even for the |
| 99 | // same app_id - there's no caching) so tests can distinguish registrations. |
| 100 | std::string registration_id = base::StringPrintf( |
| 101 | "%" PRIuS "-%d", sender_ids.size(), registration_count_); |
| 102 | ++registration_count_; |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 103 | |
skyostil | 0259835 | 2015-06-12 12:37:25 | [diff] [blame] | 104 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
tzik | 29ea5c7 | 2017-04-20 02:16:51 | [diff] [blame] | 105 | FROM_HERE, base::BindOnce(&CustomFakeGCMDriver::DoRegister, |
| 106 | weak_factory_.GetWeakPtr(), app_id, sender_ids, |
| 107 | registration_id)); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 108 | } |
| 109 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 110 | void FakeGCMProfileService::CustomFakeGCMDriver::DoRegister( |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 111 | const std::string& app_id, |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 112 | const std::vector<std::string>& sender_ids, |
| 113 | const std::string& registration_id) { |
| 114 | if (service_->collect_) { |
| 115 | service_->last_registered_app_id_ = app_id; |
| 116 | service_->last_registered_sender_ids_ = sender_ids; |
| 117 | } |
| 118 | RegisterFinished(app_id, registration_id, GCMClient::SUCCESS); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 119 | } |
peter | c5634e8f | 2015-12-02 15:34:59 | [diff] [blame] | 120 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 121 | void FakeGCMProfileService::CustomFakeGCMDriver::UnregisterImpl( |
| 122 | const std::string& app_id) { |
| 123 | if (service_->is_offline_) |
| 124 | return; // Drop request. |
| 125 | |
| 126 | GCMClient::Result result = GCMClient::SUCCESS; |
| 127 | if (!service_->unregister_responses_.empty()) { |
| 128 | result = service_->unregister_responses_.front(); |
| 129 | service_->unregister_responses_.pop_front(); |
| 130 | } |
| 131 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
tzik | 29ea5c7 | 2017-04-20 02:16:51 | [diff] [blame] | 132 | FROM_HERE, base::BindOnce(&CustomFakeGCMDriver::UnregisterFinished, |
| 133 | weak_factory_.GetWeakPtr(), app_id, result)); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 134 | } |
peter | c5634e8f | 2015-12-02 15:34:59 | [diff] [blame] | 135 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 136 | void FakeGCMProfileService::CustomFakeGCMDriver::UnregisterWithSenderIdImpl( |
| 137 | const std::string& app_id, |
| 138 | const std::string& sender_id) { |
| 139 | NOTREACHED() << "This Android-specific method is not yet faked."; |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 140 | } |
| 141 | |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 142 | void FakeGCMProfileService::CustomFakeGCMDriver::SendImpl( |
| 143 | const std::string& app_id, |
| 144 | const std::string& receiver_id, |
| 145 | const OutgoingMessage& message) { |
| 146 | if (service_->is_offline_) |
| 147 | return; // Drop request. |
| 148 | |
| 149 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 150 | FROM_HERE, |
tzik | 29ea5c7 | 2017-04-20 02:16:51 | [diff] [blame] | 151 | base::BindOnce(&CustomFakeGCMDriver::DoSend, weak_factory_.GetWeakPtr(), |
| 152 | app_id, receiver_id, message)); |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void FakeGCMProfileService::CustomFakeGCMDriver::DoSend( |
| 156 | const std::string& app_id, |
| 157 | const std::string& receiver_id, |
| 158 | const OutgoingMessage& message) { |
| 159 | if (service_->collect_) { |
| 160 | service_->last_sent_message_ = message; |
| 161 | service_->last_receiver_id_ = receiver_id; |
| 162 | } |
| 163 | SendFinished(app_id, message.id, GCMClient::SUCCESS); |
| 164 | } |
| 165 | |
| 166 | void FakeGCMProfileService::CustomFakeGCMDriver::GetToken( |
| 167 | const std::string& app_id, |
| 168 | const std::string& authorized_entity, |
| 169 | const std::string& scope, |
| 170 | const std::map<std::string, std::string>& options, |
| 171 | const GetTokenCallback& callback) { |
| 172 | if (service_->is_offline_) |
| 173 | return; // Drop request. |
| 174 | |
| 175 | instance_id::FakeGCMDriverForInstanceID::GetToken(app_id, authorized_entity, |
| 176 | scope, options, callback); |
| 177 | } |
| 178 | |
| 179 | void FakeGCMProfileService::CustomFakeGCMDriver::DeleteToken( |
| 180 | const std::string& app_id, |
| 181 | const std::string& authorized_entity, |
| 182 | const std::string& scope, |
| 183 | const DeleteTokenCallback& callback) { |
| 184 | if (service_->is_offline_) |
| 185 | return; // Drop request. |
| 186 | |
| 187 | instance_id::FakeGCMDriverForInstanceID::DeleteToken( |
| 188 | app_id, authorized_entity, scope, callback); |
| 189 | } |
| 190 | |
| 191 | void FakeGCMProfileService::CustomFakeGCMDriver::OnDispatchMessage( |
| 192 | const std::string& app_id, |
| 193 | const IncomingMessage& message) { |
peter | c5634e8f | 2015-12-02 15:34:59 | [diff] [blame] | 194 | DispatchMessage(app_id, message); |
| 195 | } |
| 196 | |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 197 | // static |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 198 | std::unique_ptr<KeyedService> FakeGCMProfileService::Build( |
isherman | 30fa851a | 2015-06-09 23:32:10 | [diff] [blame] | 199 | content::BrowserContext* context) { |
Peter Beverloo | 3413946 | 2018-04-10 14:18:06 | [diff] [blame] | 200 | std::unique_ptr<FakeGCMProfileService> service = |
| 201 | std::make_unique<FakeGCMProfileService>(); |
| 202 | service->SetDriverForTesting( |
| 203 | std::make_unique<CustomFakeGCMDriver>(service.get())); |
| 204 | |
| 205 | return service; |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 206 | } |
| 207 | |
Peter Beverloo | 3413946 | 2018-04-10 14:18:06 | [diff] [blame] | 208 | FakeGCMProfileService::FakeGCMProfileService() = default; |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 209 | |
Peter Beverloo | 3413946 | 2018-04-10 14:18:06 | [diff] [blame] | 210 | FakeGCMProfileService::~FakeGCMProfileService() = default; |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 211 | |
[email protected] | 0e88e1d1 | 2014-03-19 06:53:08 | [diff] [blame] | 212 | void FakeGCMProfileService::AddExpectedUnregisterResponse( |
| 213 | GCMClient::Result result) { |
| 214 | unregister_responses_.push_back(result); |
| 215 | } |
| 216 | |
peter | c5634e8f | 2015-12-02 15:34:59 | [diff] [blame] | 217 | void FakeGCMProfileService::DispatchMessage(const std::string& app_id, |
| 218 | const IncomingMessage& message) { |
| 219 | CustomFakeGCMDriver* custom_driver = |
| 220 | static_cast<CustomFakeGCMDriver*>(driver()); |
| 221 | custom_driver->OnDispatchMessage(app_id, message); |
| 222 | } |
| 223 | |
[email protected] | 2508c65 | 2013-11-27 01:49:50 | [diff] [blame] | 224 | } // namespace gcm |