| 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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 base::TimeTicks expires_; | 106 base::TimeTicks expires_; |
| 107 // Copied from the cache's network_changes_ when the entry is set; can0 | 107 // Copied from the cache's network_changes_ when the entry is set; can0 |
| 108 // later be compared to it to see if the entry was received on the current | 108 // later be compared to it to see if the entry was received on the current |
| 109 // network. | 109 // network. |
| 110 int network_changes_; | 110 int network_changes_; |
| 111 int total_hits_; | 111 int total_hits_; |
| 112 int stale_hits_; | 112 int stale_hits_; |
| 113 }; | 113 }; |
| 114 | 114 |
| 115 using EntryMap = std::map<Key, Entry>; | 115 using EntryMap = std::map<Key, Entry>; |
| 116 using EvictionCallback = base::Callback<void(const Key&, const Entry&)>; |
| 116 | 117 |
| 117 // Constructs a HostCache that stores up to |max_entries|. | 118 // Constructs a HostCache that stores up to |max_entries|. |
| 118 explicit HostCache(size_t max_entries); | 119 explicit HostCache(size_t max_entries); |
| 119 | 120 |
| 120 ~HostCache(); | 121 ~HostCache(); |
| 121 | 122 |
| 122 // Returns a pointer to the entry for |key|, which is valid at time | 123 // Returns a pointer to the entry for |key|, which is valid at time |
| 123 // |now|. If there is no such entry, returns NULL. | 124 // |now|. If there is no such entry, returns NULL. |
| 124 const Entry* Lookup(const Key& key, base::TimeTicks now); | 125 const Entry* Lookup(const Key& key, base::TimeTicks now); |
| 125 | 126 |
| 126 // Returns a pointer to the entry for |key|, whether it is valid or stale at | 127 // Returns a pointer to the entry for |key|, whether it is valid or stale at |
| 127 // time |now|. Fills in |stale_out| with information about how stale it is. | 128 // time |now|. Fills in |stale_out| with information about how stale it is. |
| 128 // If there is no entry for |key| at all, returns NULL. | 129 // If there is no entry for |key| at all, returns NULL. |
| 129 const Entry* LookupStale(const Key& key, | 130 const Entry* LookupStale(const Key& key, |
| 130 base::TimeTicks now, | 131 base::TimeTicks now, |
| 131 EntryStaleness* stale_out); | 132 EntryStaleness* stale_out); |
| 132 | 133 |
| 133 // Overwrites or creates an entry for |key|. | 134 // Overwrites or creates an entry for |key|. |
| 134 // |entry| is the value to set, |now| is the current time | 135 // |entry| is the value to set, |now| is the current time |
| 135 // |ttl| is the "time to live". | 136 // |ttl| is the "time to live". |
| 136 void Set(const Key& key, | 137 void Set(const Key& key, |
| 137 const Entry& entry, | 138 const Entry& entry, |
| 138 base::TimeTicks now, | 139 base::TimeTicks now, |
| 139 base::TimeDelta ttl); | 140 base::TimeDelta ttl); |
| 140 | 141 |
| 141 // Marks all entries as stale on account of a network change. | 142 // Marks all entries as stale on account of a network change. |
| 142 void OnNetworkChange(); | 143 void OnNetworkChange(); |
| 143 | 144 |
| 145 void set_eviction_callback(const EvictionCallback& callback) { |
| 146 eviction_callback_ = callback; |
| 147 } |
| 148 |
| 144 // Empties the cache | 149 // Empties the cache |
| 145 void clear(); | 150 void clear(); |
| 146 | 151 |
| 147 // Returns the number of entries in the cache. | 152 // Returns the number of entries in the cache. |
| 148 size_t size() const; | 153 size_t size() const; |
| 149 | 154 |
| 150 // Following are used by net_internals UI. | 155 // Following are used by net_internals UI. |
| 151 size_t max_entries() const; | 156 size_t max_entries() const; |
| 152 | 157 |
| 153 const EntryMap& entries() const { return entries_; } | 158 const EntryMap& entries() const { return entries_; } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 179 // Returns true if this HostCache can contain no entries. | 184 // Returns true if this HostCache can contain no entries. |
| 180 bool caching_is_disabled() const { return max_entries_ == 0; } | 185 bool caching_is_disabled() const { return max_entries_ == 0; } |
| 181 | 186 |
| 182 void EvictOneEntry(base::TimeTicks now); | 187 void EvictOneEntry(base::TimeTicks now); |
| 183 | 188 |
| 184 // Map from hostname (presumably in lowercase canonicalized format) to | 189 // Map from hostname (presumably in lowercase canonicalized format) to |
| 185 // a resolved result entry. | 190 // a resolved result entry. |
| 186 EntryMap entries_; | 191 EntryMap entries_; |
| 187 size_t max_entries_; | 192 size_t max_entries_; |
| 188 int network_changes_; | 193 int network_changes_; |
| 194 EvictionCallback eviction_callback_; |
| 189 | 195 |
| 190 DISALLOW_COPY_AND_ASSIGN(HostCache); | 196 DISALLOW_COPY_AND_ASSIGN(HostCache); |
| 191 }; | 197 }; |
| 192 | 198 |
| 193 } // namespace net | 199 } // namespace net |
| 194 | 200 |
| 195 #endif // NET_DNS_HOST_CACHE_H_ | 201 #endif // NET_DNS_HOST_CACHE_H_ |
| OLD | NEW |