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