[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 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 | |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 16 | GCMClientMock::GCMClientMock(LoadingDelay loading_delay, |
| 17 | ErrorSimulation error_simulation) |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 18 | : delegate_(NULL), |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 19 | status_(UNINITIALIZED), |
| 20 | loading_delay_(loading_delay), |
| 21 | error_simulation_(error_simulation), |
| 22 | weak_ptr_factory_(this) { |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | GCMClientMock::~GCMClientMock() { |
| 26 | } |
| 27 | |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 28 | void GCMClientMock::Initialize( |
| 29 | const checkin_proto::ChromeBuildProto& chrome_build_proto, |
| 30 | const base::FilePath& store_path, |
[email protected] | 495a7db9 | 2014-02-22 07:49:59 | [diff] [blame] | 31 | const std::vector<std::string>& account_ids, |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 32 | const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner, |
| 33 | const scoped_refptr<net::URLRequestContextGetter>& |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 34 | url_request_context_getter, |
| 35 | Delegate* delegate) { |
| 36 | delegate_ = delegate; |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 37 | } |
| 38 | |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 39 | void 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 | |
| 48 | void 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] | 21fee548 | 2014-03-05 00:57:15 | [diff] [blame] | 56 | void GCMClientMock::Stop() { |
| 57 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 58 | status_ = STOPPED; |
| 59 | } |
| 60 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 61 | void GCMClientMock::CheckOut() { |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 62 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 63 | status_ = CHECKED_OUT; |
[email protected] | 1b1c3cd | 2013-12-17 18:40:04 | [diff] [blame] | 64 | } |
| 65 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 66 | void GCMClientMock::Register(const std::string& app_id, |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 67 | const std::vector<std::string>& sender_ids) { |
| 68 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 69 | |
[email protected] | 1b1c3cd | 2013-12-17 18:40:04 | [diff] [blame] | 70 | std::string registration_id; |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 71 | if (error_simulation_ == ALWAYS_SUCCEED) |
[email protected] | 1b1c3cd | 2013-12-17 18:40:04 | [diff] [blame] | 72 | registration_id = GetRegistrationIdFromSenderIds(sender_ids); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 73 | |
| 74 | base::MessageLoop::current()->PostTask( |
| 75 | FROM_HERE, |
| 76 | base::Bind(&GCMClientMock::RegisterFinished, |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 77 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 78 | app_id, |
| 79 | registration_id)); |
| 80 | } |
| 81 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 82 | void GCMClientMock::Unregister(const std::string& app_id) { |
[email protected] | 0e88e1d1 | 2014-03-19 06:53:08 | [diff] [blame^] | 83 | 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] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 90 | } |
| 91 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 92 | void GCMClientMock::Send(const std::string& app_id, |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 93 | 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] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 100 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 101 | app_id, |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 102 | message)); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 103 | } |
| 104 | |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 105 | GCMClient::GCMStatistics GCMClientMock::GetStatistics() const { |
| 106 | return GCMClient::GCMStatistics(); |
| 107 | } |
| 108 | |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 109 | void 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] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 118 | void GCMClientMock::ReceiveMessage(const std::string& app_id, |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 119 | 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] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 126 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 127 | app_id, |
| 128 | message)); |
| 129 | } |
| 130 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 131 | void GCMClientMock::DeleteMessages(const std::string& app_id) { |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 132 | 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] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 138 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 139 | app_id)); |
| 140 | } |
| 141 | |
[email protected] | 1b1c3cd | 2013-12-17 18:40:04 | [diff] [blame] | 142 | // static |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 143 | std::string GCMClientMock::GetRegistrationIdFromSenderIds( |
[email protected] | 1b1c3cd | 2013-12-17 18:40:04 | [diff] [blame] | 144 | const std::vector<std::string>& sender_ids) { |
[email protected] | c7f7b53 | 2014-01-24 07:24:45 | [diff] [blame] | 145 | // 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] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 149 | // 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] | c7f7b53 | 2014-01-24 07:24:45 | [diff] [blame] | 155 | for (size_t i = 0; i < normalized_sender_ids.size(); ++i) { |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 156 | if (i > 0) |
| 157 | registration_id += ","; |
[email protected] | c7f7b53 | 2014-01-24 07:24:45 | [diff] [blame] | 158 | registration_id += normalized_sender_ids[i]; |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | return registration_id; |
| 162 | } |
| 163 | |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 164 | void GCMClientMock::CheckinFinished() { |
| 165 | delegate_->OnGCMReady(); |
| 166 | } |
| 167 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 168 | void GCMClientMock::RegisterFinished(const std::string& app_id, |
| 169 | const std::string& registrion_id) { |
| 170 | delegate_->OnRegisterFinished( |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 171 | app_id, registrion_id, registrion_id.empty() ? SERVER_ERROR : SUCCESS); |
| 172 | } |
| 173 | |
[email protected] | 0e88e1d1 | 2014-03-19 06:53:08 | [diff] [blame^] | 174 | void GCMClientMock::UnregisterFinished(const std::string& app_id) { |
| 175 | delegate_->OnUnregisterFinished(app_id, GCMClient::SUCCESS); |
| 176 | } |
| 177 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 178 | void GCMClientMock::SendFinished(const std::string& app_id, |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 179 | const OutgoingMessage& message) { |
| 180 | delegate_->OnSendFinished(app_id, message.id, SUCCESS); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 181 | |
| 182 | // Simulate send error if message id contains a hint. |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 183 | 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] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 188 | base::MessageLoop::current()->PostDelayedTask( |
| 189 | FROM_HERE, |
| 190 | base::Bind(&GCMClientMock::MessageSendError, |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 191 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 192 | app_id, |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 193 | send_error_details), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 194 | base::TimeDelta::FromMilliseconds(200)); |
| 195 | } |
| 196 | } |
| 197 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 198 | void GCMClientMock::MessageReceived(const std::string& app_id, |
| 199 | const IncomingMessage& message) { |
| 200 | if (delegate_) |
| 201 | delegate_->OnMessageReceived(app_id, message); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 202 | } |
| 203 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 204 | void GCMClientMock::MessagesDeleted(const std::string& app_id) { |
| 205 | if (delegate_) |
| 206 | delegate_->OnMessagesDeleted(app_id); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 207 | } |
| 208 | |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 209 | void GCMClientMock::MessageSendError( |
| 210 | const std::string& app_id, |
| 211 | const GCMClient::SendErrorDetails& send_error_details) { |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 212 | if (delegate_) |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 213 | delegate_->OnMessageSendError(app_id, send_error_details); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 214 | } |
| 215 | |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 216 | } // namespace gcm |