| 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_openssl.h" | 5 #include "net/ssl/ssl_client_session_cache_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/time/clock.h" | 9 #include "base/time/clock.h" |
| 10 #include "base/time/default_clock.h" | 10 #include "base/time/default_clock.h" |
| 11 | 11 |
| 12 namespace net { | 12 namespace net { |
| 13 | 13 |
| 14 SSLClientSessionCacheOpenSSL::SSLClientSessionCacheOpenSSL(const Config& config) | 14 SSLClientSessionCacheImpl::SSLClientSessionCacheImpl(const Config& config) |
| 15 : clock_(new base::DefaultClock), | 15 : clock_(new base::DefaultClock), |
| 16 config_(config), | 16 config_(config), |
| 17 cache_(config.max_entries), | 17 cache_(config.max_entries), |
| 18 lookups_since_flush_(0) { | 18 lookups_since_flush_(0) {} |
| 19 } | |
| 20 | 19 |
| 21 SSLClientSessionCacheOpenSSL::~SSLClientSessionCacheOpenSSL() { | 20 SSLClientSessionCacheImpl::~SSLClientSessionCacheImpl() { |
| 22 Flush(); | 21 Flush(); |
| 23 } | 22 } |
| 24 | 23 |
| 25 size_t SSLClientSessionCacheOpenSSL::size() const { | 24 size_t SSLClientSessionCacheImpl::size() const { |
| 26 return cache_.size(); | 25 return cache_.size(); |
| 27 } | 26 } |
| 28 | 27 |
| 29 ScopedSSL_SESSION SSLClientSessionCacheOpenSSL::Lookup( | 28 ScopedSSL_SESSION SSLClientSessionCacheImpl::Lookup( |
| 30 const std::string& cache_key) { | 29 const std::string& cache_key) { |
| 31 base::AutoLock lock(lock_); | 30 base::AutoLock lock(lock_); |
| 32 | 31 |
| 33 // Expire stale sessions. | 32 // Expire stale sessions. |
| 34 lookups_since_flush_++; | 33 lookups_since_flush_++; |
| 35 if (lookups_since_flush_ >= config_.expiration_check_count) { | 34 if (lookups_since_flush_ >= config_.expiration_check_count) { |
| 36 lookups_since_flush_ = 0; | 35 lookups_since_flush_ = 0; |
| 37 FlushExpiredSessions(); | 36 FlushExpiredSessions(); |
| 38 } | 37 } |
| 39 | 38 |
| 40 CacheEntryMap::iterator iter = cache_.Get(cache_key); | 39 CacheEntryMap::iterator iter = cache_.Get(cache_key); |
| 41 if (iter == cache_.end()) | 40 if (iter == cache_.end()) |
| 42 return nullptr; | 41 return nullptr; |
| 43 if (IsExpired(iter->second.get(), clock_->Now())) { | 42 if (IsExpired(iter->second.get(), clock_->Now())) { |
| 44 cache_.Erase(iter); | 43 cache_.Erase(iter); |
| 45 return nullptr; | 44 return nullptr; |
| 46 } | 45 } |
| 47 return ScopedSSL_SESSION(SSL_SESSION_up_ref(iter->second->session.get())); | 46 return ScopedSSL_SESSION(SSL_SESSION_up_ref(iter->second->session.get())); |
| 48 } | 47 } |
| 49 | 48 |
| 50 void SSLClientSessionCacheOpenSSL::Insert(const std::string& cache_key, | 49 void SSLClientSessionCacheImpl::Insert(const std::string& cache_key, |
| 51 SSL_SESSION* session) { | 50 SSL_SESSION* session) { |
| 52 base::AutoLock lock(lock_); | 51 base::AutoLock lock(lock_); |
| 53 | 52 |
| 54 // Make a new entry. | 53 // Make a new entry. |
| 55 std::unique_ptr<CacheEntry> entry(new CacheEntry); | 54 std::unique_ptr<CacheEntry> entry(new CacheEntry); |
| 56 entry->session.reset(SSL_SESSION_up_ref(session)); | 55 entry->session.reset(SSL_SESSION_up_ref(session)); |
| 57 entry->creation_time = clock_->Now(); | 56 entry->creation_time = clock_->Now(); |
| 58 | 57 |
| 59 // Takes ownership. | 58 // Takes ownership. |
| 60 cache_.Put(cache_key, std::move(entry)); | 59 cache_.Put(cache_key, std::move(entry)); |
| 61 } | 60 } |
| 62 | 61 |
| 63 void SSLClientSessionCacheOpenSSL::Flush() { | 62 void SSLClientSessionCacheImpl::Flush() { |
| 64 base::AutoLock lock(lock_); | 63 base::AutoLock lock(lock_); |
| 65 | 64 |
| 66 cache_.Clear(); | 65 cache_.Clear(); |
| 67 } | 66 } |
| 68 | 67 |
| 69 void SSLClientSessionCacheOpenSSL::SetClockForTesting( | 68 void SSLClientSessionCacheImpl::SetClockForTesting( |
| 70 std::unique_ptr<base::Clock> clock) { | 69 std::unique_ptr<base::Clock> clock) { |
| 71 clock_ = std::move(clock); | 70 clock_ = std::move(clock); |
| 72 } | 71 } |
| 73 | 72 |
| 74 SSLClientSessionCacheOpenSSL::CacheEntry::CacheEntry() { | 73 SSLClientSessionCacheImpl::CacheEntry::CacheEntry() {} |
| 75 } | |
| 76 | 74 |
| 77 SSLClientSessionCacheOpenSSL::CacheEntry::~CacheEntry() { | 75 SSLClientSessionCacheImpl::CacheEntry::~CacheEntry() {} |
| 78 } | |
| 79 | 76 |
| 80 bool SSLClientSessionCacheOpenSSL::IsExpired( | 77 bool SSLClientSessionCacheImpl::IsExpired( |
| 81 SSLClientSessionCacheOpenSSL::CacheEntry* entry, | 78 SSLClientSessionCacheImpl::CacheEntry* entry, |
| 82 const base::Time& now) { | 79 const base::Time& now) { |
| 83 return now < entry->creation_time || | 80 return now < entry->creation_time || |
| 84 entry->creation_time + config_.timeout < now; | 81 entry->creation_time + config_.timeout < now; |
| 85 } | 82 } |
| 86 | 83 |
| 87 void SSLClientSessionCacheOpenSSL::FlushExpiredSessions() { | 84 void SSLClientSessionCacheImpl::FlushExpiredSessions() { |
| 88 base::Time now = clock_->Now(); | 85 base::Time now = clock_->Now(); |
| 89 CacheEntryMap::iterator iter = cache_.begin(); | 86 CacheEntryMap::iterator iter = cache_.begin(); |
| 90 while (iter != cache_.end()) { | 87 while (iter != cache_.end()) { |
| 91 if (IsExpired(iter->second.get(), now)) { | 88 if (IsExpired(iter->second.get(), now)) { |
| 92 iter = cache_.Erase(iter); | 89 iter = cache_.Erase(iter); |
| 93 } else { | 90 } else { |
| 94 ++iter; | 91 ++iter; |
| 95 } | 92 } |
| 96 } | 93 } |
| 97 } | 94 } |
| 98 | 95 |
| 99 } // namespace net | 96 } // namespace net |
| OLD | NEW |