spdy_session_pool.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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_SPDY_SPDY_SESSION_POOL_H_ 6#define NET_SPDY_SPDY_SESSION_POOL_H_ 7 8#include <map> 9#include <set> 10#include <string> 11#include <vector> 12 13#include "base/basictypes.h" 14#include "base/gtest_prod_util.h" 15#include "base/memory/ref_counted.h" 16#include "base/memory/weak_ptr.h" 17#include "net/base/host_port_pair.h" 18#include "net/base/ip_endpoint.h" 19#include "net/base/net_errors.h" 20#include "net/base/net_export.h" 21#include "net/base/network_change_notifier.h" 22#include "net/cert/cert_database.h" 23#include "net/proxy/proxy_config.h" 24#include "net/proxy/proxy_server.h" 25#include "net/socket/next_proto.h" 26#include "net/spdy/spdy_session_key.h" 27#include "net/ssl/ssl_config_service.h" 28 29namespace net { 30 31class AddressList; 32class BoundNetLog; 33class ClientSocketHandle; 34class HostResolver; 35class HttpServerProperties; 36class SpdySession; 37 38// This is a very simple pool for open SpdySessions. 39class NET_EXPORT SpdySessionPool 40 : public NetworkChangeNotifier::IPAddressObserver, 41 public SSLConfigService::Observer, 42 public CertDatabase::Observer { 43 public: 44 typedef base::TimeTicks (*TimeFunc)(void); 45 46 // |default_protocol| may be kProtoUnknown (e.g., if SPDY is 47 // disabled), in which case it's set to a default value. Otherwise, 48 // it must be a SPDY protocol. 49 SpdySessionPool( 50 HostResolver* host_resolver, 51 SSLConfigService* ssl_config_service, 52 const base::WeakPtr<HttpServerProperties>& http_server_properties, 53 bool force_single_domain, 54 bool enable_compression, 55 bool enable_ping_based_connection_checking, 56 NextProto default_protocol, 57 size_t stream_initial_recv_window_size, 58 size_t initial_max_concurrent_streams, 59 size_t max_concurrent_streams_limit, 60 SpdySessionPool::TimeFunc time_func, 61 const std::string& trusted_spdy_proxy); 62 virtual ~SpdySessionPool(); 63 64 // In the functions below, a session is "available" if this pool has 65 // a reference to it and there is some SpdySessionKey for which 66 // FindAvailableSession() will return it. A session is "unavailable" 67 // if this pool has a reference to it but it won't be returned by 68 // FindAvailableSession() for any SpdySessionKey; for example, this 69 // can happen when a session receives a GOAWAY frame and is still 70 // processing existing streams. 71 72 // Create a new SPDY session from an existing socket. There must 73 // not already be a session for the given key. This pool must have 74 // been constructed with a valid |default_protocol| value. 75 // 76 // |is_secure| can be false for testing or when SPDY is configured 77 // to work with non-secure sockets. If |is_secure| is true, 78 // |certificate_error_code| indicates that the certificate error 79 // encountered when connecting the SSL socket, with OK meaning there 80 // was no error. 81 // 82 // If successful, OK is returned and |available_session| will be 83 // non-NULL and available. Otherwise, an error is returned and 84 // |available_session| will be NULL. 85 net::Error CreateAvailableSessionFromSocket( 86 const SpdySessionKey& key, 87 scoped_ptr<ClientSocketHandle> connection, 88 const BoundNetLog& net_log, 89 int certificate_error_code, 90 base::WeakPtr<SpdySession>* available_session, 91 bool is_secure); 92 93 // Find an available session for the given key, or NULL if there isn't one. 94 base::WeakPtr<SpdySession> FindAvailableSession(const SpdySessionKey& key, 95 const BoundNetLog& net_log); 96 97 // Remove all mappings and aliases for the given session, which must 98 // still be available. Except for in tests, this must be called by 99 // the given session itself. 100 void MakeSessionUnavailable( 101 const base::WeakPtr<SpdySession>& available_session); 102 103 // Removes an unavailable session from the pool. Except for in 104 // tests, this must be called by the given session itself. 105 void RemoveUnavailableSession( 106 const base::WeakPtr<SpdySession>& unavailable_session); 107 108 // Close only the currently existing SpdySessions with |error|. 109 // Let any new ones created while this method is running continue to 110 // live. 111 void CloseCurrentSessions(net::Error error); 112 113 // Close only the currently existing SpdySessions that are idle. 114 // Let any new ones created while this method is running continue to 115 // live. 116 void CloseCurrentIdleSessions(); 117 118 // Close all SpdySessions, including any new ones created in the process of 119 // closing the current ones. 120 void CloseAllSessions(); 121 122 // Creates a Value summary of the state of the spdy session pool. The caller 123 // responsible for deleting the returned value. 124 base::Value* SpdySessionPoolInfoToValue() const; 125 126 base::WeakPtr<HttpServerProperties> http_server_properties() { 127 return http_server_properties_; 128 } 129 130 // NetworkChangeNotifier::IPAddressObserver methods: 131 132 // We flush all idle sessions and release references to the active ones so 133 // they won't get re-used. The active ones will either complete successfully 134 // or error out due to the IP address change. 135 virtual void OnIPAddressChanged() OVERRIDE; 136 137 // SSLConfigService::Observer methods: 138 139 // We perform the same flushing as described above when SSL settings change. 140 virtual void OnSSLConfigChanged() OVERRIDE; 141 142 // CertDatabase::Observer methods: 143 144 // We perform the same flushing as described above when certificate database 145 // is changed. 146 virtual void OnCertAdded(const X509Certificate* cert) OVERRIDE; 147 virtual void OnCACertChanged(const X509Certificate* cert) OVERRIDE; 148 149 private: 150 friend class SpdySessionPoolPeer; // For testing. 151 152 typedef std::set<SpdySession*> SessionSet; 153 typedef std::vector<base::WeakPtr<SpdySession> > WeakSessionList; 154 typedef std::map<SpdySessionKey, base::WeakPtr<SpdySession> > 155 AvailableSessionMap; 156 typedef std::map<IPEndPoint, SpdySessionKey> AliasMap; 157 158 // Returns true iff |session| is in |available_sessions_|. 159 bool IsSessionAvailable(const base::WeakPtr<SpdySession>& session) const; 160 161 // Returns a normalized version of the given key suitable for lookup 162 // into |available_sessions_|. 163 const SpdySessionKey& NormalizeListKey(const SpdySessionKey& key) const; 164 165 // Map the given key to the given session. There must not already be 166 // a mapping for |key|. 167 void MapKeyToAvailableSession(const SpdySessionKey& key, 168 const base::WeakPtr<SpdySession>& session); 169 170 // Returns an iterator into |available_sessions_| for the given key, 171 // which may be equal to |available_sessions_.end()|. 172 AvailableSessionMap::iterator LookupAvailableSessionByKey( 173 const SpdySessionKey& key); 174 175 // Remove the mapping of the given key, which must exist. 176 void UnmapKey(const SpdySessionKey& key); 177 178 // Remove all aliases for |key| from the aliases table. 179 void RemoveAliases(const SpdySessionKey& key); 180 181 // Get a copy of the current sessions as a list of WeakPtrs. Used by 182 // CloseCurrentSessionsHelper() below. 183 WeakSessionList GetCurrentSessions() const; 184 185 // Close only the currently existing SpdySessions with |error|. Let 186 // any new ones created while this method is running continue to 187 // live. If |idle_only| is true only idle sessions are closed. 188 void CloseCurrentSessionsHelper( 189 Error error, 190 const std::string& description, 191 bool idle_only); 192 193 const base::WeakPtr<HttpServerProperties> http_server_properties_; 194 195 // The set of all sessions. This is a superset of the sessions in 196 // |available_sessions_|. 197 // 198 // |sessions_| owns all its SpdySession objects. 199 SessionSet sessions_; 200 201 // This is a map of available sessions by key. A session may appear 202 // more than once in this map if it has aliases. 203 AvailableSessionMap available_sessions_; 204 205 // A map of IPEndPoint aliases for sessions. 206 AliasMap aliases_; 207 208 static bool g_force_single_domain; 209 210 const scoped_refptr<SSLConfigService> ssl_config_service_; 211 HostResolver* const resolver_; 212 213 // Defaults to true. May be controlled via SpdySessionPoolPeer for tests. 214 bool verify_domain_authentication_; 215 bool enable_sending_initial_data_; 216 bool force_single_domain_; 217 bool enable_compression_; 218 bool enable_ping_based_connection_checking_; 219 const NextProto default_protocol_; 220 size_t stream_initial_recv_window_size_; 221 size_t initial_max_concurrent_streams_; 222 size_t max_concurrent_streams_limit_; 223 TimeFunc time_func_; 224 225 // This SPDY proxy is allowed to push resources from origins that are 226 // different from those of their associated streams. 227 HostPortPair trusted_spdy_proxy_; 228 229 DISALLOW_COPY_AND_ASSIGN(SpdySessionPool); 230}; 231 232} // namespace net 233 234#endif // NET_SPDY_SPDY_SESSION_POOL_H_ 235