[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 5 | #ifndef NET_SPDY_SPDY_SESSION_POOL_H_ |
| 6 | #define NET_SPDY_SPDY_SESSION_POOL_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 8 | |
| 9 | #include <map> |
| 10 | #include <list> |
| 11 | #include <string> |
| 12 | |
[email protected] | 367ead4 | 2010-02-26 00:15:21 | [diff] [blame] | 13 | #include "base/basictypes.h" |
[email protected] | 8822f38 | 2010-07-30 21:49:03 | [diff] [blame] | 14 | #include "base/gtest_prod_util.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
| 16 | #include "base/memory/scoped_ptr.h" |
[email protected] | 62635c7 | 2011-03-10 04:16:25 | [diff] [blame] | 17 | #include "net/base/cert_database.h" |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 18 | #include "net/base/host_port_pair.h" |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 19 | #include "net/base/ip_endpoint.h" |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 20 | #include "net/base/net_errors.h" |
[email protected] | b846acd | 2010-06-07 18:13:10 | [diff] [blame] | 21 | #include "net/base/network_change_notifier.h" |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 22 | #include "net/base/ssl_config_service.h" |
[email protected] | b261d0e | 2010-08-02 19:13:24 | [diff] [blame] | 23 | #include "net/proxy/proxy_config.h" |
[email protected] | 31e68d7 | 2010-08-25 06:36:58 | [diff] [blame] | 24 | #include "net/proxy/proxy_server.h" |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 25 | #include "net/spdy/spdy_settings_storage.h" |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 26 | |
| 27 | namespace net { |
| 28 | |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 29 | class AddressList; |
[email protected] | b846acd | 2010-06-07 18:13:10 | [diff] [blame] | 30 | class BoundNetLog; |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 31 | class ClientSocketHandle; |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 32 | class HostResolver; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 33 | class HttpNetworkSession; |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 34 | class SpdySession; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 35 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 36 | // This is a very simple pool for open SpdySessions. |
[email protected] | b846acd | 2010-06-07 18:13:10 | [diff] [blame] | 37 | class SpdySessionPool |
[email protected] | 232a581 | 2011-03-04 22:42:08 | [diff] [blame] | 38 | : public NetworkChangeNotifier::IPAddressObserver, |
[email protected] | 62635c7 | 2011-03-10 04:16:25 | [diff] [blame] | 39 | public SSLConfigService::Observer, |
| 40 | public CertDatabase::Observer { |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 41 | public: |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 42 | explicit SpdySessionPool(HostResolver* host_resolver, |
| 43 | SSLConfigService* ssl_config_service); |
[email protected] | 87bfa3f | 2010-09-30 14:54:56 | [diff] [blame] | 44 | virtual ~SpdySessionPool(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 45 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 46 | // Either returns an existing SpdySession or creates a new SpdySession for |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 47 | // use. |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 48 | scoped_refptr<SpdySession> Get( |
[email protected] | b261d0e | 2010-08-02 19:13:24 | [diff] [blame] | 49 | const HostPortProxyPair& host_port_proxy_pair, |
[email protected] | 635909f | 2010-05-12 18:19:36 | [diff] [blame] | 50 | const BoundNetLog& net_log); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 51 | |
[email protected] | 4a5e178 | 2010-03-05 20:49:02 | [diff] [blame] | 52 | // Set the maximum concurrent sessions per domain. |
| 53 | static void set_max_sessions_per_domain(int max) { |
| 54 | if (max >= 1) |
| 55 | g_max_sessions_per_domain = max; |
| 56 | } |
| 57 | |
[email protected] | 5fe524e | 2010-02-20 00:43:22 | [diff] [blame] | 58 | // Builds a SpdySession from an existing SSL socket. Users should try |
| 59 | // calling Get() first to use an existing SpdySession so we don't get |
| 60 | // multiple SpdySessions per domain. Note that ownership of |connection| is |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 61 | // transferred from the caller to the SpdySession. |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 62 | // |certificate_error_code| is used to indicate the certificate error |
| 63 | // encountered when connecting the SSL socket. OK means there was no error. |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 64 | // For testing, setting is_secure to false allows Spdy to connect with a |
| 65 | // pre-existing TCP socket. |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 66 | // Returns OK on success, and the |spdy_session| will be provided. |
| 67 | // Returns an error on failure, and |spdy_session| will be NULL. |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 68 | net::Error GetSpdySessionFromSocket( |
[email protected] | b261d0e | 2010-08-02 19:13:24 | [diff] [blame] | 69 | const HostPortProxyPair& host_port_proxy_pair, |
[email protected] | 635909f | 2010-05-12 18:19:36 | [diff] [blame] | 70 | ClientSocketHandle* connection, |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 71 | const BoundNetLog& net_log, |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 72 | int certificate_error_code, |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 73 | scoped_refptr<SpdySession>* spdy_session, |
| 74 | bool is_secure); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 75 | |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 76 | // TODO(willchan): Consider renaming to HasReusableSession, since perhaps we |
| 77 | // should be creating a new session. |
[email protected] | b261d0e | 2010-08-02 19:13:24 | [diff] [blame] | 78 | bool HasSession(const HostPortProxyPair& host_port_proxy_pair) const; |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 79 | |
[email protected] | a01ea22 | 2010-08-19 16:50:53 | [diff] [blame] | 80 | // Close all SpdySessions, including any new ones created in the process of |
| 81 | // closing the current ones. |
[email protected] | b278eb7 | 2010-07-09 20:17:00 | [diff] [blame] | 82 | void CloseAllSessions(); |
[email protected] | a01ea22 | 2010-08-19 16:50:53 | [diff] [blame] | 83 | // Close only the currently existing SpdySessions. Let any new ones created |
| 84 | // continue to live. |
| 85 | void CloseCurrentSessions(); |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 86 | |
[email protected] | 6c6ea17 | 2010-07-27 20:04:03 | [diff] [blame] | 87 | // Removes a SpdySession from the SpdySessionPool. This should only be called |
| 88 | // by SpdySession, because otherwise session->state_ is not set to CLOSED. |
[email protected] | 807c01a | 2010-04-21 16:57:45 | [diff] [blame] | 89 | void Remove(const scoped_refptr<SpdySession>& session); |
| 90 | |
[email protected] | 1ce7b66b | 2010-10-12 20:32:44 | [diff] [blame] | 91 | // Creates a Value summary of the state of the spdy session pool. The caller |
| 92 | // responsible for deleting the returned value. |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 93 | Value* SpdySessionPoolInfoToValue() const; |
[email protected] | 1ce7b66b | 2010-10-12 20:32:44 | [diff] [blame] | 94 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 95 | SpdySettingsStorage* mutable_spdy_settings() { return &spdy_settings_; } |
| 96 | const SpdySettingsStorage& spdy_settings() const { return spdy_settings_; } |
| 97 | |
[email protected] | 232a581 | 2011-03-04 22:42:08 | [diff] [blame] | 98 | // NetworkChangeNotifier::IPAddressObserver methods: |
[email protected] | b846acd | 2010-06-07 18:13:10 | [diff] [blame] | 99 | |
| 100 | // We flush all idle sessions and release references to the active ones so |
| 101 | // they won't get re-used. The active ones will either complete successfully |
| 102 | // or error out due to the IP address change. |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 103 | virtual void OnIPAddressChanged(); |
[email protected] | b846acd | 2010-06-07 18:13:10 | [diff] [blame] | 104 | |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 105 | // SSLConfigService::Observer methods: |
| 106 | |
| 107 | // We perform the same flushing as described above when SSL settings change. |
| 108 | virtual void OnSSLConfigChanged(); |
| 109 | |
[email protected] | ea1cc2cd | 2011-02-22 16:47:38 | [diff] [blame] | 110 | // A debugging mode where we compress all accesses through a single domain. |
| 111 | static void ForceSingleDomain() { g_force_single_domain = true; } |
| 112 | |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 113 | // Controls whether the pool allows use of a common session for domains |
| 114 | // which share IP address resolutions. |
| 115 | static void enable_ip_pooling(bool value) { g_enable_ip_pooling = value; } |
| 116 | |
[email protected] | 62635c7 | 2011-03-10 04:16:25 | [diff] [blame] | 117 | // CertDatabase::Observer methods: |
| 118 | virtual void OnUserCertAdded(X509Certificate* cert); |
| 119 | |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 120 | private: |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 121 | friend class SpdySessionPoolPeer; // For testing. |
[email protected] | cb54b3b2 | 2010-06-03 16:28:55 | [diff] [blame] | 122 | friend class SpdyNetworkTransactionTest; // For testing. |
[email protected] | 8822f38 | 2010-07-30 21:49:03 | [diff] [blame] | 123 | FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest, WindowUpdateOverflow); |
[email protected] | 9f7c4fd | 2009-11-24 18:50:15 | [diff] [blame] | 124 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 125 | typedef std::list<scoped_refptr<SpdySession> > SpdySessionList; |
[email protected] | b261d0e | 2010-08-02 19:13:24 | [diff] [blame] | 126 | typedef std::map<HostPortProxyPair, SpdySessionList*> SpdySessionsMap; |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 127 | typedef std::map<IPEndPoint, HostPortProxyPair> SpdyAliasMap; |
| 128 | |
| 129 | scoped_refptr<SpdySession> GetExistingSession( |
| 130 | SpdySessionList* list, |
| 131 | const BoundNetLog& net_log) const; |
| 132 | scoped_refptr<SpdySession> GetFromAlias( |
| 133 | const HostPortProxyPair& host_port_proxy_pair, |
| 134 | const BoundNetLog& net_log, |
| 135 | bool record_histograms) const; |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 136 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 137 | // Helper functions for manipulating the lists. |
[email protected] | ea1cc2cd | 2011-02-22 16:47:38 | [diff] [blame] | 138 | const HostPortProxyPair& NormalizeListPair( |
| 139 | const HostPortProxyPair& host_port_proxy_pair) const; |
[email protected] | b261d0e | 2010-08-02 19:13:24 | [diff] [blame] | 140 | SpdySessionList* AddSessionList( |
| 141 | const HostPortProxyPair& host_port_proxy_pair); |
| 142 | SpdySessionList* GetSessionList( |
[email protected] | b261d0e | 2010-08-02 19:13:24 | [diff] [blame] | 143 | const HostPortProxyPair& host_port_proxy_pair) const; |
| 144 | void RemoveSessionList(const HostPortProxyPair& host_port_proxy_pair); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 145 | |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 146 | // Does a DNS cache lookup for |pair|, and returns the |addresses| found. |
| 147 | // Returns true if addresses found, false otherwise. |
| 148 | bool LookupAddresses(const HostPortProxyPair& pair, |
| 149 | AddressList* addresses) const; |
| 150 | |
| 151 | // Add a set of |addresses| as IP-equivalent addresses for |pair|. |
| 152 | void AddAliases(const AddressList& addresses, const HostPortProxyPair& pair); |
| 153 | |
| 154 | // Remove all aliases for |pair| from the aliases table. |
| 155 | void RemoveAliases(const HostPortProxyPair& pair); |
| 156 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 157 | SpdySettingsStorage spdy_settings_; |
| 158 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 159 | // This is our weak session pool - one session per domain. |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 160 | SpdySessionsMap sessions_; |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 161 | // A map of IPEndPoint aliases for sessions. |
| 162 | SpdyAliasMap aliases_; |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 163 | |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 164 | static size_t g_max_sessions_per_domain; |
[email protected] | ea1cc2cd | 2011-02-22 16:47:38 | [diff] [blame] | 165 | static bool g_force_single_domain; |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 166 | static bool g_enable_ip_pooling; |
[email protected] | 4a5e178 | 2010-03-05 20:49:02 | [diff] [blame] | 167 | |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 168 | const scoped_refptr<SSLConfigService> ssl_config_service_; |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 169 | HostResolver* resolver_; |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 170 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 171 | DISALLOW_COPY_AND_ASSIGN(SpdySessionPool); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | } // namespace net |
| 175 | |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 176 | #endif // NET_SPDY_SPDY_SESSION_POOL_H_ |