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 #ifndef NET_SSL_SSL_CLIENT_SESSION_CACHE_OPENSSL_H |
| 6 #define NET_SSL_SSL_CLIENT_SESSION_CACHE_OPENSSL_H |
| 7 |
| 8 #include "base/containers/mru_cache.h" |
| 9 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/threading/thread_checker.h" |
| 12 #include "base/time/time.h" |
| 13 #include "net/base/net_export.h" |
| 14 #include "net/ssl/scoped_openssl_types.h" |
| 15 |
| 16 namespace base { |
| 17 class Clock; |
| 18 } |
| 19 |
| 20 namespace net { |
| 21 |
| 22 class NET_EXPORT SSLClientSessionCacheOpenSSL { |
| 23 public: |
| 24 struct Config { |
| 25 // The maximum number of entries in the cache. |
| 26 size_t max_entries = 1024; |
| 27 // The number of calls to Lookup before a new check for expired sessions. |
| 28 size_t expiration_check_count = 256; |
| 29 // How long each session should last. |
| 30 base::TimeDelta timeout = base::TimeDelta::FromHours(1); |
| 31 }; |
| 32 |
| 33 explicit SSLClientSessionCacheOpenSSL(const Config& config); |
| 34 virtual ~SSLClientSessionCacheOpenSSL(); |
| 35 |
| 36 size_t size() const; |
| 37 |
| 38 // Returns the session associated with |cache_key| and moves it to the front |
| 39 // of the MRU list. Returns null if there is none. The caller is responsible |
| 40 // for taking a reference to the pointer if the cache is destroyed or a call |
| 41 // to Insert is made. |
| 42 SSL_SESSION* Lookup(const std::string& cache_key); |
| 43 |
| 44 // Inserts |session| into the cache at |cache_key|. If there is an existing |
| 45 // one, it is released. Every |expiration_check_count| calls, the cache is |
| 46 // checked for stale entries. |
| 47 void Insert(const std::string& cache_key, SSL_SESSION* session); |
| 48 |
| 49 // Removes all entries from the cache. |
| 50 void Flush(); |
| 51 |
| 52 void SetClockForTesting(scoped_ptr<base::Clock> clock); |
| 53 |
| 54 private: |
| 55 struct CacheEntry { |
| 56 CacheEntry(); |
| 57 ~CacheEntry(); |
| 58 |
| 59 ScopedSSL_SESSION session; |
| 60 // The time at which this entry expires. |
| 61 base::Time expiration; |
| 62 }; |
| 63 |
| 64 using CacheEntryMap = |
| 65 base::MRUCacheBase<std::string, |
| 66 CacheEntry*, |
| 67 base::MRUCachePointerDeletor<CacheEntry*>, |
| 68 base::MRUCacheHashMap>; |
| 69 |
| 70 // Removes all expired sessions from the cache. |
| 71 void FlushExpiredSessions(); |
| 72 |
| 73 scoped_ptr<base::Clock> clock_; |
| 74 Config config_; |
| 75 CacheEntryMap cache_; |
| 76 size_t lookups_since_flush_; |
| 77 |
| 78 base::ThreadChecker thread_checker_; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(SSLClientSessionCacheOpenSSL); |
| 81 }; |
| 82 |
| 83 } // namespace net |
| 84 |
| 85 #endif // NET_SSL_SSL_CLIENT_SESSION_CACHE_OPENSSL_H |
OLD | NEW |