Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/non_thread_safe.h" | |
| 11 #include "base/ref_counted.h" | 12 #include "base/ref_counted.h" |
| 12 #include "base/time.h" | 13 #include "base/time.h" |
| 13 #include "net/base/address_family.h" | 14 #include "net/base/address_family.h" |
| 14 #include "net/base/address_list.h" | 15 #include "net/base/address_list.h" |
| 15 #include "testing/gtest/include/gtest/gtest_prod.h" | 16 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 16 | 17 |
| 17 namespace net { | 18 namespace net { |
| 18 | 19 |
| 19 // Cache used by HostResolver to map hostnames to their resolved result. | 20 // Cache used by HostResolver to map hostnames to their resolved result. |
| 20 class HostCache { | 21 class HostCache : public NonThreadSafe { |
| 21 public: | 22 public: |
| 22 // Stores the latest address list that was looked up for a hostname. | 23 // Stores the latest address list that was looked up for a hostname. |
| 23 struct Entry : public base::RefCounted<Entry> { | 24 struct Entry : public base::RefCounted<Entry> { |
| 24 Entry(int error, const AddressList& addrlist, base::TimeTicks expiration); | 25 Entry(int error, const AddressList& addrlist, base::TimeTicks expiration); |
| 25 | 26 |
| 26 // The resolve results for this entry. | 27 // The resolve results for this entry. |
| 27 int error; | 28 int error; |
| 28 AddressList addrlist; | 29 AddressList addrlist; |
| 29 | 30 |
| 30 // The time when this entry expires. | 31 // The time when this entry expires. |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 | 86 |
| 86 // Overwrites or creates an entry for |key|. Returns the pointer to the | 87 // Overwrites or creates an entry for |key|. Returns the pointer to the |
| 87 // entry, or NULL on failure (fails if caching is disabled). | 88 // entry, or NULL on failure (fails if caching is disabled). |
| 88 // (|error|, |addrlist|) is the value to set, and |now| is the current | 89 // (|error|, |addrlist|) is the value to set, and |now| is the current |
| 89 // timestamp. | 90 // timestamp. |
| 90 Entry* Set(const Key& key, | 91 Entry* Set(const Key& key, |
| 91 int error, | 92 int error, |
| 92 const AddressList addrlist, | 93 const AddressList addrlist, |
| 93 base::TimeTicks now); | 94 base::TimeTicks now); |
| 94 | 95 |
| 95 // Empties the cache. | 96 // Empties the cache |
| 96 void clear() { | 97 void clear(); |
| 97 entries_.clear(); | |
| 98 } | |
| 99 | |
| 100 // Returns true if this HostCache can contain no entries. | |
| 101 bool caching_is_disabled() const { | |
| 102 return max_entries_ == 0; | |
| 103 } | |
| 104 | 98 |
| 105 // Returns the number of entries in the cache. | 99 // Returns the number of entries in the cache. |
| 106 size_t size() const { | 100 size_t size() const; |
| 107 return entries_.size(); | |
| 108 } | |
| 109 | 101 |
| 110 size_t max_entries() const { | 102 // Following are used by net_internals UI. |
| 111 return max_entries_; | 103 size_t max_entries() const; |
| 112 } | |
| 113 | 104 |
| 114 base::TimeDelta success_entry_ttl() const { | 105 base::TimeDelta success_entry_ttl() const; |
| 115 return success_entry_ttl_; | |
| 116 } | |
| 117 | 106 |
| 118 base::TimeDelta failure_entry_ttl() const { | 107 base::TimeDelta failure_entry_ttl() const; |
| 119 return failure_entry_ttl_; | |
| 120 } | |
| 121 | 108 |
| 122 // Note that this map may contain expired entries. | 109 // Note that this map may contain expired entries. |
| 123 const EntryMap& entries() const { | 110 const EntryMap& entries() const; |
| 124 return entries_; | |
| 125 } | |
| 126 | 111 |
| 127 private: | 112 private: |
| 128 FRIEND_TEST(HostCacheTest, Compact); | 113 FRIEND_TEST(HostCacheTest, Compact); |
| 129 FRIEND_TEST(HostCacheTest, NoCache); | 114 FRIEND_TEST(HostCacheTest, NoCache); |
|
willchan no longer on Chromium
2010/06/08 20:24:22
Was this \n deletion intentional? Up to you, but
cbentzel
2010/06/09 18:01:29
Unintentional, thanks for the catch.
| |
| 130 | |
| 131 // Returns true if this cache entry's result is valid at time |now|. | 115 // Returns true if this cache entry's result is valid at time |now|. |
| 132 static bool CanUseEntry(const Entry* entry, const base::TimeTicks now); | 116 static bool CanUseEntry(const Entry* entry, const base::TimeTicks now); |
| 133 | 117 |
| 134 // Prunes entries from the cache to bring it below max entry bound. Entries | 118 // Prunes entries from the cache to bring it below max entry bound. Entries |
| 135 // matching |pinned_entry| will NOT be pruned. | 119 // matching |pinned_entry| will NOT be pruned. |
| 136 void Compact(base::TimeTicks now, const Entry* pinned_entry); | 120 void Compact(base::TimeTicks now, const Entry* pinned_entry); |
| 137 | 121 |
| 122 // Returns true if this HostCache can contain no entries. | |
| 123 bool caching_is_disabled() const { | |
| 124 return max_entries_ == 0; | |
| 125 } | |
| 126 | |
| 138 // Bound on total size of the cache. | 127 // Bound on total size of the cache. |
| 139 size_t max_entries_; | 128 size_t max_entries_; |
| 140 | 129 |
| 141 // Time to live for cache entries. | 130 // Time to live for cache entries. |
| 142 base::TimeDelta success_entry_ttl_; | 131 base::TimeDelta success_entry_ttl_; |
| 143 base::TimeDelta failure_entry_ttl_; | 132 base::TimeDelta failure_entry_ttl_; |
| 144 | 133 |
| 145 // Map from hostname (presumably in lowercase canonicalized format) to | 134 // Map from hostname (presumably in lowercase canonicalized format) to |
| 146 // a resolved result entry. | 135 // a resolved result entry. |
| 147 EntryMap entries_; | 136 EntryMap entries_; |
| 148 | 137 |
| 149 DISALLOW_COPY_AND_ASSIGN(HostCache); | 138 DISALLOW_COPY_AND_ASSIGN(HostCache); |
| 150 }; | 139 }; |
| 151 | 140 |
| 152 } // namespace net | 141 } // namespace net |
| 153 | 142 |
| 154 #endif // NET_BASE_HOST_CACHE_H_ | 143 #endif // NET_BASE_HOST_CACHE_H_ |
| OLD | NEW |