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