[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] | 2ac4673 | 2014-04-03 23:14:31 | [diff] [blame] | 16 | GCMClientMock::GCMClientMock(LoadingDelay loading_delay) |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 17 | : delegate_(NULL), |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 18 | status_(UNINITIALIZED), |
| 19 | loading_delay_(loading_delay), |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 20 | weak_ptr_factory_(this) { |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | GCMClientMock::~GCMClientMock() { |
| 24 | } |
| 25 | |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 26 | void GCMClientMock::Initialize( |
| 27 | const checkin_proto::ChromeBuildProto& chrome_build_proto, |
| 28 | const base::FilePath& store_path, |
[email protected] | 495a7db9 | 2014-02-22 07:49:59 | [diff] [blame] | 29 | const std::vector<std::string>& account_ids, |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 30 | const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner, |
| 31 | const scoped_refptr<net::URLRequestContextGetter>& |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 32 | url_request_context_getter, |
| 33 | Delegate* delegate) { |
| 34 | delegate_ = delegate; |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 35 | } |
| 36 | |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 37 | void GCMClientMock::Load() { |
| 38 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 39 | DCHECK_NE(LOADED, status_); |
| 40 | |
| 41 | if (loading_delay_ == DELAY_LOADING) |
| 42 | return; |
| 43 | DoLoading(); |
| 44 | } |
| 45 | |
| 46 | void GCMClientMock::DoLoading() { |
| 47 | status_ = LOADED; |
| 48 | base::MessageLoop::current()->PostTask( |
| 49 | FROM_HERE, |
| 50 | base::Bind(&GCMClientMock::CheckinFinished, |
| 51 | weak_ptr_factory_.GetWeakPtr())); |
| 52 | } |
| 53 | |
[email protected] | 21fee548 | 2014-03-05 00:57:15 | [diff] [blame] | 54 | void GCMClientMock::Stop() { |
| 55 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 56 | status_ = STOPPED; |
| 57 | } |
| 58 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 59 | void GCMClientMock::CheckOut() { |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 60 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 61 | status_ = CHECKED_OUT; |
[email protected] | 1b1c3cd | 2013-12-17 18:40:04 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 64 | void GCMClientMock::Register(const std::string& app_id, |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 65 | const std::vector<std::string>& sender_ids) { |
| 66 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 67 | |
[email protected] | 2ac4673 | 2014-04-03 23:14:31 | [diff] [blame] | 68 | std::string registration_id = GetRegistrationIdFromSenderIds(sender_ids); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 69 | base::MessageLoop::current()->PostTask( |
| 70 | FROM_HERE, |
| 71 | base::Bind(&GCMClientMock::RegisterFinished, |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 72 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 73 | app_id, |
| 74 | registration_id)); |
| 75 | } |
| 76 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 77 | void GCMClientMock::Unregister(const std::string& app_id) { |
[email protected] | 0e88e1d1 | 2014-03-19 06:53:08 | [diff] [blame] | 78 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 79 | |
| 80 | base::MessageLoop::current()->PostTask( |
| 81 | FROM_HERE, |
| 82 | base::Bind(&GCMClientMock::UnregisterFinished, |
| 83 | weak_ptr_factory_.GetWeakPtr(), |
| 84 | app_id)); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 87 | void GCMClientMock::Send(const std::string& app_id, |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 88 | const std::string& receiver_id, |
| 89 | const OutgoingMessage& message) { |
| 90 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 91 | |
| 92 | base::MessageLoop::current()->PostTask( |
| 93 | FROM_HERE, |
| 94 | base::Bind(&GCMClientMock::SendFinished, |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 95 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 96 | app_id, |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 97 | message)); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame^] | 100 | void GCMClientMock::SetRecording(bool recording) { |
| 101 | } |
| 102 | |
| 103 | void GCMClientMock::ClearActivityLogs() { |
| 104 | } |
| 105 | |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 106 | GCMClient::GCMStatistics GCMClientMock::GetStatistics() const { |
| 107 | return GCMClient::GCMStatistics(); |
| 108 | } |
| 109 | |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 110 | void GCMClientMock::PerformDelayedLoading() { |
| 111 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 112 | |
| 113 | content::BrowserThread::PostTask( |
| 114 | content::BrowserThread::IO, |
| 115 | FROM_HERE, |
| 116 | base::Bind(&GCMClientMock::DoLoading, weak_ptr_factory_.GetWeakPtr())); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 119 | void GCMClientMock::ReceiveMessage(const std::string& app_id, |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 120 | const IncomingMessage& message) { |
| 121 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 122 | |
| 123 | content::BrowserThread::PostTask( |
| 124 | content::BrowserThread::IO, |
| 125 | FROM_HERE, |
| 126 | base::Bind(&GCMClientMock::MessageReceived, |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 127 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 128 | app_id, |
| 129 | message)); |
| 130 | } |
| 131 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 132 | void GCMClientMock::DeleteMessages(const std::string& app_id) { |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 133 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 134 | |
| 135 | content::BrowserThread::PostTask( |
| 136 | content::BrowserThread::IO, |
| 137 | FROM_HERE, |
| 138 | base::Bind(&GCMClientMock::MessagesDeleted, |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 139 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 140 | app_id)); |
| 141 | } |
| 142 | |
[email protected] | 1b1c3cd | 2013-12-17 18:40:04 | [diff] [blame] | 143 | // static |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 144 | std::string GCMClientMock::GetRegistrationIdFromSenderIds( |
[email protected] | 1b1c3cd | 2013-12-17 18:40:04 | [diff] [blame] | 145 | const std::vector<std::string>& sender_ids) { |
[email protected] | c7f7b53 | 2014-01-24 07:24:45 | [diff] [blame] | 146 | // GCMProfileService normalizes the sender IDs by making them sorted. |
| 147 | std::vector<std::string> normalized_sender_ids = sender_ids; |
| 148 | std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end()); |
| 149 | |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 150 | // Simulate the registration_id by concaternating all sender IDs. |
| 151 | // Set registration_id to empty to denote an error if sender_ids contains a |
| 152 | // hint. |
| 153 | std::string registration_id; |
| 154 | if (sender_ids.size() != 1 || |
| 155 | sender_ids[0].find("error") == std::string::npos) { |
[email protected] | c7f7b53 | 2014-01-24 07:24:45 | [diff] [blame] | 156 | for (size_t i = 0; i < normalized_sender_ids.size(); ++i) { |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 157 | if (i > 0) |
| 158 | registration_id += ","; |
[email protected] | c7f7b53 | 2014-01-24 07:24:45 | [diff] [blame] | 159 | registration_id += normalized_sender_ids[i]; |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | return registration_id; |
| 163 | } |
| 164 | |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 165 | void GCMClientMock::CheckinFinished() { |
| 166 | delegate_->OnGCMReady(); |
| 167 | } |
| 168 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 169 | void GCMClientMock::RegisterFinished(const std::string& app_id, |
| 170 | const std::string& registrion_id) { |
| 171 | delegate_->OnRegisterFinished( |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 172 | app_id, registrion_id, registrion_id.empty() ? SERVER_ERROR : SUCCESS); |
| 173 | } |
| 174 | |
[email protected] | 0e88e1d1 | 2014-03-19 06:53:08 | [diff] [blame] | 175 | void GCMClientMock::UnregisterFinished(const std::string& app_id) { |
| 176 | delegate_->OnUnregisterFinished(app_id, GCMClient::SUCCESS); |
| 177 | } |
| 178 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 179 | void GCMClientMock::SendFinished(const std::string& app_id, |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 180 | const OutgoingMessage& message) { |
| 181 | delegate_->OnSendFinished(app_id, message.id, SUCCESS); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 182 | |
| 183 | // Simulate send error if message id contains a hint. |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 184 | if (message.id.find("error") != std::string::npos) { |
| 185 | SendErrorDetails send_error_details; |
| 186 | send_error_details.message_id = message.id; |
| 187 | send_error_details.result = NETWORK_ERROR; |
| 188 | send_error_details.additional_data = message.data; |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 189 | base::MessageLoop::current()->PostDelayedTask( |
| 190 | FROM_HERE, |
| 191 | base::Bind(&GCMClientMock::MessageSendError, |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 192 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 193 | app_id, |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 194 | send_error_details), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 195 | base::TimeDelta::FromMilliseconds(200)); |
| 196 | } |
| 197 | } |
| 198 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 199 | void GCMClientMock::MessageReceived(const std::string& app_id, |
| 200 | const IncomingMessage& message) { |
| 201 | if (delegate_) |
| 202 | delegate_->OnMessageReceived(app_id, message); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 203 | } |
| 204 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 205 | void GCMClientMock::MessagesDeleted(const std::string& app_id) { |
| 206 | if (delegate_) |
| 207 | delegate_->OnMessagesDeleted(app_id); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 208 | } |
| 209 | |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 210 | void GCMClientMock::MessageSendError( |
| 211 | const std::string& app_id, |
| 212 | const GCMClient::SendErrorDetails& send_error_details) { |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 213 | if (delegate_) |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 214 | delegate_->OnMessageSendError(app_id, send_error_details); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 215 | } |
| 216 | |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 217 | } // namespace gcm |