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 3 // found in the LICENSE file. 4 5 #ifndef NET_DNS_HOST_CACHE_H_ 6 #define NET_DNS_HOST_CACHE_H_ 7 8 #include <functional> 9 #include <string> 10 11 #include "base/gtest_prod_util.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/threading/non_thread_safe.h" 14 #include "base/time/time.h" 15 #include "net/base/address_family.h" 16 #include "net/base/address_list.h" 17 #include "net/base/expiring_cache.h" 18 #include "net/base/net_export.h" 19 20 namespace net { 21 22 // Cache used by HostResolver to map hostnames to their resolved result. 23 class NET_EXPORT HostCache : NON_EXPORTED_BASE(public base::NonThreadSafe) { 24 public: 25 // Stores the latest address list that was looked up for a hostname. 26 struct NET_EXPORT Entry { 27 Entry(int error, const AddressList& addrlist, base::TimeDelta ttl); 28 // Use when |ttl| is unknown. 29 Entry(int error, const AddressList& addrlist); 30 ~Entry(); 31 32 bool has_ttl() const { return ttl >= base::TimeDelta(); } 33 34 // The resolve results for this entry. 35 int error; 36 AddressList addrlist; 37 // TTL obtained from the nameserver. Negative if unknown. 38 base::TimeDelta ttl; 39 }; 40 41 struct Key { 42 Key(const std::string& hostname, AddressFamily address_family, 43 HostResolverFlags host_resolver_flags) 44 : hostname(hostname), 45 address_family(address_family), 46 host_resolver_flags(host_resolver_flags) {} 47 48 bool operator<(const Key& other) const { 49 // |address_family| and |host_resolver_flags| are compared before 50 // |hostname| under assumption that integer comparisons are faster than 51 // string comparisons. 52 if (address_family != other.address_family) 53 return address_family < other.address_family; 54 if (host_resolver_flags != other.host_resolver_flags) 55 return host_resolver_flags < other.host_resolver_flags; 56 return hostname < other.hostname; 57 } 58 59 std::string hostname; 60 AddressFamily address_family; 61 HostResolverFlags host_resolver_flags; 62 }; 63 64 struct EvictionHandler { 65 void Handle(const Key& key, 66 const Entry& entry, 67 const base::TimeTicks& expiration, 68 const base::TimeTicks& now, 69 bool onGet) const; 70 }; 71 72 typedef ExpiringCache<Key, Entry, base::TimeTicks, 73 std::less<base::TimeTicks>, 74 EvictionHandler> EntryMap; 75 76 // Constructs a HostCache that stores up to |max_entries|. 77 explicit HostCache(size_t max_entries); 78 79 ~HostCache(); 80 81 // Returns a pointer to the entry for |key|, which is valid at time 82 // |now|. If there is no such entry, returns NULL. 83 const Entry* Lookup(const Key& key, base::TimeTicks now); 84 85 // Overwrites or creates an entry for |key|. 86 // |entry| is the value to set, |now| is the current time 87 // |ttl| is the "time to live". 88 void Set(const Key& key, 89 const Entry& entry, 90 base::TimeTicks now, 91 base::TimeDelta ttl); 92 93 // Empties the cache 94 void clear(); 95 96 // Returns the number of entries in the cache. 97 size_t size() const; 98 99 // Following are used by net_internals UI. 100 size_t max_entries() const; 101 102 const EntryMap& entries() const; 103 104 // Creates a default cache. 105 static scoped_ptr<HostCache> CreateDefaultCache(); 106 107 private: 108 FRIEND_TEST_ALL_PREFIXES(HostCacheTest, NoCache); 109 110 // Returns true if this HostCache can contain no entries. 111 bool caching_is_disabled() const { 112 return entries_.max_entries() == 0; 113 } 114 115 // Map from hostname (presumably in lowercase canonicalized format) to 116 // a resolved result entry. 117 EntryMap entries_; 118 119 DISALLOW_COPY_AND_ASSIGN(HostCache); 120 }; 121 122 } // namespace net 123 124 #endif // NET_DNS_HOST_CACHE_H_ 125