blob: 6b71ebb2f62e270c493c58dc2c853a75864547af [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
dcheng0917ec42015-11-19 07:00:207#include <algorithm>
8
[email protected]b16a7c52013-11-20 01:18:599#include "base/bind.h"
skyostilb0daa012015-06-02 19:03:4810#include "base/location.h"
[email protected]b16a7c52013-11-20 01:18:5911#include "base/logging.h"
[email protected]1795df51a2014-05-22 20:18:0412#include "base/sequenced_task_runner.h"
skyostilb0daa012015-06-02 19:03:4813#include "base/single_thread_task_runner.h"
jianli15aecc152014-10-07 03:03:5314#include "base/strings/string_number_conversions.h"
[email protected]b16a7c52013-11-20 01:18:5915#include "base/sys_byteorder.h"
skyostilb0daa012015-06-02 19:03:4816#include "base/thread_task_runner_handle.h"
[email protected]b16a7c52013-11-20 01:18:5917#include "base/time/time.h"
chirantan192a9212014-12-06 03:30:4518#include "base/timer/timer.h"
[email protected]446f73c22014-05-14 20:47:1819#include "google_apis/gcm/base/encryptor.h"
fgorskid578c18b2014-09-24 23:40:1720#include "google_apis/gcm/engine/account_mapping.h"
[email protected]fc6078a2014-06-14 08:28:4321#include "net/base/ip_endpoint.h"
[email protected]b16a7c52013-11-20 01:18:5922
23namespace gcm {
24
jianli012b5c82015-05-28 01:41:2925// static
26std::string FakeGCMClient::GenerateGCMRegistrationID(
27 const std::vector<std::string>& sender_ids) {
28 // GCMService normalizes the sender IDs by making them sorted.
29 std::vector<std::string> normalized_sender_ids = sender_ids;
30 std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end());
31
32 // Simulate the registration_id by concaternating all sender IDs.
33 // Set registration_id to empty to denote an error if sender_ids contains a
34 // hint.
35 std::string registration_id;
36 if (sender_ids.size() != 1 ||
37 sender_ids[0].find("error") == std::string::npos) {
38 for (size_t i = 0; i < normalized_sender_ids.size(); ++i) {
39 if (i > 0)
40 registration_id += ",";
41 registration_id += normalized_sender_ids[i];
42 }
43 }
44 return registration_id;
45}
46
47// static
48std::string FakeGCMClient::GenerateInstanceIDToken(
49 const std::string& authorized_entity, const std::string& scope) {
50 if (authorized_entity.find("error") != std::string::npos)
51 return "";
52 std::string token(authorized_entity);
53 token += ",";
54 token += scope;
55 return token;
56}
57
[email protected]1795df51a2014-05-22 20:18:0458FakeGCMClient::FakeGCMClient(
[email protected]1795df51a2014-05-22 20:18:0459 const scoped_refptr<base::SequencedTaskRunner>& ui_thread,
60 const scoped_refptr<base::SequencedTaskRunner>& io_thread)
[email protected]5799d052014-02-12 20:47:3961 : delegate_(NULL),
jianli19562592015-01-12 20:16:3062 started_(false),
jianlif3e52af42015-01-21 23:18:4763 start_mode_(DELAYED_START),
64 start_mode_overridding_(RESPECT_START_MODE),
[email protected]1795df51a2014-05-22 20:18:0465 ui_thread_(ui_thread),
66 io_thread_(io_thread),
[email protected]d3a4b2e2014-02-27 13:46:5467 weak_ptr_factory_(this) {
[email protected]b16a7c52013-11-20 01:18:5968}
69
[email protected]accbbc92014-05-15 21:28:5970FakeGCMClient::~FakeGCMClient() {
[email protected]b16a7c52013-11-20 01:18:5971}
72
[email protected]accbbc92014-05-15 21:28:5973void FakeGCMClient::Initialize(
[email protected]8ad80512014-05-23 09:40:4774 const ChromeBuildInfo& chrome_build_info,
[email protected]e2a4a8012014-02-07 22:32:5275 const base::FilePath& store_path,
76 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
77 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:3978 url_request_context_getter,
[email protected]446f73c22014-05-14 20:47:1879 scoped_ptr<Encryptor> encryptor,
[email protected]5799d052014-02-12 20:47:3980 Delegate* delegate) {
81 delegate_ = delegate;
[email protected]e2a4a8012014-02-07 22:32:5282}
83
jianlif3e52af42015-01-21 23:18:4784void FakeGCMClient::Start(StartMode start_mode) {
[email protected]1795df51a2014-05-22 20:18:0485 DCHECK(io_thread_->RunsTasksOnCurrentThread());
[email protected]d3a4b2e2014-02-27 13:46:5486
jianlif3e52af42015-01-21 23:18:4787 if (started_)
[email protected]d3a4b2e2014-02-27 13:46:5488 return;
jianlif3e52af42015-01-21 23:18:4789
90 if (start_mode == IMMEDIATE_START)
91 start_mode_ = IMMEDIATE_START;
92 if (start_mode_ == DELAYED_START ||
93 start_mode_overridding_ == FORCE_TO_ALWAYS_DELAY_START_GCM) {
94 return;
95 }
96
97 DoStart();
[email protected]d3a4b2e2014-02-27 13:46:5498}
99
jianlif3e52af42015-01-21 23:18:47100void FakeGCMClient::DoStart() {
jianli19562592015-01-12 20:16:30101 started_ = true;
skyostilb0daa012015-06-02 19:03:48102 base::ThreadTaskRunnerHandle::Get()->PostTask(
[email protected]d3a4b2e2014-02-27 13:46:54103 FROM_HERE,
skyostilb0daa012015-06-02 19:03:48104 base::Bind(&FakeGCMClient::Started, weak_ptr_factory_.GetWeakPtr()));
[email protected]d3a4b2e2014-02-27 13:46:54105}
106
[email protected]accbbc92014-05-15 21:28:59107void FakeGCMClient::Stop() {
[email protected]1795df51a2014-05-22 20:18:04108 DCHECK(io_thread_->RunsTasksOnCurrentThread());
jianli19562592015-01-12 20:16:30109 started_ = false;
[email protected]fc6078a2014-06-14 08:28:43110 delegate_->OnDisconnected();
[email protected]21fee5482014-03-05 00:57:15111}
112
jianli7a0c9b62015-05-26 23:24:47113void FakeGCMClient::Register(
114 const linked_ptr<RegistrationInfo>& registration_info) {
[email protected]1795df51a2014-05-22 20:18:04115 DCHECK(io_thread_->RunsTasksOnCurrentThread());
[email protected]b16a7c52013-11-20 01:18:59116
jianli012b5c82015-05-28 01:41:29117 std::string registration_id;
118
jianli7a0c9b62015-05-26 23:24:47119 GCMRegistrationInfo* gcm_registration_info =
120 GCMRegistrationInfo::FromRegistrationInfo(registration_info.get());
jianli012b5c82015-05-28 01:41:29121 if (gcm_registration_info) {
122 registration_id = GenerateGCMRegistrationID(
123 gcm_registration_info->sender_ids);
124 }
jianli7a0c9b62015-05-26 23:24:47125
jianli012b5c82015-05-28 01:41:29126 InstanceIDTokenInfo* instance_id_token_info =
127 InstanceIDTokenInfo::FromRegistrationInfo(registration_info.get());
128 if (instance_id_token_info) {
129 registration_id = GenerateInstanceIDToken(
130 instance_id_token_info->authorized_entity,
131 instance_id_token_info->scope);
132 }
133
skyostilb0daa012015-06-02 19:03:48134 base::ThreadTaskRunnerHandle::Get()->PostTask(
135 FROM_HERE, base::Bind(&FakeGCMClient::RegisterFinished,
136 weak_ptr_factory_.GetWeakPtr(), registration_info,
137 registration_id));
[email protected]b16a7c52013-11-20 01:18:59138}
139
jianli7a0c9b62015-05-26 23:24:47140void FakeGCMClient::Unregister(
141 const linked_ptr<RegistrationInfo>& registration_info) {
[email protected]1795df51a2014-05-22 20:18:04142 DCHECK(io_thread_->RunsTasksOnCurrentThread());
[email protected]0e88e1d12014-03-19 06:53:08143
skyostilb0daa012015-06-02 19:03:48144 base::ThreadTaskRunnerHandle::Get()->PostTask(
145 FROM_HERE, base::Bind(&FakeGCMClient::UnregisterFinished,
146 weak_ptr_factory_.GetWeakPtr(), registration_info));
[email protected]b16a7c52013-11-20 01:18:59147}
148
[email protected]accbbc92014-05-15 21:28:59149void FakeGCMClient::Send(const std::string& app_id,
[email protected]b16a7c52013-11-20 01:18:59150 const std::string& receiver_id,
151 const OutgoingMessage& message) {
[email protected]1795df51a2014-05-22 20:18:04152 DCHECK(io_thread_->RunsTasksOnCurrentThread());
[email protected]b16a7c52013-11-20 01:18:59153
skyostilb0daa012015-06-02 19:03:48154 base::ThreadTaskRunnerHandle::Get()->PostTask(
155 FROM_HERE, base::Bind(&FakeGCMClient::SendFinished,
156 weak_ptr_factory_.GetWeakPtr(), app_id, message));
[email protected]b16a7c52013-11-20 01:18:59157}
158
[email protected]accbbc92014-05-15 21:28:59159void FakeGCMClient::SetRecording(bool recording) {
[email protected]436bcb82014-04-18 00:40:57160}
161
[email protected]accbbc92014-05-15 21:28:59162void FakeGCMClient::ClearActivityLogs() {
[email protected]436bcb82014-04-18 00:40:57163}
164
[email protected]accbbc92014-05-15 21:28:59165GCMClient::GCMStatistics FakeGCMClient::GetStatistics() const {
[email protected]35601812014-03-07 19:52:43166 return GCMClient::GCMStatistics();
167}
168
fgorski58b9dfc2014-09-29 16:46:18169void FakeGCMClient::SetAccountTokens(
170 const std::vector<AccountTokenInfo>& account_tokens) {
[email protected]7df5ef22014-07-17 07:35:58171}
172
[email protected]72d4f252014-08-20 22:34:28173void FakeGCMClient::UpdateAccountMapping(
174 const AccountMapping& account_mapping) {
175}
176
177void FakeGCMClient::RemoveAccountMapping(const std::string& account_id) {
178}
179
fgorski5df101702014-10-28 02:09:31180void FakeGCMClient::SetLastTokenFetchTime(const base::Time& time) {
181}
182
chirantan192a9212014-12-06 03:30:45183void FakeGCMClient::UpdateHeartbeatTimer(scoped_ptr<base::Timer> timer) {
184}
185
jianli10018b2d2015-05-11 21:14:13186void FakeGCMClient::AddInstanceIDData(const std::string& app_id,
jianli7a0c9b62015-05-26 23:24:47187 const std::string& instance_id,
188 const std::string& extra_data) {
jianliea8534872015-06-22 21:06:22189 instance_id_data_[app_id] = make_pair(instance_id, extra_data);
jianli10018b2d2015-05-11 21:14:13190}
191
192void FakeGCMClient::RemoveInstanceIDData(const std::string& app_id) {
jianliea8534872015-06-22 21:06:22193 instance_id_data_.erase(app_id);
jianli10018b2d2015-05-11 21:14:13194}
195
jianli7a0c9b62015-05-26 23:24:47196void FakeGCMClient::GetInstanceIDData(const std::string& app_id,
197 std::string* instance_id,
198 std::string* extra_data) {
jianliea8534872015-06-22 21:06:22199 auto iter = instance_id_data_.find(app_id);
200 if (iter == instance_id_data_.end()) {
201 instance_id->clear();
202 extra_data->clear();
203 return;
204 }
205
206 *instance_id = iter->second.first;
207 *extra_data = iter->second.second;
jianli10018b2d2015-05-11 21:14:13208}
209
fgorski22754462015-05-14 00:05:22210void FakeGCMClient::AddHeartbeatInterval(const std::string& scope,
211 int interval_ms) {
212}
213
214void FakeGCMClient::RemoveHeartbeatInterval(const std::string& scope) {
215}
216
jianlif3e52af42015-01-21 23:18:47217void FakeGCMClient::PerformDelayedStart() {
[email protected]1795df51a2014-05-22 20:18:04218 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
[email protected]d3a4b2e2014-02-27 13:46:54219
[email protected]1795df51a2014-05-22 20:18:04220 io_thread_->PostTask(
[email protected]d3a4b2e2014-02-27 13:46:54221 FROM_HERE,
jianlif3e52af42015-01-21 23:18:47222 base::Bind(&FakeGCMClient::DoStart, weak_ptr_factory_.GetWeakPtr()));
[email protected]b16a7c52013-11-20 01:18:59223}
224
[email protected]accbbc92014-05-15 21:28:59225void FakeGCMClient::ReceiveMessage(const std::string& app_id,
[email protected]b16a7c52013-11-20 01:18:59226 const IncomingMessage& message) {
[email protected]1795df51a2014-05-22 20:18:04227 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
[email protected]b16a7c52013-11-20 01:18:59228
[email protected]1795df51a2014-05-22 20:18:04229 io_thread_->PostTask(
[email protected]b16a7c52013-11-20 01:18:59230 FROM_HERE,
[email protected]accbbc92014-05-15 21:28:59231 base::Bind(&FakeGCMClient::MessageReceived,
[email protected]d3a4b2e2014-02-27 13:46:54232 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:59233 app_id,
234 message));
235}
236
[email protected]accbbc92014-05-15 21:28:59237void FakeGCMClient::DeleteMessages(const std::string& app_id) {
[email protected]1795df51a2014-05-22 20:18:04238 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
[email protected]b16a7c52013-11-20 01:18:59239
[email protected]1795df51a2014-05-22 20:18:04240 io_thread_->PostTask(
[email protected]b16a7c52013-11-20 01:18:59241 FROM_HERE,
[email protected]accbbc92014-05-15 21:28:59242 base::Bind(&FakeGCMClient::MessagesDeleted,
[email protected]d3a4b2e2014-02-27 13:46:54243 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:59244 app_id));
245}
246
jianlif3e52af42015-01-21 23:18:47247void FakeGCMClient::Started() {
fgorski5df101702014-10-28 02:09:31248 delegate_->OnGCMReady(std::vector<AccountMapping>(), base::Time());
[email protected]fc6078a2014-06-14 08:28:43249 delegate_->OnConnected(net::IPEndPoint());
[email protected]d3a4b2e2014-02-27 13:46:54250}
251
jianli7a0c9b62015-05-26 23:24:47252void FakeGCMClient::RegisterFinished(
253 const linked_ptr<RegistrationInfo>& registration_info,
254 const std::string& registrion_id) {
[email protected]5799d052014-02-12 20:47:39255 delegate_->OnRegisterFinished(
jianli7a0c9b62015-05-26 23:24:47256 registration_info,
257 registrion_id,
258 registrion_id.empty() ? SERVER_ERROR : SUCCESS);
[email protected]b16a7c52013-11-20 01:18:59259}
260
jianli7a0c9b62015-05-26 23:24:47261void FakeGCMClient::UnregisterFinished(
262 const linked_ptr<RegistrationInfo>& registration_info) {
263 delegate_->OnUnregisterFinished(registration_info, GCMClient::SUCCESS);
[email protected]0e88e1d12014-03-19 06:53:08264}
265
[email protected]accbbc92014-05-15 21:28:59266void FakeGCMClient::SendFinished(const std::string& app_id,
[email protected]c6fe36b2014-03-11 10:58:12267 const OutgoingMessage& message) {
268 delegate_->OnSendFinished(app_id, message.id, SUCCESS);
[email protected]b16a7c52013-11-20 01:18:59269
270 // Simulate send error if message id contains a hint.
[email protected]c6fe36b2014-03-11 10:58:12271 if (message.id.find("error") != std::string::npos) {
272 SendErrorDetails send_error_details;
273 send_error_details.message_id = message.id;
274 send_error_details.result = NETWORK_ERROR;
275 send_error_details.additional_data = message.data;
skyostilb0daa012015-06-02 19:03:48276 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
[email protected]b16a7c52013-11-20 01:18:59277 FROM_HERE,
[email protected]accbbc92014-05-15 21:28:59278 base::Bind(&FakeGCMClient::MessageSendError,
skyostilb0daa012015-06-02 19:03:48279 weak_ptr_factory_.GetWeakPtr(), app_id, send_error_details),
[email protected]b16a7c52013-11-20 01:18:59280 base::TimeDelta::FromMilliseconds(200));
[email protected]292af2b22014-08-06 19:42:45281 } else if(message.id.find("ack") != std::string::npos) {
skyostilb0daa012015-06-02 19:03:48282 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
[email protected]292af2b22014-08-06 19:42:45283 FROM_HERE,
284 base::Bind(&FakeGCMClient::SendAcknowledgement,
skyostilb0daa012015-06-02 19:03:48285 weak_ptr_factory_.GetWeakPtr(), app_id, message.id),
[email protected]292af2b22014-08-06 19:42:45286 base::TimeDelta::FromMilliseconds(200));
[email protected]b16a7c52013-11-20 01:18:59287 }
288}
289
[email protected]accbbc92014-05-15 21:28:59290void FakeGCMClient::MessageReceived(const std::string& app_id,
[email protected]5799d052014-02-12 20:47:39291 const IncomingMessage& message) {
292 if (delegate_)
293 delegate_->OnMessageReceived(app_id, message);
[email protected]b16a7c52013-11-20 01:18:59294}
295
[email protected]accbbc92014-05-15 21:28:59296void FakeGCMClient::MessagesDeleted(const std::string& app_id) {
[email protected]5799d052014-02-12 20:47:39297 if (delegate_)
298 delegate_->OnMessagesDeleted(app_id);
[email protected]b16a7c52013-11-20 01:18:59299}
300
[email protected]accbbc92014-05-15 21:28:59301void FakeGCMClient::MessageSendError(
[email protected]c6fe36b2014-03-11 10:58:12302 const std::string& app_id,
303 const GCMClient::SendErrorDetails& send_error_details) {
[email protected]5799d052014-02-12 20:47:39304 if (delegate_)
[email protected]c6fe36b2014-03-11 10:58:12305 delegate_->OnMessageSendError(app_id, send_error_details);
[email protected]b16a7c52013-11-20 01:18:59306}
307
[email protected]292af2b22014-08-06 19:42:45308void FakeGCMClient::SendAcknowledgement(const std::string& app_id,
309 const std::string& message_id) {
310 if (delegate_)
311 delegate_->OnSendAcknowledged(app_id, message_id);
312}
313
[email protected]b16a7c52013-11-20 01:18:59314} // namespace gcm