| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "net/base/completion_callback.h" | 14 #include "net/base/completion_callback.h" |
| 15 #include "net/base/net_export.h" |
| 15 #include "net/base/request_priority.h" | 16 #include "net/base/request_priority.h" |
| 17 #include "net/http/bidirectional_stream_impl.h" |
| 16 #include "net/http/http_auth.h" | 18 #include "net/http/http_auth.h" |
| 17 #include "net/http/http_auth_controller.h" | 19 #include "net/http/http_auth_controller.h" |
| 18 #include "net/http/http_request_info.h" | 20 #include "net/http/http_request_info.h" |
| 19 #include "net/http/http_stream_factory_impl.h" | 21 #include "net/http/http_stream_factory_impl.h" |
| 20 #include "net/log/net_log.h" | 22 #include "net/log/net_log.h" |
| 21 #include "net/proxy/proxy_service.h" | 23 #include "net/proxy/proxy_service.h" |
| 22 #include "net/quic/quic_stream_factory.h" | 24 #include "net/quic/quic_stream_factory.h" |
| 23 #include "net/socket/client_socket_handle.h" | 25 #include "net/socket/client_socket_handle.h" |
| 24 #include "net/socket/client_socket_pool_manager.h" | 26 #include "net/socket/client_socket_pool_manager.h" |
| 25 #include "net/socket/ssl_client_socket.h" | 27 #include "net/socket/ssl_client_socket.h" |
| 26 #include "net/spdy/spdy_session_key.h" | 28 #include "net/spdy/spdy_session_key.h" |
| 27 #include "net/ssl/ssl_config_service.h" | 29 #include "net/ssl/ssl_config_service.h" |
| 28 | 30 |
| 29 namespace net { | 31 namespace net { |
| 30 | 32 |
| 31 class BidirectionalStreamImpl; | |
| 32 class ClientSocketHandle; | 33 class ClientSocketHandle; |
| 33 class HttpAuthController; | 34 class HttpAuthController; |
| 34 class HttpNetworkSession; | 35 class HttpNetworkSession; |
| 35 class HttpStream; | 36 class HttpStream; |
| 36 class SpdySessionPool; | 37 class SpdySessionPool; |
| 37 class QuicHttpStream; | 38 class QuicHttpStream; |
| 38 | 39 |
| 39 // An HttpStreamRequestImpl exists for each stream which is in progress of being | 40 // An HttpStreamRequestImpl exists for each stream which is in progress of being |
| 40 // created for the StreamFactory. | 41 // created for the StreamFactory. |
| 41 class HttpStreamFactoryImpl::Job { | 42 class HttpStreamFactoryImpl::Job { |
| 42 public: | 43 public: |
| 44 // Delegate to report Job's status to Request and HttpStreamFactory. |
| 45 class NET_EXPORT_PRIVATE Delegate { |
| 46 public: |
| 47 virtual ~Delegate() {} |
| 48 |
| 49 // Invoked when |job| has an HttpStream ready. |
| 50 virtual void OnStreamReady(Job* job, |
| 51 const SSLConfig& used_ssl_config, |
| 52 const ProxyInfo& used_proxy_info) = 0; |
| 53 |
| 54 // Invoked when |job| has a BidirectionalStream ready. |
| 55 virtual void OnBidirectionalStreamImplReady( |
| 56 Job* job, |
| 57 const SSLConfig& used_ssl_config, |
| 58 const ProxyInfo& used_proxy_info) = 0; |
| 59 |
| 60 // Invoked when |job| has a WebSocketHandshakeStream ready. |
| 61 virtual void OnWebSocketHandshakeStreamReady( |
| 62 Job* job, |
| 63 const SSLConfig& used_ssl_config, |
| 64 const ProxyInfo& used_proxy_info, |
| 65 WebSocketHandshakeStreamBase* stream) = 0; |
| 66 |
| 67 // Invoked when |job| fails to create a stream. |
| 68 virtual void OnStreamFailed(Job* job, |
| 69 int status, |
| 70 const SSLConfig& used_ssl_config, |
| 71 SSLFailureState ssl_failure_state) = 0; |
| 72 |
| 73 // Invoked when |job| has a certificate error for the Request. |
| 74 virtual void OnCertificateError(Job* job, |
| 75 int status, |
| 76 const SSLConfig& used_ssl_config, |
| 77 const SSLInfo& ssl_info) = 0; |
| 78 |
| 79 // Invoked when |job| has a failure of the CONNECT request through an HTTPS |
| 80 // proxy. |
| 81 virtual void OnHttpsProxyTunnelResponse( |
| 82 Job* job, |
| 83 const HttpResponseInfo& response_info, |
| 84 const SSLConfig& used_ssl_config, |
| 85 const ProxyInfo& used_proxy_info, |
| 86 HttpStream* stream) = 0; |
| 87 |
| 88 // Invoked when |job| raises failure for SSL Client Auth. |
| 89 virtual void OnNeedsClientAuth(Job* job, |
| 90 const SSLConfig& used_ssl_config, |
| 91 SSLCertRequestInfo* cert_info) = 0; |
| 92 |
| 93 // Invoked when |job| needs proxy authentication. |
| 94 virtual void OnNeedsProxyAuth(Job* job, |
| 95 const HttpResponseInfo& proxy_response, |
| 96 const SSLConfig& used_ssl_config, |
| 97 const ProxyInfo& used_proxy_info, |
| 98 HttpAuthController* auth_controller) = 0; |
| 99 |
| 100 // Invoked to notify the Request and Factory of the readiness of new |
| 101 // SPDY session. |
| 102 virtual void OnNewSpdySessionReady( |
| 103 Job* job, |
| 104 const base::WeakPtr<SpdySession>& spdy_session, |
| 105 bool direct) = 0; |
| 106 |
| 107 // Invoked when the orphaned |job| finishes. |
| 108 virtual void OnOrphanedJobComplete(const Job* job) = 0; |
| 109 |
| 110 // Invoked when the |job| finishes pre-connecting sockets. |
| 111 virtual void OnPreconnectsComplete(Job* job) = 0; |
| 112 |
| 113 // Invoked to record connection attempts made by the socket layer to |
| 114 // Request if |job| is associated with Request. |
| 115 virtual void AddConnectionAttemptsToRequest( |
| 116 Job* job, |
| 117 const ConnectionAttempts& attempts) = 0; |
| 118 |
| 119 // Called when |job| determines the appropriate |spdy_session_key| for the |
| 120 // Request. Note that this does not mean that SPDY is necessarily supported |
| 121 // for this SpdySessionKey, since we may need to wait for NPN to complete |
| 122 // before knowing if SPDY is available. |
| 123 virtual void SetSpdySessionKey(Job* job, |
| 124 const SpdySessionKey& spdy_session_key) = 0; |
| 125 |
| 126 // Remove session from the SpdySessionRequestMap. |
| 127 virtual void RemoveRequestFromSpdySessionRequestMapForJob(Job* job) = 0; |
| 128 |
| 129 virtual const BoundNetLog* GetNetLog(Job* job) const = 0; |
| 130 |
| 131 virtual WebSocketHandshakeStreamBase::CreateHelper* |
| 132 websocket_handshake_stream_create_helper() = 0; |
| 133 |
| 134 virtual bool for_websockets() = 0; |
| 135 }; |
| 136 |
| 43 // Constructor for non-alternative Job. | 137 // Constructor for non-alternative Job. |
| 44 Job(HttpStreamFactoryImpl* stream_factory, | 138 // Job is owned by |delegate|, hence |delegate| is valid for the |
| 139 // lifetime of the Job. |
| 140 Job(Delegate* delegate, |
| 141 JobType job_type, |
| 45 HttpNetworkSession* session, | 142 HttpNetworkSession* session, |
| 46 const HttpRequestInfo& request_info, | 143 const HttpRequestInfo& request_info, |
| 47 RequestPriority priority, | 144 RequestPriority priority, |
| 48 const SSLConfig& server_ssl_config, | 145 const SSLConfig& server_ssl_config, |
| 49 const SSLConfig& proxy_ssl_config, | 146 const SSLConfig& proxy_ssl_config, |
| 50 HostPortPair destination, | 147 HostPortPair destination, |
| 51 GURL origin_url, | 148 GURL origin_url, |
| 52 NetLog* net_log); | 149 NetLog* net_log); |
| 150 |
| 53 // Constructor for alternative Job. | 151 // Constructor for alternative Job. |
| 54 Job(HttpStreamFactoryImpl* stream_factory, | 152 // Job is owned by |delegate|, hence |delegate| is valid for the |
| 153 // lifetime of the Job. |
| 154 Job(Delegate* delegate, |
| 155 JobType job_type, |
| 55 HttpNetworkSession* session, | 156 HttpNetworkSession* session, |
| 56 const HttpRequestInfo& request_info, | 157 const HttpRequestInfo& request_info, |
| 57 RequestPriority priority, | 158 RequestPriority priority, |
| 58 const SSLConfig& server_ssl_config, | 159 const SSLConfig& server_ssl_config, |
| 59 const SSLConfig& proxy_ssl_config, | 160 const SSLConfig& proxy_ssl_config, |
| 60 HostPortPair destination, | 161 HostPortPair destination, |
| 61 GURL origin_url, | 162 GURL origin_url, |
| 62 AlternativeService alternative_service, | 163 AlternativeService alternative_service, |
| 63 NetLog* net_log); | 164 NetLog* net_log); |
| 64 ~Job(); | 165 virtual ~Job(); |
| 65 | 166 |
| 66 // Start initiates the process of creating a new HttpStream. |request| will be | 167 // Start initiates the process of creating a new HttpStream. |
| 67 // notified upon completion if the Job has not been Orphan()'d. | 168 // |delegate_| will be notified upon completion. |
| 68 void Start(Request* request); | 169 virtual void Start(HttpStreamRequest::StreamType stream_type); |
| 69 | 170 |
| 70 // Preconnect will attempt to request |num_streams| sockets from the | 171 // Preconnect will attempt to request |num_streams| sockets from the |
| 71 // appropriate ClientSocketPool. | 172 // appropriate ClientSocketPool. |
| 72 int Preconnect(int num_streams); | 173 int Preconnect(int num_streams); |
| 73 | 174 |
| 74 int RestartTunnelWithProxyAuth(const AuthCredentials& credentials); | 175 int RestartTunnelWithProxyAuth(const AuthCredentials& credentials); |
| 75 LoadState GetLoadState() const; | 176 LoadState GetLoadState() const; |
| 76 | 177 |
| 77 // Tells |this| to wait for |job| to resume it. | 178 // Tells |this| to wait for |job| to resume it. |
| 78 void WaitFor(Job* job); | 179 void WaitFor(Job* job); |
| 79 | 180 |
| 80 // Tells |this| that |job| has determined it still needs to continue | 181 // Tells |this| that |job| has determined it still needs to continue |
| 81 // connecting, so allow |this| to continue after the specified |delay|. If | 182 // connecting, so allow |this| to continue after the specified |delay|. If |
| 82 // this is not called, then |request_| is expected to cancel |this| by | 183 // this is not called, then |request_| is expected to cancel |this| by |
| 83 // deleting it. | 184 // deleting it. |
| 84 void Resume(Job* job, const base::TimeDelta& delay); | 185 void Resume(Job* job, const base::TimeDelta& delay); |
| 85 | 186 |
| 86 // Used to detach the Job from |request|. | 187 // Called to detach |this| Job. May resume the other Job, will disconnect |
| 87 void Orphan(const Request* request); | 188 // the socket for |this| Job, and notify |delegate| upon completion. |
| 189 void Orphan(); |
| 88 | 190 |
| 89 void SetPriority(RequestPriority priority); | 191 void SetPriority(RequestPriority priority); |
| 90 | 192 |
| 91 RequestPriority priority() const { return priority_; } | 193 RequestPriority priority() const { return priority_; } |
| 92 bool was_npn_negotiated() const; | 194 bool was_npn_negotiated() const; |
| 93 NextProto protocol_negotiated() const; | 195 NextProto protocol_negotiated() const; |
| 94 bool using_spdy() const; | 196 bool using_spdy() const; |
| 95 const BoundNetLog& net_log() const { return net_log_; } | 197 const BoundNetLog& net_log() const { return net_log_; } |
| 96 HttpStreamRequest::StreamType stream_type() const { return stream_type_; } | 198 HttpStreamRequest::StreamType stream_type() const { return stream_type_; } |
| 97 | 199 |
| 200 std::unique_ptr<HttpStream> ReleaseStream() { return std::move(stream_); } |
| 201 |
| 202 void SetStream(HttpStream* http_stream) { stream_.reset(http_stream); } |
| 203 |
| 204 std::unique_ptr<BidirectionalStreamImpl> ReleaseBidirectionalStream() { |
| 205 return std::move(bidirectional_stream_impl_); |
| 206 } |
| 207 |
| 98 const SSLConfig& server_ssl_config() const; | 208 const SSLConfig& server_ssl_config() const; |
| 99 const SSLConfig& proxy_ssl_config() const; | 209 const SSLConfig& proxy_ssl_config() const; |
| 100 const ProxyInfo& proxy_info() const; | 210 const ProxyInfo& proxy_info() const; |
| 101 | 211 |
| 102 // Indicates whether or not this job is performing a preconnect. | |
| 103 bool IsPreconnecting() const; | |
| 104 | |
| 105 // Indicates whether or not this Job has been orphaned by a Request. | |
| 106 bool IsOrphaned() const; | |
| 107 | |
| 108 // Called to indicate that this job succeeded, and some other jobs | 212 // Called to indicate that this job succeeded, and some other jobs |
| 109 // will be orphaned. | 213 // will be orphaned. |
| 110 void ReportJobSucceededForRequest(); | 214 void ReportJobSucceededForRequest(); |
| 111 | 215 |
| 112 // Marks that the other |job| has completed. | 216 // Marks that the other |job| has completed. |
| 113 void MarkOtherJobComplete(const Job& job); | 217 virtual void MarkOtherJobComplete(const Job& job); |
| 218 |
| 219 JobType job_type() const { return job_type_; } |
| 114 | 220 |
| 115 private: | 221 private: |
| 116 FRIEND_TEST_ALL_PREFIXES(HttpStreamFactoryImplRequestTest, DelayMainJob); | 222 FRIEND_TEST_ALL_PREFIXES(HttpStreamFactoryImplRequestTest, DelayMainJob); |
| 117 | 223 |
| 118 enum State { | 224 enum State { |
| 119 STATE_START, | 225 STATE_START, |
| 120 STATE_RESOLVE_PROXY, | 226 STATE_RESOLVE_PROXY, |
| 121 STATE_RESOLVE_PROXY_COMPLETE, | 227 STATE_RESOLVE_PROXY_COMPLETE, |
| 122 | 228 |
| 123 // Note that when Alternate-Protocol says we can connect to an alternate | 229 // Note that when Alternate-Protocol says we can connect to an alternate |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 // Invoked by the transport socket pool after host resolution is complete | 417 // Invoked by the transport socket pool after host resolution is complete |
| 312 // to allow the connection to be aborted, if a matching SPDY session can | 418 // to allow the connection to be aborted, if a matching SPDY session can |
| 313 // be found. Will return ERR_SPDY_SESSION_ALREADY_EXISTS if such a | 419 // be found. Will return ERR_SPDY_SESSION_ALREADY_EXISTS if such a |
| 314 // session is found, and OK otherwise. | 420 // session is found, and OK otherwise. |
| 315 static int OnHostResolution(SpdySessionPool* spdy_session_pool, | 421 static int OnHostResolution(SpdySessionPool* spdy_session_pool, |
| 316 const SpdySessionKey& spdy_session_key, | 422 const SpdySessionKey& spdy_session_key, |
| 317 const GURL& origin_url, | 423 const GURL& origin_url, |
| 318 const AddressList& addresses, | 424 const AddressList& addresses, |
| 319 const BoundNetLog& net_log); | 425 const BoundNetLog& net_log); |
| 320 | 426 |
| 321 Request* request_; | |
| 322 | |
| 323 const HttpRequestInfo request_info_; | 427 const HttpRequestInfo request_info_; |
| 324 RequestPriority priority_; | 428 RequestPriority priority_; |
| 325 ProxyInfo proxy_info_; | 429 ProxyInfo proxy_info_; |
| 326 SSLConfig server_ssl_config_; | 430 SSLConfig server_ssl_config_; |
| 327 SSLConfig proxy_ssl_config_; | 431 SSLConfig proxy_ssl_config_; |
| 328 const BoundNetLog net_log_; | 432 const BoundNetLog net_log_; |
| 329 | 433 |
| 330 CompletionCallback io_callback_; | 434 CompletionCallback io_callback_; |
| 331 std::unique_ptr<ClientSocketHandle> connection_; | 435 std::unique_ptr<ClientSocketHandle> connection_; |
| 332 HttpNetworkSession* const session_; | 436 HttpNetworkSession* const session_; |
| 333 HttpStreamFactoryImpl* const stream_factory_; | |
| 334 State next_state_; | 437 State next_state_; |
| 335 ProxyService::PacRequest* pac_request_; | 438 ProxyService::PacRequest* pac_request_; |
| 336 SSLInfo ssl_info_; | 439 SSLInfo ssl_info_; |
| 337 | 440 |
| 338 // The server we are trying to reach, could be that of the origin or of the | 441 // The server we are trying to reach, could be that of the origin or of the |
| 339 // alternative service (after applying host mapping rules). | 442 // alternative service (after applying host mapping rules). |
| 340 HostPortPair destination_; | 443 HostPortPair destination_; |
| 341 | 444 |
| 342 // The origin url we're trying to reach. This url may be different from the | 445 // The origin url we're trying to reach. This url may be different from the |
| 343 // original request when host mapping rules are set-up. | 446 // original request when host mapping rules are set-up. |
| 344 GURL origin_url_; | 447 GURL origin_url_; |
| 345 | 448 |
| 346 // AlternativeService for this Job if this is an alternative Job. | 449 // AlternativeService for this Job if this is an alternative Job. |
| 347 const AlternativeService alternative_service_; | 450 const AlternativeService alternative_service_; |
| 348 | 451 |
| 349 // AlternativeService for the other Job if this is not an alternative Job. | 452 // AlternativeService for the other Job if this is not an alternative Job. |
| 350 AlternativeService other_job_alternative_service_; | 453 AlternativeService other_job_alternative_service_; |
| 351 | 454 |
| 455 // Unowned. |this| job is owned by |delegate_|. |
| 456 Delegate* delegate_; |
| 457 |
| 458 JobType job_type_; |
| 459 |
| 352 // This is the Job we're dependent on. It will notify us if/when it's OK to | 460 // This is the Job we're dependent on. It will notify us if/when it's OK to |
| 353 // proceed. | 461 // proceed. |
| 354 Job* blocking_job_; | 462 Job* blocking_job_; |
| 355 | 463 |
| 356 // |waiting_job_| is a Job waiting to see if |this| can reuse a connection. | 464 // |waiting_job_| is a Job waiting to see if |this| can reuse a connection. |
| 357 // If |this| is unable to do so, we'll notify |waiting_job_| that it's ok to | 465 // If |this| is unable to do so, we'll notify |waiting_job_| that it's ok to |
| 358 // proceed and then race the two Jobs. | 466 // proceed and then race the two Jobs. |
| 359 Job* waiting_job_; | 467 Job* waiting_job_; |
| 360 | 468 |
| 361 base::TimeDelta wait_time_; | 469 base::TimeDelta wait_time_; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 base::TimeTicks job_stream_ready_start_time_; | 524 base::TimeTicks job_stream_ready_start_time_; |
| 417 | 525 |
| 418 // Type of stream that is requested. | 526 // Type of stream that is requested. |
| 419 HttpStreamRequest::StreamType stream_type_; | 527 HttpStreamRequest::StreamType stream_type_; |
| 420 | 528 |
| 421 base::WeakPtrFactory<Job> ptr_factory_; | 529 base::WeakPtrFactory<Job> ptr_factory_; |
| 422 | 530 |
| 423 DISALLOW_COPY_AND_ASSIGN(Job); | 531 DISALLOW_COPY_AND_ASSIGN(Job); |
| 424 }; | 532 }; |
| 425 | 533 |
| 534 // Factory for creating Jobs. |
| 535 class HttpStreamFactoryImpl::JobFactory { |
| 536 public: |
| 537 virtual ~JobFactory() {} |
| 538 |
| 539 // Creates an alternative Job. |
| 540 virtual HttpStreamFactoryImpl::Job* CreateJob( |
| 541 HttpStreamFactoryImpl::Job::Delegate* delegate, |
| 542 HttpStreamFactoryImpl::JobType job_type, |
| 543 HttpNetworkSession* session, |
| 544 const HttpRequestInfo& request_info, |
| 545 RequestPriority priority, |
| 546 const SSLConfig& server_ssl_config, |
| 547 const SSLConfig& proxy_ssl_config, |
| 548 HostPortPair destination, |
| 549 GURL origin_url, |
| 550 AlternativeService alternative_service, |
| 551 NetLog* net_log) = 0; |
| 552 |
| 553 // Creates a non-alternative Job. |
| 554 virtual HttpStreamFactoryImpl::Job* CreateJob( |
| 555 HttpStreamFactoryImpl::Job::Delegate* delegate, |
| 556 HttpStreamFactoryImpl::JobType job_type, |
| 557 HttpNetworkSession* session, |
| 558 const HttpRequestInfo& request_info, |
| 559 RequestPriority priority, |
| 560 const SSLConfig& server_ssl_config, |
| 561 const SSLConfig& proxy_ssl_config, |
| 562 HostPortPair destination, |
| 563 GURL origin_url, |
| 564 NetLog* net_log) = 0; |
| 565 }; |
| 566 |
| 426 } // namespace net | 567 } // namespace net |
| 427 | 568 |
| 428 #endif // NET_HTTP_HTTP_STREAM_FACTORY_IMPL_JOB_H_ | 569 #endif // NET_HTTP_HTTP_STREAM_FACTORY_IMPL_JOB_H_ |
| OLD | NEW |