summaryrefslogtreecommitdiffstats
path: root/net/spdy/spdy_session_pool.cc
blob: 40557f25a020726f8721b25e47065727c3017433 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/spdy/spdy_session_pool.h"

#include "base/logging.h"
#include "net/spdy/spdy_session.h"

namespace net {

// The maximum number of sessions to open to a single domain.
static const size_t kMaxSessionsPerDomain = 1;

int SpdySessionPool::g_max_sessions_per_domain = kMaxSessionsPerDomain;

SpdySessionPool::SpdySessionPool(NetworkChangeNotifier* notifier)
    : network_change_notifier_(notifier) {
  if (network_change_notifier_)
    network_change_notifier_->AddObserver(this);
}

SpdySessionPool::~SpdySessionPool() {
  CloseAllSessions();

  if (network_change_notifier_)
    network_change_notifier_->RemoveObserver(this);
}

scoped_refptr<SpdySession> SpdySessionPool::Get(
    const HostPortPair& host_port_pair, HttpNetworkSession* session,
    const BoundNetLog& net_log) {
  scoped_refptr<SpdySession> spdy_session;
  SpdySessionList* list = GetSessionList(host_port_pair);
  if (list) {
    if (list->size() >= static_cast<unsigned int>(g_max_sessions_per_domain)) {
      spdy_session = list->front();
      list->pop_front();
    }
  } else {
    list = AddSessionList(host_port_pair);
  }

  DCHECK(list);
  if (!spdy_session)
    spdy_session = new SpdySession(host_port_pair, session, net_log.net_log());

  DCHECK(spdy_session);
  list->push_back(spdy_session);
  DCHECK_LE(list->size(), static_cast<unsigned int>(g_max_sessions_per_domain));
  return spdy_session;
}

scoped_refptr<SpdySession> SpdySessionPool::GetSpdySessionFromSSLSocket(
    const HostPortPair& host_port_pair,
    HttpNetworkSession* session,
    ClientSocketHandle* connection,
    const BoundNetLog& net_log) {
  SpdySessionList* list = GetSessionList(host_port_pair);
  if (!list)
    list = AddSessionList(host_port_pair);
  DCHECK(list->empty());
  scoped_refptr<SpdySession> spdy_session(
      new SpdySession(host_port_pair, session, net_log.net_log()));
  spdy_session->InitializeWithSSLSocket(connection);
  list->push_back(spdy_session);
  return spdy_session;
}

bool SpdySessionPool::HasSession(const HostPortPair& host_port_pair) const {
  if (GetSessionList(host_port_pair))
    return true;
  return false;
}

void SpdySessionPool::Remove(const scoped_refptr<SpdySession>& session) {
  SpdySessionList* list = GetSessionList(session->host_port_pair());
  DCHECK(list);  // We really shouldn't remove if we've already been removed.
  if (!list)
    return;
  list->remove(session);
  if (list->empty())
    RemoveSessionList(session->host_port_pair());
}

SpdySessionPool::SpdySessionList*
    SpdySessionPool::AddSessionList(const HostPortPair& host_port_pair) {
  DCHECK(sessions_.find(host_port_pair) == sessions_.end());
  SpdySessionPool::SpdySessionList* list = new SpdySessionList();
  sessions_[host_port_pair] = list;
  return list;
}

SpdySessionPool::SpdySessionList*
    SpdySessionPool::GetSessionList(const HostPortPair& host_port_pair) {
  SpdySessionsMap::iterator it = sessions_.find(host_port_pair);
  if (it == sessions_.end())
    return NULL;
  return it->second;
}

const SpdySessionPool::SpdySessionList*
    SpdySessionPool::GetSessionList(const HostPortPair& host_port_pair) const {
  SpdySessionsMap::const_iterator it = sessions_.find(host_port_pair);
  if (it == sessions_.end())
    return NULL;
  return it->second;
}

void SpdySessionPool::RemoveSessionList(const HostPortPair& host_port_pair) {
  SpdySessionList* list = GetSessionList(host_port_pair);
  if (list) {
    delete list;
    sessions_.erase(host_port_pair);
  } else {
    DCHECK(false) << "removing orphaned session list";
  }
}

void SpdySessionPool::RemoveAllSessions(bool close) {
  while (sessions_.size()) {
    SpdySessionList* list = sessions_.begin()->second;
    DCHECK(list);
    sessions_.erase(sessions_.begin()->first);
    while (list->size()) {
      scoped_refptr<SpdySession> session = list->front();
      list->pop_front();
      if (close)
        session->CloseAllStreams(net::ERR_ABORTED);
    }
    delete list;
  }
}

}  // namespace net