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.
4
5#ifndef NET_BASE_HOST_CACHE_H_
6#define NET_BASE_HOST_CACHE_H_
7#pragma once
8
9#include <map>
10#include <string>
11
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
19namespace net {
20
21// Cache used by HostResolver to map hostnames to their resolved result.
22class HostCache : public base::NonThreadSafe {
23 public:
24  // Stores the latest address list that was looked up for a hostname.
25  struct Entry : public base::RefCounted<Entry> {
26    Entry(int error, const AddressList& addrlist, base::TimeTicks expiration);
27
28    // The resolve results for this entry.
29    int error;
30    AddressList addrlist;
31
32    // The time when this entry expires.
33    base::TimeTicks expiration;
34
35   private:
36    friend class base::RefCounted<Entry>;
37
38    ~Entry();
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      return (other.address_family == address_family &&
53              other.host_resolver_flags == host_resolver_flags &&
54              other.hostname == hostname);
55    }
56
57    bool operator<(const Key& other) const {
58      // |address_family| and |host_resolver_flags| are compared before
59      // |hostname| under assumption that integer comparisons are faster than
60      // string comparisons.
61      if (address_family != other.address_family)
62        return address_family < other.address_family;
63      if (host_resolver_flags != other.host_resolver_flags)
64        return host_resolver_flags < other.host_resolver_flags;
65      return hostname < other.hostname;
66    }
67
68    std::string hostname;
69    AddressFamily address_family;
70    HostResolverFlags host_resolver_flags;
71  };
72
73  typedef std::map<Key, scoped_refptr<Entry> > EntryMap;
74
75  // Constructs a HostCache that caches successful host resolves for
76  // |success_entry_ttl| time, and failed host resolves for
77  // |failure_entry_ttl|. The cache will store up to |max_entries|.
78  HostCache(size_t max_entries,
79            base::TimeDelta success_entry_ttl,
80            base::TimeDelta failure_entry_ttl);
81
82  ~HostCache();
83
84  // Returns a pointer to the entry for |key|, which is valid at time
85  // |now|. If there is no such entry, returns NULL.
86  const Entry* Lookup(const Key& key, base::TimeTicks now) const;
87
88  // Overwrites or creates an entry for |key|. Returns the pointer to the
89  // entry, or NULL on failure (fails if caching is disabled).
90  // (|error|, |addrlist|) is the value to set, and |now| is the current
91  // timestamp.
92  Entry* Set(const Key& key,
93             int error,
94             const AddressList& addrlist,
95             base::TimeTicks now);
96
97  // Empties the cache
98  void clear();
99
100  // Returns the number of entries in the cache.
101  size_t size() const;
102
103  // Following are used by net_internals UI.
104  size_t max_entries() const;
105
106  base::TimeDelta success_entry_ttl() const;
107
108  base::TimeDelta failure_entry_ttl() const;
109
110  // Note that this map may contain expired entries.
111  const EntryMap& entries() const;
112
113 private:
114  FRIEND_TEST_ALL_PREFIXES(HostCacheTest, Compact);
115  FRIEND_TEST_ALL_PREFIXES(HostCacheTest, NoCache);
116
117  // Returns true if this cache entry's result is valid at time |now|.
118  static bool CanUseEntry(const Entry* entry, const base::TimeTicks now);
119
120  // Prunes entries from the cache to bring it below max entry bound. Entries
121  // matching |pinned_entry| will NOT be pruned.
122  void Compact(base::TimeTicks now, const Entry* pinned_entry);
123
124  // Returns true if this HostCache can contain no entries.
125  bool caching_is_disabled() const {
126    return max_entries_ == 0;
127  }
128
129  // Bound on total size of the cache.
130  size_t max_entries_;
131
132  // Time to live for cache entries.
133  base::TimeDelta success_entry_ttl_;
134  base::TimeDelta failure_entry_ttl_;
135
136  // Map from hostname (presumably in lowercase canonicalized format) to
137  // a resolved result entry.
138  EntryMap entries_;
139
140  DISALLOW_COPY_AND_ASSIGN(HostCache);
141};
142
143}  // namespace net
144
145#endif  // NET_BASE_HOST_CACHE_H_
146