blob: d924238005978d4019a9703494fc9d614e3cf6c1 [file] [log] [blame]
[email protected]b16a7c52013-11-20 01:18:591// 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
5#include "chrome/browser/services/gcm/gcm_client_mock.h"
6
7#include "base/bind.h"
8#include "base/logging.h"
9#include "base/message_loop/message_loop.h"
10#include "base/sys_byteorder.h"
11#include "base/time/time.h"
12#include "content/public/browser/browser_thread.h"
13
14namespace gcm {
15
[email protected]2ac46732014-04-03 23:14:3116GCMClientMock::GCMClientMock(LoadingDelay loading_delay)
[email protected]5799d052014-02-12 20:47:3917 : delegate_(NULL),
[email protected]d3a4b2e2014-02-27 13:46:5418 status_(UNINITIALIZED),
19 loading_delay_(loading_delay),
[email protected]d3a4b2e2014-02-27 13:46:5420 weak_ptr_factory_(this) {
[email protected]b16a7c52013-11-20 01:18:5921}
22
23GCMClientMock::~GCMClientMock() {
24}
25
[email protected]e2a4a8012014-02-07 22:32:5226void GCMClientMock::Initialize(
27 const checkin_proto::ChromeBuildProto& chrome_build_proto,
28 const base::FilePath& store_path,
[email protected]495a7db92014-02-22 07:49:5929 const std::vector<std::string>& account_ids,
[email protected]e2a4a8012014-02-07 22:32:5230 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
31 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:3932 url_request_context_getter,
33 Delegate* delegate) {
34 delegate_ = delegate;
[email protected]e2a4a8012014-02-07 22:32:5235}
36
[email protected]d3a4b2e2014-02-27 13:46:5437void GCMClientMock::Load() {
38 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
39 DCHECK_NE(LOADED, status_);
40
41 if (loading_delay_ == DELAY_LOADING)
42 return;
43 DoLoading();
44}
45
46void GCMClientMock::DoLoading() {
47 status_ = LOADED;
48 base::MessageLoop::current()->PostTask(
49 FROM_HERE,
50 base::Bind(&GCMClientMock::CheckinFinished,
51 weak_ptr_factory_.GetWeakPtr()));
52}
53
[email protected]21fee5482014-03-05 00:57:1554void GCMClientMock::Stop() {
55 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
56 status_ = STOPPED;
57}
58
[email protected]5799d052014-02-12 20:47:3959void GCMClientMock::CheckOut() {
[email protected]b16a7c52013-11-20 01:18:5960 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
[email protected]d3a4b2e2014-02-27 13:46:5461 status_ = CHECKED_OUT;
[email protected]1b1c3cd2013-12-17 18:40:0462}
63
[email protected]5799d052014-02-12 20:47:3964void GCMClientMock::Register(const std::string& app_id,
[email protected]b16a7c52013-11-20 01:18:5965 const std::vector<std::string>& sender_ids) {
66 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
67
[email protected]2ac46732014-04-03 23:14:3168 std::string registration_id = GetRegistrationIdFromSenderIds(sender_ids);
[email protected]b16a7c52013-11-20 01:18:5969 base::MessageLoop::current()->PostTask(
70 FROM_HERE,
71 base::Bind(&GCMClientMock::RegisterFinished,
[email protected]d3a4b2e2014-02-27 13:46:5472 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:5973 app_id,
74 registration_id));
75}
76
[email protected]5799d052014-02-12 20:47:3977void GCMClientMock::Unregister(const std::string& app_id) {
[email protected]0e88e1d12014-03-19 06:53:0878 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
79
80 base::MessageLoop::current()->PostTask(
81 FROM_HERE,
82 base::Bind(&GCMClientMock::UnregisterFinished,
83 weak_ptr_factory_.GetWeakPtr(),
84 app_id));
[email protected]b16a7c52013-11-20 01:18:5985}
86
[email protected]5799d052014-02-12 20:47:3987void GCMClientMock::Send(const std::string& app_id,
[email protected]b16a7c52013-11-20 01:18:5988 const std::string& receiver_id,
89 const OutgoingMessage& message) {
90 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
91
92 base::MessageLoop::current()->PostTask(
93 FROM_HERE,
94 base::Bind(&GCMClientMock::SendFinished,
[email protected]d3a4b2e2014-02-27 13:46:5495 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:5996 app_id,
[email protected]c6fe36b2014-03-11 10:58:1297 message));
[email protected]b16a7c52013-11-20 01:18:5998}
99
[email protected]436bcb82014-04-18 00:40:57100void GCMClientMock::SetRecording(bool recording) {
101}
102
103void GCMClientMock::ClearActivityLogs() {
104}
105
[email protected]35601812014-03-07 19:52:43106GCMClient::GCMStatistics GCMClientMock::GetStatistics() const {
107 return GCMClient::GCMStatistics();
108}
109
[email protected]d3a4b2e2014-02-27 13:46:54110void GCMClientMock::PerformDelayedLoading() {
111 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
112
113 content::BrowserThread::PostTask(
114 content::BrowserThread::IO,
115 FROM_HERE,
116 base::Bind(&GCMClientMock::DoLoading, weak_ptr_factory_.GetWeakPtr()));
[email protected]b16a7c52013-11-20 01:18:59117}
118
[email protected]5799d052014-02-12 20:47:39119void GCMClientMock::ReceiveMessage(const std::string& app_id,
[email protected]b16a7c52013-11-20 01:18:59120 const IncomingMessage& message) {
121 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
122
123 content::BrowserThread::PostTask(
124 content::BrowserThread::IO,
125 FROM_HERE,
126 base::Bind(&GCMClientMock::MessageReceived,
[email protected]d3a4b2e2014-02-27 13:46:54127 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:59128 app_id,
129 message));
130}
131
[email protected]5799d052014-02-12 20:47:39132void GCMClientMock::DeleteMessages(const std::string& app_id) {
[email protected]b16a7c52013-11-20 01:18:59133 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
134
135 content::BrowserThread::PostTask(
136 content::BrowserThread::IO,
137 FROM_HERE,
138 base::Bind(&GCMClientMock::MessagesDeleted,
[email protected]d3a4b2e2014-02-27 13:46:54139 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:59140 app_id));
141}
142
[email protected]1b1c3cd2013-12-17 18:40:04143// static
[email protected]b16a7c52013-11-20 01:18:59144std::string GCMClientMock::GetRegistrationIdFromSenderIds(
[email protected]1b1c3cd2013-12-17 18:40:04145 const std::vector<std::string>& sender_ids) {
[email protected]c7f7b532014-01-24 07:24:45146 // GCMProfileService normalizes the sender IDs by making them sorted.
147 std::vector<std::string> normalized_sender_ids = sender_ids;
148 std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end());
149
[email protected]b16a7c52013-11-20 01:18:59150 // Simulate the registration_id by concaternating all sender IDs.
151 // Set registration_id to empty to denote an error if sender_ids contains a
152 // hint.
153 std::string registration_id;
154 if (sender_ids.size() != 1 ||
155 sender_ids[0].find("error") == std::string::npos) {
[email protected]c7f7b532014-01-24 07:24:45156 for (size_t i = 0; i < normalized_sender_ids.size(); ++i) {
[email protected]b16a7c52013-11-20 01:18:59157 if (i > 0)
158 registration_id += ",";
[email protected]c7f7b532014-01-24 07:24:45159 registration_id += normalized_sender_ids[i];
[email protected]b16a7c52013-11-20 01:18:59160 }
161 }
162 return registration_id;
163}
164
[email protected]d3a4b2e2014-02-27 13:46:54165void GCMClientMock::CheckinFinished() {
166 delegate_->OnGCMReady();
167}
168
[email protected]5799d052014-02-12 20:47:39169void GCMClientMock::RegisterFinished(const std::string& app_id,
170 const std::string& registrion_id) {
171 delegate_->OnRegisterFinished(
[email protected]b16a7c52013-11-20 01:18:59172 app_id, registrion_id, registrion_id.empty() ? SERVER_ERROR : SUCCESS);
173}
174
[email protected]0e88e1d12014-03-19 06:53:08175void GCMClientMock::UnregisterFinished(const std::string& app_id) {
176 delegate_->OnUnregisterFinished(app_id, GCMClient::SUCCESS);
177}
178
[email protected]5799d052014-02-12 20:47:39179void GCMClientMock::SendFinished(const std::string& app_id,
[email protected]c6fe36b2014-03-11 10:58:12180 const OutgoingMessage& message) {
181 delegate_->OnSendFinished(app_id, message.id, SUCCESS);
[email protected]b16a7c52013-11-20 01:18:59182
183 // Simulate send error if message id contains a hint.
[email protected]c6fe36b2014-03-11 10:58:12184 if (message.id.find("error") != std::string::npos) {
185 SendErrorDetails send_error_details;
186 send_error_details.message_id = message.id;
187 send_error_details.result = NETWORK_ERROR;
188 send_error_details.additional_data = message.data;
[email protected]b16a7c52013-11-20 01:18:59189 base::MessageLoop::current()->PostDelayedTask(
190 FROM_HERE,
191 base::Bind(&GCMClientMock::MessageSendError,
[email protected]d3a4b2e2014-02-27 13:46:54192 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:59193 app_id,
[email protected]c6fe36b2014-03-11 10:58:12194 send_error_details),
[email protected]b16a7c52013-11-20 01:18:59195 base::TimeDelta::FromMilliseconds(200));
196 }
197}
198
[email protected]5799d052014-02-12 20:47:39199void GCMClientMock::MessageReceived(const std::string& app_id,
200 const IncomingMessage& message) {
201 if (delegate_)
202 delegate_->OnMessageReceived(app_id, message);
[email protected]b16a7c52013-11-20 01:18:59203}
204
[email protected]5799d052014-02-12 20:47:39205void GCMClientMock::MessagesDeleted(const std::string& app_id) {
206 if (delegate_)
207 delegate_->OnMessagesDeleted(app_id);
[email protected]b16a7c52013-11-20 01:18:59208}
209
[email protected]c6fe36b2014-03-11 10:58:12210void GCMClientMock::MessageSendError(
211 const std::string& app_id,
212 const GCMClient::SendErrorDetails& send_error_details) {
[email protected]5799d052014-02-12 20:47:39213 if (delegate_)
[email protected]c6fe36b2014-03-11 10:58:12214 delegate_->OnMessageSendError(app_id, send_error_details);
[email protected]b16a7c52013-11-20 01:18:59215}
216
[email protected]b16a7c52013-11-20 01:18:59217} // namespace gcm