blob: 3292f3fad5e71ab273d0499c38375aac274979ec [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]d3a4b2e2014-02-27 13:46:5416GCMClientMock::GCMClientMock(LoadingDelay loading_delay,
17 ErrorSimulation error_simulation)
[email protected]5799d052014-02-12 20:47:3918 : delegate_(NULL),
[email protected]d3a4b2e2014-02-27 13:46:5419 status_(UNINITIALIZED),
20 loading_delay_(loading_delay),
21 error_simulation_(error_simulation),
22 weak_ptr_factory_(this) {
[email protected]b16a7c52013-11-20 01:18:5923}
24
25GCMClientMock::~GCMClientMock() {
26}
27
[email protected]e2a4a8012014-02-07 22:32:5228void GCMClientMock::Initialize(
29 const checkin_proto::ChromeBuildProto& chrome_build_proto,
30 const base::FilePath& store_path,
[email protected]495a7db92014-02-22 07:49:5931 const std::vector<std::string>& account_ids,
[email protected]e2a4a8012014-02-07 22:32:5232 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
33 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:3934 url_request_context_getter,
35 Delegate* delegate) {
36 delegate_ = delegate;
[email protected]e2a4a8012014-02-07 22:32:5237}
38
[email protected]d3a4b2e2014-02-27 13:46:5439void GCMClientMock::Load() {
40 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
41 DCHECK_NE(LOADED, status_);
42
43 if (loading_delay_ == DELAY_LOADING)
44 return;
45 DoLoading();
46}
47
48void GCMClientMock::DoLoading() {
49 status_ = LOADED;
50 base::MessageLoop::current()->PostTask(
51 FROM_HERE,
52 base::Bind(&GCMClientMock::CheckinFinished,
53 weak_ptr_factory_.GetWeakPtr()));
54}
55
[email protected]21fee5482014-03-05 00:57:1556void GCMClientMock::Stop() {
57 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
58 status_ = STOPPED;
59}
60
[email protected]5799d052014-02-12 20:47:3961void GCMClientMock::CheckOut() {
[email protected]b16a7c52013-11-20 01:18:5962 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
[email protected]d3a4b2e2014-02-27 13:46:5463 status_ = CHECKED_OUT;
[email protected]1b1c3cd2013-12-17 18:40:0464}
65
[email protected]5799d052014-02-12 20:47:3966void GCMClientMock::Register(const std::string& app_id,
[email protected]b16a7c52013-11-20 01:18:5967 const std::vector<std::string>& sender_ids) {
68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
69
[email protected]1b1c3cd2013-12-17 18:40:0470 std::string registration_id;
[email protected]5799d052014-02-12 20:47:3971 if (error_simulation_ == ALWAYS_SUCCEED)
[email protected]1b1c3cd2013-12-17 18:40:0472 registration_id = GetRegistrationIdFromSenderIds(sender_ids);
[email protected]b16a7c52013-11-20 01:18:5973
74 base::MessageLoop::current()->PostTask(
75 FROM_HERE,
76 base::Bind(&GCMClientMock::RegisterFinished,
[email protected]d3a4b2e2014-02-27 13:46:5477 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:5978 app_id,
79 registration_id));
80}
81
[email protected]5799d052014-02-12 20:47:3982void GCMClientMock::Unregister(const std::string& app_id) {
[email protected]0e88e1d12014-03-19 06:53:0883 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
84
85 base::MessageLoop::current()->PostTask(
86 FROM_HERE,
87 base::Bind(&GCMClientMock::UnregisterFinished,
88 weak_ptr_factory_.GetWeakPtr(),
89 app_id));
[email protected]b16a7c52013-11-20 01:18:5990}
91
[email protected]5799d052014-02-12 20:47:3992void GCMClientMock::Send(const std::string& app_id,
[email protected]b16a7c52013-11-20 01:18:5993 const std::string& receiver_id,
94 const OutgoingMessage& message) {
95 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
96
97 base::MessageLoop::current()->PostTask(
98 FROM_HERE,
99 base::Bind(&GCMClientMock::SendFinished,
[email protected]d3a4b2e2014-02-27 13:46:54100 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:59101 app_id,
[email protected]c6fe36b2014-03-11 10:58:12102 message));
[email protected]b16a7c52013-11-20 01:18:59103}
104
[email protected]35601812014-03-07 19:52:43105GCMClient::GCMStatistics GCMClientMock::GetStatistics() const {
106 return GCMClient::GCMStatistics();
107}
108
[email protected]d3a4b2e2014-02-27 13:46:54109void GCMClientMock::PerformDelayedLoading() {
110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
111
112 content::BrowserThread::PostTask(
113 content::BrowserThread::IO,
114 FROM_HERE,
115 base::Bind(&GCMClientMock::DoLoading, weak_ptr_factory_.GetWeakPtr()));
[email protected]b16a7c52013-11-20 01:18:59116}
117
[email protected]5799d052014-02-12 20:47:39118void GCMClientMock::ReceiveMessage(const std::string& app_id,
[email protected]b16a7c52013-11-20 01:18:59119 const IncomingMessage& message) {
120 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
121
122 content::BrowserThread::PostTask(
123 content::BrowserThread::IO,
124 FROM_HERE,
125 base::Bind(&GCMClientMock::MessageReceived,
[email protected]d3a4b2e2014-02-27 13:46:54126 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:59127 app_id,
128 message));
129}
130
[email protected]5799d052014-02-12 20:47:39131void GCMClientMock::DeleteMessages(const std::string& app_id) {
[email protected]b16a7c52013-11-20 01:18:59132 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
133
134 content::BrowserThread::PostTask(
135 content::BrowserThread::IO,
136 FROM_HERE,
137 base::Bind(&GCMClientMock::MessagesDeleted,
[email protected]d3a4b2e2014-02-27 13:46:54138 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:59139 app_id));
140}
141
[email protected]1b1c3cd2013-12-17 18:40:04142// static
[email protected]b16a7c52013-11-20 01:18:59143std::string GCMClientMock::GetRegistrationIdFromSenderIds(
[email protected]1b1c3cd2013-12-17 18:40:04144 const std::vector<std::string>& sender_ids) {
[email protected]c7f7b532014-01-24 07:24:45145 // GCMProfileService normalizes the sender IDs by making them sorted.
146 std::vector<std::string> normalized_sender_ids = sender_ids;
147 std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end());
148
[email protected]b16a7c52013-11-20 01:18:59149 // Simulate the registration_id by concaternating all sender IDs.
150 // Set registration_id to empty to denote an error if sender_ids contains a
151 // hint.
152 std::string registration_id;
153 if (sender_ids.size() != 1 ||
154 sender_ids[0].find("error") == std::string::npos) {
[email protected]c7f7b532014-01-24 07:24:45155 for (size_t i = 0; i < normalized_sender_ids.size(); ++i) {
[email protected]b16a7c52013-11-20 01:18:59156 if (i > 0)
157 registration_id += ",";
[email protected]c7f7b532014-01-24 07:24:45158 registration_id += normalized_sender_ids[i];
[email protected]b16a7c52013-11-20 01:18:59159 }
160 }
161 return registration_id;
162}
163
[email protected]d3a4b2e2014-02-27 13:46:54164void GCMClientMock::CheckinFinished() {
165 delegate_->OnGCMReady();
166}
167
[email protected]5799d052014-02-12 20:47:39168void GCMClientMock::RegisterFinished(const std::string& app_id,
169 const std::string& registrion_id) {
170 delegate_->OnRegisterFinished(
[email protected]b16a7c52013-11-20 01:18:59171 app_id, registrion_id, registrion_id.empty() ? SERVER_ERROR : SUCCESS);
172}
173
[email protected]0e88e1d12014-03-19 06:53:08174void GCMClientMock::UnregisterFinished(const std::string& app_id) {
175 delegate_->OnUnregisterFinished(app_id, GCMClient::SUCCESS);
176}
177
[email protected]5799d052014-02-12 20:47:39178void GCMClientMock::SendFinished(const std::string& app_id,
[email protected]c6fe36b2014-03-11 10:58:12179 const OutgoingMessage& message) {
180 delegate_->OnSendFinished(app_id, message.id, SUCCESS);
[email protected]b16a7c52013-11-20 01:18:59181
182 // Simulate send error if message id contains a hint.
[email protected]c6fe36b2014-03-11 10:58:12183 if (message.id.find("error") != std::string::npos) {
184 SendErrorDetails send_error_details;
185 send_error_details.message_id = message.id;
186 send_error_details.result = NETWORK_ERROR;
187 send_error_details.additional_data = message.data;
[email protected]b16a7c52013-11-20 01:18:59188 base::MessageLoop::current()->PostDelayedTask(
189 FROM_HERE,
190 base::Bind(&GCMClientMock::MessageSendError,
[email protected]d3a4b2e2014-02-27 13:46:54191 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:59192 app_id,
[email protected]c6fe36b2014-03-11 10:58:12193 send_error_details),
[email protected]b16a7c52013-11-20 01:18:59194 base::TimeDelta::FromMilliseconds(200));
195 }
196}
197
[email protected]5799d052014-02-12 20:47:39198void GCMClientMock::MessageReceived(const std::string& app_id,
199 const IncomingMessage& message) {
200 if (delegate_)
201 delegate_->OnMessageReceived(app_id, message);
[email protected]b16a7c52013-11-20 01:18:59202}
203
[email protected]5799d052014-02-12 20:47:39204void GCMClientMock::MessagesDeleted(const std::string& app_id) {
205 if (delegate_)
206 delegate_->OnMessagesDeleted(app_id);
[email protected]b16a7c52013-11-20 01:18:59207}
208
[email protected]c6fe36b2014-03-11 10:58:12209void GCMClientMock::MessageSendError(
210 const std::string& app_id,
211 const GCMClient::SendErrorDetails& send_error_details) {
[email protected]5799d052014-02-12 20:47:39212 if (delegate_)
[email protected]c6fe36b2014-03-11 10:58:12213 delegate_->OnMessageSendError(app_id, send_error_details);
[email protected]b16a7c52013-11-20 01:18:59214}
215
[email protected]b16a7c52013-11-20 01:18:59216} // namespace gcm