| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/gcm_client_mock.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 | 13 |
| 14 namespace gcm { | 14 namespace gcm { |
| 15 | 15 |
| 16 GCMClientMock::GCMClientMock(Status status, ErrorSimulation error_simulation) | 16 GCMClientMock::GCMClientMock(LoadingDelay loading_delay, |
| 17 ErrorSimulation error_simulation) |
| 17 : delegate_(NULL), | 18 : delegate_(NULL), |
| 18 status_(status), | 19 status_(UNINITIALIZED), |
| 19 error_simulation_(error_simulation) { | 20 loading_delay_(loading_delay), |
| 21 error_simulation_(error_simulation), |
| 22 weak_ptr_factory_(this) { |
| 20 } | 23 } |
| 21 | 24 |
| 22 GCMClientMock::~GCMClientMock() { | 25 GCMClientMock::~GCMClientMock() { |
| 23 } | 26 } |
| 24 | 27 |
| 25 void GCMClientMock::Initialize( | 28 void GCMClientMock::Initialize( |
| 26 const checkin_proto::ChromeBuildProto& chrome_build_proto, | 29 const checkin_proto::ChromeBuildProto& chrome_build_proto, |
| 27 const base::FilePath& store_path, | 30 const base::FilePath& store_path, |
| 28 const std::vector<std::string>& account_ids, | 31 const std::vector<std::string>& account_ids, |
| 29 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner, | 32 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner, |
| 30 const scoped_refptr<net::URLRequestContextGetter>& | 33 const scoped_refptr<net::URLRequestContextGetter>& |
| 31 url_request_context_getter, | 34 url_request_context_getter, |
| 32 Delegate* delegate) { | 35 Delegate* delegate) { |
| 33 delegate_ = delegate; | 36 delegate_ = delegate; |
| 34 } | 37 } |
| 35 | 38 |
| 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 |
| 36 void GCMClientMock::CheckOut() { | 56 void GCMClientMock::CheckOut() { |
| 37 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 57 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 58 status_ = CHECKED_OUT; |
| 38 } | 59 } |
| 39 | 60 |
| 40 void GCMClientMock::Register(const std::string& app_id, | 61 void GCMClientMock::Register(const std::string& app_id, |
| 41 const std::string& cert, | 62 const std::string& cert, |
| 42 const std::vector<std::string>& sender_ids) { | 63 const std::vector<std::string>& sender_ids) { |
| 43 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 64 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 44 | 65 |
| 45 std::string registration_id; | 66 std::string registration_id; |
| 46 if (error_simulation_ == ALWAYS_SUCCEED) | 67 if (error_simulation_ == ALWAYS_SUCCEED) |
| 47 registration_id = GetRegistrationIdFromSenderIds(sender_ids); | 68 registration_id = GetRegistrationIdFromSenderIds(sender_ids); |
| 48 | 69 |
| 49 base::MessageLoop::current()->PostTask( | 70 base::MessageLoop::current()->PostTask( |
| 50 FROM_HERE, | 71 FROM_HERE, |
| 51 base::Bind(&GCMClientMock::RegisterFinished, | 72 base::Bind(&GCMClientMock::RegisterFinished, |
| 52 base::Unretained(this), | 73 weak_ptr_factory_.GetWeakPtr(), |
| 53 app_id, | 74 app_id, |
| 54 registration_id)); | 75 registration_id)); |
| 55 } | 76 } |
| 56 | 77 |
| 57 void GCMClientMock::Unregister(const std::string& app_id) { | 78 void GCMClientMock::Unregister(const std::string& app_id) { |
| 58 } | 79 } |
| 59 | 80 |
| 60 void GCMClientMock::Send(const std::string& app_id, | 81 void GCMClientMock::Send(const std::string& app_id, |
| 61 const std::string& receiver_id, | 82 const std::string& receiver_id, |
| 62 const OutgoingMessage& message) { | 83 const OutgoingMessage& message) { |
| 63 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 84 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 64 | 85 |
| 65 base::MessageLoop::current()->PostTask( | 86 base::MessageLoop::current()->PostTask( |
| 66 FROM_HERE, | 87 FROM_HERE, |
| 67 base::Bind(&GCMClientMock::SendFinished, | 88 base::Bind(&GCMClientMock::SendFinished, |
| 68 base::Unretained(this), | 89 weak_ptr_factory_.GetWeakPtr(), |
| 69 app_id, | 90 app_id, |
| 70 message.id)); | 91 message.id)); |
| 71 } | 92 } |
| 72 | 93 |
| 73 bool GCMClientMock::IsReady() const { | 94 void GCMClientMock::PerformDelayedLoading() { |
| 74 return status_ == READY; | 95 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 96 |
| 97 content::BrowserThread::PostTask( |
| 98 content::BrowserThread::IO, |
| 99 FROM_HERE, |
| 100 base::Bind(&GCMClientMock::DoLoading, weak_ptr_factory_.GetWeakPtr())); |
| 75 } | 101 } |
| 76 | 102 |
| 77 void GCMClientMock::ReceiveMessage(const std::string& app_id, | 103 void GCMClientMock::ReceiveMessage(const std::string& app_id, |
| 78 const IncomingMessage& message) { | 104 const IncomingMessage& message) { |
| 79 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 105 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 80 | 106 |
| 81 content::BrowserThread::PostTask( | 107 content::BrowserThread::PostTask( |
| 82 content::BrowserThread::IO, | 108 content::BrowserThread::IO, |
| 83 FROM_HERE, | 109 FROM_HERE, |
| 84 base::Bind(&GCMClientMock::MessageReceived, | 110 base::Bind(&GCMClientMock::MessageReceived, |
| 85 base::Unretained(this), | 111 weak_ptr_factory_.GetWeakPtr(), |
| 86 app_id, | 112 app_id, |
| 87 message)); | 113 message)); |
| 88 } | 114 } |
| 89 | 115 |
| 90 void GCMClientMock::DeleteMessages(const std::string& app_id) { | 116 void GCMClientMock::DeleteMessages(const std::string& app_id) { |
| 91 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 117 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 92 | 118 |
| 93 content::BrowserThread::PostTask( | 119 content::BrowserThread::PostTask( |
| 94 content::BrowserThread::IO, | 120 content::BrowserThread::IO, |
| 95 FROM_HERE, | 121 FROM_HERE, |
| 96 base::Bind(&GCMClientMock::MessagesDeleted, | 122 base::Bind(&GCMClientMock::MessagesDeleted, |
| 97 base::Unretained(this), | 123 weak_ptr_factory_.GetWeakPtr(), |
| 98 app_id)); | 124 app_id)); |
| 99 } | 125 } |
| 100 | 126 |
| 101 void GCMClientMock::SetReady() { | |
| 102 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 103 DCHECK_EQ(status_, NOT_READY); | |
| 104 | |
| 105 status_ = READY; | |
| 106 content::BrowserThread::PostTask( | |
| 107 content::BrowserThread::IO, | |
| 108 FROM_HERE, | |
| 109 base::Bind(&GCMClientMock::SetReadyOnIO, | |
| 110 base::Unretained(this))); | |
| 111 } | |
| 112 | |
| 113 // static | 127 // static |
| 114 std::string GCMClientMock::GetRegistrationIdFromSenderIds( | 128 std::string GCMClientMock::GetRegistrationIdFromSenderIds( |
| 115 const std::vector<std::string>& sender_ids) { | 129 const std::vector<std::string>& sender_ids) { |
| 116 // GCMProfileService normalizes the sender IDs by making them sorted. | 130 // GCMProfileService normalizes the sender IDs by making them sorted. |
| 117 std::vector<std::string> normalized_sender_ids = sender_ids; | 131 std::vector<std::string> normalized_sender_ids = sender_ids; |
| 118 std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end()); | 132 std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end()); |
| 119 | 133 |
| 120 // Simulate the registration_id by concaternating all sender IDs. | 134 // Simulate the registration_id by concaternating all sender IDs. |
| 121 // Set registration_id to empty to denote an error if sender_ids contains a | 135 // Set registration_id to empty to denote an error if sender_ids contains a |
| 122 // hint. | 136 // hint. |
| 123 std::string registration_id; | 137 std::string registration_id; |
| 124 if (sender_ids.size() != 1 || | 138 if (sender_ids.size() != 1 || |
| 125 sender_ids[0].find("error") == std::string::npos) { | 139 sender_ids[0].find("error") == std::string::npos) { |
| 126 for (size_t i = 0; i < normalized_sender_ids.size(); ++i) { | 140 for (size_t i = 0; i < normalized_sender_ids.size(); ++i) { |
| 127 if (i > 0) | 141 if (i > 0) |
| 128 registration_id += ","; | 142 registration_id += ","; |
| 129 registration_id += normalized_sender_ids[i]; | 143 registration_id += normalized_sender_ids[i]; |
| 130 } | 144 } |
| 131 } | 145 } |
| 132 return registration_id; | 146 return registration_id; |
| 133 } | 147 } |
| 134 | 148 |
| 149 void GCMClientMock::CheckinFinished() { |
| 150 delegate_->OnGCMReady(); |
| 151 } |
| 152 |
| 135 void GCMClientMock::RegisterFinished(const std::string& app_id, | 153 void GCMClientMock::RegisterFinished(const std::string& app_id, |
| 136 const std::string& registrion_id) { | 154 const std::string& registrion_id) { |
| 137 delegate_->OnRegisterFinished( | 155 delegate_->OnRegisterFinished( |
| 138 app_id, registrion_id, registrion_id.empty() ? SERVER_ERROR : SUCCESS); | 156 app_id, registrion_id, registrion_id.empty() ? SERVER_ERROR : SUCCESS); |
| 139 } | 157 } |
| 140 | 158 |
| 141 void GCMClientMock::SendFinished(const std::string& app_id, | 159 void GCMClientMock::SendFinished(const std::string& app_id, |
| 142 const std::string& message_id) { | 160 const std::string& message_id) { |
| 143 delegate_->OnSendFinished(app_id, message_id, SUCCESS); | 161 delegate_->OnSendFinished(app_id, message_id, SUCCESS); |
| 144 | 162 |
| 145 // Simulate send error if message id contains a hint. | 163 // Simulate send error if message id contains a hint. |
| 146 if (message_id.find("error") != std::string::npos) { | 164 if (message_id.find("error") != std::string::npos) { |
| 147 base::MessageLoop::current()->PostDelayedTask( | 165 base::MessageLoop::current()->PostDelayedTask( |
| 148 FROM_HERE, | 166 FROM_HERE, |
| 149 base::Bind(&GCMClientMock::MessageSendError, | 167 base::Bind(&GCMClientMock::MessageSendError, |
| 150 base::Unretained(this), | 168 weak_ptr_factory_.GetWeakPtr(), |
| 151 app_id, | 169 app_id, |
| 152 message_id), | 170 message_id), |
| 153 base::TimeDelta::FromMilliseconds(200)); | 171 base::TimeDelta::FromMilliseconds(200)); |
| 154 } | 172 } |
| 155 } | 173 } |
| 156 | 174 |
| 157 void GCMClientMock::MessageReceived(const std::string& app_id, | 175 void GCMClientMock::MessageReceived(const std::string& app_id, |
| 158 const IncomingMessage& message) { | 176 const IncomingMessage& message) { |
| 159 if (delegate_) | 177 if (delegate_) |
| 160 delegate_->OnMessageReceived(app_id, message); | 178 delegate_->OnMessageReceived(app_id, message); |
| 161 } | 179 } |
| 162 | 180 |
| 163 void GCMClientMock::MessagesDeleted(const std::string& app_id) { | 181 void GCMClientMock::MessagesDeleted(const std::string& app_id) { |
| 164 if (delegate_) | 182 if (delegate_) |
| 165 delegate_->OnMessagesDeleted(app_id); | 183 delegate_->OnMessagesDeleted(app_id); |
| 166 } | 184 } |
| 167 | 185 |
| 168 void GCMClientMock::MessageSendError(const std::string& app_id, | 186 void GCMClientMock::MessageSendError(const std::string& app_id, |
| 169 const std::string& message_id) { | 187 const std::string& message_id) { |
| 170 if (delegate_) | 188 if (delegate_) |
| 171 delegate_->OnMessageSendError(app_id, message_id, NETWORK_ERROR); | 189 delegate_->OnMessageSendError(app_id, message_id, NETWORK_ERROR); |
| 172 } | 190 } |
| 173 | 191 |
| 174 void GCMClientMock::SetReadyOnIO() { | |
| 175 delegate_->OnGCMReady(); | |
| 176 } | |
| 177 | |
| 178 } // namespace gcm | 192 } // namespace gcm |
| OLD | NEW |