blob: 605122249165ac171af3e5e52a8bb201073e7235 [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
[email protected]accbbc92014-05-15 21:28:59133void FakeGCMClient::PerformDelayedLoading() {
[email protected]1795df51a2014-05-22 20:18:04134 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
[email protected]d3a4b2e2014-02-27 13:46:54135
[email protected]1795df51a2014-05-22 20:18:04136 io_thread_->PostTask(
[email protected]d3a4b2e2014-02-27 13:46:54137 FROM_HERE,
[email protected]accbbc92014-05-15 21:28:59138 base::Bind(&FakeGCMClient::DoLoading, weak_ptr_factory_.GetWeakPtr()));
[email protected]b16a7c52013-11-20 01:18:59139}
140
[email protected]accbbc92014-05-15 21:28:59141void FakeGCMClient::ReceiveMessage(const std::string& app_id,
[email protected]b16a7c52013-11-20 01:18:59142 const IncomingMessage& message) {
[email protected]1795df51a2014-05-22 20:18:04143 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
[email protected]b16a7c52013-11-20 01:18:59144
[email protected]1795df51a2014-05-22 20:18:04145 io_thread_->PostTask(
[email protected]b16a7c52013-11-20 01:18:59146 FROM_HERE,
[email protected]accbbc92014-05-15 21:28:59147 base::Bind(&FakeGCMClient::MessageReceived,
[email protected]d3a4b2e2014-02-27 13:46:54148 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:59149 app_id,
150 message));
151}
152
[email protected]accbbc92014-05-15 21:28:59153void FakeGCMClient::DeleteMessages(const std::string& app_id) {
[email protected]1795df51a2014-05-22 20:18:04154 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
[email protected]b16a7c52013-11-20 01:18:59155
[email protected]1795df51a2014-05-22 20:18:04156 io_thread_->PostTask(
[email protected]b16a7c52013-11-20 01:18:59157 FROM_HERE,
[email protected]accbbc92014-05-15 21:28:59158 base::Bind(&FakeGCMClient::MessagesDeleted,
[email protected]d3a4b2e2014-02-27 13:46:54159 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:59160 app_id));
161}
162
[email protected]accbbc92014-05-15 21:28:59163std::string FakeGCMClient::GetRegistrationIdFromSenderIds(
jianli15aecc152014-10-07 03:03:53164 const std::vector<std::string>& sender_ids) const {
[email protected]df84c532014-04-25 15:36:54165 // GCMService normalizes the sender IDs by making them sorted.
[email protected]c7f7b532014-01-24 07:24:45166 std::vector<std::string> normalized_sender_ids = sender_ids;
167 std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end());
168
[email protected]b16a7c52013-11-20 01:18:59169 // Simulate the registration_id by concaternating all sender IDs.
170 // Set registration_id to empty to denote an error if sender_ids contains a
171 // hint.
172 std::string registration_id;
173 if (sender_ids.size() != 1 ||
174 sender_ids[0].find("error") == std::string::npos) {
[email protected]c7f7b532014-01-24 07:24:45175 for (size_t i = 0; i < normalized_sender_ids.size(); ++i) {
[email protected]b16a7c52013-11-20 01:18:59176 if (i > 0)
177 registration_id += ",";
[email protected]c7f7b532014-01-24 07:24:45178 registration_id += normalized_sender_ids[i];
[email protected]b16a7c52013-11-20 01:18:59179 }
jianli15aecc152014-10-07 03:03:53180 registration_id += base::IntToString(sequence_id_);
[email protected]b16a7c52013-11-20 01:18:59181 }
182 return registration_id;
183}
184
[email protected]accbbc92014-05-15 21:28:59185void FakeGCMClient::CheckinFinished() {
fgorskid578c18b2014-09-24 23:40:17186 delegate_->OnGCMReady(std::vector<AccountMapping>());
[email protected]fc6078a2014-06-14 08:28:43187 delegate_->OnConnected(net::IPEndPoint());
[email protected]d3a4b2e2014-02-27 13:46:54188}
189
[email protected]accbbc92014-05-15 21:28:59190void FakeGCMClient::RegisterFinished(const std::string& app_id,
[email protected]5799d052014-02-12 20:47:39191 const std::string& registrion_id) {
192 delegate_->OnRegisterFinished(
[email protected]b16a7c52013-11-20 01:18:59193 app_id, registrion_id, registrion_id.empty() ? SERVER_ERROR : SUCCESS);
194}
195
[email protected]accbbc92014-05-15 21:28:59196void FakeGCMClient::UnregisterFinished(const std::string& app_id) {
[email protected]0e88e1d12014-03-19 06:53:08197 delegate_->OnUnregisterFinished(app_id, GCMClient::SUCCESS);
198}
199
[email protected]accbbc92014-05-15 21:28:59200void FakeGCMClient::SendFinished(const std::string& app_id,
[email protected]c6fe36b2014-03-11 10:58:12201 const OutgoingMessage& message) {
202 delegate_->OnSendFinished(app_id, message.id, SUCCESS);
[email protected]b16a7c52013-11-20 01:18:59203
204 // Simulate send error if message id contains a hint.
[email protected]c6fe36b2014-03-11 10:58:12205 if (message.id.find("error") != std::string::npos) {
206 SendErrorDetails send_error_details;
207 send_error_details.message_id = message.id;
208 send_error_details.result = NETWORK_ERROR;
209 send_error_details.additional_data = message.data;
[email protected]b16a7c52013-11-20 01:18:59210 base::MessageLoop::current()->PostDelayedTask(
211 FROM_HERE,
[email protected]accbbc92014-05-15 21:28:59212 base::Bind(&FakeGCMClient::MessageSendError,
[email protected]d3a4b2e2014-02-27 13:46:54213 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:59214 app_id,
[email protected]c6fe36b2014-03-11 10:58:12215 send_error_details),
[email protected]b16a7c52013-11-20 01:18:59216 base::TimeDelta::FromMilliseconds(200));
[email protected]292af2b22014-08-06 19:42:45217 } else if(message.id.find("ack") != std::string::npos) {
218 base::MessageLoop::current()->PostDelayedTask(
219 FROM_HERE,
220 base::Bind(&FakeGCMClient::SendAcknowledgement,
221 weak_ptr_factory_.GetWeakPtr(),
222 app_id,
223 message.id),
224 base::TimeDelta::FromMilliseconds(200));
225
[email protected]b16a7c52013-11-20 01:18:59226 }
227}
228
[email protected]accbbc92014-05-15 21:28:59229void FakeGCMClient::MessageReceived(const std::string& app_id,
[email protected]5799d052014-02-12 20:47:39230 const IncomingMessage& message) {
231 if (delegate_)
232 delegate_->OnMessageReceived(app_id, message);
[email protected]b16a7c52013-11-20 01:18:59233}
234
[email protected]accbbc92014-05-15 21:28:59235void FakeGCMClient::MessagesDeleted(const std::string& app_id) {
[email protected]5799d052014-02-12 20:47:39236 if (delegate_)
237 delegate_->OnMessagesDeleted(app_id);
[email protected]b16a7c52013-11-20 01:18:59238}
239
[email protected]accbbc92014-05-15 21:28:59240void FakeGCMClient::MessageSendError(
[email protected]c6fe36b2014-03-11 10:58:12241 const std::string& app_id,
242 const GCMClient::SendErrorDetails& send_error_details) {
[email protected]5799d052014-02-12 20:47:39243 if (delegate_)
[email protected]c6fe36b2014-03-11 10:58:12244 delegate_->OnMessageSendError(app_id, send_error_details);
[email protected]b16a7c52013-11-20 01:18:59245}
246
[email protected]292af2b22014-08-06 19:42:45247void FakeGCMClient::SendAcknowledgement(const std::string& app_id,
248 const std::string& message_id) {
249 if (delegate_)
250 delegate_->OnSendAcknowledged(app_id, message_id);
251}
252
[email protected]b16a7c52013-11-20 01:18:59253} // namespace gcm