Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_DNS_HOST_CACHE_H_ | 5 #ifndef NET_DNS_HOST_CACHE_H_ |
| 6 #define NET_DNS_HOST_CACHE_H_ | 6 #define NET_DNS_HOST_CACHE_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <functional> | 10 #include <functional> |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 57 return std::tie(address_family, host_resolver_flags, hostname) < | 57 return std::tie(address_family, host_resolver_flags, hostname) < |
| 58 std::tie(other.address_family, other.host_resolver_flags, | 58 std::tie(other.address_family, other.host_resolver_flags, |
| 59 other.hostname); | 59 other.hostname); |
| 60 } | 60 } |
| 61 | 61 |
| 62 std::string hostname; | 62 std::string hostname; |
| 63 AddressFamily address_family; | 63 AddressFamily address_family; |
| 64 HostResolverFlags host_resolver_flags; | 64 HostResolverFlags host_resolver_flags; |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 struct EvictionHandler { | 67 struct NET_EXPORT EntryStaleness { |
| 68 void Handle(const Key& key, | 68 // Time since the entry's TTL has expired. Negative if not expired. |
| 69 const Entry& entry, | 69 base::TimeDelta expired_by; |
| 70 const base::TimeTicks& expiration, | 70 // Number of network changes since this result was cached. |
| 71 const base::TimeTicks& now, | 71 int network_changes; |
| 72 bool onGet) const; | 72 // Number of hits to the cache entry while stale (expired or past-network). |
| 73 int stale_hits; | |
| 74 | |
| 75 bool is_stale() const { | |
| 76 return network_changes > 0 || expired_by >= base::TimeDelta(); | |
| 77 } | |
| 73 }; | 78 }; |
| 74 | 79 |
| 75 typedef ExpiringCache<Key, Entry, base::TimeTicks, | |
| 76 std::less<base::TimeTicks>, | |
| 77 EvictionHandler> EntryMap; | |
| 78 | |
| 79 // Constructs a HostCache that stores up to |max_entries|. | 80 // Constructs a HostCache that stores up to |max_entries|. |
| 80 explicit HostCache(size_t max_entries); | 81 explicit HostCache(size_t max_entries); |
| 81 | 82 |
| 82 ~HostCache(); | 83 ~HostCache(); |
| 83 | 84 |
| 84 // Returns a pointer to the entry for |key|, which is valid at time | 85 // Returns a pointer to the entry for |key|, which is valid at time |
| 85 // |now|. If there is no such entry, returns NULL. | 86 // |now|. If there is no such entry, returns NULL. |
| 86 const Entry* Lookup(const Key& key, base::TimeTicks now); | 87 const Entry* Lookup(const Key& key, base::TimeTicks now); |
| 87 | 88 |
| 89 // Returns a pointer to the entry for |key|, whether it is valid or stale at | |
| 90 // time |now|. Fills in |stale_out| with information about how stale it is. | |
| 91 // If there is no entry for |key| at all, returns NULL. | |
| 92 const Entry* LookupStale(const Key& key, | |
| 93 base::TimeTicks now, | |
| 94 EntryStaleness* stale_out); | |
| 95 | |
| 88 // Overwrites or creates an entry for |key|. | 96 // Overwrites or creates an entry for |key|. |
| 89 // |entry| is the value to set, |now| is the current time | 97 // |entry| is the value to set, |now| is the current time |
| 90 // |ttl| is the "time to live". | 98 // |ttl| is the "time to live". |
| 91 void Set(const Key& key, | 99 void Set(const Key& key, |
| 92 const Entry& entry, | 100 const Entry& entry, |
| 93 base::TimeTicks now, | 101 base::TimeTicks now, |
| 94 base::TimeDelta ttl); | 102 base::TimeDelta ttl); |
| 95 | 103 |
| 104 // Marks all entries as stale on account of a network change. | |
| 105 void OnNetworkChange(); | |
| 106 | |
| 96 // Empties the cache | 107 // Empties the cache |
| 97 void clear(); | 108 void clear(); |
| 98 | 109 |
| 99 // Returns the number of entries in the cache. | 110 // Returns the number of entries in the cache. |
| 100 size_t size() const; | 111 size_t size() const; |
| 101 | 112 |
| 102 // Following are used by net_internals UI. | 113 // Following are used by net_internals UI. |
| 103 size_t max_entries() const; | 114 size_t max_entries() const; |
| 104 | 115 |
| 105 const EntryMap& entries() const; | 116 std::unique_ptr<base::Value> GetEntriesAsValue() const; |
| 106 | 117 |
| 107 // Creates a default cache. | 118 // Creates a default cache. |
| 108 static std::unique_ptr<HostCache> CreateDefaultCache(); | 119 static std::unique_ptr<HostCache> CreateDefaultCache(); |
| 109 | 120 |
| 110 private: | 121 private: |
| 111 FRIEND_TEST_ALL_PREFIXES(HostCacheTest, NoCache); | 122 FRIEND_TEST_ALL_PREFIXES(HostCacheTest, NoCache); |
| 112 | 123 |
| 124 struct EntryInternal; | |
| 125 typedef std::map<Key, EntryInternal> EntryMap; | |
| 126 | |
| 127 EntryInternal* LookupInternal(const Key& key); | |
| 128 | |
| 129 enum SetOutcome : int; | |
| 130 enum LookupOutcome : int; | |
| 131 enum EraseReason : int; | |
|
Ryan Sleevi
2016/05/04 01:12:34
style: These enums should go up with 124/125 (as t
Julia Tuttle
2016/05/04 14:33:16
Done.
| |
| 132 void RecordSet(SetOutcome outcome, | |
| 133 base::TimeTicks now, | |
| 134 const EntryInternal* old_entry, | |
| 135 const Entry& new_entry); | |
| 136 void RecordUpdateStale(const EntryInternal& old_entry, | |
| 137 const Entry& new_entry, | |
| 138 base::TimeDelta expired_by, | |
| 139 int network_changes, | |
| 140 int stale_hits); | |
| 141 void RecordLookup(LookupOutcome outcome, | |
| 142 base::TimeTicks now, | |
| 143 const EntryInternal* entry); | |
| 144 void RecordErase(EraseReason reason, | |
| 145 base::TimeTicks now, | |
| 146 const EntryInternal& entry); | |
| 147 void RecordEraseAll(EraseReason reason, base::TimeTicks now); | |
| 148 | |
| 113 // Returns true if this HostCache can contain no entries. | 149 // Returns true if this HostCache can contain no entries. |
| 114 bool caching_is_disabled() const { | 150 bool caching_is_disabled() const { return max_entries_ == 0; } |
| 115 return entries_.max_entries() == 0; | 151 |
| 116 } | 152 void EvictOneEntry(base::TimeTicks now); |
| 117 | 153 |
| 118 // Map from hostname (presumably in lowercase canonicalized format) to | 154 // Map from hostname (presumably in lowercase canonicalized format) to |
| 119 // a resolved result entry. | 155 // a resolved result entry. |
| 120 EntryMap entries_; | 156 EntryMap entries_; |
| 157 size_t max_entries_; | |
| 158 int network_changes_; | |
| 121 | 159 |
| 122 DISALLOW_COPY_AND_ASSIGN(HostCache); | 160 DISALLOW_COPY_AND_ASSIGN(HostCache); |
| 123 }; | 161 }; |
| 124 | 162 |
| 125 } // namespace net | 163 } // namespace net |
| 126 | 164 |
| 127 #endif // NET_DNS_HOST_CACHE_H_ | 165 #endif // NET_DNS_HOST_CACHE_H_ |
| OLD | NEW |