| 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 #ifndef NET_SOCKET_CLIENT_SOCKET_HANDLE_H_ | 5 #ifndef NET_SOCKET_CLIENT_SOCKET_HANDLE_H_ |
| 6 #define NET_SOCKET_CLIENT_SOCKET_HANDLE_H_ | 6 #define NET_SOCKET_CLIENT_SOCKET_HANDLE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/time.h" | 14 #include "base/time.h" |
| 15 #include "net/base/completion_callback.h" | 15 #include "net/base/completion_callback.h" |
| 16 #include "net/base/load_states.h" | 16 #include "net/base/load_states.h" |
| 17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 18 #include "net/base/net_log.h" | 18 #include "net/base/net_log.h" |
| 19 #include "net/base/request_priority.h" | 19 #include "net/base/request_priority.h" |
| 20 #include "net/http/http_response_info.h" | 20 #include "net/http/http_response_info.h" |
| 21 #include "net/socket/client_socket.h" | |
| 22 #include "net/socket/client_socket_pool.h" | 21 #include "net/socket/client_socket_pool.h" |
| 22 #include "net/socket/stream_socket.h" |
| 23 | 23 |
| 24 namespace net { | 24 namespace net { |
| 25 | 25 |
| 26 // A container for a ClientSocket. | 26 // A container for a StreamSocket. |
| 27 // | 27 // |
| 28 // The handle's |group_name| uniquely identifies the origin and type of the | 28 // The handle's |group_name| uniquely identifies the origin and type of the |
| 29 // connection. It is used by the ClientSocketPool to group similar connected | 29 // connection. It is used by the ClientSocketPool to group similar connected |
| 30 // client socket objects. | 30 // client socket objects. |
| 31 // | 31 // |
| 32 class ClientSocketHandle { | 32 class ClientSocketHandle { |
| 33 public: | 33 public: |
| 34 enum SocketReuseType { | 34 enum SocketReuseType { |
| 35 UNUSED = 0, // unused socket that just finished connecting | 35 UNUSED = 0, // unused socket that just finished connecting |
| 36 UNUSED_IDLE, // unused socket that has been idle for awhile | 36 UNUSED_IDLE, // unused socket that has been idle for awhile |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 PoolType* pool, | 77 PoolType* pool, |
| 78 const BoundNetLog& net_log); | 78 const BoundNetLog& net_log); |
| 79 | 79 |
| 80 // An initialized handle can be reset, which causes it to return to the | 80 // An initialized handle can be reset, which causes it to return to the |
| 81 // un-initialized state. This releases the underlying socket, which in the | 81 // un-initialized state. This releases the underlying socket, which in the |
| 82 // case of a socket that still has an established connection, indicates that | 82 // case of a socket that still has an established connection, indicates that |
| 83 // the socket may be kept alive for use by a subsequent ClientSocketHandle. | 83 // the socket may be kept alive for use by a subsequent ClientSocketHandle. |
| 84 // | 84 // |
| 85 // NOTE: To prevent the socket from being kept alive, be sure to call its | 85 // NOTE: To prevent the socket from being kept alive, be sure to call its |
| 86 // Disconnect method. This will result in the ClientSocketPool deleting the | 86 // Disconnect method. This will result in the ClientSocketPool deleting the |
| 87 // ClientSocket. | 87 // StreamSocket. |
| 88 void Reset(); | 88 void Reset(); |
| 89 | 89 |
| 90 // Used after Init() is called, but before the ClientSocketPool has | 90 // Used after Init() is called, but before the ClientSocketPool has |
| 91 // initialized the ClientSocketHandle. | 91 // initialized the ClientSocketHandle. |
| 92 LoadState GetLoadState() const; | 92 LoadState GetLoadState() const; |
| 93 | 93 |
| 94 // Returns true when Init() has completed successfully. | 94 // Returns true when Init() has completed successfully. |
| 95 bool is_initialized() const { return is_initialized_; } | 95 bool is_initialized() const { return is_initialized_; } |
| 96 | 96 |
| 97 // Returns the time tick when Init() was called. | 97 // Returns the time tick when Init() was called. |
| 98 base::TimeTicks init_time() const { return init_time_; } | 98 base::TimeTicks init_time() const { return init_time_; } |
| 99 | 99 |
| 100 // Returns the time between Init() and when is_initialized() becomes true. | 100 // Returns the time between Init() and when is_initialized() becomes true. |
| 101 base::TimeDelta setup_time() const { return setup_time_; } | 101 base::TimeDelta setup_time() const { return setup_time_; } |
| 102 | 102 |
| 103 // Used by ClientSocketPool to initialize the ClientSocketHandle. | 103 // Used by ClientSocketPool to initialize the ClientSocketHandle. |
| 104 void set_is_reused(bool is_reused) { is_reused_ = is_reused; } | 104 void set_is_reused(bool is_reused) { is_reused_ = is_reused; } |
| 105 void set_socket(ClientSocket* s) { socket_.reset(s); } | 105 void set_socket(StreamSocket* s) { socket_.reset(s); } |
| 106 void set_idle_time(base::TimeDelta idle_time) { idle_time_ = idle_time; } | 106 void set_idle_time(base::TimeDelta idle_time) { idle_time_ = idle_time; } |
| 107 void set_pool_id(int id) { pool_id_ = id; } | 107 void set_pool_id(int id) { pool_id_ = id; } |
| 108 void set_is_ssl_error(bool is_ssl_error) { is_ssl_error_ = is_ssl_error; } | 108 void set_is_ssl_error(bool is_ssl_error) { is_ssl_error_ = is_ssl_error; } |
| 109 void set_ssl_error_response_info(const HttpResponseInfo& ssl_error_state) { | 109 void set_ssl_error_response_info(const HttpResponseInfo& ssl_error_state) { |
| 110 ssl_error_response_info_ = ssl_error_state; | 110 ssl_error_response_info_ = ssl_error_state; |
| 111 } | 111 } |
| 112 void set_pending_http_proxy_connection(ClientSocketHandle* connection) { | 112 void set_pending_http_proxy_connection(ClientSocketHandle* connection) { |
| 113 pending_http_proxy_connection_.reset(connection); | 113 pending_http_proxy_connection_.reset(connection); |
| 114 } | 114 } |
| 115 | 115 |
| 116 // Only valid if there is no |socket_|. | 116 // Only valid if there is no |socket_|. |
| 117 bool is_ssl_error() const { | 117 bool is_ssl_error() const { |
| 118 DCHECK(socket_.get() == NULL); | 118 DCHECK(socket_.get() == NULL); |
| 119 return is_ssl_error_; | 119 return is_ssl_error_; |
| 120 } | 120 } |
| 121 // On an ERR_PROXY_AUTH_REQUESTED error, the |headers| and |auth_challenge| | 121 // On an ERR_PROXY_AUTH_REQUESTED error, the |headers| and |auth_challenge| |
| 122 // fields are filled in. On an ERR_SSL_CLIENT_AUTH_CERT_NEEDED error, | 122 // fields are filled in. On an ERR_SSL_CLIENT_AUTH_CERT_NEEDED error, |
| 123 // the |cert_request_info| field is set. | 123 // the |cert_request_info| field is set. |
| 124 const HttpResponseInfo& ssl_error_response_info() const { | 124 const HttpResponseInfo& ssl_error_response_info() const { |
| 125 return ssl_error_response_info_; | 125 return ssl_error_response_info_; |
| 126 } | 126 } |
| 127 ClientSocketHandle* release_pending_http_proxy_connection() { | 127 ClientSocketHandle* release_pending_http_proxy_connection() { |
| 128 return pending_http_proxy_connection_.release(); | 128 return pending_http_proxy_connection_.release(); |
| 129 } | 129 } |
| 130 | 130 |
| 131 // These may only be used if is_initialized() is true. | 131 // These may only be used if is_initialized() is true. |
| 132 const std::string& group_name() const { return group_name_; } | 132 const std::string& group_name() const { return group_name_; } |
| 133 int id() const { return pool_id_; } | 133 int id() const { return pool_id_; } |
| 134 ClientSocket* socket() { return socket_.get(); } | 134 StreamSocket* socket() { return socket_.get(); } |
| 135 ClientSocket* release_socket() { return socket_.release(); } | 135 StreamSocket* release_socket() { return socket_.release(); } |
| 136 bool is_reused() const { return is_reused_; } | 136 bool is_reused() const { return is_reused_; } |
| 137 base::TimeDelta idle_time() const { return idle_time_; } | 137 base::TimeDelta idle_time() const { return idle_time_; } |
| 138 SocketReuseType reuse_type() const { | 138 SocketReuseType reuse_type() const { |
| 139 if (is_reused()) { | 139 if (is_reused()) { |
| 140 return REUSED_IDLE; | 140 return REUSED_IDLE; |
| 141 } else if (idle_time() == base::TimeDelta()) { | 141 } else if (idle_time() == base::TimeDelta()) { |
| 142 return UNUSED; | 142 return UNUSED; |
| 143 } else { | 143 } else { |
| 144 return UNUSED_IDLE; | 144 return UNUSED_IDLE; |
| 145 } | 145 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 156 // Resets the state of the ClientSocketHandle. |cancel| indicates whether or | 156 // Resets the state of the ClientSocketHandle. |cancel| indicates whether or |
| 157 // not to try to cancel the request with the ClientSocketPool. Does not | 157 // not to try to cancel the request with the ClientSocketPool. Does not |
| 158 // reset the supplemental error state. | 158 // reset the supplemental error state. |
| 159 void ResetInternal(bool cancel); | 159 void ResetInternal(bool cancel); |
| 160 | 160 |
| 161 // Resets the supplemental error state. | 161 // Resets the supplemental error state. |
| 162 void ResetErrorState(); | 162 void ResetErrorState(); |
| 163 | 163 |
| 164 bool is_initialized_; | 164 bool is_initialized_; |
| 165 ClientSocketPool* pool_; | 165 ClientSocketPool* pool_; |
| 166 scoped_ptr<ClientSocket> socket_; | 166 scoped_ptr<StreamSocket> socket_; |
| 167 std::string group_name_; | 167 std::string group_name_; |
| 168 bool is_reused_; | 168 bool is_reused_; |
| 169 CompletionCallbackImpl<ClientSocketHandle> callback_; | 169 CompletionCallbackImpl<ClientSocketHandle> callback_; |
| 170 CompletionCallback* user_callback_; | 170 CompletionCallback* user_callback_; |
| 171 base::TimeDelta idle_time_; | 171 base::TimeDelta idle_time_; |
| 172 int pool_id_; // See ClientSocketPool::ReleaseSocket() for an explanation. | 172 int pool_id_; // See ClientSocketPool::ReleaseSocket() for an explanation. |
| 173 bool is_ssl_error_; | 173 bool is_ssl_error_; |
| 174 HttpResponseInfo ssl_error_response_info_; | 174 HttpResponseInfo ssl_error_response_info_; |
| 175 scoped_ptr<ClientSocketHandle> pending_http_proxy_connection_; | 175 scoped_ptr<ClientSocketHandle> pending_http_proxy_connection_; |
| 176 base::TimeTicks init_time_; | 176 base::TimeTicks init_time_; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 207 user_callback_ = callback; | 207 user_callback_ = callback; |
| 208 } else { | 208 } else { |
| 209 HandleInitCompletion(rv); | 209 HandleInitCompletion(rv); |
| 210 } | 210 } |
| 211 return rv; | 211 return rv; |
| 212 } | 212 } |
| 213 | 213 |
| 214 } // namespace net | 214 } // namespace net |
| 215 | 215 |
| 216 #endif // NET_SOCKET_CLIENT_SOCKET_HANDLE_H_ | 216 #endif // NET_SOCKET_CLIENT_SOCKET_HANDLE_H_ |
| OLD | NEW |