OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
12 #include "base/scoped_ptr.h" | 12 #include "base/scoped_ptr.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 #include "net/base/completion_callback.h" | 14 #include "net/base/completion_callback.h" |
15 #include "net/base/load_states.h" | 15 #include "net/base/load_states.h" |
16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
17 #include "net/base/net_log.h" | 17 #include "net/base/net_log.h" |
18 #include "net/base/request_priority.h" | 18 #include "net/base/request_priority.h" |
| 19 #include "net/http/http_response_info.h" |
19 #include "net/socket/client_socket.h" | 20 #include "net/socket/client_socket.h" |
20 #include "net/socket/client_socket_pool.h" | 21 #include "net/socket/client_socket_pool.h" |
21 | 22 |
22 namespace net { | 23 namespace net { |
23 | 24 |
24 // A container for a ClientSocket. | 25 // A container for a ClientSocket. |
25 // | 26 // |
26 // The handle's |group_name| uniquely identifies the origin and type of the | 27 // The handle's |group_name| uniquely identifies the origin and type of the |
27 // connection. It is used by the ClientSocketPool to group similar connected | 28 // connection. It is used by the ClientSocketPool to group similar connected |
28 // client socket objects. | 29 // client socket objects. |
(...skipping 24 matching lines...) Expand all Loading... |
53 // | 54 // |
54 // This method returns ERR_IO_PENDING if it cannot complete synchronously, in | 55 // This method returns ERR_IO_PENDING if it cannot complete synchronously, in |
55 // which case the consumer will be notified of completion via |callback|. | 56 // which case the consumer will be notified of completion via |callback|. |
56 // | 57 // |
57 // If the pool was not able to reuse an existing socket, the new socket | 58 // If the pool was not able to reuse an existing socket, the new socket |
58 // may report a recoverable error. In this case, the return value will | 59 // may report a recoverable error. In this case, the return value will |
59 // indicate an error and the socket member will be set. If it is determined | 60 // indicate an error and the socket member will be set. If it is determined |
60 // that the error is not recoverable, the Disconnect method should be used | 61 // that the error is not recoverable, the Disconnect method should be used |
61 // on the socket, so that it does not get reused. | 62 // on the socket, so that it does not get reused. |
62 // | 63 // |
| 64 // A non-recoverable error may set additional state in the ClientSocketHandle |
| 65 // to allow the caller to determine what went wrong. |
| 66 // |
63 // Init may be called multiple times. | 67 // Init may be called multiple times. |
64 // | 68 // |
65 // Profiling information for the request is saved to |net_log| if non-NULL. | 69 // Profiling information for the request is saved to |net_log| if non-NULL. |
66 // | 70 // |
67 template <typename SocketParams, typename PoolType> | 71 template <typename SocketParams, typename PoolType> |
68 int Init(const std::string& group_name, | 72 int Init(const std::string& group_name, |
69 const scoped_refptr<SocketParams>& socket_params, | 73 const scoped_refptr<SocketParams>& socket_params, |
70 RequestPriority priority, | 74 RequestPriority priority, |
71 CompletionCallback* callback, | 75 CompletionCallback* callback, |
72 const scoped_refptr<PoolType>& pool, | 76 const scoped_refptr<PoolType>& pool, |
(...skipping 20 matching lines...) Expand all Loading... |
93 base::TimeTicks init_time() const { return init_time_; } | 97 base::TimeTicks init_time() const { return init_time_; } |
94 | 98 |
95 // Returns the time between Init() and when is_initialized() becomes true. | 99 // Returns the time between Init() and when is_initialized() becomes true. |
96 base::TimeDelta setup_time() const { return setup_time_; } | 100 base::TimeDelta setup_time() const { return setup_time_; } |
97 | 101 |
98 // Used by ClientSocketPool to initialize the ClientSocketHandle. | 102 // Used by ClientSocketPool to initialize the ClientSocketHandle. |
99 void set_is_reused(bool is_reused) { is_reused_ = is_reused; } | 103 void set_is_reused(bool is_reused) { is_reused_ = is_reused; } |
100 void set_socket(ClientSocket* s) { socket_.reset(s); } | 104 void set_socket(ClientSocket* s) { socket_.reset(s); } |
101 void set_idle_time(base::TimeDelta idle_time) { idle_time_ = idle_time; } | 105 void set_idle_time(base::TimeDelta idle_time) { idle_time_ = idle_time; } |
102 void set_pool_id(int id) { pool_id_ = id; } | 106 void set_pool_id(int id) { pool_id_ = id; } |
| 107 void set_tunnel_auth_response_info( |
| 108 const scoped_refptr<HttpResponseHeaders>& headers, |
| 109 const scoped_refptr<AuthChallengeInfo>& auth_challenge) { |
| 110 tunnel_auth_response_info_.headers = headers; |
| 111 tunnel_auth_response_info_.auth_challenge = auth_challenge; |
| 112 } |
| 113 void set_is_ssl_error(bool is_ssl_error) { is_ssl_error_ = is_ssl_error; } |
103 | 114 |
104 // These may only be used if is_initialized() is true. | 115 // These may only be used if is_initialized() is true. |
105 const std::string& group_name() const { return group_name_; } | 116 const std::string& group_name() const { return group_name_; } |
106 ClientSocket* socket() { return socket_.get(); } | 117 ClientSocket* socket() { return socket_.get(); } |
107 ClientSocket* release_socket() { return socket_.release(); } | 118 ClientSocket* release_socket() { return socket_.release(); } |
| 119 const HttpResponseInfo& tunnel_auth_response_info() const { |
| 120 return tunnel_auth_response_info_; |
| 121 } |
| 122 // Only valid if there is no |socket_|. |
| 123 bool is_ssl_error() const { |
| 124 DCHECK(socket_.get() == NULL); |
| 125 return is_ssl_error_; |
| 126 } |
108 bool is_reused() const { return is_reused_; } | 127 bool is_reused() const { return is_reused_; } |
109 base::TimeDelta idle_time() const { return idle_time_; } | 128 base::TimeDelta idle_time() const { return idle_time_; } |
110 SocketReuseType reuse_type() const { | 129 SocketReuseType reuse_type() const { |
111 if (is_reused()) { | 130 if (is_reused()) { |
112 return REUSED_IDLE; | 131 return REUSED_IDLE; |
113 } else if (idle_time() == base::TimeDelta()) { | 132 } else if (idle_time() == base::TimeDelta()) { |
114 return UNUSED; | 133 return UNUSED; |
115 } else { | 134 } else { |
116 return UNUSED_IDLE; | 135 return UNUSED_IDLE; |
117 } | 136 } |
(...skipping 14 matching lines...) Expand all Loading... |
132 | 151 |
133 private: | 152 private: |
134 // Called on asynchronous completion of an Init() request. | 153 // Called on asynchronous completion of an Init() request. |
135 void OnIOComplete(int result); | 154 void OnIOComplete(int result); |
136 | 155 |
137 // Called on completion (both asynchronous & synchronous) of an Init() | 156 // Called on completion (both asynchronous & synchronous) of an Init() |
138 // request. | 157 // request. |
139 void HandleInitCompletion(int result); | 158 void HandleInitCompletion(int result); |
140 | 159 |
141 // Resets the state of the ClientSocketHandle. |cancel| indicates whether or | 160 // Resets the state of the ClientSocketHandle. |cancel| indicates whether or |
142 // not to try to cancel the request with the ClientSocketPool. | 161 // not to try to cancel the request with the ClientSocketPool. Does not |
| 162 // reset the supplemental error state. |
143 void ResetInternal(bool cancel); | 163 void ResetInternal(bool cancel); |
144 | 164 |
| 165 // Resets the supplemental error state. |
| 166 void ResetErrorState(); |
| 167 |
145 scoped_refptr<ClientSocketPool> pool_; | 168 scoped_refptr<ClientSocketPool> pool_; |
146 scoped_ptr<ClientSocket> socket_; | 169 scoped_ptr<ClientSocket> socket_; |
147 std::string group_name_; | 170 std::string group_name_; |
148 bool is_reused_; | 171 bool is_reused_; |
149 CompletionCallbackImpl<ClientSocketHandle> callback_; | 172 CompletionCallbackImpl<ClientSocketHandle> callback_; |
150 CompletionCallback* user_callback_; | 173 CompletionCallback* user_callback_; |
151 base::TimeDelta idle_time_; | 174 base::TimeDelta idle_time_; |
152 int pool_id_; // See ClientSocketPool::ReleaseSocket() for an explanation. | 175 int pool_id_; // See ClientSocketPool::ReleaseSocket() for an explanation. |
| 176 bool is_ssl_error_; |
| 177 HttpResponseInfo tunnel_auth_response_info_; |
153 base::TimeTicks init_time_; | 178 base::TimeTicks init_time_; |
154 base::TimeDelta setup_time_; | 179 base::TimeDelta setup_time_; |
155 | 180 |
156 NetLog::Source requesting_source_; | 181 NetLog::Source requesting_source_; |
157 | 182 |
158 DISALLOW_COPY_AND_ASSIGN(ClientSocketHandle); | 183 DISALLOW_COPY_AND_ASSIGN(ClientSocketHandle); |
159 }; | 184 }; |
160 | 185 |
161 // Template function implementation: | 186 // Template function implementation: |
162 template <typename SocketParams, typename PoolType> | 187 template <typename SocketParams, typename PoolType> |
163 int ClientSocketHandle::Init(const std::string& group_name, | 188 int ClientSocketHandle::Init(const std::string& group_name, |
164 const scoped_refptr<SocketParams>& socket_params, | 189 const scoped_refptr<SocketParams>& socket_params, |
165 RequestPriority priority, | 190 RequestPriority priority, |
166 CompletionCallback* callback, | 191 CompletionCallback* callback, |
167 const scoped_refptr<PoolType>& pool, | 192 const scoped_refptr<PoolType>& pool, |
168 const BoundNetLog& net_log) { | 193 const BoundNetLog& net_log) { |
169 requesting_source_ = net_log.source(); | 194 requesting_source_ = net_log.source(); |
170 | 195 |
171 CHECK(!group_name.empty()); | 196 CHECK(!group_name.empty()); |
172 // Note that this will result in a compile error if the SocketParams has not | 197 // Note that this will result in a compile error if the SocketParams has not |
173 // been registered for the PoolType via REGISTER_SOCKET_PARAMS_FOR_POOL | 198 // been registered for the PoolType via REGISTER_SOCKET_PARAMS_FOR_POOL |
174 // (defined in client_socket_pool.h). | 199 // (defined in client_socket_pool.h). |
175 CheckIsValidSocketParamsForPool<PoolType, SocketParams>(); | 200 CheckIsValidSocketParamsForPool<PoolType, SocketParams>(); |
176 ResetInternal(true); | 201 ResetInternal(true); |
| 202 ResetErrorState(); |
177 pool_ = pool; | 203 pool_ = pool; |
178 group_name_ = group_name; | 204 group_name_ = group_name; |
179 init_time_ = base::TimeTicks::Now(); | 205 init_time_ = base::TimeTicks::Now(); |
180 int rv = pool_->RequestSocket( | 206 int rv = pool_->RequestSocket( |
181 group_name, &socket_params, priority, this, &callback_, net_log); | 207 group_name, &socket_params, priority, this, &callback_, net_log); |
182 if (rv == ERR_IO_PENDING) { | 208 if (rv == ERR_IO_PENDING) { |
183 user_callback_ = callback; | 209 user_callback_ = callback; |
184 } else { | 210 } else { |
185 HandleInitCompletion(rv); | 211 HandleInitCompletion(rv); |
186 } | 212 } |
187 return rv; | 213 return rv; |
188 } | 214 } |
189 | 215 |
190 } // namespace net | 216 } // namespace net |
191 | 217 |
192 #endif // NET_SOCKET_CLIENT_SOCKET_HANDLE_H_ | 218 #endif // NET_SOCKET_CLIENT_SOCKET_HANDLE_H_ |
OLD | NEW |