blob: 0586b1c13aab23d9bc5c98dd1bcd0309c48ac001 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.commit09911bf2008-07-26 23:55:294
5// A DnsMaster object is instantiated once in the browser
[email protected]1933eb202009-02-19 18:23:256// process, and manages asynchronous resolution of DNS hostnames.
initial.commit09911bf2008-07-26 23:55:297// 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]1933eb202009-02-19 18:23:2512// lookup paths.
initial.commit09911bf2008-07-26 23:55:2913
[email protected]e8013b32008-10-27 18:55:5214#ifndef CHROME_BROWSER_NET_DNS_MASTER_H_
15#define CHROME_BROWSER_NET_DNS_MASTER_H_
initial.commit09911bf2008-07-26 23:55:2916
17#include <map>
18#include <queue>
[email protected]1933eb202009-02-19 18:23:2519#include <set>
initial.commit09911bf2008-07-26 23:55:2920#include <string>
21
[email protected]a918f872010-06-01 14:30:5122#include "base/gtest_prod_util.h"
[email protected]fd2f8afe2009-06-11 21:53:5523#include "base/ref_counted.h"
initial.commit09911bf2008-07-26 23:55:2924#include "chrome/browser/net/dns_host_info.h"
[email protected]21dae9b2008-11-06 23:32:5325#include "chrome/browser/net/referrer.h"
initial.commit09911bf2008-07-26 23:55:2926#include "chrome/common/net/dns.h"
[email protected]760d970a2010-05-18 00:39:1827#include "net/base/host_port_pair.h"
initial.commit09911bf2008-07-26 23:55:2928
[email protected]fd2f8afe2009-06-11 21:53:5529namespace net {
30class HostResolver;
[email protected]0ac83682010-01-22 17:46:2731} // namespace net
[email protected]fd2f8afe2009-06-11 21:53:5532
initial.commit09911bf2008-07-26 23:55:2933namespace chrome_browser_net {
34
initial.commit09911bf2008-07-26 23:55:2935typedef chrome_common_net::NameList NameList;
[email protected]760d970a2010-05-18 00:39:1836typedef std::map<net::HostPortPair, DnsHostInfo> Results;
initial.commit09911bf2008-07-26 23:55:2937
[email protected]ec86bea2009-12-08 18:35:1438// 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]fd2f8afe2009-06-11 21:53:5540class DnsMaster : public base::RefCountedThreadSafe<DnsMaster> {
initial.commit09911bf2008-07-26 23:55:2941 public:
[email protected]760d970a2010-05-18 00:39:1842 // 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]ec86bea2009-12-08 18:35:1447 // be performed. Host lookups will be issued through |host_resolver|.
[email protected]6fad2632009-11-02 05:59:3748 DnsMaster(net::HostResolver* host_resolver,
[email protected]760d970a2010-05-18 00:39:1849 base::TimeDelta max_queue_delay_ms, size_t max_concurrent,
50 bool preconnect_enabled);
[email protected]b2b8b832009-02-06 19:03:2951
[email protected]1933eb202009-02-19 18:23:2552 // Cancel pending requests and prevent new ones from being made.
53 void Shutdown();
initial.commit09911bf2008-07-26 23:55:2954
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]1933eb202009-02-19 18:23:2562 // Add hostname(s) to the queue for processing.
[email protected]21dae9b2008-11-06 23:32:5363 void ResolveList(const NameList& hostnames,
64 DnsHostInfo::ResolutionMotivation motivation);
[email protected]760d970a2010-05-18 00:39:1865 void Resolve(const net::HostPortPair& hostport,
[email protected]21dae9b2008-11-06 23:32:5366 DnsHostInfo::ResolutionMotivation motivation);
initial.commit09911bf2008-07-26 23:55:2967
68 // Get latency benefit of the prefetch that we are navigating to.
[email protected]760d970a2010-05-18 00:39:1869 bool AccruePrefetchBenefits(const net::HostPortPair& referrer,
[email protected]21dae9b2008-11-06 23:32:5370 DnsHostInfo* navigation_info);
initial.commit09911bf2008-07-26 23:55:2971
[email protected]21dae9b2008-11-06 23:32:5372 // Instigate prefetch of any domains we predict will be needed after this
73 // navigation.
[email protected]760d970a2010-05-18 00:39:1874 void NavigatingTo(const net::HostPortPair& hostport);
[email protected]21dae9b2008-11-06 23:32:5375
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]ec86bea2009-12-08 18:35:1478 // TODO(eroman): can this be a const& instead?
[email protected]760d970a2010-05-18 00:39:1879 void NonlinkNavigation(const net::HostPortPair& referrer,
[email protected]ec86bea2009-12-08 18:35:1480 const DnsHostInfo* navigation_info);
[email protected]21dae9b2008-11-06 23:32:5381
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.commit09911bf2008-07-26 23:55:2987 void GetHtmlInfo(std::string* output);
88
[email protected]03c5e862009-02-17 22:50:1489 // 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]ec86bea2009-12-08 18:35:14105 void DeserializeReferrersThenDelete(ListValue* referral_list) {
106 DeserializeReferrers(*referral_list);
107 delete referral_list;
108 }
109
[email protected]e695fbd62009-06-30 16:31:54110 // For unit test code only.
111 size_t max_concurrent_lookups() const { return max_concurrent_lookups_; }
112
[email protected]760d970a2010-05-18 00:39:18113 // Flag setting to use preconnection instead of just DNS pre-fetching.
114 bool preconnect_enabled() const { return preconnect_enabled_; }
115
[email protected]b2b8b832009-02-06 19:03:29116 private:
[email protected]7991a232009-11-06 01:55:48117 friend class base::RefCountedThreadSafe<DnsMaster>;
[email protected]a918f872010-06-01 14:30:51118 FRIEND_TEST_ALL_PREFIXES(DnsMasterTest, BenefitLookupTest);
119 FRIEND_TEST_ALL_PREFIXES(DnsMasterTest, ShutdownWhenResolutionIsPendingTest);
120 FRIEND_TEST_ALL_PREFIXES(DnsMasterTest, SingleLookupTest);
121 FRIEND_TEST_ALL_PREFIXES(DnsMasterTest, ConcurrentLookupTest);
122 FRIEND_TEST_ALL_PREFIXES(DnsMasterTest, MassiveConcurrentLookupTest);
123 FRIEND_TEST_ALL_PREFIXES(DnsMasterTest, PriorityQueuePushPopTest);
124 FRIEND_TEST_ALL_PREFIXES(DnsMasterTest, PriorityQueueReorderTest);
[email protected]1933eb202009-02-19 18:23:25125 friend class WaitForResolutionHelper; // For testing.
126
[email protected]7991a232009-11-06 01:55:48127 ~DnsMaster();
128
[email protected]1933eb202009-02-19 18:23:25129 class LookupRequest;
130
[email protected]a20bc092009-06-05 01:34:20131 // 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]760d970a2010-05-18 00:39:18143 void Push(const net::HostPortPair& hostport,
[email protected]a20bc092009-06-05 01:34:20144 DnsHostInfo::ResolutionMotivation motivation);
145 bool IsEmpty() const;
[email protected]760d970a2010-05-18 00:39:18146 net::HostPortPair Pop();
[email protected]a20bc092009-06-05 01:34:20147
148 private:
149 // The names in the queue that should be serviced (popped) ASAP.
[email protected]760d970a2010-05-18 00:39:18150 std::queue<net::HostPortPair> rush_queue_;
[email protected]a20bc092009-06-05 01:34:20151 // The names in the queue that should only be serviced when rush_queue is
152 // empty.
[email protected]760d970a2010-05-18 00:39:18153 std::queue<net::HostPortPair> background_queue_;
[email protected]a20bc092009-06-05 01:34:20154
155 DISALLOW_COPY_AND_ASSIGN(HostNameQueue);
156 };
157
[email protected]760d970a2010-05-18 00:39:18158 // 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]7c19b87b02009-01-26 16:19:44162
[email protected]1933eb202009-02-19 18:23:25163 // Only for testing. Returns true if hostname has been successfully resolved
164 // (name found).
[email protected]760d970a2010-05-18 00:39:18165 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]1933eb202009-02-19 18:23:25169 }
170
171 // Only for testing. Return how long was the resolution
172 // or DnsHostInfo::kNullDuration if it hasn't been resolved yet.
[email protected]760d970a2010-05-18 00:39:18173 base::TimeDelta GetResolutionDuration(const net::HostPortPair& hostport) {
174
175 if (results_.find(hostport) == results_.end())
[email protected]1933eb202009-02-19 18:23:25176 return DnsHostInfo::kNullDuration;
[email protected]760d970a2010-05-18 00:39:18177 return results_[hostport].resolve_duration();
[email protected]1933eb202009-02-19 18:23:25178 }
179
180 // Only for testing;
181 size_t peak_pending_lookups() const { return peak_pending_lookups_; }
182
[email protected]85398532009-06-16 21:32:18183 // Access method for use by async lookup request to pass resolution result.
[email protected]1933eb202009-02-19 18:23:25184 void OnLookupFinished(LookupRequest* request,
[email protected]760d970a2010-05-18 00:39:18185 const net::HostPortPair& hostport, bool found);
[email protected]1933eb202009-02-19 18:23:25186
[email protected]85398532009-06-16 21:32:18187 // Underlying method for both async and synchronous lookup to update state.
[email protected]ec86bea2009-12-08 18:35:14188 void LookupFinished(LookupRequest* request,
[email protected]760d970a2010-05-18 00:39:18189 const net::HostPortPair& hostport, bool found);
[email protected]85398532009-06-16 21:32:18190
[email protected]21dae9b2008-11-06 23:32:53191 // Queue hostname for resolution. If queueing was done, return the pointer
192 // to the queued instance, otherwise return NULL.
[email protected]760d970a2010-05-18 00:39:18193 DnsHostInfo* AppendToResolutionQueue(const net::HostPortPair& hostport,
[email protected]ec86bea2009-12-08 18:35:14194 DnsHostInfo::ResolutionMotivation motivation);
initial.commit09911bf2008-07-26 23:55:29195
[email protected]a20bc092009-06-05 01:34:20196 // 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]ec86bea2009-12-08 18:35:14208 bool CongestionControlPerformed(DnsHostInfo* info);
[email protected]a20bc092009-06-05 01:34:20209
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]ec86bea2009-12-08 18:35:14212 void StartSomeQueuedResolutions();
initial.commit09911bf2008-07-26 23:55:29213
[email protected]a20bc092009-06-05 01:34:20214 // work_queue_ holds a list of names we need to look up.
215 HostNameQueue work_queue_;
initial.commit09911bf2008-07-26 23:55:29216
[email protected]21dae9b2008-11-06 23:32:53217 // results_ contains information for existing/prior prefetches.
initial.commit09911bf2008-07-26 23:55:29218 Results results_;
219
[email protected]760d970a2010-05-18 00:39:18220 // For each URL that we might navigate to (that we've "learned about")
[email protected]21dae9b2008-11-06 23:32:53221 // 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]1933eb202009-02-19 18:23:25225 std::set<LookupRequest*> pending_lookups_;
initial.commit09911bf2008-07-26 23:55:29226
[email protected]1933eb202009-02-19 18:23:25227 // For testing, to verify that we don't exceed the limit.
228 size_t peak_pending_lookups_;
[email protected]b2b8b832009-02-06 19:03:29229
[email protected]1933eb202009-02-19 18:23:25230 // When true, we don't make new lookup requests.
[email protected]b2b8b832009-02-06 19:03:29231 bool shutdown_;
232
initial.commit09911bf2008-07-26 23:55:29233 // 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]e085c302009-06-01 18:31:36239 // The number of concurrent lookups currently allowed.
240 const size_t max_concurrent_lookups_;
241
[email protected]602faf3c2009-06-27 14:35:44242 // 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]760d970a2010-05-18 00:39:18244 const base::TimeDelta max_queue_delay_;
[email protected]602faf3c2009-06-27 14:35:44245
[email protected]ec86bea2009-12-08 18:35:14246 // The host resovler we warm DNS entries for.
[email protected]94a0d3d92009-06-27 01:50:14247 scoped_refptr<net::HostResolver> host_resolver_;
[email protected]fd2f8afe2009-06-11 21:53:55248
[email protected]760d970a2010-05-18 00:39:18249 // 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]e8013b32008-10-27 18:55:52253 DISALLOW_COPY_AND_ASSIGN(DnsMaster);
initial.commit09911bf2008-07-26 23:55:29254};
255
256} // namespace chrome_browser_net
257
[email protected]e8013b32008-10-27 18:55:52258#endif // CHROME_BROWSER_NET_DNS_MASTER_H_