| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_SSL_SSL_CLIENT_SESSION_CACHE_H | 5 #ifndef NET_SSL_SSL_CLIENT_SESSION_CACHE_H |
| 6 #define NET_SSL_SSL_CLIENT_SESSION_CACHE_H | 6 #define NET_SSL_SSL_CLIENT_SESSION_CACHE_H |
| 7 | 7 |
| 8 #include <openssl/ssl.h> | 8 #include <openssl/base.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/containers/mru_cache.h" | 15 #include "base/containers/mru_cache.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/memory_coordinator_client.h" | 17 #include "base/memory/memory_coordinator_client.h" |
| 18 #include "base/memory/memory_pressure_monitor.h" | 18 #include "base/memory/memory_pressure_monitor.h" |
| 19 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
| 20 #include "base/threading/thread_checker.h" | 20 #include "base/threading/thread_checker.h" |
| 21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 22 #include "net/base/net_export.h" | 22 #include "net/base/net_export.h" |
| 23 #include "net/ssl/scoped_openssl_types.h" | |
| 24 | 23 |
| 25 namespace base { | 24 namespace base { |
| 26 class Clock; | 25 class Clock; |
| 27 } | 26 } |
| 28 | 27 |
| 29 namespace net { | 28 namespace net { |
| 30 | 29 |
| 31 class NET_EXPORT SSLClientSessionCache : public base::MemoryCoordinatorClient { | 30 class NET_EXPORT SSLClientSessionCache : public base::MemoryCoordinatorClient { |
| 32 public: | 31 public: |
| 33 struct Config { | 32 struct Config { |
| 34 // The maximum number of entries in the cache. | 33 // The maximum number of entries in the cache. |
| 35 size_t max_entries = 1024; | 34 size_t max_entries = 1024; |
| 36 // The number of calls to Lookup before a new check for expired sessions. | 35 // The number of calls to Lookup before a new check for expired sessions. |
| 37 size_t expiration_check_count = 256; | 36 size_t expiration_check_count = 256; |
| 38 // How long each session should last. | 37 // How long each session should last. |
| 39 base::TimeDelta timeout = base::TimeDelta::FromHours(1); | 38 base::TimeDelta timeout = base::TimeDelta::FromHours(1); |
| 40 }; | 39 }; |
| 41 | 40 |
| 42 explicit SSLClientSessionCache(const Config& config); | 41 explicit SSLClientSessionCache(const Config& config); |
| 43 ~SSLClientSessionCache() override; | 42 ~SSLClientSessionCache() override; |
| 44 | 43 |
| 45 size_t size() const; | 44 size_t size() const; |
| 46 | 45 |
| 47 // Returns the session associated with |cache_key| and moves it to the front | 46 // Returns the session associated with |cache_key| and moves it to the front |
| 48 // of the MRU list. Returns nullptr if there is none. | 47 // of the MRU list. Returns nullptr if there is none. |
| 49 ScopedSSL_SESSION Lookup(const std::string& cache_key); | 48 bssl::UniquePtr<SSL_SESSION> Lookup(const std::string& cache_key); |
| 50 | 49 |
| 51 // Inserts |session| into the cache at |cache_key|. If there is an existing | 50 // Inserts |session| into the cache at |cache_key|. If there is an existing |
| 52 // one, it is released. Every |expiration_check_count| calls, the cache is | 51 // one, it is released. Every |expiration_check_count| calls, the cache is |
| 53 // checked for stale entries. | 52 // checked for stale entries. |
| 54 void Insert(const std::string& cache_key, SSL_SESSION* session); | 53 void Insert(const std::string& cache_key, SSL_SESSION* session); |
| 55 | 54 |
| 56 // Removes all entries from the cache. | 55 // Removes all entries from the cache. |
| 57 void Flush(); | 56 void Flush(); |
| 58 | 57 |
| 59 void SetClockForTesting(std::unique_ptr<base::Clock> clock); | 58 void SetClockForTesting(std::unique_ptr<base::Clock> clock); |
| 60 | 59 |
| 61 private: | 60 private: |
| 62 struct CacheEntry { | 61 struct CacheEntry { |
| 63 CacheEntry(); | 62 CacheEntry(); |
| 64 ~CacheEntry(); | 63 ~CacheEntry(); |
| 65 | 64 |
| 66 ScopedSSL_SESSION session; | 65 bssl::UniquePtr<SSL_SESSION> session; |
| 67 // The time at which this entry was created. | 66 // The time at which this entry was created. |
| 68 base::Time creation_time; | 67 base::Time creation_time; |
| 69 }; | 68 }; |
| 70 | 69 |
| 71 using CacheEntryMap = | 70 using CacheEntryMap = |
| 72 base::HashingMRUCache<std::string, std::unique_ptr<CacheEntry>>; | 71 base::HashingMRUCache<std::string, std::unique_ptr<CacheEntry>>; |
| 73 | 72 |
| 74 // base::MemoryCoordinatorClient implementation: | 73 // base::MemoryCoordinatorClient implementation: |
| 75 void OnMemoryStateChange(base::MemoryState state) override; | 74 void OnMemoryStateChange(base::MemoryState state) override; |
| 76 | 75 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 95 base::Lock lock_; | 94 base::Lock lock_; |
| 96 | 95 |
| 97 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; | 96 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; |
| 98 | 97 |
| 99 DISALLOW_COPY_AND_ASSIGN(SSLClientSessionCache); | 98 DISALLOW_COPY_AND_ASSIGN(SSLClientSessionCache); |
| 100 }; | 99 }; |
| 101 | 100 |
| 102 } // namespace net | 101 } // namespace net |
| 103 | 102 |
| 104 #endif // NET_SSL_SSL_CLIENT_SESSION_CACHE_H | 103 #endif // NET_SSL_SSL_CLIENT_SESSION_CACHE_H |
| OLD | NEW |