| 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/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/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/sys_byteorder.h" | 12 #include "base/sys_byteorder.h" |
| 12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 13 #include "google_apis/gcm/base/encryptor.h" | 14 #include "google_apis/gcm/base/encryptor.h" |
| 14 #include "google_apis/gcm/engine/account_mapping.h" | 15 #include "google_apis/gcm/engine/account_mapping.h" |
| 15 #include "net/base/ip_endpoint.h" | 16 #include "net/base/ip_endpoint.h" |
| 16 | 17 |
| 17 namespace gcm { | 18 namespace gcm { |
| 18 | 19 |
| 19 FakeGCMClient::FakeGCMClient( | 20 FakeGCMClient::FakeGCMClient( |
| 20 StartMode start_mode, | 21 StartMode start_mode, |
| 21 const scoped_refptr<base::SequencedTaskRunner>& ui_thread, | 22 const scoped_refptr<base::SequencedTaskRunner>& ui_thread, |
| 22 const scoped_refptr<base::SequencedTaskRunner>& io_thread) | 23 const scoped_refptr<base::SequencedTaskRunner>& io_thread) |
| 23 : delegate_(NULL), | 24 : delegate_(NULL), |
| 25 sequence_id_(0), |
| 24 status_(UNINITIALIZED), | 26 status_(UNINITIALIZED), |
| 25 start_mode_(start_mode), | 27 start_mode_(start_mode), |
| 26 ui_thread_(ui_thread), | 28 ui_thread_(ui_thread), |
| 27 io_thread_(io_thread), | 29 io_thread_(io_thread), |
| 28 weak_ptr_factory_(this) { | 30 weak_ptr_factory_(this) { |
| 29 } | 31 } |
| 30 | 32 |
| 31 FakeGCMClient::~FakeGCMClient() { | 33 FakeGCMClient::~FakeGCMClient() { |
| 32 } | 34 } |
| 33 | 35 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 61 | 63 |
| 62 void FakeGCMClient::Stop() { | 64 void FakeGCMClient::Stop() { |
| 63 DCHECK(io_thread_->RunsTasksOnCurrentThread()); | 65 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
| 64 status_ = STOPPED; | 66 status_ = STOPPED; |
| 65 delegate_->OnDisconnected(); | 67 delegate_->OnDisconnected(); |
| 66 } | 68 } |
| 67 | 69 |
| 68 void FakeGCMClient::CheckOut() { | 70 void FakeGCMClient::CheckOut() { |
| 69 DCHECK(io_thread_->RunsTasksOnCurrentThread()); | 71 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
| 70 status_ = CHECKED_OUT; | 72 status_ = CHECKED_OUT; |
| 73 sequence_id_++; |
| 71 } | 74 } |
| 72 | 75 |
| 73 void FakeGCMClient::Register(const std::string& app_id, | 76 void FakeGCMClient::Register(const std::string& app_id, |
| 74 const std::vector<std::string>& sender_ids) { | 77 const std::vector<std::string>& sender_ids) { |
| 75 DCHECK(io_thread_->RunsTasksOnCurrentThread()); | 78 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
| 76 | 79 |
| 77 std::string registration_id = GetRegistrationIdFromSenderIds(sender_ids); | 80 std::string registration_id = GetRegistrationIdFromSenderIds(sender_ids); |
| 78 base::MessageLoop::current()->PostTask( | 81 base::MessageLoop::current()->PostTask( |
| 79 FROM_HERE, | 82 FROM_HERE, |
| 80 base::Bind(&FakeGCMClient::RegisterFinished, | 83 base::Bind(&FakeGCMClient::RegisterFinished, |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 void FakeGCMClient::DeleteMessages(const std::string& app_id) { | 153 void FakeGCMClient::DeleteMessages(const std::string& app_id) { |
| 151 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 154 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 152 | 155 |
| 153 io_thread_->PostTask( | 156 io_thread_->PostTask( |
| 154 FROM_HERE, | 157 FROM_HERE, |
| 155 base::Bind(&FakeGCMClient::MessagesDeleted, | 158 base::Bind(&FakeGCMClient::MessagesDeleted, |
| 156 weak_ptr_factory_.GetWeakPtr(), | 159 weak_ptr_factory_.GetWeakPtr(), |
| 157 app_id)); | 160 app_id)); |
| 158 } | 161 } |
| 159 | 162 |
| 160 // static | |
| 161 std::string FakeGCMClient::GetRegistrationIdFromSenderIds( | 163 std::string FakeGCMClient::GetRegistrationIdFromSenderIds( |
| 162 const std::vector<std::string>& sender_ids) { | 164 const std::vector<std::string>& sender_ids) const { |
| 163 // GCMService normalizes the sender IDs by making them sorted. | 165 // GCMService normalizes the sender IDs by making them sorted. |
| 164 std::vector<std::string> normalized_sender_ids = sender_ids; | 166 std::vector<std::string> normalized_sender_ids = sender_ids; |
| 165 std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end()); | 167 std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end()); |
| 166 | 168 |
| 167 // Simulate the registration_id by concaternating all sender IDs. | 169 // Simulate the registration_id by concaternating all sender IDs. |
| 168 // Set registration_id to empty to denote an error if sender_ids contains a | 170 // Set registration_id to empty to denote an error if sender_ids contains a |
| 169 // hint. | 171 // hint. |
| 170 std::string registration_id; | 172 std::string registration_id; |
| 171 if (sender_ids.size() != 1 || | 173 if (sender_ids.size() != 1 || |
| 172 sender_ids[0].find("error") == std::string::npos) { | 174 sender_ids[0].find("error") == std::string::npos) { |
| 173 for (size_t i = 0; i < normalized_sender_ids.size(); ++i) { | 175 for (size_t i = 0; i < normalized_sender_ids.size(); ++i) { |
| 174 if (i > 0) | 176 if (i > 0) |
| 175 registration_id += ","; | 177 registration_id += ","; |
| 176 registration_id += normalized_sender_ids[i]; | 178 registration_id += normalized_sender_ids[i]; |
| 177 } | 179 } |
| 180 registration_id += base::IntToString(sequence_id_); |
| 178 } | 181 } |
| 179 return registration_id; | 182 return registration_id; |
| 180 } | 183 } |
| 181 | 184 |
| 182 void FakeGCMClient::CheckinFinished() { | 185 void FakeGCMClient::CheckinFinished() { |
| 183 delegate_->OnGCMReady(std::vector<AccountMapping>()); | 186 delegate_->OnGCMReady(std::vector<AccountMapping>()); |
| 184 delegate_->OnConnected(net::IPEndPoint()); | 187 delegate_->OnConnected(net::IPEndPoint()); |
| 185 } | 188 } |
| 186 | 189 |
| 187 void FakeGCMClient::RegisterFinished(const std::string& app_id, | 190 void FakeGCMClient::RegisterFinished(const std::string& app_id, |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 delegate_->OnMessageSendError(app_id, send_error_details); | 244 delegate_->OnMessageSendError(app_id, send_error_details); |
| 242 } | 245 } |
| 243 | 246 |
| 244 void FakeGCMClient::SendAcknowledgement(const std::string& app_id, | 247 void FakeGCMClient::SendAcknowledgement(const std::string& app_id, |
| 245 const std::string& message_id) { | 248 const std::string& message_id) { |
| 246 if (delegate_) | 249 if (delegate_) |
| 247 delegate_->OnSendAcknowledged(app_id, message_id); | 250 delegate_->OnSendAcknowledged(app_id, message_id); |
| 248 } | 251 } |
| 249 | 252 |
| 250 } // namespace gcm | 253 } // namespace gcm |
| OLD | NEW |