| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <string> | 9 #include <string> |
| 9 | 10 |
| 10 #include "base/hash_tables.h" | |
| 11 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "net/base/address_family.h" |
| 13 #include "net/base/address_list.h" | 14 #include "net/base/address_list.h" |
| 14 #include "testing/gtest/include/gtest/gtest_prod.h" | 15 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 15 | 16 |
| 16 namespace net { | 17 namespace net { |
| 17 | 18 |
| 18 // Cache used by HostResolver to map hostnames to their resolved result. | 19 // Cache used by HostResolver to map hostnames to their resolved result. |
| 19 class HostCache { | 20 class HostCache { |
| 20 public: | 21 public: |
| 21 // Stores the latest address list that was looked up for a hostname. | 22 // Stores the latest address list that was looked up for a hostname. |
| 22 struct Entry : public base::RefCounted<Entry> { | 23 struct Entry : public base::RefCounted<Entry> { |
| 23 Entry(int error, const AddressList& addrlist, base::TimeTicks expiration); | 24 Entry(int error, const AddressList& addrlist, base::TimeTicks expiration); |
| 24 ~Entry(); | 25 ~Entry(); |
| 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. |
| 31 base::TimeTicks expiration; | 32 base::TimeTicks expiration; |
| 32 }; | 33 }; |
| 33 | 34 |
| 34 typedef base::hash_map<std::string, scoped_refptr<Entry> > EntryMap; | 35 struct Key { |
| 36 Key(const std::string& hostname, AddressFamily address_family) |
| 37 : hostname(hostname), address_family(address_family) {} |
| 38 |
| 39 bool operator==(const Key& other) const { |
| 40 return other.hostname == hostname && |
| 41 other.address_family == address_family; |
| 42 } |
| 43 |
| 44 bool operator<(const Key& other) const { |
| 45 if (address_family < other.address_family) |
| 46 return true; |
| 47 return hostname < other.hostname; |
| 48 } |
| 49 |
| 50 std::string hostname; |
| 51 AddressFamily address_family; |
| 52 }; |
| 53 |
| 54 typedef std::map<Key, scoped_refptr<Entry> > EntryMap; |
| 35 | 55 |
| 36 // Constructs a HostCache whose entries are valid for |cache_duration_ms| | 56 // Constructs a HostCache whose entries are valid for |cache_duration_ms| |
| 37 // milliseconds. The cache will store up to |max_entries|. | 57 // milliseconds. The cache will store up to |max_entries|. |
| 38 HostCache(size_t max_entries, size_t cache_duration_ms); | 58 HostCache(size_t max_entries, size_t cache_duration_ms); |
| 39 | 59 |
| 40 ~HostCache(); | 60 ~HostCache(); |
| 41 | 61 |
| 42 // Returns a pointer to the entry for |hostname|, which is valid at time | 62 // Returns a pointer to the entry for |key|, which is valid at time |
| 43 // |now|. If there is no such entry, returns NULL. | 63 // |now|. If there is no such entry, returns NULL. |
| 44 const Entry* Lookup(const std::string& hostname, base::TimeTicks now) const; | 64 const Entry* Lookup(const Key& key, base::TimeTicks now) const; |
| 45 | 65 |
| 46 // Overwrites or creates an entry for |hostname|. Returns the pointer to the | 66 // Overwrites or creates an entry for |key|. Returns the pointer to the |
| 47 // entry, or NULL on failure (fails if caching is disabled). | 67 // entry, or NULL on failure (fails if caching is disabled). |
| 48 // (|error|, |addrlist|) is the value to set, and |now| is the current | 68 // (|error|, |addrlist|) is the value to set, and |now| is the current |
| 49 // timestamp. | 69 // timestamp. |
| 50 Entry* Set(const std::string& hostname, | 70 Entry* Set(const Key& key, |
| 51 int error, | 71 int error, |
| 52 const AddressList addrlist, | 72 const AddressList addrlist, |
| 53 base::TimeTicks now); | 73 base::TimeTicks now); |
| 54 | 74 |
| 55 // Returns true if this HostCache can contain no entries. | 75 // Returns true if this HostCache can contain no entries. |
| 56 bool caching_is_disabled() const { | 76 bool caching_is_disabled() const { |
| 57 return max_entries_ == 0; | 77 return max_entries_ == 0; |
| 58 } | 78 } |
| 59 | 79 |
| 60 // Returns the number of entries in the cache. | 80 // Returns the number of entries in the cache. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 // Map from hostname (presumably in lowercase canonicalized format) to | 115 // Map from hostname (presumably in lowercase canonicalized format) to |
| 96 // a resolved result entry. | 116 // a resolved result entry. |
| 97 EntryMap entries_; | 117 EntryMap entries_; |
| 98 | 118 |
| 99 DISALLOW_COPY_AND_ASSIGN(HostCache); | 119 DISALLOW_COPY_AND_ASSIGN(HostCache); |
| 100 }; | 120 }; |
| 101 | 121 |
| 102 } // namespace net | 122 } // namespace net |
| 103 | 123 |
| 104 #endif // NET_BASE_HOST_CACHE_H_ | 124 #endif // NET_BASE_HOST_CACHE_H_ |
| OLD | NEW |