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