Chromium Code Reviews| 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 |
|
csilv
2011/12/09 00:42:00
nit: include base/bind.h, base/bind_helpers.h
| |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
| 13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 14 | 14 |
| 15 namespace notifier { | 15 namespace notifier { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 arraysize(kSslClientHello)); | 71 arraysize(kSslClientHello)); |
| 72 } | 72 } |
| 73 | 73 |
| 74 base::StringPiece FakeSSLClientSocket::GetSslServerHello() { | 74 base::StringPiece FakeSSLClientSocket::GetSslServerHello() { |
| 75 return base::StringPiece(reinterpret_cast<const char*>(kSslServerHello), | 75 return base::StringPiece(reinterpret_cast<const char*>(kSslServerHello), |
| 76 arraysize(kSslServerHello)); | 76 arraysize(kSslServerHello)); |
| 77 } | 77 } |
| 78 | 78 |
| 79 FakeSSLClientSocket::FakeSSLClientSocket( | 79 FakeSSLClientSocket::FakeSSLClientSocket( |
| 80 net::StreamSocket* transport_socket) | 80 net::StreamSocket* transport_socket) |
| 81 : connect_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), | 81 : transport_socket_(transport_socket), |
| 82 &FakeSSLClientSocket::OnConnectDone), | |
| 83 send_client_hello_callback_( | |
| 84 ALLOW_THIS_IN_INITIALIZER_LIST(this), | |
| 85 &FakeSSLClientSocket::OnSendClientHelloDone), | |
| 86 verify_server_hello_callback_( | |
| 87 ALLOW_THIS_IN_INITIALIZER_LIST(this), | |
| 88 &FakeSSLClientSocket::OnVerifyServerHelloDone), | |
| 89 transport_socket_(transport_socket), | |
| 90 next_handshake_state_(STATE_NONE), | 82 next_handshake_state_(STATE_NONE), |
| 91 handshake_completed_(false), | 83 handshake_completed_(false), |
| 92 old_user_connect_callback_(NULL), | |
| 93 write_buf_(NewDrainableIOBufferWithSize(arraysize(kSslClientHello))), | 84 write_buf_(NewDrainableIOBufferWithSize(arraysize(kSslClientHello))), |
| 94 read_buf_(NewDrainableIOBufferWithSize(arraysize(kSslServerHello))) { | 85 read_buf_(NewDrainableIOBufferWithSize(arraysize(kSslServerHello))) { |
| 95 CHECK(transport_socket_.get()); | 86 CHECK(transport_socket_.get()); |
| 96 std::memcpy(write_buf_->data(), kSslClientHello, arraysize(kSslClientHello)); | 87 std::memcpy(write_buf_->data(), kSslClientHello, arraysize(kSslClientHello)); |
| 97 } | 88 } |
| 98 | 89 |
| 99 FakeSSLClientSocket::~FakeSSLClientSocket() {} | 90 FakeSSLClientSocket::~FakeSSLClientSocket() {} |
| 100 | 91 |
| 101 int FakeSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, | 92 int FakeSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, |
| 102 net::OldCompletionCallback* callback) { | |
| 103 DCHECK_EQ(next_handshake_state_, STATE_NONE); | |
| 104 DCHECK(handshake_completed_); | |
| 105 return transport_socket_->Read(buf, buf_len, callback); | |
| 106 } | |
| 107 int FakeSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, | |
| 108 const net::CompletionCallback& callback) { | 93 const net::CompletionCallback& callback) { |
| 109 DCHECK_EQ(next_handshake_state_, STATE_NONE); | 94 DCHECK_EQ(next_handshake_state_, STATE_NONE); |
| 110 DCHECK(handshake_completed_); | 95 DCHECK(handshake_completed_); |
| 111 return transport_socket_->Read(buf, buf_len, callback); | 96 return transport_socket_->Read(buf, buf_len, callback); |
| 112 } | 97 } |
| 113 | 98 |
| 114 int FakeSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, | 99 int FakeSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, |
| 115 net::OldCompletionCallback* callback) { | 100 const net::CompletionCallback& callback) { |
| 116 DCHECK_EQ(next_handshake_state_, STATE_NONE); | 101 DCHECK_EQ(next_handshake_state_, STATE_NONE); |
| 117 DCHECK(handshake_completed_); | 102 DCHECK(handshake_completed_); |
| 118 return transport_socket_->Write(buf, buf_len, callback); | 103 return transport_socket_->Write(buf, buf_len, callback); |
| 119 } | 104 } |
| 120 | 105 |
| 121 bool FakeSSLClientSocket::SetReceiveBufferSize(int32 size) { | 106 bool FakeSSLClientSocket::SetReceiveBufferSize(int32 size) { |
| 122 return transport_socket_->SetReceiveBufferSize(size); | 107 return transport_socket_->SetReceiveBufferSize(size); |
| 123 } | 108 } |
| 124 | 109 |
| 125 bool FakeSSLClientSocket::SetSendBufferSize(int32 size) { | 110 bool FakeSSLClientSocket::SetSendBufferSize(int32 size) { |
| 126 return transport_socket_->SetSendBufferSize(size); | 111 return transport_socket_->SetSendBufferSize(size); |
| 127 } | 112 } |
| 128 | 113 |
| 129 int FakeSSLClientSocket::Connect(net::OldCompletionCallback* callback) { | |
| 130 // We don't support synchronous operation, even if | |
| 131 // |transport_socket_| does. | |
| 132 DCHECK(callback); | |
| 133 DCHECK_EQ(next_handshake_state_, STATE_NONE); | |
| 134 DCHECK(!handshake_completed_); | |
| 135 DCHECK(!old_user_connect_callback_); | |
| 136 DCHECK_EQ(write_buf_->BytesConsumed(), 0); | |
| 137 DCHECK_EQ(read_buf_->BytesConsumed(), 0); | |
| 138 | |
| 139 next_handshake_state_ = STATE_CONNECT; | |
| 140 int status = DoHandshakeLoop(); | |
| 141 if (status == net::ERR_IO_PENDING) { | |
| 142 old_user_connect_callback_ = callback; | |
| 143 } | |
| 144 return status; | |
| 145 } | |
| 146 int FakeSSLClientSocket::Connect(const net::CompletionCallback& callback) { | 114 int FakeSSLClientSocket::Connect(const net::CompletionCallback& callback) { |
| 147 // We don't support synchronous operation, even if | 115 // We don't support synchronous operation, even if |
| 148 // |transport_socket_| does. | 116 // |transport_socket_| does. |
| 149 DCHECK(!callback.is_null()); | 117 DCHECK(!callback.is_null()); |
| 150 DCHECK_EQ(next_handshake_state_, STATE_NONE); | 118 DCHECK_EQ(next_handshake_state_, STATE_NONE); |
| 151 DCHECK(!handshake_completed_); | 119 DCHECK(!handshake_completed_); |
| 152 DCHECK(user_connect_callback_.is_null()); | 120 DCHECK(user_connect_callback_.is_null()); |
| 153 DCHECK_EQ(write_buf_->BytesConsumed(), 0); | 121 DCHECK_EQ(write_buf_->BytesConsumed(), 0); |
| 154 DCHECK_EQ(read_buf_->BytesConsumed(), 0); | 122 DCHECK_EQ(read_buf_->BytesConsumed(), 0); |
| 155 | 123 |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 183 break; | 151 break; |
| 184 } | 152 } |
| 185 } while ((status != net::ERR_IO_PENDING) && | 153 } while ((status != net::ERR_IO_PENDING) && |
| 186 (next_handshake_state_ != STATE_NONE)); | 154 (next_handshake_state_ != STATE_NONE)); |
| 187 return status; | 155 return status; |
| 188 } | 156 } |
| 189 | 157 |
| 190 void FakeSSLClientSocket::RunUserConnectCallback(int status) { | 158 void FakeSSLClientSocket::RunUserConnectCallback(int status) { |
| 191 DCHECK_LE(status, net::OK); | 159 DCHECK_LE(status, net::OK); |
| 192 next_handshake_state_ = STATE_NONE; | 160 next_handshake_state_ = STATE_NONE; |
| 193 if (old_user_connect_callback_) { | 161 net::CompletionCallback user_connect_callback = user_connect_callback_; |
| 194 net::OldCompletionCallback* user_connect_callback = | 162 user_connect_callback_.Reset(); |
| 195 old_user_connect_callback_; | 163 user_connect_callback.Run(status); |
| 196 old_user_connect_callback_ = NULL; | |
| 197 user_connect_callback->Run(status); | |
| 198 } else { | |
| 199 net::CompletionCallback user_connect_callback = user_connect_callback_; | |
| 200 user_connect_callback_.Reset(); | |
| 201 user_connect_callback.Run(status); | |
| 202 } | |
| 203 } | 164 } |
| 204 | 165 |
| 205 void FakeSSLClientSocket::DoHandshakeLoopWithUserConnectCallback() { | 166 void FakeSSLClientSocket::DoHandshakeLoopWithUserConnectCallback() { |
| 206 int status = DoHandshakeLoop(); | 167 int status = DoHandshakeLoop(); |
| 207 if (status != net::ERR_IO_PENDING) { | 168 if (status != net::ERR_IO_PENDING) { |
| 208 RunUserConnectCallback(status); | 169 RunUserConnectCallback(status); |
| 209 } | 170 } |
| 210 } | 171 } |
| 211 | 172 |
| 212 int FakeSSLClientSocket::DoConnect() { | 173 int FakeSSLClientSocket::DoConnect() { |
| 213 int status = transport_socket_->Connect(&connect_callback_); | 174 int status = transport_socket_->Connect( |
| 175 base::Bind(&FakeSSLClientSocket::OnConnectDone, base::Unretained(this))); | |
| 214 if (status != net::OK) { | 176 if (status != net::OK) { |
| 215 return status; | 177 return status; |
| 216 } | 178 } |
| 217 ProcessConnectDone(); | 179 ProcessConnectDone(); |
| 218 return net::OK; | 180 return net::OK; |
| 219 } | 181 } |
| 220 | 182 |
| 221 void FakeSSLClientSocket::OnConnectDone(int status) { | 183 void FakeSSLClientSocket::OnConnectDone(int status) { |
| 222 DCHECK_NE(status, net::ERR_IO_PENDING); | 184 DCHECK_NE(status, net::ERR_IO_PENDING); |
| 223 DCHECK_LE(status, net::OK); | 185 DCHECK_LE(status, net::OK); |
| 224 DCHECK(old_user_connect_callback_ || !user_connect_callback_.is_null()); | 186 DCHECK(!user_connect_callback_.is_null()); |
| 225 if (status != net::OK) { | 187 if (status != net::OK) { |
| 226 RunUserConnectCallback(status); | 188 RunUserConnectCallback(status); |
| 227 return; | 189 return; |
| 228 } | 190 } |
| 229 ProcessConnectDone(); | 191 ProcessConnectDone(); |
| 230 DoHandshakeLoopWithUserConnectCallback(); | 192 DoHandshakeLoopWithUserConnectCallback(); |
| 231 } | 193 } |
| 232 | 194 |
| 233 void FakeSSLClientSocket::ProcessConnectDone() { | 195 void FakeSSLClientSocket::ProcessConnectDone() { |
| 234 DCHECK_EQ(write_buf_->BytesConsumed(), 0); | 196 DCHECK_EQ(write_buf_->BytesConsumed(), 0); |
| 235 DCHECK_EQ(read_buf_->BytesConsumed(), 0); | 197 DCHECK_EQ(read_buf_->BytesConsumed(), 0); |
| 236 next_handshake_state_ = STATE_SEND_CLIENT_HELLO; | 198 next_handshake_state_ = STATE_SEND_CLIENT_HELLO; |
| 237 } | 199 } |
| 238 | 200 |
| 239 int FakeSSLClientSocket::DoSendClientHello() { | 201 int FakeSSLClientSocket::DoSendClientHello() { |
| 240 int status = transport_socket_->Write( | 202 int status = transport_socket_->Write( |
| 241 write_buf_, write_buf_->BytesRemaining(), | 203 write_buf_, write_buf_->BytesRemaining(), |
| 242 &send_client_hello_callback_); | 204 base::Bind(&FakeSSLClientSocket::OnSendClientHelloDone, |
| 205 base::Unretained(this))); | |
| 243 if (status < net::OK) { | 206 if (status < net::OK) { |
| 244 return status; | 207 return status; |
| 245 } | 208 } |
| 246 ProcessSendClientHelloDone(static_cast<size_t>(status)); | 209 ProcessSendClientHelloDone(static_cast<size_t>(status)); |
| 247 return net::OK; | 210 return net::OK; |
| 248 } | 211 } |
| 249 | 212 |
| 250 void FakeSSLClientSocket::OnSendClientHelloDone(int status) { | 213 void FakeSSLClientSocket::OnSendClientHelloDone(int status) { |
| 251 DCHECK_NE(status, net::ERR_IO_PENDING); | 214 DCHECK_NE(status, net::ERR_IO_PENDING); |
| 252 DCHECK(old_user_connect_callback_ || !user_connect_callback_.is_null()); | 215 DCHECK(!user_connect_callback_.is_null()); |
| 253 if (status < net::OK) { | 216 if (status < net::OK) { |
| 254 RunUserConnectCallback(status); | 217 RunUserConnectCallback(status); |
| 255 return; | 218 return; |
| 256 } | 219 } |
| 257 ProcessSendClientHelloDone(static_cast<size_t>(status)); | 220 ProcessSendClientHelloDone(static_cast<size_t>(status)); |
| 258 DoHandshakeLoopWithUserConnectCallback(); | 221 DoHandshakeLoopWithUserConnectCallback(); |
| 259 } | 222 } |
| 260 | 223 |
| 261 void FakeSSLClientSocket::ProcessSendClientHelloDone(size_t written) { | 224 void FakeSSLClientSocket::ProcessSendClientHelloDone(size_t written) { |
| 262 DCHECK_LE(written, static_cast<size_t>(write_buf_->BytesRemaining())); | 225 DCHECK_LE(written, static_cast<size_t>(write_buf_->BytesRemaining())); |
| 263 DCHECK_EQ(read_buf_->BytesConsumed(), 0); | 226 DCHECK_EQ(read_buf_->BytesConsumed(), 0); |
| 264 if (written < static_cast<size_t>(write_buf_->BytesRemaining())) { | 227 if (written < static_cast<size_t>(write_buf_->BytesRemaining())) { |
| 265 next_handshake_state_ = STATE_SEND_CLIENT_HELLO; | 228 next_handshake_state_ = STATE_SEND_CLIENT_HELLO; |
| 266 write_buf_->DidConsume(written); | 229 write_buf_->DidConsume(written); |
| 267 } else { | 230 } else { |
| 268 next_handshake_state_ = STATE_VERIFY_SERVER_HELLO; | 231 next_handshake_state_ = STATE_VERIFY_SERVER_HELLO; |
| 269 } | 232 } |
| 270 } | 233 } |
| 271 | 234 |
| 272 int FakeSSLClientSocket::DoVerifyServerHello() { | 235 int FakeSSLClientSocket::DoVerifyServerHello() { |
| 273 int status = transport_socket_->Read( | 236 int status = transport_socket_->Read( |
| 274 read_buf_, read_buf_->BytesRemaining(), | 237 read_buf_, read_buf_->BytesRemaining(), |
| 275 &verify_server_hello_callback_); | 238 base::Bind(&FakeSSLClientSocket::OnVerifyServerHelloDone, |
| 239 base::Unretained(this))); | |
| 276 if (status < net::OK) { | 240 if (status < net::OK) { |
| 277 return status; | 241 return status; |
| 278 } | 242 } |
| 279 size_t read = static_cast<size_t>(status); | 243 size_t read = static_cast<size_t>(status); |
| 280 return ProcessVerifyServerHelloDone(read); | 244 return ProcessVerifyServerHelloDone(read); |
| 281 } | 245 } |
| 282 | 246 |
| 283 void FakeSSLClientSocket::OnVerifyServerHelloDone(int status) { | 247 void FakeSSLClientSocket::OnVerifyServerHelloDone(int status) { |
| 284 DCHECK_NE(status, net::ERR_IO_PENDING); | 248 DCHECK_NE(status, net::ERR_IO_PENDING); |
| 285 DCHECK(old_user_connect_callback_ || !user_connect_callback_.is_null()); | 249 DCHECK(!user_connect_callback_.is_null()); |
| 286 if (status < net::OK) { | 250 if (status < net::OK) { |
| 287 RunUserConnectCallback(status); | 251 RunUserConnectCallback(status); |
| 288 return; | 252 return; |
| 289 } | 253 } |
| 290 size_t read = static_cast<size_t>(status); | 254 size_t read = static_cast<size_t>(status); |
| 291 status = ProcessVerifyServerHelloDone(read); | 255 status = ProcessVerifyServerHelloDone(read); |
| 292 if (status < net::OK) { | 256 if (status < net::OK) { |
| 293 RunUserConnectCallback(status); | 257 RunUserConnectCallback(status); |
| 294 return; | 258 return; |
| 295 } | 259 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 318 next_handshake_state_ = STATE_NONE; | 282 next_handshake_state_ = STATE_NONE; |
| 319 handshake_completed_ = true; | 283 handshake_completed_ = true; |
| 320 } | 284 } |
| 321 return net::OK; | 285 return net::OK; |
| 322 } | 286 } |
| 323 | 287 |
| 324 void FakeSSLClientSocket::Disconnect() { | 288 void FakeSSLClientSocket::Disconnect() { |
| 325 transport_socket_->Disconnect(); | 289 transport_socket_->Disconnect(); |
| 326 next_handshake_state_ = STATE_NONE; | 290 next_handshake_state_ = STATE_NONE; |
| 327 handshake_completed_ = false; | 291 handshake_completed_ = false; |
| 328 old_user_connect_callback_ = NULL; | |
| 329 user_connect_callback_.Reset(); | 292 user_connect_callback_.Reset(); |
| 330 write_buf_->SetOffset(0); | 293 write_buf_->SetOffset(0); |
| 331 read_buf_->SetOffset(0); | 294 read_buf_->SetOffset(0); |
| 332 } | 295 } |
| 333 | 296 |
| 334 bool FakeSSLClientSocket::IsConnected() const { | 297 bool FakeSSLClientSocket::IsConnected() const { |
| 335 return handshake_completed_ && transport_socket_->IsConnected(); | 298 return handshake_completed_ && transport_socket_->IsConnected(); |
| 336 } | 299 } |
| 337 | 300 |
| 338 bool FakeSSLClientSocket::IsConnectedAndIdle() const { | 301 bool FakeSSLClientSocket::IsConnectedAndIdle() const { |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 369 | 332 |
| 370 int64 FakeSSLClientSocket::NumBytesRead() const { | 333 int64 FakeSSLClientSocket::NumBytesRead() const { |
| 371 return transport_socket_->NumBytesRead(); | 334 return transport_socket_->NumBytesRead(); |
| 372 } | 335 } |
| 373 | 336 |
| 374 base::TimeDelta FakeSSLClientSocket::GetConnectTimeMicros() const { | 337 base::TimeDelta FakeSSLClientSocket::GetConnectTimeMicros() const { |
| 375 return transport_socket_->GetConnectTimeMicros(); | 338 return transport_socket_->GetConnectTimeMicros(); |
| 376 } | 339 } |
| 377 | 340 |
| 378 } // namespace notifier | 341 } // namespace notifier |
| OLD | NEW |