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 <functional> | 8 #include <functional> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 21 matching lines...) Expand all Loading... |
32 bool has_ttl() const { return ttl >= base::TimeDelta(); } | 32 bool has_ttl() const { return ttl >= base::TimeDelta(); } |
33 | 33 |
34 // The resolve results for this entry. | 34 // The resolve results for this entry. |
35 int error; | 35 int error; |
36 AddressList addrlist; | 36 AddressList addrlist; |
37 // TTL obtained from the nameserver. Negative if unknown. | 37 // TTL obtained from the nameserver. Negative if unknown. |
38 base::TimeDelta ttl; | 38 base::TimeDelta ttl; |
39 }; | 39 }; |
40 | 40 |
41 struct Key { | 41 struct Key { |
42 Key(const std::string& hostname, AddressFamily address_family, | 42 Key(const std::string& hostname, |
| 43 AddressFamily address_family, |
43 HostResolverFlags host_resolver_flags) | 44 HostResolverFlags host_resolver_flags) |
44 : hostname(hostname), | 45 : hostname(hostname), |
45 address_family(address_family), | 46 address_family(address_family), |
46 host_resolver_flags(host_resolver_flags) {} | 47 host_resolver_flags(host_resolver_flags) {} |
47 | 48 |
48 bool operator<(const Key& other) const { | 49 bool operator<(const Key& other) const { |
49 // |address_family| and |host_resolver_flags| are compared before | 50 // |address_family| and |host_resolver_flags| are compared before |
50 // |hostname| under assumption that integer comparisons are faster than | 51 // |hostname| under assumption that integer comparisons are faster than |
51 // string comparisons. | 52 // string comparisons. |
52 if (address_family != other.address_family) | 53 if (address_family != other.address_family) |
53 return address_family < other.address_family; | 54 return address_family < other.address_family; |
54 if (host_resolver_flags != other.host_resolver_flags) | 55 if (host_resolver_flags != other.host_resolver_flags) |
55 return host_resolver_flags < other.host_resolver_flags; | 56 return host_resolver_flags < other.host_resolver_flags; |
56 return hostname < other.hostname; | 57 return hostname < other.hostname; |
57 } | 58 } |
58 | 59 |
59 std::string hostname; | 60 std::string hostname; |
60 AddressFamily address_family; | 61 AddressFamily address_family; |
61 HostResolverFlags host_resolver_flags; | 62 HostResolverFlags host_resolver_flags; |
62 }; | 63 }; |
63 | 64 |
64 struct EvictionHandler { | 65 struct EvictionHandler { |
65 void Handle(const Key& key, | 66 void Handle(const Key& key, |
66 const Entry& entry, | 67 const Entry& entry, |
67 const base::TimeTicks& expiration, | 68 const base::TimeTicks& expiration, |
68 const base::TimeTicks& now, | 69 const base::TimeTicks& now, |
69 bool onGet) const; | 70 bool onGet) const; |
70 }; | 71 }; |
71 | 72 |
72 typedef ExpiringCache<Key, Entry, base::TimeTicks, | 73 typedef ExpiringCache<Key, |
| 74 Entry, |
| 75 base::TimeTicks, |
73 std::less<base::TimeTicks>, | 76 std::less<base::TimeTicks>, |
74 EvictionHandler> EntryMap; | 77 EvictionHandler> EntryMap; |
75 | 78 |
76 // Constructs a HostCache that stores up to |max_entries|. | 79 // Constructs a HostCache that stores up to |max_entries|. |
77 explicit HostCache(size_t max_entries); | 80 explicit HostCache(size_t max_entries); |
78 | 81 |
79 ~HostCache(); | 82 ~HostCache(); |
80 | 83 |
81 // Returns a pointer to the entry for |key|, which is valid at time | 84 // Returns a pointer to the entry for |key|, which is valid at time |
82 // |now|. If there is no such entry, returns NULL. | 85 // |now|. If there is no such entry, returns NULL. |
(...skipping 18 matching lines...) Expand all Loading... |
101 | 104 |
102 const EntryMap& entries() const; | 105 const EntryMap& entries() const; |
103 | 106 |
104 // Creates a default cache. | 107 // Creates a default cache. |
105 static scoped_ptr<HostCache> CreateDefaultCache(); | 108 static scoped_ptr<HostCache> CreateDefaultCache(); |
106 | 109 |
107 private: | 110 private: |
108 FRIEND_TEST_ALL_PREFIXES(HostCacheTest, NoCache); | 111 FRIEND_TEST_ALL_PREFIXES(HostCacheTest, NoCache); |
109 | 112 |
110 // Returns true if this HostCache can contain no entries. | 113 // Returns true if this HostCache can contain no entries. |
111 bool caching_is_disabled() const { | 114 bool caching_is_disabled() const { return entries_.max_entries() == 0; } |
112 return entries_.max_entries() == 0; | |
113 } | |
114 | 115 |
115 // Map from hostname (presumably in lowercase canonicalized format) to | 116 // Map from hostname (presumably in lowercase canonicalized format) to |
116 // a resolved result entry. | 117 // a resolved result entry. |
117 EntryMap entries_; | 118 EntryMap entries_; |
118 | 119 |
119 DISALLOW_COPY_AND_ASSIGN(HostCache); | 120 DISALLOW_COPY_AND_ASSIGN(HostCache); |
120 }; | 121 }; |
121 | 122 |
122 } // namespace net | 123 } // namespace net |
123 | 124 |
124 #endif // NET_DNS_HOST_CACHE_H_ | 125 #endif // NET_DNS_HOST_CACHE_H_ |
OLD | NEW |