OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "jingle/notifier/base/fake_ssl_client_socket.h" | 5 #include "jingle/notifier/base/fake_ssl_client_socket.h" |
6 | 6 |
7 #include <cstdlib> | 7 #include <cstdlib> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 &FakeSSLClientSocket::OnConnectDone), | 82 &FakeSSLClientSocket::OnConnectDone), |
83 send_client_hello_callback_( | 83 send_client_hello_callback_( |
84 ALLOW_THIS_IN_INITIALIZER_LIST(this), | 84 ALLOW_THIS_IN_INITIALIZER_LIST(this), |
85 &FakeSSLClientSocket::OnSendClientHelloDone), | 85 &FakeSSLClientSocket::OnSendClientHelloDone), |
86 verify_server_hello_callback_( | 86 verify_server_hello_callback_( |
87 ALLOW_THIS_IN_INITIALIZER_LIST(this), | 87 ALLOW_THIS_IN_INITIALIZER_LIST(this), |
88 &FakeSSLClientSocket::OnVerifyServerHelloDone), | 88 &FakeSSLClientSocket::OnVerifyServerHelloDone), |
89 transport_socket_(transport_socket), | 89 transport_socket_(transport_socket), |
90 next_handshake_state_(STATE_NONE), | 90 next_handshake_state_(STATE_NONE), |
91 handshake_completed_(false), | 91 handshake_completed_(false), |
92 user_connect_callback_(NULL), | 92 old_user_connect_callback_(NULL), |
93 write_buf_(NewDrainableIOBufferWithSize(arraysize(kSslClientHello))), | 93 write_buf_(NewDrainableIOBufferWithSize(arraysize(kSslClientHello))), |
94 read_buf_(NewDrainableIOBufferWithSize(arraysize(kSslServerHello))) { | 94 read_buf_(NewDrainableIOBufferWithSize(arraysize(kSslServerHello))) { |
95 CHECK(transport_socket_.get()); | 95 CHECK(transport_socket_.get()); |
96 std::memcpy(write_buf_->data(), kSslClientHello, arraysize(kSslClientHello)); | 96 std::memcpy(write_buf_->data(), kSslClientHello, arraysize(kSslClientHello)); |
97 } | 97 } |
98 | 98 |
99 FakeSSLClientSocket::~FakeSSLClientSocket() {} | 99 FakeSSLClientSocket::~FakeSSLClientSocket() {} |
100 | 100 |
101 int FakeSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, | 101 int FakeSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, |
102 net::OldCompletionCallback* callback) { | 102 net::OldCompletionCallback* callback) { |
(...skipping 16 matching lines...) Expand all Loading... |
119 bool FakeSSLClientSocket::SetSendBufferSize(int32 size) { | 119 bool FakeSSLClientSocket::SetSendBufferSize(int32 size) { |
120 return transport_socket_->SetSendBufferSize(size); | 120 return transport_socket_->SetSendBufferSize(size); |
121 } | 121 } |
122 | 122 |
123 int FakeSSLClientSocket::Connect(net::OldCompletionCallback* callback) { | 123 int FakeSSLClientSocket::Connect(net::OldCompletionCallback* callback) { |
124 // We don't support synchronous operation, even if | 124 // We don't support synchronous operation, even if |
125 // |transport_socket_| does. | 125 // |transport_socket_| does. |
126 DCHECK(callback); | 126 DCHECK(callback); |
127 DCHECK_EQ(next_handshake_state_, STATE_NONE); | 127 DCHECK_EQ(next_handshake_state_, STATE_NONE); |
128 DCHECK(!handshake_completed_); | 128 DCHECK(!handshake_completed_); |
129 DCHECK(!user_connect_callback_); | 129 DCHECK(!old_user_connect_callback_); |
130 DCHECK_EQ(write_buf_->BytesConsumed(), 0); | 130 DCHECK_EQ(write_buf_->BytesConsumed(), 0); |
131 DCHECK_EQ(read_buf_->BytesConsumed(), 0); | 131 DCHECK_EQ(read_buf_->BytesConsumed(), 0); |
132 | 132 |
133 next_handshake_state_ = STATE_CONNECT; | 133 next_handshake_state_ = STATE_CONNECT; |
134 int status = DoHandshakeLoop(); | 134 int status = DoHandshakeLoop(); |
135 if (status == net::ERR_IO_PENDING) { | 135 if (status == net::ERR_IO_PENDING) { |
| 136 old_user_connect_callback_ = callback; |
| 137 } |
| 138 return status; |
| 139 } |
| 140 int FakeSSLClientSocket::Connect(const net::CompletionCallback& callback) { |
| 141 // We don't support synchronous operation, even if |
| 142 // |transport_socket_| does. |
| 143 DCHECK(!callback.is_null()); |
| 144 DCHECK_EQ(next_handshake_state_, STATE_NONE); |
| 145 DCHECK(!handshake_completed_); |
| 146 DCHECK(user_connect_callback_.is_null()); |
| 147 DCHECK_EQ(write_buf_->BytesConsumed(), 0); |
| 148 DCHECK_EQ(read_buf_->BytesConsumed(), 0); |
| 149 |
| 150 next_handshake_state_ = STATE_CONNECT; |
| 151 int status = DoHandshakeLoop(); |
| 152 if (status == net::ERR_IO_PENDING) |
136 user_connect_callback_ = callback; | 153 user_connect_callback_ = callback; |
137 } | 154 |
138 return status; | 155 return status; |
139 } | 156 } |
140 | 157 |
141 int FakeSSLClientSocket::DoHandshakeLoop() { | 158 int FakeSSLClientSocket::DoHandshakeLoop() { |
142 DCHECK_NE(next_handshake_state_, STATE_NONE); | 159 DCHECK_NE(next_handshake_state_, STATE_NONE); |
143 int status = net::OK; | 160 int status = net::OK; |
144 do { | 161 do { |
145 HandshakeState state = next_handshake_state_; | 162 HandshakeState state = next_handshake_state_; |
146 next_handshake_state_ = STATE_NONE; | 163 next_handshake_state_ = STATE_NONE; |
147 switch (state) { | 164 switch (state) { |
(...skipping 12 matching lines...) Expand all Loading... |
160 break; | 177 break; |
161 } | 178 } |
162 } while ((status != net::ERR_IO_PENDING) && | 179 } while ((status != net::ERR_IO_PENDING) && |
163 (next_handshake_state_ != STATE_NONE)); | 180 (next_handshake_state_ != STATE_NONE)); |
164 return status; | 181 return status; |
165 } | 182 } |
166 | 183 |
167 void FakeSSLClientSocket::RunUserConnectCallback(int status) { | 184 void FakeSSLClientSocket::RunUserConnectCallback(int status) { |
168 DCHECK_LE(status, net::OK); | 185 DCHECK_LE(status, net::OK); |
169 next_handshake_state_ = STATE_NONE; | 186 next_handshake_state_ = STATE_NONE; |
170 net::OldCompletionCallback* user_connect_callback = user_connect_callback_; | 187 if (old_user_connect_callback_) { |
171 user_connect_callback_ = NULL; | 188 net::OldCompletionCallback* user_connect_callback = |
172 user_connect_callback->Run(status); | 189 old_user_connect_callback_; |
| 190 old_user_connect_callback_ = NULL; |
| 191 user_connect_callback->Run(status); |
| 192 } else { |
| 193 net::CompletionCallback user_connect_callback = user_connect_callback_; |
| 194 user_connect_callback_.Reset(); |
| 195 user_connect_callback.Run(status); |
| 196 } |
173 } | 197 } |
174 | 198 |
175 void FakeSSLClientSocket::DoHandshakeLoopWithUserConnectCallback() { | 199 void FakeSSLClientSocket::DoHandshakeLoopWithUserConnectCallback() { |
176 int status = DoHandshakeLoop(); | 200 int status = DoHandshakeLoop(); |
177 if (status != net::ERR_IO_PENDING) { | 201 if (status != net::ERR_IO_PENDING) { |
178 RunUserConnectCallback(status); | 202 RunUserConnectCallback(status); |
179 } | 203 } |
180 } | 204 } |
181 | 205 |
182 int FakeSSLClientSocket::DoConnect() { | 206 int FakeSSLClientSocket::DoConnect() { |
183 int status = transport_socket_->Connect(&connect_callback_); | 207 int status = transport_socket_->Connect(&connect_callback_); |
184 if (status != net::OK) { | 208 if (status != net::OK) { |
185 return status; | 209 return status; |
186 } | 210 } |
187 ProcessConnectDone(); | 211 ProcessConnectDone(); |
188 return net::OK; | 212 return net::OK; |
189 } | 213 } |
190 | 214 |
191 void FakeSSLClientSocket::OnConnectDone(int status) { | 215 void FakeSSLClientSocket::OnConnectDone(int status) { |
192 DCHECK_NE(status, net::ERR_IO_PENDING); | 216 DCHECK_NE(status, net::ERR_IO_PENDING); |
193 DCHECK_LE(status, net::OK); | 217 DCHECK_LE(status, net::OK); |
194 DCHECK(user_connect_callback_); | 218 DCHECK(old_user_connect_callback_ || !user_connect_callback_.is_null()); |
195 if (status != net::OK) { | 219 if (status != net::OK) { |
196 RunUserConnectCallback(status); | 220 RunUserConnectCallback(status); |
197 return; | 221 return; |
198 } | 222 } |
199 ProcessConnectDone(); | 223 ProcessConnectDone(); |
200 DoHandshakeLoopWithUserConnectCallback(); | 224 DoHandshakeLoopWithUserConnectCallback(); |
201 } | 225 } |
202 | 226 |
203 void FakeSSLClientSocket::ProcessConnectDone() { | 227 void FakeSSLClientSocket::ProcessConnectDone() { |
204 DCHECK_EQ(write_buf_->BytesConsumed(), 0); | 228 DCHECK_EQ(write_buf_->BytesConsumed(), 0); |
205 DCHECK_EQ(read_buf_->BytesConsumed(), 0); | 229 DCHECK_EQ(read_buf_->BytesConsumed(), 0); |
206 next_handshake_state_ = STATE_SEND_CLIENT_HELLO; | 230 next_handshake_state_ = STATE_SEND_CLIENT_HELLO; |
207 } | 231 } |
208 | 232 |
209 int FakeSSLClientSocket::DoSendClientHello() { | 233 int FakeSSLClientSocket::DoSendClientHello() { |
210 int status = transport_socket_->Write( | 234 int status = transport_socket_->Write( |
211 write_buf_, write_buf_->BytesRemaining(), | 235 write_buf_, write_buf_->BytesRemaining(), |
212 &send_client_hello_callback_); | 236 &send_client_hello_callback_); |
213 if (status < net::OK) { | 237 if (status < net::OK) { |
214 return status; | 238 return status; |
215 } | 239 } |
216 ProcessSendClientHelloDone(static_cast<size_t>(status)); | 240 ProcessSendClientHelloDone(static_cast<size_t>(status)); |
217 return net::OK; | 241 return net::OK; |
218 } | 242 } |
219 | 243 |
220 void FakeSSLClientSocket::OnSendClientHelloDone(int status) { | 244 void FakeSSLClientSocket::OnSendClientHelloDone(int status) { |
221 DCHECK_NE(status, net::ERR_IO_PENDING); | 245 DCHECK_NE(status, net::ERR_IO_PENDING); |
222 DCHECK(user_connect_callback_); | 246 DCHECK(old_user_connect_callback_ || !user_connect_callback_.is_null()); |
223 if (status < net::OK) { | 247 if (status < net::OK) { |
224 RunUserConnectCallback(status); | 248 RunUserConnectCallback(status); |
225 return; | 249 return; |
226 } | 250 } |
227 ProcessSendClientHelloDone(static_cast<size_t>(status)); | 251 ProcessSendClientHelloDone(static_cast<size_t>(status)); |
228 DoHandshakeLoopWithUserConnectCallback(); | 252 DoHandshakeLoopWithUserConnectCallback(); |
229 } | 253 } |
230 | 254 |
231 void FakeSSLClientSocket::ProcessSendClientHelloDone(size_t written) { | 255 void FakeSSLClientSocket::ProcessSendClientHelloDone(size_t written) { |
232 DCHECK_LE(written, static_cast<size_t>(write_buf_->BytesRemaining())); | 256 DCHECK_LE(written, static_cast<size_t>(write_buf_->BytesRemaining())); |
(...skipping 12 matching lines...) Expand all Loading... |
245 &verify_server_hello_callback_); | 269 &verify_server_hello_callback_); |
246 if (status < net::OK) { | 270 if (status < net::OK) { |
247 return status; | 271 return status; |
248 } | 272 } |
249 size_t read = static_cast<size_t>(status); | 273 size_t read = static_cast<size_t>(status); |
250 return ProcessVerifyServerHelloDone(read); | 274 return ProcessVerifyServerHelloDone(read); |
251 } | 275 } |
252 | 276 |
253 void FakeSSLClientSocket::OnVerifyServerHelloDone(int status) { | 277 void FakeSSLClientSocket::OnVerifyServerHelloDone(int status) { |
254 DCHECK_NE(status, net::ERR_IO_PENDING); | 278 DCHECK_NE(status, net::ERR_IO_PENDING); |
255 DCHECK(user_connect_callback_); | 279 DCHECK(old_user_connect_callback_ || !user_connect_callback_.is_null()); |
256 if (status < net::OK) { | 280 if (status < net::OK) { |
257 RunUserConnectCallback(status); | 281 RunUserConnectCallback(status); |
258 return; | 282 return; |
259 } | 283 } |
260 size_t read = static_cast<size_t>(status); | 284 size_t read = static_cast<size_t>(status); |
261 status = ProcessVerifyServerHelloDone(read); | 285 status = ProcessVerifyServerHelloDone(read); |
262 if (status < net::OK) { | 286 if (status < net::OK) { |
263 RunUserConnectCallback(status); | 287 RunUserConnectCallback(status); |
264 return; | 288 return; |
265 } | 289 } |
(...skipping 22 matching lines...) Expand all Loading... |
288 next_handshake_state_ = STATE_NONE; | 312 next_handshake_state_ = STATE_NONE; |
289 handshake_completed_ = true; | 313 handshake_completed_ = true; |
290 } | 314 } |
291 return net::OK; | 315 return net::OK; |
292 } | 316 } |
293 | 317 |
294 void FakeSSLClientSocket::Disconnect() { | 318 void FakeSSLClientSocket::Disconnect() { |
295 transport_socket_->Disconnect(); | 319 transport_socket_->Disconnect(); |
296 next_handshake_state_ = STATE_NONE; | 320 next_handshake_state_ = STATE_NONE; |
297 handshake_completed_ = false; | 321 handshake_completed_ = false; |
298 user_connect_callback_ = NULL; | 322 old_user_connect_callback_ = NULL; |
| 323 user_connect_callback_.Reset(); |
299 write_buf_->SetOffset(0); | 324 write_buf_->SetOffset(0); |
300 read_buf_->SetOffset(0); | 325 read_buf_->SetOffset(0); |
301 } | 326 } |
302 | 327 |
303 bool FakeSSLClientSocket::IsConnected() const { | 328 bool FakeSSLClientSocket::IsConnected() const { |
304 return handshake_completed_ && transport_socket_->IsConnected(); | 329 return handshake_completed_ && transport_socket_->IsConnected(); |
305 } | 330 } |
306 | 331 |
307 bool FakeSSLClientSocket::IsConnectedAndIdle() const { | 332 bool FakeSSLClientSocket::IsConnectedAndIdle() const { |
308 return handshake_completed_ && transport_socket_->IsConnectedAndIdle(); | 333 return handshake_completed_ && transport_socket_->IsConnectedAndIdle(); |
(...skipping 29 matching lines...) Expand all Loading... |
338 | 363 |
339 int64 FakeSSLClientSocket::NumBytesRead() const { | 364 int64 FakeSSLClientSocket::NumBytesRead() const { |
340 return transport_socket_->NumBytesRead(); | 365 return transport_socket_->NumBytesRead(); |
341 } | 366 } |
342 | 367 |
343 base::TimeDelta FakeSSLClientSocket::GetConnectTimeMicros() const { | 368 base::TimeDelta FakeSSLClientSocket::GetConnectTimeMicros() const { |
344 return transport_socket_->GetConnectTimeMicros(); | 369 return transport_socket_->GetConnectTimeMicros(); |
345 } | 370 } |
346 | 371 |
347 } // namespace notifier | 372 } // namespace notifier |
OLD | NEW |