| 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 NET_EXPORT Entry { |
| 26 Entry(int error, const AddressList& addrlist, base::TimeDelta ttl); |
| 27 // Use when |ttl| is unknown. |
| 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 // TTL obtained from the nameserver. Negative if unknown. |
| 37 base::TimeDelta ttl; |
| 32 }; | 38 }; |
| 33 | 39 |
| 34 struct Key { | 40 struct Key { |
| 35 Key(const std::string& hostname, AddressFamily address_family, | 41 Key(const std::string& hostname, AddressFamily address_family, |
| 36 HostResolverFlags host_resolver_flags) | 42 HostResolverFlags host_resolver_flags) |
| 37 : hostname(hostname), | 43 : hostname(hostname), |
| 38 address_family(address_family), | 44 address_family(address_family), |
| 39 host_resolver_flags(host_resolver_flags) {} | 45 host_resolver_flags(host_resolver_flags) {} |
| 40 | 46 |
| 41 bool operator<(const Key& other) const { | 47 bool operator<(const Key& other) const { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 60 // Constructs a HostCache that stores up to |max_entries|. | 66 // Constructs a HostCache that stores up to |max_entries|. |
| 61 explicit HostCache(size_t max_entries); | 67 explicit HostCache(size_t max_entries); |
| 62 | 68 |
| 63 ~HostCache(); | 69 ~HostCache(); |
| 64 | 70 |
| 65 // Returns a pointer to the entry for |key|, which is valid at time | 71 // Returns a pointer to the entry for |key|, which is valid at time |
| 66 // |now|. If there is no such entry, returns NULL. | 72 // |now|. If there is no such entry, returns NULL. |
| 67 const Entry* Lookup(const Key& key, base::TimeTicks now); | 73 const Entry* Lookup(const Key& key, base::TimeTicks now); |
| 68 | 74 |
| 69 // Overwrites or creates an entry for |key|. | 75 // Overwrites or creates an entry for |key|. |
| 70 // (|error|, |addrlist|) is the value to set, |now| is the current time | 76 // |entry| is the value to set, |now| is the current time |
| 71 // |ttl| is the "time to live". | 77 // |ttl| is the "time to live". |
| 72 void Set(const Key& key, | 78 void Set(const Key& key, |
| 73 int error, | 79 const Entry& entry, |
| 74 const AddressList& addrlist, | |
| 75 base::TimeTicks now, | 80 base::TimeTicks now, |
| 76 base::TimeDelta ttl); | 81 base::TimeDelta ttl); |
| 77 | 82 |
| 78 // Empties the cache | 83 // Empties the cache |
| 79 void clear(); | 84 void clear(); |
| 80 | 85 |
| 81 // Returns the number of entries in the cache. | 86 // Returns the number of entries in the cache. |
| 82 size_t size() const; | 87 size_t size() const; |
| 83 | 88 |
| 84 // Following are used by net_internals UI. | 89 // Following are used by net_internals UI. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 100 // Map from hostname (presumably in lowercase canonicalized format) to | 105 // Map from hostname (presumably in lowercase canonicalized format) to |
| 101 // a resolved result entry. | 106 // a resolved result entry. |
| 102 EntryMap entries_; | 107 EntryMap entries_; |
| 103 | 108 |
| 104 DISALLOW_COPY_AND_ASSIGN(HostCache); | 109 DISALLOW_COPY_AND_ASSIGN(HostCache); |
| 105 }; | 110 }; |
| 106 | 111 |
| 107 } // namespace net | 112 } // namespace net |
| 108 | 113 |
| 109 #endif // NET_BASE_HOST_CACHE_H_ | 114 #endif // NET_BASE_HOST_CACHE_H_ |
| OLD | NEW |