[email protected] | 7c46a708 | 2012-01-14 01:24:36 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 5 | // 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] | f4ef861ba | 2010-07-28 22:37:23 | [diff] [blame] | 16 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | |
[email protected] | 3530cd9 | 2010-06-27 06:22:01 | [diff] [blame] | 19 | #ifndef CHROME_BROWSER_NET_PREDICTOR_H_ |
| 20 | #define CHROME_BROWSER_NET_PREDICTOR_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | |
r.karu | 9726030 | 2016-01-06 14:08:30 | [diff] [blame] | 22 | #include <stddef.h> |
| 23 | #include <stdint.h> |
| 24 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | #include <map> |
dcheng | 4e7c042 | 2016-04-14 00:59:05 | [diff] [blame] | 26 | #include <memory> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | #include <queue> |
| 28 | #include <string> |
[email protected] | 579f2a1 | 2011-04-06 16:27:31 | [diff] [blame] | 29 | #include <vector> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 30 | |
csharrison | 43bf3c3 | 2016-06-23 21:49:47 | [diff] [blame] | 31 | #include "base/containers/mru_cache.h" |
[email protected] | a918f87 | 2010-06-01 14:30:51 | [diff] [blame] | 32 | #include "base/gtest_prod_util.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 33 | #include "base/macros.h" |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 34 | #include "base/memory/weak_ptr.h" |
[email protected] | 8a9712c2 | 2014-07-21 22:47:28 | [diff] [blame] | 35 | #include "chrome/browser/net/prediction_options.h" |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 36 | #include "chrome/browser/net/referrer.h" |
[email protected] | 685e284 | 2013-05-27 09:43:56 | [diff] [blame] | 37 | #include "chrome/browser/net/timed_cache.h" |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 38 | #include "chrome/browser/net/url_info.h" |
pmeenan | ed4b6f8 | 2015-01-30 21:59:59 | [diff] [blame] | 39 | #include "components/network_hints/common/network_hints_common.h" |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 40 | #include "net/base/host_port_pair.h" |
csharrison | a7b1611 | 2016-05-13 23:25:39 | [diff] [blame] | 41 | #include "url/gurl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | |
[email protected] | 443e931 | 2013-05-06 06:17:34 | [diff] [blame] | 43 | class IOThread; |
| 44 | class PrefService; |
| 45 | class Profile; |
[email protected] | 8a9712c2 | 2014-07-21 22:47:28 | [diff] [blame] | 46 | class ProfileIOData; |
[email protected] | c02c853d7 | 2010-08-07 06:23:24 | [diff] [blame] | 47 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 48 | namespace base { |
[email protected] | 443e931 | 2013-05-06 06:17:34 | [diff] [blame] | 49 | class ListValue; |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 50 | } |
| 51 | |
[email protected] | fd2f8afe | 2009-06-11 21:53:55 | [diff] [blame] | 52 | namespace net { |
| 53 | class HostResolver; |
[email protected] | bb5ec785 | 2014-04-02 00:45:49 | [diff] [blame] | 54 | class SSLConfigService; |
bemasc | 1515a12c | 2014-10-20 22:30:16 | [diff] [blame] | 55 | class ProxyService; |
[email protected] | bb5ec785 | 2014-04-02 00:45:49 | [diff] [blame] | 56 | class TransportSecurityState; |
[email protected] | 7c46a708 | 2012-01-14 01:24:36 | [diff] [blame] | 57 | class URLRequestContextGetter; |
[email protected] | 443e931 | 2013-05-06 06:17:34 | [diff] [blame] | 58 | } |
[email protected] | fd2f8afe | 2009-06-11 21:53:55 | [diff] [blame] | 59 | |
[email protected] | 443e931 | 2013-05-06 06:17:34 | [diff] [blame] | 60 | namespace user_prefs { |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 61 | class PrefRegistrySyncable; |
[email protected] | 443e931 | 2013-05-06 06:17:34 | [diff] [blame] | 62 | } |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 63 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 64 | namespace chrome_browser_net { |
| 65 | |
lizeb | 8e8aa98f | 2017-06-27 09:30:25 | [diff] [blame^] | 66 | class InitialObserver; |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 67 | typedef std::map<GURL, UrlInfo> Results; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | |
[email protected] | bb5ec785 | 2014-04-02 00:45:49 | [diff] [blame] | 69 | // An observer for testing. |
| 70 | class PredictorObserver { |
| 71 | public: |
| 72 | virtual ~PredictorObserver() {} |
| 73 | |
| 74 | virtual void OnPreconnectUrl(const GURL& original_url, |
| 75 | const GURL& first_party_for_cookies, |
| 76 | UrlInfo::ResolutionMotivation motivation, |
csharrison | a7b1611 | 2016-05-13 23:25:39 | [diff] [blame] | 77 | int count) {} |
| 78 | virtual void OnLearnFromNavigation(const GURL& referring_url, |
| 79 | const GURL& target_url) {} |
csharrison | 0597b217 | 2016-05-27 12:24:04 | [diff] [blame] | 80 | |
| 81 | virtual void OnDnsLookupFinished(const GURL& url, bool found) {} |
[email protected] | bb5ec785 | 2014-04-02 00:45:49 | [diff] [blame] | 82 | }; |
| 83 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 84 | // Predictor is constructed during Profile construction (on the UI thread), |
| 85 | // but it is destroyed on the IO thread when ProfileIOData goes away. All of |
| 86 | // its core state and functionality happens on the IO thread. The only UI |
| 87 | // methods are initialization / shutdown related (including preconnect |
| 88 | // initialization), or convenience methods that internally forward calls to |
| 89 | // the IO thread. |
| 90 | class Predictor { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 91 | public: |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 92 | // A version number for prefs that are saved. This should be incremented when |
| 93 | // we change the format so that we discard old data. |
[email protected] | d643172 | 2011-09-01 00:46:33 | [diff] [blame] | 94 | static const int kPredictorReferrerVersion; |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 95 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 96 | // Given that the underlying Chromium resolver defaults to a total maximum of |
| 97 | // 8 paralell resolutions, we will avoid any chance of starving navigational |
| 98 | // resolutions by limiting the number of paralell speculative resolutions. |
| 99 | // This is used in the field trials and testing. |
| 100 | // TODO(jar): Move this limitation into the resolver. |
| 101 | static const size_t kMaxSpeculativeParallelResolves; |
| 102 | |
| 103 | // To control the congestion avoidance system, we need an estimate of how |
| 104 | // many speculative requests may arrive at once. Since we currently only |
| 105 | // keep 8 subresource names for each frame, we'll use that as our basis. |
| 106 | // Note that when scanning search results lists, we might actually get 10 at |
| 107 | // a time, and wikipedia can often supply (during a page scan) upwards of 50. |
| 108 | // In those odd cases, we may discard some of the later speculative requests |
| 109 | // mistakenly assuming that the resolutions took too long. |
| 110 | static const int kTypicalSpeculativeGroupSize; |
| 111 | |
| 112 | // The next constant specifies an amount of queueing delay that is |
| 113 | // "too large," and indicative of problems with resolutions (perhaps due to |
| 114 | // an overloaded router, or such). When we exceed this delay, congestion |
| 115 | // avoidance will kick in and all speculations in the queue will be discarded. |
| 116 | static const int kMaxSpeculativeResolveQueueDelayMs; |
| 117 | |
[email protected] | 685e284 | 2013-05-27 09:43:56 | [diff] [blame] | 118 | // We don't bother learning to preconnect via a GET if the original URL |
| 119 | // navigation was so long ago, that a preconnection would have been dropped |
| 120 | // anyway. We believe most servers will drop the connection in 10 seconds, so |
| 121 | // we currently estimate this time-till-drop at 10 seconds. |
| 122 | // TODO(jar): We should do a persistent field trial to validate/optimize this. |
| 123 | static const int kMaxUnusedSocketLifetimeSecondsWithoutAGet; |
| 124 | |
csharrison | 43bf3c3 | 2016-06-23 21:49:47 | [diff] [blame] | 125 | // The maximum size of the MRU cache of referrers. |
| 126 | static const int kMaxReferrers; |
| 127 | |
csharrison | efcb48d | 2016-08-12 03:05:32 | [diff] [blame] | 128 | explicit Predictor(bool predictor_enabled); |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 129 | |
| 130 | virtual ~Predictor(); |
| 131 | |
| 132 | // This function is used to create a predictor. For testing, we can create |
| 133 | // a version which does a simpler shutdown. |
csharrison | efcb48d | 2016-08-12 03:05:32 | [diff] [blame] | 134 | // TODO(636128): This method should return a unique_ptr. |
| 135 | static Predictor* CreatePredictor(bool simple_shutdown); |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 136 | |
[email protected] | 37ca3fe0 | 2013-07-05 15:32:44 | [diff] [blame] | 137 | static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 138 | |
| 139 | // ------------- Start UI thread methods. |
| 140 | |
| 141 | virtual void InitNetworkPredictor(PrefService* user_prefs, |
[email protected] | 7c46a708 | 2012-01-14 01:24:36 | [diff] [blame] | 142 | IOThread* io_thread, |
[email protected] | 8a9712c2 | 2014-07-21 22:47:28 | [diff] [blame] | 143 | net::URLRequestContextGetter* getter, |
| 144 | ProfileIOData* profile_io_data); |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 145 | |
| 146 | // The Omnibox has proposed a given url to the user, and if it is a search |
| 147 | // URL, then it also indicates that this is preconnectable (i.e., we could |
| 148 | // preconnect to the search server). |
| 149 | void AnticipateOmniboxUrl(const GURL& url, bool preconnectable); |
| 150 | |
| 151 | // Preconnect a URL and all of its subresource domains. |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 152 | void PreconnectUrlAndSubresources(const GURL& url, |
| 153 | const GURL& first_party_for_cookies); |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 154 | |
csharrison | b4c49706 | 2016-06-02 18:00:44 | [diff] [blame] | 155 | static std::vector<GURL> GetPredictedUrlListAtStartup( |
| 156 | PrefService* user_prefs); |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 157 | |
csharrison | f2113bc | 2016-06-29 15:07:55 | [diff] [blame] | 158 | // Calls ClearPrefsOnUIThread and posts a task to the IO thread to |
| 159 | // DiscardAllResults. |
| 160 | void DiscardAllResultsAndClearPrefsOnUIThread(); |
| 161 | |
| 162 | // Clears the preferences used by the predictor. Must be called on the UI |
| 163 | // thread. |
| 164 | void ClearPrefsOnUIThread(); |
| 165 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 166 | static void set_max_queueing_delay(int max_queueing_delay_ms); |
| 167 | |
| 168 | static void set_max_parallel_resolves(size_t max_parallel_resolves); |
| 169 | |
[email protected] | 1101dbc5 | 2013-10-05 00:19:12 | [diff] [blame] | 170 | virtual void ShutdownOnUIThread(); |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 171 | |
csharrison | ee45633 | 2016-08-01 19:20:03 | [diff] [blame] | 172 | void UpdatePrefsOnUIThread(std::unique_ptr<base::ListValue> startup_list, |
| 173 | std::unique_ptr<base::ListValue> referral_list); |
| 174 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 175 | // ------------- End UI thread methods. |
| 176 | |
| 177 | // ------------- Start IO thread methods. |
[email protected] | b2b8b83 | 2009-02-06 19:03:29 | [diff] [blame] | 178 | |
csharrison | ee45633 | 2016-08-01 19:20:03 | [diff] [blame] | 179 | void WriteDnsPrefetchState(base::ListValue* startup_list, |
| 180 | base::ListValue* referral_list); |
| 181 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 182 | // Cancel pending requests and prevent new ones from being made. |
| 183 | void Shutdown(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 184 | |
| 185 | // In some circumstances, for privacy reasons, all results should be |
| 186 | // discarded. This method gracefully handles that activity. |
| 187 | // Destroy all our internal state, which shows what names we've looked up, and |
| 188 | // how long each has taken, etc. etc. We also destroy records of suggesses |
| 189 | // (cache hits etc.). |
| 190 | void DiscardAllResults(); |
| 191 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 192 | // Add hostname(s) to the queue for processing. |
csharrison | b4c49706 | 2016-06-02 18:00:44 | [diff] [blame] | 193 | void ResolveList(const std::vector<GURL>& urls, |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 194 | UrlInfo::ResolutionMotivation motivation); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 195 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 196 | void Resolve(const GURL& url, UrlInfo::ResolutionMotivation motivation); |
[email protected] | e326922d | 2010-09-03 09:08:10 | [diff] [blame] | 197 | |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 198 | // Record details of a navigation so that we can preresolve the host name |
| 199 | // ahead of time the next time the users navigates to the indicated host. |
[email protected] | d6bb256 | 2010-08-25 23:31:30 | [diff] [blame] | 200 | // Should only be called when urls are distinct, and they should already be |
| 201 | // canonicalized to not have a path. |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 202 | void LearnFromNavigation(const GURL& referring_url, const GURL& target_url); |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 203 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 204 | // When displaying info in about:dns, the following API is called. |
| 205 | static void PredictorGetHtmlInfo(Predictor* predictor, std::string* output); |
| 206 | |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 207 | // Dump HTML table containing list of referrers for about:dns. |
| 208 | void GetHtmlReferrerLists(std::string* output); |
| 209 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 210 | // Dump the list of currently known referrer domains and related prefetchable |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 211 | // domains for about:dns. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 212 | void GetHtmlInfo(std::string* output); |
| 213 | |
[email protected] | 03c5e86 | 2009-02-17 22:50:14 | [diff] [blame] | 214 | // Construct a ListValue object that contains all the data in the referrers_ |
| 215 | // so that it can be persisted in a pref. |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 216 | void SerializeReferrers(base::ListValue* referral_list); |
[email protected] | 03c5e86 | 2009-02-17 22:50:14 | [diff] [blame] | 217 | |
| 218 | // Process a ListValue that contains all the data from a previous reference |
| 219 | // list, as constructed by SerializeReferrers(), and add all the identified |
| 220 | // values into the current referrer list. |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 221 | void DeserializeReferrers(const base::ListValue& referral_list); |
[email protected] | 03c5e86 | 2009-02-17 22:50:14 | [diff] [blame] | 222 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 223 | void DiscardInitialNavigationHistory(); |
[email protected] | e695fbd6 | 2009-06-30 16:31:54 | [diff] [blame] | 224 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 225 | void FinalizeInitializationOnIOThread( |
| 226 | const std::vector<GURL>& urls_to_prefetch, |
csharrison | f2113bc | 2016-06-29 15:07:55 | [diff] [blame] | 227 | std::unique_ptr<base::ListValue> referral_list, |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 228 | IOThread* io_thread, |
[email protected] | 8a9712c2 | 2014-07-21 22:47:28 | [diff] [blame] | 229 | ProfileIOData* profile_io_data); |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 230 | |
| 231 | // During startup, we learn what the first N urls visited are, and then |
| 232 | // resolve the associated hosts ASAP during our next startup. |
| 233 | void LearnAboutInitialNavigation(const GURL& url); |
| 234 | |
| 235 | // Renderer bundles up list and sends to this browser API via IPC. |
csharrison | b4c49706 | 2016-06-02 18:00:44 | [diff] [blame] | 236 | // TODO(csharrison): Use a GURL vector instead to include port and scheme. |
| 237 | void DnsPrefetchList(const std::vector<std::string>& hostnames); |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 238 | |
| 239 | // May be called from either the IO or UI thread and will PostTask |
| 240 | // to the IO thread if necessary. |
csharrison | b4c49706 | 2016-06-02 18:00:44 | [diff] [blame] | 241 | void DnsPrefetchMotivatedList(const std::vector<GURL>& urls, |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 242 | UrlInfo::ResolutionMotivation motivation); |
| 243 | |
csharrison | ee45633 | 2016-08-01 19:20:03 | [diff] [blame] | 244 | // Called from the UI thread in response to the load event. |
csharrison | 43bf3c3 | 2016-06-23 21:49:47 | [diff] [blame] | 245 | void SaveStateForNextStartup(); |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 246 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 247 | // May be called from either the IO or UI thread and will PostTask |
| 248 | // to the IO thread if necessary. |
yoav | 9131644 | 2015-07-29 06:43:34 | [diff] [blame] | 249 | void PreconnectUrl(const GURL& url, |
| 250 | const GURL& first_party_for_cookies, |
| 251 | UrlInfo::ResolutionMotivation motivation, |
| 252 | bool allow_credentials, |
| 253 | int count); |
[email protected] | 685e284 | 2013-05-27 09:43:56 | [diff] [blame] | 254 | |
| 255 | void PreconnectUrlOnIOThread(const GURL& url, |
| 256 | const GURL& first_party_for_cookies, |
| 257 | UrlInfo::ResolutionMotivation motivation, |
yoav | 9131644 | 2015-07-29 06:43:34 | [diff] [blame] | 258 | bool allow_credentials, |
[email protected] | 685e284 | 2013-05-27 09:43:56 | [diff] [blame] | 259 | int count); |
| 260 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 261 | // ------------- End IO thread methods. |
| 262 | |
| 263 | // The following methods may be called on either the IO or UI threads. |
| 264 | |
| 265 | // Instigate pre-connection to any URLs, or pre-resolution of related host, |
| 266 | // that we predict will be needed after this navigation (typically |
| 267 | // more-embedded resources on a page). This method will actually post a task |
| 268 | // to do the actual work, so as not to jump ahead of the frame navigation that |
| 269 | // instigated this activity. |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 270 | void PredictFrameSubresources(const GURL& url, |
| 271 | const GURL& first_party_for_cookies); |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 272 | |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 273 | // Put URL in canonical form, including a scheme, host, and port. |
| 274 | // Returns GURL::EmptyGURL() if the scheme is not http/https or if the url |
| 275 | // cannot be otherwise canonicalized. |
| 276 | static GURL CanonicalizeUrl(const GURL& url); |
| 277 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 278 | // Used for testing. |
[email protected] | bb5ec785 | 2014-04-02 00:45:49 | [diff] [blame] | 279 | void SetTransportSecurityState( |
| 280 | net::TransportSecurityState* transport_security_state) { |
| 281 | transport_security_state_ = transport_security_state; |
| 282 | } |
| 283 | // Used for testing. |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 284 | size_t max_concurrent_dns_lookups() const { |
| 285 | return max_concurrent_dns_lookups_; |
| 286 | } |
| 287 | // Used for testing. |
| 288 | void SetShutdown(bool shutdown) { |
| 289 | shutdown_ = shutdown; |
| 290 | } |
[email protected] | bb5ec785 | 2014-04-02 00:45:49 | [diff] [blame] | 291 | // Used for testing. |
| 292 | void SetObserver(PredictorObserver* observer) { |
| 293 | observer_ = observer; |
| 294 | } |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 295 | |
[email protected] | 8a9712c2 | 2014-07-21 22:47:28 | [diff] [blame] | 296 | ProfileIOData* profile_io_data() const { |
| 297 | return profile_io_data_; |
| 298 | } |
| 299 | |
csharrison | efcb48d | 2016-08-12 03:05:32 | [diff] [blame] | 300 | bool PredictorEnabled() const; |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 301 | |
csharrison | efcb48d | 2016-08-12 03:05:32 | [diff] [blame] | 302 | // Used only for testing. Overrides command line flag to disable the |
| 303 | // predictor, which is added in the browser test fixture. |
| 304 | void SetPredictorEnabledForTest(bool predictor_enabled); |
csharrison | a7b1611 | 2016-05-13 23:25:39 | [diff] [blame] | 305 | |
| 306 | net::URLRequestContextGetter* url_request_context_getter_for_test() { |
| 307 | return url_request_context_getter_.get(); |
| 308 | } |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 309 | |
csharrison | cb0f4015 | 2016-06-01 00:13:09 | [diff] [blame] | 310 | TimedCache* timed_cache() { return timed_cache_.get(); } |
| 311 | |
[email protected] | b2b8b83 | 2009-02-06 19:03:29 | [diff] [blame] | 312 | private: |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 313 | FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueuePushPopTest); |
| 314 | FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueueReorderTest); |
[email protected] | 579f2a1 | 2011-04-06 16:27:31 | [diff] [blame] | 315 | FRIEND_TEST_ALL_PREFIXES(PredictorTest, ReferrerSerializationTrimTest); |
[email protected] | c9043a88 | 2013-11-16 00:06:00 | [diff] [blame] | 316 | FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTestWithDisabledAdvisor); |
| 317 | FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTestWithEnabledAdvisor); |
| 318 | FRIEND_TEST_ALL_PREFIXES(PredictorTest, TestSimplePreconnectAdvisor); |
bemasc | 1515a12c | 2014-10-20 22:30:16 | [diff] [blame] | 319 | FRIEND_TEST_ALL_PREFIXES(PredictorTest, NoProxyService); |
| 320 | FRIEND_TEST_ALL_PREFIXES(PredictorTest, ProxyDefinitelyEnabled); |
| 321 | FRIEND_TEST_ALL_PREFIXES(PredictorTest, ProxyDefinitelyNotEnabled); |
| 322 | FRIEND_TEST_ALL_PREFIXES(PredictorTest, ProxyMaybeEnabled); |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 323 | friend class WaitForResolutionHelper; // For testing. |
csharrison | 0597b217 | 2016-05-27 12:24:04 | [diff] [blame] | 324 | friend class PredictorBrowserTest; |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 325 | |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 326 | // A simple priority queue for handling host names. |
| 327 | // Some names that are queued up have |motivation| that requires very rapid |
| 328 | // handling. For example, a sub-resource name lookup MUST be done before the |
| 329 | // actual sub-resource is fetched. In contrast, a name that was speculatively |
| 330 | // noted in a page has to be resolved before the user "gets around to" |
| 331 | // clicking on a link. By tagging (with a motivation) each push we make into |
| 332 | // this FIFO queue, the queue can re-order the more important names to service |
| 333 | // them sooner (relative to some low priority background resolutions). |
| 334 | class HostNameQueue { |
| 335 | public: |
| 336 | HostNameQueue(); |
| 337 | ~HostNameQueue(); |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 338 | void Push(const GURL& url, |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 339 | UrlInfo::ResolutionMotivation motivation); |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 340 | bool IsEmpty() const; |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 341 | GURL Pop(); |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 342 | |
[email protected] | 579f2a1 | 2011-04-06 16:27:31 | [diff] [blame] | 343 | private: |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 344 | // The names in the queue that should be serviced (popped) ASAP. |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 345 | std::queue<GURL> rush_queue_; |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 346 | // The names in the queue that should only be serviced when rush_queue is |
| 347 | // empty. |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 348 | std::queue<GURL> background_queue_; |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 349 | |
| 350 | DISALLOW_COPY_AND_ASSIGN(HostNameQueue); |
| 351 | }; |
| 352 | |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 353 | // A map that is keyed with the host/port that we've learned were the cause |
| 354 | // of loading additional URLs. The list of additional targets is held |
| 355 | // in a Referrer instance, which is a value in this map. |
csharrison | 43bf3c3 | 2016-06-23 21:49:47 | [diff] [blame] | 356 | typedef base::MRUCache<GURL, Referrer> Referrers; |
[email protected] | 7c19b87b0 | 2009-01-26 16:19:44 | [diff] [blame] | 357 | |
[email protected] | 579f2a1 | 2011-04-06 16:27:31 | [diff] [blame] | 358 | // Depending on the expected_subresource_use_, we may either make a TCP/IP |
| 359 | // preconnection, or merely pre-resolve the hostname via DNS (or even do |
| 360 | // nothing). The following are the threasholds for taking those actions. |
| 361 | static const double kPreconnectWorthyExpectedValue; |
| 362 | static const double kDNSPreresolutionWorthyExpectedValue; |
[email protected] | 579f2a1 | 2011-04-06 16:27:31 | [diff] [blame] | 363 | static const double kDiscardableExpectedValue; |
[email protected] | 579f2a1 | 2011-04-06 16:27:31 | [diff] [blame] | 364 | |
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 365 | // These two members call the appropriate global functions in |
| 366 | // prediction_options.cc depending on which thread they are called on. |
| 367 | virtual bool CanPrefetchAndPrerender() const; |
| 368 | virtual bool CanPreresolveAndPreconnect() const; |
[email protected] | 8a9712c2 | 2014-07-21 22:47:28 | [diff] [blame] | 369 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 370 | // ------------- Start IO thread methods. |
| 371 | |
| 372 | // Perform actual resolution or preconnection to subresources now. This is |
| 373 | // an internal worker method that is reached via a post task from |
| 374 | // PredictFrameSubresources(). |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 375 | void PrepareFrameSubresources(const GURL& url, |
| 376 | const GURL& first_party_for_cookies); |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 377 | |
[email protected] | 8539853 | 2009-06-16 21:32:18 | [diff] [blame] | 378 | // Access method for use by async lookup request to pass resolution result. |
csharrison | f848daf2 | 2016-06-01 21:27:59 | [diff] [blame] | 379 | void OnLookupFinished(const GURL& url, int result); |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 380 | |
[email protected] | 8539853 | 2009-06-16 21:32:18 | [diff] [blame] | 381 | // Underlying method for both async and synchronous lookup to update state. |
csharrison | f848daf2 | 2016-06-01 21:27:59 | [diff] [blame] | 382 | void LookupFinished(const GURL& url, bool found); |
[email protected] | 8539853 | 2009-06-16 21:32:18 | [diff] [blame] | 383 | |
csharrison | f848daf2 | 2016-06-01 21:27:59 | [diff] [blame] | 384 | // Queues hostname for resolution. If queueing was done, return the pointer |
| 385 | // to the queued instance, otherwise return nullptr. If the proxy advisor is |
[email protected] | c9043a88 | 2013-11-16 00:06:00 | [diff] [blame] | 386 | // enabled, and |url| is likely to be proxied, the hostname will not be |
| 387 | // queued as the browser is not expected to fetch it directly. |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 388 | UrlInfo* AppendToResolutionQueue(const GURL& url, |
[email protected] | c9043a88 | 2013-11-16 00:06:00 | [diff] [blame] | 389 | UrlInfo::ResolutionMotivation motivation); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 390 | |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 391 | // Check to see if too much queuing delay has been noted for the given info, |
| 392 | // which indicates that there is "congestion" or growing delay in handling the |
| 393 | // resolution of names. Rather than letting this congestion potentially grow |
| 394 | // without bounds, we abandon our queued efforts at pre-resolutions in such a |
| 395 | // case. |
| 396 | // To do this, we will recycle |info|, as well as all queued items, back to |
| 397 | // the state they had before they were queued up. We can't do anything about |
| 398 | // the resolutions we've already sent off for processing on another thread, so |
| 399 | // we just let them complete. On a slow system, subject to congestion, this |
| 400 | // will greatly reduce the number of resolutions done, but it will assure that |
| 401 | // any resolutions that are done, are in a timely and hence potentially |
| 402 | // helpful manner. |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 403 | bool CongestionControlPerformed(UrlInfo* info); |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 404 | |
| 405 | // Take lookup requests from work_queue_ and tell HostResolver to look them up |
| 406 | // asynchronously, provided we don't exceed concurrent resolution limit. |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 407 | void StartSomeQueuedResolutions(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 408 | |
bemasc | 1515a12c | 2014-10-20 22:30:16 | [diff] [blame] | 409 | // If we can determine immediately (i.e. synchronously) that requests to this |
| 410 | // URL would likely go through a proxy, then return true. Otherwise, return |
| 411 | // false. This is used to avoid issuing DNS requests when a fixed proxy |
| 412 | // configuration is in place, which improves efficiency, and is also important |
| 413 | // if the unproxied DNS may contain incorrect entries. |
| 414 | bool WouldLikelyProxyURL(const GURL& url); |
| 415 | |
[email protected] | bb5ec785 | 2014-04-02 00:45:49 | [diff] [blame] | 416 | // Applies the HSTS redirect for |url|, if any. |
| 417 | GURL GetHSTSRedirectOnIOThread(const GURL& url); |
| 418 | |
csharrison | d0da3d1 | 2016-06-21 03:40:39 | [diff] [blame] | 419 | void LogStartupMetrics(); |
| 420 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 421 | // ------------- End IO thread methods. |
| 422 | |
dcheng | 4e7c042 | 2016-04-14 00:59:05 | [diff] [blame] | 423 | std::unique_ptr<InitialObserver> initial_observer_; |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 424 | |
[email protected] | 7c46a708 | 2012-01-14 01:24:36 | [diff] [blame] | 425 | // Reference to URLRequestContextGetter from the Profile which owns the |
| 426 | // predictor. Used by Preconnect. |
| 427 | scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 428 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 429 | // Status of speculative DNS resolution and speculative TCP/IP connection |
[email protected] | 8a9712c2 | 2014-07-21 22:47:28 | [diff] [blame] | 430 | // feature. This is false if and only if disabled by a command line switch. |
csharrison | efcb48d | 2016-08-12 03:05:32 | [diff] [blame] | 431 | // Protected by |preconnect_enabled_lock_|, which is used by tests to bypass |
| 432 | // the command line flags. |
| 433 | bool predictor_enabled_; |
[email protected] | 8a9712c2 | 2014-07-21 22:47:28 | [diff] [blame] | 434 | |
| 435 | // This is set by InitNetworkPredictor and used for calling |
bnc | 85ee710 | 2014-09-02 16:15:57 | [diff] [blame] | 436 | // CanPrefetchAndPrerenderUI and CanPreresolveAndPreconnectUI. |
[email protected] | 8a9712c2 | 2014-07-21 22:47:28 | [diff] [blame] | 437 | PrefService* user_prefs_; |
| 438 | |
| 439 | // This is set by InitNetworkPredictor and used for calling |
bnc | 85ee710 | 2014-09-02 16:15:57 | [diff] [blame] | 440 | // CanPrefetchAndPrerenderIO and CanPreresolveAndPreconnectIO. |
[email protected] | 8a9712c2 | 2014-07-21 22:47:28 | [diff] [blame] | 441 | ProfileIOData* profile_io_data_; |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 442 | |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 443 | // work_queue_ holds a list of names we need to look up. |
| 444 | HostNameQueue work_queue_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 445 | |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 446 | // results_ contains information for existing/prior prefetches. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 447 | Results results_; |
| 448 | |
csharrison | f848daf2 | 2016-06-01 21:27:59 | [diff] [blame] | 449 | size_t num_pending_lookups_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 450 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 451 | // For testing, to verify that we don't exceed the limit. |
| 452 | size_t peak_pending_lookups_; |
[email protected] | b2b8b83 | 2009-02-06 19:03:29 | [diff] [blame] | 453 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 454 | // When true, we don't make new lookup requests. |
[email protected] | b2b8b83 | 2009-02-06 19:03:29 | [diff] [blame] | 455 | bool shutdown_; |
| 456 | |
[email protected] | 755a9335 | 2010-10-29 06:33:59 | [diff] [blame] | 457 | // The number of concurrent speculative lookups currently allowed to be sent |
| 458 | // to the resolver. Any additional lookups will be queued to avoid exceeding |
| 459 | // this value. The queue is a priority queue that will accelerate |
| 460 | // sub-resource speculation, and retard resolutions suggested by page scans. |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 461 | const size_t max_concurrent_dns_lookups_; |
[email protected] | e085c30 | 2009-06-01 18:31:36 | [diff] [blame] | 462 | |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 463 | // The maximum queueing delay that is acceptable before we enter congestion |
| 464 | // reduction mode, and discard all queued (but not yet assigned) resolutions. |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 465 | const base::TimeDelta max_dns_queue_delay_; |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 466 | |
[email protected] | bb5ec785 | 2014-04-02 00:45:49 | [diff] [blame] | 467 | // The TransportSecurityState instance we query HSTS redirects from. |
| 468 | net::TransportSecurityState* transport_security_state_; |
| 469 | |
| 470 | // The SSLConfigService we query SNI support from (used in querying HSTS |
| 471 | // redirects). |
| 472 | net::SSLConfigService* ssl_config_service_; |
| 473 | |
bemasc | 1515a12c | 2014-10-20 22:30:16 | [diff] [blame] | 474 | // The ProxyService, used to determine whether preresolve is useful. |
| 475 | net::ProxyService* proxy_service_; |
| 476 | |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 477 | // Most recent suggestion from Omnibox provided via AnticipateOmniboxUrl(). |
| 478 | std::string last_omnibox_host_; |
| 479 | |
| 480 | // The time when the last preresolve was done for last_omnibox_host_. |
| 481 | base::TimeTicks last_omnibox_preresolve_; |
| 482 | |
| 483 | // The number of consecutive requests to AnticipateOmniboxUrl() that suggested |
| 484 | // preconnecting (because it was to a search service). |
| 485 | int consecutive_omnibox_preconnect_count_; |
| 486 | |
| 487 | // The time when the last preconnection was requested to a search service. |
| 488 | base::TimeTicks last_omnibox_preconnect_; |
| 489 | |
[email protected] | 579f2a1 | 2011-04-06 16:27:31 | [diff] [blame] | 490 | // For each URL that we might navigate to (that we've "learned about") |
| 491 | // we have a Referrer list. Each Referrer list has all hostnames we might |
| 492 | // need to pre-resolve or pre-connect to when there is a navigation to the |
| 493 | // orginial hostname. |
| 494 | Referrers referrers_; |
| 495 | |
[email protected] | bb5ec785 | 2014-04-02 00:45:49 | [diff] [blame] | 496 | // An observer for testing. |
| 497 | PredictorObserver* observer_; |
| 498 | |
csharrison | cb0f4015 | 2016-06-01 00:13:09 | [diff] [blame] | 499 | std::unique_ptr<TimedCache> timed_cache_; |
| 500 | |
csharrison | ee45633 | 2016-08-01 19:20:03 | [diff] [blame] | 501 | // TODO(csharrison): It is not great that two weak pointer factories are |
| 502 | // needed in this class. Let's split it into two classes that live on each |
| 503 | // thread. |
| 504 | // |
| 505 | // Weak factory for weak pointers that should be dereferenced on the IO |
| 506 | // thread. |
| 507 | std::unique_ptr<base::WeakPtrFactory<Predictor>> io_weak_factory_; |
| 508 | |
| 509 | // Weak factory for weak pointers that should be dereferenced on the UI |
| 510 | // thread. |
| 511 | std::unique_ptr<base::WeakPtrFactory<Predictor>> ui_weak_factory_; |
mohan.reddy | 14cb4ad4 | 2014-09-17 18:15:14 | [diff] [blame] | 512 | |
csharrison | efcb48d | 2016-08-12 03:05:32 | [diff] [blame] | 513 | // Protects |predictor_enabled_|. |
| 514 | mutable base::Lock predictor_enabled_lock_; |
csharrison | a7b1611 | 2016-05-13 23:25:39 | [diff] [blame] | 515 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 516 | DISALLOW_COPY_AND_ASSIGN(Predictor); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 517 | }; |
| 518 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 519 | // This version of the predictor is used for testing. |
| 520 | class SimplePredictor : public Predictor { |
| 521 | public: |
csharrison | efcb48d | 2016-08-12 03:05:32 | [diff] [blame] | 522 | explicit SimplePredictor(bool predictor_enabled) |
| 523 | : Predictor(predictor_enabled) {} |
dcheng | fce29ad | 2014-10-23 03:47:47 | [diff] [blame] | 524 | ~SimplePredictor() override {} |
| 525 | void InitNetworkPredictor(PrefService* user_prefs, |
dcheng | fce29ad | 2014-10-23 03:47:47 | [diff] [blame] | 526 | IOThread* io_thread, |
| 527 | net::URLRequestContextGetter* getter, |
| 528 | ProfileIOData* profile_io_data) override; |
| 529 | void ShutdownOnUIThread() override; |
| 530 | |
[email protected] | 8a9712c2 | 2014-07-21 22:47:28 | [diff] [blame] | 531 | private: |
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 532 | // These member functions return True for unittests. |
dcheng | fce29ad | 2014-10-23 03:47:47 | [diff] [blame] | 533 | bool CanPrefetchAndPrerender() const override; |
| 534 | bool CanPreresolveAndPreconnect() const override; |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 535 | }; |
| 536 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 537 | } // namespace chrome_browser_net |
| 538 | |
[email protected] | 3530cd9 | 2010-06-27 06:22:01 | [diff] [blame] | 539 | #endif // CHROME_BROWSER_NET_PREDICTOR_H_ |