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.
5 #ifndef NET_SPDY_SPDY_SESSION_POOL_H_
6 #define NET_SPDY_SPDY_SESSION_POOL_H_
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"
33 class ClientSocketHandle
;
35 class HttpServerProperties
;
37 class TransportSecurityState
;
39 // This is a very simple pool for open SpdySessions.
40 class NET_EXPORT SpdySessionPool
41 : public NetworkChangeNotifier::IPAddressObserver
,
42 public SSLConfigService::Observer
,
43 public CertDatabase::Observer
{
45 typedef base::TimeTicks (*TimeFunc
)(void);
47 // |default_protocol| may be kProtoUnknown (e.g., if SPDY is
48 // disabled), in which case it's set to a default value. Otherwise,
49 // it must be a SPDY protocol.
51 HostResolver
* host_resolver
,
52 SSLConfigService
* ssl_config_service
,
53 const base::WeakPtr
<HttpServerProperties
>& http_server_properties
,
54 TransportSecurityState
* transport_security_state
,
55 bool force_single_domain
,
56 bool enable_compression
,
57 bool enable_ping_based_connection_checking
,
58 NextProto default_protocol
,
59 size_t session_max_recv_window_size
,
60 size_t stream_max_recv_window_size
,
61 size_t initial_max_concurrent_streams
,
62 size_t max_concurrent_streams_limit
,
63 SpdySessionPool::TimeFunc time_func
,
64 const std::string
& trusted_spdy_proxy
);
65 ~SpdySessionPool() override
;
67 // In the functions below, a session is "available" if this pool has
68 // a reference to it and there is some SpdySessionKey for which
69 // FindAvailableSession() will return it. A session is "unavailable"
70 // if this pool has a reference to it but it won't be returned by
71 // FindAvailableSession() for any SpdySessionKey; for example, this
72 // can happen when a session receives a GOAWAY frame and is still
73 // processing existing streams.
75 // Create a new SPDY session from an existing socket. There must
76 // not already be a session for the given key. This pool must have
77 // been constructed with a valid |default_protocol| value.
79 // |is_secure| can be false for testing or when SPDY is configured
80 // to work with non-secure sockets. If |is_secure| is true,
81 // |certificate_error_code| indicates that the certificate error
82 // encountered when connecting the SSL socket, with OK meaning there
85 // Returns the new SpdySession. Note that the SpdySession begins reading from
86 // |connection| on a subsequent event loop iteration, so it may be closed
87 // immediately afterwards if the first read of |connection| fails.
88 base::WeakPtr
<SpdySession
> CreateAvailableSessionFromSocket(
89 const SpdySessionKey
& key
,
90 scoped_ptr
<ClientSocketHandle
> connection
,
91 const BoundNetLog
& net_log
,
92 int certificate_error_code
,
95 // Find an available session for the given key, or NULL if there isn't one.
96 base::WeakPtr
<SpdySession
> FindAvailableSession(const SpdySessionKey
& key
,
97 const BoundNetLog
& net_log
);
99 // Remove all mappings and aliases for the given session, which must
100 // still be available. Except for in tests, this must be called by
101 // the given session itself.
102 void MakeSessionUnavailable(
103 const base::WeakPtr
<SpdySession
>& available_session
);
105 // Removes an unavailable session from the pool. Except for in
106 // tests, this must be called by the given session itself.
107 void RemoveUnavailableSession(
108 const base::WeakPtr
<SpdySession
>& unavailable_session
);
110 // Close only the currently existing SpdySessions with |error|.
111 // Let any new ones created while this method is running continue to
113 void CloseCurrentSessions(net::Error error
);
115 // Close only the currently existing SpdySessions that are idle.
116 // Let any new ones created while this method is running continue to
118 void CloseCurrentIdleSessions();
120 // Close all SpdySessions, including any new ones created in the process of
121 // closing the current ones.
122 void CloseAllSessions();
124 // Creates a Value summary of the state of the spdy session pool. The caller
125 // responsible for deleting the returned value.
126 base::Value
* SpdySessionPoolInfoToValue() const;
128 base::WeakPtr
<HttpServerProperties
> http_server_properties() {
129 return http_server_properties_
;
132 // NetworkChangeNotifier::IPAddressObserver methods:
134 // We flush all idle sessions and release references to the active ones so
135 // they won't get re-used. The active ones will either complete successfully
136 // or error out due to the IP address change.
137 void OnIPAddressChanged() override
;
139 // SSLConfigService::Observer methods:
141 // We perform the same flushing as described above when SSL settings change.
142 void OnSSLConfigChanged() override
;
144 // CertDatabase::Observer methods:
146 // We perform the same flushing as described above when certificate database
148 void OnCertAdded(const X509Certificate
* cert
) override
;
149 void OnCACertChanged(const X509Certificate
* cert
) override
;
152 friend class SpdySessionPoolPeer
; // For testing.
154 typedef std::set
<SpdySession
*> SessionSet
;
155 typedef std::vector
<base::WeakPtr
<SpdySession
> > WeakSessionList
;
156 typedef std::map
<SpdySessionKey
, base::WeakPtr
<SpdySession
> >
158 typedef std::map
<IPEndPoint
, SpdySessionKey
> AliasMap
;
160 // Returns true iff |session| is in |available_sessions_|.
161 bool IsSessionAvailable(const base::WeakPtr
<SpdySession
>& session
) const;
163 // Returns a normalized version of the given key suitable for lookup
164 // into |available_sessions_|.
165 const SpdySessionKey
& NormalizeListKey(const SpdySessionKey
& key
) const;
167 // Map the given key to the given session. There must not already be
168 // a mapping for |key|.
169 void MapKeyToAvailableSession(const SpdySessionKey
& key
,
170 const base::WeakPtr
<SpdySession
>& session
);
172 // Returns an iterator into |available_sessions_| for the given key,
173 // which may be equal to |available_sessions_.end()|.
174 AvailableSessionMap::iterator
LookupAvailableSessionByKey(
175 const SpdySessionKey
& key
);
177 // Remove the mapping of the given key, which must exist.
178 void UnmapKey(const SpdySessionKey
& key
);
180 // Remove all aliases for |key| from the aliases table.
181 void RemoveAliases(const SpdySessionKey
& key
);
183 // Get a copy of the current sessions as a list of WeakPtrs. Used by
184 // CloseCurrentSessionsHelper() below.
185 WeakSessionList
GetCurrentSessions() const;
187 // Close only the currently existing SpdySessions with |error|. Let
188 // any new ones created while this method is running continue to
189 // live. If |idle_only| is true only idle sessions are closed.
190 void CloseCurrentSessionsHelper(
192 const std::string
& description
,
195 const base::WeakPtr
<HttpServerProperties
> http_server_properties_
;
197 TransportSecurityState
* transport_security_state_
;
199 // The set of all sessions. This is a superset of the sessions in
200 // |available_sessions_|.
202 // |sessions_| owns all its SpdySession objects.
203 SessionSet sessions_
;
205 // This is a map of available sessions by key. A session may appear
206 // more than once in this map if it has aliases.
207 AvailableSessionMap available_sessions_
;
209 // A map of IPEndPoint aliases for sessions.
212 static bool g_force_single_domain
;
214 const scoped_refptr
<SSLConfigService
> ssl_config_service_
;
215 HostResolver
* const resolver_
;
217 // Defaults to true. May be controlled via SpdySessionPoolPeer for tests.
218 bool verify_domain_authentication_
;
219 bool enable_sending_initial_data_
;
220 bool force_single_domain_
;
221 bool enable_compression_
;
222 bool enable_ping_based_connection_checking_
;
223 const NextProto default_protocol_
;
224 size_t session_max_recv_window_size_
;
225 size_t stream_max_recv_window_size_
;
226 size_t initial_max_concurrent_streams_
;
227 size_t max_concurrent_streams_limit_
;
230 // This SPDY proxy is allowed to push resources from origins that are
231 // different from those of their associated streams.
232 HostPortPair trusted_spdy_proxy_
;
234 DISALLOW_COPY_AND_ASSIGN(SpdySessionPool
);
239 #endif // NET_SPDY_SPDY_SESSION_POOL_H_