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/chrome_async_socket.h" | 5 #include "jingle/notifier/base/chrome_async_socket.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstring> | 8 #include <cstring> |
9 #include <cstdlib> | 9 #include <cstdlib> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
csilv
2011/12/09 00:42:00
nit: include base/bind_helpers.h
| |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
16 #include "jingle/notifier/base/resolving_client_socket_factory.h" | 16 #include "jingle/notifier/base/resolving_client_socket_factory.h" |
17 #include "net/base/address_list.h" | 17 #include "net/base/address_list.h" |
18 #include "net/base/host_port_pair.h" | 18 #include "net/base/host_port_pair.h" |
19 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
20 #include "net/base/net_util.h" | 20 #include "net/base/net_util.h" |
21 #include "net/base/ssl_config_service.h" | 21 #include "net/base/ssl_config_service.h" |
22 #include "net/base/sys_addrinfo.h" | 22 #include "net/base/sys_addrinfo.h" |
23 #include "net/socket/client_socket_factory.h" | 23 #include "net/socket/client_socket_factory.h" |
24 #include "net/socket/client_socket_handle.h" | 24 #include "net/socket/client_socket_handle.h" |
25 #include "net/socket/ssl_client_socket.h" | 25 #include "net/socket/ssl_client_socket.h" |
26 #include "net/socket/tcp_client_socket.h" | 26 #include "net/socket/tcp_client_socket.h" |
27 #include "talk/base/socketaddress.h" | 27 #include "talk/base/socketaddress.h" |
28 | 28 |
29 namespace notifier { | 29 namespace notifier { |
30 | 30 |
31 ChromeAsyncSocket::ChromeAsyncSocket( | 31 ChromeAsyncSocket::ChromeAsyncSocket( |
32 ResolvingClientSocketFactory* client_socket_factory, | 32 ResolvingClientSocketFactory* client_socket_factory, |
33 size_t read_buf_size, | 33 size_t read_buf_size, |
34 size_t write_buf_size) | 34 size_t write_buf_size) |
35 : connect_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), | 35 : client_socket_factory_(client_socket_factory), |
36 &ChromeAsyncSocket::ProcessConnectDone), | |
37 read_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), | |
38 &ChromeAsyncSocket::ProcessReadDone), | |
39 write_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), | |
40 &ChromeAsyncSocket::ProcessWriteDone), | |
41 ssl_connect_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), | |
42 &ChromeAsyncSocket::ProcessSSLConnectDone), | |
43 client_socket_factory_(client_socket_factory), | |
44 state_(STATE_CLOSED), | 36 state_(STATE_CLOSED), |
45 error_(ERROR_NONE), | 37 error_(ERROR_NONE), |
46 net_error_(net::OK), | 38 net_error_(net::OK), |
47 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 39 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
48 read_state_(IDLE), | 40 read_state_(IDLE), |
49 read_buf_(new net::IOBufferWithSize(read_buf_size)), | 41 read_buf_(new net::IOBufferWithSize(read_buf_size)), |
50 read_start_(0U), | 42 read_start_(0U), |
51 read_end_(0U), | 43 read_end_(0U), |
52 write_state_(IDLE), | 44 write_state_(IDLE), |
53 write_buf_(new net::IOBufferWithSize(write_buf_size)), | 45 write_buf_(new net::IOBufferWithSize(write_buf_size)), |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 | 104 |
113 state_ = STATE_CONNECTING; | 105 state_ = STATE_CONNECTING; |
114 | 106 |
115 DCHECK_EQ(false, weak_factory_.HasWeakPtrs()); | 107 DCHECK_EQ(false, weak_factory_.HasWeakPtrs()); |
116 | 108 |
117 net::HostPortPair dest_host_port_pair(address.IPAsString(), address.port()); | 109 net::HostPortPair dest_host_port_pair(address.IPAsString(), address.port()); |
118 | 110 |
119 transport_socket_.reset( | 111 transport_socket_.reset( |
120 client_socket_factory_->CreateTransportClientSocket( | 112 client_socket_factory_->CreateTransportClientSocket( |
121 dest_host_port_pair)); | 113 dest_host_port_pair)); |
122 int status = transport_socket_->Connect(&connect_callback_); | 114 int status = transport_socket_->Connect( |
115 base::Bind(&ChromeAsyncSocket::ProcessConnectDone, | |
116 base::Unretained(this))); | |
123 if (status != net::ERR_IO_PENDING) { | 117 if (status != net::ERR_IO_PENDING) { |
124 // We defer execution of ProcessConnectDone instead of calling it | 118 // We defer execution of ProcessConnectDone instead of calling it |
125 // directly here as the caller may not expect an error/close to | 119 // directly here as the caller may not expect an error/close to |
126 // happen here. This is okay, as from the caller's point of view, | 120 // happen here. This is okay, as from the caller's point of view, |
127 // the connect always happens asynchronously. | 121 // the connect always happens asynchronously. |
128 MessageLoop* message_loop = MessageLoop::current(); | 122 MessageLoop* message_loop = MessageLoop::current(); |
129 CHECK(message_loop); | 123 CHECK(message_loop); |
130 message_loop->PostTask(FROM_HERE, | 124 message_loop->PostTask(FROM_HERE, |
131 base::Bind(&ChromeAsyncSocket::ProcessConnectDone, | 125 base::Bind(&ChromeAsyncSocket::ProcessConnectDone, |
132 weak_factory_.GetWeakPtr(), status)); | 126 weak_factory_.GetWeakPtr(), status)); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
175 DCHECK(IsOpen()); | 169 DCHECK(IsOpen()); |
176 DCHECK_EQ(read_state_, POSTED); | 170 DCHECK_EQ(read_state_, POSTED); |
177 DCHECK_EQ(read_start_, 0U); | 171 DCHECK_EQ(read_start_, 0U); |
178 DCHECK_EQ(read_end_, 0U); | 172 DCHECK_EQ(read_end_, 0U); |
179 // Once we call Read(), we cannot call StartTls() until the read | 173 // Once we call Read(), we cannot call StartTls() until the read |
180 // finishes. This is okay, as StartTls() is called only from a read | 174 // finishes. This is okay, as StartTls() is called only from a read |
181 // handler (i.e., after a read finishes and before another read is | 175 // handler (i.e., after a read finishes and before another read is |
182 // done). | 176 // done). |
183 int status = | 177 int status = |
184 transport_socket_->Read( | 178 transport_socket_->Read( |
185 read_buf_.get(), read_buf_->size(), &read_callback_); | 179 read_buf_.get(), read_buf_->size(), |
180 base::Bind(&ChromeAsyncSocket::ProcessReadDone, | |
181 base::Unretained(this))); | |
186 read_state_ = PENDING; | 182 read_state_ = PENDING; |
187 if (status != net::ERR_IO_PENDING) { | 183 if (status != net::ERR_IO_PENDING) { |
188 ProcessReadDone(status); | 184 ProcessReadDone(status); |
189 } | 185 } |
190 } | 186 } |
191 | 187 |
192 // read_state_ == PENDING -> read_state_ == IDLE | 188 // read_state_ == PENDING -> read_state_ == IDLE |
193 | 189 |
194 void ChromeAsyncSocket::ProcessReadDone(int status) { | 190 void ChromeAsyncSocket::ProcessReadDone(int status) { |
195 DCHECK_NE(status, net::ERR_IO_PENDING); | 191 DCHECK_NE(status, net::ERR_IO_PENDING); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
305 void ChromeAsyncSocket::DoWrite() { | 301 void ChromeAsyncSocket::DoWrite() { |
306 DCHECK(IsOpen()); | 302 DCHECK(IsOpen()); |
307 DCHECK_EQ(write_state_, POSTED); | 303 DCHECK_EQ(write_state_, POSTED); |
308 DCHECK_GT(write_end_, 0U); | 304 DCHECK_GT(write_end_, 0U); |
309 // Once we call Write(), we cannot call StartTls() until the write | 305 // Once we call Write(), we cannot call StartTls() until the write |
310 // finishes. This is okay, as StartTls() is called only after we | 306 // finishes. This is okay, as StartTls() is called only after we |
311 // have received a reply to a message we sent to the server and | 307 // have received a reply to a message we sent to the server and |
312 // before we send the next message. | 308 // before we send the next message. |
313 int status = | 309 int status = |
314 transport_socket_->Write( | 310 transport_socket_->Write( |
315 write_buf_.get(), write_end_, &write_callback_); | 311 write_buf_.get(), write_end_, |
312 base::Bind(&ChromeAsyncSocket::ProcessWriteDone, | |
313 base::Unretained(this))); | |
316 write_state_ = PENDING; | 314 write_state_ = PENDING; |
317 if (status != net::ERR_IO_PENDING) { | 315 if (status != net::ERR_IO_PENDING) { |
318 ProcessWriteDone(status); | 316 ProcessWriteDone(status); |
319 } | 317 } |
320 } | 318 } |
321 | 319 |
322 // write_state_ == PENDING -> write_state_ == IDLE or POSTED (the | 320 // write_state_ == PENDING -> write_state_ == IDLE or POSTED (the |
323 // latter via PostDoWrite()) | 321 // latter via PostDoWrite()) |
324 | 322 |
325 void ChromeAsyncSocket::ProcessWriteDone(int status) { | 323 void ChromeAsyncSocket::ProcessWriteDone(int status) { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
401 | 399 |
402 // Clear out any posted DoRead() tasks. | 400 // Clear out any posted DoRead() tasks. |
403 weak_factory_.InvalidateWeakPtrs(); | 401 weak_factory_.InvalidateWeakPtrs(); |
404 | 402 |
405 DCHECK(transport_socket_.get()); | 403 DCHECK(transport_socket_.get()); |
406 net::ClientSocketHandle* socket_handle = new net::ClientSocketHandle(); | 404 net::ClientSocketHandle* socket_handle = new net::ClientSocketHandle(); |
407 socket_handle->set_socket(transport_socket_.release()); | 405 socket_handle->set_socket(transport_socket_.release()); |
408 transport_socket_.reset( | 406 transport_socket_.reset( |
409 client_socket_factory_->CreateSSLClientSocket( | 407 client_socket_factory_->CreateSSLClientSocket( |
410 socket_handle, net::HostPortPair(domain_name, 443))); | 408 socket_handle, net::HostPortPair(domain_name, 443))); |
411 int status = transport_socket_->Connect(&ssl_connect_callback_); | 409 int status = transport_socket_->Connect( |
410 base::Bind(&ChromeAsyncSocket::ProcessSSLConnectDone, | |
411 base::Unretained(this))); | |
412 if (status != net::ERR_IO_PENDING) { | 412 if (status != net::ERR_IO_PENDING) { |
413 MessageLoop* message_loop = MessageLoop::current(); | 413 MessageLoop* message_loop = MessageLoop::current(); |
414 CHECK(message_loop); | 414 CHECK(message_loop); |
415 message_loop->PostTask(FROM_HERE, | 415 message_loop->PostTask(FROM_HERE, |
416 base::Bind(&ChromeAsyncSocket::ProcessSSLConnectDone, | 416 base::Bind(&ChromeAsyncSocket::ProcessSSLConnectDone, |
417 weak_factory_.GetWeakPtr(), status)); | 417 weak_factory_.GetWeakPtr(), status)); |
418 } | 418 } |
419 return true; | 419 return true; |
420 } | 420 } |
421 | 421 |
(...skipping 16 matching lines...) Expand all Loading... | |
438 } | 438 } |
439 state_ = STATE_TLS_OPEN; | 439 state_ = STATE_TLS_OPEN; |
440 PostDoRead(); | 440 PostDoRead(); |
441 if (write_end_ > 0U) { | 441 if (write_end_ > 0U) { |
442 PostDoWrite(); | 442 PostDoWrite(); |
443 } | 443 } |
444 SignalSSLConnected(); | 444 SignalSSLConnected(); |
445 } | 445 } |
446 | 446 |
447 } // namespace notifier | 447 } // namespace notifier |
OLD | NEW |