1 // Copyright (c) 2011 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.
5 #ifndef NET_BASE_HOST_CACHE_H_
6 #define NET_BASE_HOST_CACHE_H_
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "base/time.h"
16 #include "net/base/address_family.h"
17 #include "net/base/address_list.h"
18 #include "net/base/net_export.h"
22 // Cache used by HostResolver to map hostnames to their resolved result.
23 class NET_EXPORT HostCache
: NON_EXPORTED_BASE(public base::NonThreadSafe
) {
25 // Stores the latest address list that was looked up for a hostname.
26 struct Entry
: public base::RefCounted
<Entry
> {
27 Entry(int error
, const AddressList
& addrlist
, base::TimeTicks expiration
);
29 // The resolve results for this entry.
33 // The time when this entry expires.
34 base::TimeTicks expiration
;
37 friend class base::RefCounted
<Entry
>;
43 Key(const std::string
& hostname
, AddressFamily address_family
,
44 HostResolverFlags host_resolver_flags
)
46 address_family(address_family
),
47 host_resolver_flags(host_resolver_flags
) {}
49 bool operator==(const Key
& other
) const {
50 // |address_family| and |host_resolver_flags| are compared before
51 // |hostname| under assumption that integer comparisons are faster than
52 // string comparisons.
53 return (other
.address_family
== address_family
&&
54 other
.host_resolver_flags
== host_resolver_flags
&&
55 other
.hostname
== hostname
);
58 bool operator<(const Key
& other
) const {
59 // |address_family| and |host_resolver_flags| are compared before
60 // |hostname| under assumption that integer comparisons are faster than
61 // string comparisons.
62 if (address_family
!= other
.address_family
)
63 return address_family
< other
.address_family
;
64 if (host_resolver_flags
!= other
.host_resolver_flags
)
65 return host_resolver_flags
< other
.host_resolver_flags
;
66 return hostname
< other
.hostname
;
70 AddressFamily address_family
;
71 HostResolverFlags host_resolver_flags
;
74 typedef std::map
<Key
, scoped_refptr
<Entry
> > EntryMap
;
76 // Constructs a HostCache that caches successful host resolves for
77 // |success_entry_ttl| time, and failed host resolves for
78 // |failure_entry_ttl|. The cache will store up to |max_entries|.
79 HostCache(size_t max_entries
,
80 base::TimeDelta success_entry_ttl
,
81 base::TimeDelta failure_entry_ttl
);
85 // Returns a pointer to the entry for |key|, which is valid at time
86 // |now|. If there is no such entry, returns NULL.
87 const Entry
* Lookup(const Key
& key
, base::TimeTicks now
) const;
89 // Overwrites or creates an entry for |key|. Returns the pointer to the
90 // entry, or NULL on failure (fails if caching is disabled).
91 // (|error|, |addrlist|) is the value to set, and |now| is the current
93 Entry
* Set(const Key
& key
,
95 const AddressList
& addrlist
,
101 // Returns the number of entries in the cache.
104 // Following are used by net_internals UI.
105 size_t max_entries() const;
107 base::TimeDelta
success_entry_ttl() const;
109 base::TimeDelta
failure_entry_ttl() const;
111 // Note that this map may contain expired entries.
112 const EntryMap
& entries() const;
114 // Creates a default cache.
115 static HostCache
* CreateDefaultCache();
118 FRIEND_TEST_ALL_PREFIXES(HostCacheTest
, Compact
);
119 FRIEND_TEST_ALL_PREFIXES(HostCacheTest
, NoCache
);
121 // Returns true if this cache entry's result is valid at time |now|.
122 static bool CanUseEntry(const Entry
* entry
, const base::TimeTicks now
);
124 // Prunes entries from the cache to bring it below max entry bound. Entries
125 // matching |pinned_entry| will NOT be pruned.
126 void Compact(base::TimeTicks now
, const Entry
* pinned_entry
);
128 // Returns true if this HostCache can contain no entries.
129 bool caching_is_disabled() const {
130 return max_entries_
== 0;
133 // Bound on total size of the cache.
136 // Time to live for cache entries.
137 base::TimeDelta success_entry_ttl_
;
138 base::TimeDelta failure_entry_ttl_
;
140 // Map from hostname (presumably in lowercase canonicalized format) to
141 // a resolved result entry.
144 DISALLOW_COPY_AND_ASSIGN(HostCache
);
149 #endif // NET_BASE_HOST_CACHE_H_