Chromium Code Reviews| Index: net/ssl/ssl_client_session_cache_openssl.cc |
| diff --git a/net/ssl/ssl_client_session_cache_openssl.cc b/net/ssl/ssl_client_session_cache_openssl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b34bd3dceee0ad1451ac166d0b9e62e768d90ef3 |
| --- /dev/null |
| +++ b/net/ssl/ssl_client_session_cache_openssl.cc |
| @@ -0,0 +1,96 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/ssl/ssl_client_session_cache_openssl.h" |
| + |
| +#include <utility> |
| + |
| +#include "base/logging.h" |
| + |
| +namespace { |
| +const size_t kDefaultMaxEntries = 1024; |
| +const size_t kDefaultExpirationCheckCount = 256; |
| +const int kDefaultTimeoutSeconds = 60 * 60; |
| +} |
|
Ryan Sleevi
2015/03/17 00:50:33
} // namespace
davidben
2015/03/20 22:41:26
N/A with the in-line initialization.
|
| + |
| +namespace net { |
| + |
| +SSLClientSessionCacheOpenSSL::Config::Config() |
| + : max_entries(kDefaultMaxEntries), |
| + expiration_check_count(kDefaultExpirationCheckCount), |
| + timeout(base::TimeDelta::FromSeconds(kDefaultTimeoutSeconds)) { |
| +} |
| + |
| +SSLClientSessionCacheOpenSSL::SSLClientSessionCacheOpenSSL(const Config& config) |
| + : config_(config), cache_(config.max_entries), lookups_since_flush_(0) { |
| +} |
| + |
| +SSLClientSessionCacheOpenSSL::~SSLClientSessionCacheOpenSSL() { |
| + // TODO(davidben): The session cache is currently a singleton, so it is |
| + // destroyed on a different thread than the one it's created on. When |
| + // https://crbug.com/458365 is fixed, this will no longer be an issue. |
| + DetachFromThread(); |
| + |
| + Flush(); |
| +} |
| + |
| +SSL_SESSION* SSLClientSessionCacheOpenSSL::Lookup( |
| + const std::string& cache_key) { |
| + DCHECK(CalledOnValidThread()); |
| + |
| + // Expire stale sessions. |
| + lookups_since_flush_++; |
| + if (lookups_since_flush_ >= config_.expiration_check_count) { |
| + lookups_since_flush_ = 0; |
| + FlushExpiredSessions(); |
| + } |
| + |
| + CacheEntryMap::iterator iter = cache_.Get(cache_key); |
| + if (iter == cache_.end()) |
| + return nullptr; |
| + return iter->second->session.get(); |
| +} |
| + |
| +void SSLClientSessionCacheOpenSSL::Insert(const std::string& cache_key, |
| + SSL_SESSION* session) { |
| + DCHECK(CalledOnValidThread()); |
| + |
| + // Make a new entry. |
| + CacheEntry* entry = new CacheEntry; |
| + entry->session.reset(SSL_SESSION_up_ref(session)); |
| + entry->expiration = Now() + config_.timeout; |
| + |
| + // Takes ownership. |
| + cache_.Put(cache_key, entry); |
| +} |
| + |
| +void SSLClientSessionCacheOpenSSL::Flush() { |
| + DCHECK(CalledOnValidThread()); |
| + |
| + cache_.Clear(); |
| +} |
| + |
| +base::Time SSLClientSessionCacheOpenSSL::Now() { |
| + return base::Time::Now(); |
| +} |
| + |
| +SSLClientSessionCacheOpenSSL::CacheEntry::CacheEntry() { |
| +} |
| + |
| +SSLClientSessionCacheOpenSSL::CacheEntry::~CacheEntry() { |
| +} |
| + |
| +void SSLClientSessionCacheOpenSSL::FlushExpiredSessions() { |
| + base::Time now = Now(); |
| + CacheEntryMap::iterator iter = cache_.begin(); |
| + while (iter != cache_.end()) { |
| + if (now > iter->second->expiration) { |
| + iter = cache_.Erase(iter); |
| + } else { |
| + ++iter; |
| + } |
| + } |
| +} |
| + |
| +} // namespace net |