OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/ssl/ssl_client_session_cache.h" | 5 #include "net/ssl/ssl_client_session_cache.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/memory/memory_coordinator_client_registry.h" | 9 #include "base/memory/memory_coordinator_client_registry.h" |
10 #include "base/time/clock.h" | 10 #include "base/time/clock.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 const std::string& cache_key) { | 36 const std::string& cache_key) { |
37 base::AutoLock lock(lock_); | 37 base::AutoLock lock(lock_); |
38 | 38 |
39 // Expire stale sessions. | 39 // Expire stale sessions. |
40 lookups_since_flush_++; | 40 lookups_since_flush_++; |
41 if (lookups_since_flush_ >= config_.expiration_check_count) { | 41 if (lookups_since_flush_ >= config_.expiration_check_count) { |
42 lookups_since_flush_ = 0; | 42 lookups_since_flush_ = 0; |
43 FlushExpiredSessions(); | 43 FlushExpiredSessions(); |
44 } | 44 } |
45 | 45 |
46 CacheEntryMap::iterator iter = cache_.Get(cache_key); | 46 auto iter = cache_.Get(cache_key); |
47 if (iter == cache_.end()) | 47 if (iter == cache_.end()) |
48 return nullptr; | 48 return nullptr; |
49 if (IsExpired(iter->second.get(), clock_->Now())) { | 49 |
| 50 SSL_SESSION* session = iter->second.get(); |
| 51 if (IsExpired(session, clock_->Now().ToTimeT())) { |
50 cache_.Erase(iter); | 52 cache_.Erase(iter); |
51 return nullptr; | 53 return nullptr; |
52 } | 54 } |
53 | 55 |
54 SSL_SESSION* session = iter->second->session.get(); | |
55 SSL_SESSION_up_ref(session); | 56 SSL_SESSION_up_ref(session); |
56 return bssl::UniquePtr<SSL_SESSION>(session); | 57 return bssl::UniquePtr<SSL_SESSION>(session); |
57 } | 58 } |
58 | 59 |
59 void SSLClientSessionCache::Insert(const std::string& cache_key, | 60 void SSLClientSessionCache::Insert(const std::string& cache_key, |
60 SSL_SESSION* session) { | 61 SSL_SESSION* session) { |
61 base::AutoLock lock(lock_); | 62 base::AutoLock lock(lock_); |
62 | 63 |
63 // Make a new entry. | |
64 std::unique_ptr<CacheEntry> entry(new CacheEntry); | |
65 SSL_SESSION_up_ref(session); | 64 SSL_SESSION_up_ref(session); |
66 entry->session.reset(session); | 65 cache_.Put(cache_key, bssl::UniquePtr<SSL_SESSION>(session)); |
67 entry->creation_time = clock_->Now(); | |
68 | |
69 // Takes ownership. | |
70 cache_.Put(cache_key, std::move(entry)); | |
71 } | 66 } |
72 | 67 |
73 void SSLClientSessionCache::Flush() { | 68 void SSLClientSessionCache::Flush() { |
74 base::AutoLock lock(lock_); | 69 base::AutoLock lock(lock_); |
75 | 70 |
76 cache_.Clear(); | 71 cache_.Clear(); |
77 } | 72 } |
78 | 73 |
79 void SSLClientSessionCache::SetClockForTesting( | 74 void SSLClientSessionCache::SetClockForTesting( |
80 std::unique_ptr<base::Clock> clock) { | 75 std::unique_ptr<base::Clock> clock) { |
81 clock_ = std::move(clock); | 76 clock_ = std::move(clock); |
82 } | 77 } |
83 | 78 |
84 SSLClientSessionCache::CacheEntry::CacheEntry() {} | 79 bool SSLClientSessionCache::IsExpired(SSL_SESSION* session, time_t now) { |
85 | 80 return now < SSL_SESSION_get_time(session) || |
86 SSLClientSessionCache::CacheEntry::~CacheEntry() {} | 81 now >= |
87 | 82 SSL_SESSION_get_time(session) + SSL_SESSION_get_timeout(session); |
88 bool SSLClientSessionCache::IsExpired(SSLClientSessionCache::CacheEntry* entry, | |
89 const base::Time& now) { | |
90 return now < entry->creation_time || | |
91 entry->creation_time + config_.timeout < now; | |
92 } | 83 } |
93 | 84 |
94 void SSLClientSessionCache::FlushExpiredSessions() { | 85 void SSLClientSessionCache::FlushExpiredSessions() { |
95 base::Time now = clock_->Now(); | 86 time_t now = clock_->Now().ToTimeT(); |
96 CacheEntryMap::iterator iter = cache_.begin(); | 87 auto iter = cache_.begin(); |
97 while (iter != cache_.end()) { | 88 while (iter != cache_.end()) { |
98 if (IsExpired(iter->second.get(), now)) { | 89 if (IsExpired(iter->second.get(), now)) { |
99 iter = cache_.Erase(iter); | 90 iter = cache_.Erase(iter); |
100 } else { | 91 } else { |
101 ++iter; | 92 ++iter; |
102 } | 93 } |
103 } | 94 } |
104 } | 95 } |
105 | 96 |
106 void SSLClientSessionCache::OnMemoryPressure( | 97 void SSLClientSessionCache::OnMemoryPressure( |
(...skipping 22 matching lines...) Expand all Loading... |
129 break; | 120 break; |
130 case base::MemoryState::SUSPENDED: | 121 case base::MemoryState::SUSPENDED: |
131 // Note: Not supported at present. Fall through. | 122 // Note: Not supported at present. Fall through. |
132 case base::MemoryState::UNKNOWN: | 123 case base::MemoryState::UNKNOWN: |
133 NOTREACHED(); | 124 NOTREACHED(); |
134 break; | 125 break; |
135 } | 126 } |
136 } | 127 } |
137 | 128 |
138 } // namespace net | 129 } // namespace net |
OLD | NEW |