| 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 // An implementation of buzz::AsyncSocket that uses Chrome sockets. | 5 // An implementation of buzz::AsyncSocket that uses Chrome sockets. |
| 6 | 6 |
| 7 #ifndef JINGLE_NOTIFIER_BASE_CHROME_ASYNC_SOCKET_H_ | 7 #ifndef JINGLE_NOTIFIER_BASE_CHROME_ASYNC_SOCKET_H_ |
| 8 #define JINGLE_NOTIFIER_BASE_CHROME_ASYNC_SOCKET_H_ | 8 #define JINGLE_NOTIFIER_BASE_CHROME_ASYNC_SOCKET_H_ |
| 9 | 9 |
| 10 #if !defined(FEATURE_ENABLE_SSL) | 10 #if !defined(FEATURE_ENABLE_SSL) |
| 11 #error ChromeAsyncSocket expects FEATURE_ENABLE_SSL to be defined | 11 #error ChromeAsyncSocket expects FEATURE_ENABLE_SSL to be defined |
| 12 #endif | 12 #endif |
| 13 | 13 |
| 14 #include <string> | 14 #include <string> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
| 18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 19 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
| 20 #include "base/task.h" | 20 #include "base/task.h" |
| 21 #include "net/base/completion_callback.h" | 21 #include "net/base/completion_callback.h" |
| 22 #include "net/base/host_port_pair.h" |
| 22 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
| 23 #include "net/base/net_log.h" | 24 #include "net/base/net_log.h" |
| 24 #include "net/base/ssl_config_service.h" | 25 #include "net/base/ssl_config_service.h" |
| 26 #include "net/proxy/proxy_info.h" |
| 27 #include "net/proxy/proxy_service.h" |
| 25 #include "talk/xmpp/asyncsocket.h" | 28 #include "talk/xmpp/asyncsocket.h" |
| 26 | 29 |
| 27 namespace net { | 30 namespace net { |
| 28 class CertVerifier; | |
| 29 class ClientSocket; | 31 class ClientSocket; |
| 32 class ClientSocketHandle; |
| 30 class ClientSocketFactory; | 33 class ClientSocketFactory; |
| 34 class HttpNetworkSession; |
| 31 class IOBufferWithSize; | 35 class IOBufferWithSize; |
| 36 class URLRequestContextGetter; |
| 32 } // namespace net | 37 } // namespace net |
| 33 | 38 |
| 34 namespace notifier { | 39 namespace notifier { |
| 35 | 40 |
| 36 class ChromeAsyncSocket : public buzz::AsyncSocket { | 41 class ChromeAsyncSocket : public buzz::AsyncSocket { |
| 37 public: | 42 public: |
| 38 // Takes ownership of |client_socket_factory| but not |cert_verifier| nor | 43 // Does not take ownership of |net_log|. |net_log| may be NULL. |
| 39 // |net_log|. |cert_verifier| may not be NULL. |net_log| may be NULL. | 44 // The transport socket is created from the client socket pool. |
| 40 ChromeAsyncSocket(net::ClientSocketFactory* client_socket_factory, | 45 ChromeAsyncSocket(const net::SSLConfig& ssl_config, |
| 41 const net::SSLConfig& ssl_config, | 46 const scoped_refptr<net::URLRequestContextGetter>& |
| 42 net::CertVerifier* cert_verifier, | 47 request_context_getter, |
| 48 bool use_fake_ssl_client_socket, |
| 43 size_t read_buf_size, | 49 size_t read_buf_size, |
| 44 size_t write_buf_size, | 50 size_t write_buf_size, |
| 45 net::NetLog* net_log); | 51 net::NetLog* net_log); |
| 46 | 52 |
| 47 // Does not raise any signals. | 53 // Does not raise any signals. |
| 48 virtual ~ChromeAsyncSocket(); | 54 virtual ~ChromeAsyncSocket(); |
| 49 | 55 |
| 50 // buzz::AsyncSocket implementation. | 56 // buzz::AsyncSocket implementation. |
| 51 | 57 |
| 52 // The current state (see buzz::AsyncSocket::State; all but | 58 // The current state (see buzz::AsyncSocket::State; all but |
| 53 // STATE_CLOSING is used). | 59 // STATE_CLOSING is used). |
| 54 virtual State state(); | 60 virtual State state(); |
| 55 | 61 |
| 56 // The last generated error. Errors are generated when the main | 62 // The last generated error. Errors are generated when the main |
| 57 // functions below return false or when SignalClosed is raised due | 63 // functions below return false or when SignalClosed is raised due |
| 58 // to an asynchronous error. | 64 // to an asynchronous error. |
| 59 virtual Error error(); | 65 virtual Error error(); |
| 60 | 66 |
| 61 // GetError() (which is of type net::Error) != net::OK only when | 67 // GetError() (which is of type net::Error) != net::OK only when |
| 62 // error() == ERROR_WINSOCK. | 68 // error() == ERROR_WINSOCK. |
| 63 virtual int GetError(); | 69 virtual int GetError(); |
| 64 | 70 |
| 65 // Tries to connect to the given address. | 71 // Tries to connect to the given address. |
| 66 // | 72 // |
| 67 // If state() is not STATE_CLOSED, sets error to ERROR_WRONGSTATE | 73 // If state() is not STATE_CLOSED, sets error to ERROR_WRONGSTATE |
| 68 // and returns false. | 74 // and returns false. |
| 69 // | 75 // |
| 70 // If |address| is not resolved, sets error to ERROR_DNS and returns | |
| 71 // false. | |
| 72 // | 76 // |
| 73 // Otherwise, starts the connection process and returns true. | 77 // Otherwise, starts the connection process and returns true. The connection |
| 78 // process involves resolving proxy servers to use for the connection and |
| 79 // then connecting using the client socket pool. |
| 74 // SignalConnected will be raised when the connection is successful; | 80 // SignalConnected will be raised when the connection is successful; |
| 75 // otherwise, SignalClosed will be raised with a net error set. | 81 // otherwise, SignalClosed will be raised with a net error set. |
| 76 virtual bool Connect(const talk_base::SocketAddress& address); | 82 virtual bool Connect(const talk_base::SocketAddress& address); |
| 77 | 83 |
| 78 // Tries to read at most |len| bytes into |data|. | 84 // Tries to read at most |len| bytes into |data|. |
| 79 // | 85 // |
| 80 // If state() is not STATE_TLS_CONNECTING, STATE_OPEN, or | 86 // If state() is not STATE_TLS_CONNECTING, STATE_OPEN, or |
| 81 // STATE_TLS_OPEN, sets error to ERROR_WRONGSTATE and returns false. | 87 // STATE_TLS_OPEN, sets error to ERROR_WRONGSTATE and returns false. |
| 82 // | 88 // |
| 83 // Otherwise, fills in |len_read| with the number of bytes read and | 89 // Otherwise, fills in |len_read| with the number of bytes read and |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 // SignalClosed: raised whenever the socket is closed, either due to | 144 // SignalClosed: raised whenever the socket is closed, either due to |
| 139 // an asynchronous error, the other side closing the connection, or | 145 // an asynchronous error, the other side closing the connection, or |
| 140 // when Close() is called. | 146 // when Close() is called. |
| 141 // | 147 // |
| 142 // SignalRead: raised whenever the next call to Read() will succeed | 148 // SignalRead: raised whenever the next call to Read() will succeed |
| 143 // with a non-zero |len_read| (assuming nothing else happens in the | 149 // with a non-zero |len_read| (assuming nothing else happens in the |
| 144 // meantime). | 150 // meantime). |
| 145 // | 151 // |
| 146 // SignalError: not used. | 152 // SignalError: not used. |
| 147 | 153 |
| 154 #if defined(UNIT_TEST) |
| 155 // Used to override the socket used in unit-tests. Takes ownership of |
| 156 // |client_socket_factory|. |
| 157 void set_client_socket_factory( |
| 158 net::ClientSocketFactory* client_socket_factory) { |
| 159 client_socket_factory_.reset(client_socket_factory); |
| 160 } |
| 161 #endif // defined(UNIT_TEST) |
| 162 |
| 148 private: | 163 private: |
| 149 enum AsyncIOState { | 164 enum AsyncIOState { |
| 150 // An I/O op is not in progress. | 165 // An I/O op is not in progress. |
| 151 IDLE, | 166 IDLE, |
| 152 // A function has been posted to do the I/O. | 167 // A function has been posted to do the I/O. |
| 153 POSTED, | 168 POSTED, |
| 154 // An async I/O operation is pending. | 169 // An async I/O operation is pending. |
| 155 PENDING, | 170 PENDING, |
| 156 }; | 171 }; |
| 157 | 172 |
| 173 // A version of Connect that uses the passed in ClientSocketFactory to |
| 174 // create a connection rather than use the client socket pool. Used for |
| 175 // unit-tests. |
| 176 bool ConnectUsingFactory(const talk_base::SocketAddress& address); |
| 177 |
| 158 bool IsOpen() const; | 178 bool IsOpen() const; |
| 159 | 179 |
| 160 // Error functions. | 180 // Error functions. |
| 161 void DoNonNetError(Error error); | 181 void DoNonNetError(Error error); |
| 162 void DoNetError(net::Error net_error); | 182 void DoNetError(net::Error net_error); |
| 163 void DoNetErrorFromStatus(int status); | 183 void DoNetErrorFromStatus(int status); |
| 164 | 184 |
| 165 // Connection functions. | 185 // Proxy resolution and connection functions. |
| 186 void ProcessProxyResolveDone(int status); |
| 187 int ReconsiderProxyAfterError(int error); |
| 166 void ProcessConnectDone(int status); | 188 void ProcessConnectDone(int status); |
| 189 void ProcessFakeSSLHandshakeDone(int status); |
| 167 | 190 |
| 168 // Read loop functions. | 191 // Read loop functions. |
| 169 void PostDoRead(); | 192 void PostDoRead(); |
| 170 void DoRead(); | 193 void DoRead(); |
| 171 void ProcessReadDone(int status); | 194 void ProcessReadDone(int status); |
| 172 | 195 |
| 173 // Write loop functions. | 196 // Write loop functions. |
| 174 void PostDoWrite(); | 197 void PostDoWrite(); |
| 175 void DoWrite(); | 198 void DoWrite(); |
| 176 void ProcessWriteDone(int status); | 199 void ProcessWriteDone(int status); |
| 177 | 200 |
| 178 // SSL/TLS connection functions. | 201 // SSL/TLS connection functions. |
| 179 void ProcessSSLConnectDone(int status); | 202 void ProcessSSLConnectDone(int status); |
| 180 | 203 |
| 181 // Close functions. | 204 // Close functions. |
| 182 void DoClose(); | 205 void DoClose(); |
| 183 | 206 |
| 184 // Callbacks passed to |transport_socket_|. | 207 // Callbacks passed to net APIs. |
| 208 net::CompletionCallbackImpl<ChromeAsyncSocket> proxy_resolve_callback_; |
| 185 net::CompletionCallbackImpl<ChromeAsyncSocket> connect_callback_; | 209 net::CompletionCallbackImpl<ChromeAsyncSocket> connect_callback_; |
| 186 net::CompletionCallbackImpl<ChromeAsyncSocket> read_callback_; | 210 net::CompletionCallbackImpl<ChromeAsyncSocket> read_callback_; |
| 187 net::CompletionCallbackImpl<ChromeAsyncSocket> write_callback_; | 211 net::CompletionCallbackImpl<ChromeAsyncSocket> write_callback_; |
| 188 net::CompletionCallbackImpl<ChromeAsyncSocket> ssl_connect_callback_; | 212 net::CompletionCallbackImpl<ChromeAsyncSocket> ssl_connect_callback_; |
| 213 net::CompletionCallbackImpl<ChromeAsyncSocket> fake_ssl_handshake_callback_; |
| 189 | 214 |
| 215 // Used for unit-tests. |
| 190 scoped_ptr<net::ClientSocketFactory> client_socket_factory_; | 216 scoped_ptr<net::ClientSocketFactory> client_socket_factory_; |
| 191 const net::SSLConfig ssl_config_; | 217 const net::SSLConfig ssl_config_; |
| 192 net::CertVerifier* const cert_verifier_; | 218 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 219 bool use_fake_ssl_client_socket_; |
| 193 net::BoundNetLog bound_net_log_; | 220 net::BoundNetLog bound_net_log_; |
| 221 scoped_refptr<net::HttpNetworkSession> network_session_; |
| 194 | 222 |
| 195 // buzz::AsyncSocket state. | 223 // buzz::AsyncSocket state. |
| 196 buzz::AsyncSocket::State state_; | 224 buzz::AsyncSocket::State state_; |
| 197 buzz::AsyncSocket::Error error_; | 225 buzz::AsyncSocket::Error error_; |
| 198 net::Error net_error_; | 226 net::Error net_error_; |
| 199 | 227 |
| 200 // Used by read/write loops. | 228 // Used by read/write loops. |
| 201 ScopedRunnableMethodFactory<ChromeAsyncSocket> | 229 ScopedRunnableMethodFactory<ChromeAsyncSocket> |
| 202 scoped_runnable_method_factory_; | 230 scoped_runnable_method_factory_; |
| 203 | 231 |
| 204 // NULL iff state() == STATE_CLOSED. | 232 // NULL iff state() == STATE_CLOSED. |
| 205 // | 233 // |
| 206 // TODO(akalin): Use ClientSocketPool. | 234 // The transport socket. |
| 207 scoped_ptr<net::ClientSocket> transport_socket_; | 235 scoped_ptr<net::ClientSocketHandle> connection_; |
| 236 net::ProxyService::PacRequest* pac_request_; |
| 237 net::ProxyInfo proxy_info_; |
| 238 net::HostPortPair dest_host_port_pair_; |
| 239 bool tried_direct_connect_fallback_; |
| 208 | 240 |
| 209 // State for the read loop. |read_start_| <= |read_end_| <= | 241 // State for the read loop. |read_start_| <= |read_end_| <= |
| 210 // |read_buf_->size()|. There's a read in flight (i.e., | 242 // |read_buf_->size()|. There's a read in flight (i.e., |
| 211 // |read_state_| != IDLE) iff |read_end_| == 0. | 243 // |read_state_| != IDLE) iff |read_end_| == 0. |
| 212 AsyncIOState read_state_; | 244 AsyncIOState read_state_; |
| 213 scoped_refptr<net::IOBufferWithSize> read_buf_; | 245 scoped_refptr<net::IOBufferWithSize> read_buf_; |
| 214 size_t read_start_, read_end_; | 246 size_t read_start_, read_end_; |
| 215 | 247 |
| 216 // State for the write loop. |write_end_| <= |write_buf_->size()|. | 248 // State for the write loop. |write_end_| <= |write_buf_->size()|. |
| 217 // There's a write in flight (i.e., |write_state_| != IDLE) iff | 249 // There's a write in flight (i.e., |write_state_| != IDLE) iff |
| 218 // |write_end_| > 0. | 250 // |write_end_| > 0. |
| 219 AsyncIOState write_state_; | 251 AsyncIOState write_state_; |
| 220 scoped_refptr<net::IOBufferWithSize> write_buf_; | 252 scoped_refptr<net::IOBufferWithSize> write_buf_; |
| 221 size_t write_end_; | 253 size_t write_end_; |
| 222 | 254 |
| 223 DISALLOW_COPY_AND_ASSIGN(ChromeAsyncSocket); | 255 DISALLOW_COPY_AND_ASSIGN(ChromeAsyncSocket); |
| 224 }; | 256 }; |
| 225 | 257 |
| 226 } // namespace notifier | 258 } // namespace notifier |
| 227 | 259 |
| 228 #endif // JINGLE_NOTIFIER_BASE_CHROME_ASYNC_SOCKET_H_ | 260 #endif // JINGLE_NOTIFIER_BASE_CHROME_ASYNC_SOCKET_H_ |
| OLD | NEW |