[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::string& cert, |
| 68 | const std::vector<std::string>& sender_ids) { |
| 69 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 70 | |
[email protected] | 1b1c3cd | 2013-12-17 18:40:04 | [diff] [blame] | 71 | std::string registration_id; |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 72 | if (error_simulation_ == ALWAYS_SUCCEED) |
[email protected] | 1b1c3cd | 2013-12-17 18:40:04 | [diff] [blame] | 73 | registration_id = GetRegistrationIdFromSenderIds(sender_ids); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 74 | |
| 75 | base::MessageLoop::current()->PostTask( |
| 76 | FROM_HERE, |
| 77 | base::Bind(&GCMClientMock::RegisterFinished, |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 78 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 79 | app_id, |
| 80 | registration_id)); |
| 81 | } |
| 82 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 83 | void GCMClientMock::Unregister(const std::string& app_id) { |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 84 | } |
| 85 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 86 | void GCMClientMock::Send(const std::string& app_id, |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 87 | 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] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 94 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 95 | app_id, |
| 96 | message.id)); |
| 97 | } |
| 98 | |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 99 | void 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] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 106 | } |
| 107 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 108 | void GCMClientMock::ReceiveMessage(const std::string& app_id, |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 109 | 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] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 116 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 117 | app_id, |
| 118 | message)); |
| 119 | } |
| 120 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 121 | void GCMClientMock::DeleteMessages(const std::string& app_id) { |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 122 | 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] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 128 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 129 | app_id)); |
| 130 | } |
| 131 | |
[email protected] | 1b1c3cd | 2013-12-17 18:40:04 | [diff] [blame] | 132 | // static |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 133 | std::string GCMClientMock::GetRegistrationIdFromSenderIds( |
[email protected] | 1b1c3cd | 2013-12-17 18:40:04 | [diff] [blame] | 134 | const std::vector<std::string>& sender_ids) { |
[email protected] | c7f7b53 | 2014-01-24 07:24:45 | [diff] [blame] | 135 | // 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] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 139 | // 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] | c7f7b53 | 2014-01-24 07:24:45 | [diff] [blame] | 145 | for (size_t i = 0; i < normalized_sender_ids.size(); ++i) { |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 146 | if (i > 0) |
| 147 | registration_id += ","; |
[email protected] | c7f7b53 | 2014-01-24 07:24:45 | [diff] [blame] | 148 | registration_id += normalized_sender_ids[i]; |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | return registration_id; |
| 152 | } |
| 153 | |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 154 | void GCMClientMock::CheckinFinished() { |
| 155 | delegate_->OnGCMReady(); |
| 156 | } |
| 157 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 158 | void GCMClientMock::RegisterFinished(const std::string& app_id, |
| 159 | const std::string& registrion_id) { |
| 160 | delegate_->OnRegisterFinished( |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 161 | app_id, registrion_id, registrion_id.empty() ? SERVER_ERROR : SUCCESS); |
| 162 | } |
| 163 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 164 | void GCMClientMock::SendFinished(const std::string& app_id, |
| 165 | const std::string& message_id) { |
| 166 | delegate_->OnSendFinished(app_id, message_id, SUCCESS); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 167 | |
| 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] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 173 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 174 | app_id, |
| 175 | message_id), |
| 176 | base::TimeDelta::FromMilliseconds(200)); |
| 177 | } |
| 178 | } |
| 179 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 180 | void GCMClientMock::MessageReceived(const std::string& app_id, |
| 181 | const IncomingMessage& message) { |
| 182 | if (delegate_) |
| 183 | delegate_->OnMessageReceived(app_id, message); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 186 | void GCMClientMock::MessagesDeleted(const std::string& app_id) { |
| 187 | if (delegate_) |
| 188 | delegate_->OnMessagesDeleted(app_id); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 189 | } |
| 190 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 191 | void 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] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 195 | } |
| 196 | |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 197 | } // namespace gcm |