[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 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_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 21 | #pragma once |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | |
| 23 | #include <map> |
| 24 | #include <queue> |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 25 | #include <set> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | #include <string> |
[email protected] | 579f2a1 | 2011-04-06 16:27:31 | [diff] [blame] | 27 | #include <vector> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | |
[email protected] | a918f87 | 2010-06-01 14:30:51 | [diff] [blame] | 29 | #include "base/gtest_prod_util.h" |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 30 | #include "base/memory/scoped_ptr.h" |
[email protected] | 3530cd9 | 2010-06-27 06:22:01 | [diff] [blame] | 31 | #include "chrome/browser/net/url_info.h" |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 32 | #include "chrome/browser/net/referrer.h" |
[email protected] | 3530cd9 | 2010-06-27 06:22:01 | [diff] [blame] | 33 | #include "chrome/common/net/predictor_common.h" |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 34 | #include "net/base/host_port_pair.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 36 | namespace base { |
[email protected] | c02c853d7 | 2010-08-07 06:23:24 | [diff] [blame] | 37 | class ListValue; |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 38 | } |
[email protected] | c02c853d7 | 2010-08-07 06:23:24 | [diff] [blame] | 39 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 40 | namespace base { |
| 41 | class WaitableEvent; |
| 42 | } |
| 43 | |
[email protected] | fd2f8afe | 2009-06-11 21:53:55 | [diff] [blame] | 44 | namespace net { |
| 45 | class HostResolver; |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 46 | } // namespace net |
[email protected] | fd2f8afe | 2009-06-11 21:53:55 | [diff] [blame] | 47 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 48 | class IOThread; |
| 49 | class PrefService; |
| 50 | class Profile; |
| 51 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | namespace chrome_browser_net { |
| 53 | |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 54 | typedef chrome_common_net::UrlList UrlList; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 55 | typedef chrome_common_net::NameList NameList; |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 56 | typedef std::map<GURL, UrlInfo> Results; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 58 | // Predictor is constructed during Profile construction (on the UI thread), |
| 59 | // but it is destroyed on the IO thread when ProfileIOData goes away. All of |
| 60 | // its core state and functionality happens on the IO thread. The only UI |
| 61 | // methods are initialization / shutdown related (including preconnect |
| 62 | // initialization), or convenience methods that internally forward calls to |
| 63 | // the IO thread. |
| 64 | class Predictor { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | public: |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 66 | // A version number for prefs that are saved. This should be incremented when |
| 67 | // we change the format so that we discard old data. |
[email protected] | d643172 | 2011-09-01 00:46:33 | [diff] [blame] | 68 | static const int kPredictorReferrerVersion; |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 69 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 70 | // Given that the underlying Chromium resolver defaults to a total maximum of |
| 71 | // 8 paralell resolutions, we will avoid any chance of starving navigational |
| 72 | // resolutions by limiting the number of paralell speculative resolutions. |
| 73 | // This is used in the field trials and testing. |
| 74 | // TODO(jar): Move this limitation into the resolver. |
| 75 | static const size_t kMaxSpeculativeParallelResolves; |
| 76 | |
| 77 | // To control the congestion avoidance system, we need an estimate of how |
| 78 | // many speculative requests may arrive at once. Since we currently only |
| 79 | // keep 8 subresource names for each frame, we'll use that as our basis. |
| 80 | // Note that when scanning search results lists, we might actually get 10 at |
| 81 | // a time, and wikipedia can often supply (during a page scan) upwards of 50. |
| 82 | // In those odd cases, we may discard some of the later speculative requests |
| 83 | // mistakenly assuming that the resolutions took too long. |
| 84 | static const int kTypicalSpeculativeGroupSize; |
| 85 | |
| 86 | // The next constant specifies an amount of queueing delay that is |
| 87 | // "too large," and indicative of problems with resolutions (perhaps due to |
| 88 | // an overloaded router, or such). When we exceed this delay, congestion |
| 89 | // avoidance will kick in and all speculations in the queue will be discarded. |
| 90 | static const int kMaxSpeculativeResolveQueueDelayMs; |
| 91 | |
[email protected] | f4ef861ba | 2010-07-28 22:37:23 | [diff] [blame] | 92 | // |max_concurrent| specifies how many concurrent (parallel) prefetches will |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 93 | // be performed. Host lookups will be issued through |host_resolver|. |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 94 | explicit Predictor(bool preconnect_enabled); |
| 95 | |
| 96 | virtual ~Predictor(); |
| 97 | |
| 98 | // This function is used to create a predictor. For testing, we can create |
| 99 | // a version which does a simpler shutdown. |
| 100 | static Predictor* CreatePredictor(bool preconnect_enabled, |
| 101 | bool simple_shutdown); |
| 102 | |
| 103 | static void RegisterUserPrefs(PrefService* user_prefs); |
| 104 | |
| 105 | // ------------- Start UI thread methods. |
| 106 | |
| 107 | virtual void InitNetworkPredictor(PrefService* user_prefs, |
| 108 | PrefService* local_state, |
[email protected] | 36b9eee | 2011-10-20 02:44:08 | [diff] [blame] | 109 | IOThread* io_thread); |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 110 | |
| 111 | // The Omnibox has proposed a given url to the user, and if it is a search |
| 112 | // URL, then it also indicates that this is preconnectable (i.e., we could |
| 113 | // preconnect to the search server). |
| 114 | void AnticipateOmniboxUrl(const GURL& url, bool preconnectable); |
| 115 | |
| 116 | // Preconnect a URL and all of its subresource domains. |
| 117 | void PreconnectUrlAndSubresources(const GURL& url); |
| 118 | |
| 119 | static UrlList GetPredictedUrlListAtStartup(PrefService* user_prefs, |
| 120 | PrefService* local_state); |
| 121 | |
| 122 | static void set_max_queueing_delay(int max_queueing_delay_ms); |
| 123 | |
| 124 | static void set_max_parallel_resolves(size_t max_parallel_resolves); |
| 125 | |
| 126 | virtual void ShutdownOnUIThread(PrefService* user_prefs); |
| 127 | |
| 128 | // ------------- End UI thread methods. |
| 129 | |
| 130 | // ------------- Start IO thread methods. |
[email protected] | b2b8b83 | 2009-02-06 19:03:29 | [diff] [blame] | 131 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 132 | // Cancel pending requests and prevent new ones from being made. |
| 133 | void Shutdown(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | |
| 135 | // In some circumstances, for privacy reasons, all results should be |
| 136 | // discarded. This method gracefully handles that activity. |
| 137 | // Destroy all our internal state, which shows what names we've looked up, and |
| 138 | // how long each has taken, etc. etc. We also destroy records of suggesses |
| 139 | // (cache hits etc.). |
| 140 | void DiscardAllResults(); |
| 141 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 142 | // Add hostname(s) to the queue for processing. |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 143 | void ResolveList(const UrlList& urls, |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 144 | UrlInfo::ResolutionMotivation motivation); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 145 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 146 | void Resolve(const GURL& url, UrlInfo::ResolutionMotivation motivation); |
[email protected] | e326922d | 2010-09-03 09:08:10 | [diff] [blame] | 147 | |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 148 | // Record details of a navigation so that we can preresolve the host name |
| 149 | // ahead of time the next time the users navigates to the indicated host. |
[email protected] | d6bb256 | 2010-08-25 23:31:30 | [diff] [blame] | 150 | // Should only be called when urls are distinct, and they should already be |
| 151 | // canonicalized to not have a path. |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 152 | void LearnFromNavigation(const GURL& referring_url, const GURL& target_url); |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 153 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 154 | // When displaying info in about:dns, the following API is called. |
| 155 | static void PredictorGetHtmlInfo(Predictor* predictor, std::string* output); |
| 156 | |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 157 | // Dump HTML table containing list of referrers for about:dns. |
| 158 | void GetHtmlReferrerLists(std::string* output); |
| 159 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 160 | // Dump the list of currently known referrer domains and related prefetchable |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 161 | // domains for about:dns. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | void GetHtmlInfo(std::string* output); |
| 163 | |
[email protected] | 579f2a1 | 2011-04-06 16:27:31 | [diff] [blame] | 164 | // Discards any referrer for which all the suggested host names are currently |
| 165 | // annotated with negligible expected-use. Scales down (diminishes) the |
| 166 | // expected-use of those that remain, so that their use will go down by a |
| 167 | // factor each time we trim (moving the referrer closer to being discarded in |
| 168 | // a future call). |
| 169 | // The task is performed synchronously and completes before returing. |
| 170 | void TrimReferrersNow(); |
[email protected] | 03c5e86 | 2009-02-17 22:50:14 | [diff] [blame] | 171 | |
| 172 | // Construct a ListValue object that contains all the data in the referrers_ |
| 173 | // so that it can be persisted in a pref. |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 174 | void SerializeReferrers(base::ListValue* referral_list); |
[email protected] | 03c5e86 | 2009-02-17 22:50:14 | [diff] [blame] | 175 | |
| 176 | // Process a ListValue that contains all the data from a previous reference |
| 177 | // list, as constructed by SerializeReferrers(), and add all the identified |
| 178 | // values into the current referrer list. |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 179 | void DeserializeReferrers(const base::ListValue& referral_list); |
[email protected] | 03c5e86 | 2009-02-17 22:50:14 | [diff] [blame] | 180 | |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 181 | void DeserializeReferrersThenDelete(base::ListValue* referral_list); |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 182 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 183 | void DiscardInitialNavigationHistory(); |
[email protected] | e695fbd6 | 2009-06-30 16:31:54 | [diff] [blame] | 184 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 185 | void FinalizeInitializationOnIOThread( |
| 186 | const std::vector<GURL>& urls_to_prefetch, |
| 187 | base::ListValue* referral_list, |
| 188 | IOThread* io_thread, |
| 189 | bool predictor_enabled); |
| 190 | |
| 191 | // During startup, we learn what the first N urls visited are, and then |
| 192 | // resolve the associated hosts ASAP during our next startup. |
| 193 | void LearnAboutInitialNavigation(const GURL& url); |
| 194 | |
| 195 | // Renderer bundles up list and sends to this browser API via IPC. |
| 196 | // TODO(jar): Use UrlList instead to include port and scheme. |
| 197 | void DnsPrefetchList(const NameList& hostnames); |
| 198 | |
| 199 | // May be called from either the IO or UI thread and will PostTask |
| 200 | // to the IO thread if necessary. |
| 201 | void DnsPrefetchMotivatedList(const UrlList& urls, |
| 202 | UrlInfo::ResolutionMotivation motivation); |
| 203 | |
| 204 | // May be called from either the IO or UI thread and will PostTask |
| 205 | // to the IO thread if necessary. |
| 206 | void SaveStateForNextStartupAndTrim(PrefService* prefs); |
| 207 | |
| 208 | void SaveDnsPrefetchStateForNextStartupAndTrim( |
| 209 | base::ListValue* startup_list, |
| 210 | base::ListValue* referral_list, |
| 211 | base::WaitableEvent* completion); |
| 212 | |
| 213 | // May be called from either the IO or UI thread and will PostTask |
| 214 | // to the IO thread if necessary. |
| 215 | void EnablePredictor(bool enable); |
| 216 | |
| 217 | void EnablePredictorOnIOThread(bool enable); |
| 218 | |
| 219 | // ------------- End IO thread methods. |
| 220 | |
| 221 | // The following methods may be called on either the IO or UI threads. |
| 222 | |
| 223 | // Instigate pre-connection to any URLs, or pre-resolution of related host, |
| 224 | // that we predict will be needed after this navigation (typically |
| 225 | // more-embedded resources on a page). This method will actually post a task |
| 226 | // to do the actual work, so as not to jump ahead of the frame navigation that |
| 227 | // instigated this activity. |
| 228 | void PredictFrameSubresources(const GURL& url); |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 229 | |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 230 | // Put URL in canonical form, including a scheme, host, and port. |
| 231 | // Returns GURL::EmptyGURL() if the scheme is not http/https or if the url |
| 232 | // cannot be otherwise canonicalized. |
| 233 | static GURL CanonicalizeUrl(const GURL& url); |
| 234 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 235 | // Used for testing. |
| 236 | void SetHostResolver(net::HostResolver* host_resolver) { |
| 237 | host_resolver_ = host_resolver; |
| 238 | } |
| 239 | // Used for testing. |
| 240 | size_t max_concurrent_dns_lookups() const { |
| 241 | return max_concurrent_dns_lookups_; |
| 242 | } |
| 243 | // Used for testing. |
| 244 | void SetShutdown(bool shutdown) { |
| 245 | shutdown_ = shutdown; |
| 246 | } |
| 247 | |
| 248 | // Flag setting to use preconnection instead of just DNS pre-fetching. |
| 249 | bool preconnect_enabled() const { |
| 250 | return preconnect_enabled_; |
| 251 | } |
| 252 | |
| 253 | // Flag setting for whether we are prefetching dns lookups. |
| 254 | bool predictor_enabled() const { |
| 255 | return predictor_enabled_; |
| 256 | } |
| 257 | |
| 258 | |
[email protected] | b2b8b83 | 2009-02-06 19:03:29 | [diff] [blame] | 259 | private: |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 260 | FRIEND_TEST_ALL_PREFIXES(PredictorTest, BenefitLookupTest); |
| 261 | FRIEND_TEST_ALL_PREFIXES(PredictorTest, ShutdownWhenResolutionIsPendingTest); |
| 262 | FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTest); |
| 263 | FRIEND_TEST_ALL_PREFIXES(PredictorTest, ConcurrentLookupTest); |
| 264 | FRIEND_TEST_ALL_PREFIXES(PredictorTest, MassiveConcurrentLookupTest); |
| 265 | FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueuePushPopTest); |
| 266 | FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueueReorderTest); |
[email protected] | 579f2a1 | 2011-04-06 16:27:31 | [diff] [blame] | 267 | FRIEND_TEST_ALL_PREFIXES(PredictorTest, ReferrerSerializationTrimTest); |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 268 | friend class WaitForResolutionHelper; // For testing. |
| 269 | |
| 270 | class LookupRequest; |
| 271 | |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 272 | // A simple priority queue for handling host names. |
| 273 | // Some names that are queued up have |motivation| that requires very rapid |
| 274 | // handling. For example, a sub-resource name lookup MUST be done before the |
| 275 | // actual sub-resource is fetched. In contrast, a name that was speculatively |
| 276 | // noted in a page has to be resolved before the user "gets around to" |
| 277 | // clicking on a link. By tagging (with a motivation) each push we make into |
| 278 | // this FIFO queue, the queue can re-order the more important names to service |
| 279 | // them sooner (relative to some low priority background resolutions). |
| 280 | class HostNameQueue { |
| 281 | public: |
| 282 | HostNameQueue(); |
| 283 | ~HostNameQueue(); |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 284 | void Push(const GURL& url, |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 285 | UrlInfo::ResolutionMotivation motivation); |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 286 | bool IsEmpty() const; |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 287 | GURL Pop(); |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 288 | |
[email protected] | 579f2a1 | 2011-04-06 16:27:31 | [diff] [blame] | 289 | private: |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 290 | // The names in the queue that should be serviced (popped) ASAP. |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 291 | std::queue<GURL> rush_queue_; |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 292 | // The names in the queue that should only be serviced when rush_queue is |
| 293 | // empty. |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 294 | std::queue<GURL> background_queue_; |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 295 | |
| 296 | DISALLOW_COPY_AND_ASSIGN(HostNameQueue); |
| 297 | }; |
| 298 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 299 | // The InitialObserver monitors navigations made by the network stack. This |
| 300 | // is only used to identify startup time resolutions (for re-resolution |
| 301 | // during our next process startup). |
| 302 | // TODO(jar): Consider preconnecting at startup, which may be faster than |
| 303 | // waiting for render process to start and request a connection. |
| 304 | class InitialObserver { |
| 305 | public: |
| 306 | InitialObserver(); |
| 307 | ~InitialObserver(); |
| 308 | // Recording of when we observed each navigation. |
| 309 | typedef std::map<GURL, base::TimeTicks> FirstNavigations; |
| 310 | |
| 311 | // Potentially add a new URL to our startup list. |
| 312 | void Append(const GURL& url, Predictor* predictor); |
| 313 | |
| 314 | // Get an HTML version of our current planned first_navigations_. |
| 315 | void GetFirstResolutionsHtml(std::string* output); |
| 316 | |
| 317 | // Persist the current first_navigations_ for storage in a list. |
| 318 | void GetInitialDnsResolutionList(base::ListValue* startup_list); |
| 319 | |
| 320 | // Discards all initial loading history. |
| 321 | void DiscardInitialNavigationHistory() { first_navigations_.clear(); } |
| 322 | |
| 323 | private: |
| 324 | // List of the first N URL resolutions observed in this run. |
| 325 | FirstNavigations first_navigations_; |
| 326 | |
| 327 | // The number of URLs we'll save for pre-resolving at next startup. |
| 328 | static const size_t kStartupResolutionCount = 10; |
| 329 | }; |
| 330 | |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 331 | // A map that is keyed with the host/port that we've learned were the cause |
| 332 | // of loading additional URLs. The list of additional targets is held |
| 333 | // in a Referrer instance, which is a value in this map. |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 334 | typedef std::map<GURL, Referrer> Referrers; |
[email protected] | 7c19b87b0 | 2009-01-26 16:19:44 | [diff] [blame] | 335 | |
[email protected] | 579f2a1 | 2011-04-06 16:27:31 | [diff] [blame] | 336 | // Depending on the expected_subresource_use_, we may either make a TCP/IP |
| 337 | // preconnection, or merely pre-resolve the hostname via DNS (or even do |
| 338 | // nothing). The following are the threasholds for taking those actions. |
| 339 | static const double kPreconnectWorthyExpectedValue; |
| 340 | static const double kDNSPreresolutionWorthyExpectedValue; |
| 341 | // Referred hosts with a subresource_use_rate_ that are less than the |
| 342 | // following threshold will be discarded when we Trim() the list. |
| 343 | static const double kDiscardableExpectedValue; |
| 344 | // During trimming operation to discard hosts for which we don't have likely |
| 345 | // subresources, we multiply the expected_subresource_use_ value by the |
| 346 | // following ratio until that value is less than kDiscardableExpectedValue. |
| 347 | // This number should always be less than 1, an more than 0. |
| 348 | static const double kReferrerTrimRatio; |
| 349 | |
| 350 | // Interval between periodic trimming of our whole referrer list. |
| 351 | // We only do a major trimming about once an hour, and then only when the user |
| 352 | // is actively browsing. |
| 353 | static const base::TimeDelta kDurationBetweenTrimmings; |
| 354 | // Interval between incremental trimmings (to avoid inducing Jank). |
| 355 | static const base::TimeDelta kDurationBetweenTrimmingIncrements; |
| 356 | // Number of referring URLs processed in an incremental trimming. |
| 357 | static const size_t kUrlsTrimmedPerIncrement; |
| 358 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 359 | // Only for testing. Returns true if hostname has been successfully resolved |
| 360 | // (name found). |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 361 | bool WasFound(const GURL& url) const { |
| 362 | Results::const_iterator it(results_.find(url)); |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 363 | return (it != results_.end()) && |
| 364 | it->second.was_found(); |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | // Only for testing. Return how long was the resolution |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 368 | // or UrlInfo::kNullDuration if it hasn't been resolved yet. |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 369 | base::TimeDelta GetResolutionDuration(const GURL& url) { |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 370 | if (results_.find(url) == results_.end()) |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 371 | return UrlInfo::kNullDuration; |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 372 | return results_[url].resolve_duration(); |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | // Only for testing; |
| 376 | size_t peak_pending_lookups() const { return peak_pending_lookups_; } |
| 377 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 378 | // ------------- Start IO thread methods. |
| 379 | |
| 380 | // Perform actual resolution or preconnection to subresources now. This is |
| 381 | // an internal worker method that is reached via a post task from |
| 382 | // PredictFrameSubresources(). |
| 383 | void PrepareFrameSubresources(const GURL& url); |
| 384 | |
[email protected] | 8539853 | 2009-06-16 21:32:18 | [diff] [blame] | 385 | // Access method for use by async lookup request to pass resolution result. |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 386 | void OnLookupFinished(LookupRequest* request, const GURL& url, bool found); |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 387 | |
[email protected] | 8539853 | 2009-06-16 21:32:18 | [diff] [blame] | 388 | // Underlying method for both async and synchronous lookup to update state. |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 389 | void LookupFinished(LookupRequest* request, |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 390 | const GURL& url, bool found); |
[email protected] | 8539853 | 2009-06-16 21:32:18 | [diff] [blame] | 391 | |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 392 | // Queue hostname for resolution. If queueing was done, return the pointer |
| 393 | // to the queued instance, otherwise return NULL. |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 394 | UrlInfo* AppendToResolutionQueue(const GURL& url, |
| 395 | UrlInfo::ResolutionMotivation motivation); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 396 | |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 397 | // Check to see if too much queuing delay has been noted for the given info, |
| 398 | // which indicates that there is "congestion" or growing delay in handling the |
| 399 | // resolution of names. Rather than letting this congestion potentially grow |
| 400 | // without bounds, we abandon our queued efforts at pre-resolutions in such a |
| 401 | // case. |
| 402 | // To do this, we will recycle |info|, as well as all queued items, back to |
| 403 | // the state they had before they were queued up. We can't do anything about |
| 404 | // the resolutions we've already sent off for processing on another thread, so |
| 405 | // we just let them complete. On a slow system, subject to congestion, this |
| 406 | // will greatly reduce the number of resolutions done, but it will assure that |
| 407 | // any resolutions that are done, are in a timely and hence potentially |
| 408 | // helpful manner. |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 409 | bool CongestionControlPerformed(UrlInfo* info); |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 410 | |
| 411 | // Take lookup requests from work_queue_ and tell HostResolver to look them up |
| 412 | // asynchronously, provided we don't exceed concurrent resolution limit. |
[email protected] | ec86bea | 2009-12-08 18:35:14 | [diff] [blame] | 413 | void StartSomeQueuedResolutions(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 414 | |
[email protected] | 579f2a1 | 2011-04-06 16:27:31 | [diff] [blame] | 415 | // Performs trimming similar to TrimReferrersNow(), except it does it as a |
| 416 | // series of short tasks by posting continuations again an again until done. |
| 417 | void TrimReferrers(); |
| 418 | |
| 419 | // Loads urls_being_trimmed_ from keys of current referrers_. |
| 420 | void LoadUrlsForTrimming(); |
| 421 | |
| 422 | // Posts a task to do additional incremental trimming of referrers_. |
| 423 | void PostIncrementalTrimTask(); |
| 424 | |
| 425 | // Calls Trim() on some or all of urls_being_trimmed_. |
| 426 | // If it does not process all the URLs in that vector, it posts a task to |
| 427 | // continue with them shortly (i.e., it yeilds and continues). |
| 428 | void IncrementalTrimReferrers(bool trim_all_now); |
| 429 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 430 | // ------------- End IO thread methods. |
| 431 | |
| 432 | scoped_ptr<InitialObserver> initial_observer_; |
| 433 | |
| 434 | // Status of speculative DNS resolution and speculative TCP/IP connection |
| 435 | // feature. |
| 436 | bool predictor_enabled_; |
| 437 | |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 438 | // work_queue_ holds a list of names we need to look up. |
| 439 | HostNameQueue work_queue_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 440 | |
[email protected] | 21dae9b | 2008-11-06 23:32:53 | [diff] [blame] | 441 | // results_ contains information for existing/prior prefetches. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 442 | Results results_; |
| 443 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 444 | std::set<LookupRequest*> pending_lookups_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 445 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 446 | // For testing, to verify that we don't exceed the limit. |
| 447 | size_t peak_pending_lookups_; |
[email protected] | b2b8b83 | 2009-02-06 19:03:29 | [diff] [blame] | 448 | |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 449 | // When true, we don't make new lookup requests. |
[email protected] | b2b8b83 | 2009-02-06 19:03:29 | [diff] [blame] | 450 | bool shutdown_; |
| 451 | |
[email protected] | 755a9335 | 2010-10-29 06:33:59 | [diff] [blame] | 452 | // The number of concurrent speculative lookups currently allowed to be sent |
| 453 | // to the resolver. Any additional lookups will be queued to avoid exceeding |
| 454 | // this value. The queue is a priority queue that will accelerate |
| 455 | // sub-resource speculation, and retard resolutions suggested by page scans. |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 456 | const size_t max_concurrent_dns_lookups_; |
[email protected] | e085c30 | 2009-06-01 18:31:36 | [diff] [blame] | 457 | |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 458 | // The maximum queueing delay that is acceptable before we enter congestion |
| 459 | // reduction mode, and discard all queued (but not yet assigned) resolutions. |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 460 | const base::TimeDelta max_dns_queue_delay_; |
[email protected] | 602faf3c | 2009-06-27 14:35:44 | [diff] [blame] | 461 | |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 462 | // The host resolver we warm DNS entries for. |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 463 | net::HostResolver* host_resolver_; |
[email protected] | fd2f8afe | 2009-06-11 21:53:55 | [diff] [blame] | 464 | |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 465 | // Are we currently using preconnection, rather than just DNS resolution, for |
| 466 | // subresources and omni-box search URLs. |
| 467 | bool preconnect_enabled_; |
| 468 | |
[email protected] | 1455ccf1 | 2010-08-18 16:32:14 | [diff] [blame] | 469 | // Most recent suggestion from Omnibox provided via AnticipateOmniboxUrl(). |
| 470 | std::string last_omnibox_host_; |
| 471 | |
| 472 | // The time when the last preresolve was done for last_omnibox_host_. |
| 473 | base::TimeTicks last_omnibox_preresolve_; |
| 474 | |
| 475 | // The number of consecutive requests to AnticipateOmniboxUrl() that suggested |
| 476 | // preconnecting (because it was to a search service). |
| 477 | int consecutive_omnibox_preconnect_count_; |
| 478 | |
| 479 | // The time when the last preconnection was requested to a search service. |
| 480 | base::TimeTicks last_omnibox_preconnect_; |
| 481 | |
[email protected] | 579f2a1 | 2011-04-06 16:27:31 | [diff] [blame] | 482 | // For each URL that we might navigate to (that we've "learned about") |
| 483 | // we have a Referrer list. Each Referrer list has all hostnames we might |
| 484 | // need to pre-resolve or pre-connect to when there is a navigation to the |
| 485 | // orginial hostname. |
| 486 | Referrers referrers_; |
| 487 | |
| 488 | // List of URLs in referrers_ currently being trimmed (scaled down to |
| 489 | // eventually be aged out of use). |
| 490 | std::vector<GURL> urls_being_trimmed_; |
| 491 | |
| 492 | // A time after which we need to do more trimming of referrers. |
| 493 | base::TimeTicks next_trim_time_; |
| 494 | |
[email protected] | 12465ec | 2011-11-17 21:18:23 | [diff] [blame^] | 495 | scoped_ptr<base::WeakPtrFactory<Predictor> > weak_factory_; |
[email protected] | 579f2a1 | 2011-04-06 16:27:31 | [diff] [blame] | 496 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 497 | DISALLOW_COPY_AND_ASSIGN(Predictor); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 498 | }; |
| 499 | |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 500 | // This version of the predictor is used for testing. |
| 501 | class SimplePredictor : public Predictor { |
| 502 | public: |
| 503 | explicit SimplePredictor(bool preconnect_enabled) |
| 504 | : Predictor(preconnect_enabled) {} |
| 505 | virtual ~SimplePredictor() {} |
| 506 | virtual void InitNetworkPredictor(PrefService* user_prefs, |
| 507 | PrefService* local_state, |
[email protected] | 36b9eee | 2011-10-20 02:44:08 | [diff] [blame] | 508 | IOThread* io_thread); |
[email protected] | 67372ecf | 2011-09-10 01:30:46 | [diff] [blame] | 509 | virtual void ShutdownOnUIThread(PrefService* user_prefs); |
| 510 | }; |
| 511 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 512 | } // namespace chrome_browser_net |
| 513 | |
[email protected] | 3530cd9 | 2010-06-27 06:22:01 | [diff] [blame] | 514 | #endif // CHROME_BROWSER_NET_PREDICTOR_H_ |