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> |
| 11 #include <map> |
11 #include <memory> | 12 #include <memory> |
12 #include <string> | 13 #include <string> |
13 #include <tuple> | 14 #include <tuple> |
14 | 15 |
15 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
16 #include "base/macros.h" | 17 #include "base/macros.h" |
17 #include "base/threading/non_thread_safe.h" | 18 #include "base/threading/non_thread_safe.h" |
18 #include "base/time/time.h" | 19 #include "base/time/time.h" |
19 #include "net/base/address_family.h" | 20 #include "net/base/address_family.h" |
20 #include "net/base/address_list.h" | 21 #include "net/base/address_list.h" |
21 #include "net/base/expiring_cache.h" | 22 #include "net/base/expiring_cache.h" |
22 #include "net/base/net_export.h" | 23 #include "net/base/net_export.h" |
23 #include "net/dns/dns_util.h" | 24 #include "net/dns/dns_util.h" |
24 | 25 |
| 26 namespace base { |
| 27 class ListValue; |
| 28 } |
| 29 |
25 namespace net { | 30 namespace net { |
26 | 31 |
27 // Cache used by HostResolver to map hostnames to their resolved result. | 32 // Cache used by HostResolver to map hostnames to their resolved result. |
28 class NET_EXPORT HostCache : NON_EXPORTED_BASE(public base::NonThreadSafe) { | 33 class NET_EXPORT HostCache : NON_EXPORTED_BASE(public base::NonThreadSafe) { |
29 public: | 34 public: |
30 struct Key { | 35 struct Key { |
31 Key(const std::string& hostname, AddressFamily address_family, | 36 Key(const std::string& hostname, AddressFamily address_family, |
32 HostResolverFlags host_resolver_flags) | 37 HostResolverFlags host_resolver_flags) |
33 : hostname(hostname), | 38 : hostname(hostname), |
34 address_family(address_family), | 39 address_family(address_family), |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 int network_changes() const { return network_changes_; } | 88 int network_changes() const { return network_changes_; } |
84 | 89 |
85 private: | 90 private: |
86 friend class HostCache; | 91 friend class HostCache; |
87 | 92 |
88 Entry(const Entry& entry, | 93 Entry(const Entry& entry, |
89 base::TimeTicks now, | 94 base::TimeTicks now, |
90 base::TimeDelta ttl, | 95 base::TimeDelta ttl, |
91 int network_changes); | 96 int network_changes); |
92 | 97 |
| 98 Entry(int error, |
| 99 const AddressList& addresses, |
| 100 base::TimeTicks expires, |
| 101 int network_changes); |
| 102 |
93 int total_hits() const { return total_hits_; } | 103 int total_hits() const { return total_hits_; } |
94 int stale_hits() const { return stale_hits_; } | 104 int stale_hits() const { return stale_hits_; } |
95 | 105 |
96 bool IsStale(base::TimeTicks now, int network_changes) const; | 106 bool IsStale(base::TimeTicks now, int network_changes) const; |
97 void CountHit(bool hit_is_stale); | 107 void CountHit(bool hit_is_stale); |
98 void GetStaleness(base::TimeTicks now, | 108 void GetStaleness(base::TimeTicks now, |
99 int network_changes, | 109 int network_changes, |
100 EntryStaleness* out) const; | 110 EntryStaleness* out) const; |
101 | 111 |
102 // The resolve results for this entry. | 112 // The resolve results for this entry. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 eviction_callback_ = callback; | 158 eviction_callback_ = callback; |
149 } | 159 } |
150 | 160 |
151 // Empties the cache. | 161 // Empties the cache. |
152 void clear(); | 162 void clear(); |
153 | 163 |
154 // Clears hosts matching |host_filter| from the cache. | 164 // Clears hosts matching |host_filter| from the cache. |
155 void ClearForHosts( | 165 void ClearForHosts( |
156 const base::Callback<bool(const std::string&)>& host_filter); | 166 const base::Callback<bool(const std::string&)>& host_filter); |
157 | 167 |
| 168 // Returns the contents of the cache represented as a base::ListValue for |
| 169 // serialization. |
| 170 std::unique_ptr<base::ListValue> GetAsListValue(bool include_staleness) const; |
| 171 // Takes a base::ListValue representing cache entries and stores them in the |
| 172 // cache, skipping any that already have entries. Returns true on success, |
| 173 // false on failure. |
| 174 bool RestoreFromListValue(base::ListValue& old_cache); |
| 175 |
158 // Returns the number of entries in the cache. | 176 // Returns the number of entries in the cache. |
159 size_t size() const; | 177 size_t size() const; |
160 | 178 |
161 // Following are used by net_internals UI. | 179 // Following are used by net_internals UI. |
162 size_t max_entries() const; | 180 size_t max_entries() const; |
163 int network_changes() const { return network_changes_; } | 181 int network_changes() const { return network_changes_; } |
164 const EntryMap& entries() const { return entries_; } | 182 const EntryMap& entries() const { return entries_; } |
165 | 183 |
166 // Creates a default cache. | 184 // Creates a default cache. |
167 static std::unique_ptr<HostCache> CreateDefaultCache(); | 185 static std::unique_ptr<HostCache> CreateDefaultCache(); |
(...skipping 16 matching lines...) Expand all Loading... |
184 void RecordLookup(LookupOutcome outcome, | 202 void RecordLookup(LookupOutcome outcome, |
185 base::TimeTicks now, | 203 base::TimeTicks now, |
186 const Entry* entry); | 204 const Entry* entry); |
187 void RecordErase(EraseReason reason, base::TimeTicks now, const Entry& entry); | 205 void RecordErase(EraseReason reason, base::TimeTicks now, const Entry& entry); |
188 void RecordEraseAll(EraseReason reason, base::TimeTicks now); | 206 void RecordEraseAll(EraseReason reason, base::TimeTicks now); |
189 | 207 |
190 // Returns true if this HostCache can contain no entries. | 208 // Returns true if this HostCache can contain no entries. |
191 bool caching_is_disabled() const { return max_entries_ == 0; } | 209 bool caching_is_disabled() const { return max_entries_ == 0; } |
192 | 210 |
193 void EvictOneEntry(base::TimeTicks now); | 211 void EvictOneEntry(base::TimeTicks now); |
| 212 // Helper to insert an Entry into the cache. |
| 213 void AddEntry(const Key& key, const Entry& entry); |
194 | 214 |
195 // Map from hostname (presumably in lowercase canonicalized format) to | 215 // Map from hostname (presumably in lowercase canonicalized format) to |
196 // a resolved result entry. | 216 // a resolved result entry. |
197 EntryMap entries_; | 217 EntryMap entries_; |
198 size_t max_entries_; | 218 size_t max_entries_; |
199 int network_changes_; | 219 int network_changes_; |
200 EvictionCallback eviction_callback_; | 220 EvictionCallback eviction_callback_; |
201 | 221 |
202 DISALLOW_COPY_AND_ASSIGN(HostCache); | 222 DISALLOW_COPY_AND_ASSIGN(HostCache); |
203 }; | 223 }; |
204 | 224 |
205 } // namespace net | 225 } // namespace net |
206 | 226 |
207 #endif // NET_DNS_HOST_CACHE_H_ | 227 #endif // NET_DNS_HOST_CACHE_H_ |
OLD | NEW |