blob: fdf636f2217dbbb714ca3597b262515ee26c457a [file] [log] [blame]
[email protected]accbbc92014-05-15 21:28:591// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]b16a7c52013-11-20 01:18:592// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]8d41e5a2014-05-28 03:18:495#include "components/gcm_driver/fake_gcm_client.h"
[email protected]b16a7c52013-11-20 01:18:596
7#include "base/bind.h"
8#include "base/logging.h"
9#include "base/message_loop/message_loop.h"
[email protected]1795df51a2014-05-22 20:18:0410#include "base/sequenced_task_runner.h"
jianli15aecc152014-10-07 03:03:5311#include "base/strings/string_number_conversions.h"
[email protected]b16a7c52013-11-20 01:18:5912#include "base/sys_byteorder.h"
13#include "base/time/time.h"
[email protected]446f73c22014-05-14 20:47:1814#include "google_apis/gcm/base/encryptor.h"
fgorskid578c18b2014-09-24 23:40:1715#include "google_apis/gcm/engine/account_mapping.h"
[email protected]fc6078a2014-06-14 08:28:4316#include "net/base/ip_endpoint.h"
[email protected]b16a7c52013-11-20 01:18:5917
18namespace gcm {
19
[email protected]1795df51a2014-05-22 20:18:0420FakeGCMClient::FakeGCMClient(
21 StartMode start_mode,
22 const scoped_refptr<base::SequencedTaskRunner>& ui_thread,
23 const scoped_refptr<base::SequencedTaskRunner>& io_thread)
[email protected]5799d052014-02-12 20:47:3924 : delegate_(NULL),
jianli15aecc152014-10-07 03:03:5325 sequence_id_(0),
[email protected]d3a4b2e2014-02-27 13:46:5426 status_(UNINITIALIZED),
[email protected]fc767e42014-05-13 05:02:1427 start_mode_(start_mode),
[email protected]1795df51a2014-05-22 20:18:0428 ui_thread_(ui_thread),
29 io_thread_(io_thread),
[email protected]d3a4b2e2014-02-27 13:46:5430 weak_ptr_factory_(this) {
[email protected]b16a7c52013-11-20 01:18:5931}
32
[email protected]accbbc92014-05-15 21:28:5933FakeGCMClient::~FakeGCMClient() {
[email protected]b16a7c52013-11-20 01:18:5934}
35
[email protected]accbbc92014-05-15 21:28:5936void FakeGCMClient::Initialize(
[email protected]8ad80512014-05-23 09:40:4737 const ChromeBuildInfo& chrome_build_info,
[email protected]e2a4a8012014-02-07 22:32:5238 const base::FilePath& store_path,
39 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
40 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:3941 url_request_context_getter,
[email protected]446f73c22014-05-14 20:47:1842 scoped_ptr<Encryptor> encryptor,
[email protected]5799d052014-02-12 20:47:3943 Delegate* delegate) {
44 delegate_ = delegate;
[email protected]e2a4a8012014-02-07 22:32:5245}
46
[email protected]accbbc92014-05-15 21:28:5947void FakeGCMClient::Start() {
[email protected]1795df51a2014-05-22 20:18:0448 DCHECK(io_thread_->RunsTasksOnCurrentThread());
[email protected]fc767e42014-05-13 05:02:1449 DCHECK_NE(STARTED, status_);
[email protected]d3a4b2e2014-02-27 13:46:5450
[email protected]fc767e42014-05-13 05:02:1451 if (start_mode_ == DELAY_START)
[email protected]d3a4b2e2014-02-27 13:46:5452 return;
53 DoLoading();
54}
55
[email protected]accbbc92014-05-15 21:28:5956void FakeGCMClient::DoLoading() {
[email protected]fc767e42014-05-13 05:02:1457 status_ = STARTED;
[email protected]d3a4b2e2014-02-27 13:46:5458 base::MessageLoop::current()->PostTask(
59 FROM_HERE,
[email protected]accbbc92014-05-15 21:28:5960 base::Bind(&FakeGCMClient::CheckinFinished,
[email protected]d3a4b2e2014-02-27 13:46:5461 weak_ptr_factory_.GetWeakPtr()));
62}
63
[email protected]accbbc92014-05-15 21:28:5964void FakeGCMClient::Stop() {
[email protected]1795df51a2014-05-22 20:18:0465 DCHECK(io_thread_->RunsTasksOnCurrentThread());
[email protected]21fee5482014-03-05 00:57:1566 status_ = STOPPED;
[email protected]fc6078a2014-06-14 08:28:4367 delegate_->OnDisconnected();
[email protected]21fee5482014-03-05 00:57:1568}
69
[email protected]accbbc92014-05-15 21:28:5970void FakeGCMClient::CheckOut() {
[email protected]1795df51a2014-05-22 20:18:0471 DCHECK(io_thread_->RunsTasksOnCurrentThread());
[email protected]d3a4b2e2014-02-27 13:46:5472 status_ = CHECKED_OUT;
jianli15aecc152014-10-07 03:03:5373 sequence_id_++;
[email protected]1b1c3cd2013-12-17 18:40:0474}
75
[email protected]accbbc92014-05-15 21:28:5976void FakeGCMClient::Register(const std::string& app_id,
[email protected]b16a7c52013-11-20 01:18:5977 const std::vector<std::string>& sender_ids) {
[email protected]1795df51a2014-05-22 20:18:0478 DCHECK(io_thread_->RunsTasksOnCurrentThread());
[email protected]b16a7c52013-11-20 01:18:5979
[email protected]2ac46732014-04-03 23:14:3180 std::string registration_id = GetRegistrationIdFromSenderIds(sender_ids);
[email protected]b16a7c52013-11-20 01:18:5981 base::MessageLoop::current()->PostTask(
82 FROM_HERE,
[email protected]accbbc92014-05-15 21:28:5983 base::Bind(&FakeGCMClient::RegisterFinished,
[email protected]d3a4b2e2014-02-27 13:46:5484 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:5985 app_id,
86 registration_id));
87}
88
[email protected]accbbc92014-05-15 21:28:5989void FakeGCMClient::Unregister(const std::string& app_id) {
[email protected]1795df51a2014-05-22 20:18:0490 DCHECK(io_thread_->RunsTasksOnCurrentThread());
[email protected]0e88e1d12014-03-19 06:53:0891
92 base::MessageLoop::current()->PostTask(
93 FROM_HERE,
[email protected]accbbc92014-05-15 21:28:5994 base::Bind(&FakeGCMClient::UnregisterFinished,
[email protected]0e88e1d12014-03-19 06:53:0895 weak_ptr_factory_.GetWeakPtr(),
96 app_id));
[email protected]b16a7c52013-11-20 01:18:5997}
98
[email protected]accbbc92014-05-15 21:28:5999void FakeGCMClient::Send(const std::string& app_id,
[email protected]b16a7c52013-11-20 01:18:59100 const std::string& receiver_id,
101 const OutgoingMessage& message) {
[email protected]1795df51a2014-05-22 20:18:04102 DCHECK(io_thread_->RunsTasksOnCurrentThread());
[email protected]b16a7c52013-11-20 01:18:59103
104 base::MessageLoop::current()->PostTask(
105 FROM_HERE,
[email protected]accbbc92014-05-15 21:28:59106 base::Bind(&FakeGCMClient::SendFinished,
[email protected]d3a4b2e2014-02-27 13:46:54107 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:59108 app_id,
[email protected]c6fe36b2014-03-11 10:58:12109 message));
[email protected]b16a7c52013-11-20 01:18:59110}
111
[email protected]accbbc92014-05-15 21:28:59112void FakeGCMClient::SetRecording(bool recording) {
[email protected]436bcb82014-04-18 00:40:57113}
114
[email protected]accbbc92014-05-15 21:28:59115void FakeGCMClient::ClearActivityLogs() {
[email protected]436bcb82014-04-18 00:40:57116}
117
[email protected]accbbc92014-05-15 21:28:59118GCMClient::GCMStatistics FakeGCMClient::GetStatistics() const {
[email protected]35601812014-03-07 19:52:43119 return GCMClient::GCMStatistics();
120}
121
fgorski58b9dfc2014-09-29 16:46:18122void FakeGCMClient::SetAccountTokens(
123 const std::vector<AccountTokenInfo>& account_tokens) {
[email protected]7df5ef22014-07-17 07:35:58124}
125
[email protected]72d4f252014-08-20 22:34:28126void FakeGCMClient::UpdateAccountMapping(
127 const AccountMapping& account_mapping) {
128}
129
130void FakeGCMClient::RemoveAccountMapping(const std::string& account_id) {
131}
132
fgorski5df101702014-10-28 02:09:31133void FakeGCMClient::SetLastTokenFetchTime(const base::Time& time) {
134}
135
[email protected]accbbc92014-05-15 21:28:59136void FakeGCMClient::PerformDelayedLoading() {
[email protected]1795df51a2014-05-22 20:18:04137 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
[email protected]d3a4b2e2014-02-27 13:46:54138
[email protected]1795df51a2014-05-22 20:18:04139 io_thread_->PostTask(
[email protected]d3a4b2e2014-02-27 13:46:54140 FROM_HERE,
[email protected]accbbc92014-05-15 21:28:59141 base::Bind(&FakeGCMClient::DoLoading, weak_ptr_factory_.GetWeakPtr()));
[email protected]b16a7c52013-11-20 01:18:59142}
143
[email protected]accbbc92014-05-15 21:28:59144void FakeGCMClient::ReceiveMessage(const std::string& app_id,
[email protected]b16a7c52013-11-20 01:18:59145 const IncomingMessage& message) {
[email protected]1795df51a2014-05-22 20:18:04146 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
[email protected]b16a7c52013-11-20 01:18:59147
[email protected]1795df51a2014-05-22 20:18:04148 io_thread_->PostTask(
[email protected]b16a7c52013-11-20 01:18:59149 FROM_HERE,
[email protected]accbbc92014-05-15 21:28:59150 base::Bind(&FakeGCMClient::MessageReceived,
[email protected]d3a4b2e2014-02-27 13:46:54151 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:59152 app_id,
153 message));
154}
155
[email protected]accbbc92014-05-15 21:28:59156void FakeGCMClient::DeleteMessages(const std::string& app_id) {
[email protected]1795df51a2014-05-22 20:18:04157 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
[email protected]b16a7c52013-11-20 01:18:59158
[email protected]1795df51a2014-05-22 20:18:04159 io_thread_->PostTask(
[email protected]b16a7c52013-11-20 01:18:59160 FROM_HERE,
[email protected]accbbc92014-05-15 21:28:59161 base::Bind(&FakeGCMClient::MessagesDeleted,
[email protected]d3a4b2e2014-02-27 13:46:54162 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:59163 app_id));
164}
165
[email protected]accbbc92014-05-15 21:28:59166std::string FakeGCMClient::GetRegistrationIdFromSenderIds(
jianli15aecc152014-10-07 03:03:53167 const std::vector<std::string>& sender_ids) const {
[email protected]df84c532014-04-25 15:36:54168 // GCMService normalizes the sender IDs by making them sorted.
[email protected]c7f7b532014-01-24 07:24:45169 std::vector<std::string> normalized_sender_ids = sender_ids;
170 std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end());
171
[email protected]b16a7c52013-11-20 01:18:59172 // Simulate the registration_id by concaternating all sender IDs.
173 // Set registration_id to empty to denote an error if sender_ids contains a
174 // hint.
175 std::string registration_id;
176 if (sender_ids.size() != 1 ||
177 sender_ids[0].find("error") == std::string::npos) {
[email protected]c7f7b532014-01-24 07:24:45178 for (size_t i = 0; i < normalized_sender_ids.size(); ++i) {
[email protected]b16a7c52013-11-20 01:18:59179 if (i > 0)
180 registration_id += ",";
[email protected]c7f7b532014-01-24 07:24:45181 registration_id += normalized_sender_ids[i];
[email protected]b16a7c52013-11-20 01:18:59182 }
jianli15aecc152014-10-07 03:03:53183 registration_id += base::IntToString(sequence_id_);
[email protected]b16a7c52013-11-20 01:18:59184 }
185 return registration_id;
186}
187
[email protected]accbbc92014-05-15 21:28:59188void FakeGCMClient::CheckinFinished() {
fgorski5df101702014-10-28 02:09:31189 delegate_->OnGCMReady(std::vector<AccountMapping>(), base::Time());
[email protected]fc6078a2014-06-14 08:28:43190 delegate_->OnConnected(net::IPEndPoint());
[email protected]d3a4b2e2014-02-27 13:46:54191}
192
[email protected]accbbc92014-05-15 21:28:59193void FakeGCMClient::RegisterFinished(const std::string& app_id,
[email protected]5799d052014-02-12 20:47:39194 const std::string& registrion_id) {
195 delegate_->OnRegisterFinished(
[email protected]b16a7c52013-11-20 01:18:59196 app_id, registrion_id, registrion_id.empty() ? SERVER_ERROR : SUCCESS);
197}
198
[email protected]accbbc92014-05-15 21:28:59199void FakeGCMClient::UnregisterFinished(const std::string& app_id) {
[email protected]0e88e1d12014-03-19 06:53:08200 delegate_->OnUnregisterFinished(app_id, GCMClient::SUCCESS);
201}
202
[email protected]accbbc92014-05-15 21:28:59203void FakeGCMClient::SendFinished(const std::string& app_id,
[email protected]c6fe36b2014-03-11 10:58:12204 const OutgoingMessage& message) {
205 delegate_->OnSendFinished(app_id, message.id, SUCCESS);
[email protected]b16a7c52013-11-20 01:18:59206
207 // Simulate send error if message id contains a hint.
[email protected]c6fe36b2014-03-11 10:58:12208 if (message.id.find("error") != std::string::npos) {
209 SendErrorDetails send_error_details;
210 send_error_details.message_id = message.id;
211 send_error_details.result = NETWORK_ERROR;
212 send_error_details.additional_data = message.data;
[email protected]b16a7c52013-11-20 01:18:59213 base::MessageLoop::current()->PostDelayedTask(
214 FROM_HERE,
[email protected]accbbc92014-05-15 21:28:59215 base::Bind(&FakeGCMClient::MessageSendError,
[email protected]d3a4b2e2014-02-27 13:46:54216 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:59217 app_id,
[email protected]c6fe36b2014-03-11 10:58:12218 send_error_details),
[email protected]b16a7c52013-11-20 01:18:59219 base::TimeDelta::FromMilliseconds(200));
[email protected]292af2b22014-08-06 19:42:45220 } else if(message.id.find("ack") != std::string::npos) {
221 base::MessageLoop::current()->PostDelayedTask(
222 FROM_HERE,
223 base::Bind(&FakeGCMClient::SendAcknowledgement,
224 weak_ptr_factory_.GetWeakPtr(),
225 app_id,
226 message.id),
227 base::TimeDelta::FromMilliseconds(200));
228
[email protected]b16a7c52013-11-20 01:18:59229 }
230}
231
[email protected]accbbc92014-05-15 21:28:59232void FakeGCMClient::MessageReceived(const std::string& app_id,
[email protected]5799d052014-02-12 20:47:39233 const IncomingMessage& message) {
234 if (delegate_)
235 delegate_->OnMessageReceived(app_id, message);
[email protected]b16a7c52013-11-20 01:18:59236}
237
[email protected]accbbc92014-05-15 21:28:59238void FakeGCMClient::MessagesDeleted(const std::string& app_id) {
[email protected]5799d052014-02-12 20:47:39239 if (delegate_)
240 delegate_->OnMessagesDeleted(app_id);
[email protected]b16a7c52013-11-20 01:18:59241}
242
[email protected]accbbc92014-05-15 21:28:59243void FakeGCMClient::MessageSendError(
[email protected]c6fe36b2014-03-11 10:58:12244 const std::string& app_id,
245 const GCMClient::SendErrorDetails& send_error_details) {
[email protected]5799d052014-02-12 20:47:39246 if (delegate_)
[email protected]c6fe36b2014-03-11 10:58:12247 delegate_->OnMessageSendError(app_id, send_error_details);
[email protected]b16a7c52013-11-20 01:18:59248}
249
[email protected]292af2b22014-08-06 19:42:45250void FakeGCMClient::SendAcknowledgement(const std::string& app_id,
251 const std::string& message_id) {
252 if (delegate_)
253 delegate_->OnSendAcknowledged(app_id, message_id);
254}
255
[email protected]b16a7c52013-11-20 01:18:59256} // namespace gcm