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 <stddef.h> | 8 #include <stddef.h> |
| 9 #include <time.h> |
9 | 10 |
10 #include <memory> | 11 #include <memory> |
11 #include <string> | 12 #include <string> |
12 | 13 |
13 #include "base/bind.h" | 14 #include "base/bind.h" |
14 #include "base/containers/mru_cache.h" | 15 #include "base/containers/mru_cache.h" |
15 #include "base/macros.h" | 16 #include "base/macros.h" |
16 #include "base/memory/memory_coordinator_client.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 "third_party/boringssl/src/include/openssl/base.h" | 23 #include "third_party/boringssl/src/include/openssl/base.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 : public base::MemoryCoordinatorClient { | 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 base::TimeDelta timeout = base::TimeDelta::FromHours(1); | |
39 }; | 38 }; |
40 | 39 |
41 explicit SSLClientSessionCache(const Config& config); | 40 explicit SSLClientSessionCache(const Config& config); |
42 ~SSLClientSessionCache() override; | 41 ~SSLClientSessionCache() override; |
43 | 42 |
44 size_t size() const; | 43 size_t size() const; |
45 | 44 |
46 // Returns the session associated with |cache_key| and moves it to the front | 45 // Returns the session associated with |cache_key| and moves it to the front |
47 // of the MRU list. Returns nullptr if there is none. | 46 // of the MRU list. Returns nullptr if there is none. |
48 bssl::UniquePtr<SSL_SESSION> Lookup(const std::string& cache_key); | 47 bssl::UniquePtr<SSL_SESSION> Lookup(const std::string& cache_key); |
49 | 48 |
50 // Inserts |session| into the cache at |cache_key|. If there is an existing | 49 // 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 | 50 // one, it is released. Every |expiration_check_count| calls, the cache is |
52 // checked for stale entries. | 51 // checked for stale entries. |
53 void Insert(const std::string& cache_key, SSL_SESSION* session); | 52 void Insert(const std::string& cache_key, SSL_SESSION* session); |
54 | 53 |
55 // Removes all entries from the cache. | 54 // Removes all entries from the cache. |
56 void Flush(); | 55 void Flush(); |
57 | 56 |
58 void SetClockForTesting(std::unique_ptr<base::Clock> clock); | 57 void SetClockForTesting(std::unique_ptr<base::Clock> clock); |
59 | 58 |
60 private: | 59 private: |
61 struct CacheEntry { | |
62 CacheEntry(); | |
63 ~CacheEntry(); | |
64 | |
65 bssl::UniquePtr<SSL_SESSION> session; | |
66 // The time at which this entry was created. | |
67 base::Time creation_time; | |
68 }; | |
69 | |
70 using CacheEntryMap = | |
71 base::HashingMRUCache<std::string, std::unique_ptr<CacheEntry>>; | |
72 | |
73 // base::MemoryCoordinatorClient implementation: | 60 // base::MemoryCoordinatorClient implementation: |
74 void OnMemoryStateChange(base::MemoryState state) override; | 61 void OnMemoryStateChange(base::MemoryState state) override; |
75 | 62 |
76 // Returns true if |entry| is expired as of |now|. | 63 // Returns true if |entry| is expired as of |now|. |
77 bool IsExpired(CacheEntry* entry, const base::Time& now); | 64 bool IsExpired(SSL_SESSION* session, time_t now); |
78 | 65 |
79 // Removes all expired sessions from the cache. | 66 // Removes all expired sessions from the cache. |
80 void FlushExpiredSessions(); | 67 void FlushExpiredSessions(); |
81 | 68 |
82 // Clear cache on low memory notifications callback. | 69 // Clear cache on low memory notifications callback. |
83 void OnMemoryPressure( | 70 void OnMemoryPressure( |
84 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); | 71 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); |
85 | 72 |
86 std::unique_ptr<base::Clock> clock_; | 73 std::unique_ptr<base::Clock> clock_; |
87 Config config_; | 74 Config config_; |
88 CacheEntryMap cache_; | 75 base::HashingMRUCache<std::string, bssl::UniquePtr<SSL_SESSION>> cache_; |
89 size_t lookups_since_flush_; | 76 size_t lookups_since_flush_; |
90 | 77 |
91 // TODO(davidben): After https://crbug.com/458365 is fixed, replace this with | 78 // TODO(davidben): After https://crbug.com/458365 is fixed, replace this with |
92 // a ThreadChecker. The session cache should be single-threaded like other | 79 // a ThreadChecker. The session cache should be single-threaded like other |
93 // classes in net. | 80 // classes in net. |
94 base::Lock lock_; | 81 base::Lock lock_; |
95 | 82 |
96 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; | 83 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; |
97 | 84 |
98 DISALLOW_COPY_AND_ASSIGN(SSLClientSessionCache); | 85 DISALLOW_COPY_AND_ASSIGN(SSLClientSessionCache); |
99 }; | 86 }; |
100 | 87 |
101 } // namespace net | 88 } // namespace net |
102 | 89 |
103 #endif // NET_SSL_SSL_CLIENT_SESSION_CACHE_H | 90 #endif // NET_SSL_SSL_CLIENT_SESSION_CACHE_H |
OLD | NEW |