blob: 2a5de23dbd15975da994b58df614d6e73e305a86 [file] [log] [blame]
[email protected]2508c652013-11-27 01:49:501// 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 Beverloo34139462018-04-10 14:18:065#include "components/gcm_driver/fake_gcm_profile_service.h"
[email protected]2508c652013-11-27 01:49:506
dchenge73d8520c2015-12-27 01:19:097#include <utility>
8
[email protected]2508c652013-11-27 01:49:509#include "base/bind.h"
johnme29022c52014-12-11 16:26:3010#include "base/format_macros.h"
skyostil02598352015-06-12 12:37:2511#include "base/location.h"
avib896c712015-12-26 02:10:4312#include "base/macros.h"
johnmeef71bd02017-02-09 17:45:5613#include "base/memory/weak_ptr.h"
skyostil02598352015-06-12 12:37:2514#include "base/single_thread_task_runner.h"
[email protected]2508c652013-11-27 01:49:5015#include "base/strings/string_number_conversions.h"
johnme29022c52014-12-11 16:26:3016#include "base/strings/stringprintf.h"
gabb15e19072016-05-11 20:45:4117#include "base/threading/thread_task_runner_handle.h"
johnmeef71bd02017-02-09 17:45:5618#include "base/time/time.h"
[email protected]8d41e5a2014-05-28 03:18:4919#include "components/gcm_driver/fake_gcm_client_factory.h"
johnmeef71bd02017-02-09 17:45:5620#include "components/gcm_driver/gcm_driver.h"
johnmea5045732016-09-08 17:23:2921#include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h"
[email protected]2508c652013-11-27 01:49:5022
23namespace gcm {
24
johnmeef71bd02017-02-09 17:45:5625class FakeGCMProfileService::CustomFakeGCMDriver
26 : public instance_id::FakeGCMDriverForInstanceID {
[email protected]9d7e5c02014-05-21 03:09:0327 public:
[email protected]21b77652014-05-31 01:21:0928 explicit CustomFakeGCMDriver(FakeGCMProfileService* service);
Daniel Chenga542fca2014-10-21 09:51:2929 ~CustomFakeGCMDriver() override;
[email protected]9d7e5c02014-05-21 03:09:0330
[email protected]c27c10792014-06-05 15:27:2331 void OnRegisterFinished(const std::string& app_id,
32 const std::string& registration_id,
33 GCMClient::Result result);
[email protected]c27c10792014-06-05 15:27:2334 void OnSendFinished(const std::string& app_id,
35 const std::string& message_id,
36 GCMClient::Result result);
37
peterc5634e8f2015-12-02 15:34:5938 void OnDispatchMessage(const std::string& app_id,
39 const IncomingMessage& message);
40
[email protected]c27c10792014-06-05 15:27:2341 protected:
johnmeef71bd02017-02-09 17:45:5642 // FakeGCMDriver overrides:
Daniel Chenga542fca2014-10-21 09:51:2943 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;
peterc5634e8f2015-12-02 15:34:5946 void UnregisterWithSenderIdImpl(const std::string& app_id,
47 const std::string& sender_id) override;
Daniel Chenga542fca2014-10-21 09:51:2948 void SendImpl(const std::string& app_id,
49 const std::string& receiver_id,
mvanouwerkerkf8633deb2015-07-13 11:04:0650 const OutgoingMessage& message) override;
[email protected]9d7e5c02014-05-21 03:09:0351
johnmeef71bd02017-02-09 17:45:5652 // 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]9d7e5c02014-05-21 03:09:0363 private:
johnmeef71bd02017-02-09 17:45:5664 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]9d7e5c02014-05-21 03:09:0371 FakeGCMProfileService* service_;
72
johnmeef71bd02017-02-09 17:45:5673 // 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]21b77652014-05-31 01:21:0979 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver);
[email protected]9d7e5c02014-05-21 03:09:0380};
81
johnmeef71bd02017-02-09 17:45:5682FakeGCMProfileService::CustomFakeGCMDriver::CustomFakeGCMDriver(
83 FakeGCMProfileService* service)
johnmea5045732016-09-08 17:23:2984 : instance_id::FakeGCMDriverForInstanceID(
85 base::ThreadTaskRunnerHandle::Get()),
johnmeef71bd02017-02-09 17:45:5686 service_(service),
87 weak_factory_(this) {}
[email protected]2508c652013-11-27 01:49:5088
johnmeef71bd02017-02-09 17:45:5689FakeGCMProfileService::CustomFakeGCMDriver::~CustomFakeGCMDriver() {}
[email protected]2508c652013-11-27 01:49:5090
johnmeef71bd02017-02-09 17:45:5691void FakeGCMProfileService::CustomFakeGCMDriver::RegisterImpl(
[email protected]c27c10792014-06-05 15:27:2392 const std::string& app_id,
93 const std::vector<std::string>& sender_ids) {
johnmeef71bd02017-02-09 17:45:5694 if (service_->is_offline_)
95 return; // Drop request.
[email protected]2508c652013-11-27 01:49:5096
johnmeef71bd02017-02-09 17:45:5697 // 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]9d7e5c02014-05-21 03:09:03103
skyostil02598352015-06-12 12:37:25104 base::ThreadTaskRunnerHandle::Get()->PostTask(
tzik29ea5c72017-04-20 02:16:51105 FROM_HERE, base::BindOnce(&CustomFakeGCMDriver::DoRegister,
106 weak_factory_.GetWeakPtr(), app_id, sender_ids,
107 registration_id));
[email protected]c27c10792014-06-05 15:27:23108}
109
johnmeef71bd02017-02-09 17:45:56110void FakeGCMProfileService::CustomFakeGCMDriver::DoRegister(
[email protected]c27c10792014-06-05 15:27:23111 const std::string& app_id,
johnmeef71bd02017-02-09 17:45:56112 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]c27c10792014-06-05 15:27:23119}
peterc5634e8f2015-12-02 15:34:59120
johnmeef71bd02017-02-09 17:45:56121void 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(
tzik29ea5c72017-04-20 02:16:51132 FROM_HERE, base::BindOnce(&CustomFakeGCMDriver::UnregisterFinished,
133 weak_factory_.GetWeakPtr(), app_id, result));
[email protected]c27c10792014-06-05 15:27:23134}
peterc5634e8f2015-12-02 15:34:59135
johnmeef71bd02017-02-09 17:45:56136void 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]9d7e5c02014-05-21 03:09:03140}
141
johnmeef71bd02017-02-09 17:45:56142void 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,
tzik29ea5c72017-04-20 02:16:51151 base::BindOnce(&CustomFakeGCMDriver::DoSend, weak_factory_.GetWeakPtr(),
152 app_id, receiver_id, message));
johnmeef71bd02017-02-09 17:45:56153}
154
155void 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
166void 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
179void 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
191void FakeGCMProfileService::CustomFakeGCMDriver::OnDispatchMessage(
192 const std::string& app_id,
193 const IncomingMessage& message) {
peterc5634e8f2015-12-02 15:34:59194 DispatchMessage(app_id, message);
195}
196
[email protected]9d7e5c02014-05-21 03:09:03197// static
dcheng4af48582016-04-19 00:29:35198std::unique_ptr<KeyedService> FakeGCMProfileService::Build(
isherman30fa851a2015-06-09 23:32:10199 content::BrowserContext* context) {
Peter Beverloo34139462018-04-10 14:18:06200 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]9d7e5c02014-05-21 03:09:03206}
207
Peter Beverloo34139462018-04-10 14:18:06208FakeGCMProfileService::FakeGCMProfileService() = default;
[email protected]9d7e5c02014-05-21 03:09:03209
Peter Beverloo34139462018-04-10 14:18:06210FakeGCMProfileService::~FakeGCMProfileService() = default;
[email protected]9d7e5c02014-05-21 03:09:03211
[email protected]0e88e1d12014-03-19 06:53:08212void FakeGCMProfileService::AddExpectedUnregisterResponse(
213 GCMClient::Result result) {
214 unregister_responses_.push_back(result);
215}
216
peterc5634e8f2015-12-02 15:34:59217void 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]2508c652013-11-27 01:49:50224} // namespace gcm