blob: f2bb2bda45abd44f4c010261f12f15d4f3d88ffd [file] [log] [blame]
[email protected]74be069e82010-06-25 00:12:491// Copyright (c) 2006-2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// 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
[email protected]74be069e82010-06-25 00:12:495// A Predictor object is instantiated once in the browser process, and manages
6// both preresolution of hostnames, as well as TCP/IP preconnection to expected
7// subresources.
8// Most hostname lists are provided by the renderer processes, and include URLs
9// that *might* be used in the near future by the browsing user. One goal of
10// this class is to cause the underlying DNS structure to lookup a hostname
11// before it is really needed, and hence reduce latency in the standard lookup
12// paths.
13// Subresource relationships are usually acquired from the referrer field in a
14// navigation. A subresource URL may be associated with a referrer URL. Later
15// navigations may, if the likelihood of needing the subresource is high enough,
[email protected]f4ef861ba2010-07-28 22:37:2316// cause this module to speculatively create a TCP/IP connection. If there is
17// only a low likelihood, then a DNS pre-resolution operation may be performed.
initial.commit09911bf2008-07-26 23:55:2918
[email protected]3530cd92010-06-27 06:22:0119#ifndef CHROME_BROWSER_NET_PREDICTOR_H_
20#define CHROME_BROWSER_NET_PREDICTOR_H_
[email protected]32b76ef2010-07-26 23:08:2421#pragma once
initial.commit09911bf2008-07-26 23:55:2922
23#include <map>
24#include <queue>
[email protected]1933eb202009-02-19 18:23:2525#include <set>
initial.commit09911bf2008-07-26 23:55:2926#include <string>
27
[email protected]a918f872010-06-01 14:30:5128#include "base/gtest_prod_util.h"
[email protected]fd2f8afe2009-06-11 21:53:5529#include "base/ref_counted.h"
[email protected]3530cd92010-06-27 06:22:0130#include "chrome/browser/net/url_info.h"
[email protected]21dae9b2008-11-06 23:32:5331#include "chrome/browser/net/referrer.h"
[email protected]3530cd92010-06-27 06:22:0132#include "chrome/common/net/predictor_common.h"
[email protected]760d970a2010-05-18 00:39:1833#include "net/base/host_port_pair.h"
initial.commit09911bf2008-07-26 23:55:2934
[email protected]c02c853d72010-08-07 06:23:2435class ListValue;
36
[email protected]fd2f8afe2009-06-11 21:53:5537namespace net {
38class HostResolver;
[email protected]0ac83682010-01-22 17:46:2739} // namespace net
[email protected]fd2f8afe2009-06-11 21:53:5540
initial.commit09911bf2008-07-26 23:55:2941namespace chrome_browser_net {
42
[email protected]c5629c32010-06-23 01:22:4343typedef chrome_common_net::UrlList UrlList;
initial.commit09911bf2008-07-26 23:55:2944typedef chrome_common_net::NameList NameList;
[email protected]74be069e82010-06-25 00:12:4945typedef std::map<GURL, UrlInfo> Results;
initial.commit09911bf2008-07-26 23:55:2946
[email protected]74be069e82010-06-25 00:12:4947// Note that Predictor is not thread safe, and must only be called from
[email protected]ec86bea2009-12-08 18:35:1448// the IO thread. Failure to do so will result in a DCHECK at runtime.
[email protected]74be069e82010-06-25 00:12:4949class Predictor : public base::RefCountedThreadSafe<Predictor> {
initial.commit09911bf2008-07-26 23:55:2950 public:
[email protected]760d970a2010-05-18 00:39:1851 // A version number for prefs that are saved. This should be incremented when
52 // we change the format so that we discard old data.
[email protected]f4ef861ba2010-07-28 22:37:2353 enum { PREDICTOR_REFERRER_VERSION = 2 };
[email protected]760d970a2010-05-18 00:39:1854
[email protected]f4ef861ba2010-07-28 22:37:2355 // Depending on the expected_subresource_use_, we may either make a TCP/IP
56 // preconnection, or merely pre-resolve the hostname via DNS (or even do
57 // nothing). The following are the threasholds for taking those actions.
58 static const double kPreconnectWorthyExpectedValue;
59 static const double kDNSPreresolutionWorthyExpectedValue;
60 // Values of expected_subresource_use_ that are less than the following
61 // threshold will be discarded when we Trim() the values, such as is done when
62 // the process ends, and some values are persisted.
63 static const double kPersistWorthyExpectedValue;
64
65 // |max_concurrent| specifies how many concurrent (parallel) prefetches will
[email protected]ec86bea2009-12-08 18:35:1466 // be performed. Host lookups will be issued through |host_resolver|.
[email protected]74be069e82010-06-25 00:12:4967 Predictor(net::HostResolver* host_resolver,
[email protected]760d970a2010-05-18 00:39:1868 base::TimeDelta max_queue_delay_ms, size_t max_concurrent,
69 bool preconnect_enabled);
[email protected]b2b8b832009-02-06 19:03:2970
[email protected]1933eb202009-02-19 18:23:2571 // Cancel pending requests and prevent new ones from being made.
72 void Shutdown();
initial.commit09911bf2008-07-26 23:55:2973
74 // In some circumstances, for privacy reasons, all results should be
75 // discarded. This method gracefully handles that activity.
76 // Destroy all our internal state, which shows what names we've looked up, and
77 // how long each has taken, etc. etc. We also destroy records of suggesses
78 // (cache hits etc.).
79 void DiscardAllResults();
80
[email protected]1933eb202009-02-19 18:23:2581 // Add hostname(s) to the queue for processing.
[email protected]c5629c32010-06-23 01:22:4382 void ResolveList(const UrlList& urls,
[email protected]74be069e82010-06-25 00:12:4983 UrlInfo::ResolutionMotivation motivation);
[email protected]c5629c32010-06-23 01:22:4384 void Resolve(const GURL& url,
[email protected]74be069e82010-06-25 00:12:4985 UrlInfo::ResolutionMotivation motivation);
initial.commit09911bf2008-07-26 23:55:2986
[email protected]9008c86f2010-08-06 07:10:2487 // Instigate pre-connection to any URLs, or pre-resolution of related host,
88 // that we predict will be needed after this navigation (typically
89 // more-embedded resources on a page). This method will actually post a task
90 // to do the actual work, so as not to jump ahead of the frame navigation that
91 // instigated this activity.
[email protected]74be069e82010-06-25 00:12:4992 void PredictFrameSubresources(const GURL& url);
[email protected]21dae9b2008-11-06 23:32:5393
[email protected]1455ccf12010-08-18 16:32:1494 // The Omnibox has proposed a given url to the user, and if it is a search
95 // URL, then it also indicates that this is preconnectable (i.e., we could
96 // preconnect to the search server).
97 void AnticipateOmniboxUrl(const GURL& url, bool preconnectable);
98
[email protected]21dae9b2008-11-06 23:32:5399 // Record details of a navigation so that we can preresolve the host name
100 // ahead of time the next time the users navigates to the indicated host.
[email protected]d6bb2562010-08-25 23:31:30101 // Should only be called when urls are distinct, and they should already be
102 // canonicalized to not have a path.
[email protected]74be069e82010-06-25 00:12:49103 void LearnFromNavigation(const GURL& referring_url, const GURL& target_url);
[email protected]21dae9b2008-11-06 23:32:53104
105 // Dump HTML table containing list of referrers for about:dns.
106 void GetHtmlReferrerLists(std::string* output);
107
[email protected]74be069e82010-06-25 00:12:49108 // Dump the list of currently known referrer domains and related prefetchable
[email protected]21dae9b2008-11-06 23:32:53109 // domains.
initial.commit09911bf2008-07-26 23:55:29110 void GetHtmlInfo(std::string* output);
111
[email protected]03c5e862009-02-17 22:50:14112 // Discard any referrer for which all the suggested host names are currently
113 // annotated with no user latency reduction. Also scale down (diminish) the
114 // total benefit of those that did help, so that their reported contribution
115 // wll go done by a factor of 2 each time we trim (moving the referrer closer
116 // to being discarded at a future Trim).
117 void TrimReferrers();
118
119 // Construct a ListValue object that contains all the data in the referrers_
120 // so that it can be persisted in a pref.
121 void SerializeReferrers(ListValue* referral_list);
122
123 // Process a ListValue that contains all the data from a previous reference
124 // list, as constructed by SerializeReferrers(), and add all the identified
125 // values into the current referrer list.
126 void DeserializeReferrers(const ListValue& referral_list);
127
[email protected]c02c853d72010-08-07 06:23:24128 void DeserializeReferrersThenDelete(ListValue* referral_list);
[email protected]ec86bea2009-12-08 18:35:14129
[email protected]e695fbd62009-06-30 16:31:54130 // For unit test code only.
[email protected]74be069e82010-06-25 00:12:49131 size_t max_concurrent_dns_lookups() const {
132 return max_concurrent_dns_lookups_;
133 }
[email protected]e695fbd62009-06-30 16:31:54134
[email protected]760d970a2010-05-18 00:39:18135 // Flag setting to use preconnection instead of just DNS pre-fetching.
136 bool preconnect_enabled() const { return preconnect_enabled_; }
137
[email protected]1455ccf12010-08-18 16:32:14138 // Put URL in canonical form, including a scheme, host, and port.
139 // Returns GURL::EmptyGURL() if the scheme is not http/https or if the url
140 // cannot be otherwise canonicalized.
141 static GURL CanonicalizeUrl(const GURL& url);
142
[email protected]b2b8b832009-02-06 19:03:29143 private:
[email protected]74be069e82010-06-25 00:12:49144 friend class base::RefCountedThreadSafe<Predictor>;
145 FRIEND_TEST_ALL_PREFIXES(PredictorTest, BenefitLookupTest);
146 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ShutdownWhenResolutionIsPendingTest);
147 FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTest);
148 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ConcurrentLookupTest);
149 FRIEND_TEST_ALL_PREFIXES(PredictorTest, MassiveConcurrentLookupTest);
150 FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueuePushPopTest);
151 FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueueReorderTest);
[email protected]1933eb202009-02-19 18:23:25152 friend class WaitForResolutionHelper; // For testing.
153
154 class LookupRequest;
155
[email protected]a20bc092009-06-05 01:34:20156 // A simple priority queue for handling host names.
157 // Some names that are queued up have |motivation| that requires very rapid
158 // handling. For example, a sub-resource name lookup MUST be done before the
159 // actual sub-resource is fetched. In contrast, a name that was speculatively
160 // noted in a page has to be resolved before the user "gets around to"
161 // clicking on a link. By tagging (with a motivation) each push we make into
162 // this FIFO queue, the queue can re-order the more important names to service
163 // them sooner (relative to some low priority background resolutions).
164 class HostNameQueue {
165 public:
166 HostNameQueue();
167 ~HostNameQueue();
[email protected]c5629c32010-06-23 01:22:43168 void Push(const GURL& url,
[email protected]74be069e82010-06-25 00:12:49169 UrlInfo::ResolutionMotivation motivation);
[email protected]a20bc092009-06-05 01:34:20170 bool IsEmpty() const;
[email protected]c5629c32010-06-23 01:22:43171 GURL Pop();
[email protected]a20bc092009-06-05 01:34:20172
173 private:
174 // The names in the queue that should be serviced (popped) ASAP.
[email protected]c5629c32010-06-23 01:22:43175 std::queue<GURL> rush_queue_;
[email protected]a20bc092009-06-05 01:34:20176 // The names in the queue that should only be serviced when rush_queue is
177 // empty.
[email protected]c5629c32010-06-23 01:22:43178 std::queue<GURL> background_queue_;
[email protected]a20bc092009-06-05 01:34:20179
180 DISALLOW_COPY_AND_ASSIGN(HostNameQueue);
181 };
182
[email protected]760d970a2010-05-18 00:39:18183 // A map that is keyed with the host/port that we've learned were the cause
184 // of loading additional URLs. The list of additional targets is held
185 // in a Referrer instance, which is a value in this map.
[email protected]c5629c32010-06-23 01:22:43186 typedef std::map<GURL, Referrer> Referrers;
[email protected]7c19b87b02009-01-26 16:19:44187
[email protected]9008c86f2010-08-06 07:10:24188 ~Predictor();
189
190 // Perform actual resolution or preconnection to subresources now. This is
191 // an internal worker method that is reached via a post task from
192 // PredictFrameSubresources().
193 void PrepareFrameSubresources(const GURL& url);
194
[email protected]1933eb202009-02-19 18:23:25195 // Only for testing. Returns true if hostname has been successfully resolved
196 // (name found).
[email protected]c5629c32010-06-23 01:22:43197 bool WasFound(const GURL& url) const {
198 Results::const_iterator it(results_.find(url));
[email protected]760d970a2010-05-18 00:39:18199 return (it != results_.end()) &&
200 it->second.was_found();
[email protected]1933eb202009-02-19 18:23:25201 }
202
203 // Only for testing. Return how long was the resolution
[email protected]74be069e82010-06-25 00:12:49204 // or UrlInfo::kNullDuration if it hasn't been resolved yet.
[email protected]c5629c32010-06-23 01:22:43205 base::TimeDelta GetResolutionDuration(const GURL& url) {
[email protected]c5629c32010-06-23 01:22:43206 if (results_.find(url) == results_.end())
[email protected]74be069e82010-06-25 00:12:49207 return UrlInfo::kNullDuration;
[email protected]c5629c32010-06-23 01:22:43208 return results_[url].resolve_duration();
[email protected]1933eb202009-02-19 18:23:25209 }
210
211 // Only for testing;
212 size_t peak_pending_lookups() const { return peak_pending_lookups_; }
213
[email protected]85398532009-06-16 21:32:18214 // Access method for use by async lookup request to pass resolution result.
[email protected]c5629c32010-06-23 01:22:43215 void OnLookupFinished(LookupRequest* request, const GURL& url, bool found);
[email protected]1933eb202009-02-19 18:23:25216
[email protected]85398532009-06-16 21:32:18217 // Underlying method for both async and synchronous lookup to update state.
[email protected]ec86bea2009-12-08 18:35:14218 void LookupFinished(LookupRequest* request,
[email protected]c5629c32010-06-23 01:22:43219 const GURL& url, bool found);
[email protected]85398532009-06-16 21:32:18220
[email protected]21dae9b2008-11-06 23:32:53221 // Queue hostname for resolution. If queueing was done, return the pointer
222 // to the queued instance, otherwise return NULL.
[email protected]74be069e82010-06-25 00:12:49223 UrlInfo* AppendToResolutionQueue(const GURL& url,
224 UrlInfo::ResolutionMotivation motivation);
initial.commit09911bf2008-07-26 23:55:29225
[email protected]a20bc092009-06-05 01:34:20226 // Check to see if too much queuing delay has been noted for the given info,
227 // which indicates that there is "congestion" or growing delay in handling the
228 // resolution of names. Rather than letting this congestion potentially grow
229 // without bounds, we abandon our queued efforts at pre-resolutions in such a
230 // case.
231 // To do this, we will recycle |info|, as well as all queued items, back to
232 // the state they had before they were queued up. We can't do anything about
233 // the resolutions we've already sent off for processing on another thread, so
234 // we just let them complete. On a slow system, subject to congestion, this
235 // will greatly reduce the number of resolutions done, but it will assure that
236 // any resolutions that are done, are in a timely and hence potentially
237 // helpful manner.
[email protected]74be069e82010-06-25 00:12:49238 bool CongestionControlPerformed(UrlInfo* info);
[email protected]a20bc092009-06-05 01:34:20239
240 // Take lookup requests from work_queue_ and tell HostResolver to look them up
241 // asynchronously, provided we don't exceed concurrent resolution limit.
[email protected]ec86bea2009-12-08 18:35:14242 void StartSomeQueuedResolutions();
initial.commit09911bf2008-07-26 23:55:29243
[email protected]a20bc092009-06-05 01:34:20244 // work_queue_ holds a list of names we need to look up.
245 HostNameQueue work_queue_;
initial.commit09911bf2008-07-26 23:55:29246
[email protected]21dae9b2008-11-06 23:32:53247 // results_ contains information for existing/prior prefetches.
initial.commit09911bf2008-07-26 23:55:29248 Results results_;
249
[email protected]760d970a2010-05-18 00:39:18250 // For each URL that we might navigate to (that we've "learned about")
[email protected]21dae9b2008-11-06 23:32:53251 // we have a Referrer list. Each Referrer list has all hostnames we need to
252 // pre-resolve when there is a navigation to the orginial hostname.
253 Referrers referrers_;
254
[email protected]1933eb202009-02-19 18:23:25255 std::set<LookupRequest*> pending_lookups_;
initial.commit09911bf2008-07-26 23:55:29256
[email protected]1933eb202009-02-19 18:23:25257 // For testing, to verify that we don't exceed the limit.
258 size_t peak_pending_lookups_;
[email protected]b2b8b832009-02-06 19:03:29259
[email protected]1933eb202009-02-19 18:23:25260 // When true, we don't make new lookup requests.
[email protected]b2b8b832009-02-06 19:03:29261 bool shutdown_;
262
[email protected]e085c302009-06-01 18:31:36263 // The number of concurrent lookups currently allowed.
[email protected]74be069e82010-06-25 00:12:49264 const size_t max_concurrent_dns_lookups_;
[email protected]e085c302009-06-01 18:31:36265
[email protected]602faf3c2009-06-27 14:35:44266 // The maximum queueing delay that is acceptable before we enter congestion
267 // reduction mode, and discard all queued (but not yet assigned) resolutions.
[email protected]74be069e82010-06-25 00:12:49268 const base::TimeDelta max_dns_queue_delay_;
[email protected]602faf3c2009-06-27 14:35:44269
[email protected]ec86bea2009-12-08 18:35:14270 // The host resovler we warm DNS entries for.
[email protected]94a0d3d92009-06-27 01:50:14271 scoped_refptr<net::HostResolver> host_resolver_;
[email protected]fd2f8afe2009-06-11 21:53:55272
[email protected]760d970a2010-05-18 00:39:18273 // Are we currently using preconnection, rather than just DNS resolution, for
274 // subresources and omni-box search URLs.
275 bool preconnect_enabled_;
276
[email protected]1455ccf12010-08-18 16:32:14277 // Most recent suggestion from Omnibox provided via AnticipateOmniboxUrl().
278 std::string last_omnibox_host_;
279
280 // The time when the last preresolve was done for last_omnibox_host_.
281 base::TimeTicks last_omnibox_preresolve_;
282
283 // The number of consecutive requests to AnticipateOmniboxUrl() that suggested
284 // preconnecting (because it was to a search service).
285 int consecutive_omnibox_preconnect_count_;
286
287 // The time when the last preconnection was requested to a search service.
288 base::TimeTicks last_omnibox_preconnect_;
289
[email protected]74be069e82010-06-25 00:12:49290 DISALLOW_COPY_AND_ASSIGN(Predictor);
initial.commit09911bf2008-07-26 23:55:29291};
292
293} // namespace chrome_browser_net
294
[email protected]3530cd92010-06-27 06:22:01295#endif // CHROME_BROWSER_NET_PREDICTOR_H_