| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 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, | |
| 16 // cause this module to speculatively create a TCP/IP connection that will | |
| 17 // probably be needed to fetch the subresource. | |
| 18 | |
| 19 #ifndef CHROME_BROWSER_NET_DNS_MASTER_H_ | |
| 20 #define CHROME_BROWSER_NET_DNS_MASTER_H_ | |
| 21 | |
| 22 #include <map> | |
| 23 #include <queue> | |
| 24 #include <set> | |
| 25 #include <string> | |
| 26 #include <vector> | |
| 27 | |
| 28 #include "base/gtest_prod_util.h" | |
| 29 #include "base/ref_counted.h" | |
| 30 #include "chrome/browser/net/dns_host_info.h" | |
| 31 #include "chrome/browser/net/referrer.h" | |
| 32 #include "chrome/common/net/dns.h" | |
| 33 #include "net/base/host_port_pair.h" | |
| 34 | |
| 35 namespace net { | |
| 36 class HostResolver; | |
| 37 } // namespace net | |
| 38 | |
| 39 namespace chrome_browser_net { | |
| 40 | |
| 41 typedef chrome_common_net::UrlList UrlList; | |
| 42 typedef chrome_common_net::NameList NameList; | |
| 43 typedef std::map<GURL, UrlInfo> Results; | |
| 44 | |
| 45 // Note that Predictor is not thread safe, and must only be called from | |
| 46 // the IO thread. Failure to do so will result in a DCHECK at runtime. | |
| 47 class Predictor : public base::RefCountedThreadSafe<Predictor> { | |
| 48 public: | |
| 49 // A version number for prefs that are saved. This should be incremented when | |
| 50 // we change the format so that we discard old data. | |
| 51 enum { DNS_REFERRER_VERSION = 1 }; | |
| 52 | |
| 53 // |max_concurrent| specifies how many concurrent (parallel) prefetches will | |
| 54 // be performed. Host lookups will be issued through |host_resolver|. | |
| 55 Predictor(net::HostResolver* host_resolver, | |
| 56 base::TimeDelta max_queue_delay_ms, size_t max_concurrent, | |
| 57 bool preconnect_enabled); | |
| 58 | |
| 59 // Cancel pending requests and prevent new ones from being made. | |
| 60 void Shutdown(); | |
| 61 | |
| 62 // In some circumstances, for privacy reasons, all results should be | |
| 63 // discarded. This method gracefully handles that activity. | |
| 64 // Destroy all our internal state, which shows what names we've looked up, and | |
| 65 // how long each has taken, etc. etc. We also destroy records of suggesses | |
| 66 // (cache hits etc.). | |
| 67 void DiscardAllResults(); | |
| 68 | |
| 69 // Add hostname(s) to the queue for processing. | |
| 70 void ResolveList(const UrlList& urls, | |
| 71 UrlInfo::ResolutionMotivation motivation); | |
| 72 void Resolve(const GURL& url, | |
| 73 UrlInfo::ResolutionMotivation motivation); | |
| 74 | |
| 75 // Get latency benefit of the prefetch that we are navigating to. | |
| 76 bool AccruePrefetchBenefits(const GURL& referrer, | |
| 77 UrlInfo* navigation_info); | |
| 78 | |
| 79 // Instigate preresolution of any domains we predict will be needed after this | |
| 80 // navigation. | |
| 81 void PredictSubresources(const GURL& url); | |
| 82 | |
| 83 // Instigate pre-connection to any URLs we predict will be needed after this | |
| 84 // navigation (typically more-embedded resources on a page). | |
| 85 void PredictFrameSubresources(const GURL& url); | |
| 86 | |
| 87 // Record details of a navigation so that we can preresolve the host name | |
| 88 // ahead of time the next time the users navigates to the indicated host. | |
| 89 void LearnFromNavigation(const GURL& referring_url, const GURL& target_url); | |
| 90 | |
| 91 // Dump HTML table containing list of referrers for about:dns. | |
| 92 void GetHtmlReferrerLists(std::string* output); | |
| 93 | |
| 94 // Dump the list of currently known referrer domains and related prefetchable | |
| 95 // domains. | |
| 96 void GetHtmlInfo(std::string* output); | |
| 97 | |
| 98 // Discard any referrer for which all the suggested host names are currently | |
| 99 // annotated with no user latency reduction. Also scale down (diminish) the | |
| 100 // total benefit of those that did help, so that their reported contribution | |
| 101 // wll go done by a factor of 2 each time we trim (moving the referrer closer | |
| 102 // to being discarded at a future Trim). | |
| 103 void TrimReferrers(); | |
| 104 | |
| 105 // Construct a ListValue object that contains all the data in the referrers_ | |
| 106 // so that it can be persisted in a pref. | |
| 107 void SerializeReferrers(ListValue* referral_list); | |
| 108 | |
| 109 // Process a ListValue that contains all the data from a previous reference | |
| 110 // list, as constructed by SerializeReferrers(), and add all the identified | |
| 111 // values into the current referrer list. | |
| 112 void DeserializeReferrers(const ListValue& referral_list); | |
| 113 | |
| 114 void DeserializeReferrersThenDelete(ListValue* referral_list) { | |
| 115 DeserializeReferrers(*referral_list); | |
| 116 delete referral_list; | |
| 117 } | |
| 118 | |
| 119 // For unit test code only. | |
| 120 size_t max_concurrent_dns_lookups() const { | |
| 121 return max_concurrent_dns_lookups_; | |
| 122 } | |
| 123 | |
| 124 // Flag setting to use preconnection instead of just DNS pre-fetching. | |
| 125 bool preconnect_enabled() const { return preconnect_enabled_; } | |
| 126 | |
| 127 private: | |
| 128 friend class base::RefCountedThreadSafe<Predictor>; | |
| 129 FRIEND_TEST_ALL_PREFIXES(PredictorTest, BenefitLookupTest); | |
| 130 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ShutdownWhenResolutionIsPendingTest); | |
| 131 FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTest); | |
| 132 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ConcurrentLookupTest); | |
| 133 FRIEND_TEST_ALL_PREFIXES(PredictorTest, MassiveConcurrentLookupTest); | |
| 134 FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueuePushPopTest); | |
| 135 FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueueReorderTest); | |
| 136 friend class WaitForResolutionHelper; // For testing. | |
| 137 | |
| 138 ~Predictor(); | |
| 139 | |
| 140 class LookupRequest; | |
| 141 | |
| 142 // A simple priority queue for handling host names. | |
| 143 // Some names that are queued up have |motivation| that requires very rapid | |
| 144 // handling. For example, a sub-resource name lookup MUST be done before the | |
| 145 // actual sub-resource is fetched. In contrast, a name that was speculatively | |
| 146 // noted in a page has to be resolved before the user "gets around to" | |
| 147 // clicking on a link. By tagging (with a motivation) each push we make into | |
| 148 // this FIFO queue, the queue can re-order the more important names to service | |
| 149 // them sooner (relative to some low priority background resolutions). | |
| 150 class HostNameQueue { | |
| 151 public: | |
| 152 HostNameQueue(); | |
| 153 ~HostNameQueue(); | |
| 154 void Push(const GURL& url, | |
| 155 UrlInfo::ResolutionMotivation motivation); | |
| 156 bool IsEmpty() const; | |
| 157 GURL Pop(); | |
| 158 | |
| 159 private: | |
| 160 // The names in the queue that should be serviced (popped) ASAP. | |
| 161 std::queue<GURL> rush_queue_; | |
| 162 // The names in the queue that should only be serviced when rush_queue is | |
| 163 // empty. | |
| 164 std::queue<GURL> background_queue_; | |
| 165 | |
| 166 DISALLOW_COPY_AND_ASSIGN(HostNameQueue); | |
| 167 }; | |
| 168 | |
| 169 // A map that is keyed with the host/port that we've learned were the cause | |
| 170 // of loading additional URLs. The list of additional targets is held | |
| 171 // in a Referrer instance, which is a value in this map. | |
| 172 typedef std::map<GURL, Referrer> Referrers; | |
| 173 | |
| 174 // Only for testing. Returns true if hostname has been successfully resolved | |
| 175 // (name found). | |
| 176 bool WasFound(const GURL& url) const { | |
| 177 Results::const_iterator it(results_.find(url)); | |
| 178 return (it != results_.end()) && | |
| 179 it->second.was_found(); | |
| 180 } | |
| 181 | |
| 182 // Only for testing. Return how long was the resolution | |
| 183 // or UrlInfo::kNullDuration if it hasn't been resolved yet. | |
| 184 base::TimeDelta GetResolutionDuration(const GURL& url) { | |
| 185 if (results_.find(url) == results_.end()) | |
| 186 return UrlInfo::kNullDuration; | |
| 187 return results_[url].resolve_duration(); | |
| 188 } | |
| 189 | |
| 190 // Only for testing; | |
| 191 size_t peak_pending_lookups() const { return peak_pending_lookups_; } | |
| 192 | |
| 193 // Access method for use by async lookup request to pass resolution result. | |
| 194 void OnLookupFinished(LookupRequest* request, const GURL& url, bool found); | |
| 195 | |
| 196 // Underlying method for both async and synchronous lookup to update state. | |
| 197 void LookupFinished(LookupRequest* request, | |
| 198 const GURL& url, bool found); | |
| 199 | |
| 200 // Queue hostname for resolution. If queueing was done, return the pointer | |
| 201 // to the queued instance, otherwise return NULL. | |
| 202 UrlInfo* AppendToResolutionQueue(const GURL& url, | |
| 203 UrlInfo::ResolutionMotivation motivation); | |
| 204 | |
| 205 // Check to see if too much queuing delay has been noted for the given info, | |
| 206 // which indicates that there is "congestion" or growing delay in handling the | |
| 207 // resolution of names. Rather than letting this congestion potentially grow | |
| 208 // without bounds, we abandon our queued efforts at pre-resolutions in such a | |
| 209 // case. | |
| 210 // To do this, we will recycle |info|, as well as all queued items, back to | |
| 211 // the state they had before they were queued up. We can't do anything about | |
| 212 // the resolutions we've already sent off for processing on another thread, so | |
| 213 // we just let them complete. On a slow system, subject to congestion, this | |
| 214 // will greatly reduce the number of resolutions done, but it will assure that | |
| 215 // any resolutions that are done, are in a timely and hence potentially | |
| 216 // helpful manner. | |
| 217 bool CongestionControlPerformed(UrlInfo* info); | |
| 218 | |
| 219 // Take lookup requests from work_queue_ and tell HostResolver to look them up | |
| 220 // asynchronously, provided we don't exceed concurrent resolution limit. | |
| 221 void StartSomeQueuedResolutions(); | |
| 222 | |
| 223 // work_queue_ holds a list of names we need to look up. | |
| 224 HostNameQueue work_queue_; | |
| 225 | |
| 226 // results_ contains information for existing/prior prefetches. | |
| 227 Results results_; | |
| 228 | |
| 229 // For each URL that we might navigate to (that we've "learned about") | |
| 230 // we have a Referrer list. Each Referrer list has all hostnames we need to | |
| 231 // pre-resolve when there is a navigation to the orginial hostname. | |
| 232 Referrers referrers_; | |
| 233 | |
| 234 std::set<LookupRequest*> pending_lookups_; | |
| 235 | |
| 236 // For testing, to verify that we don't exceed the limit. | |
| 237 size_t peak_pending_lookups_; | |
| 238 | |
| 239 // When true, we don't make new lookup requests. | |
| 240 bool shutdown_; | |
| 241 | |
| 242 // A list of successful events resulting from pre-fetching. | |
| 243 UrlInfo::DnsInfoTable dns_cache_hits_; | |
| 244 // A map of hosts that were evicted from our cache (after we prefetched them) | |
| 245 // and before the HTTP stack tried to look them up. | |
| 246 Results cache_eviction_map_; | |
| 247 | |
| 248 // The number of concurrent lookups currently allowed. | |
| 249 const size_t max_concurrent_dns_lookups_; | |
| 250 | |
| 251 // The maximum queueing delay that is acceptable before we enter congestion | |
| 252 // reduction mode, and discard all queued (but not yet assigned) resolutions. | |
| 253 const base::TimeDelta max_dns_queue_delay_; | |
| 254 | |
| 255 // The host resovler we warm DNS entries for. | |
| 256 scoped_refptr<net::HostResolver> host_resolver_; | |
| 257 | |
| 258 // Are we currently using preconnection, rather than just DNS resolution, for | |
| 259 // subresources and omni-box search URLs. | |
| 260 bool preconnect_enabled_; | |
| 261 | |
| 262 DISALLOW_COPY_AND_ASSIGN(Predictor); | |
| 263 }; | |
| 264 | |
| 265 } // namespace chrome_browser_net | |
| 266 | |
| 267 #endif // CHROME_BROWSER_NET_DNS_MASTER_H_ | |
| OLD | NEW |