| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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_CONTROLLER_H_ | 5 #ifndef NET_HTTP_HTTP_STREAM_FACTORY_IMPL_JOB_CONTROLLER_H_ |
| 6 #define NET_HTTP_HTTP_STREAM_FACTORY_IMPL_JOB_CONTROLLER_H_ | 6 #define NET_HTTP_HTTP_STREAM_FACTORY_IMPL_JOB_CONTROLLER_H_ |
| 7 | 7 |
| 8 #include "net/base/host_port_pair.h" | 8 #include "net/base/host_port_pair.h" |
| 9 #include "net/base/privacy_mode.h" | 9 #include "net/base/privacy_mode.h" |
| 10 #include "net/http/http_stream_factory_impl_job.h" | 10 #include "net/http/http_stream_factory_impl_job.h" |
| 11 #include "net/http/http_stream_factory_impl_request.h" | 11 #include "net/http/http_stream_factory_impl_request.h" |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 | 14 |
| 15 class NetLogWithSource; | 15 class NetLogWithSource; |
| 16 | 16 |
| 17 // HttpStreamFactoryImpl::JobController manages Request and Job(s). | 17 // HttpStreamFactoryImpl::JobController manages Request and Job(s). |
| 18 class HttpStreamFactoryImpl::JobController | 18 class HttpStreamFactoryImpl::JobController |
| 19 : public HttpStreamFactoryImpl::Job::Delegate, | 19 : public HttpStreamFactoryImpl::Job::Delegate, |
| 20 public HttpStreamFactoryImpl::Request::Helper { | 20 public HttpStreamFactoryImpl::Request::Helper { |
| 21 public: | 21 public: |
| 22 JobController(HttpStreamFactoryImpl* factory, | 22 JobController(HttpStreamFactoryImpl* factory, |
| 23 HttpStreamRequest::Delegate* delegate, | 23 HttpStreamRequest::Delegate* delegate, |
| 24 HttpNetworkSession* session, | 24 HttpNetworkSession* session, |
| 25 JobFactory* job_factory, | 25 JobFactory* job_factory, |
| 26 const HttpRequestInfo& request_info, | 26 const HttpRequestInfo& request_info, |
| 27 bool is_preconnect, | 27 bool is_preconnect, |
| 28 bool enable_ip_based_pooling, | 28 bool enable_ip_based_pooling, |
| 29 bool enable_alternative_services); | 29 bool enable_alternative_services, |
| 30 const SSLConfig& server_ssl_config, |
| 31 const SSLConfig& proxy_ssl_config); |
| 30 | 32 |
| 31 ~JobController() override; | 33 ~JobController() override; |
| 32 | 34 |
| 33 bool for_websockets() override; | 35 bool for_websockets() override; |
| 34 | 36 |
| 35 // Used in tests only for verification purpose. | 37 // Used in tests only for verification purpose. |
| 36 const Job* main_job() const { return main_job_.get(); } | 38 const Job* main_job() const { return main_job_.get(); } |
| 37 const Job* alternative_job() const { return alternative_job_.get(); } | 39 const Job* alternative_job() const { return alternative_job_.get(); } |
| 38 | 40 |
| 39 GURL ApplyHostMappingRules(const GURL& url, HostPortPair* endpoint); | 41 GURL ApplyHostMappingRules(const GURL& url, HostPortPair* endpoint); |
| 40 | 42 |
| 41 // Methods below are called by HttpStreamFactoryImpl only. | 43 // Methods below are called by HttpStreamFactoryImpl only. |
| 42 // Creates request and hands out to HttpStreamFactoryImpl, this will also | 44 // Creates request and hands out to HttpStreamFactoryImpl, this will also |
| 43 // create Job(s) and start serving the created request. | 45 // create Job(s) and start serving the created request. |
| 44 Request* Start(const HttpRequestInfo& request_info, | 46 Request* Start(HttpStreamRequest::Delegate* delegate, |
| 45 HttpStreamRequest::Delegate* delegate, | |
| 46 WebSocketHandshakeStreamBase::CreateHelper* | 47 WebSocketHandshakeStreamBase::CreateHelper* |
| 47 websocket_handshake_stream_create_helper, | 48 websocket_handshake_stream_create_helper, |
| 48 const NetLogWithSource& source_net_log, | 49 const NetLogWithSource& source_net_log, |
| 49 HttpStreamRequest::StreamType stream_type, | 50 HttpStreamRequest::StreamType stream_type, |
| 50 RequestPriority priority, | 51 RequestPriority priority); |
| 51 const SSLConfig& server_ssl_config, | |
| 52 const SSLConfig& proxy_ssl_config); | |
| 53 | 52 |
| 54 void Preconnect(int num_streams, | 53 void Preconnect(int num_streams); |
| 55 const HttpRequestInfo& request_info, | |
| 56 const SSLConfig& server_ssl_config, | |
| 57 const SSLConfig& proxy_ssl_config); | |
| 58 | 54 |
| 59 // From HttpStreamFactoryImpl::Request::Helper. | 55 // From HttpStreamFactoryImpl::Request::Helper. |
| 60 // Returns the LoadState for Request. | 56 // Returns the LoadState for Request. |
| 61 LoadState GetLoadState() const override; | 57 LoadState GetLoadState() const override; |
| 62 | 58 |
| 63 // Called when Request is destructed. Job(s) associated with but not bound to | 59 // Called when Request is destructed. Job(s) associated with but not bound to |
| 64 // |request_| will be deleted. |request_| and |bound_job_| will be nulled if | 60 // |request_| will be deleted. |request_| and |bound_job_| will be nulled if |
| 65 // ever set. | 61 // ever set. |
| 66 void OnRequestComplete() override; | 62 void OnRequestComplete() override; |
| 67 | 63 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 111 |
| 116 // Invoked when |job| needs proxy authentication. | 112 // Invoked when |job| needs proxy authentication. |
| 117 void OnNeedsProxyAuth(Job* job, | 113 void OnNeedsProxyAuth(Job* job, |
| 118 const HttpResponseInfo& proxy_response, | 114 const HttpResponseInfo& proxy_response, |
| 119 const SSLConfig& used_ssl_config, | 115 const SSLConfig& used_ssl_config, |
| 120 const ProxyInfo& used_proxy_info, | 116 const ProxyInfo& used_proxy_info, |
| 121 HttpAuthController* auth_controller) override; | 117 HttpAuthController* auth_controller) override; |
| 122 | 118 |
| 123 bool OnInitConnection(const ProxyInfo& proxy_info) override; | 119 bool OnInitConnection(const ProxyInfo& proxy_info) override; |
| 124 | 120 |
| 125 void OnResolveProxyComplete( | |
| 126 Job* job, | |
| 127 const HttpRequestInfo& request_info, | |
| 128 RequestPriority priority, | |
| 129 const SSLConfig& server_ssl_config, | |
| 130 const SSLConfig& proxy_ssl_config, | |
| 131 HttpStreamRequest::StreamType stream_type) override; | |
| 132 | 121 |
| 133 // Invoked to notify the Request and Factory of the readiness of new | 122 // Invoked to notify the Request and Factory of the readiness of new |
| 134 // SPDY session. | 123 // SPDY session. |
| 135 void OnNewSpdySessionReady(Job* job, | 124 void OnNewSpdySessionReady(Job* job, |
| 136 const base::WeakPtr<SpdySession>& spdy_session, | 125 const base::WeakPtr<SpdySession>& spdy_session, |
| 137 bool direct) override; | 126 bool direct) override; |
| 138 | 127 |
| 139 // Invoked when the |job| finishes pre-connecting sockets. | 128 // Invoked when the |job| finishes pre-connecting sockets. |
| 140 void OnPreconnectsComplete(Job* job) override; | 129 void OnPreconnectsComplete(Job* job) override; |
| 141 | 130 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 // TODO(xunjieli): Added to investigate crbug.com/711721. Remove when no | 173 // TODO(xunjieli): Added to investigate crbug.com/711721. Remove when no |
| 185 // longer needed. | 174 // longer needed. |
| 186 void LogHistograms() const; | 175 void LogHistograms() const; |
| 187 | 176 |
| 188 // Returns the estimated memory usage in bytes. | 177 // Returns the estimated memory usage in bytes. |
| 189 size_t EstimateMemoryUsage() const; | 178 size_t EstimateMemoryUsage() const; |
| 190 | 179 |
| 191 private: | 180 private: |
| 192 friend class JobControllerPeer; | 181 friend class JobControllerPeer; |
| 193 | 182 |
| 194 // Creates Job(s) for |request_|. Job(s) will be owned by |this|. | 183 enum State { |
| 195 void CreateJobs(const HttpRequestInfo& request_info, | 184 STATE_RESOLVE_PROXY, |
| 196 RequestPriority priority, | 185 STATE_RESOLVE_PROXY_COMPLETE, |
| 197 const SSLConfig& server_ssl_config, | 186 STATE_CREATE_JOBS, |
| 198 const SSLConfig& proxy_ssl_config, | 187 STATE_NONE |
| 199 HttpStreamRequest::Delegate* delegate, | 188 }; |
| 200 HttpStreamRequest::StreamType stream_type); | 189 |
| 190 void OnIOComplete(int result); |
| 191 void OnResolveProxyError(int error); |
| 192 void RunLoop(int result); |
| 193 int DoLoop(int result); |
| 194 int DoResolveProxy(); |
| 195 int DoResolveProxyComplete(int result); |
| 196 // Creates Job(s) for |request_info_|. Job(s) will be owned by |this|. |
| 197 int DoCreateJobs(); |
| 201 | 198 |
| 202 // Called to bind |job| to the |request_| and orphan all other jobs that are | 199 // Called to bind |job| to the |request_| and orphan all other jobs that are |
| 203 // still associated with |request_|. | 200 // still associated with |request_|. |
| 204 void BindJob(Job* job); | 201 void BindJob(Job* job); |
| 205 | 202 |
| 206 // Called when |request_| is destructed. | 203 // Called when |request_| is destructed. |
| 207 // Job(s) associated with but not bound to |request_| will be deleted. | 204 // Job(s) associated with but not bound to |request_| will be deleted. |
| 208 void CancelJobs(); | 205 void CancelJobs(); |
| 209 | 206 |
| 210 // Called after BindJob() to notify the unbound job that its result should be | 207 // Called after BindJob() to notify the unbound job that its result should be |
| 211 // ignored by JobController. The unbound job can be canceled or continue until | 208 // ignored by JobController. The unbound job can be canceled or continue until |
| 212 // completion. | 209 // completion. |
| 213 void OrphanUnboundJob(); | 210 void OrphanUnboundJob(); |
| 214 | 211 |
| 215 // Invoked when the orphaned |job| finishes. | 212 // Invoked when the orphaned |job| finishes. |
| 216 void OnOrphanedJobComplete(const Job* job); | 213 void OnOrphanedJobComplete(const Job* job); |
| 217 | 214 |
| 218 // Called when a Job succeeds. | 215 // Called when a Job succeeds. |
| 219 void OnJobSucceeded(Job* job); | 216 void OnJobSucceeded(Job* job); |
| 220 | 217 |
| 221 // Marks completion of the |request_|. | 218 // Marks completion of the |request_|. |
| 222 void MarkRequestComplete(bool was_alpn_negotiated, | 219 void MarkRequestComplete(bool was_alpn_negotiated, |
| 223 NextProto negotiated_protocol, | 220 NextProto negotiated_protocol, |
| 224 bool using_spdy); | 221 bool using_spdy); |
| 225 | 222 |
| 226 // Must be called when the alternative job fails. |net_error| is the net error | 223 // Must be called when the alternative service job fails. |net_error| is the |
| 227 // of the failed alternative job. | 224 // net error of the failed alternative service job. |
| 228 void OnAlternativeJobFailed(int net_error); | 225 void OnAlternativeServiceJobFailed(int net_error); |
| 226 |
| 227 // Must be called when the alternative proxy job fails. |net_error| is the |
| 228 // net error of the failed alternative proxy job. |
| 229 void OnAlternativeProxyJobFailed(int net_error); |
| 229 | 230 |
| 230 // Called to report to http_server_properties to mark alternative service | 231 // Called to report to http_server_properties to mark alternative service |
| 231 // broken. | 232 // broken. |
| 232 void ReportBrokenAlternativeService(); | 233 void ReportBrokenAlternativeService(); |
| 233 | 234 |
| 234 void MaybeNotifyFactoryOfCompletion(); | 235 void MaybeNotifyFactoryOfCompletion(); |
| 235 | 236 |
| 237 void NotifyRequestFailed(int rv); |
| 238 |
| 236 // Called to resume the main job with delay. Main job is resumed only when | 239 // Called to resume the main job with delay. Main job is resumed only when |
| 237 // |alternative_job_| has failed or |main_job_wait_time_| elapsed. | 240 // |alternative_job_| has failed or |main_job_wait_time_| elapsed. |
| 238 void MaybeResumeMainJob(Job* job, const base::TimeDelta& delay); | 241 void MaybeResumeMainJob(Job* job, const base::TimeDelta& delay); |
| 239 | 242 |
| 240 // Posts a task to resume the main job after |delay|. | 243 // Posts a task to resume the main job after |delay|. |
| 241 void ResumeMainJobLater(const base::TimeDelta& delay); | 244 void ResumeMainJobLater(const base::TimeDelta& delay); |
| 242 | 245 |
| 243 // Resumes the main job immediately. | 246 // Resumes the main job immediately. |
| 244 void ResumeMainJob(); | 247 void ResumeMainJob(); |
| 245 | 248 |
| 246 AlternativeService GetAlternativeServiceFor( | 249 AlternativeService GetAlternativeServiceFor( |
| 247 const HttpRequestInfo& request_info, | 250 const HttpRequestInfo& request_info, |
| 248 HttpStreamRequest::Delegate* delegate, | 251 HttpStreamRequest::Delegate* delegate, |
| 249 HttpStreamRequest::StreamType stream_type); | 252 HttpStreamRequest::StreamType stream_type); |
| 250 | 253 |
| 251 AlternativeService GetAlternativeServiceForInternal( | 254 AlternativeService GetAlternativeServiceForInternal( |
| 252 const HttpRequestInfo& request_info, | 255 const HttpRequestInfo& request_info, |
| 253 HttpStreamRequest::Delegate* delegate, | 256 HttpStreamRequest::Delegate* delegate, |
| 254 HttpStreamRequest::StreamType stream_type); | 257 HttpStreamRequest::StreamType stream_type); |
| 255 | 258 |
| 256 // Remove session from the SpdySessionRequestMap. | 259 // Remove session from the SpdySessionRequestMap. |
| 257 void RemoveRequestFromSpdySessionRequestMap(); | 260 void RemoveRequestFromSpdySessionRequestMap(); |
| 258 | 261 |
| 259 // Returns true if the |request_| can be fetched via an alternative | 262 // Returns true if the |request_| can be fetched via an alternative |
| 260 // proxy server, and sets |alternative_proxy_server| to the available | 263 // proxy server, and sets |alternative_proxy_server| to the available |
| 261 // alternative proxy server. |alternative_proxy_server| should not be null, | 264 // alternative proxy server. |alternative_proxy_server| should not be null, |
| 262 // and is owned by the caller. | 265 // and is owned by the caller. |
| 263 bool ShouldCreateAlternativeProxyServerJob( | 266 bool ShouldCreateAlternativeProxyServerJob( |
| 264 Job* job, | |
| 265 const ProxyInfo& proxy_info_, | 267 const ProxyInfo& proxy_info_, |
| 266 const GURL& url, | 268 const GURL& url, |
| 267 ProxyServer* alternative_proxy_server) const; | 269 ProxyServer* alternative_proxy_server) const; |
| 268 | 270 |
| 269 // Records histogram metrics for the usage of alternative protocol. Must be | 271 // Records histogram metrics for the usage of alternative protocol. Must be |
| 270 // called when |job| has succeeded and the other job will be orphaned. | 272 // called when |job| has succeeded and the other job will be orphaned. |
| 271 void ReportAlternateProtocolUsage(Job* job) const; | 273 void ReportAlternateProtocolUsage(Job* job) const; |
| 272 | 274 |
| 273 // Starts the |alternative_job_|. | |
| 274 void StartAlternativeProxyServerJob(); | |
| 275 | |
| 276 // Returns whether |job| is an orphaned job. | 275 // Returns whether |job| is an orphaned job. |
| 277 bool IsJobOrphaned(Job* job) const; | 276 bool IsJobOrphaned(Job* job) const; |
| 278 | 277 |
| 278 // Called when a Job encountered a network error that could be resolved by |
| 279 // trying a new proxy configuration. If there is another proxy configuration |
| 280 // to try then this method sets |next_state_| appropriately and returns either |
| 281 // OK or ERR_IO_PENDING depending on whether or not the new proxy |
| 282 // configuration is available synchronously or asynchronously. Otherwise, the |
| 283 // given error code is simply returned. |
| 284 int ReconsiderProxyAfterError(Job* job, int error); |
| 285 |
| 279 HttpStreamFactoryImpl* factory_; | 286 HttpStreamFactoryImpl* factory_; |
| 280 HttpNetworkSession* session_; | 287 HttpNetworkSession* session_; |
| 281 JobFactory* job_factory_; | 288 JobFactory* job_factory_; |
| 282 | 289 |
| 283 // Request will be handed out to factory once created. This just keeps an | 290 // Request will be handed out to factory once created. This just keeps an |
| 284 // reference and is safe as |request_| will notify |this| JobController | 291 // reference and is safe as |request_| will notify |this| JobController |
| 285 // when it's destructed by calling OnRequestComplete(), which nulls | 292 // when it's destructed by calling OnRequestComplete(), which nulls |
| 286 // |request_|. | 293 // |request_|. |
| 287 Request* request_; | 294 Request* request_; |
| 288 | 295 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 299 const bool enable_alternative_services_; | 306 const bool enable_alternative_services_; |
| 300 | 307 |
| 301 // |main_job_| is a job waiting to see if |alternative_job_| can reuse a | 308 // |main_job_| is a job waiting to see if |alternative_job_| can reuse a |
| 302 // connection. If |alternative_job_| is unable to do so, |this| will notify | 309 // connection. If |alternative_job_| is unable to do so, |this| will notify |
| 303 // |main_job_| to proceed and then race the two jobs. | 310 // |main_job_| to proceed and then race the two jobs. |
| 304 std::unique_ptr<Job> main_job_; | 311 std::unique_ptr<Job> main_job_; |
| 305 std::unique_ptr<Job> alternative_job_; | 312 std::unique_ptr<Job> alternative_job_; |
| 306 | 313 |
| 307 // Net error code of the failed alternative job. Set to OK by default. | 314 // Net error code of the failed alternative job. Set to OK by default. |
| 308 int alternative_job_net_error_; | 315 int alternative_job_net_error_; |
| 309 | 316 // The alternative service server that |alternative_job_| uses failed. |
| 310 // Either and only one of these records failed alternative service/proxy | |
| 311 // server that |alternative_job_| uses. | |
| 312 AlternativeService failed_alternative_service_; | 317 AlternativeService failed_alternative_service_; |
| 313 ProxyServer failed_alternative_proxy_server_; | |
| 314 | 318 |
| 315 // True if a Job has ever been bound to the |request_|. | 319 // True if a Job has ever been bound to the |request_|. |
| 316 bool job_bound_; | 320 bool job_bound_; |
| 317 | 321 |
| 318 // True if the main job has to wait for the alternative job: i.e., the main | 322 // True if the main job has to wait for the alternative job: i.e., the main |
| 319 // job must not create a connection until it is resumed. | 323 // job must not create a connection until it is resumed. |
| 320 bool main_job_is_blocked_; | 324 bool main_job_is_blocked_; |
| 321 | 325 |
| 322 // True if the main job was blocked and has been resumed in ResumeMainJob(). | 326 // True if the main job was blocked and has been resumed in ResumeMainJob(). |
| 323 bool main_job_is_resumed_; | 327 bool main_job_is_resumed_; |
| 324 | 328 |
| 325 // Waiting time for the main job before it is resumed. | 329 // Waiting time for the main job before it is resumed. |
| 326 base::TimeDelta main_job_wait_time_; | 330 base::TimeDelta main_job_wait_time_; |
| 327 | 331 |
| 328 // At the point where a Job is irrevocably tied to |request_|, we set this. | 332 // At the point where a Job is irrevocably tied to |request_|, we set this. |
| 329 // It will be nulled when the |request_| is finished. | 333 // It will be nulled when the |request_| is finished. |
| 330 Job* bound_job_; | 334 Job* bound_job_; |
| 331 | 335 |
| 332 // True if an alternative proxy server job can be started to fetch |request_|. | 336 // True if an alternative proxy server job can be started to fetch |request_|. |
| 333 bool can_start_alternative_proxy_job_; | 337 bool can_start_alternative_proxy_job_; |
| 334 | 338 |
| 335 // Privacy mode that should be used for fetching the resource. | 339 State next_state_; |
| 336 PrivacyMode privacy_mode_; | 340 ProxyService::PacRequest* pac_request_; |
| 337 | 341 CompletionCallback io_callback_; |
| 342 const HttpRequestInfo request_info_; |
| 343 ProxyInfo proxy_info_; |
| 344 const SSLConfig server_ssl_config_; |
| 345 const SSLConfig proxy_ssl_config_; |
| 346 int num_streams_; |
| 347 HttpStreamRequest::StreamType stream_type_; |
| 348 RequestPriority priority_; |
| 338 const NetLogWithSource net_log_; | 349 const NetLogWithSource net_log_; |
| 339 | 350 |
| 340 base::WeakPtrFactory<JobController> ptr_factory_; | 351 base::WeakPtrFactory<JobController> ptr_factory_; |
| 341 }; | 352 }; |
| 342 | 353 |
| 343 } // namespace net | 354 } // namespace net |
| 344 | 355 |
| 345 #endif // NET_HTTP_HTTP_STREAM_FACTORY_IMPL_JOB_CONTROLLER_H_ | 356 #endif // NET_HTTP_HTTP_STREAM_FACTORY_IMPL_JOB_CONTROLLER_H_ |
| OLD | NEW |