Chromium Code Reviews| 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 | |
| 11 namespace { | |
| 12 const size_t kDefaultMaxEntries = 1024; | |
| 13 const size_t kDefaultExpirationCheckCount = 256; | |
| 14 const int kDefaultTimeoutSeconds = 60 * 60; | |
| 15 } | |
|
Ryan Sleevi
2015/03/17 00:50:33
} // namespace
davidben
2015/03/20 22:41:26
N/A with the in-line initialization.
| |
| 16 | |
| 17 namespace net { | |
| 18 | |
| 19 SSLClientSessionCacheOpenSSL::Config::Config() | |
| 20 : max_entries(kDefaultMaxEntries), | |
| 21 expiration_check_count(kDefaultExpirationCheckCount), | |
| 22 timeout(base::TimeDelta::FromSeconds(kDefaultTimeoutSeconds)) { | |
| 23 } | |
| 24 | |
| 25 SSLClientSessionCacheOpenSSL::SSLClientSessionCacheOpenSSL(const Config& config) | |
| 26 : config_(config), cache_(config.max_entries), lookups_since_flush_(0) { | |
| 27 } | |
| 28 | |
| 29 SSLClientSessionCacheOpenSSL::~SSLClientSessionCacheOpenSSL() { | |
| 30 // TODO(davidben): The session cache is currently a singleton, so it is | |
| 31 // destroyed on a different thread than the one it's created on. When | |
| 32 // https://crbug.com/458365 is fixed, this will no longer be an issue. | |
| 33 DetachFromThread(); | |
| 34 | |
| 35 Flush(); | |
| 36 } | |
| 37 | |
| 38 SSL_SESSION* SSLClientSessionCacheOpenSSL::Lookup( | |
| 39 const std::string& cache_key) { | |
| 40 DCHECK(CalledOnValidThread()); | |
| 41 | |
| 42 // Expire stale sessions. | |
| 43 lookups_since_flush_++; | |
| 44 if (lookups_since_flush_ >= config_.expiration_check_count) { | |
| 45 lookups_since_flush_ = 0; | |
| 46 FlushExpiredSessions(); | |
| 47 } | |
| 48 | |
| 49 CacheEntryMap::iterator iter = cache_.Get(cache_key); | |
| 50 if (iter == cache_.end()) | |
| 51 return nullptr; | |
| 52 return iter->second->session.get(); | |
| 53 } | |
| 54 | |
| 55 void SSLClientSessionCacheOpenSSL::Insert(const std::string& cache_key, | |
| 56 SSL_SESSION* session) { | |
| 57 DCHECK(CalledOnValidThread()); | |
| 58 | |
| 59 // Make a new entry. | |
| 60 CacheEntry* entry = new CacheEntry; | |
| 61 entry->session.reset(SSL_SESSION_up_ref(session)); | |
| 62 entry->expiration = Now() + config_.timeout; | |
| 63 | |
| 64 // Takes ownership. | |
| 65 cache_.Put(cache_key, entry); | |
| 66 } | |
| 67 | |
| 68 void SSLClientSessionCacheOpenSSL::Flush() { | |
| 69 DCHECK(CalledOnValidThread()); | |
| 70 | |
| 71 cache_.Clear(); | |
| 72 } | |
| 73 | |
| 74 base::Time SSLClientSessionCacheOpenSSL::Now() { | |
| 75 return base::Time::Now(); | |
| 76 } | |
| 77 | |
| 78 SSLClientSessionCacheOpenSSL::CacheEntry::CacheEntry() { | |
| 79 } | |
| 80 | |
| 81 SSLClientSessionCacheOpenSSL::CacheEntry::~CacheEntry() { | |
| 82 } | |
| 83 | |
| 84 void SSLClientSessionCacheOpenSSL::FlushExpiredSessions() { | |
| 85 base::Time now = 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 |