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