| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "components/gcm_driver/fake_gcm_client.h" | 5 #include "components/gcm_driver/fake_gcm_client.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" |
| 8 #include "base/logging.h" | 9 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
| 11 #include "base/single_thread_task_runner.h" |
| 11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/sys_byteorder.h" | 13 #include "base/sys_byteorder.h" |
| 14 #include "base/thread_task_runner_handle.h" |
| 13 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 14 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
| 15 #include "google_apis/gcm/base/encryptor.h" | 17 #include "google_apis/gcm/base/encryptor.h" |
| 16 #include "google_apis/gcm/engine/account_mapping.h" | 18 #include "google_apis/gcm/engine/account_mapping.h" |
| 17 #include "net/base/ip_endpoint.h" | 19 #include "net/base/ip_endpoint.h" |
| 18 | 20 |
| 19 namespace gcm { | 21 namespace gcm { |
| 20 | 22 |
| 21 // static | 23 // static |
| 22 std::string FakeGCMClient::GenerateGCMRegistrationID( | 24 std::string FakeGCMClient::GenerateGCMRegistrationID( |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 if (start_mode_ == DELAYED_START || | 90 if (start_mode_ == DELAYED_START || |
| 89 start_mode_overridding_ == FORCE_TO_ALWAYS_DELAY_START_GCM) { | 91 start_mode_overridding_ == FORCE_TO_ALWAYS_DELAY_START_GCM) { |
| 90 return; | 92 return; |
| 91 } | 93 } |
| 92 | 94 |
| 93 DoStart(); | 95 DoStart(); |
| 94 } | 96 } |
| 95 | 97 |
| 96 void FakeGCMClient::DoStart() { | 98 void FakeGCMClient::DoStart() { |
| 97 started_ = true; | 99 started_ = true; |
| 98 base::MessageLoop::current()->PostTask( | 100 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 99 FROM_HERE, | 101 FROM_HERE, |
| 100 base::Bind(&FakeGCMClient::Started, | 102 base::Bind(&FakeGCMClient::Started, weak_ptr_factory_.GetWeakPtr())); |
| 101 weak_ptr_factory_.GetWeakPtr())); | |
| 102 } | 103 } |
| 103 | 104 |
| 104 void FakeGCMClient::Stop() { | 105 void FakeGCMClient::Stop() { |
| 105 DCHECK(io_thread_->RunsTasksOnCurrentThread()); | 106 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
| 106 started_ = false; | 107 started_ = false; |
| 107 delegate_->OnDisconnected(); | 108 delegate_->OnDisconnected(); |
| 108 } | 109 } |
| 109 | 110 |
| 110 void FakeGCMClient::Register( | 111 void FakeGCMClient::Register( |
| 111 const linked_ptr<RegistrationInfo>& registration_info) { | 112 const linked_ptr<RegistrationInfo>& registration_info) { |
| 112 DCHECK(io_thread_->RunsTasksOnCurrentThread()); | 113 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
| 113 | 114 |
| 114 std::string registration_id; | 115 std::string registration_id; |
| 115 | 116 |
| 116 GCMRegistrationInfo* gcm_registration_info = | 117 GCMRegistrationInfo* gcm_registration_info = |
| 117 GCMRegistrationInfo::FromRegistrationInfo(registration_info.get()); | 118 GCMRegistrationInfo::FromRegistrationInfo(registration_info.get()); |
| 118 if (gcm_registration_info) { | 119 if (gcm_registration_info) { |
| 119 registration_id = GenerateGCMRegistrationID( | 120 registration_id = GenerateGCMRegistrationID( |
| 120 gcm_registration_info->sender_ids); | 121 gcm_registration_info->sender_ids); |
| 121 } | 122 } |
| 122 | 123 |
| 123 InstanceIDTokenInfo* instance_id_token_info = | 124 InstanceIDTokenInfo* instance_id_token_info = |
| 124 InstanceIDTokenInfo::FromRegistrationInfo(registration_info.get()); | 125 InstanceIDTokenInfo::FromRegistrationInfo(registration_info.get()); |
| 125 if (instance_id_token_info) { | 126 if (instance_id_token_info) { |
| 126 registration_id = GenerateInstanceIDToken( | 127 registration_id = GenerateInstanceIDToken( |
| 127 instance_id_token_info->authorized_entity, | 128 instance_id_token_info->authorized_entity, |
| 128 instance_id_token_info->scope); | 129 instance_id_token_info->scope); |
| 129 } | 130 } |
| 130 | 131 |
| 131 base::MessageLoop::current()->PostTask( | 132 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 132 FROM_HERE, | 133 FROM_HERE, base::Bind(&FakeGCMClient::RegisterFinished, |
| 133 base::Bind(&FakeGCMClient::RegisterFinished, | 134 weak_ptr_factory_.GetWeakPtr(), registration_info, |
| 134 weak_ptr_factory_.GetWeakPtr(), | 135 registration_id)); |
| 135 registration_info, | |
| 136 registration_id)); | |
| 137 } | 136 } |
| 138 | 137 |
| 139 void FakeGCMClient::Unregister( | 138 void FakeGCMClient::Unregister( |
| 140 const linked_ptr<RegistrationInfo>& registration_info) { | 139 const linked_ptr<RegistrationInfo>& registration_info) { |
| 141 DCHECK(io_thread_->RunsTasksOnCurrentThread()); | 140 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
| 142 | 141 |
| 143 base::MessageLoop::current()->PostTask( | 142 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 144 FROM_HERE, | 143 FROM_HERE, base::Bind(&FakeGCMClient::UnregisterFinished, |
| 145 base::Bind(&FakeGCMClient::UnregisterFinished, | 144 weak_ptr_factory_.GetWeakPtr(), registration_info)); |
| 146 weak_ptr_factory_.GetWeakPtr(), | |
| 147 registration_info)); | |
| 148 } | 145 } |
| 149 | 146 |
| 150 void FakeGCMClient::Send(const std::string& app_id, | 147 void FakeGCMClient::Send(const std::string& app_id, |
| 151 const std::string& receiver_id, | 148 const std::string& receiver_id, |
| 152 const OutgoingMessage& message) { | 149 const OutgoingMessage& message) { |
| 153 DCHECK(io_thread_->RunsTasksOnCurrentThread()); | 150 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
| 154 | 151 |
| 155 base::MessageLoop::current()->PostTask( | 152 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 156 FROM_HERE, | 153 FROM_HERE, base::Bind(&FakeGCMClient::SendFinished, |
| 157 base::Bind(&FakeGCMClient::SendFinished, | 154 weak_ptr_factory_.GetWeakPtr(), app_id, message)); |
| 158 weak_ptr_factory_.GetWeakPtr(), | |
| 159 app_id, | |
| 160 message)); | |
| 161 } | 155 } |
| 162 | 156 |
| 163 void FakeGCMClient::SetRecording(bool recording) { | 157 void FakeGCMClient::SetRecording(bool recording) { |
| 164 } | 158 } |
| 165 | 159 |
| 166 void FakeGCMClient::ClearActivityLogs() { | 160 void FakeGCMClient::ClearActivityLogs() { |
| 167 } | 161 } |
| 168 | 162 |
| 169 GCMClient::GCMStatistics FakeGCMClient::GetStatistics() const { | 163 GCMClient::GCMStatistics FakeGCMClient::GetStatistics() const { |
| 170 return GCMClient::GCMStatistics(); | 164 return GCMClient::GCMStatistics(); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 void FakeGCMClient::SendFinished(const std::string& app_id, | 253 void FakeGCMClient::SendFinished(const std::string& app_id, |
| 260 const OutgoingMessage& message) { | 254 const OutgoingMessage& message) { |
| 261 delegate_->OnSendFinished(app_id, message.id, SUCCESS); | 255 delegate_->OnSendFinished(app_id, message.id, SUCCESS); |
| 262 | 256 |
| 263 // Simulate send error if message id contains a hint. | 257 // Simulate send error if message id contains a hint. |
| 264 if (message.id.find("error") != std::string::npos) { | 258 if (message.id.find("error") != std::string::npos) { |
| 265 SendErrorDetails send_error_details; | 259 SendErrorDetails send_error_details; |
| 266 send_error_details.message_id = message.id; | 260 send_error_details.message_id = message.id; |
| 267 send_error_details.result = NETWORK_ERROR; | 261 send_error_details.result = NETWORK_ERROR; |
| 268 send_error_details.additional_data = message.data; | 262 send_error_details.additional_data = message.data; |
| 269 base::MessageLoop::current()->PostDelayedTask( | 263 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 270 FROM_HERE, | 264 FROM_HERE, |
| 271 base::Bind(&FakeGCMClient::MessageSendError, | 265 base::Bind(&FakeGCMClient::MessageSendError, |
| 272 weak_ptr_factory_.GetWeakPtr(), | 266 weak_ptr_factory_.GetWeakPtr(), app_id, send_error_details), |
| 273 app_id, | |
| 274 send_error_details), | |
| 275 base::TimeDelta::FromMilliseconds(200)); | 267 base::TimeDelta::FromMilliseconds(200)); |
| 276 } else if(message.id.find("ack") != std::string::npos) { | 268 } else if(message.id.find("ack") != std::string::npos) { |
| 277 base::MessageLoop::current()->PostDelayedTask( | 269 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 278 FROM_HERE, | 270 FROM_HERE, |
| 279 base::Bind(&FakeGCMClient::SendAcknowledgement, | 271 base::Bind(&FakeGCMClient::SendAcknowledgement, |
| 280 weak_ptr_factory_.GetWeakPtr(), | 272 weak_ptr_factory_.GetWeakPtr(), app_id, message.id), |
| 281 app_id, | |
| 282 message.id), | |
| 283 base::TimeDelta::FromMilliseconds(200)); | 273 base::TimeDelta::FromMilliseconds(200)); |
| 284 | |
| 285 } | 274 } |
| 286 } | 275 } |
| 287 | 276 |
| 288 void FakeGCMClient::MessageReceived(const std::string& app_id, | 277 void FakeGCMClient::MessageReceived(const std::string& app_id, |
| 289 const IncomingMessage& message) { | 278 const IncomingMessage& message) { |
| 290 if (delegate_) | 279 if (delegate_) |
| 291 delegate_->OnMessageReceived(app_id, message); | 280 delegate_->OnMessageReceived(app_id, message); |
| 292 } | 281 } |
| 293 | 282 |
| 294 void FakeGCMClient::MessagesDeleted(const std::string& app_id) { | 283 void FakeGCMClient::MessagesDeleted(const std::string& app_id) { |
| 295 if (delegate_) | 284 if (delegate_) |
| 296 delegate_->OnMessagesDeleted(app_id); | 285 delegate_->OnMessagesDeleted(app_id); |
| 297 } | 286 } |
| 298 | 287 |
| 299 void FakeGCMClient::MessageSendError( | 288 void FakeGCMClient::MessageSendError( |
| 300 const std::string& app_id, | 289 const std::string& app_id, |
| 301 const GCMClient::SendErrorDetails& send_error_details) { | 290 const GCMClient::SendErrorDetails& send_error_details) { |
| 302 if (delegate_) | 291 if (delegate_) |
| 303 delegate_->OnMessageSendError(app_id, send_error_details); | 292 delegate_->OnMessageSendError(app_id, send_error_details); |
| 304 } | 293 } |
| 305 | 294 |
| 306 void FakeGCMClient::SendAcknowledgement(const std::string& app_id, | 295 void FakeGCMClient::SendAcknowledgement(const std::string& app_id, |
| 307 const std::string& message_id) { | 296 const std::string& message_id) { |
| 308 if (delegate_) | 297 if (delegate_) |
| 309 delegate_->OnSendAcknowledged(app_id, message_id); | 298 delegate_->OnSendAcknowledged(app_id, message_id); |
| 310 } | 299 } |
| 311 | 300 |
| 312 } // namespace gcm | 301 } // namespace gcm |
| OLD | NEW |