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