[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | #include "net/spdy/spdy_session_pool.h" |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 6 | |
| 7 | #include "base/logging.h" |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 8 | #include "base/metrics/histogram.h" |
[email protected] | 1ce7b66b | 2010-10-12 20:32:44 | [diff] [blame] | 9 | #include "base/values.h" |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 10 | #include "net/base/address_list.h" |
[email protected] | f458033 | 2010-09-25 21:20:27 | [diff] [blame] | 11 | #include "net/http/http_network_session.h" |
[email protected] | 53bfa31c | 2011-11-15 19:20:31 | [diff] [blame] | 12 | #include "net/http/http_server_properties.h" |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 13 | #include "net/spdy/spdy_session.h" |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 14 | |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 15 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 16 | namespace net { |
| 17 | |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | enum SpdySessionGetTypes { |
| 21 | CREATED_NEW = 0, |
| 22 | FOUND_EXISTING = 1, |
| 23 | FOUND_EXISTING_FROM_IP_POOL = 2, |
| 24 | IMPORTED_FROM_SOCKET = 3, |
| 25 | SPDY_SESSION_GET_MAX = 4 |
| 26 | }; |
| 27 | |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 28 | } // namespace |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 29 | |
[email protected] | 61b4efc | 2012-04-27 18:12:50 | [diff] [blame] | 30 | SpdySessionPool::SpdySessionPool( |
| 31 | HostResolver* resolver, |
| 32 | SSLConfigService* ssl_config_service, |
[email protected] | 30d4c02 | 2013-07-18 22:58:16 | [diff] [blame] | 33 | const base::WeakPtr<HttpServerProperties>& http_server_properties, |
[email protected] | f9cf557 | 2012-12-04 15:52:09 | [diff] [blame] | 34 | bool force_single_domain, |
| 35 | bool enable_ip_pooling, |
| 36 | bool enable_credential_frames, |
| 37 | bool enable_compression, |
| 38 | bool enable_ping_based_connection_checking, |
| 39 | NextProto default_protocol, |
[email protected] | a3827a0 | 2013-02-22 00:04:32 | [diff] [blame] | 40 | size_t stream_initial_recv_window_size, |
[email protected] | f9cf557 | 2012-12-04 15:52:09 | [diff] [blame] | 41 | size_t initial_max_concurrent_streams, |
| 42 | size_t max_concurrent_streams_limit, |
| 43 | SpdySessionPool::TimeFunc time_func, |
[email protected] | 61b4efc | 2012-04-27 18:12:50 | [diff] [blame] | 44 | const std::string& trusted_spdy_proxy) |
[email protected] | 53bfa31c | 2011-11-15 19:20:31 | [diff] [blame] | 45 | : http_server_properties_(http_server_properties), |
| 46 | ssl_config_service_(ssl_config_service), |
[email protected] | b9ec688 | 2011-07-01 07:40:26 | [diff] [blame] | 47 | resolver_(resolver), |
[email protected] | 61b4efc | 2012-04-27 18:12:50 | [diff] [blame] | 48 | verify_domain_authentication_(true), |
[email protected] | e8bde62 | 2012-06-14 23:20:24 | [diff] [blame] | 49 | enable_sending_initial_settings_(true), |
[email protected] | f9cf557 | 2012-12-04 15:52:09 | [diff] [blame] | 50 | force_single_domain_(force_single_domain), |
| 51 | enable_ip_pooling_(enable_ip_pooling), |
| 52 | enable_credential_frames_(enable_credential_frames), |
| 53 | enable_compression_(enable_compression), |
| 54 | enable_ping_based_connection_checking_( |
| 55 | enable_ping_based_connection_checking), |
| 56 | default_protocol_(default_protocol), |
[email protected] | a3827a0 | 2013-02-22 00:04:32 | [diff] [blame] | 57 | stream_initial_recv_window_size_(stream_initial_recv_window_size), |
[email protected] | f9cf557 | 2012-12-04 15:52:09 | [diff] [blame] | 58 | initial_max_concurrent_streams_(initial_max_concurrent_streams), |
| 59 | max_concurrent_streams_limit_(max_concurrent_streams_limit), |
| 60 | time_func_(time_func), |
[email protected] | 61b4efc | 2012-04-27 18:12:50 | [diff] [blame] | 61 | trusted_spdy_proxy_( |
| 62 | HostPortPair::FromString(trusted_spdy_proxy)) { |
[email protected] | 232a581 | 2011-03-04 22:42:08 | [diff] [blame] | 63 | NetworkChangeNotifier::AddIPAddressObserver(this); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 64 | if (ssl_config_service_.get()) |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 65 | ssl_config_service_->AddObserver(this); |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 66 | CertDatabase::GetInstance()->AddObserver(this); |
[email protected] | b846acd | 2010-06-07 18:13:10 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 69 | SpdySessionPool::~SpdySessionPool() { |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 70 | CloseAllSessions(); |
[email protected] | b846acd | 2010-06-07 18:13:10 | [diff] [blame] | 71 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 72 | if (ssl_config_service_.get()) |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 73 | ssl_config_service_->RemoveObserver(this); |
[email protected] | 232a581 | 2011-03-04 22:42:08 | [diff] [blame] | 74 | NetworkChangeNotifier::RemoveIPAddressObserver(this); |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 75 | CertDatabase::GetInstance()->RemoveObserver(this); |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 76 | } |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 77 | |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 78 | net::Error SpdySessionPool::CreateAvailableSessionFromSocket( |
| 79 | const SpdySessionKey& key, |
| 80 | scoped_ptr<ClientSocketHandle> connection, |
| 81 | const BoundNetLog& net_log, |
| 82 | int certificate_error_code, |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame^] | 83 | base::WeakPtr<SpdySession>* available_session, |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 84 | bool is_secure) { |
| 85 | UMA_HISTOGRAM_ENUMERATION( |
| 86 | "Net.SpdySessionGet", IMPORTED_FROM_SOCKET, SPDY_SESSION_GET_MAX); |
| 87 | |
| 88 | scoped_refptr<SpdySession> new_session( |
| 89 | new SpdySession(key, |
| 90 | http_server_properties_, |
| 91 | verify_domain_authentication_, |
| 92 | enable_sending_initial_settings_, |
| 93 | enable_credential_frames_, |
| 94 | enable_compression_, |
| 95 | enable_ping_based_connection_checking_, |
| 96 | default_protocol_, |
| 97 | stream_initial_recv_window_size_, |
| 98 | initial_max_concurrent_streams_, |
| 99 | max_concurrent_streams_limit_, |
| 100 | time_func_, |
| 101 | trusted_spdy_proxy_, |
| 102 | net_log.net_log())); |
| 103 | |
| 104 | Error error = new_session->InitializeWithSocket( |
| 105 | connection.Pass(), this, is_secure, certificate_error_code); |
| 106 | DCHECK_NE(error, ERR_IO_PENDING); |
| 107 | |
| 108 | if (error != OK) { |
| 109 | new_session = NULL; |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame^] | 110 | available_session->reset(); |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 111 | return error; |
| 112 | } |
| 113 | |
| 114 | sessions_.insert(new_session); |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame^] | 115 | *available_session = new_session->GetWeakPtr(); |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 116 | MapKeyToAvailableSession(key, *available_session); |
| 117 | |
| 118 | net_log.AddEvent( |
| 119 | NetLog::TYPE_SPDY_SESSION_POOL_IMPORTED_SESSION_FROM_SOCKET, |
| 120 | (*available_session)->net_log().source().ToEventParametersCallback()); |
| 121 | |
| 122 | // Look up the IP address for this session so that we can match |
| 123 | // future sessions (potentially to different domains) which can |
| 124 | // potentially be pooled with this one. Because GetPeerAddress() |
| 125 | // reports the proxy's address instead of the origin server, check |
| 126 | // to see if this is a direct connection. |
| 127 | if (enable_ip_pooling_ && key.proxy_server().is_direct()) { |
| 128 | IPEndPoint address; |
| 129 | if ((*available_session)->GetPeerAddress(&address) == OK) |
| 130 | aliases_[address] = key; |
| 131 | } |
| 132 | |
| 133 | return error; |
| 134 | } |
| 135 | |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame^] | 136 | base::WeakPtr<SpdySession> SpdySessionPool::FindAvailableSession( |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 137 | const SpdySessionKey& key, |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 138 | const BoundNetLog& net_log) { |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 139 | AvailableSessionMap::iterator it = LookupAvailableSessionByKey(key); |
| 140 | if (it != available_sessions_.end()) { |
| 141 | UMA_HISTOGRAM_ENUMERATION( |
| 142 | "Net.SpdySessionGet", FOUND_EXISTING, SPDY_SESSION_GET_MAX); |
[email protected] | 00cd9c4 | 2010-11-02 20:15:57 | [diff] [blame] | 143 | net_log.AddEvent( |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 144 | NetLog::TYPE_SPDY_SESSION_POOL_FOUND_EXISTING_SESSION, |
| 145 | it->second->net_log().source().ToEventParametersCallback()); |
| 146 | return it->second; |
| 147 | } |
| 148 | |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 149 | if (!enable_ip_pooling_) |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame^] | 150 | return base::WeakPtr<SpdySession>(); |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 151 | |
| 152 | // Look up the key's from the resolver's cache. |
| 153 | net::HostResolver::RequestInfo resolve_info(key.host_port_pair()); |
| 154 | AddressList addresses; |
| 155 | int rv = resolver_->ResolveFromCache(resolve_info, &addresses, net_log); |
| 156 | DCHECK_NE(rv, ERR_IO_PENDING); |
| 157 | if (rv != OK) |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame^] | 158 | return base::WeakPtr<SpdySession>(); |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 159 | |
| 160 | // Check if we have a session through a domain alias. |
| 161 | for (AddressList::const_iterator address_it = addresses.begin(); |
| 162 | address_it != addresses.end(); |
| 163 | ++address_it) { |
| 164 | AliasMap::const_iterator alias_it = aliases_.find(*address_it); |
| 165 | if (alias_it == aliases_.end()) |
| 166 | continue; |
| 167 | |
| 168 | // We found an alias. |
| 169 | const SpdySessionKey& alias_key = alias_it->second; |
| 170 | |
| 171 | // We can reuse this session only if the proxy and privacy |
| 172 | // settings match. |
| 173 | if (!(alias_key.proxy_server() == key.proxy_server()) || |
| 174 | !(alias_key.privacy_mode() == key.privacy_mode())) |
| 175 | continue; |
| 176 | |
| 177 | AvailableSessionMap::iterator available_session_it = |
| 178 | LookupAvailableSessionByKey(alias_key); |
| 179 | if (available_session_it == available_sessions_.end()) { |
| 180 | NOTREACHED(); // It shouldn't be in the aliases table if we can't get it! |
| 181 | continue; |
| 182 | } |
| 183 | |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame^] | 184 | const base::WeakPtr<SpdySession>& available_session = |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 185 | available_session_it->second; |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame^] | 186 | DCHECK(ContainsKey(sessions_, available_session.get())); |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 187 | // If the session is a secure one, we need to verify that the |
| 188 | // server is authenticated to serve traffic for |host_port_proxy_pair| too. |
| 189 | if (!available_session->VerifyDomainAuthentication( |
| 190 | key.host_port_pair().host())) { |
| 191 | UMA_HISTOGRAM_ENUMERATION("Net.SpdyIPPoolDomainMatch", 0, 2); |
| 192 | continue; |
| 193 | } |
| 194 | |
| 195 | UMA_HISTOGRAM_ENUMERATION("Net.SpdyIPPoolDomainMatch", 1, 2); |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 196 | UMA_HISTOGRAM_ENUMERATION("Net.SpdySessionGet", |
| 197 | FOUND_EXISTING_FROM_IP_POOL, |
| 198 | SPDY_SESSION_GET_MAX); |
| 199 | net_log.AddEvent( |
| 200 | NetLog::TYPE_SPDY_SESSION_POOL_FOUND_EXISTING_SESSION_FROM_IP_POOL, |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 201 | available_session->net_log().source().ToEventParametersCallback()); |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 202 | // Add this session to the map so that we can find it next time. |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 203 | MapKeyToAvailableSession(key, available_session); |
| 204 | available_session->AddPooledAlias(key); |
| 205 | return available_session; |
[email protected] | 6cd3bd20 | 2010-08-30 05:23:06 | [diff] [blame] | 206 | } |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 207 | |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame^] | 208 | return base::WeakPtr<SpdySession>(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 209 | } |
| 210 | |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 211 | void SpdySessionPool::MakeSessionUnavailable( |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame^] | 212 | const base::WeakPtr<SpdySession>& available_session) { |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 213 | UnmapKey(available_session->spdy_session_key()); |
| 214 | RemoveAliases(available_session->spdy_session_key()); |
| 215 | const std::set<SpdySessionKey>& aliases = available_session->pooled_aliases(); |
| 216 | for (std::set<SpdySessionKey>::const_iterator it = aliases.begin(); |
| 217 | it != aliases.end(); ++it) { |
| 218 | UnmapKey(*it); |
| 219 | RemoveAliases(*it); |
[email protected] | 46da33be | 2011-07-19 21:58:04 | [diff] [blame] | 220 | } |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 221 | DCHECK(!IsSessionAvailable(available_session)); |
| 222 | } |
[email protected] | 46da33be | 2011-07-19 21:58:04 | [diff] [blame] | 223 | |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 224 | void SpdySessionPool::RemoveUnavailableSession( |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame^] | 225 | const base::WeakPtr<SpdySession>& unavailable_session) { |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 226 | DCHECK(!IsSessionAvailable(unavailable_session)); |
| 227 | |
| 228 | unavailable_session->net_log().AddEvent( |
| 229 | NetLog::TYPE_SPDY_SESSION_POOL_REMOVE_SESSION, |
| 230 | unavailable_session->net_log().source().ToEventParametersCallback()); |
| 231 | |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame^] | 232 | SessionSet::iterator it = sessions_.find(unavailable_session.get()); |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 233 | CHECK(it != sessions_.end()); |
| 234 | sessions_.erase(it); |
[email protected] | 9f7c4fd | 2009-11-24 18:50:15 | [diff] [blame] | 235 | } |
| 236 | |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 237 | // Make a copy of |sessions_| in the Close* functions below to avoid |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 238 | // reentrancy problems. Since arbitrary functions get called by close |
| 239 | // handlers, it doesn't suffice to simply increment the iterator |
| 240 | // before closing. |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 241 | |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 242 | void SpdySessionPool::CloseCurrentSessions(net::Error error) { |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 243 | CloseCurrentSessionsHelper(error, "Closing current sessions.", |
| 244 | false /* idle_only */); |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void SpdySessionPool::CloseCurrentIdleSessions() { |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 248 | CloseCurrentSessionsHelper(ERR_ABORTED, "Closing idle sessions.", |
| 249 | true /* idle_only */); |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void SpdySessionPool::CloseAllSessions() { |
| 253 | while (!sessions_.empty()) { |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 254 | CloseCurrentSessionsHelper(ERR_ABORTED, "Closing all sessions.", |
| 255 | false /* idle_only */); |
[email protected] | 6cbfa85 | 2012-03-14 06:35:54 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 259 | base::Value* SpdySessionPool::SpdySessionPoolInfoToValue() const { |
| 260 | base::ListValue* list = new base::ListValue(); |
[email protected] | 1ce7b66b | 2010-10-12 20:32:44 | [diff] [blame] | 261 | |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 262 | for (AvailableSessionMap::const_iterator it = available_sessions_.begin(); |
| 263 | it != available_sessions_.end(); ++it) { |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 264 | // Only add the session if the key in the map matches the main |
| 265 | // host_port_proxy_pair (not an alias). |
| 266 | const SpdySessionKey& key = it->first; |
| 267 | const SpdySessionKey& session_key = it->second->spdy_session_key(); |
| 268 | if (key.Equals(session_key)) |
| 269 | list->Append(it->second->GetInfoAsValue()); |
[email protected] | 1ce7b66b | 2010-10-12 20:32:44 | [diff] [blame] | 270 | } |
| 271 | return list; |
| 272 | } |
| 273 | |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 274 | void SpdySessionPool::OnIPAddressChanged() { |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 275 | CloseCurrentSessions(ERR_NETWORK_CHANGED); |
[email protected] | 6dd1134e | 2013-04-30 21:13:09 | [diff] [blame] | 276 | http_server_properties_->ClearAllSpdySettings(); |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 277 | } |
| 278 | |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 279 | void SpdySessionPool::OnSSLConfigChanged() { |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 280 | CloseCurrentSessions(ERR_NETWORK_CHANGED); |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 281 | } |
| 282 | |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 283 | void SpdySessionPool::OnCertAdded(const X509Certificate* cert) { |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 284 | CloseCurrentSessions(ERR_NETWORK_CHANGED); |
[email protected] | c940d37 | 2011-04-13 17:20:18 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void SpdySessionPool::OnCertTrustChanged(const X509Certificate* cert) { |
| 288 | // Per wtc, we actually only need to CloseCurrentSessions when trust is |
| 289 | // reduced. CloseCurrentSessions now because OnCertTrustChanged does not |
| 290 | // tell us this. |
| 291 | // See comments in ClientSocketPoolManager::OnCertTrustChanged. |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 292 | CloseCurrentSessions(ERR_NETWORK_CHANGED); |
[email protected] | 62635c7 | 2011-03-10 04:16:25 | [diff] [blame] | 293 | } |
| 294 | |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 295 | bool SpdySessionPool::IsSessionAvailable( |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame^] | 296 | const base::WeakPtr<SpdySession>& session) const { |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 297 | for (AvailableSessionMap::const_iterator it = available_sessions_.begin(); |
| 298 | it != available_sessions_.end(); ++it) { |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame^] | 299 | if (it->second.get() == session.get()) |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 300 | return true; |
| 301 | } |
| 302 | return false; |
| 303 | } |
| 304 | |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 305 | const SpdySessionKey& SpdySessionPool::NormalizeListKey( |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 306 | const SpdySessionKey& key) const { |
[email protected] | f9cf557 | 2012-12-04 15:52:09 | [diff] [blame] | 307 | if (!force_single_domain_) |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 308 | return key; |
[email protected] | ea1cc2cd | 2011-02-22 16:47:38 | [diff] [blame] | 309 | |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 310 | static SpdySessionKey* single_domain_key = NULL; |
| 311 | if (!single_domain_key) { |
[email protected] | ea1cc2cd | 2011-02-22 16:47:38 | [diff] [blame] | 312 | HostPortPair single_domain = HostPortPair("singledomain.com", 80); |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 313 | single_domain_key = new SpdySessionKey(single_domain, |
| 314 | ProxyServer::Direct(), |
| 315 | kPrivacyModeDisabled); |
[email protected] | ea1cc2cd | 2011-02-22 16:47:38 | [diff] [blame] | 316 | } |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 317 | return *single_domain_key; |
[email protected] | ea1cc2cd | 2011-02-22 16:47:38 | [diff] [blame] | 318 | } |
| 319 | |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 320 | void SpdySessionPool::MapKeyToAvailableSession( |
| 321 | const SpdySessionKey& key, |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame^] | 322 | const base::WeakPtr<SpdySession>& session) { |
| 323 | DCHECK(ContainsKey(sessions_, scoped_refptr<SpdySession>(session.get()))); |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 324 | const SpdySessionKey& normalized_key = NormalizeListKey(key); |
| 325 | std::pair<AvailableSessionMap::iterator, bool> result = |
| 326 | available_sessions_.insert(std::make_pair(normalized_key, session)); |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 327 | CHECK(result.second); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 328 | } |
| 329 | |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 330 | SpdySessionPool::AvailableSessionMap::iterator |
| 331 | SpdySessionPool::LookupAvailableSessionByKey( |
| 332 | const SpdySessionKey& key) { |
| 333 | const SpdySessionKey& normalized_key = NormalizeListKey(key); |
| 334 | return available_sessions_.find(normalized_key); |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 335 | } |
| 336 | |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 337 | void SpdySessionPool::UnmapKey(const SpdySessionKey& key) { |
| 338 | AvailableSessionMap::iterator it = LookupAvailableSessionByKey(key); |
| 339 | CHECK(it != available_sessions_.end()); |
| 340 | available_sessions_.erase(it); |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 341 | } |
| 342 | |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 343 | void SpdySessionPool::RemoveAliases(const SpdySessionKey& key) { |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 344 | // Walk the aliases map, find references to this pair. |
| 345 | // TODO(mbelshe): Figure out if this is too expensive. |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 346 | for (AliasMap::iterator it = aliases_.begin(); it != aliases_.end(); ) { |
| 347 | if (it->second.Equals(key)) { |
| 348 | AliasMap::iterator old_it = it; |
| 349 | ++it; |
| 350 | aliases_.erase(old_it); |
| 351 | } else { |
| 352 | ++it; |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 353 | } |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
| 357 | void SpdySessionPool::CloseCurrentSessionsHelper( |
| 358 | Error error, |
| 359 | const std::string& description, |
| 360 | bool idle_only) { |
| 361 | SessionSet current_sessions = sessions_; |
| 362 | for (SessionSet::const_iterator it = current_sessions.begin(); |
| 363 | it != current_sessions.end(); ++it) { |
| 364 | if (!ContainsKey(sessions_, *it)) |
| 365 | continue; |
| 366 | |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 367 | if (idle_only && (*it)->is_active()) |
| 368 | continue; |
| 369 | |
| 370 | (*it)->CloseSessionOnError(error, description); |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame^] | 371 | DCHECK(!IsSessionAvailable((*it)->GetWeakPtr())); |
[email protected] | 04644d4 | 2013-07-08 04:29:59 | [diff] [blame] | 372 | DCHECK(!ContainsKey(sessions_, *it)); |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 373 | } |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 374 | } |
| 375 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 376 | } // namespace net |