blob: 7a08861ab8b802dbb54eaf99c5211dcdbe929132 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]aea80602009-09-18 00:55:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]dab9c7d2010-02-06 21:44:325#ifndef NET_SPDY_SPDY_SESSION_POOL_H_
6#define NET_SPDY_SPDY_SESSION_POOL_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]aea80602009-09-18 00:55:088
9#include <map>
10#include <list>
11#include <string>
12
[email protected]367ead42010-02-26 00:15:2113#include "base/basictypes.h"
[email protected]8822f382010-07-30 21:49:0314#include "base/gtest_prod_util.h"
[email protected]3b63f8f42011-03-28 01:54:1515#include "base/memory/ref_counted.h"
16#include "base/memory/scoped_ptr.h"
[email protected]62635c72011-03-10 04:16:2517#include "net/base/cert_database.h"
[email protected]564b4912010-03-09 16:30:4218#include "net/base/host_port_pair.h"
[email protected]8b114dd72011-03-25 05:33:0219#include "net/base/ip_endpoint.h"
[email protected]26ef6582010-06-24 02:30:4720#include "net/base/net_errors.h"
[email protected]b846acd2010-06-07 18:13:1021#include "net/base/network_change_notifier.h"
[email protected]7abf7d22010-09-04 01:41:5922#include "net/base/ssl_config_service.h"
[email protected]b261d0e2010-08-02 19:13:2423#include "net/proxy/proxy_config.h"
[email protected]31e68d72010-08-25 06:36:5824#include "net/proxy/proxy_server.h"
[email protected]102e27c2011-02-23 01:01:3125#include "net/spdy/spdy_settings_storage.h"
[email protected]aea80602009-09-18 00:55:0826
27namespace net {
28
[email protected]8b114dd72011-03-25 05:33:0229class AddressList;
[email protected]b846acd2010-06-07 18:13:1030class BoundNetLog;
[email protected]1f14a912009-12-21 20:32:4431class ClientSocketHandle;
[email protected]8b114dd72011-03-25 05:33:0232class HostResolver;
[email protected]aea80602009-09-18 00:55:0833class HttpNetworkSession;
[email protected]955fc2e72010-02-08 20:37:3034class SpdySession;
[email protected]aea80602009-09-18 00:55:0835
[email protected]955fc2e72010-02-08 20:37:3036// This is a very simple pool for open SpdySessions.
[email protected]b846acd2010-06-07 18:13:1037class SpdySessionPool
[email protected]232a5812011-03-04 22:42:0838 : public NetworkChangeNotifier::IPAddressObserver,
[email protected]62635c72011-03-10 04:16:2539 public SSLConfigService::Observer,
40 public CertDatabase::Observer {
[email protected]aea80602009-09-18 00:55:0841 public:
[email protected]8b114dd72011-03-25 05:33:0242 explicit SpdySessionPool(HostResolver* host_resolver,
43 SSLConfigService* ssl_config_service);
[email protected]87bfa3f2010-09-30 14:54:5644 virtual ~SpdySessionPool();
[email protected]aea80602009-09-18 00:55:0845
[email protected]955fc2e72010-02-08 20:37:3046 // Either returns an existing SpdySession or creates a new SpdySession for
[email protected]d1eda932009-11-04 01:03:1047 // use.
[email protected]955fc2e72010-02-08 20:37:3048 scoped_refptr<SpdySession> Get(
[email protected]b261d0e2010-08-02 19:13:2449 const HostPortProxyPair& host_port_proxy_pair,
[email protected]635909f2010-05-12 18:19:3650 const BoundNetLog& net_log);
[email protected]aea80602009-09-18 00:55:0851
[email protected]4a5e1782010-03-05 20:49:0252 // 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]5fe524e2010-02-20 00:43:2258 // 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]955fc2e72010-02-08 20:37:3061 // transferred from the caller to the SpdySession.
[email protected]bdbda462010-06-28 17:30:3762 // |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]9e9e842e2010-07-23 23:09:1564 // For testing, setting is_secure to false allows Spdy to connect with a
65 // pre-existing TCP socket.
[email protected]26ef6582010-06-24 02:30:4766 // 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]9e9e842e2010-07-23 23:09:1568 net::Error GetSpdySessionFromSocket(
[email protected]b261d0e2010-08-02 19:13:2469 const HostPortProxyPair& host_port_proxy_pair,
[email protected]635909f2010-05-12 18:19:3670 ClientSocketHandle* connection,
[email protected]26ef6582010-06-24 02:30:4771 const BoundNetLog& net_log,
[email protected]bdbda462010-06-28 17:30:3772 int certificate_error_code,
[email protected]9e9e842e2010-07-23 23:09:1573 scoped_refptr<SpdySession>* spdy_session,
74 bool is_secure);
[email protected]aea80602009-09-18 00:55:0875
[email protected]d1eda932009-11-04 01:03:1076 // TODO(willchan): Consider renaming to HasReusableSession, since perhaps we
77 // should be creating a new session.
[email protected]b261d0e2010-08-02 19:13:2478 bool HasSession(const HostPortProxyPair& host_port_proxy_pair) const;
[email protected]d1eda932009-11-04 01:03:1079
[email protected]a01ea222010-08-19 16:50:5380 // Close all SpdySessions, including any new ones created in the process of
81 // closing the current ones.
[email protected]b278eb72010-07-09 20:17:0082 void CloseAllSessions();
[email protected]a01ea222010-08-19 16:50:5383 // Close only the currently existing SpdySessions. Let any new ones created
84 // continue to live.
85 void CloseCurrentSessions();
[email protected]d1eda932009-11-04 01:03:1086
[email protected]6c6ea172010-07-27 20:04:0387 // 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]807c01a2010-04-21 16:57:4589 void Remove(const scoped_refptr<SpdySession>& session);
90
[email protected]1ce7b66b2010-10-12 20:32:4491 // Creates a Value summary of the state of the spdy session pool. The caller
92 // responsible for deleting the returned value.
[email protected]9e1bdd32011-02-03 21:48:3493 Value* SpdySessionPoolInfoToValue() const;
[email protected]1ce7b66b2010-10-12 20:32:4494
[email protected]102e27c2011-02-23 01:01:3195 SpdySettingsStorage* mutable_spdy_settings() { return &spdy_settings_; }
96 const SpdySettingsStorage& spdy_settings() const { return spdy_settings_; }
97
[email protected]232a5812011-03-04 22:42:0898 // NetworkChangeNotifier::IPAddressObserver methods:
[email protected]b846acd2010-06-07 18:13:1099
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]66761b952010-06-25 21:30:38103 virtual void OnIPAddressChanged();
[email protected]b846acd2010-06-07 18:13:10104
[email protected]7abf7d22010-09-04 01:41:59105 // SSLConfigService::Observer methods:
106
107 // We perform the same flushing as described above when SSL settings change.
108 virtual void OnSSLConfigChanged();
109
[email protected]ea1cc2cd2011-02-22 16:47:38110 // A debugging mode where we compress all accesses through a single domain.
111 static void ForceSingleDomain() { g_force_single_domain = true; }
112
[email protected]8b114dd72011-03-25 05:33:02113 // 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]62635c72011-03-10 04:16:25117 // CertDatabase::Observer methods:
118 virtual void OnUserCertAdded(X509Certificate* cert);
119
[email protected]d1eda932009-11-04 01:03:10120 private:
[email protected]955fc2e72010-02-08 20:37:30121 friend class SpdySessionPoolPeer; // For testing.
[email protected]cb54b3b22010-06-03 16:28:55122 friend class SpdyNetworkTransactionTest; // For testing.
[email protected]8822f382010-07-30 21:49:03123 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest, WindowUpdateOverflow);
[email protected]9f7c4fd2009-11-24 18:50:15124
[email protected]955fc2e72010-02-08 20:37:30125 typedef std::list<scoped_refptr<SpdySession> > SpdySessionList;
[email protected]b261d0e2010-08-02 19:13:24126 typedef std::map<HostPortProxyPair, SpdySessionList*> SpdySessionsMap;
[email protected]8b114dd72011-03-25 05:33:02127 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]d1eda932009-11-04 01:03:10136
[email protected]aea80602009-09-18 00:55:08137 // Helper functions for manipulating the lists.
[email protected]ea1cc2cd2011-02-22 16:47:38138 const HostPortProxyPair& NormalizeListPair(
139 const HostPortProxyPair& host_port_proxy_pair) const;
[email protected]b261d0e2010-08-02 19:13:24140 SpdySessionList* AddSessionList(
141 const HostPortProxyPair& host_port_proxy_pair);
142 SpdySessionList* GetSessionList(
[email protected]b261d0e2010-08-02 19:13:24143 const HostPortProxyPair& host_port_proxy_pair) const;
144 void RemoveSessionList(const HostPortProxyPair& host_port_proxy_pair);
[email protected]aea80602009-09-18 00:55:08145
[email protected]8b114dd72011-03-25 05:33:02146 // 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]102e27c2011-02-23 01:01:31157 SpdySettingsStorage spdy_settings_;
158
[email protected]aea80602009-09-18 00:55:08159 // This is our weak session pool - one session per domain.
[email protected]955fc2e72010-02-08 20:37:30160 SpdySessionsMap sessions_;
[email protected]8b114dd72011-03-25 05:33:02161 // A map of IPEndPoint aliases for sessions.
162 SpdyAliasMap aliases_;
[email protected]d1eda932009-11-04 01:03:10163
[email protected]8b114dd72011-03-25 05:33:02164 static size_t g_max_sessions_per_domain;
[email protected]ea1cc2cd2011-02-22 16:47:38165 static bool g_force_single_domain;
[email protected]8b114dd72011-03-25 05:33:02166 static bool g_enable_ip_pooling;
[email protected]4a5e1782010-03-05 20:49:02167
[email protected]7abf7d22010-09-04 01:41:59168 const scoped_refptr<SSLConfigService> ssl_config_service_;
[email protected]8b114dd72011-03-25 05:33:02169 HostResolver* resolver_;
[email protected]7abf7d22010-09-04 01:41:59170
[email protected]955fc2e72010-02-08 20:37:30171 DISALLOW_COPY_AND_ASSIGN(SpdySessionPool);
[email protected]aea80602009-09-18 00:55:08172};
173
174} // namespace net
175
[email protected]dab9c7d2010-02-06 21:44:32176#endif // NET_SPDY_SPDY_SESSION_POOL_H_