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 3 // found in the LICENSE file. 4 5 #ifndef NET_HTTP_HTTP_STREAM_FACTORY_IMPL_H_ 6 #define NET_HTTP_HTTP_STREAM_FACTORY_IMPL_H_ 7 8 #include <map> 9 #include <set> 10 #include <vector> 11 12 #include "base/gtest_prod_util.h" 13 #include "base/memory/ref_counted.h" 14 #include "net/base/host_port_pair.h" 15 #include "net/base/net_log.h" 16 #include "net/http/http_pipelined_host_pool.h" 17 #include "net/http/http_stream_factory.h" 18 #include "net/proxy/proxy_server.h" 19 #include "net/socket/ssl_client_socket.h" 20 #include "net/spdy/spdy_session_key.h" 21 22 namespace net { 23 24 class HttpNetworkSession; 25 class HttpPipelinedHost; 26 class SpdySession; 27 28 class NET_EXPORT_PRIVATE HttpStreamFactoryImpl : 29 public HttpStreamFactory, 30 public HttpPipelinedHostPool::Delegate { 31 public: 32 // RequestStream may only be called if |for_websockets| is false. 33 // RequestWebSocketHandshakeStream may only be called if |for_websockets| 34 // is true. 35 HttpStreamFactoryImpl(HttpNetworkSession* session, bool for_websockets); 36 virtual ~HttpStreamFactoryImpl(); 37 38 // HttpStreamFactory interface 39 virtual HttpStreamRequest* RequestStream( 40 const HttpRequestInfo& info, 41 RequestPriority priority, 42 const SSLConfig& server_ssl_config, 43 const SSLConfig& proxy_ssl_config, 44 HttpStreamRequest::Delegate* delegate, 45 const BoundNetLog& net_log) OVERRIDE; 46 47 virtual HttpStreamRequest* RequestWebSocketHandshakeStream( 48 const HttpRequestInfo& info, 49 RequestPriority priority, 50 const SSLConfig& server_ssl_config, 51 const SSLConfig& proxy_ssl_config, 52 HttpStreamRequest::Delegate* delegate, 53 WebSocketHandshakeStreamBase::CreateHelper* create_helper, 54 const BoundNetLog& net_log) OVERRIDE; 55 56 virtual void PreconnectStreams(int num_streams, 57 const HttpRequestInfo& info, 58 RequestPriority priority, 59 const SSLConfig& server_ssl_config, 60 const SSLConfig& proxy_ssl_config) OVERRIDE; 61 virtual base::Value* PipelineInfoToValue() const OVERRIDE; 62 virtual const HostMappingRules* GetHostMappingRules() const OVERRIDE; 63 64 // HttpPipelinedHostPool::Delegate interface 65 virtual void OnHttpPipelinedHostHasAdditionalCapacity( 66 HttpPipelinedHost* host) OVERRIDE; 67 68 size_t num_orphaned_jobs() const { return orphaned_job_set_.size(); } 69 70 private: 71 FRIEND_TEST_ALL_PREFIXES(HttpStreamFactoryImplRequestTest, SetPriority); 72 73 class NET_EXPORT_PRIVATE Request; 74 class NET_EXPORT_PRIVATE Job; 75 76 typedef std::set<Request*> RequestSet; 77 typedef std::vector<Request*> RequestVector; 78 typedef std::map<SpdySessionKey, RequestSet> SpdySessionRequestMap; 79 typedef std::map<HttpPipelinedHost::Key, 80 RequestVector> HttpPipeliningRequestMap; 81 82 HttpStreamRequest* RequestStreamInternal( 83 const HttpRequestInfo& info, 84 RequestPriority priority, 85 const SSLConfig& server_ssl_config, 86 const SSLConfig& proxy_ssl_config, 87 HttpStreamRequest::Delegate* delegate, 88 WebSocketHandshakeStreamBase::CreateHelper* create_helper, 89 const BoundNetLog& net_log); 90 91 PortAlternateProtocolPair GetAlternateProtocolRequestFor( 92 const GURL& original_url, 93 GURL* alternate_url) const; 94 95 // Detaches |job| from |request|. 96 void OrphanJob(Job* job, const Request* request); 97 98 // Called when a SpdySession is ready. It will find appropriate Requests and 99 // fulfill them. |direct| indicates whether or not |spdy_session| uses a 100 // proxy. 101 void OnNewSpdySessionReady(const base::WeakPtr<SpdySession>& spdy_session, 102 bool direct, 103 const SSLConfig& used_ssl_config, 104 const ProxyInfo& used_proxy_info, 105 bool was_npn_negotiated, 106 NextProto protocol_negotiated, 107 bool using_spdy, 108 const BoundNetLog& net_log); 109 110 // Called when the Job detects that the endpoint indicated by the 111 // Alternate-Protocol does not work. Lets the factory update 112 // HttpAlternateProtocols with the failure and resets the SPDY session key. 113 void OnBrokenAlternateProtocol(const Job*, const HostPortPair& origin); 114 115 // Invoked when an orphaned Job finishes. 116 void OnOrphanedJobComplete(const Job* job); 117 118 // Invoked when the Job finishes preconnecting sockets. 119 void OnPreconnectsComplete(const Job* job); 120 121 // Called when the Preconnect completes. Used for testing. 122 virtual void OnPreconnectsCompleteInternal() {} 123 124 void AbortPipelinedRequestsWithKey(const Job* job, 125 const HttpPipelinedHost::Key& key, 126 int status, 127 const SSLConfig& used_ssl_config); 128 129 HttpNetworkSession* const session_; 130 131 // All Requests are handed out to clients. By the time HttpStreamFactoryImpl 132 // is destroyed, all Requests should be deleted (which should remove them from 133 // |request_map_|. The Requests will delete the corresponding job. 134 std::map<const Job*, Request*> request_map_; 135 136 SpdySessionRequestMap spdy_session_request_map_; 137 HttpPipeliningRequestMap http_pipelining_request_map_; 138 139 HttpPipelinedHostPool http_pipelined_host_pool_; 140 141 // These jobs correspond to jobs orphaned by Requests and now owned by 142 // HttpStreamFactoryImpl. Since they are no longer tied to Requests, they will 143 // not be canceled when Requests are canceled. Therefore, in 144 // ~HttpStreamFactoryImpl, it is possible for some jobs to still exist in this 145 // set. Leftover jobs will be deleted when the factory is destroyed. 146 std::set<const Job*> orphaned_job_set_; 147 148 // These jobs correspond to preconnect requests and have no associated Request 149 // object. They're owned by HttpStreamFactoryImpl. Leftover jobs will be 150 // deleted when the factory is destroyed. 151 std::set<const Job*> preconnect_job_set_; 152 153 const bool for_websockets_; 154 DISALLOW_COPY_AND_ASSIGN(HttpStreamFactoryImpl); 155 }; 156 157 } // namespace net 158 159 #endif // NET_HTTP_HTTP_STREAM_FACTORY_IMPL_H_ 160