Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / services / proxy_resolver / proxy_host_resolver_cache.h
1 // Copyright 2021 The Chromium Authors
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 SERVICES_PROXY_RESOLVER_PROXY_HOST_RESOLVER_CACHE_H_
6 #define SERVICES_PROXY_RESOLVER_PROXY_HOST_RESOLVER_CACHE_H_
7
8 #include <list>
9 #include <map>
10 #include <string>
11 #include <tuple>
12 #include <vector>
13
14 #include "base/time/time.h"
15 #include "net/base/ip_address.h"
16 #include "net/base/network_anonymization_key.h"
17
18 namespace proxy_resolver {
19
20 // Simple cache for proxy host resolutions. Maintains cached entries for up to
21 // `kTtl`, and evicts oldest entries when filled past capacity.
22 class ProxyHostResolverCache {
23  public:
24   static constexpr auto kTtl = base::Seconds(5);
25
26   explicit ProxyHostResolverCache(size_t max_entries = 500u);
27   ~ProxyHostResolverCache();
28   ProxyHostResolverCache(const ProxyHostResolverCache&) = delete;
29   ProxyHostResolverCache& operator=(const ProxyHostResolverCache&) = delete;
30
31   void StoreEntry(std::string hostname,
32                   net::NetworkAnonymizationKey network_anonymization_key,
33                   bool is_ex_operation,
34                   std::vector<net::IPAddress> results);
35
36   // Returns `nullptr` if entry not found or expired. Erases from cache if
37   // expired.
38   const std::vector<net::IPAddress>* LookupEntry(
39       std::string hostname,
40       net::NetworkAnonymizationKey network_anonymization_key,
41       bool is_ex_operation);
42
43   size_t GetSizeForTesting() const;
44
45  private:
46   struct Key {
47     bool operator<(const Key& other) const {
48       return std::tie(hostname, network_anonymization_key, is_ex_operation) <
49              std::tie(other.hostname, other.network_anonymization_key,
50                       other.is_ex_operation);
51     }
52
53     std::string hostname;
54     net::NetworkAnonymizationKey network_anonymization_key;
55     bool is_ex_operation;
56   };
57
58   using ExpirationList = std::list<const Key*>;
59
60   struct Entry {
61     Entry(std::vector<net::IPAddress> results,
62           base::TimeTicks expiration,
63           ExpirationList::iterator expiration_list_it);
64     ~Entry();
65     Entry(Entry&&);
66     Entry& operator=(Entry&&);
67
68     std::vector<net::IPAddress> results;
69     base::TimeTicks expiration;
70     ExpirationList::iterator expiration_list_it;
71   };
72
73   using EntryMap = std::map<Key, Entry>;
74
75   // Removes oldest entry iff `max_entries_` exceeded.
76   void RemoveOldestEntry();
77
78   size_t max_entries_;
79   EntryMap entries_;
80
81   // List of `const Key*`s in expiration order starting from the earliest to
82   // expire.
83   ExpirationList expiration_list_;
84 };
85
86 }  // namespace proxy_resolver
87
88 #endif  // SERVICES_PROXY_RESOLVER_PROXY_HOST_RESOLVER_CACHE_H_