license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | // A DnsMaster object is instantiated once in the browser |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 6 | // process, and manages asynchronous resolution of DNS hostnames. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | // Most hostname lists are sent out by renderer processes, and |
| 8 | // involve lists of hostnames that *might* be used in the near |
| 9 | // future by the browsing user. The goal of this class is to |
| 10 | // cause the underlying DNS structure to lookup a hostname before |
| 11 | // it is really needed, and hence reduce latency in the standard |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 12 | // lookup paths. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | |
[email protected] | e8013b3 | 2008-10-27 18:55:52 | [diff] [blame] | 14 | #ifndef CHROME_BROWSER_NET_DNS_MASTER_H_ |
| 15 | #define CHROME_BROWSER_NET_DNS_MASTER_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 16 | |
| 17 | #include <map> |
| 18 | #include <queue> |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 19 | #include <set> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 20 | #include <string> |
| 21 | |
[email protected] | fd2f8afe | 2009-06-11 21:53:55 | [diff] [blame] | 22 | #include "base/ref_counted.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 23 | #include "chrome/browser/net/dns_host_info.h" |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 24 | #include "chrome/browser/net/referrer.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | #include "chrome/common/net/dns.h" |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 26 | #include "testing/gtest/include/gtest/gtest_prod.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 28 | using base::TimeDelta; |
| 29 | |
[email protected] | fd2f8afe | 2009-06-11 21:53:55 | [diff] [blame] | 30 | namespace net { |
| 31 | class HostResolver; |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame^] | 32 | } // namespace net |
[email protected] | fd2f8afe | 2009-06-11 21:53:55 | [diff] [blame] | 33 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | namespace chrome_browser_net { |
| 35 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | typedef chrome_common_net::NameList NameList; |
| 37 | typedef std::map<std::string, DnsHostInfo> Results; |
| 38 | |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 39 | // Note that DNS master is not thread safe, and must only be called from |
| 40 | // the IO thread. Failure to do so will result in a DCHECK at runtime. |
[email protected] | fd2f8afe | 2009-06-11 21:53:55 | [diff] [blame] | 41 | class DnsMaster : public base::RefCountedThreadSafe<DnsMaster> { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | public: |
[email protected] | 3a29e291 | 2009-11-23 18:34:55 | [diff] [blame] | 43 | // |max_concurrent| specifies how many concurrent (parallel) prefetches will |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 44 | // be performed. Host lookups will be issued through |host_resolver|. |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 45 | DnsMaster(net::HostResolver* host_resolver, |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 46 | TimeDelta max_queue_delay_ms, size_t max_concurrent); |
[email protected] | b2b8b83 | 2009-02-06 19:03:29 | [diff] [blame] | 47 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 48 | // Cancel pending requests and prevent new ones from being made. |
| 49 | void Shutdown(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | |
| 51 | // In some circumstances, for privacy reasons, all results should be |
| 52 | // discarded. This method gracefully handles that activity. |
| 53 | // Destroy all our internal state, which shows what names we've looked up, and |
| 54 | // how long each has taken, etc. etc. We also destroy records of suggesses |
| 55 | // (cache hits etc.). |
| 56 | void DiscardAllResults(); |
| 57 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 58 | // Add hostname(s) to the queue for processing. |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 59 | void ResolveList(const NameList& hostnames, |
| 60 | DnsHostInfo::ResolutionMotivation motivation); |
| 61 | void Resolve(const std::string& hostname, |
| 62 | DnsHostInfo::ResolutionMotivation motivation); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | |
| 64 | // Get latency benefit of the prefetch that we are navigating to. |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 65 | bool AccruePrefetchBenefits(const GURL& referrer, |
| 66 | DnsHostInfo* navigation_info); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 68 | // Instigate prefetch of any domains we predict will be needed after this |
| 69 | // navigation. |
| 70 | void NavigatingTo(const std::string& host_name); |
| 71 | |
| 72 | // Record details of a navigation so that we can preresolve the host name |
| 73 | // ahead of time the next time the users navigates to the indicated host. |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 74 | // TODO(eroman): can this be a const& instead? |
| 75 | void NonlinkNavigation(const GURL& referrer, |
| 76 | const DnsHostInfo* navigation_info); |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 77 | |
| 78 | // Dump HTML table containing list of referrers for about:dns. |
| 79 | void GetHtmlReferrerLists(std::string* output); |
| 80 | |
| 81 | // Dump the list of currently know referrer domains and related prefetchable |
| 82 | // domains. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | void GetHtmlInfo(std::string* output); |
| 84 | |
[email protected] | 03c5e86 | 2009-02-17 22:50:14 | [diff] [blame] | 85 | // Discard any referrer for which all the suggested host names are currently |
| 86 | // annotated with no user latency reduction. Also scale down (diminish) the |
| 87 | // total benefit of those that did help, so that their reported contribution |
| 88 | // wll go done by a factor of 2 each time we trim (moving the referrer closer |
| 89 | // to being discarded at a future Trim). |
| 90 | void TrimReferrers(); |
| 91 | |
| 92 | // Construct a ListValue object that contains all the data in the referrers_ |
| 93 | // so that it can be persisted in a pref. |
| 94 | void SerializeReferrers(ListValue* referral_list); |
| 95 | |
| 96 | // Process a ListValue that contains all the data from a previous reference |
| 97 | // list, as constructed by SerializeReferrers(), and add all the identified |
| 98 | // values into the current referrer list. |
| 99 | void DeserializeReferrers(const ListValue& referral_list); |
| 100 | |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 101 | void DeserializeReferrersThenDelete(ListValue* referral_list) { |
| 102 | DeserializeReferrers(*referral_list); |
| 103 | delete referral_list; |
| 104 | } |
| 105 | |
[email protected] | e695fbd6 | 2009-06-30 16:31:54 | [diff] [blame] | 106 | // For unit test code only. |
| 107 | size_t max_concurrent_lookups() const { return max_concurrent_lookups_; } |
| 108 | |
[email protected] | b2b8b83 | 2009-02-06 19:03:29 | [diff] [blame] | 109 | private: |
[email protected] | 7991a23 | 2009-11-06 01:55:48 | [diff] [blame] | 110 | friend class base::RefCountedThreadSafe<DnsMaster>; |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 111 | FRIEND_TEST(DnsMasterTest, BenefitLookupTest); |
| 112 | FRIEND_TEST(DnsMasterTest, ShutdownWhenResolutionIsPendingTest); |
| 113 | FRIEND_TEST(DnsMasterTest, SingleLookupTest); |
| 114 | FRIEND_TEST(DnsMasterTest, ConcurrentLookupTest); |
[email protected] | 7d1e0d0 | 2009-07-29 21:51:41 | [diff] [blame] | 115 | FRIEND_TEST(DnsMasterTest, MassiveConcurrentLookupTest); |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 116 | FRIEND_TEST(DnsMasterTest, PriorityQueuePushPopTest); |
| 117 | FRIEND_TEST(DnsMasterTest, PriorityQueueReorderTest); |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 118 | friend class WaitForResolutionHelper; // For testing. |
| 119 | |
[email protected] | 7991a23 | 2009-11-06 01:55:48 | [diff] [blame] | 120 | ~DnsMaster(); |
| 121 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 122 | class LookupRequest; |
| 123 | |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 124 | // A simple priority queue for handling host names. |
| 125 | // Some names that are queued up have |motivation| that requires very rapid |
| 126 | // handling. For example, a sub-resource name lookup MUST be done before the |
| 127 | // actual sub-resource is fetched. In contrast, a name that was speculatively |
| 128 | // noted in a page has to be resolved before the user "gets around to" |
| 129 | // clicking on a link. By tagging (with a motivation) each push we make into |
| 130 | // this FIFO queue, the queue can re-order the more important names to service |
| 131 | // them sooner (relative to some low priority background resolutions). |
| 132 | class HostNameQueue { |
| 133 | public: |
| 134 | HostNameQueue(); |
| 135 | ~HostNameQueue(); |
| 136 | void Push(const std::string& hostname, |
| 137 | DnsHostInfo::ResolutionMotivation motivation); |
| 138 | bool IsEmpty() const; |
| 139 | std::string Pop(); |
| 140 | |
| 141 | private: |
| 142 | // The names in the queue that should be serviced (popped) ASAP. |
| 143 | std::queue<std::string> rush_queue_; |
| 144 | // The names in the queue that should only be serviced when rush_queue is |
| 145 | // empty. |
| 146 | std::queue<std::string> background_queue_; |
| 147 | |
| 148 | DISALLOW_COPY_AND_ASSIGN(HostNameQueue); |
| 149 | }; |
| 150 | |
[email protected] | b2b8b83 | 2009-02-06 19:03:29 | [diff] [blame] | 151 | // A map that is keyed with the hostnames that we've learned were the cause |
| 152 | // of loading additional hostnames. The list of additional hostnames in held |
| 153 | // in a Referrer instance, which is found in this type. |
| 154 | typedef std::map<std::string, Referrer> Referrers; |
[email protected] | 7c19b87b0 | 2009-01-26 16:19:44 | [diff] [blame] | 155 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 156 | // Only for testing. Returns true if hostname has been successfully resolved |
| 157 | // (name found). |
| 158 | bool WasFound(const std::string& hostname) { |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 159 | return (results_.find(hostname) != results_.end()) && |
| 160 | results_[hostname].was_found(); |
| 161 | } |
| 162 | |
| 163 | // Only for testing. Return how long was the resolution |
| 164 | // or DnsHostInfo::kNullDuration if it hasn't been resolved yet. |
| 165 | base::TimeDelta GetResolutionDuration(const std::string& hostname) { |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 166 | if (results_.find(hostname) == results_.end()) |
| 167 | return DnsHostInfo::kNullDuration; |
| 168 | return results_[hostname].resolve_duration(); |
| 169 | } |
| 170 | |
| 171 | // Only for testing; |
| 172 | size_t peak_pending_lookups() const { return peak_pending_lookups_; } |
| 173 | |
[email protected] | 8539853 | 2009-06-16 21:32:18 | [diff] [blame] | 174 | // Access method for use by async lookup request to pass resolution result. |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 175 | void OnLookupFinished(LookupRequest* request, |
| 176 | const std::string& hostname, bool found); |
| 177 | |
[email protected] | 8539853 | 2009-06-16 21:32:18 | [diff] [blame] | 178 | // Underlying method for both async and synchronous lookup to update state. |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 179 | void LookupFinished(LookupRequest* request, |
| 180 | const std::string& hostname, |
| 181 | bool found); |
[email protected] | 8539853 | 2009-06-16 21:32:18 | [diff] [blame] | 182 | |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 183 | // Queue hostname for resolution. If queueing was done, return the pointer |
| 184 | // to the queued instance, otherwise return NULL. |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 185 | DnsHostInfo* AppendToResolutionQueue(const std::string& hostname, |
| 186 | DnsHostInfo::ResolutionMotivation motivation); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 187 | |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 188 | // Check to see if too much queuing delay has been noted for the given info, |
| 189 | // which indicates that there is "congestion" or growing delay in handling the |
| 190 | // resolution of names. Rather than letting this congestion potentially grow |
| 191 | // without bounds, we abandon our queued efforts at pre-resolutions in such a |
| 192 | // case. |
| 193 | // To do this, we will recycle |info|, as well as all queued items, back to |
| 194 | // the state they had before they were queued up. We can't do anything about |
| 195 | // the resolutions we've already sent off for processing on another thread, so |
| 196 | // we just let them complete. On a slow system, subject to congestion, this |
| 197 | // will greatly reduce the number of resolutions done, but it will assure that |
| 198 | // any resolutions that are done, are in a timely and hence potentially |
| 199 | // helpful manner. |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 200 | bool CongestionControlPerformed(DnsHostInfo* info); |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 201 | |
| 202 | // Take lookup requests from work_queue_ and tell HostResolver to look them up |
| 203 | // asynchronously, provided we don't exceed concurrent resolution limit. |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 204 | void StartSomeQueuedResolutions(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 205 | |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 206 | // work_queue_ holds a list of names we need to look up. |
| 207 | HostNameQueue work_queue_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 208 | |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 209 | // results_ contains information for existing/prior prefetches. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 210 | Results results_; |
| 211 | |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 212 | // For each hostname that we might navigate to (that we've "learned about") |
| 213 | // we have a Referrer list. Each Referrer list has all hostnames we need to |
| 214 | // pre-resolve when there is a navigation to the orginial hostname. |
| 215 | Referrers referrers_; |
| 216 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 217 | std::set<LookupRequest*> pending_lookups_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 218 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 219 | // For testing, to verify that we don't exceed the limit. |
| 220 | size_t peak_pending_lookups_; |
[email protected] | b2b8b83 | 2009-02-06 19:03:29 | [diff] [blame] | 221 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 222 | // When true, we don't make new lookup requests. |
[email protected] | b2b8b83 | 2009-02-06 19:03:29 | [diff] [blame] | 223 | bool shutdown_; |
| 224 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 225 | // A list of successful events resulting from pre-fetching. |
| 226 | DnsHostInfo::DnsInfoTable cache_hits_; |
| 227 | // A map of hosts that were evicted from our cache (after we prefetched them) |
| 228 | // and before the HTTP stack tried to look them up. |
| 229 | Results cache_eviction_map_; |
| 230 | |
[email protected] | e085c30 | 2009-06-01 18:31:36 | [diff] [blame] | 231 | // The number of concurrent lookups currently allowed. |
| 232 | const size_t max_concurrent_lookups_; |
| 233 | |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 234 | // The maximum queueing delay that is acceptable before we enter congestion |
| 235 | // reduction mode, and discard all queued (but not yet assigned) resolutions. |
| 236 | const TimeDelta max_queue_delay_; |
| 237 | |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 238 | // The host resovler we warm DNS entries for. |
[email protected] | 94a0d3d9 | 2009-06-27 01:50:14 | [diff] [blame] | 239 | scoped_refptr<net::HostResolver> host_resolver_; |
[email protected] | fd2f8afe | 2009-06-11 21:53:55 | [diff] [blame] | 240 | |
[email protected] | e8013b3 | 2008-10-27 18:55:52 | [diff] [blame] | 241 | DISALLOW_COPY_AND_ASSIGN(DnsMaster); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | } // namespace chrome_browser_net |
| 245 | |
[email protected] | e8013b3 | 2008-10-27 18:55:52 | [diff] [blame] | 246 | #endif // CHROME_BROWSER_NET_DNS_MASTER_H_ |