blob: de65af216f21ffc12ec86a1586f4f49712d8e975 [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::string& cert,
68 const std::vector<std::string>& sender_ids) {
69 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
70
[email protected]1b1c3cd2013-12-17 18:40:0471 std::string registration_id;
[email protected]5799d052014-02-12 20:47:3972 if (error_simulation_ == ALWAYS_SUCCEED)
[email protected]1b1c3cd2013-12-17 18:40:0473 registration_id = GetRegistrationIdFromSenderIds(sender_ids);
[email protected]b16a7c52013-11-20 01:18:5974
75 base::MessageLoop::current()->PostTask(
76 FROM_HERE,
77 base::Bind(&GCMClientMock::RegisterFinished,
[email protected]d3a4b2e2014-02-27 13:46:5478 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:5979 app_id,
80 registration_id));
81}
82
[email protected]5799d052014-02-12 20:47:3983void GCMClientMock::Unregister(const std::string& app_id) {
[email protected]b16a7c52013-11-20 01:18:5984}
85
[email protected]5799d052014-02-12 20:47:3986void GCMClientMock::Send(const std::string& app_id,
[email protected]b16a7c52013-11-20 01:18:5987 const std::string& receiver_id,
88 const OutgoingMessage& message) {
89 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
90
91 base::MessageLoop::current()->PostTask(
92 FROM_HERE,
93 base::Bind(&GCMClientMock::SendFinished,
[email protected]d3a4b2e2014-02-27 13:46:5494 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:5995 app_id,
96 message.id));
97}
98
[email protected]d3a4b2e2014-02-27 13:46:5499void GCMClientMock::PerformDelayedLoading() {
100 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
101
102 content::BrowserThread::PostTask(
103 content::BrowserThread::IO,
104 FROM_HERE,
105 base::Bind(&GCMClientMock::DoLoading, weak_ptr_factory_.GetWeakPtr()));
[email protected]b16a7c52013-11-20 01:18:59106}
107
[email protected]5799d052014-02-12 20:47:39108void GCMClientMock::ReceiveMessage(const std::string& app_id,
[email protected]b16a7c52013-11-20 01:18:59109 const IncomingMessage& message) {
110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
111
112 content::BrowserThread::PostTask(
113 content::BrowserThread::IO,
114 FROM_HERE,
115 base::Bind(&GCMClientMock::MessageReceived,
[email protected]d3a4b2e2014-02-27 13:46:54116 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:59117 app_id,
118 message));
119}
120
[email protected]5799d052014-02-12 20:47:39121void GCMClientMock::DeleteMessages(const std::string& app_id) {
[email protected]b16a7c52013-11-20 01:18:59122 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
123
124 content::BrowserThread::PostTask(
125 content::BrowserThread::IO,
126 FROM_HERE,
127 base::Bind(&GCMClientMock::MessagesDeleted,
[email protected]d3a4b2e2014-02-27 13:46:54128 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:59129 app_id));
130}
131
[email protected]1b1c3cd2013-12-17 18:40:04132// static
[email protected]b16a7c52013-11-20 01:18:59133std::string GCMClientMock::GetRegistrationIdFromSenderIds(
[email protected]1b1c3cd2013-12-17 18:40:04134 const std::vector<std::string>& sender_ids) {
[email protected]c7f7b532014-01-24 07:24:45135 // GCMProfileService normalizes the sender IDs by making them sorted.
136 std::vector<std::string> normalized_sender_ids = sender_ids;
137 std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end());
138
[email protected]b16a7c52013-11-20 01:18:59139 // Simulate the registration_id by concaternating all sender IDs.
140 // Set registration_id to empty to denote an error if sender_ids contains a
141 // hint.
142 std::string registration_id;
143 if (sender_ids.size() != 1 ||
144 sender_ids[0].find("error") == std::string::npos) {
[email protected]c7f7b532014-01-24 07:24:45145 for (size_t i = 0; i < normalized_sender_ids.size(); ++i) {
[email protected]b16a7c52013-11-20 01:18:59146 if (i > 0)
147 registration_id += ",";
[email protected]c7f7b532014-01-24 07:24:45148 registration_id += normalized_sender_ids[i];
[email protected]b16a7c52013-11-20 01:18:59149 }
150 }
151 return registration_id;
152}
153
[email protected]d3a4b2e2014-02-27 13:46:54154void GCMClientMock::CheckinFinished() {
155 delegate_->OnGCMReady();
156}
157
[email protected]5799d052014-02-12 20:47:39158void GCMClientMock::RegisterFinished(const std::string& app_id,
159 const std::string& registrion_id) {
160 delegate_->OnRegisterFinished(
[email protected]b16a7c52013-11-20 01:18:59161 app_id, registrion_id, registrion_id.empty() ? SERVER_ERROR : SUCCESS);
162}
163
[email protected]5799d052014-02-12 20:47:39164void GCMClientMock::SendFinished(const std::string& app_id,
165 const std::string& message_id) {
166 delegate_->OnSendFinished(app_id, message_id, SUCCESS);
[email protected]b16a7c52013-11-20 01:18:59167
168 // Simulate send error if message id contains a hint.
169 if (message_id.find("error") != std::string::npos) {
170 base::MessageLoop::current()->PostDelayedTask(
171 FROM_HERE,
172 base::Bind(&GCMClientMock::MessageSendError,
[email protected]d3a4b2e2014-02-27 13:46:54173 weak_ptr_factory_.GetWeakPtr(),
[email protected]b16a7c52013-11-20 01:18:59174 app_id,
175 message_id),
176 base::TimeDelta::FromMilliseconds(200));
177 }
178}
179
[email protected]5799d052014-02-12 20:47:39180void GCMClientMock::MessageReceived(const std::string& app_id,
181 const IncomingMessage& message) {
182 if (delegate_)
183 delegate_->OnMessageReceived(app_id, message);
[email protected]b16a7c52013-11-20 01:18:59184}
185
[email protected]5799d052014-02-12 20:47:39186void GCMClientMock::MessagesDeleted(const std::string& app_id) {
187 if (delegate_)
188 delegate_->OnMessagesDeleted(app_id);
[email protected]b16a7c52013-11-20 01:18:59189}
190
[email protected]5799d052014-02-12 20:47:39191void GCMClientMock::MessageSendError(const std::string& app_id,
192 const std::string& message_id) {
193 if (delegate_)
194 delegate_->OnMessageSendError(app_id, message_id, NETWORK_ERROR);
[email protected]b16a7c52013-11-20 01:18:59195}
196
[email protected]b16a7c52013-11-20 01:18:59197} // namespace gcm