#ifndef NET_SSL_SSL_CLIENT_SESSION_CACHE_H_
#define NET_SSL_SSL_CLIENT_SESSION_CACHE_H_
#include <stddef.h>
#include <time.h>
#include <memory>
#include <string>
#include "base/containers/lru_cache.h"
#include "base/functional/bind.h"
#include "base/memory/memory_pressure_monitor.h"
#include "base/memory/raw_ptr.h"
#include "net/base/host_port_pair.h"
#include "net/base/ip_address.h"
#include "net/base/net_export.h"
#include "net/base/network_anonymization_key.h"
#include "net/base/privacy_mode.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/boringssl/src/include/openssl/base.h"
namespace base {
class Clock;
}
namespace net {
class NET_EXPORT SSLClientSessionCache {
public:
struct Config {
size_t max_entries = 1024;
size_t expiration_check_count = 256;
};
struct NET_EXPORT Key {
Key();
Key(const Key& other);
Key(Key&& other);
~Key();
Key& operator=(const Key& other);
Key& operator=(Key&& other);
bool operator==(const Key& other) const;
bool operator<(const Key& other) const;
HostPortPair server;
absl::optional<IPAddress> dest_ip_addr;
NetworkAnonymizationKey network_anonymization_key;
PrivacyMode privacy_mode = PRIVACY_MODE_DISABLED;
bool disable_legacy_crypto = false;
};
explicit SSLClientSessionCache(const Config& config);
SSLClientSessionCache(const SSLClientSessionCache&) = delete;
SSLClientSessionCache& operator=(const SSLClientSessionCache&) = delete;
~SSLClientSessionCache();
static bool IsExpired(SSL_SESSION* session, time_t now);
size_t size() const;
bssl::UniquePtr<SSL_SESSION> Lookup(const Key& cache_key);
void Insert(const Key& cache_key, bssl::UniquePtr<SSL_SESSION> session);
void ClearEarlyData(const Key& cache_key);
void FlushForServer(const HostPortPair& server);
void Flush();
void SetClockForTesting(base::Clock* clock);
private:
struct Entry {
Entry();
Entry(Entry&&);
~Entry();
void Push(bssl::UniquePtr<SSL_SESSION> session);
bssl::UniquePtr<SSL_SESSION> Pop();
bool ExpireSessions(time_t now);
bssl::UniquePtr<SSL_SESSION> sessions[2];
};
void FlushExpiredSessions();
void OnMemoryPressure(
base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
raw_ptr<base::Clock> clock_;
Config config_;
base::LRUCache<Key, Entry> cache_;
size_t lookups_since_flush_ = 0;
std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_;
};
}
#endif