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_BASE_HOST_CACHE_H_ | 5 #ifndef NET_BASE_HOST_CACHE_H_ |
6 #define NET_BASE_HOST_CACHE_H_ | 6 #define NET_BASE_HOST_CACHE_H_ |
7 | 7 |
8 #include <functional> | 8 #include <functional> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
12 #include "base/threading/non_thread_safe.h" | 12 #include "base/threading/non_thread_safe.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 #include "net/base/address_family.h" | 14 #include "net/base/address_family.h" |
15 #include "net/base/address_list.h" | 15 #include "net/base/address_list.h" |
16 #include "net/base/expiring_cache.h" | 16 #include "net/base/expiring_cache.h" |
17 #include "net/base/net_export.h" | 17 #include "net/base/net_export.h" |
18 | 18 |
19 namespace net { | 19 namespace net { |
20 | 20 |
21 // Cache used by HostResolver to map hostnames to their resolved result. | 21 // Cache used by HostResolver to map hostnames to their resolved result. |
22 class NET_EXPORT HostCache : NON_EXPORTED_BASE(public base::NonThreadSafe) { | 22 class NET_EXPORT HostCache : NON_EXPORTED_BASE(public base::NonThreadSafe) { |
23 public: | 23 public: |
24 // Stores the latest address list that was looked up for a hostname. | 24 // Stores the latest address list that was looked up for a hostname. |
25 struct Entry { | 25 struct Entry { |
26 Entry(int error, const AddressList& addrlist, base::TimeDelta ttl); | |
27 // As above, but sets |ttl| to negative. | |
cbentzel
2012/10/13 10:50:45
This comment actually confuses things a bit - it's
| |
26 Entry(int error, const AddressList& addrlist); | 28 Entry(int error, const AddressList& addrlist); |
27 ~Entry(); | 29 ~Entry(); |
28 | 30 |
31 bool has_ttl() const { return ttl >= base::TimeDelta(); } | |
32 | |
29 // The resolve results for this entry. | 33 // The resolve results for this entry. |
30 int error; | 34 int error; |
31 AddressList addrlist; | 35 AddressList addrlist; |
36 base::TimeDelta ttl; | |
32 }; | 37 }; |
33 | 38 |
34 struct Key { | 39 struct Key { |
35 Key(const std::string& hostname, AddressFamily address_family, | 40 Key(const std::string& hostname, AddressFamily address_family, |
36 HostResolverFlags host_resolver_flags) | 41 HostResolverFlags host_resolver_flags) |
37 : hostname(hostname), | 42 : hostname(hostname), |
38 address_family(address_family), | 43 address_family(address_family), |
39 host_resolver_flags(host_resolver_flags) {} | 44 host_resolver_flags(host_resolver_flags) {} |
40 | 45 |
41 bool operator<(const Key& other) const { | 46 bool operator<(const Key& other) const { |
(...skipping 18 matching lines...) Expand all Loading... | |
60 // Constructs a HostCache that stores up to |max_entries|. | 65 // Constructs a HostCache that stores up to |max_entries|. |
61 explicit HostCache(size_t max_entries); | 66 explicit HostCache(size_t max_entries); |
62 | 67 |
63 ~HostCache(); | 68 ~HostCache(); |
64 | 69 |
65 // Returns a pointer to the entry for |key|, which is valid at time | 70 // Returns a pointer to the entry for |key|, which is valid at time |
66 // |now|. If there is no such entry, returns NULL. | 71 // |now|. If there is no such entry, returns NULL. |
67 const Entry* Lookup(const Key& key, base::TimeTicks now); | 72 const Entry* Lookup(const Key& key, base::TimeTicks now); |
68 | 73 |
69 // Overwrites or creates an entry for |key|. | 74 // Overwrites or creates an entry for |key|. |
70 // (|error|, |addrlist|) is the value to set, |now| is the current time | 75 // |entry| is the value to set, |now| is the current time |
71 // |ttl| is the "time to live". | 76 // |ttl| is the "time to live". |
72 void Set(const Key& key, | 77 void Set(const Key& key, |
73 int error, | 78 const Entry& entry, |
74 const AddressList& addrlist, | |
75 base::TimeTicks now, | 79 base::TimeTicks now, |
76 base::TimeDelta ttl); | 80 base::TimeDelta ttl); |
77 | 81 |
78 // Empties the cache | 82 // Empties the cache |
79 void clear(); | 83 void clear(); |
80 | 84 |
81 // Returns the number of entries in the cache. | 85 // Returns the number of entries in the cache. |
82 size_t size() const; | 86 size_t size() const; |
83 | 87 |
84 // Following are used by net_internals UI. | 88 // Following are used by net_internals UI. |
(...skipping 15 matching lines...) Expand all Loading... | |
100 // Map from hostname (presumably in lowercase canonicalized format) to | 104 // Map from hostname (presumably in lowercase canonicalized format) to |
101 // a resolved result entry. | 105 // a resolved result entry. |
102 EntryMap entries_; | 106 EntryMap entries_; |
103 | 107 |
104 DISALLOW_COPY_AND_ASSIGN(HostCache); | 108 DISALLOW_COPY_AND_ASSIGN(HostCache); |
105 }; | 109 }; |
106 | 110 |
107 } // namespace net | 111 } // namespace net |
108 | 112 |
109 #endif // NET_BASE_HOST_CACHE_H_ | 113 #endif // NET_BASE_HOST_CACHE_H_ |
OLD | NEW |