Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / services / gcm / gcm_client_mock.cc
1 // 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
14 namespace gcm {
15
16 GCMClientMock::GCMClientMock(Status status, ErrorSimulation error_simulation)
17     : delegate_(NULL),
18       status_(status),
19       error_simulation_(error_simulation) {
20 }
21
22 GCMClientMock::~GCMClientMock() {
23 }
24
25 void GCMClientMock::Initialize(
26     const checkin_proto::ChromeBuildProto& chrome_build_proto,
27     const base::FilePath& store_path,
28     const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
29     const scoped_refptr<net::URLRequestContextGetter>&
30         url_request_context_getter,
31     Delegate* delegate) {
32   delegate_ = delegate;
33 }
34
35 void GCMClientMock::CheckOut() {
36   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
37 }
38
39 void GCMClientMock::Register(const std::string& app_id,
40                              const std::string& cert,
41                              const std::vector<std::string>& sender_ids) {
42   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
43
44   std::string registration_id;
45   if (error_simulation_ == ALWAYS_SUCCEED)
46     registration_id = GetRegistrationIdFromSenderIds(sender_ids);
47
48   base::MessageLoop::current()->PostTask(
49       FROM_HERE,
50       base::Bind(&GCMClientMock::RegisterFinished,
51                  base::Unretained(this),
52                  app_id,
53                  registration_id));
54 }
55
56 void GCMClientMock::Unregister(const std::string& app_id) {
57 }
58
59 void GCMClientMock::Send(const std::string& app_id,
60                          const std::string& receiver_id,
61                          const OutgoingMessage& message) {
62   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
63
64   base::MessageLoop::current()->PostTask(
65       FROM_HERE,
66       base::Bind(&GCMClientMock::SendFinished,
67                  base::Unretained(this),
68                  app_id,
69                  message.id));
70 }
71
72 bool GCMClientMock::IsReady() const {
73   return status_ == READY;
74 }
75
76 void GCMClientMock::ReceiveMessage(const std::string& app_id,
77                                    const IncomingMessage& message) {
78   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
79
80   content::BrowserThread::PostTask(
81       content::BrowserThread::IO,
82       FROM_HERE,
83       base::Bind(&GCMClientMock::MessageReceived,
84                  base::Unretained(this),
85                  app_id,
86                  message));
87 }
88
89 void GCMClientMock::DeleteMessages(const std::string& app_id) {
90   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
91
92   content::BrowserThread::PostTask(
93       content::BrowserThread::IO,
94       FROM_HERE,
95       base::Bind(&GCMClientMock::MessagesDeleted,
96                  base::Unretained(this),
97                  app_id));
98 }
99
100 void GCMClientMock::SetReady() {
101   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
102   DCHECK_EQ(status_, NOT_READY);
103
104   status_ = READY;
105   content::BrowserThread::PostTask(
106       content::BrowserThread::IO,
107       FROM_HERE,
108       base::Bind(&GCMClientMock::SetReadyOnIO,
109                  base::Unretained(this)));
110 }
111
112 // static
113 std::string GCMClientMock::GetRegistrationIdFromSenderIds(
114     const std::vector<std::string>& sender_ids) {
115   // GCMProfileService normalizes the sender IDs by making them sorted.
116   std::vector<std::string> normalized_sender_ids = sender_ids;
117   std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end());
118
119   // Simulate the registration_id by concaternating all sender IDs.
120   // Set registration_id to empty to denote an error if sender_ids contains a
121   // hint.
122   std::string registration_id;
123   if (sender_ids.size() != 1 ||
124       sender_ids[0].find("error") == std::string::npos) {
125     for (size_t i = 0; i < normalized_sender_ids.size(); ++i) {
126       if (i > 0)
127         registration_id += ",";
128       registration_id += normalized_sender_ids[i];
129     }
130   }
131   return registration_id;
132 }
133
134 void GCMClientMock::RegisterFinished(const std::string& app_id,
135                                      const std::string& registrion_id) {
136   delegate_->OnRegisterFinished(
137       app_id, registrion_id, registrion_id.empty() ? SERVER_ERROR : SUCCESS);
138 }
139
140 void GCMClientMock::SendFinished(const std::string& app_id,
141                                  const std::string& message_id) {
142   delegate_->OnSendFinished(app_id, message_id, SUCCESS);
143
144   // Simulate send error if message id contains a hint.
145   if (message_id.find("error") != std::string::npos) {
146     base::MessageLoop::current()->PostDelayedTask(
147         FROM_HERE,
148         base::Bind(&GCMClientMock::MessageSendError,
149                    base::Unretained(this),
150                    app_id,
151                    message_id),
152         base::TimeDelta::FromMilliseconds(200));
153   }
154 }
155
156 void GCMClientMock::MessageReceived(const std::string& app_id,
157                                     const IncomingMessage& message) {
158   if (delegate_)
159     delegate_->OnMessageReceived(app_id, message);
160 }
161
162 void GCMClientMock::MessagesDeleted(const std::string& app_id) {
163   if (delegate_)
164     delegate_->OnMessagesDeleted(app_id);
165 }
166
167 void GCMClientMock::MessageSendError(const std::string& app_id,
168                                      const std::string& message_id) {
169   if (delegate_)
170     delegate_->OnMessageSendError(app_id, message_id, NETWORK_ERROR);
171 }
172
173 void GCMClientMock::SetReadyOnIO() {
174   delegate_->OnGCMReady();
175 }
176
177 }  // namespace gcm