OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/flip/flip_session_pool.h" | 5 #include "net/flip/flip_session_pool.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "net/flip/flip_session.h" | 8 #include "net/flip/flip_session.h" |
9 | 9 |
10 namespace net { | 10 namespace net { |
11 | 11 |
12 // The maximum number of sessions to open to a single domain. | 12 // The maximum number of sessions to open to a single domain. |
13 const size_t kMaxSessionsPerDomain = 1; | 13 static const size_t kMaxSessionsPerDomain = 1; |
14 | 14 |
15 scoped_ptr<FlipSessionPool::FlipSessionsMap> FlipSessionPool::sessions_; | 15 FlipSessionPool::FlipSessionPool() {} |
| 16 FlipSessionPool::~FlipSessionPool() { |
| 17 CloseAllSessions(); |
| 18 } |
16 | 19 |
17 FlipSession* FlipSessionPool::Get(const HostResolver::RequestInfo& info, | 20 FlipSession* FlipSessionPool::Get(const HostResolver::RequestInfo& info, |
18 HttpNetworkSession* session) { | 21 HttpNetworkSession* session) { |
19 if (!sessions_.get()) | 22 const std::string& domain = info.hostname(); |
20 sessions_.reset(new FlipSessionsMap()); | |
21 | |
22 const std::string domain = info.hostname(); | |
23 FlipSession* flip_session = NULL; | 23 FlipSession* flip_session = NULL; |
24 FlipSessionList* list = GetSessionList(domain); | 24 FlipSessionList* list = GetSessionList(domain); |
25 if (list) { | 25 if (list) { |
26 if (list->size() >= kMaxSessionsPerDomain) { | 26 if (list->size() >= kMaxSessionsPerDomain) { |
27 flip_session = list->front(); | 27 flip_session = list->front(); |
28 list->pop_front(); | 28 list->pop_front(); |
29 } | 29 } |
30 } else { | 30 } else { |
31 list = AddSessionList(domain); | 31 list = AddSessionList(domain); |
32 } | 32 } |
33 | 33 |
34 DCHECK(list); | 34 DCHECK(list); |
35 if (!flip_session) { | 35 if (!flip_session) { |
36 flip_session = new FlipSession(domain, session); | 36 flip_session = new FlipSession(domain, session); |
37 flip_session->AddRef(); // Keep it in the cache. | 37 flip_session->AddRef(); // Keep it in the cache. |
38 } | 38 } |
39 | 39 |
40 DCHECK(flip_session); | 40 DCHECK(flip_session); |
41 list->push_back(flip_session); | 41 list->push_back(flip_session); |
42 DCHECK(list->size() <= kMaxSessionsPerDomain); | 42 DCHECK(list->size() <= kMaxSessionsPerDomain); |
43 return flip_session; | 43 return flip_session; |
44 } | 44 } |
45 | 45 |
| 46 bool FlipSessionPool::HasSession(const HostResolver::RequestInfo& info) const { |
| 47 const std::string& domain = info.hostname(); |
| 48 if (GetSessionList(domain)) |
| 49 return true; |
| 50 return false; |
| 51 } |
| 52 |
46 void FlipSessionPool::Remove(FlipSession* session) { | 53 void FlipSessionPool::Remove(FlipSession* session) { |
47 std::string domain = session->domain(); | 54 std::string domain = session->domain(); |
48 FlipSessionList* list = GetSessionList(domain); | 55 FlipSessionList* list = GetSessionList(domain); |
49 if (list == NULL) | 56 if (list == NULL) |
50 return; | 57 return; |
51 list->remove(session); | 58 list->remove(session); |
52 if (!list->size()) | 59 if (list->empty()) |
53 RemoveSessionList(domain); | 60 RemoveSessionList(domain); |
54 } | 61 } |
55 | 62 |
56 FlipSessionPool::FlipSessionList* | 63 FlipSessionPool::FlipSessionList* |
57 FlipSessionPool::AddSessionList(std::string domain) { | 64 FlipSessionPool::AddSessionList(const std::string& domain) { |
58 DCHECK(sessions_->find(domain) == sessions_->end()); | 65 DCHECK(sessions_.find(domain) == sessions_.end()); |
59 return (*sessions_)[domain] = new FlipSessionList(); | 66 return sessions_[domain] = new FlipSessionList(); |
60 } | 67 } |
61 | 68 |
62 // static | |
63 FlipSessionPool::FlipSessionList* | 69 FlipSessionPool::FlipSessionList* |
64 FlipSessionPool::GetSessionList(std::string domain) { | 70 FlipSessionPool::GetSessionList(const std::string& domain) { |
65 FlipSessionsMap::iterator it = sessions_->find(domain); | 71 FlipSessionsMap::iterator it = sessions_.find(domain); |
66 if (it == sessions_->end()) | 72 if (it == sessions_.end()) |
67 return NULL; | 73 return NULL; |
68 return it->second; | 74 return it->second; |
69 } | 75 } |
70 | 76 |
71 // static | 77 const FlipSessionPool::FlipSessionList* |
72 void FlipSessionPool::RemoveSessionList(std::string domain) { | 78 FlipSessionPool::GetSessionList(const std::string& domain) const { |
| 79 FlipSessionsMap::const_iterator it = sessions_.find(domain); |
| 80 if (it == sessions_.end()) |
| 81 return NULL; |
| 82 return it->second; |
| 83 } |
| 84 |
| 85 void FlipSessionPool::RemoveSessionList(const std::string& domain) { |
73 FlipSessionList* list = GetSessionList(domain); | 86 FlipSessionList* list = GetSessionList(domain); |
74 if (list) { | 87 if (list) { |
75 delete list; | 88 delete list; |
76 sessions_->erase(domain); | 89 sessions_.erase(domain); |
77 } else { | 90 } else { |
78 DCHECK(false) << "removing orphaned session list"; | 91 DCHECK(false) << "removing orphaned session list"; |
79 } | 92 } |
80 } | 93 } |
81 | 94 |
82 // static | |
83 void FlipSessionPool::CloseAllSessions() { | 95 void FlipSessionPool::CloseAllSessions() { |
84 while (sessions_.get() && sessions_->size()) { | 96 while (sessions_.size()) { |
85 FlipSessionList* list = sessions_->begin()->second; | 97 FlipSessionList* list = sessions_.begin()->second; |
86 DCHECK(list); | 98 DCHECK(list); |
87 sessions_->erase(sessions_->begin()->first); | 99 sessions_.erase(sessions_.begin()->first); |
88 while (list->size()) { | 100 while (list->size()) { |
89 FlipSession* session = list->front(); | 101 FlipSession* session = list->front(); |
90 list->pop_front(); | 102 list->pop_front(); |
91 session->CloseAllStreams(net::OK); | 103 session->CloseAllStreams(net::OK); |
92 session->Release(); | 104 session->Release(); |
93 } | 105 } |
94 delete list; | 106 delete list; |
95 } | 107 } |
96 } | 108 } |
97 | 109 |
98 } // namespace net | 110 } // namespace net |
99 | |
OLD | NEW |