fake_gcm_client.cc revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright 2014 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 "components/gcm_driver/fake_gcm_client.h"
6
7#include "base/bind.h"
8#include "base/logging.h"
9#include "base/message_loop/message_loop.h"
10#include "base/sequenced_task_runner.h"
11#include "base/sys_byteorder.h"
12#include "base/time/time.h"
13#include "google_apis/gcm/base/encryptor.h"
14#include "net/base/ip_endpoint.h"
15
16namespace gcm {
17
18FakeGCMClient::FakeGCMClient(
19    StartMode start_mode,
20    const scoped_refptr<base::SequencedTaskRunner>& ui_thread,
21    const scoped_refptr<base::SequencedTaskRunner>& io_thread)
22    : delegate_(NULL),
23      status_(UNINITIALIZED),
24      start_mode_(start_mode),
25      ui_thread_(ui_thread),
26      io_thread_(io_thread),
27      weak_ptr_factory_(this) {
28}
29
30FakeGCMClient::~FakeGCMClient() {
31}
32
33void FakeGCMClient::Initialize(
34    const ChromeBuildInfo& chrome_build_info,
35    const base::FilePath& store_path,
36    const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
37    const scoped_refptr<net::URLRequestContextGetter>&
38        url_request_context_getter,
39    scoped_ptr<Encryptor> encryptor,
40    Delegate* delegate) {
41  delegate_ = delegate;
42}
43
44void FakeGCMClient::Start() {
45  DCHECK(io_thread_->RunsTasksOnCurrentThread());
46  DCHECK_NE(STARTED, status_);
47
48  if (start_mode_ == DELAY_START)
49    return;
50  DoLoading();
51}
52
53void FakeGCMClient::DoLoading() {
54  status_ = STARTED;
55  base::MessageLoop::current()->PostTask(
56      FROM_HERE,
57      base::Bind(&FakeGCMClient::CheckinFinished,
58                 weak_ptr_factory_.GetWeakPtr()));
59}
60
61void FakeGCMClient::Stop() {
62  DCHECK(io_thread_->RunsTasksOnCurrentThread());
63  status_ = STOPPED;
64  delegate_->OnDisconnected();
65}
66
67void FakeGCMClient::CheckOut() {
68  DCHECK(io_thread_->RunsTasksOnCurrentThread());
69  status_ = CHECKED_OUT;
70}
71
72void FakeGCMClient::Register(const std::string& app_id,
73                             const std::vector<std::string>& sender_ids) {
74  DCHECK(io_thread_->RunsTasksOnCurrentThread());
75
76  std::string registration_id = GetRegistrationIdFromSenderIds(sender_ids);
77  base::MessageLoop::current()->PostTask(
78      FROM_HERE,
79      base::Bind(&FakeGCMClient::RegisterFinished,
80                 weak_ptr_factory_.GetWeakPtr(),
81                 app_id,
82                 registration_id));
83}
84
85void FakeGCMClient::Unregister(const std::string& app_id) {
86  DCHECK(io_thread_->RunsTasksOnCurrentThread());
87
88  base::MessageLoop::current()->PostTask(
89      FROM_HERE,
90      base::Bind(&FakeGCMClient::UnregisterFinished,
91                 weak_ptr_factory_.GetWeakPtr(),
92                 app_id));
93}
94
95void FakeGCMClient::Send(const std::string& app_id,
96                         const std::string& receiver_id,
97                         const OutgoingMessage& message) {
98  DCHECK(io_thread_->RunsTasksOnCurrentThread());
99
100  base::MessageLoop::current()->PostTask(
101      FROM_HERE,
102      base::Bind(&FakeGCMClient::SendFinished,
103                 weak_ptr_factory_.GetWeakPtr(),
104                 app_id,
105                 message));
106}
107
108void FakeGCMClient::SetRecording(bool recording) {
109}
110
111void FakeGCMClient::ClearActivityLogs() {
112}
113
114GCMClient::GCMStatistics FakeGCMClient::GetStatistics() const {
115  return GCMClient::GCMStatistics();
116}
117
118void FakeGCMClient::SetAccountsForCheckin(
119    const std::map<std::string, std::string>& account_tokens) {
120}
121
122void FakeGCMClient::PerformDelayedLoading() {
123  DCHECK(ui_thread_->RunsTasksOnCurrentThread());
124
125  io_thread_->PostTask(
126      FROM_HERE,
127      base::Bind(&FakeGCMClient::DoLoading, weak_ptr_factory_.GetWeakPtr()));
128}
129
130void FakeGCMClient::ReceiveMessage(const std::string& app_id,
131                                   const IncomingMessage& message) {
132  DCHECK(ui_thread_->RunsTasksOnCurrentThread());
133
134  io_thread_->PostTask(
135      FROM_HERE,
136      base::Bind(&FakeGCMClient::MessageReceived,
137                 weak_ptr_factory_.GetWeakPtr(),
138                 app_id,
139                 message));
140}
141
142void FakeGCMClient::DeleteMessages(const std::string& app_id) {
143  DCHECK(ui_thread_->RunsTasksOnCurrentThread());
144
145  io_thread_->PostTask(
146      FROM_HERE,
147      base::Bind(&FakeGCMClient::MessagesDeleted,
148                 weak_ptr_factory_.GetWeakPtr(),
149                 app_id));
150}
151
152// static
153std::string FakeGCMClient::GetRegistrationIdFromSenderIds(
154    const std::vector<std::string>& sender_ids) {
155  // GCMService normalizes the sender IDs by making them sorted.
156  std::vector<std::string> normalized_sender_ids = sender_ids;
157  std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end());
158
159  // Simulate the registration_id by concaternating all sender IDs.
160  // Set registration_id to empty to denote an error if sender_ids contains a
161  // hint.
162  std::string registration_id;
163  if (sender_ids.size() != 1 ||
164      sender_ids[0].find("error") == std::string::npos) {
165    for (size_t i = 0; i < normalized_sender_ids.size(); ++i) {
166      if (i > 0)
167        registration_id += ",";
168      registration_id += normalized_sender_ids[i];
169    }
170  }
171  return registration_id;
172}
173
174void FakeGCMClient::CheckinFinished() {
175  delegate_->OnGCMReady();
176  delegate_->OnConnected(net::IPEndPoint());
177}
178
179void FakeGCMClient::RegisterFinished(const std::string& app_id,
180                                     const std::string& registrion_id) {
181  delegate_->OnRegisterFinished(
182      app_id, registrion_id, registrion_id.empty() ? SERVER_ERROR : SUCCESS);
183}
184
185void FakeGCMClient::UnregisterFinished(const std::string& app_id) {
186  delegate_->OnUnregisterFinished(app_id, GCMClient::SUCCESS);
187}
188
189void FakeGCMClient::SendFinished(const std::string& app_id,
190                                 const OutgoingMessage& message) {
191  delegate_->OnSendFinished(app_id, message.id, SUCCESS);
192
193  // Simulate send error if message id contains a hint.
194  if (message.id.find("error") != std::string::npos) {
195    SendErrorDetails send_error_details;
196    send_error_details.message_id = message.id;
197    send_error_details.result = NETWORK_ERROR;
198    send_error_details.additional_data = message.data;
199    base::MessageLoop::current()->PostDelayedTask(
200        FROM_HERE,
201        base::Bind(&FakeGCMClient::MessageSendError,
202                   weak_ptr_factory_.GetWeakPtr(),
203                   app_id,
204                   send_error_details),
205        base::TimeDelta::FromMilliseconds(200));
206  }
207}
208
209void FakeGCMClient::MessageReceived(const std::string& app_id,
210                                    const IncomingMessage& message) {
211  if (delegate_)
212    delegate_->OnMessageReceived(app_id, message);
213}
214
215void FakeGCMClient::MessagesDeleted(const std::string& app_id) {
216  if (delegate_)
217    delegate_->OnMessagesDeleted(app_id);
218}
219
220void FakeGCMClient::MessageSendError(
221    const std::string& app_id,
222    const GCMClient::SendErrorDetails& send_error_details) {
223  if (delegate_)
224    delegate_->OnMessageSendError(app_id, send_error_details);
225}
226
227}  // namespace gcm
228