This source file includes following definitions.
- trusted_spdy_proxy_
- CreateAvailableSessionFromSocket
- FindAvailableSession
- MakeSessionUnavailable
- RemoveUnavailableSession
- CloseCurrentSessions
- CloseCurrentIdleSessions
- CloseAllSessions
- SpdySessionPoolInfoToValue
- OnIPAddressChanged
- OnSSLConfigChanged
- OnCertAdded
- OnCACertChanged
- IsSessionAvailable
- NormalizeListKey
- MapKeyToAvailableSession
- LookupAvailableSessionByKey
- UnmapKey
- RemoveAliases
- GetCurrentSessions
- CloseCurrentSessionsHelper
#include "net/spdy/spdy_session_pool.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/values.h"
#include "net/base/address_list.h"
#include "net/http/http_network_session.h"
#include "net/http/http_server_properties.h"
#include "net/spdy/spdy_session.h"
namespace net {
namespace {
enum SpdySessionGetTypes {
CREATED_NEW = 0,
FOUND_EXISTING = 1,
FOUND_EXISTING_FROM_IP_POOL = 2,
IMPORTED_FROM_SOCKET = 3,
SPDY_SESSION_GET_MAX = 4
};
}
SpdySessionPool::SpdySessionPool(
HostResolver* resolver,
SSLConfigService* ssl_config_service,
const base::WeakPtr<HttpServerProperties>& http_server_properties,
bool force_single_domain,
bool enable_compression,
bool enable_ping_based_connection_checking,
NextProto default_protocol,
size_t stream_initial_recv_window_size,
size_t initial_max_concurrent_streams,
size_t max_concurrent_streams_limit,
SpdySessionPool::TimeFunc time_func,
const std::string& trusted_spdy_proxy)
: http_server_properties_(http_server_properties),
ssl_config_service_(ssl_config_service),
resolver_(resolver),
verify_domain_authentication_(true),
enable_sending_initial_data_(true),
force_single_domain_(force_single_domain),
enable_compression_(enable_compression),
enable_ping_based_connection_checking_(
enable_ping_based_connection_checking),
default_protocol_(
(default_protocol == kProtoUnknown) ?
kProtoSPDY3 : default_protocol),
stream_initial_recv_window_size_(stream_initial_recv_window_size),
initial_max_concurrent_streams_(initial_max_concurrent_streams),
max_concurrent_streams_limit_(max_concurrent_streams_limit),
time_func_(time_func),
trusted_spdy_proxy_(
HostPortPair::FromString(trusted_spdy_proxy)) {
DCHECK(default_protocol_ >= kProtoSPDYMinimumVersion &&
default_protocol_ <= kProtoSPDYMaximumVersion);
NetworkChangeNotifier::AddIPAddressObserver(this);
if (ssl_config_service_.get())
ssl_config_service_->AddObserver(this);
CertDatabase::GetInstance()->AddObserver(this);
}
SpdySessionPool::~SpdySessionPool() {
CloseAllSessions();
if (ssl_config_service_.get())
ssl_config_service_->RemoveObserver(this);
NetworkChangeNotifier::RemoveIPAddressObserver(this);
CertDatabase::GetInstance()->RemoveObserver(this);
}
base::WeakPtr<SpdySession> SpdySessionPool::CreateAvailableSessionFromSocket(
const SpdySessionKey& key,
scoped_ptr<ClientSocketHandle> connection,
const BoundNetLog& net_log,
int certificate_error_code,
bool is_secure) {
DCHECK_GE(default_protocol_, kProtoSPDYMinimumVersion);
DCHECK_LE(default_protocol_, kProtoSPDYMaximumVersion);
UMA_HISTOGRAM_ENUMERATION(
"Net.SpdySessionGet", IMPORTED_FROM_SOCKET, SPDY_SESSION_GET_MAX);
scoped_ptr<SpdySession> new_session(
new SpdySession(key,
http_server_properties_,
verify_domain_authentication_,
enable_sending_initial_data_,
enable_compression_,
enable_ping_based_connection_checking_,
default_protocol_,
stream_initial_recv_window_size_,
initial_max_concurrent_streams_,
max_concurrent_streams_limit_,
time_func_,
trusted_spdy_proxy_,
net_log.net_log()));
new_session->InitializeWithSocket(
connection.Pass(), this, is_secure, certificate_error_code);
base::WeakPtr<SpdySession> available_session = new_session->GetWeakPtr();
sessions_.insert(new_session.release());
MapKeyToAvailableSession(key, available_session);
net_log.AddEvent(
NetLog::TYPE_SPDY_SESSION_POOL_IMPORTED_SESSION_FROM_SOCKET,
available_session->net_log().source().ToEventParametersCallback());
if (key.proxy_server().is_direct()) {
IPEndPoint address;
if (available_session->GetPeerAddress(&address) == OK)
aliases_[address] = key;
}
return available_session;
}
base::WeakPtr<SpdySession> SpdySessionPool::FindAvailableSession(
const SpdySessionKey& key,
const BoundNetLog& net_log) {
AvailableSessionMap::iterator it = LookupAvailableSessionByKey(key);
if (it != available_sessions_.end()) {
UMA_HISTOGRAM_ENUMERATION(
"Net.SpdySessionGet", FOUND_EXISTING, SPDY_SESSION_GET_MAX);
net_log.AddEvent(
NetLog::TYPE_SPDY_SESSION_POOL_FOUND_EXISTING_SESSION,
it->second->net_log().source().ToEventParametersCallback());
return it->second;
}
net::HostResolver::RequestInfo resolve_info(key.host_port_pair());
AddressList addresses;
int rv = resolver_->ResolveFromCache(resolve_info, &addresses, net_log);
DCHECK_NE(rv, ERR_IO_PENDING);
if (rv != OK)
return base::WeakPtr<SpdySession>();
for (AddressList::const_iterator address_it = addresses.begin();
address_it != addresses.end();
++address_it) {
AliasMap::const_iterator alias_it = aliases_.find(*address_it);
if (alias_it == aliases_.end())
continue;
const SpdySessionKey& alias_key = alias_it->second;
if (!(alias_key.proxy_server() == key.proxy_server()) ||
!(alias_key.privacy_mode() == key.privacy_mode()))
continue;
AvailableSessionMap::iterator available_session_it =
LookupAvailableSessionByKey(alias_key);
if (available_session_it == available_sessions_.end()) {
NOTREACHED();
continue;
}
const base::WeakPtr<SpdySession>& available_session =
available_session_it->second;
DCHECK(ContainsKey(sessions_, available_session.get()));
if (!available_session->VerifyDomainAuthentication(
key.host_port_pair().host())) {
UMA_HISTOGRAM_ENUMERATION("Net.SpdyIPPoolDomainMatch", 0, 2);
continue;
}
UMA_HISTOGRAM_ENUMERATION("Net.SpdyIPPoolDomainMatch", 1, 2);
UMA_HISTOGRAM_ENUMERATION("Net.SpdySessionGet",
FOUND_EXISTING_FROM_IP_POOL,
SPDY_SESSION_GET_MAX);
net_log.AddEvent(
NetLog::TYPE_SPDY_SESSION_POOL_FOUND_EXISTING_SESSION_FROM_IP_POOL,
available_session->net_log().source().ToEventParametersCallback());
MapKeyToAvailableSession(key, available_session);
available_session->AddPooledAlias(key);
return available_session;
}
return base::WeakPtr<SpdySession>();
}
void SpdySessionPool::MakeSessionUnavailable(
const base::WeakPtr<SpdySession>& available_session) {
UnmapKey(available_session->spdy_session_key());
RemoveAliases(available_session->spdy_session_key());
const std::set<SpdySessionKey>& aliases = available_session->pooled_aliases();
for (std::set<SpdySessionKey>::const_iterator it = aliases.begin();
it != aliases.end(); ++it) {
UnmapKey(*it);
RemoveAliases(*it);
}
DCHECK(!IsSessionAvailable(available_session));
}
void SpdySessionPool::RemoveUnavailableSession(
const base::WeakPtr<SpdySession>& unavailable_session) {
DCHECK(!IsSessionAvailable(unavailable_session));
unavailable_session->net_log().AddEvent(
NetLog::TYPE_SPDY_SESSION_POOL_REMOVE_SESSION,
unavailable_session->net_log().source().ToEventParametersCallback());
SessionSet::iterator it = sessions_.find(unavailable_session.get());
CHECK(it != sessions_.end());
scoped_ptr<SpdySession> owned_session(*it);
sessions_.erase(it);
}
void SpdySessionPool::CloseCurrentSessions(net::Error error) {
CloseCurrentSessionsHelper(error, "Closing current sessions.",
false );
}
void SpdySessionPool::CloseCurrentIdleSessions() {
CloseCurrentSessionsHelper(ERR_ABORTED, "Closing idle sessions.",
true );
}
void SpdySessionPool::CloseAllSessions() {
while (!sessions_.empty()) {
CloseCurrentSessionsHelper(ERR_ABORTED, "Closing all sessions.",
false );
}
}
base::Value* SpdySessionPool::SpdySessionPoolInfoToValue() const {
base::ListValue* list = new base::ListValue();
for (AvailableSessionMap::const_iterator it = available_sessions_.begin();
it != available_sessions_.end(); ++it) {
const SpdySessionKey& key = it->first;
const SpdySessionKey& session_key = it->second->spdy_session_key();
if (key.Equals(session_key))
list->Append(it->second->GetInfoAsValue());
}
return list;
}
void SpdySessionPool::OnIPAddressChanged() {
WeakSessionList current_sessions = GetCurrentSessions();
for (WeakSessionList::const_iterator it = current_sessions.begin();
it != current_sessions.end(); ++it) {
if (!*it)
continue;
#if defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_IOS)
(*it)->MakeUnavailable();
#else
(*it)->CloseSessionOnError(ERR_NETWORK_CHANGED,
"Closing current sessions.");
DCHECK(!*it);
#endif
DCHECK(!IsSessionAvailable(*it));
}
http_server_properties_->ClearAllSpdySettings();
}
void SpdySessionPool::OnSSLConfigChanged() {
CloseCurrentSessions(ERR_NETWORK_CHANGED);
}
void SpdySessionPool::OnCertAdded(const X509Certificate* cert) {
CloseCurrentSessions(ERR_CERT_DATABASE_CHANGED);
}
void SpdySessionPool::OnCACertChanged(const X509Certificate* cert) {
CloseCurrentSessions(ERR_CERT_DATABASE_CHANGED);
}
bool SpdySessionPool::IsSessionAvailable(
const base::WeakPtr<SpdySession>& session) const {
for (AvailableSessionMap::const_iterator it = available_sessions_.begin();
it != available_sessions_.end(); ++it) {
if (it->second.get() == session.get())
return true;
}
return false;
}
const SpdySessionKey& SpdySessionPool::NormalizeListKey(
const SpdySessionKey& key) const {
if (!force_single_domain_)
return key;
static SpdySessionKey* single_domain_key = NULL;
if (!single_domain_key) {
HostPortPair single_domain = HostPortPair("singledomain.com", 80);
single_domain_key = new SpdySessionKey(single_domain,
ProxyServer::Direct(),
PRIVACY_MODE_DISABLED);
}
return *single_domain_key;
}
void SpdySessionPool::MapKeyToAvailableSession(
const SpdySessionKey& key,
const base::WeakPtr<SpdySession>& session) {
DCHECK(ContainsKey(sessions_, session.get()));
const SpdySessionKey& normalized_key = NormalizeListKey(key);
std::pair<AvailableSessionMap::iterator, bool> result =
available_sessions_.insert(std::make_pair(normalized_key, session));
CHECK(result.second);
}
SpdySessionPool::AvailableSessionMap::iterator
SpdySessionPool::LookupAvailableSessionByKey(
const SpdySessionKey& key) {
const SpdySessionKey& normalized_key = NormalizeListKey(key);
return available_sessions_.find(normalized_key);
}
void SpdySessionPool::UnmapKey(const SpdySessionKey& key) {
AvailableSessionMap::iterator it = LookupAvailableSessionByKey(key);
CHECK(it != available_sessions_.end());
available_sessions_.erase(it);
}
void SpdySessionPool::RemoveAliases(const SpdySessionKey& key) {
for (AliasMap::iterator it = aliases_.begin(); it != aliases_.end(); ) {
if (it->second.Equals(key)) {
AliasMap::iterator old_it = it;
++it;
aliases_.erase(old_it);
} else {
++it;
}
}
}
SpdySessionPool::WeakSessionList SpdySessionPool::GetCurrentSessions() const {
WeakSessionList current_sessions;
for (SessionSet::const_iterator it = sessions_.begin();
it != sessions_.end(); ++it) {
current_sessions.push_back((*it)->GetWeakPtr());
}
return current_sessions;
}
void SpdySessionPool::CloseCurrentSessionsHelper(
Error error,
const std::string& description,
bool idle_only) {
WeakSessionList current_sessions = GetCurrentSessions();
for (WeakSessionList::const_iterator it = current_sessions.begin();
it != current_sessions.end(); ++it) {
if (!*it)
continue;
if (idle_only && (*it)->is_active())
continue;
(*it)->CloseSessionOnError(error, description);
DCHECK(!IsSessionAvailable(*it));
DCHECK(!*it);
}
}
}