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_QUIC_QUIC_STREAM_FACTORY_H_ 6 #define NET_QUIC_QUIC_STREAM_FACTORY_H_ 7 8 #include <map> 9 #include <string> 10 11 #include "base/memory/weak_ptr.h" 12 #include "net/base/address_list.h" 13 #include "net/base/completion_callback.h" 14 #include "net/base/host_port_pair.h" 15 #include "net/base/net_log.h" 16 #include "net/base/network_change_notifier.h" 17 #include "net/proxy/proxy_server.h" 18 #include "net/quic/quic_config.h" 19 #include "net/quic/quic_crypto_stream.h" 20 #include "net/quic/quic_http_stream.h" 21 #include "net/quic/quic_protocol.h" 22 23 namespace net { 24 25 class CertVerifier; 26 class ClientSocketFactory; 27 class HostResolver; 28 class QuicClock; 29 class QuicClientSession; 30 class QuicCryptoClientStreamFactory; 31 class QuicRandom; 32 class QuicStreamFactory; 33 34 // Encapsulates a pending request for a QuicHttpStream. 35 // If the request is still pending when it is destroyed, it will 36 // cancel the request with the factory. 37 class NET_EXPORT_PRIVATE QuicStreamRequest { 38 public: 39 explicit QuicStreamRequest(QuicStreamFactory* factory); 40 ~QuicStreamRequest(); 41 42 // For http, |is_https| is false and |cert_verifier| can be null. 43 int Request(const HostPortProxyPair& host_port_proxy_pair, 44 bool is_https, 45 CertVerifier* cert_verifier, 46 const BoundNetLog& net_log, 47 const CompletionCallback& callback); 48 49 void OnRequestComplete(int rv); 50 51 scoped_ptr<QuicHttpStream> ReleaseStream(); 52 53 void set_stream(scoped_ptr<QuicHttpStream> stream); 54 55 const BoundNetLog& net_log() const{ 56 return net_log_; 57 } 58 59 private: 60 QuicStreamFactory* factory_; 61 HostPortProxyPair host_port_proxy_pair_; 62 bool is_https_; 63 CertVerifier* cert_verifier_; 64 BoundNetLog net_log_; 65 CompletionCallback callback_; 66 scoped_ptr<QuicHttpStream> stream_; 67 68 DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest); 69 }; 70 71 // A factory for creating new QuicHttpStreams on top of a pool of 72 // QuicClientSessions. 73 class NET_EXPORT_PRIVATE QuicStreamFactory 74 : public NetworkChangeNotifier::IPAddressObserver { 75 public: 76 QuicStreamFactory( 77 HostResolver* host_resolver, 78 ClientSocketFactory* client_socket_factory, 79 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory, 80 QuicRandom* random_generator, 81 QuicClock* clock); 82 virtual ~QuicStreamFactory(); 83 84 // Creates a new QuicHttpStream to |host_port_proxy_pair| which will be 85 // owned by |request|. |is_https| specifies if the protocol is https or not. 86 // |cert_verifier| is used by ProofVerifier for verifying the certificate 87 // chain and signature. For http, this can be null. If a matching session 88 // already exists, this method will return OK. If no matching session exists, 89 // this will return ERR_IO_PENDING and will invoke OnRequestComplete 90 // asynchronously. 91 int Create(const HostPortProxyPair& host_port_proxy_pair, 92 bool is_https, 93 CertVerifier* cert_verifier, 94 const BoundNetLog& net_log, 95 QuicStreamRequest* request); 96 97 // Returns a newly created QuicHttpStream owned by the caller, if a 98 // matching session already exists. Returns NULL otherwise. 99 scoped_ptr<QuicHttpStream> CreateIfSessionExists( 100 const HostPortProxyPair& host_port_proxy_pair, 101 const BoundNetLog& net_log); 102 103 // Called by a session when it becomes idle. 104 void OnIdleSession(QuicClientSession* session); 105 106 // Called by a session after it shuts down. 107 void OnSessionClose(QuicClientSession* session); 108 109 // Cancels a pending request. 110 void CancelRequest(QuicStreamRequest* request); 111 112 // Closes all current sessions. 113 void CloseAllSessions(int error); 114 115 base::Value* QuicStreamFactoryInfoToValue() const; 116 117 // NetworkChangeNotifier::IPAddressObserver methods: 118 119 // Until the servers support roaming, close all connections when the local 120 // IP address changes. 121 virtual void OnIPAddressChanged() OVERRIDE; 122 123 private: 124 class Job; 125 126 typedef std::map<HostPortProxyPair, QuicClientSession*> SessionMap; 127 typedef std::set<HostPortProxyPair> AliasSet; 128 typedef std::map<QuicClientSession*, AliasSet> SessionAliasMap; 129 typedef std::set<QuicClientSession*> SessionSet; 130 typedef std::map<HostPortProxyPair, QuicCryptoClientConfig*> CryptoConfigMap; 131 typedef std::map<HostPortProxyPair, Job*> JobMap; 132 typedef std::map<QuicStreamRequest*, Job*> RequestMap; 133 typedef std::set<QuicStreamRequest*> RequestSet; 134 typedef std::map<Job*, RequestSet> JobRequestsMap; 135 136 void OnJobComplete(Job* job, int rv); 137 bool HasActiveSession(const HostPortProxyPair& host_port_proxy_pair); 138 bool HasActiveJob(const HostPortProxyPair& host_port_proxy_pair); 139 QuicClientSession* CreateSession( 140 const HostPortProxyPair& host_port_proxy_pair, 141 bool is_https, 142 CertVerifier* cert_verifier, 143 const AddressList& address_list, 144 const BoundNetLog& net_log); 145 void ActivateSession(const HostPortProxyPair& host_port_proxy_pair, 146 QuicClientSession* session); 147 148 QuicCryptoClientConfig* GetOrCreateCryptoConfig( 149 const HostPortProxyPair& host_port_proxy_pair); 150 151 HostResolver* host_resolver_; 152 ClientSocketFactory* client_socket_factory_; 153 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_; 154 QuicRandom* random_generator_; 155 scoped_ptr<QuicClock> clock_; 156 157 // Contains owning pointers to all sessions that currently exist. 158 SessionSet all_sessions_; 159 // Contains non-owning pointers to currently active session 160 // (not going away session, once they're implemented). 161 SessionMap active_sessions_; 162 SessionAliasMap session_aliases_; 163 164 // Contains owning pointers to QuicCryptoClientConfig. QuicCryptoClientConfig 165 // contains configuration and cached state about servers. 166 // TODO(rtenneti): Persist all_crypto_configs_ to disk and decide when to 167 // clear the data in the map. 168 CryptoConfigMap all_crypto_configs_; 169 170 QuicConfig config_; 171 172 JobMap active_jobs_; 173 JobRequestsMap job_requests_map_; 174 RequestMap active_requests_; 175 176 base::WeakPtrFactory<QuicStreamFactory> weak_factory_; 177 178 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory); 179 }; 180 181 } // namespace net 182 183 #endif // NET_QUIC_QUIC_STREAM_FACTORY_H_ 184