blob: 7023275b58856ac3a668e99a44dc0fc7d884b084 [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
r.karu97260302016-01-06 14:08:3022#include <stddef.h>
23#include <stdint.h>
24
initial.commit09911bf2008-07-26 23:55:2925#include <map>
dcheng4e7c0422016-04-14 00:59:0526#include <memory>
initial.commit09911bf2008-07-26 23:55:2927#include <queue>
28#include <string>
[email protected]579f2a12011-04-06 16:27:3129#include <vector>
initial.commit09911bf2008-07-26 23:55:2930
csharrison43bf3c32016-06-23 21:49:4731#include "base/containers/mru_cache.h"
[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]c6944272012-01-06 22:12:2834#include "base/memory/weak_ptr.h"
[email protected]8a9712c22014-07-21 22:47:2835#include "chrome/browser/net/prediction_options.h"
[email protected]21dae9b2008-11-06 23:32:5336#include "chrome/browser/net/referrer.h"
[email protected]685e2842013-05-27 09:43:5637#include "chrome/browser/net/timed_cache.h"
[email protected]c753f142013-02-10 13:14:0438#include "chrome/browser/net/url_info.h"
pmeenaned4b6f82015-01-30 21:59:5939#include "components/network_hints/common/network_hints_common.h"
[email protected]760d970a2010-05-18 00:39:1840#include "net/base/host_port_pair.h"
csharrisona7b16112016-05-13 23:25:3941#include "url/gurl.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:4650}
51
[email protected]fd2f8afe2009-06-11 21:53:5552namespace net {
53class HostResolver;
[email protected]bb5ec7852014-04-02 00:45:4954class SSLConfigService;
bemasc1515a12c2014-10-20 22:30:1655class ProxyService;
[email protected]bb5ec7852014-04-02 00:45:4956class TransportSecurityState;
[email protected]7c46a7082012-01-14 01:24:3657class URLRequestContextGetter;
[email protected]443e9312013-05-06 06:17:3458}
[email protected]fd2f8afe2009-06-11 21:53:5559
[email protected]443e9312013-05-06 06:17:3460namespace user_prefs {
[email protected]c753f142013-02-10 13:14:0461class PrefRegistrySyncable;
[email protected]443e9312013-05-06 06:17:3462}
[email protected]67372ecf2011-09-10 01:30:4663
initial.commit09911bf2008-07-26 23:55:2964namespace chrome_browser_net {
65
[email protected]74be069e82010-06-25 00:12:4966typedef std::map<GURL, UrlInfo> Results;
initial.commit09911bf2008-07-26 23:55:2967
[email protected]bb5ec7852014-04-02 00:45:4968// An observer for testing.
69class PredictorObserver {
70 public:
71 virtual ~PredictorObserver() {}
72
73 virtual void OnPreconnectUrl(const GURL& original_url,
74 const GURL& first_party_for_cookies,
75 UrlInfo::ResolutionMotivation motivation,
csharrisona7b16112016-05-13 23:25:3976 int count) {}
77 virtual void OnLearnFromNavigation(const GURL& referring_url,
78 const GURL& target_url) {}
csharrison0597b2172016-05-27 12:24:0479
80 virtual void OnDnsLookupFinished(const GURL& url, bool found) {}
[email protected]bb5ec7852014-04-02 00:45:4981};
82
[email protected]67372ecf2011-09-10 01:30:4683// Predictor is constructed during Profile construction (on the UI thread),
84// but it is destroyed on the IO thread when ProfileIOData goes away. All of
85// its core state and functionality happens on the IO thread. The only UI
86// methods are initialization / shutdown related (including preconnect
87// initialization), or convenience methods that internally forward calls to
88// the IO thread.
89class Predictor {
initial.commit09911bf2008-07-26 23:55:2990 public:
[email protected]760d970a2010-05-18 00:39:1891 // A version number for prefs that are saved. This should be incremented when
92 // we change the format so that we discard old data.
[email protected]d6431722011-09-01 00:46:3393 static const int kPredictorReferrerVersion;
[email protected]760d970a2010-05-18 00:39:1894
[email protected]67372ecf2011-09-10 01:30:4695 // Given that the underlying Chromium resolver defaults to a total maximum of
96 // 8 paralell resolutions, we will avoid any chance of starving navigational
97 // resolutions by limiting the number of paralell speculative resolutions.
98 // This is used in the field trials and testing.
99 // TODO(jar): Move this limitation into the resolver.
100 static const size_t kMaxSpeculativeParallelResolves;
101
102 // To control the congestion avoidance system, we need an estimate of how
103 // many speculative requests may arrive at once. Since we currently only
104 // keep 8 subresource names for each frame, we'll use that as our basis.
105 // Note that when scanning search results lists, we might actually get 10 at
106 // a time, and wikipedia can often supply (during a page scan) upwards of 50.
107 // In those odd cases, we may discard some of the later speculative requests
108 // mistakenly assuming that the resolutions took too long.
109 static const int kTypicalSpeculativeGroupSize;
110
111 // The next constant specifies an amount of queueing delay that is
112 // "too large," and indicative of problems with resolutions (perhaps due to
113 // an overloaded router, or such). When we exceed this delay, congestion
114 // avoidance will kick in and all speculations in the queue will be discarded.
115 static const int kMaxSpeculativeResolveQueueDelayMs;
116
[email protected]685e2842013-05-27 09:43:56117 // We don't bother learning to preconnect via a GET if the original URL
118 // navigation was so long ago, that a preconnection would have been dropped
119 // anyway. We believe most servers will drop the connection in 10 seconds, so
120 // we currently estimate this time-till-drop at 10 seconds.
121 // TODO(jar): We should do a persistent field trial to validate/optimize this.
122 static const int kMaxUnusedSocketLifetimeSecondsWithoutAGet;
123
csharrison43bf3c32016-06-23 21:49:47124 // The maximum size of the MRU cache of referrers.
125 static const int kMaxReferrers;
126
csharrisonefcb48d2016-08-12 03:05:32127 explicit Predictor(bool predictor_enabled);
[email protected]67372ecf2011-09-10 01:30:46128
129 virtual ~Predictor();
130
131 // This function is used to create a predictor. For testing, we can create
132 // a version which does a simpler shutdown.
csharrisonefcb48d2016-08-12 03:05:32133 // TODO(636128): This method should return a unique_ptr.
134 static Predictor* CreatePredictor(bool simple_shutdown);
[email protected]67372ecf2011-09-10 01:30:46135
[email protected]37ca3fe02013-07-05 15:32:44136 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
[email protected]67372ecf2011-09-10 01:30:46137
138 // ------------- Start UI thread methods.
139
140 virtual void InitNetworkPredictor(PrefService* user_prefs,
[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
csharrisonb4c497062016-06-02 18:00:44154 static std::vector<GURL> GetPredictedUrlListAtStartup(
155 PrefService* user_prefs);
[email protected]67372ecf2011-09-10 01:30:46156
csharrisonf2113bc2016-06-29 15:07:55157 // Calls ClearPrefsOnUIThread and posts a task to the IO thread to
158 // DiscardAllResults.
159 void DiscardAllResultsAndClearPrefsOnUIThread();
160
161 // Clears the preferences used by the predictor. Must be called on the UI
162 // thread.
163 void ClearPrefsOnUIThread();
164
[email protected]67372ecf2011-09-10 01:30:46165 static void set_max_queueing_delay(int max_queueing_delay_ms);
166
167 static void set_max_parallel_resolves(size_t max_parallel_resolves);
168
[email protected]1101dbc52013-10-05 00:19:12169 virtual void ShutdownOnUIThread();
[email protected]67372ecf2011-09-10 01:30:46170
csharrisonee456332016-08-01 19:20:03171 void UpdatePrefsOnUIThread(std::unique_ptr<base::ListValue> startup_list,
172 std::unique_ptr<base::ListValue> referral_list);
173
[email protected]67372ecf2011-09-10 01:30:46174 // ------------- End UI thread methods.
175
176 // ------------- Start IO thread methods.
[email protected]b2b8b832009-02-06 19:03:29177
csharrisonee456332016-08-01 19:20:03178 void WriteDnsPrefetchState(base::ListValue* startup_list,
179 base::ListValue* referral_list);
180
[email protected]1933eb202009-02-19 18:23:25181 // Cancel pending requests and prevent new ones from being made.
182 void Shutdown();
initial.commit09911bf2008-07-26 23:55:29183
184 // In some circumstances, for privacy reasons, all results should be
185 // discarded. This method gracefully handles that activity.
186 // Destroy all our internal state, which shows what names we've looked up, and
187 // how long each has taken, etc. etc. We also destroy records of suggesses
188 // (cache hits etc.).
189 void DiscardAllResults();
190
[email protected]1933eb202009-02-19 18:23:25191 // Add hostname(s) to the queue for processing.
csharrisonb4c497062016-06-02 18:00:44192 void ResolveList(const std::vector<GURL>& urls,
[email protected]74be069e82010-06-25 00:12:49193 UrlInfo::ResolutionMotivation motivation);
initial.commit09911bf2008-07-26 23:55:29194
[email protected]67372ecf2011-09-10 01:30:46195 void Resolve(const GURL& url, UrlInfo::ResolutionMotivation motivation);
[email protected]e326922d2010-09-03 09:08:10196
[email protected]21dae9b2008-11-06 23:32:53197 // Record details of a navigation so that we can preresolve the host name
198 // ahead of time the next time the users navigates to the indicated host.
[email protected]d6bb2562010-08-25 23:31:30199 // Should only be called when urls are distinct, and they should already be
200 // canonicalized to not have a path.
[email protected]74be069e82010-06-25 00:12:49201 void LearnFromNavigation(const GURL& referring_url, const GURL& target_url);
[email protected]21dae9b2008-11-06 23:32:53202
[email protected]67372ecf2011-09-10 01:30:46203 // When displaying info in about:dns, the following API is called.
204 static void PredictorGetHtmlInfo(Predictor* predictor, std::string* output);
205
[email protected]21dae9b2008-11-06 23:32:53206 // Dump HTML table containing list of referrers for about:dns.
207 void GetHtmlReferrerLists(std::string* output);
208
[email protected]74be069e82010-06-25 00:12:49209 // Dump the list of currently known referrer domains and related prefetchable
[email protected]67372ecf2011-09-10 01:30:46210 // domains for about:dns.
initial.commit09911bf2008-07-26 23:55:29211 void GetHtmlInfo(std::string* output);
212
[email protected]03c5e862009-02-17 22:50:14213 // Construct a ListValue object that contains all the data in the referrers_
214 // so that it can be persisted in a pref.
[email protected]f3a1c642011-07-12 19:15:03215 void SerializeReferrers(base::ListValue* referral_list);
[email protected]03c5e862009-02-17 22:50:14216
217 // Process a ListValue that contains all the data from a previous reference
218 // list, as constructed by SerializeReferrers(), and add all the identified
219 // values into the current referrer list.
[email protected]f3a1c642011-07-12 19:15:03220 void DeserializeReferrers(const base::ListValue& referral_list);
[email protected]03c5e862009-02-17 22:50:14221
[email protected]67372ecf2011-09-10 01:30:46222 void DiscardInitialNavigationHistory();
[email protected]e695fbd62009-06-30 16:31:54223
[email protected]67372ecf2011-09-10 01:30:46224 void FinalizeInitializationOnIOThread(
225 const std::vector<GURL>& urls_to_prefetch,
csharrisonf2113bc2016-06-29 15:07:55226 std::unique_ptr<base::ListValue> referral_list,
[email protected]67372ecf2011-09-10 01:30:46227 IOThread* io_thread,
[email protected]8a9712c22014-07-21 22:47:28228 ProfileIOData* profile_io_data);
[email protected]67372ecf2011-09-10 01:30:46229
230 // During startup, we learn what the first N urls visited are, and then
231 // resolve the associated hosts ASAP during our next startup.
232 void LearnAboutInitialNavigation(const GURL& url);
233
234 // Renderer bundles up list and sends to this browser API via IPC.
csharrisonb4c497062016-06-02 18:00:44235 // TODO(csharrison): Use a GURL vector instead to include port and scheme.
236 void DnsPrefetchList(const std::vector<std::string>& hostnames);
[email protected]67372ecf2011-09-10 01:30:46237
238 // May be called from either the IO or UI thread and will PostTask
239 // to the IO thread if necessary.
csharrisonb4c497062016-06-02 18:00:44240 void DnsPrefetchMotivatedList(const std::vector<GURL>& urls,
[email protected]67372ecf2011-09-10 01:30:46241 UrlInfo::ResolutionMotivation motivation);
242
csharrisonee456332016-08-01 19:20:03243 // Called from the UI thread in response to the load event.
csharrison43bf3c32016-06-23 21:49:47244 void SaveStateForNextStartup();
[email protected]67372ecf2011-09-10 01:30:46245
[email protected]67372ecf2011-09-10 01:30:46246 // May be called from either the IO or UI thread and will PostTask
247 // to the IO thread if necessary.
yoav91316442015-07-29 06:43:34248 void PreconnectUrl(const GURL& url,
249 const GURL& first_party_for_cookies,
250 UrlInfo::ResolutionMotivation motivation,
251 bool allow_credentials,
252 int count);
[email protected]685e2842013-05-27 09:43:56253
254 void PreconnectUrlOnIOThread(const GURL& url,
255 const GURL& first_party_for_cookies,
256 UrlInfo::ResolutionMotivation motivation,
yoav91316442015-07-29 06:43:34257 bool allow_credentials,
[email protected]685e2842013-05-27 09:43:56258 int count);
259
[email protected]67372ecf2011-09-10 01:30:46260 // ------------- End IO thread methods.
261
262 // The following methods may be called on either the IO or UI threads.
263
264 // Instigate pre-connection to any URLs, or pre-resolution of related host,
265 // that we predict will be needed after this navigation (typically
266 // more-embedded resources on a page). This method will actually post a task
267 // to do the actual work, so as not to jump ahead of the frame navigation that
268 // instigated this activity.
[email protected]e6d017652013-05-17 18:01:40269 void PredictFrameSubresources(const GURL& url,
270 const GURL& first_party_for_cookies);
[email protected]760d970a2010-05-18 00:39:18271
[email protected]1455ccf12010-08-18 16:32:14272 // Put URL in canonical form, including a scheme, host, and port.
273 // Returns GURL::EmptyGURL() if the scheme is not http/https or if the url
274 // cannot be otherwise canonicalized.
275 static GURL CanonicalizeUrl(const GURL& url);
276
[email protected]67372ecf2011-09-10 01:30:46277 // Used for testing.
[email protected]bb5ec7852014-04-02 00:45:49278 void SetTransportSecurityState(
279 net::TransportSecurityState* transport_security_state) {
280 transport_security_state_ = transport_security_state;
281 }
282 // Used for testing.
[email protected]67372ecf2011-09-10 01:30:46283 size_t max_concurrent_dns_lookups() const {
284 return max_concurrent_dns_lookups_;
285 }
286 // Used for testing.
287 void SetShutdown(bool shutdown) {
288 shutdown_ = shutdown;
289 }
[email protected]bb5ec7852014-04-02 00:45:49290 // Used for testing.
291 void SetObserver(PredictorObserver* observer) {
292 observer_ = observer;
293 }
[email protected]67372ecf2011-09-10 01:30:46294
[email protected]8a9712c22014-07-21 22:47:28295 ProfileIOData* profile_io_data() const {
296 return profile_io_data_;
297 }
298
csharrisonefcb48d2016-08-12 03:05:32299 bool PredictorEnabled() const;
[email protected]67372ecf2011-09-10 01:30:46300
csharrisonefcb48d2016-08-12 03:05:32301 // Used only for testing. Overrides command line flag to disable the
302 // predictor, which is added in the browser test fixture.
303 void SetPredictorEnabledForTest(bool predictor_enabled);
csharrisona7b16112016-05-13 23:25:39304
305 net::URLRequestContextGetter* url_request_context_getter_for_test() {
306 return url_request_context_getter_.get();
307 }
[email protected]67372ecf2011-09-10 01:30:46308
csharrisoncb0f40152016-06-01 00:13:09309 TimedCache* timed_cache() { return timed_cache_.get(); }
310
[email protected]b2b8b832009-02-06 19:03:29311 private:
[email protected]74be069e82010-06-25 00:12:49312 FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueuePushPopTest);
313 FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueueReorderTest);
[email protected]579f2a12011-04-06 16:27:31314 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ReferrerSerializationTrimTest);
[email protected]c9043a882013-11-16 00:06:00315 FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTestWithDisabledAdvisor);
316 FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTestWithEnabledAdvisor);
317 FRIEND_TEST_ALL_PREFIXES(PredictorTest, TestSimplePreconnectAdvisor);
bemasc1515a12c2014-10-20 22:30:16318 FRIEND_TEST_ALL_PREFIXES(PredictorTest, NoProxyService);
319 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ProxyDefinitelyEnabled);
320 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ProxyDefinitelyNotEnabled);
321 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ProxyMaybeEnabled);
[email protected]1933eb202009-02-19 18:23:25322 friend class WaitForResolutionHelper; // For testing.
csharrison0597b2172016-05-27 12:24:04323 friend class PredictorBrowserTest;
[email protected]1933eb202009-02-19 18:23:25324
[email protected]a20bc092009-06-05 01:34:20325 // A simple priority queue for handling host names.
326 // Some names that are queued up have |motivation| that requires very rapid
327 // handling. For example, a sub-resource name lookup MUST be done before the
328 // actual sub-resource is fetched. In contrast, a name that was speculatively
329 // noted in a page has to be resolved before the user "gets around to"
330 // clicking on a link. By tagging (with a motivation) each push we make into
331 // this FIFO queue, the queue can re-order the more important names to service
332 // them sooner (relative to some low priority background resolutions).
333 class HostNameQueue {
334 public:
335 HostNameQueue();
336 ~HostNameQueue();
[email protected]c5629c32010-06-23 01:22:43337 void Push(const GURL& url,
[email protected]74be069e82010-06-25 00:12:49338 UrlInfo::ResolutionMotivation motivation);
[email protected]a20bc092009-06-05 01:34:20339 bool IsEmpty() const;
[email protected]c5629c32010-06-23 01:22:43340 GURL Pop();
[email protected]a20bc092009-06-05 01:34:20341
[email protected]579f2a12011-04-06 16:27:31342 private:
[email protected]a20bc092009-06-05 01:34:20343 // The names in the queue that should be serviced (popped) ASAP.
[email protected]c5629c32010-06-23 01:22:43344 std::queue<GURL> rush_queue_;
[email protected]a20bc092009-06-05 01:34:20345 // The names in the queue that should only be serviced when rush_queue is
346 // empty.
[email protected]c5629c32010-06-23 01:22:43347 std::queue<GURL> background_queue_;
[email protected]a20bc092009-06-05 01:34:20348
349 DISALLOW_COPY_AND_ASSIGN(HostNameQueue);
350 };
351
[email protected]67372ecf2011-09-10 01:30:46352 // The InitialObserver monitors navigations made by the network stack. This
353 // is only used to identify startup time resolutions (for re-resolution
354 // during our next process startup).
355 // TODO(jar): Consider preconnecting at startup, which may be faster than
356 // waiting for render process to start and request a connection.
357 class InitialObserver {
358 public:
359 InitialObserver();
360 ~InitialObserver();
361 // Recording of when we observed each navigation.
362 typedef std::map<GURL, base::TimeTicks> FirstNavigations;
363
364 // Potentially add a new URL to our startup list.
365 void Append(const GURL& url, Predictor* predictor);
366
367 // Get an HTML version of our current planned first_navigations_.
368 void GetFirstResolutionsHtml(std::string* output);
369
370 // Persist the current first_navigations_ for storage in a list.
371 void GetInitialDnsResolutionList(base::ListValue* startup_list);
372
373 // Discards all initial loading history.
374 void DiscardInitialNavigationHistory() { first_navigations_.clear(); }
375
376 private:
377 // List of the first N URL resolutions observed in this run.
378 FirstNavigations first_navigations_;
379
380 // The number of URLs we'll save for pre-resolving at next startup.
381 static const size_t kStartupResolutionCount = 10;
382 };
383
[email protected]760d970a2010-05-18 00:39:18384 // A map that is keyed with the host/port that we've learned were the cause
385 // of loading additional URLs. The list of additional targets is held
386 // in a Referrer instance, which is a value in this map.
csharrison43bf3c32016-06-23 21:49:47387 typedef base::MRUCache<GURL, Referrer> Referrers;
[email protected]7c19b87b02009-01-26 16:19:44388
[email protected]579f2a12011-04-06 16:27:31389 // Depending on the expected_subresource_use_, we may either make a TCP/IP
390 // preconnection, or merely pre-resolve the hostname via DNS (or even do
391 // nothing). The following are the threasholds for taking those actions.
392 static const double kPreconnectWorthyExpectedValue;
393 static const double kDNSPreresolutionWorthyExpectedValue;
[email protected]579f2a12011-04-06 16:27:31394 static const double kDiscardableExpectedValue;
[email protected]579f2a12011-04-06 16:27:31395
[email protected]16b061fe2014-08-13 15:54:05396 // These two members call the appropriate global functions in
397 // prediction_options.cc depending on which thread they are called on.
398 virtual bool CanPrefetchAndPrerender() const;
399 virtual bool CanPreresolveAndPreconnect() const;
[email protected]8a9712c22014-07-21 22:47:28400
[email protected]67372ecf2011-09-10 01:30:46401 // ------------- Start IO thread methods.
402
403 // Perform actual resolution or preconnection to subresources now. This is
404 // an internal worker method that is reached via a post task from
405 // PredictFrameSubresources().
[email protected]e6d017652013-05-17 18:01:40406 void PrepareFrameSubresources(const GURL& url,
407 const GURL& first_party_for_cookies);
[email protected]67372ecf2011-09-10 01:30:46408
[email protected]85398532009-06-16 21:32:18409 // Access method for use by async lookup request to pass resolution result.
csharrisonf848daf22016-06-01 21:27:59410 void OnLookupFinished(const GURL& url, int result);
[email protected]1933eb202009-02-19 18:23:25411
[email protected]85398532009-06-16 21:32:18412 // Underlying method for both async and synchronous lookup to update state.
csharrisonf848daf22016-06-01 21:27:59413 void LookupFinished(const GURL& url, bool found);
[email protected]85398532009-06-16 21:32:18414
csharrisonf848daf22016-06-01 21:27:59415 // Queues hostname for resolution. If queueing was done, return the pointer
416 // to the queued instance, otherwise return nullptr. If the proxy advisor is
[email protected]c9043a882013-11-16 00:06:00417 // enabled, and |url| is likely to be proxied, the hostname will not be
418 // queued as the browser is not expected to fetch it directly.
[email protected]74be069e82010-06-25 00:12:49419 UrlInfo* AppendToResolutionQueue(const GURL& url,
[email protected]c9043a882013-11-16 00:06:00420 UrlInfo::ResolutionMotivation motivation);
initial.commit09911bf2008-07-26 23:55:29421
[email protected]a20bc092009-06-05 01:34:20422 // Check to see if too much queuing delay has been noted for the given info,
423 // which indicates that there is "congestion" or growing delay in handling the
424 // resolution of names. Rather than letting this congestion potentially grow
425 // without bounds, we abandon our queued efforts at pre-resolutions in such a
426 // case.
427 // To do this, we will recycle |info|, as well as all queued items, back to
428 // the state they had before they were queued up. We can't do anything about
429 // the resolutions we've already sent off for processing on another thread, so
430 // we just let them complete. On a slow system, subject to congestion, this
431 // will greatly reduce the number of resolutions done, but it will assure that
432 // any resolutions that are done, are in a timely and hence potentially
433 // helpful manner.
[email protected]74be069e82010-06-25 00:12:49434 bool CongestionControlPerformed(UrlInfo* info);
[email protected]a20bc092009-06-05 01:34:20435
436 // Take lookup requests from work_queue_ and tell HostResolver to look them up
437 // asynchronously, provided we don't exceed concurrent resolution limit.
[email protected]ec86bea2009-12-08 18:35:14438 void StartSomeQueuedResolutions();
initial.commit09911bf2008-07-26 23:55:29439
bemasc1515a12c2014-10-20 22:30:16440 // If we can determine immediately (i.e. synchronously) that requests to this
441 // URL would likely go through a proxy, then return true. Otherwise, return
442 // false. This is used to avoid issuing DNS requests when a fixed proxy
443 // configuration is in place, which improves efficiency, and is also important
444 // if the unproxied DNS may contain incorrect entries.
445 bool WouldLikelyProxyURL(const GURL& url);
446
[email protected]bb5ec7852014-04-02 00:45:49447 // Applies the HSTS redirect for |url|, if any.
448 GURL GetHSTSRedirectOnIOThread(const GURL& url);
449
csharrisond0da3d12016-06-21 03:40:39450 void LogStartupMetrics();
451
[email protected]67372ecf2011-09-10 01:30:46452 // ------------- End IO thread methods.
453
dcheng4e7c0422016-04-14 00:59:05454 std::unique_ptr<InitialObserver> initial_observer_;
[email protected]67372ecf2011-09-10 01:30:46455
[email protected]7c46a7082012-01-14 01:24:36456 // Reference to URLRequestContextGetter from the Profile which owns the
457 // predictor. Used by Preconnect.
458 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
459
[email protected]67372ecf2011-09-10 01:30:46460 // Status of speculative DNS resolution and speculative TCP/IP connection
[email protected]8a9712c22014-07-21 22:47:28461 // feature. This is false if and only if disabled by a command line switch.
csharrisonefcb48d2016-08-12 03:05:32462 // Protected by |preconnect_enabled_lock_|, which is used by tests to bypass
463 // the command line flags.
464 bool predictor_enabled_;
[email protected]8a9712c22014-07-21 22:47:28465
466 // This is set by InitNetworkPredictor and used for calling
bnc85ee7102014-09-02 16:15:57467 // CanPrefetchAndPrerenderUI and CanPreresolveAndPreconnectUI.
[email protected]8a9712c22014-07-21 22:47:28468 PrefService* user_prefs_;
469
470 // This is set by InitNetworkPredictor and used for calling
bnc85ee7102014-09-02 16:15:57471 // CanPrefetchAndPrerenderIO and CanPreresolveAndPreconnectIO.
[email protected]8a9712c22014-07-21 22:47:28472 ProfileIOData* profile_io_data_;
[email protected]67372ecf2011-09-10 01:30:46473
[email protected]a20bc092009-06-05 01:34:20474 // work_queue_ holds a list of names we need to look up.
475 HostNameQueue work_queue_;
initial.commit09911bf2008-07-26 23:55:29476
[email protected]21dae9b2008-11-06 23:32:53477 // results_ contains information for existing/prior prefetches.
initial.commit09911bf2008-07-26 23:55:29478 Results results_;
479
csharrisonf848daf22016-06-01 21:27:59480 size_t num_pending_lookups_;
initial.commit09911bf2008-07-26 23:55:29481
[email protected]1933eb202009-02-19 18:23:25482 // For testing, to verify that we don't exceed the limit.
483 size_t peak_pending_lookups_;
[email protected]b2b8b832009-02-06 19:03:29484
[email protected]1933eb202009-02-19 18:23:25485 // When true, we don't make new lookup requests.
[email protected]b2b8b832009-02-06 19:03:29486 bool shutdown_;
487
[email protected]755a93352010-10-29 06:33:59488 // The number of concurrent speculative lookups currently allowed to be sent
489 // to the resolver. Any additional lookups will be queued to avoid exceeding
490 // this value. The queue is a priority queue that will accelerate
491 // sub-resource speculation, and retard resolutions suggested by page scans.
[email protected]74be069e82010-06-25 00:12:49492 const size_t max_concurrent_dns_lookups_;
[email protected]e085c302009-06-01 18:31:36493
[email protected]602faf3c2009-06-27 14:35:44494 // The maximum queueing delay that is acceptable before we enter congestion
495 // reduction mode, and discard all queued (but not yet assigned) resolutions.
[email protected]74be069e82010-06-25 00:12:49496 const base::TimeDelta max_dns_queue_delay_;
[email protected]602faf3c2009-06-27 14:35:44497
[email protected]bb5ec7852014-04-02 00:45:49498 // The TransportSecurityState instance we query HSTS redirects from.
499 net::TransportSecurityState* transport_security_state_;
500
501 // The SSLConfigService we query SNI support from (used in querying HSTS
502 // redirects).
503 net::SSLConfigService* ssl_config_service_;
504
bemasc1515a12c2014-10-20 22:30:16505 // The ProxyService, used to determine whether preresolve is useful.
506 net::ProxyService* proxy_service_;
507
[email protected]1455ccf12010-08-18 16:32:14508 // Most recent suggestion from Omnibox provided via AnticipateOmniboxUrl().
509 std::string last_omnibox_host_;
510
511 // The time when the last preresolve was done for last_omnibox_host_.
512 base::TimeTicks last_omnibox_preresolve_;
513
514 // The number of consecutive requests to AnticipateOmniboxUrl() that suggested
515 // preconnecting (because it was to a search service).
516 int consecutive_omnibox_preconnect_count_;
517
518 // The time when the last preconnection was requested to a search service.
519 base::TimeTicks last_omnibox_preconnect_;
520
[email protected]579f2a12011-04-06 16:27:31521 // For each URL that we might navigate to (that we've "learned about")
522 // we have a Referrer list. Each Referrer list has all hostnames we might
523 // need to pre-resolve or pre-connect to when there is a navigation to the
524 // orginial hostname.
525 Referrers referrers_;
526
[email protected]bb5ec7852014-04-02 00:45:49527 // An observer for testing.
528 PredictorObserver* observer_;
529
csharrisoncb0f40152016-06-01 00:13:09530 std::unique_ptr<TimedCache> timed_cache_;
531
csharrisonee456332016-08-01 19:20:03532 // TODO(csharrison): It is not great that two weak pointer factories are
533 // needed in this class. Let's split it into two classes that live on each
534 // thread.
535 //
536 // Weak factory for weak pointers that should be dereferenced on the IO
537 // thread.
538 std::unique_ptr<base::WeakPtrFactory<Predictor>> io_weak_factory_;
539
540 // Weak factory for weak pointers that should be dereferenced on the UI
541 // thread.
542 std::unique_ptr<base::WeakPtrFactory<Predictor>> ui_weak_factory_;
mohan.reddy14cb4ad42014-09-17 18:15:14543
csharrisonefcb48d2016-08-12 03:05:32544 // Protects |predictor_enabled_|.
545 mutable base::Lock predictor_enabled_lock_;
csharrisona7b16112016-05-13 23:25:39546
[email protected]74be069e82010-06-25 00:12:49547 DISALLOW_COPY_AND_ASSIGN(Predictor);
initial.commit09911bf2008-07-26 23:55:29548};
549
[email protected]67372ecf2011-09-10 01:30:46550// This version of the predictor is used for testing.
551class SimplePredictor : public Predictor {
552 public:
csharrisonefcb48d2016-08-12 03:05:32553 explicit SimplePredictor(bool predictor_enabled)
554 : Predictor(predictor_enabled) {}
dchengfce29ad2014-10-23 03:47:47555 ~SimplePredictor() override {}
556 void InitNetworkPredictor(PrefService* user_prefs,
dchengfce29ad2014-10-23 03:47:47557 IOThread* io_thread,
558 net::URLRequestContextGetter* getter,
559 ProfileIOData* profile_io_data) override;
560 void ShutdownOnUIThread() override;
561
[email protected]8a9712c22014-07-21 22:47:28562 private:
[email protected]16b061fe2014-08-13 15:54:05563 // These member functions return True for unittests.
dchengfce29ad2014-10-23 03:47:47564 bool CanPrefetchAndPrerender() const override;
565 bool CanPreresolveAndPreconnect() const override;
[email protected]67372ecf2011-09-10 01:30:46566};
567
initial.commit09911bf2008-07-26 23:55:29568} // namespace chrome_browser_net
569
[email protected]3530cd92010-06-27 06:22:01570#endif // CHROME_BROWSER_NET_PREDICTOR_H_