OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/ssl/ssl_client_session_cache_openssl.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/time/clock.h" |
| 11 #include "base/time/default_clock.h" |
| 12 |
| 13 namespace net { |
| 14 |
| 15 SSLClientSessionCacheOpenSSL::SSLClientSessionCacheOpenSSL(const Config& config) |
| 16 : clock_(new base::DefaultClock), |
| 17 config_(config), |
| 18 cache_(config.max_entries), |
| 19 lookups_since_flush_(0) { |
| 20 } |
| 21 |
| 22 SSLClientSessionCacheOpenSSL::~SSLClientSessionCacheOpenSSL() { |
| 23 // TODO(davidben): The session cache is currently a singleton, so it is |
| 24 // destroyed on a different thread than the one it's created on. When |
| 25 // https://crbug.com/458365 is fixed, this will no longer be an issue. |
| 26 thread_checker_.DetachFromThread(); |
| 27 |
| 28 Flush(); |
| 29 } |
| 30 |
| 31 size_t SSLClientSessionCacheOpenSSL::size() const { |
| 32 return cache_.size(); |
| 33 } |
| 34 |
| 35 SSL_SESSION* SSLClientSessionCacheOpenSSL::Lookup( |
| 36 const std::string& cache_key) { |
| 37 DCHECK(thread_checker_.CalledOnValidThread()); |
| 38 |
| 39 // Expire stale sessions. |
| 40 lookups_since_flush_++; |
| 41 if (lookups_since_flush_ >= config_.expiration_check_count) { |
| 42 lookups_since_flush_ = 0; |
| 43 FlushExpiredSessions(); |
| 44 } |
| 45 |
| 46 CacheEntryMap::iterator iter = cache_.Get(cache_key); |
| 47 if (iter == cache_.end()) |
| 48 return nullptr; |
| 49 return iter->second->session.get(); |
| 50 } |
| 51 |
| 52 void SSLClientSessionCacheOpenSSL::Insert(const std::string& cache_key, |
| 53 SSL_SESSION* session) { |
| 54 DCHECK(thread_checker_.CalledOnValidThread()); |
| 55 |
| 56 // Make a new entry. |
| 57 CacheEntry* entry = new CacheEntry; |
| 58 entry->session.reset(SSL_SESSION_up_ref(session)); |
| 59 entry->expiration = clock_->Now() + config_.timeout; |
| 60 |
| 61 // Takes ownership. |
| 62 cache_.Put(cache_key, entry); |
| 63 } |
| 64 |
| 65 void SSLClientSessionCacheOpenSSL::Flush() { |
| 66 DCHECK(thread_checker_.CalledOnValidThread()); |
| 67 |
| 68 cache_.Clear(); |
| 69 } |
| 70 |
| 71 void SSLClientSessionCacheOpenSSL::SetClockForTesting( |
| 72 scoped_ptr<base::Clock> clock) { |
| 73 DCHECK(thread_checker_.CalledOnValidThread()); |
| 74 |
| 75 clock_ = clock.Pass(); |
| 76 } |
| 77 |
| 78 SSLClientSessionCacheOpenSSL::CacheEntry::CacheEntry() { |
| 79 } |
| 80 |
| 81 SSLClientSessionCacheOpenSSL::CacheEntry::~CacheEntry() { |
| 82 } |
| 83 |
| 84 void SSLClientSessionCacheOpenSSL::FlushExpiredSessions() { |
| 85 base::Time now = clock_->Now(); |
| 86 CacheEntryMap::iterator iter = cache_.begin(); |
| 87 while (iter != cache_.end()) { |
| 88 if (now > iter->second->expiration) { |
| 89 iter = cache_.Erase(iter); |
| 90 } else { |
| 91 ++iter; |
| 92 } |
| 93 } |
| 94 } |
| 95 |
| 96 } // namespace net |
OLD | NEW |