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 enable_compression
,
56 bool enable_ping_based_connection_checking
,
57 NextProto default_protocol
,
58 size_t session_max_recv_window_size
,
59 size_t stream_max_recv_window_size
,
60 size_t initial_max_concurrent_streams
,
61 SpdySessionPool::TimeFunc time_func
,
62 const std::string
& trusted_spdy_proxy
);
63 ~SpdySessionPool() override
;
65 // In the functions below, a session is "available" if this pool has
66 // a reference to it and there is some SpdySessionKey for which
67 // FindAvailableSession() will return it. A session is "unavailable"
68 // if this pool has a reference to it but it won't be returned by
69 // FindAvailableSession() for any SpdySessionKey; for example, this
70 // can happen when a session receives a GOAWAY frame and is still
71 // processing existing streams.
73 // Create a new SPDY session from an existing socket. There must
74 // not already be a session for the given key. This pool must have
75 // been constructed with a valid |default_protocol| value.
77 // |is_secure| can be false for testing or when SPDY is configured
78 // to work with non-secure sockets. If |is_secure| is true,
79 // |certificate_error_code| indicates that the certificate error
80 // encountered when connecting the SSL socket, with OK meaning there
83 // Returns the new SpdySession. Note that the SpdySession begins reading from
84 // |connection| on a subsequent event loop iteration, so it may be closed
85 // immediately afterwards if the first read of |connection| fails.
86 base::WeakPtr
<SpdySession
> CreateAvailableSessionFromSocket(
87 const SpdySessionKey
& key
,
88 scoped_ptr
<ClientSocketHandle
> connection
,
89 const BoundNetLog
& net_log
,
90 int certificate_error_code
,
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
);
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
);
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
);
108 // Close only the currently existing SpdySessions with |error|.
109 // Let any new ones created while this method is running continue to
111 void CloseCurrentSessions(Error error
);
113 // Close only the currently existing SpdySessions that are idle.
114 // Let any new ones created while this method is running continue to
116 void CloseCurrentIdleSessions();
118 // Close all SpdySessions, including any new ones created in the process of
119 // closing the current ones.
120 void CloseAllSessions();
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;
126 base::WeakPtr
<HttpServerProperties
> http_server_properties() {
127 return http_server_properties_
;
130 // NetworkChangeNotifier::IPAddressObserver methods:
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 void OnIPAddressChanged() override
;
137 // SSLConfigService::Observer methods:
139 // We perform the same flushing as described above when SSL settings change.
140 void OnSSLConfigChanged() override
;
142 // CertDatabase::Observer methods:
144 // We perform the same flushing as described above when certificate database
146 void OnCertAdded(const X509Certificate
* cert
) override
;
147 void OnCACertChanged(const X509Certificate
* cert
) override
;
150 friend class SpdySessionPoolPeer
; // For testing.
152 typedef std::set
<SpdySession
*> SessionSet
;
153 typedef std::vector
<base::WeakPtr
<SpdySession
> > WeakSessionList
;
154 typedef std::map
<SpdySessionKey
, base::WeakPtr
<SpdySession
> >
156 typedef std::map
<IPEndPoint
, SpdySessionKey
> AliasMap
;
158 // Returns true iff |session| is in |available_sessions_|.
159 bool IsSessionAvailable(const base::WeakPtr
<SpdySession
>& session
) const;
161 // Map the given key to the given session. There must not already be
162 // a mapping for |key|.
163 void MapKeyToAvailableSession(const SpdySessionKey
& key
,
164 const base::WeakPtr
<SpdySession
>& session
);
166 // Returns an iterator into |available_sessions_| for the given key,
167 // which may be equal to |available_sessions_.end()|.
168 AvailableSessionMap::iterator
LookupAvailableSessionByKey(
169 const SpdySessionKey
& key
);
171 // Remove the mapping of the given key, which must exist.
172 void UnmapKey(const SpdySessionKey
& key
);
174 // Remove all aliases for |key| from the aliases table.
175 void RemoveAliases(const SpdySessionKey
& key
);
177 // Get a copy of the current sessions as a list of WeakPtrs. Used by
178 // CloseCurrentSessionsHelper() below.
179 WeakSessionList
GetCurrentSessions() const;
181 // Close only the currently existing SpdySessions with |error|. Let
182 // any new ones created while this method is running continue to
183 // live. If |idle_only| is true only idle sessions are closed.
184 void CloseCurrentSessionsHelper(
186 const std::string
& description
,
189 const base::WeakPtr
<HttpServerProperties
> http_server_properties_
;
191 TransportSecurityState
* transport_security_state_
;
193 // The set of all sessions. This is a superset of the sessions in
194 // |available_sessions_|.
196 // |sessions_| owns all its SpdySession objects.
197 SessionSet sessions_
;
199 // This is a map of available sessions by key. A session may appear
200 // more than once in this map if it has aliases.
201 AvailableSessionMap available_sessions_
;
203 // A map of IPEndPoint aliases for sessions.
206 const scoped_refptr
<SSLConfigService
> ssl_config_service_
;
207 HostResolver
* const resolver_
;
209 // Defaults to true. May be controlled via SpdySessionPoolPeer for tests.
210 bool verify_domain_authentication_
;
211 bool enable_sending_initial_data_
;
212 bool enable_compression_
;
213 bool enable_ping_based_connection_checking_
;
214 const NextProto default_protocol_
;
215 size_t session_max_recv_window_size_
;
216 size_t stream_max_recv_window_size_
;
217 size_t initial_max_concurrent_streams_
;
220 // This SPDY proxy is allowed to push resources from origins that are
221 // different from those of their associated streams.
222 HostPortPair trusted_spdy_proxy_
;
224 DISALLOW_COPY_AND_ASSIGN(SpdySessionPool
);
229 #endif // NET_SPDY_SPDY_SESSION_POOL_H_