summaryrefslogtreecommitdiffstats
path: root/net/spdy/spdy_session_pool.cc
blob: 5cfd62cfbfe0b85c877abae75e134c69aea6c985 (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
// 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() {}
SpdySessionPool::~SpdySessionPool() {
  CloseAllSessions();
}

scoped_refptr<SpdySession> SpdySessionPool::Get(
    const HostPortPair& host_port_pair, HttpNetworkSession* session) {
  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);

  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) {
  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));
  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());
  CHECK(list);
  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());
  return sessions_[host_port_pair] = new SpdySessionList();
}

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::CloseAllSessions() {
  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();
      session->CloseAllStreams(net::ERR_ABORTED);
    }
    delete list;
  }
}

}  // namespace net