| 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/ssl.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_pressure_monitor.h" | 18 #include "base/memory/memory_pressure_monitor.h" |
| 18 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
| 19 #include "base/threading/thread_checker.h" | 20 #include "base/threading/thread_checker.h" |
| 20 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 21 #include "net/base/net_export.h" | 22 #include "net/base/net_export.h" |
| 22 #include "net/ssl/scoped_openssl_types.h" | 23 #include "net/ssl/scoped_openssl_types.h" |
| 23 | 24 |
| 24 namespace base { | 25 namespace base { |
| 25 class Clock; | 26 class Clock; |
| 26 } | 27 } |
| 27 | 28 |
| 28 namespace net { | 29 namespace net { |
| 29 | 30 |
| 30 class NET_EXPORT SSLClientSessionCache { | 31 class NET_EXPORT SSLClientSessionCache : public base::MemoryCoordinatorClient { |
| 31 public: | 32 public: |
| 32 struct Config { | 33 struct Config { |
| 33 // The maximum number of entries in the cache. | 34 // The maximum number of entries in the cache. |
| 34 size_t max_entries = 1024; | 35 size_t max_entries = 1024; |
| 35 // The number of calls to Lookup before a new check for expired sessions. | 36 // The number of calls to Lookup before a new check for expired sessions. |
| 36 size_t expiration_check_count = 256; | 37 size_t expiration_check_count = 256; |
| 37 // How long each session should last. | 38 // How long each session should last. |
| 38 base::TimeDelta timeout = base::TimeDelta::FromHours(1); | 39 base::TimeDelta timeout = base::TimeDelta::FromHours(1); |
| 39 }; | 40 }; |
| 40 | 41 |
| 41 explicit SSLClientSessionCache(const Config& config); | 42 explicit SSLClientSessionCache(const Config& config); |
| 42 ~SSLClientSessionCache(); | 43 ~SSLClientSessionCache() override; |
| 43 | 44 |
| 44 size_t size() const; | 45 size_t size() const; |
| 45 | 46 |
| 46 // Returns the session associated with |cache_key| and moves it to the front | 47 // Returns the session associated with |cache_key| and moves it to the front |
| 47 // of the MRU list. Returns nullptr if there is none. | 48 // of the MRU list. Returns nullptr if there is none. |
| 48 ScopedSSL_SESSION Lookup(const std::string& cache_key); | 49 ScopedSSL_SESSION Lookup(const std::string& cache_key); |
| 49 | 50 |
| 50 // Inserts |session| into the cache at |cache_key|. If there is an existing | 51 // Inserts |session| into the cache at |cache_key|. If there is an existing |
| 51 // one, it is released. Every |expiration_check_count| calls, the cache is | 52 // one, it is released. Every |expiration_check_count| calls, the cache is |
| 52 // checked for stale entries. | 53 // checked for stale entries. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 63 ~CacheEntry(); | 64 ~CacheEntry(); |
| 64 | 65 |
| 65 ScopedSSL_SESSION session; | 66 ScopedSSL_SESSION session; |
| 66 // The time at which this entry was created. | 67 // The time at which this entry was created. |
| 67 base::Time creation_time; | 68 base::Time creation_time; |
| 68 }; | 69 }; |
| 69 | 70 |
| 70 using CacheEntryMap = | 71 using CacheEntryMap = |
| 71 base::HashingMRUCache<std::string, std::unique_ptr<CacheEntry>>; | 72 base::HashingMRUCache<std::string, std::unique_ptr<CacheEntry>>; |
| 72 | 73 |
| 74 // base::MemoryCoordinatorClient implementation: |
| 75 void OnMemoryStateChange(base::MemoryState state) override; |
| 76 |
| 73 // Returns true if |entry| is expired as of |now|. | 77 // Returns true if |entry| is expired as of |now|. |
| 74 bool IsExpired(CacheEntry* entry, const base::Time& now); | 78 bool IsExpired(CacheEntry* entry, const base::Time& now); |
| 75 | 79 |
| 76 // Removes all expired sessions from the cache. | 80 // Removes all expired sessions from the cache. |
| 77 void FlushExpiredSessions(); | 81 void FlushExpiredSessions(); |
| 78 | 82 |
| 79 // Clear cache on low memory notifications callback. | 83 // Clear cache on low memory notifications callback. |
| 80 void OnMemoryPressure( | 84 void OnMemoryPressure( |
| 81 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); | 85 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); |
| 82 | 86 |
| 83 std::unique_ptr<base::Clock> clock_; | 87 std::unique_ptr<base::Clock> clock_; |
| 84 Config config_; | 88 Config config_; |
| 85 CacheEntryMap cache_; | 89 CacheEntryMap cache_; |
| 86 size_t lookups_since_flush_; | 90 size_t lookups_since_flush_; |
| 87 | 91 |
| 88 // TODO(davidben): After https://crbug.com/458365 is fixed, replace this with | 92 // TODO(davidben): After https://crbug.com/458365 is fixed, replace this with |
| 89 // a ThreadChecker. The session cache should be single-threaded like other | 93 // a ThreadChecker. The session cache should be single-threaded like other |
| 90 // classes in net. | 94 // classes in net. |
| 91 base::Lock lock_; | 95 base::Lock lock_; |
| 92 | 96 |
| 93 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; | 97 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; |
| 94 | 98 |
| 95 DISALLOW_COPY_AND_ASSIGN(SSLClientSessionCache); | 99 DISALLOW_COPY_AND_ASSIGN(SSLClientSessionCache); |
| 96 }; | 100 }; |
| 97 | 101 |
| 98 } // namespace net | 102 } // namespace net |
| 99 | 103 |
| 100 #endif // NET_SSL_SSL_CLIENT_SESSION_CACHE_H | 104 #endif // NET_SSL_SSL_CLIENT_SESSION_CACHE_H |
| OLD | NEW |