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.
5 #ifndef NET_DNS_HOST_CACHE_H_
6 #define NET_DNS_HOST_CACHE_H_
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"
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 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
);
32 bool has_ttl() const { return ttl
>= base::TimeDelta(); }
34 // The resolve results for this entry.
37 // TTL obtained from the nameserver. Negative if unknown.
42 Key(const std::string
& hostname
, AddressFamily address_family
,
43 HostResolverFlags host_resolver_flags
)
45 address_family(address_family
),
46 host_resolver_flags(host_resolver_flags
) {}
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
;
60 AddressFamily address_family
;
61 HostResolverFlags host_resolver_flags
;
64 struct EvictionHandler
{
65 void Handle(const Key
& key
,
67 const base::TimeTicks
& expiration
,
68 const base::TimeTicks
& now
,
72 typedef ExpiringCache
<Key
, Entry
, base::TimeTicks
,
73 std::less
<base::TimeTicks
>,
74 EvictionHandler
> EntryMap
;
76 // Constructs a HostCache that stores up to |max_entries|.
77 explicit HostCache(size_t max_entries
);
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
);
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
,
96 // Returns the number of entries in the cache.
99 // Following are used by net_internals UI.
100 size_t max_entries() const;
102 const EntryMap
& entries() const;
104 // Creates a default cache.
105 static scoped_ptr
<HostCache
> CreateDefaultCache();
108 FRIEND_TEST_ALL_PREFIXES(HostCacheTest
, NoCache
);
110 // Returns true if this HostCache can contain no entries.
111 bool caching_is_disabled() const {
112 return entries_
.max_entries() == 0;
115 // Map from hostname (presumably in lowercase canonicalized format) to
116 // a resolved result entry.
119 DISALLOW_COPY_AND_ASSIGN(HostCache
);
124 #endif // NET_DNS_HOST_CACHE_H_