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