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