blob: 608dba86a6a634512e62c260dc14e93d1dd477a4 [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]1933eb202009-02-19 18:23:2522#include "base/lock.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]1933eb202009-02-19 18:23:2527#include "testing/gtest/include/gtest/gtest_prod.h"
initial.commit09911bf2008-07-26 23:55:2928
[email protected]602faf3c2009-06-27 14:35:4429using base::TimeDelta;
30
[email protected]fd2f8afe2009-06-11 21:53:5531namespace net {
32class HostResolver;
33}
34
35class MessageLoop;
36
initial.commit09911bf2008-07-26 23:55:2937namespace chrome_browser_net {
38
initial.commit09911bf2008-07-26 23:55:2939typedef chrome_common_net::NameList NameList;
40typedef std::map<std::string, DnsHostInfo> Results;
41
[email protected]fd2f8afe2009-06-11 21:53:5542class DnsMaster : public base::RefCountedThreadSafe<DnsMaster> {
initial.commit09911bf2008-07-26 23:55:2943 public:
[email protected]fd2f8afe2009-06-11 21:53:5544 // |max_concurrent| specifies how many concurrent (paralell) prefetches will
45 // be performed. Host lookups will be issued on the |host_resolver_loop|
46 // thread, using the |host_resolver| instance.
[email protected]602faf3c2009-06-27 14:35:4447 DnsMaster(net::HostResolver* host_resolver, MessageLoop* host_resolver_loop,
48 TimeDelta max_queue_delay_ms, size_t max_concurrent);
[email protected]1933eb202009-02-19 18:23:2549 ~DnsMaster();
[email protected]b2b8b832009-02-06 19:03:2950
[email protected]1933eb202009-02-19 18:23:2551 // Cancel pending requests and prevent new ones from being made.
52 void Shutdown();
initial.commit09911bf2008-07-26 23:55:2953
54 // In some circumstances, for privacy reasons, all results should be
55 // discarded. This method gracefully handles that activity.
56 // Destroy all our internal state, which shows what names we've looked up, and
57 // how long each has taken, etc. etc. We also destroy records of suggesses
58 // (cache hits etc.).
59 void DiscardAllResults();
60
[email protected]1933eb202009-02-19 18:23:2561 // Add hostname(s) to the queue for processing.
[email protected]21dae9b2008-11-06 23:32:5362 void ResolveList(const NameList& hostnames,
63 DnsHostInfo::ResolutionMotivation motivation);
64 void Resolve(const std::string& hostname,
65 DnsHostInfo::ResolutionMotivation motivation);
initial.commit09911bf2008-07-26 23:55:2966
67 // Get latency benefit of the prefetch that we are navigating to.
[email protected]21dae9b2008-11-06 23:32:5368 bool AccruePrefetchBenefits(const GURL& referrer,
69 DnsHostInfo* navigation_info);
initial.commit09911bf2008-07-26 23:55:2970
[email protected]21dae9b2008-11-06 23:32:5371 // Instigate prefetch of any domains we predict will be needed after this
72 // navigation.
73 void NavigatingTo(const std::string& host_name);
74
75 // Record details of a navigation so that we can preresolve the host name
76 // ahead of time the next time the users navigates to the indicated host.
77 void NonlinkNavigation(const GURL& referrer, DnsHostInfo* navigation_info);
78
79 // Dump HTML table containing list of referrers for about:dns.
80 void GetHtmlReferrerLists(std::string* output);
81
82 // Dump the list of currently know referrer domains and related prefetchable
83 // domains.
initial.commit09911bf2008-07-26 23:55:2984 void GetHtmlInfo(std::string* output);
85
[email protected]03c5e862009-02-17 22:50:1486 // Discard any referrer for which all the suggested host names are currently
87 // annotated with no user latency reduction. Also scale down (diminish) the
88 // total benefit of those that did help, so that their reported contribution
89 // wll go done by a factor of 2 each time we trim (moving the referrer closer
90 // to being discarded at a future Trim).
91 void TrimReferrers();
92
93 // Construct a ListValue object that contains all the data in the referrers_
94 // so that it can be persisted in a pref.
95 void SerializeReferrers(ListValue* referral_list);
96
97 // Process a ListValue that contains all the data from a previous reference
98 // list, as constructed by SerializeReferrers(), and add all the identified
99 // values into the current referrer list.
100 void DeserializeReferrers(const ListValue& referral_list);
101
[email protected]e695fbd62009-06-30 16:31:54102 // For unit test code only.
103 size_t max_concurrent_lookups() const { return max_concurrent_lookups_; }
104
[email protected]b2b8b832009-02-06 19:03:29105 private:
[email protected]1933eb202009-02-19 18:23:25106 FRIEND_TEST(DnsMasterTest, BenefitLookupTest);
107 FRIEND_TEST(DnsMasterTest, ShutdownWhenResolutionIsPendingTest);
108 FRIEND_TEST(DnsMasterTest, SingleLookupTest);
109 FRIEND_TEST(DnsMasterTest, ConcurrentLookupTest);
[email protected]b170d9c2009-06-05 01:46:57110 FRIEND_TEST(DnsMasterTest, DISABLED_MassiveConcurrentLookupTest);
[email protected]a20bc092009-06-05 01:34:20111 FRIEND_TEST(DnsMasterTest, PriorityQueuePushPopTest);
112 FRIEND_TEST(DnsMasterTest, PriorityQueueReorderTest);
[email protected]1933eb202009-02-19 18:23:25113 friend class WaitForResolutionHelper; // For testing.
114
115 class LookupRequest;
116
[email protected]a20bc092009-06-05 01:34:20117 // A simple priority queue for handling host names.
118 // Some names that are queued up have |motivation| that requires very rapid
119 // handling. For example, a sub-resource name lookup MUST be done before the
120 // actual sub-resource is fetched. In contrast, a name that was speculatively
121 // noted in a page has to be resolved before the user "gets around to"
122 // clicking on a link. By tagging (with a motivation) each push we make into
123 // this FIFO queue, the queue can re-order the more important names to service
124 // them sooner (relative to some low priority background resolutions).
125 class HostNameQueue {
126 public:
127 HostNameQueue();
128 ~HostNameQueue();
129 void Push(const std::string& hostname,
130 DnsHostInfo::ResolutionMotivation motivation);
131 bool IsEmpty() const;
132 std::string Pop();
133
134 private:
135 // The names in the queue that should be serviced (popped) ASAP.
136 std::queue<std::string> rush_queue_;
137 // The names in the queue that should only be serviced when rush_queue is
138 // empty.
139 std::queue<std::string> background_queue_;
140
141 DISALLOW_COPY_AND_ASSIGN(HostNameQueue);
142 };
143
[email protected]b2b8b832009-02-06 19:03:29144 // A map that is keyed with the hostnames that we've learned were the cause
145 // of loading additional hostnames. The list of additional hostnames in held
146 // in a Referrer instance, which is found in this type.
147 typedef std::map<std::string, Referrer> Referrers;
[email protected]7c19b87b02009-01-26 16:19:44148
[email protected]1933eb202009-02-19 18:23:25149 // Only for testing. Returns true if hostname has been successfully resolved
150 // (name found).
151 bool WasFound(const std::string& hostname) {
152 AutoLock auto_lock(lock_);
153 return (results_.find(hostname) != results_.end()) &&
154 results_[hostname].was_found();
155 }
156
157 // Only for testing. Return how long was the resolution
158 // or DnsHostInfo::kNullDuration if it hasn't been resolved yet.
159 base::TimeDelta GetResolutionDuration(const std::string& hostname) {
160 AutoLock auto_lock(lock_);
161 if (results_.find(hostname) == results_.end())
162 return DnsHostInfo::kNullDuration;
163 return results_[hostname].resolve_duration();
164 }
165
166 // Only for testing;
167 size_t peak_pending_lookups() const { return peak_pending_lookups_; }
168
[email protected]85398532009-06-16 21:32:18169 // Access method for use by async lookup request to pass resolution result.
[email protected]1933eb202009-02-19 18:23:25170 void OnLookupFinished(LookupRequest* request,
171 const std::string& hostname, bool found);
172
[email protected]85398532009-06-16 21:32:18173 // Underlying method for both async and synchronous lookup to update state.
174 void PrelockedLookupFinished(LookupRequest* request,
175 const std::string& hostname,
176 bool found);
177
initial.commit09911bf2008-07-26 23:55:29178 // "PreLocked" means that the caller has already Acquired lock_ in the
179 // following method names.
[email protected]21dae9b2008-11-06 23:32:53180 // Queue hostname for resolution. If queueing was done, return the pointer
181 // to the queued instance, otherwise return NULL.
182 DnsHostInfo* PreLockedResolve(const std::string& hostname,
183 DnsHostInfo::ResolutionMotivation motivation);
initial.commit09911bf2008-07-26 23:55:29184
[email protected]a20bc092009-06-05 01:34:20185 // Check to see if too much queuing delay has been noted for the given info,
186 // which indicates that there is "congestion" or growing delay in handling the
187 // resolution of names. Rather than letting this congestion potentially grow
188 // without bounds, we abandon our queued efforts at pre-resolutions in such a
189 // case.
190 // To do this, we will recycle |info|, as well as all queued items, back to
191 // the state they had before they were queued up. We can't do anything about
192 // the resolutions we've already sent off for processing on another thread, so
193 // we just let them complete. On a slow system, subject to congestion, this
194 // will greatly reduce the number of resolutions done, but it will assure that
195 // any resolutions that are done, are in a timely and hence potentially
196 // helpful manner.
197 bool PreLockedCongestionControlPerformed(DnsHostInfo* info);
198
199 // Take lookup requests from work_queue_ and tell HostResolver to look them up
200 // asynchronously, provided we don't exceed concurrent resolution limit.
[email protected]1933eb202009-02-19 18:23:25201 void PreLockedScheduleLookups();
initial.commit09911bf2008-07-26 23:55:29202
[email protected]1933eb202009-02-19 18:23:25203 // Synchronize access to variables listed below.
initial.commit09911bf2008-07-26 23:55:29204 Lock lock_;
205
[email protected]a20bc092009-06-05 01:34:20206 // work_queue_ holds a list of names we need to look up.
207 HostNameQueue work_queue_;
initial.commit09911bf2008-07-26 23:55:29208
[email protected]21dae9b2008-11-06 23:32:53209 // results_ contains information for existing/prior prefetches.
initial.commit09911bf2008-07-26 23:55:29210 Results results_;
211
[email protected]21dae9b2008-11-06 23:32:53212 // 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]1933eb202009-02-19 18:23:25217 std::set<LookupRequest*> pending_lookups_;
initial.commit09911bf2008-07-26 23:55:29218
[email protected]1933eb202009-02-19 18:23:25219 // For testing, to verify that we don't exceed the limit.
220 size_t peak_pending_lookups_;
[email protected]b2b8b832009-02-06 19:03:29221
[email protected]1933eb202009-02-19 18:23:25222 // When true, we don't make new lookup requests.
[email protected]b2b8b832009-02-06 19:03:29223 bool shutdown_;
224
initial.commit09911bf2008-07-26 23:55:29225 // 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]e085c302009-06-01 18:31:36231 // The number of concurrent lookups currently allowed.
232 const size_t max_concurrent_lookups_;
233
[email protected]602faf3c2009-06-27 14:35:44234 // 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]fd2f8afe2009-06-11 21:53:55238 // The host resovler we warm DNS entries for. The resolver (which is not
239 // thread safe) should be accessed only on |host_resolver_loop_|.
[email protected]94a0d3d92009-06-27 01:50:14240 scoped_refptr<net::HostResolver> host_resolver_;
[email protected]fd2f8afe2009-06-11 21:53:55241 MessageLoop* host_resolver_loop_;
242
[email protected]e8013b32008-10-27 18:55:52243 DISALLOW_COPY_AND_ASSIGN(DnsMaster);
initial.commit09911bf2008-07-26 23:55:29244};
245
246} // namespace chrome_browser_net
247
[email protected]e8013b32008-10-27 18:55:52248#endif // CHROME_BROWSER_NET_DNS_MASTER_H_