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