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 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 | 117 |
118 base::TimeTicks expires_; | 118 base::TimeTicks expires_; |
119 // Copied from the cache's network_changes_ when the entry is set; can0 | 119 // Copied from the cache's network_changes_ when the entry is set; can0 |
120 // later be compared to it to see if the entry was received on the current | 120 // later be compared to it to see if the entry was received on the current |
121 // network. | 121 // network. |
122 int network_changes_; | 122 int network_changes_; |
123 int total_hits_; | 123 int total_hits_; |
124 int stale_hits_; | 124 int stale_hits_; |
125 }; | 125 }; |
126 | 126 |
| 127 // Interface for interacting with persistent storage, to be provided by the |
| 128 // embedder. Does not include support for writes that must happen immediately. |
| 129 class PersistenceDelegate { |
| 130 public: |
| 131 // Calling ScheduleWrite() signals that data has changed and should be |
| 132 // written to persistent storage. The write might be delayed. |
| 133 virtual void ScheduleWrite() = 0; |
| 134 }; |
| 135 |
127 using EntryMap = std::map<Key, Entry>; | 136 using EntryMap = std::map<Key, Entry>; |
128 using EvictionCallback = base::Callback<void(const Key&, const Entry&)>; | 137 using EvictionCallback = base::Callback<void(const Key&, const Entry&)>; |
129 | 138 |
130 // Constructs a HostCache that stores up to |max_entries|. | 139 // Constructs a HostCache that stores up to |max_entries|. |
131 explicit HostCache(size_t max_entries); | 140 explicit HostCache(size_t max_entries); |
132 | 141 |
133 ~HostCache(); | 142 ~HostCache(); |
134 | 143 |
135 // Returns a pointer to the entry for |key|, which is valid at time | 144 // Returns a pointer to the entry for |key|, which is valid at time |
136 // |now|. If there is no such entry, returns NULL. | 145 // |now|. If there is no such entry, returns NULL. |
(...skipping 14 matching lines...) Expand all Loading... |
151 base::TimeTicks now, | 160 base::TimeTicks now, |
152 base::TimeDelta ttl); | 161 base::TimeDelta ttl); |
153 | 162 |
154 // Marks all entries as stale on account of a network change. | 163 // Marks all entries as stale on account of a network change. |
155 void OnNetworkChange(); | 164 void OnNetworkChange(); |
156 | 165 |
157 void set_eviction_callback(const EvictionCallback& callback) { | 166 void set_eviction_callback(const EvictionCallback& callback) { |
158 eviction_callback_ = callback; | 167 eviction_callback_ = callback; |
159 } | 168 } |
160 | 169 |
| 170 void set_persistence_delegate(PersistenceDelegate* delegate) { |
| 171 delegate_ = delegate; |
| 172 } |
| 173 |
161 // Empties the cache. | 174 // Empties the cache. |
162 void clear(); | 175 void clear(); |
163 | 176 |
164 // Clears hosts matching |host_filter| from the cache. | 177 // Clears hosts matching |host_filter| from the cache. |
165 void ClearForHosts( | 178 void ClearForHosts( |
166 const base::Callback<bool(const std::string&)>& host_filter); | 179 const base::Callback<bool(const std::string&)>& host_filter); |
167 | 180 |
168 // Returns the contents of the cache represented as a base::ListValue for | 181 // Returns the contents of the cache represented as a base::ListValue for |
169 // serialization. | 182 // serialization. |
170 std::unique_ptr<base::ListValue> GetAsListValue(bool include_staleness) const; | 183 std::unique_ptr<base::ListValue> GetAsListValue(bool include_staleness) const; |
(...skipping 18 matching lines...) Expand all Loading... |
189 | 202 |
190 enum SetOutcome : int; | 203 enum SetOutcome : int; |
191 enum LookupOutcome : int; | 204 enum LookupOutcome : int; |
192 enum EraseReason : int; | 205 enum EraseReason : int; |
193 | 206 |
194 Entry* LookupInternal(const Key& key); | 207 Entry* LookupInternal(const Key& key); |
195 | 208 |
196 void RecordSet(SetOutcome outcome, | 209 void RecordSet(SetOutcome outcome, |
197 base::TimeTicks now, | 210 base::TimeTicks now, |
198 const Entry* old_entry, | 211 const Entry* old_entry, |
199 const Entry& new_entry); | 212 const Entry& new_entry, |
| 213 AddressListDeltaType delta); |
200 void RecordUpdateStale(AddressListDeltaType delta, | 214 void RecordUpdateStale(AddressListDeltaType delta, |
201 const EntryStaleness& stale); | 215 const EntryStaleness& stale); |
202 void RecordLookup(LookupOutcome outcome, | 216 void RecordLookup(LookupOutcome outcome, |
203 base::TimeTicks now, | 217 base::TimeTicks now, |
204 const Entry* entry); | 218 const Entry* entry); |
205 void RecordErase(EraseReason reason, base::TimeTicks now, const Entry& entry); | 219 void RecordErase(EraseReason reason, base::TimeTicks now, const Entry& entry); |
206 void RecordEraseAll(EraseReason reason, base::TimeTicks now); | 220 void RecordEraseAll(EraseReason reason, base::TimeTicks now); |
207 | 221 |
208 // Returns true if this HostCache can contain no entries. | 222 // Returns true if this HostCache can contain no entries. |
209 bool caching_is_disabled() const { return max_entries_ == 0; } | 223 bool caching_is_disabled() const { return max_entries_ == 0; } |
210 | 224 |
211 void EvictOneEntry(base::TimeTicks now); | 225 void EvictOneEntry(base::TimeTicks now); |
212 // Helper to insert an Entry into the cache. | 226 // Helper to insert an Entry into the cache. |
213 void AddEntry(const Key& key, const Entry& entry); | 227 void AddEntry(const Key& key, const Entry& entry); |
214 | 228 |
215 // Map from hostname (presumably in lowercase canonicalized format) to | 229 // Map from hostname (presumably in lowercase canonicalized format) to |
216 // a resolved result entry. | 230 // a resolved result entry. |
217 EntryMap entries_; | 231 EntryMap entries_; |
218 size_t max_entries_; | 232 size_t max_entries_; |
219 int network_changes_; | 233 int network_changes_; |
220 EvictionCallback eviction_callback_; | 234 EvictionCallback eviction_callback_; |
221 | 235 |
| 236 PersistenceDelegate* delegate_; |
| 237 |
222 THREAD_CHECKER(thread_checker_); | 238 THREAD_CHECKER(thread_checker_); |
223 | 239 |
224 DISALLOW_COPY_AND_ASSIGN(HostCache); | 240 DISALLOW_COPY_AND_ASSIGN(HostCache); |
225 }; | 241 }; |
226 | 242 |
227 } // namespace net | 243 } // namespace net |
228 | 244 |
229 #endif // NET_DNS_HOST_CACHE_H_ | 245 #endif // NET_DNS_HOST_CACHE_H_ |
OLD | NEW |