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] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 22 | #include "base/lock.h" |
[email protected] | fd2f8afe | 2009-06-11 21:53:55 | [diff] [blame^] | 23 | #include "base/ref_counted.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | #include "chrome/browser/net/dns_host_info.h" |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 25 | #include "chrome/browser/net/referrer.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | #include "chrome/common/net/dns.h" |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 27 | #include "testing/gtest/include/gtest/gtest_prod.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | |
[email protected] | fd2f8afe | 2009-06-11 21:53:55 | [diff] [blame^] | 29 | namespace net { |
| 30 | class HostResolver; |
| 31 | } |
| 32 | |
| 33 | class MessageLoop; |
| 34 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | namespace chrome_browser_net { |
| 36 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | typedef chrome_common_net::NameList NameList; |
| 38 | typedef std::map<std::string, DnsHostInfo> Results; |
| 39 | |
[email protected] | fd2f8afe | 2009-06-11 21:53:55 | [diff] [blame^] | 40 | class DnsMaster : public base::RefCountedThreadSafe<DnsMaster> { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 41 | public: |
[email protected] | fd2f8afe | 2009-06-11 21:53:55 | [diff] [blame^] | 42 | // |max_concurrent| specifies how many concurrent (paralell) prefetches will |
| 43 | // be performed. Host lookups will be issued on the |host_resolver_loop| |
| 44 | // thread, using the |host_resolver| instance. |
| 45 | DnsMaster(net::HostResolver* host_resolver, |
| 46 | MessageLoop* host_resolver_loop, |
| 47 | size_t max_concurrent); |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 48 | ~DnsMaster(); |
[email protected] | b2b8b83 | 2009-02-06 19:03:29 | [diff] [blame] | 49 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 50 | // Cancel pending requests and prevent new ones from being made. |
| 51 | void Shutdown(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | |
| 53 | // In some circumstances, for privacy reasons, all results should be |
| 54 | // discarded. This method gracefully handles that activity. |
| 55 | // Destroy all our internal state, which shows what names we've looked up, and |
| 56 | // how long each has taken, etc. etc. We also destroy records of suggesses |
| 57 | // (cache hits etc.). |
| 58 | void DiscardAllResults(); |
| 59 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 60 | // Add hostname(s) to the queue for processing. |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 61 | void ResolveList(const NameList& hostnames, |
| 62 | DnsHostInfo::ResolutionMotivation motivation); |
| 63 | void Resolve(const std::string& hostname, |
| 64 | DnsHostInfo::ResolutionMotivation motivation); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | |
| 66 | // Get latency benefit of the prefetch that we are navigating to. |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 67 | bool AccruePrefetchBenefits(const GURL& referrer, |
| 68 | DnsHostInfo* navigation_info); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 69 | |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 70 | // Instigate prefetch of any domains we predict will be needed after this |
| 71 | // navigation. |
| 72 | void NavigatingTo(const std::string& host_name); |
| 73 | |
| 74 | // Record details of a navigation so that we can preresolve the host name |
| 75 | // ahead of time the next time the users navigates to the indicated host. |
| 76 | void NonlinkNavigation(const GURL& referrer, DnsHostInfo* navigation_info); |
| 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] | b2b8b83 | 2009-02-06 19:03:29 | [diff] [blame] | 101 | private: |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 102 | FRIEND_TEST(DnsMasterTest, BenefitLookupTest); |
| 103 | FRIEND_TEST(DnsMasterTest, ShutdownWhenResolutionIsPendingTest); |
| 104 | FRIEND_TEST(DnsMasterTest, SingleLookupTest); |
| 105 | FRIEND_TEST(DnsMasterTest, ConcurrentLookupTest); |
[email protected] | b170d9c | 2009-06-05 01:46:57 | [diff] [blame] | 106 | FRIEND_TEST(DnsMasterTest, DISABLED_MassiveConcurrentLookupTest); |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 107 | FRIEND_TEST(DnsMasterTest, PriorityQueuePushPopTest); |
| 108 | FRIEND_TEST(DnsMasterTest, PriorityQueueReorderTest); |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 109 | friend class WaitForResolutionHelper; // For testing. |
| 110 | |
| 111 | class LookupRequest; |
| 112 | |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 113 | // A simple priority queue for handling host names. |
| 114 | // Some names that are queued up have |motivation| that requires very rapid |
| 115 | // handling. For example, a sub-resource name lookup MUST be done before the |
| 116 | // actual sub-resource is fetched. In contrast, a name that was speculatively |
| 117 | // noted in a page has to be resolved before the user "gets around to" |
| 118 | // clicking on a link. By tagging (with a motivation) each push we make into |
| 119 | // this FIFO queue, the queue can re-order the more important names to service |
| 120 | // them sooner (relative to some low priority background resolutions). |
| 121 | class HostNameQueue { |
| 122 | public: |
| 123 | HostNameQueue(); |
| 124 | ~HostNameQueue(); |
| 125 | void Push(const std::string& hostname, |
| 126 | DnsHostInfo::ResolutionMotivation motivation); |
| 127 | bool IsEmpty() const; |
| 128 | std::string Pop(); |
| 129 | |
| 130 | private: |
| 131 | // The names in the queue that should be serviced (popped) ASAP. |
| 132 | std::queue<std::string> rush_queue_; |
| 133 | // The names in the queue that should only be serviced when rush_queue is |
| 134 | // empty. |
| 135 | std::queue<std::string> background_queue_; |
| 136 | |
| 137 | DISALLOW_COPY_AND_ASSIGN(HostNameQueue); |
| 138 | }; |
| 139 | |
[email protected] | b2b8b83 | 2009-02-06 19:03:29 | [diff] [blame] | 140 | // A map that is keyed with the hostnames that we've learned were the cause |
| 141 | // of loading additional hostnames. The list of additional hostnames in held |
| 142 | // in a Referrer instance, which is found in this type. |
| 143 | typedef std::map<std::string, Referrer> Referrers; |
[email protected] | 7c19b87b0 | 2009-01-26 16:19:44 | [diff] [blame] | 144 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 145 | // Only for testing. Returns true if hostname has been successfully resolved |
| 146 | // (name found). |
| 147 | bool WasFound(const std::string& hostname) { |
| 148 | AutoLock auto_lock(lock_); |
| 149 | return (results_.find(hostname) != results_.end()) && |
| 150 | results_[hostname].was_found(); |
| 151 | } |
| 152 | |
| 153 | // Only for testing. Return how long was the resolution |
| 154 | // or DnsHostInfo::kNullDuration if it hasn't been resolved yet. |
| 155 | base::TimeDelta GetResolutionDuration(const std::string& hostname) { |
| 156 | AutoLock auto_lock(lock_); |
| 157 | if (results_.find(hostname) == results_.end()) |
| 158 | return DnsHostInfo::kNullDuration; |
| 159 | return results_[hostname].resolve_duration(); |
| 160 | } |
| 161 | |
| 162 | // Only for testing; |
| 163 | size_t peak_pending_lookups() const { return peak_pending_lookups_; } |
| 164 | |
| 165 | // Access method for use by lookup request to pass resolution result. |
| 166 | void OnLookupFinished(LookupRequest* request, |
| 167 | const std::string& hostname, bool found); |
| 168 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 169 | // "PreLocked" means that the caller has already Acquired lock_ in the |
| 170 | // following method names. |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 171 | // Queue hostname for resolution. If queueing was done, return the pointer |
| 172 | // to the queued instance, otherwise return NULL. |
| 173 | DnsHostInfo* PreLockedResolve(const std::string& hostname, |
| 174 | DnsHostInfo::ResolutionMotivation motivation); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 175 | |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 176 | // Check to see if too much queuing delay has been noted for the given info, |
| 177 | // which indicates that there is "congestion" or growing delay in handling the |
| 178 | // resolution of names. Rather than letting this congestion potentially grow |
| 179 | // without bounds, we abandon our queued efforts at pre-resolutions in such a |
| 180 | // case. |
| 181 | // To do this, we will recycle |info|, as well as all queued items, back to |
| 182 | // the state they had before they were queued up. We can't do anything about |
| 183 | // the resolutions we've already sent off for processing on another thread, so |
| 184 | // we just let them complete. On a slow system, subject to congestion, this |
| 185 | // will greatly reduce the number of resolutions done, but it will assure that |
| 186 | // any resolutions that are done, are in a timely and hence potentially |
| 187 | // helpful manner. |
| 188 | bool PreLockedCongestionControlPerformed(DnsHostInfo* info); |
| 189 | |
| 190 | // Take lookup requests from work_queue_ and tell HostResolver to look them up |
| 191 | // asynchronously, provided we don't exceed concurrent resolution limit. |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 192 | void PreLockedScheduleLookups(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 193 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 194 | // Synchronize access to variables listed below. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 195 | Lock lock_; |
| 196 | |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 197 | // work_queue_ holds a list of names we need to look up. |
| 198 | HostNameQueue work_queue_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 199 | |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 200 | // results_ contains information for existing/prior prefetches. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 201 | Results results_; |
| 202 | |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 203 | // For each hostname that we might navigate to (that we've "learned about") |
| 204 | // we have a Referrer list. Each Referrer list has all hostnames we need to |
| 205 | // pre-resolve when there is a navigation to the orginial hostname. |
| 206 | Referrers referrers_; |
| 207 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 208 | std::set<LookupRequest*> pending_lookups_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 209 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 210 | // For testing, to verify that we don't exceed the limit. |
| 211 | size_t peak_pending_lookups_; |
[email protected] | b2b8b83 | 2009-02-06 19:03:29 | [diff] [blame] | 212 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 213 | // When true, we don't make new lookup requests. |
[email protected] | b2b8b83 | 2009-02-06 19:03:29 | [diff] [blame] | 214 | bool shutdown_; |
| 215 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 216 | // A list of successful events resulting from pre-fetching. |
| 217 | DnsHostInfo::DnsInfoTable cache_hits_; |
| 218 | // A map of hosts that were evicted from our cache (after we prefetched them) |
| 219 | // and before the HTTP stack tried to look them up. |
| 220 | Results cache_eviction_map_; |
| 221 | |
[email protected] | e085c30 | 2009-06-01 18:31:36 | [diff] [blame] | 222 | // The number of concurrent lookups currently allowed. |
| 223 | const size_t max_concurrent_lookups_; |
| 224 | |
[email protected] | fd2f8afe | 2009-06-11 21:53:55 | [diff] [blame^] | 225 | // The host resovler we warm DNS entries for. The resolver (which is not |
| 226 | // thread safe) should be accessed only on |host_resolver_loop_|. |
| 227 | net::HostResolver* host_resolver_; |
| 228 | MessageLoop* host_resolver_loop_; |
| 229 | |
[email protected] | e8013b3 | 2008-10-27 18:55:52 | [diff] [blame] | 230 | DISALLOW_COPY_AND_ASSIGN(DnsMaster); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | } // namespace chrome_browser_net |
| 234 | |
[email protected] | e8013b3 | 2008-10-27 18:55:52 | [diff] [blame] | 235 | #endif // CHROME_BROWSER_NET_DNS_MASTER_H_ |