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_HTTP_HTTP_STREAM_FACTORY_IMPL_JOB_H_ | 5 #ifndef NET_HTTP_HTTP_STREAM_FACTORY_IMPL_JOB_H_ |
6 #define NET_HTTP_HTTP_STREAM_FACTORY_IMPL_JOB_H_ | 6 #define NET_HTTP_HTTP_STREAM_FACTORY_IMPL_JOB_H_ |
7 | 7 |
8 #include "base/ref_counted.h" | 8 #include "base/ref_counted.h" |
9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
10 #include "base/task.h" | 10 #include "base/task.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 class HttpStream; | 28 class HttpStream; |
29 class SOCKSSocketParams; | 29 class SOCKSSocketParams; |
30 class SSLSocketParams; | 30 class SSLSocketParams; |
31 class TCPSocketParams; | 31 class TCPSocketParams; |
32 | 32 |
33 // An HttpStreamRequestImpl exists for each stream which is in progress of being | 33 // An HttpStreamRequestImpl exists for each stream which is in progress of being |
34 // created for the StreamFactory. | 34 // created for the StreamFactory. |
35 class HttpStreamFactoryImpl::Job { | 35 class HttpStreamFactoryImpl::Job { |
36 public: | 36 public: |
37 Job(HttpStreamFactoryImpl* stream_factory, | 37 Job(HttpStreamFactoryImpl* stream_factory, |
38 HttpNetworkSession* session); | 38 HttpNetworkSession* session, |
| 39 const HttpRequestInfo& request_info, |
| 40 const SSLConfig& ssl_config, |
| 41 const BoundNetLog& net_log); |
39 ~Job(); | 42 ~Job(); |
40 | 43 |
41 // Start initiates the process of creating a new HttpStream. | 44 // Start initiates the process of creating a new HttpStream. |request| will be |
42 // 3 parameters are passed in by reference. The caller asserts that the | 45 // notified upon completion if the Job has not been Orphan()'d. |
43 // lifecycle of these parameters will remain valid until the stream is | 46 void Start(Request* request); |
44 // created, failed, or destroyed. In the first two cases, the delegate will | |
45 // be called to notify completion of the request. | |
46 void Start(Request* request, | |
47 const HttpRequestInfo& request_info, | |
48 const SSLConfig& ssl_config, | |
49 const BoundNetLog& net_log); | |
50 | 47 |
51 int Preconnect(int num_streams, | 48 // Preconnect will attempt to request |num_streams| sockets from the |
52 const HttpRequestInfo& request_info, | 49 // appropriate ClientSocketPool. |
53 const SSLConfig& ssl_config, | 50 int Preconnect(int num_streams); |
54 const BoundNetLog& net_log); | |
55 | 51 |
56 int RestartTunnelWithProxyAuth(const string16& username, | 52 int RestartTunnelWithProxyAuth(const string16& username, |
57 const string16& password); | 53 const string16& password); |
58 LoadState GetLoadState() const; | 54 LoadState GetLoadState() const; |
59 | 55 |
| 56 // Marks this Job as the "alternate" job, from Alternate-Protocol. Tracks the |
| 57 // original url so we can mark the Alternate-Protocol as broken if |
| 58 // we fail to connect. |
| 59 void MarkAsAlternate(const GURL& original_url); |
| 60 |
| 61 // Tells |this| to wait for |job| to resume it. |
| 62 void WaitFor(Job* job); |
| 63 |
| 64 // Tells |this| that |job| has determined it still needs to continue |
| 65 // connecting, so allow |this| to continue. If this is not called, then |
| 66 // |request_| is expected to cancel |this| by deleting it. |
| 67 void Resume(Job* job); |
| 68 |
| 69 // Used to detach the Job from |request|. |
60 void Orphan(const Request* request); | 70 void Orphan(const Request* request); |
61 | 71 |
62 bool was_alternate_protocol_available() const; | |
63 bool was_npn_negotiated() const; | 72 bool was_npn_negotiated() const; |
64 bool using_spdy() const; | 73 bool using_spdy() const; |
65 const BoundNetLog& net_log() const { return net_log_; } | 74 const BoundNetLog& net_log() const { return net_log_; } |
66 | 75 |
67 const SSLConfig& ssl_config() const; | 76 const SSLConfig& ssl_config() const; |
68 const ProxyInfo& proxy_info() const; | 77 const ProxyInfo& proxy_info() const; |
69 | 78 |
70 // Indicates whether or not this job is performing a preconnect. | 79 // Indicates whether or not this job is performing a preconnect. |
71 bool IsPreconnecting() const; | 80 bool IsPreconnecting() const; |
72 | 81 |
73 // Indicates whether or not this Job has been orphaned by a Request. | 82 // Indicates whether or not this Job has been orphaned by a Request. |
74 bool IsOrphaned() const; | 83 bool IsOrphaned() const; |
75 | 84 |
76 private: | 85 private: |
77 enum AlternateProtocolMode { | |
78 kUnspecified, // Unspecified, check HttpAlternateProtocols | |
79 kUsingAlternateProtocol, // Using an alternate protocol | |
80 kDoNotUseAlternateProtocol, // Failed to connect once, do not try again. | |
81 }; | |
82 | |
83 enum State { | 86 enum State { |
84 STATE_RESOLVE_PROXY, | 87 STATE_RESOLVE_PROXY, |
85 STATE_RESOLVE_PROXY_COMPLETE, | 88 STATE_RESOLVE_PROXY_COMPLETE, |
| 89 |
| 90 // Note that when Alternate-Protocol says we can connect to an alternate |
| 91 // port using a different protocol, we have the choice of communicating over |
| 92 // the original protocol, or speaking the alternate protocol (currently, |
| 93 // only npn-spdy) over an alternate port. For a cold page load, the http |
| 94 // connection that delivers the http response that has the |
| 95 // Alternate-Protocol header will already be warm. So, blocking the next |
| 96 // http request on establishing a new npn-spdy connection would incur extra |
| 97 // latency. Even if the http connection was not reused, establishing a new |
| 98 // http connection is typically faster than npn-spdy, since npn-spdy |
| 99 // requires a SSL handshake. Therefore, we start both the http and the |
| 100 // npn-spdy jobs in parallel. In order not to unnecessarily waste sockets, |
| 101 // we have the http job block on the npn-spdy job after proxy resolution. |
| 102 // The npn-spdy job will Resume() the http job if, in |
| 103 // STATE_INIT_CONNECTION_COMPLETE, it detects an error or does not find an |
| 104 // existing SpdySession. In that case, the http and npn-spdy jobs will race. |
| 105 STATE_WAIT_FOR_JOB, |
| 106 STATE_WAIT_FOR_JOB_COMPLETE, |
| 107 |
86 STATE_INIT_CONNECTION, | 108 STATE_INIT_CONNECTION, |
87 STATE_INIT_CONNECTION_COMPLETE, | 109 STATE_INIT_CONNECTION_COMPLETE, |
88 STATE_WAITING_USER_ACTION, | 110 STATE_WAITING_USER_ACTION, |
89 STATE_RESTART_TUNNEL_AUTH, | 111 STATE_RESTART_TUNNEL_AUTH, |
90 STATE_RESTART_TUNNEL_AUTH_COMPLETE, | 112 STATE_RESTART_TUNNEL_AUTH_COMPLETE, |
91 STATE_CREATE_STREAM, | 113 STATE_CREATE_STREAM, |
92 STATE_CREATE_STREAM_COMPLETE, | 114 STATE_CREATE_STREAM_COMPLETE, |
93 STATE_DRAIN_BODY_FOR_AUTH_RESTART, | 115 STATE_DRAIN_BODY_FOR_AUTH_RESTART, |
94 STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE, | 116 STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE, |
95 STATE_DONE, | 117 STATE_DONE, |
96 STATE_NONE | 118 STATE_NONE |
97 }; | 119 }; |
98 | 120 |
99 void OnStreamReadyCallback(); | 121 void OnStreamReadyCallback(); |
100 void OnSpdySessionReadyCallback(); | 122 void OnSpdySessionReadyCallback(); |
101 void OnStreamFailedCallback(int result); | 123 void OnStreamFailedCallback(int result); |
102 void OnCertificateErrorCallback(int result, const SSLInfo& ssl_info); | 124 void OnCertificateErrorCallback(int result, const SSLInfo& ssl_info); |
103 void OnNeedsProxyAuthCallback(const HttpResponseInfo& response_info, | 125 void OnNeedsProxyAuthCallback(const HttpResponseInfo& response_info, |
104 HttpAuthController* auth_controller); | 126 HttpAuthController* auth_controller); |
105 void OnNeedsClientAuthCallback(SSLCertRequestInfo* cert_info); | 127 void OnNeedsClientAuthCallback(SSLCertRequestInfo* cert_info); |
106 void OnHttpsProxyTunnelResponseCallback(const HttpResponseInfo& response_info, | 128 void OnHttpsProxyTunnelResponseCallback(const HttpResponseInfo& response_info, |
107 HttpStream* stream); | 129 HttpStream* stream); |
108 void OnPreconnectsComplete(); | 130 void OnPreconnectsComplete(); |
109 | 131 |
110 void OnIOComplete(int result); | 132 void OnIOComplete(int result); |
111 int RunLoop(int result); | 133 int RunLoop(int result); |
112 int DoLoop(int result); | 134 int DoLoop(int result); |
113 int StartInternal(const HttpRequestInfo& request_info, | 135 int StartInternal(); |
114 const SSLConfig& ssl_config, | |
115 const BoundNetLog& net_log); | |
116 | 136 |
117 // Each of these methods corresponds to a State value. Those with an input | 137 // Each of these methods corresponds to a State value. Those with an input |
118 // argument receive the result from the previous state. If a method returns | 138 // argument receive the result from the previous state. If a method returns |
119 // ERR_IO_PENDING, then the result from OnIOComplete will be passed to the | 139 // ERR_IO_PENDING, then the result from OnIOComplete will be passed to the |
120 // next state method as the result arg. | 140 // next state method as the result arg. |
121 int DoResolveProxy(); | 141 int DoResolveProxy(); |
122 int DoResolveProxyComplete(int result); | 142 int DoResolveProxyComplete(int result); |
| 143 int DoWaitForJob(); |
| 144 int DoWaitForJobComplete(int result); |
123 int DoInitConnection(); | 145 int DoInitConnection(); |
124 int DoInitConnectionComplete(int result); | 146 int DoInitConnectionComplete(int result); |
125 int DoWaitingUserAction(int result); | 147 int DoWaitingUserAction(int result); |
126 int DoCreateStream(); | 148 int DoCreateStream(); |
127 int DoCreateStreamComplete(int result); | 149 int DoCreateStreamComplete(int result); |
128 int DoRestartTunnelAuth(); | 150 int DoRestartTunnelAuth(); |
129 int DoRestartTunnelAuthComplete(int result); | 151 int DoRestartTunnelAuthComplete(int result); |
130 | 152 |
131 // Returns to STATE_INIT_CONNECTION and resets some state. | 153 // Returns to STATE_INIT_CONNECTION and resets some state. |
132 void ReturnToStateInitConnection(bool close_connection); | 154 void ReturnToStateInitConnection(bool close_connection); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 // OK if it can be ignored, or the error code otherwise. | 189 // OK if it can be ignored, or the error code otherwise. |
168 int HandleCertificateError(int error); | 190 int HandleCertificateError(int error); |
169 | 191 |
170 // Called to handle a client certificate request. | 192 // Called to handle a client certificate request. |
171 int HandleCertificateRequest(int error); | 193 int HandleCertificateRequest(int error); |
172 | 194 |
173 // Moves this stream request into SPDY mode. | 195 // Moves this stream request into SPDY mode. |
174 void SwitchToSpdyMode(); | 196 void SwitchToSpdyMode(); |
175 | 197 |
176 // Should we force SPDY to run over SSL for this stream request. | 198 // Should we force SPDY to run over SSL for this stream request. |
177 bool ShouldForceSpdySSL(); | 199 bool ShouldForceSpdySSL() const; |
178 | 200 |
179 // Should we force SPDY to run without SSL for this stream request. | 201 // Should we force SPDY to run without SSL for this stream request. |
180 bool ShouldForceSpdyWithoutSSL(); | 202 bool ShouldForceSpdyWithoutSSL() const; |
181 | 203 |
182 // Record histograms of latency until Connect() completes. | 204 // Record histograms of latency until Connect() completes. |
183 static void LogHttpConnectedMetrics(const ClientSocketHandle& handle); | 205 static void LogHttpConnectedMetrics(const ClientSocketHandle& handle); |
184 | 206 |
185 Request* request_; | 207 Request* request_; |
186 | 208 |
187 HttpRequestInfo request_info_; | 209 const HttpRequestInfo request_info_; |
188 ProxyInfo proxy_info_; | 210 ProxyInfo proxy_info_; |
189 SSLConfig ssl_config_; | 211 SSLConfig ssl_config_; |
| 212 const BoundNetLog net_log_; |
190 | 213 |
191 CompletionCallbackImpl<Job> io_callback_; | 214 CompletionCallbackImpl<Job> io_callback_; |
192 scoped_ptr<ClientSocketHandle> connection_; | 215 scoped_ptr<ClientSocketHandle> connection_; |
193 HttpNetworkSession* const session_; | 216 HttpNetworkSession* const session_; |
194 HttpStreamFactoryImpl* const stream_factory_; | 217 HttpStreamFactoryImpl* const stream_factory_; |
195 BoundNetLog net_log_; | |
196 State next_state_; | 218 State next_state_; |
197 ProxyService::PacRequest* pac_request_; | 219 ProxyService::PacRequest* pac_request_; |
198 SSLInfo ssl_info_; | 220 SSLInfo ssl_info_; |
199 // The hostname and port of the endpoint. This is not necessarily the one | 221 |
200 // specified by the URL, due to Alternate-Protocol or fixed testing ports. | 222 // The origin server we're trying to reach. |
201 HostPortPair endpoint_; | 223 HostPortPair origin_; |
| 224 |
| 225 // If this is a Job for an "Alternate-Protocol", then this will be non-NULL |
| 226 // and will specify the original URL. |
| 227 scoped_ptr<GURL> original_url_; |
| 228 |
| 229 // This is the Job we're dependent on. It will notify us if/when it's OK to |
| 230 // proceed. |
| 231 Job* blocking_job_; |
| 232 |
| 233 // |dependent_job_| is dependent on |this|. Notify it when it's ok to proceed. |
| 234 Job* dependent_job_; |
202 | 235 |
203 // True if handling a HTTPS request, or using SPDY with SSL | 236 // True if handling a HTTPS request, or using SPDY with SSL |
204 bool using_ssl_; | 237 bool using_ssl_; |
205 | 238 |
206 // True if this network transaction is using SPDY instead of HTTP. | 239 // True if this network transaction is using SPDY instead of HTTP. |
207 bool using_spdy_; | 240 bool using_spdy_; |
208 | 241 |
209 // Force spdy for all connections. | 242 // Force spdy for all connections. |
210 bool force_spdy_always_; | 243 bool force_spdy_always_; |
211 | 244 |
212 // Force spdy only for SSL connections. | 245 // Force spdy only for SSL connections. |
213 bool force_spdy_over_ssl_; | 246 bool force_spdy_over_ssl_; |
214 | 247 |
215 // The certificate error while using SPDY over SSL for insecure URLs. | 248 // The certificate error while using SPDY over SSL for insecure URLs. |
216 int spdy_certificate_error_; | 249 int spdy_certificate_error_; |
217 | 250 |
218 scoped_refptr<HttpAuthController> | 251 scoped_refptr<HttpAuthController> |
219 auth_controllers_[HttpAuth::AUTH_NUM_TARGETS]; | 252 auth_controllers_[HttpAuth::AUTH_NUM_TARGETS]; |
220 | 253 |
221 AlternateProtocolMode alternate_protocol_mode_; | |
222 | |
223 // Only valid if |alternate_protocol_mode_| == kUsingAlternateProtocol. | |
224 HttpAlternateProtocols::Protocol alternate_protocol_; | |
225 | |
226 // True when the tunnel is in the process of being established - we can't | 254 // True when the tunnel is in the process of being established - we can't |
227 // read from the socket until the tunnel is done. | 255 // read from the socket until the tunnel is done. |
228 bool establishing_tunnel_; | 256 bool establishing_tunnel_; |
229 | 257 |
230 scoped_ptr<HttpStream> stream_; | 258 scoped_ptr<HttpStream> stream_; |
231 | 259 |
232 // True if finding the connection for this request found an alternate | |
233 // protocol was available. | |
234 bool was_alternate_protocol_available_; | |
235 | |
236 // True if we negotiated NPN. | 260 // True if we negotiated NPN. |
237 bool was_npn_negotiated_; | 261 bool was_npn_negotiated_; |
238 | 262 |
239 // 0 if we're not preconnecting. Otherwise, the number of streams to | 263 // 0 if we're not preconnecting. Otherwise, the number of streams to |
240 // preconnect. | 264 // preconnect. |
241 int num_streams_; | 265 int num_streams_; |
242 | 266 |
243 // Initialized when we create a new SpdySession. | 267 // Initialized when we create a new SpdySession. |
244 scoped_refptr<SpdySession> new_spdy_session_; | 268 scoped_refptr<SpdySession> new_spdy_session_; |
245 | 269 |
246 // Only used if |new_spdy_session_| is non-NULL. | 270 // Only used if |new_spdy_session_| is non-NULL. |
247 bool spdy_session_direct_; | 271 bool spdy_session_direct_; |
248 | 272 |
249 ScopedRunnableMethodFactory<Job> method_factory_; | 273 ScopedRunnableMethodFactory<Job> method_factory_; |
250 | 274 |
251 DISALLOW_COPY_AND_ASSIGN(Job); | 275 DISALLOW_COPY_AND_ASSIGN(Job); |
252 }; | 276 }; |
253 | 277 |
254 } // namespace net | 278 } // namespace net |
255 | 279 |
256 #endif // NET_HTTP_HTTP_STREAM_FACTORY_IMPL_JOB_H_ | 280 #endif // NET_HTTP_HTTP_STREAM_FACTORY_IMPL_JOB_H_ |
OLD | NEW |