blob: 5f2670186c5a6f33bf660fa0751e8441d7d9bc89 [file] [log] [blame]
[email protected]7c46a7082012-01-14 01:24:361// Copyright (c) 2012 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_
initial.commit09911bf2008-07-26 23:55:2921
22#include <map>
23#include <queue>
[email protected]1933eb202009-02-19 18:23:2524#include <set>
initial.commit09911bf2008-07-26 23:55:2925#include <string>
[email protected]579f2a12011-04-06 16:27:3126#include <vector>
initial.commit09911bf2008-07-26 23:55:2927
avi6846aef2015-12-26 01:09:3828#include <stddef.h>
29
30#include <stdint.h>
31
[email protected]a918f872010-06-01 14:30:5132#include "base/gtest_prod_util.h"
avi6846aef2015-12-26 01:09:3833#include "base/macros.h"
[email protected]67372ecf2011-09-10 01:30:4634#include "base/memory/scoped_ptr.h"
[email protected]c6944272012-01-06 22:12:2835#include "base/memory/weak_ptr.h"
[email protected]8a9712c22014-07-21 22:47:2836#include "chrome/browser/net/prediction_options.h"
[email protected]21dae9b2008-11-06 23:32:5337#include "chrome/browser/net/referrer.h"
[email protected]685e2842013-05-27 09:43:5638#include "chrome/browser/net/timed_cache.h"
[email protected]c753f142013-02-10 13:14:0439#include "chrome/browser/net/url_info.h"
pmeenaned4b6f82015-01-30 21:59:5940#include "components/network_hints/common/network_hints_common.h"
[email protected]760d970a2010-05-18 00:39:1841#include "net/base/host_port_pair.h"
initial.commit09911bf2008-07-26 23:55:2942
[email protected]443e9312013-05-06 06:17:3443class IOThread;
44class PrefService;
45class Profile;
[email protected]8a9712c22014-07-21 22:47:2846class ProfileIOData;
[email protected]c02c853d72010-08-07 06:23:2447
[email protected]67372ecf2011-09-10 01:30:4648namespace base {
[email protected]443e9312013-05-06 06:17:3449class ListValue;
[email protected]67372ecf2011-09-10 01:30:4650class WaitableEvent;
51}
52
[email protected]fd2f8afe2009-06-11 21:53:5553namespace net {
54class HostResolver;
[email protected]bb5ec7852014-04-02 00:45:4955class SSLConfigService;
bemasc1515a12c2014-10-20 22:30:1656class ProxyService;
[email protected]bb5ec7852014-04-02 00:45:4957class TransportSecurityState;
[email protected]7c46a7082012-01-14 01:24:3658class URLRequestContextGetter;
[email protected]443e9312013-05-06 06:17:3459}
[email protected]fd2f8afe2009-06-11 21:53:5560
[email protected]443e9312013-05-06 06:17:3461namespace user_prefs {
[email protected]c753f142013-02-10 13:14:0462class PrefRegistrySyncable;
[email protected]443e9312013-05-06 06:17:3463}
[email protected]67372ecf2011-09-10 01:30:4664
initial.commit09911bf2008-07-26 23:55:2965namespace chrome_browser_net {
66
pmeenaned4b6f82015-01-30 21:59:5967typedef network_hints::UrlList UrlList;
68typedef network_hints::NameList NameList;
[email protected]74be069e82010-06-25 00:12:4969typedef std::map<GURL, UrlInfo> Results;
initial.commit09911bf2008-07-26 23:55:2970
[email protected]bb5ec7852014-04-02 00:45:4971// An observer for testing.
72class PredictorObserver {
73 public:
74 virtual ~PredictorObserver() {}
75
76 virtual void OnPreconnectUrl(const GURL& original_url,
77 const GURL& first_party_for_cookies,
78 UrlInfo::ResolutionMotivation motivation,
79 int count) = 0;
80};
81
[email protected]67372ecf2011-09-10 01:30:4682// Predictor is constructed during Profile construction (on the UI thread),
83// but it is destroyed on the IO thread when ProfileIOData goes away. All of
84// its core state and functionality happens on the IO thread. The only UI
85// methods are initialization / shutdown related (including preconnect
86// initialization), or convenience methods that internally forward calls to
87// the IO thread.
88class Predictor {
initial.commit09911bf2008-07-26 23:55:2989 public:
[email protected]760d970a2010-05-18 00:39:1890 // A version number for prefs that are saved. This should be incremented when
91 // we change the format so that we discard old data.
[email protected]d6431722011-09-01 00:46:3392 static const int kPredictorReferrerVersion;
[email protected]760d970a2010-05-18 00:39:1893
[email protected]67372ecf2011-09-10 01:30:4694 // Given that the underlying Chromium resolver defaults to a total maximum of
95 // 8 paralell resolutions, we will avoid any chance of starving navigational
96 // resolutions by limiting the number of paralell speculative resolutions.
97 // This is used in the field trials and testing.
98 // TODO(jar): Move this limitation into the resolver.
99 static const size_t kMaxSpeculativeParallelResolves;
100
101 // To control the congestion avoidance system, we need an estimate of how
102 // many speculative requests may arrive at once. Since we currently only
103 // keep 8 subresource names for each frame, we'll use that as our basis.
104 // Note that when scanning search results lists, we might actually get 10 at
105 // a time, and wikipedia can often supply (during a page scan) upwards of 50.
106 // In those odd cases, we may discard some of the later speculative requests
107 // mistakenly assuming that the resolutions took too long.
108 static const int kTypicalSpeculativeGroupSize;
109
110 // The next constant specifies an amount of queueing delay that is
111 // "too large," and indicative of problems with resolutions (perhaps due to
112 // an overloaded router, or such). When we exceed this delay, congestion
113 // avoidance will kick in and all speculations in the queue will be discarded.
114 static const int kMaxSpeculativeResolveQueueDelayMs;
115
[email protected]685e2842013-05-27 09:43:56116 // We don't bother learning to preconnect via a GET if the original URL
117 // navigation was so long ago, that a preconnection would have been dropped
118 // anyway. We believe most servers will drop the connection in 10 seconds, so
119 // we currently estimate this time-till-drop at 10 seconds.
120 // TODO(jar): We should do a persistent field trial to validate/optimize this.
121 static const int kMaxUnusedSocketLifetimeSecondsWithoutAGet;
122
[email protected]f4ef861ba2010-07-28 22:37:23123 // |max_concurrent| specifies how many concurrent (parallel) prefetches will
[email protected]ec86bea2009-12-08 18:35:14124 // be performed. Host lookups will be issued through |host_resolver|.
[email protected]8a9712c22014-07-21 22:47:28125 explicit Predictor(bool preconnect_enabled, bool predictor_enabled);
[email protected]67372ecf2011-09-10 01:30:46126
127 virtual ~Predictor();
128
129 // This function is used to create a predictor. For testing, we can create
130 // a version which does a simpler shutdown.
131 static Predictor* CreatePredictor(bool preconnect_enabled,
[email protected]8a9712c22014-07-21 22:47:28132 bool predictor_enabled,
[email protected]67372ecf2011-09-10 01:30:46133 bool simple_shutdown);
134
[email protected]37ca3fe02013-07-05 15:32:44135 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
[email protected]67372ecf2011-09-10 01:30:46136
137 // ------------- Start UI thread methods.
138
139 virtual void InitNetworkPredictor(PrefService* user_prefs,
140 PrefService* local_state,
[email protected]7c46a7082012-01-14 01:24:36141 IOThread* io_thread,
[email protected]8a9712c22014-07-21 22:47:28142 net::URLRequestContextGetter* getter,
143 ProfileIOData* profile_io_data);
[email protected]67372ecf2011-09-10 01:30:46144
145 // The Omnibox has proposed a given url to the user, and if it is a search
146 // URL, then it also indicates that this is preconnectable (i.e., we could
147 // preconnect to the search server).
148 void AnticipateOmniboxUrl(const GURL& url, bool preconnectable);
149
150 // Preconnect a URL and all of its subresource domains.
[email protected]e6d017652013-05-17 18:01:40151 void PreconnectUrlAndSubresources(const GURL& url,
152 const GURL& first_party_for_cookies);
[email protected]67372ecf2011-09-10 01:30:46153
154 static UrlList GetPredictedUrlListAtStartup(PrefService* user_prefs,
155 PrefService* local_state);
156
157 static void set_max_queueing_delay(int max_queueing_delay_ms);
158
159 static void set_max_parallel_resolves(size_t max_parallel_resolves);
160
[email protected]1101dbc52013-10-05 00:19:12161 virtual void ShutdownOnUIThread();
[email protected]67372ecf2011-09-10 01:30:46162
163 // ------------- End UI thread methods.
164
165 // ------------- Start IO thread methods.
[email protected]b2b8b832009-02-06 19:03:29166
[email protected]1933eb202009-02-19 18:23:25167 // Cancel pending requests and prevent new ones from being made.
168 void Shutdown();
initial.commit09911bf2008-07-26 23:55:29169
170 // In some circumstances, for privacy reasons, all results should be
171 // discarded. This method gracefully handles that activity.
172 // Destroy all our internal state, which shows what names we've looked up, and
173 // how long each has taken, etc. etc. We also destroy records of suggesses
174 // (cache hits etc.).
175 void DiscardAllResults();
176
[email protected]1933eb202009-02-19 18:23:25177 // Add hostname(s) to the queue for processing.
[email protected]c5629c32010-06-23 01:22:43178 void ResolveList(const UrlList& urls,
[email protected]74be069e82010-06-25 00:12:49179 UrlInfo::ResolutionMotivation motivation);
initial.commit09911bf2008-07-26 23:55:29180
[email protected]67372ecf2011-09-10 01:30:46181 void Resolve(const GURL& url, UrlInfo::ResolutionMotivation motivation);
[email protected]e326922d2010-09-03 09:08:10182
[email protected]21dae9b2008-11-06 23:32:53183 // Record details of a navigation so that we can preresolve the host name
184 // ahead of time the next time the users navigates to the indicated host.
[email protected]d6bb2562010-08-25 23:31:30185 // Should only be called when urls are distinct, and they should already be
186 // canonicalized to not have a path.
[email protected]74be069e82010-06-25 00:12:49187 void LearnFromNavigation(const GURL& referring_url, const GURL& target_url);
[email protected]21dae9b2008-11-06 23:32:53188
[email protected]67372ecf2011-09-10 01:30:46189 // When displaying info in about:dns, the following API is called.
190 static void PredictorGetHtmlInfo(Predictor* predictor, std::string* output);
191
[email protected]21dae9b2008-11-06 23:32:53192 // Dump HTML table containing list of referrers for about:dns.
193 void GetHtmlReferrerLists(std::string* output);
194
[email protected]74be069e82010-06-25 00:12:49195 // Dump the list of currently known referrer domains and related prefetchable
[email protected]67372ecf2011-09-10 01:30:46196 // domains for about:dns.
initial.commit09911bf2008-07-26 23:55:29197 void GetHtmlInfo(std::string* output);
198
[email protected]579f2a12011-04-06 16:27:31199 // Discards any referrer for which all the suggested host names are currently
200 // annotated with negligible expected-use. Scales down (diminishes) the
201 // expected-use of those that remain, so that their use will go down by a
202 // factor each time we trim (moving the referrer closer to being discarded in
203 // a future call).
204 // The task is performed synchronously and completes before returing.
205 void TrimReferrersNow();
[email protected]03c5e862009-02-17 22:50:14206
207 // Construct a ListValue object that contains all the data in the referrers_
208 // so that it can be persisted in a pref.
[email protected]f3a1c642011-07-12 19:15:03209 void SerializeReferrers(base::ListValue* referral_list);
[email protected]03c5e862009-02-17 22:50:14210
211 // Process a ListValue that contains all the data from a previous reference
212 // list, as constructed by SerializeReferrers(), and add all the identified
213 // values into the current referrer list.
[email protected]f3a1c642011-07-12 19:15:03214 void DeserializeReferrers(const base::ListValue& referral_list);
[email protected]03c5e862009-02-17 22:50:14215
[email protected]f3a1c642011-07-12 19:15:03216 void DeserializeReferrersThenDelete(base::ListValue* referral_list);
[email protected]ec86bea2009-12-08 18:35:14217
[email protected]67372ecf2011-09-10 01:30:46218 void DiscardInitialNavigationHistory();
[email protected]e695fbd62009-06-30 16:31:54219
[email protected]67372ecf2011-09-10 01:30:46220 void FinalizeInitializationOnIOThread(
221 const std::vector<GURL>& urls_to_prefetch,
222 base::ListValue* referral_list,
223 IOThread* io_thread,
[email protected]8a9712c22014-07-21 22:47:28224 ProfileIOData* profile_io_data);
[email protected]67372ecf2011-09-10 01:30:46225
226 // During startup, we learn what the first N urls visited are, and then
227 // resolve the associated hosts ASAP during our next startup.
228 void LearnAboutInitialNavigation(const GURL& url);
229
230 // Renderer bundles up list and sends to this browser API via IPC.
231 // TODO(jar): Use UrlList instead to include port and scheme.
232 void DnsPrefetchList(const NameList& hostnames);
233
234 // May be called from either the IO or UI thread and will PostTask
235 // to the IO thread if necessary.
236 void DnsPrefetchMotivatedList(const UrlList& urls,
237 UrlInfo::ResolutionMotivation motivation);
238
239 // May be called from either the IO or UI thread and will PostTask
240 // to the IO thread if necessary.
[email protected]8a9712c22014-07-21 22:47:28241 void SaveStateForNextStartupAndTrim();
[email protected]67372ecf2011-09-10 01:30:46242
243 void SaveDnsPrefetchStateForNextStartupAndTrim(
244 base::ListValue* startup_list,
245 base::ListValue* referral_list,
246 base::WaitableEvent* completion);
247
248 // May be called from either the IO or UI thread and will PostTask
249 // to the IO thread if necessary.
yoav91316442015-07-29 06:43:34250 void PreconnectUrl(const GURL& url,
251 const GURL& first_party_for_cookies,
252 UrlInfo::ResolutionMotivation motivation,
253 bool allow_credentials,
254 int count);
[email protected]685e2842013-05-27 09:43:56255
256 void PreconnectUrlOnIOThread(const GURL& url,
257 const GURL& first_party_for_cookies,
258 UrlInfo::ResolutionMotivation motivation,
yoav91316442015-07-29 06:43:34259 bool allow_credentials,
[email protected]685e2842013-05-27 09:43:56260 int count);
261
[email protected]67372ecf2011-09-10 01:30:46262 // ------------- End IO thread methods.
263
264 // The following methods may be called on either the IO or UI threads.
265
266 // Instigate pre-connection to any URLs, or pre-resolution of related host,
267 // that we predict will be needed after this navigation (typically
268 // more-embedded resources on a page). This method will actually post a task
269 // to do the actual work, so as not to jump ahead of the frame navigation that
270 // instigated this activity.
[email protected]e6d017652013-05-17 18:01:40271 void PredictFrameSubresources(const GURL& url,
272 const GURL& first_party_for_cookies);
[email protected]760d970a2010-05-18 00:39:18273
[email protected]1455ccf12010-08-18 16:32:14274 // Put URL in canonical form, including a scheme, host, and port.
275 // Returns GURL::EmptyGURL() if the scheme is not http/https or if the url
276 // cannot be otherwise canonicalized.
277 static GURL CanonicalizeUrl(const GURL& url);
278
[email protected]67372ecf2011-09-10 01:30:46279 // Used for testing.
280 void SetHostResolver(net::HostResolver* host_resolver) {
281 host_resolver_ = host_resolver;
282 }
283 // Used for testing.
[email protected]bb5ec7852014-04-02 00:45:49284 void SetTransportSecurityState(
285 net::TransportSecurityState* transport_security_state) {
286 transport_security_state_ = transport_security_state;
287 }
288 // Used for testing.
[email protected]67372ecf2011-09-10 01:30:46289 size_t max_concurrent_dns_lookups() const {
290 return max_concurrent_dns_lookups_;
291 }
292 // Used for testing.
293 void SetShutdown(bool shutdown) {
294 shutdown_ = shutdown;
295 }
[email protected]bb5ec7852014-04-02 00:45:49296 // Used for testing.
297 void SetObserver(PredictorObserver* observer) {
298 observer_ = observer;
299 }
[email protected]67372ecf2011-09-10 01:30:46300
[email protected]8a9712c22014-07-21 22:47:28301 ProfileIOData* profile_io_data() const {
302 return profile_io_data_;
303 }
304
[email protected]67372ecf2011-09-10 01:30:46305 bool preconnect_enabled() const {
306 return preconnect_enabled_;
307 }
308
[email protected]67372ecf2011-09-10 01:30:46309 bool predictor_enabled() const {
310 return predictor_enabled_;
311 }
312
313
[email protected]b2b8b832009-02-06 19:03:29314 private:
[email protected]74be069e82010-06-25 00:12:49315 FRIEND_TEST_ALL_PREFIXES(PredictorTest, BenefitLookupTest);
316 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ShutdownWhenResolutionIsPendingTest);
317 FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTest);
318 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ConcurrentLookupTest);
319 FRIEND_TEST_ALL_PREFIXES(PredictorTest, MassiveConcurrentLookupTest);
320 FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueuePushPopTest);
321 FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueueReorderTest);
[email protected]579f2a12011-04-06 16:27:31322 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ReferrerSerializationTrimTest);
[email protected]c9043a882013-11-16 00:06:00323 FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTestWithDisabledAdvisor);
324 FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTestWithEnabledAdvisor);
325 FRIEND_TEST_ALL_PREFIXES(PredictorTest, TestSimplePreconnectAdvisor);
bemasc1515a12c2014-10-20 22:30:16326 FRIEND_TEST_ALL_PREFIXES(PredictorTest, NoProxyService);
327 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ProxyDefinitelyEnabled);
328 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ProxyDefinitelyNotEnabled);
329 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ProxyMaybeEnabled);
[email protected]1933eb202009-02-19 18:23:25330 friend class WaitForResolutionHelper; // For testing.
331
332 class LookupRequest;
333
[email protected]a20bc092009-06-05 01:34:20334 // A simple priority queue for handling host names.
335 // Some names that are queued up have |motivation| that requires very rapid
336 // handling. For example, a sub-resource name lookup MUST be done before the
337 // actual sub-resource is fetched. In contrast, a name that was speculatively
338 // noted in a page has to be resolved before the user "gets around to"
339 // clicking on a link. By tagging (with a motivation) each push we make into
340 // this FIFO queue, the queue can re-order the more important names to service
341 // them sooner (relative to some low priority background resolutions).
342 class HostNameQueue {
343 public:
344 HostNameQueue();
345 ~HostNameQueue();
[email protected]c5629c32010-06-23 01:22:43346 void Push(const GURL& url,
[email protected]74be069e82010-06-25 00:12:49347 UrlInfo::ResolutionMotivation motivation);
[email protected]a20bc092009-06-05 01:34:20348 bool IsEmpty() const;
[email protected]c5629c32010-06-23 01:22:43349 GURL Pop();
[email protected]a20bc092009-06-05 01:34:20350
[email protected]579f2a12011-04-06 16:27:31351 private:
[email protected]a20bc092009-06-05 01:34:20352 // The names in the queue that should be serviced (popped) ASAP.
[email protected]c5629c32010-06-23 01:22:43353 std::queue<GURL> rush_queue_;
[email protected]a20bc092009-06-05 01:34:20354 // The names in the queue that should only be serviced when rush_queue is
355 // empty.
[email protected]c5629c32010-06-23 01:22:43356 std::queue<GURL> background_queue_;
[email protected]a20bc092009-06-05 01:34:20357
358 DISALLOW_COPY_AND_ASSIGN(HostNameQueue);
359 };
360
[email protected]67372ecf2011-09-10 01:30:46361 // The InitialObserver monitors navigations made by the network stack. This
362 // is only used to identify startup time resolutions (for re-resolution
363 // during our next process startup).
364 // TODO(jar): Consider preconnecting at startup, which may be faster than
365 // waiting for render process to start and request a connection.
366 class InitialObserver {
367 public:
368 InitialObserver();
369 ~InitialObserver();
370 // Recording of when we observed each navigation.
371 typedef std::map<GURL, base::TimeTicks> FirstNavigations;
372
373 // Potentially add a new URL to our startup list.
374 void Append(const GURL& url, Predictor* predictor);
375
376 // Get an HTML version of our current planned first_navigations_.
377 void GetFirstResolutionsHtml(std::string* output);
378
379 // Persist the current first_navigations_ for storage in a list.
380 void GetInitialDnsResolutionList(base::ListValue* startup_list);
381
382 // Discards all initial loading history.
383 void DiscardInitialNavigationHistory() { first_navigations_.clear(); }
384
385 private:
386 // List of the first N URL resolutions observed in this run.
387 FirstNavigations first_navigations_;
388
389 // The number of URLs we'll save for pre-resolving at next startup.
390 static const size_t kStartupResolutionCount = 10;
391 };
392
[email protected]760d970a2010-05-18 00:39:18393 // A map that is keyed with the host/port that we've learned were the cause
394 // of loading additional URLs. The list of additional targets is held
395 // in a Referrer instance, which is a value in this map.
[email protected]c5629c32010-06-23 01:22:43396 typedef std::map<GURL, Referrer> Referrers;
[email protected]7c19b87b02009-01-26 16:19:44397
[email protected]579f2a12011-04-06 16:27:31398 // Depending on the expected_subresource_use_, we may either make a TCP/IP
399 // preconnection, or merely pre-resolve the hostname via DNS (or even do
400 // nothing). The following are the threasholds for taking those actions.
401 static const double kPreconnectWorthyExpectedValue;
402 static const double kDNSPreresolutionWorthyExpectedValue;
403 // Referred hosts with a subresource_use_rate_ that are less than the
404 // following threshold will be discarded when we Trim() the list.
405 static const double kDiscardableExpectedValue;
406 // During trimming operation to discard hosts for which we don't have likely
407 // subresources, we multiply the expected_subresource_use_ value by the
408 // following ratio until that value is less than kDiscardableExpectedValue.
409 // This number should always be less than 1, an more than 0.
410 static const double kReferrerTrimRatio;
411
412 // Interval between periodic trimming of our whole referrer list.
413 // We only do a major trimming about once an hour, and then only when the user
414 // is actively browsing.
avi6846aef2015-12-26 01:09:38415 static const int64_t kDurationBetweenTrimmingsHours;
[email protected]579f2a12011-04-06 16:27:31416 // Interval between incremental trimmings (to avoid inducing Jank).
avi6846aef2015-12-26 01:09:38417 static const int64_t kDurationBetweenTrimmingIncrementsSeconds;
[email protected]579f2a12011-04-06 16:27:31418 // Number of referring URLs processed in an incremental trimming.
419 static const size_t kUrlsTrimmedPerIncrement;
420
[email protected]1933eb202009-02-19 18:23:25421 // Only for testing. Returns true if hostname has been successfully resolved
422 // (name found).
[email protected]c5629c32010-06-23 01:22:43423 bool WasFound(const GURL& url) const {
424 Results::const_iterator it(results_.find(url));
[email protected]760d970a2010-05-18 00:39:18425 return (it != results_.end()) &&
426 it->second.was_found();
[email protected]1933eb202009-02-19 18:23:25427 }
428
429 // Only for testing. Return how long was the resolution
[email protected]368797e2012-03-09 14:39:48430 // or UrlInfo::NullDuration() if it hasn't been resolved yet.
[email protected]c5629c32010-06-23 01:22:43431 base::TimeDelta GetResolutionDuration(const GURL& url) {
[email protected]c5629c32010-06-23 01:22:43432 if (results_.find(url) == results_.end())
[email protected]368797e2012-03-09 14:39:48433 return UrlInfo::NullDuration();
[email protected]c5629c32010-06-23 01:22:43434 return results_[url].resolve_duration();
[email protected]1933eb202009-02-19 18:23:25435 }
436
437 // Only for testing;
438 size_t peak_pending_lookups() const { return peak_pending_lookups_; }
439
[email protected]16b061fe2014-08-13 15:54:05440 // These two members call the appropriate global functions in
441 // prediction_options.cc depending on which thread they are called on.
442 virtual bool CanPrefetchAndPrerender() const;
443 virtual bool CanPreresolveAndPreconnect() const;
[email protected]8a9712c22014-07-21 22:47:28444
[email protected]67372ecf2011-09-10 01:30:46445 // ------------- Start IO thread methods.
446
447 // Perform actual resolution or preconnection to subresources now. This is
448 // an internal worker method that is reached via a post task from
449 // PredictFrameSubresources().
[email protected]e6d017652013-05-17 18:01:40450 void PrepareFrameSubresources(const GURL& url,
451 const GURL& first_party_for_cookies);
[email protected]67372ecf2011-09-10 01:30:46452
[email protected]85398532009-06-16 21:32:18453 // Access method for use by async lookup request to pass resolution result.
[email protected]c5629c32010-06-23 01:22:43454 void OnLookupFinished(LookupRequest* request, const GURL& url, bool found);
[email protected]1933eb202009-02-19 18:23:25455
[email protected]85398532009-06-16 21:32:18456 // Underlying method for both async and synchronous lookup to update state.
[email protected]ec86bea2009-12-08 18:35:14457 void LookupFinished(LookupRequest* request,
[email protected]c5629c32010-06-23 01:22:43458 const GURL& url, bool found);
[email protected]85398532009-06-16 21:32:18459
[email protected]21dae9b2008-11-06 23:32:53460 // Queue hostname for resolution. If queueing was done, return the pointer
[email protected]c9043a882013-11-16 00:06:00461 // to the queued instance, otherwise return NULL. If the proxy advisor is
462 // enabled, and |url| is likely to be proxied, the hostname will not be
463 // queued as the browser is not expected to fetch it directly.
[email protected]74be069e82010-06-25 00:12:49464 UrlInfo* AppendToResolutionQueue(const GURL& url,
[email protected]c9043a882013-11-16 00:06:00465 UrlInfo::ResolutionMotivation motivation);
initial.commit09911bf2008-07-26 23:55:29466
[email protected]a20bc092009-06-05 01:34:20467 // Check to see if too much queuing delay has been noted for the given info,
468 // which indicates that there is "congestion" or growing delay in handling the
469 // resolution of names. Rather than letting this congestion potentially grow
470 // without bounds, we abandon our queued efforts at pre-resolutions in such a
471 // case.
472 // To do this, we will recycle |info|, as well as all queued items, back to
473 // the state they had before they were queued up. We can't do anything about
474 // the resolutions we've already sent off for processing on another thread, so
475 // we just let them complete. On a slow system, subject to congestion, this
476 // will greatly reduce the number of resolutions done, but it will assure that
477 // any resolutions that are done, are in a timely and hence potentially
478 // helpful manner.
[email protected]74be069e82010-06-25 00:12:49479 bool CongestionControlPerformed(UrlInfo* info);
[email protected]a20bc092009-06-05 01:34:20480
481 // Take lookup requests from work_queue_ and tell HostResolver to look them up
482 // asynchronously, provided we don't exceed concurrent resolution limit.
[email protected]ec86bea2009-12-08 18:35:14483 void StartSomeQueuedResolutions();
initial.commit09911bf2008-07-26 23:55:29484
[email protected]579f2a12011-04-06 16:27:31485 // Performs trimming similar to TrimReferrersNow(), except it does it as a
486 // series of short tasks by posting continuations again an again until done.
487 void TrimReferrers();
488
489 // Loads urls_being_trimmed_ from keys of current referrers_.
490 void LoadUrlsForTrimming();
491
492 // Posts a task to do additional incremental trimming of referrers_.
493 void PostIncrementalTrimTask();
494
495 // Calls Trim() on some or all of urls_being_trimmed_.
496 // If it does not process all the URLs in that vector, it posts a task to
497 // continue with them shortly (i.e., it yeilds and continues).
498 void IncrementalTrimReferrers(bool trim_all_now);
499
bemasc1515a12c2014-10-20 22:30:16500 // If we can determine immediately (i.e. synchronously) that requests to this
501 // URL would likely go through a proxy, then return true. Otherwise, return
502 // false. This is used to avoid issuing DNS requests when a fixed proxy
503 // configuration is in place, which improves efficiency, and is also important
504 // if the unproxied DNS may contain incorrect entries.
505 bool WouldLikelyProxyURL(const GURL& url);
506
[email protected]bb5ec7852014-04-02 00:45:49507 // Applies the HSTS redirect for |url|, if any.
508 GURL GetHSTSRedirectOnIOThread(const GURL& url);
509
[email protected]67372ecf2011-09-10 01:30:46510 // ------------- End IO thread methods.
511
512 scoped_ptr<InitialObserver> initial_observer_;
513
[email protected]7c46a7082012-01-14 01:24:36514 // Reference to URLRequestContextGetter from the Profile which owns the
515 // predictor. Used by Preconnect.
516 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
517
[email protected]67372ecf2011-09-10 01:30:46518 // Status of speculative DNS resolution and speculative TCP/IP connection
[email protected]8a9712c22014-07-21 22:47:28519 // feature. This is false if and only if disabled by a command line switch.
520 const bool predictor_enabled_;
521
522 // This is set by InitNetworkPredictor and used for calling
bnc85ee7102014-09-02 16:15:57523 // CanPrefetchAndPrerenderUI and CanPreresolveAndPreconnectUI.
[email protected]8a9712c22014-07-21 22:47:28524 PrefService* user_prefs_;
525
526 // This is set by InitNetworkPredictor and used for calling
bnc85ee7102014-09-02 16:15:57527 // CanPrefetchAndPrerenderIO and CanPreresolveAndPreconnectIO.
[email protected]8a9712c22014-07-21 22:47:28528 ProfileIOData* profile_io_data_;
[email protected]67372ecf2011-09-10 01:30:46529
[email protected]a20bc092009-06-05 01:34:20530 // work_queue_ holds a list of names we need to look up.
531 HostNameQueue work_queue_;
initial.commit09911bf2008-07-26 23:55:29532
[email protected]21dae9b2008-11-06 23:32:53533 // results_ contains information for existing/prior prefetches.
initial.commit09911bf2008-07-26 23:55:29534 Results results_;
535
[email protected]1933eb202009-02-19 18:23:25536 std::set<LookupRequest*> pending_lookups_;
initial.commit09911bf2008-07-26 23:55:29537
[email protected]1933eb202009-02-19 18:23:25538 // For testing, to verify that we don't exceed the limit.
539 size_t peak_pending_lookups_;
[email protected]b2b8b832009-02-06 19:03:29540
[email protected]1933eb202009-02-19 18:23:25541 // When true, we don't make new lookup requests.
[email protected]b2b8b832009-02-06 19:03:29542 bool shutdown_;
543
[email protected]755a93352010-10-29 06:33:59544 // The number of concurrent speculative lookups currently allowed to be sent
545 // to the resolver. Any additional lookups will be queued to avoid exceeding
546 // this value. The queue is a priority queue that will accelerate
547 // sub-resource speculation, and retard resolutions suggested by page scans.
[email protected]74be069e82010-06-25 00:12:49548 const size_t max_concurrent_dns_lookups_;
[email protected]e085c302009-06-01 18:31:36549
[email protected]602faf3c2009-06-27 14:35:44550 // The maximum queueing delay that is acceptable before we enter congestion
551 // reduction mode, and discard all queued (but not yet assigned) resolutions.
[email protected]74be069e82010-06-25 00:12:49552 const base::TimeDelta max_dns_queue_delay_;
[email protected]602faf3c2009-06-27 14:35:44553
[email protected]73c45322010-10-01 23:57:54554 // The host resolver we warm DNS entries for.
[email protected]67372ecf2011-09-10 01:30:46555 net::HostResolver* host_resolver_;
[email protected]fd2f8afe2009-06-11 21:53:55556
[email protected]bb5ec7852014-04-02 00:45:49557 // The TransportSecurityState instance we query HSTS redirects from.
558 net::TransportSecurityState* transport_security_state_;
559
560 // The SSLConfigService we query SNI support from (used in querying HSTS
561 // redirects).
562 net::SSLConfigService* ssl_config_service_;
563
bemasc1515a12c2014-10-20 22:30:16564 // The ProxyService, used to determine whether preresolve is useful.
565 net::ProxyService* proxy_service_;
566
[email protected]760d970a2010-05-18 00:39:18567 // Are we currently using preconnection, rather than just DNS resolution, for
568 // subresources and omni-box search URLs.
[email protected]8a9712c22014-07-21 22:47:28569 // This is false if and only if disabled by a command line switch.
570 const bool preconnect_enabled_;
[email protected]760d970a2010-05-18 00:39:18571
[email protected]1455ccf12010-08-18 16:32:14572 // Most recent suggestion from Omnibox provided via AnticipateOmniboxUrl().
573 std::string last_omnibox_host_;
574
575 // The time when the last preresolve was done for last_omnibox_host_.
576 base::TimeTicks last_omnibox_preresolve_;
577
578 // The number of consecutive requests to AnticipateOmniboxUrl() that suggested
579 // preconnecting (because it was to a search service).
580 int consecutive_omnibox_preconnect_count_;
581
582 // The time when the last preconnection was requested to a search service.
583 base::TimeTicks last_omnibox_preconnect_;
584
[email protected]579f2a12011-04-06 16:27:31585 // For each URL that we might navigate to (that we've "learned about")
586 // we have a Referrer list. Each Referrer list has all hostnames we might
587 // need to pre-resolve or pre-connect to when there is a navigation to the
588 // orginial hostname.
589 Referrers referrers_;
590
591 // List of URLs in referrers_ currently being trimmed (scaled down to
592 // eventually be aged out of use).
593 std::vector<GURL> urls_being_trimmed_;
594
595 // A time after which we need to do more trimming of referrers.
596 base::TimeTicks next_trim_time_;
597
[email protected]bb5ec7852014-04-02 00:45:49598 // An observer for testing.
599 PredictorObserver* observer_;
600
mohan.reddy14cb4ad42014-09-17 18:15:14601 scoped_ptr<base::WeakPtrFactory<Predictor> > weak_factory_;
602
[email protected]74be069e82010-06-25 00:12:49603 DISALLOW_COPY_AND_ASSIGN(Predictor);
initial.commit09911bf2008-07-26 23:55:29604};
605
[email protected]67372ecf2011-09-10 01:30:46606// This version of the predictor is used for testing.
607class SimplePredictor : public Predictor {
608 public:
[email protected]8a9712c22014-07-21 22:47:28609 explicit SimplePredictor(bool preconnect_enabled, bool predictor_enabled)
610 : Predictor(preconnect_enabled, predictor_enabled) {}
dchengfce29ad2014-10-23 03:47:47611 ~SimplePredictor() override {}
612 void InitNetworkPredictor(PrefService* user_prefs,
613 PrefService* local_state,
614 IOThread* io_thread,
615 net::URLRequestContextGetter* getter,
616 ProfileIOData* profile_io_data) override;
617 void ShutdownOnUIThread() override;
618
[email protected]8a9712c22014-07-21 22:47:28619 private:
[email protected]16b061fe2014-08-13 15:54:05620 // These member functions return True for unittests.
dchengfce29ad2014-10-23 03:47:47621 bool CanPrefetchAndPrerender() const override;
622 bool CanPreresolveAndPreconnect() const override;
[email protected]67372ecf2011-09-10 01:30:46623};
624
initial.commit09911bf2008-07-26 23:55:29625} // namespace chrome_browser_net
626
[email protected]3530cd92010-06-27 06:22:01627#endif // CHROME_BROWSER_NET_PREDICTOR_H_