Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/net/predictor.h" | 5 #include "chrome/browser/net/predictor.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <sstream> | 10 #include <sstream> |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 const int Predictor::kMaxUnusedSocketLifetimeSecondsWithoutAGet = 10; | 80 const int Predictor::kMaxUnusedSocketLifetimeSecondsWithoutAGet = 10; |
| 81 // To control our congestion avoidance system, which discards a queue when | 81 // To control our congestion avoidance system, which discards a queue when |
| 82 // resolutions are "taking too long," we need an expected resolution time. | 82 // resolutions are "taking too long," we need an expected resolution time. |
| 83 // Common average is in the range of 300-500ms. | 83 // Common average is in the range of 300-500ms. |
| 84 const int kExpectedResolutionTimeMs = 500; | 84 const int kExpectedResolutionTimeMs = 500; |
| 85 const int Predictor::kTypicalSpeculativeGroupSize = 8; | 85 const int Predictor::kTypicalSpeculativeGroupSize = 8; |
| 86 const int Predictor::kMaxSpeculativeResolveQueueDelayMs = | 86 const int Predictor::kMaxSpeculativeResolveQueueDelayMs = |
| 87 (kExpectedResolutionTimeMs * Predictor::kTypicalSpeculativeGroupSize) / | 87 (kExpectedResolutionTimeMs * Predictor::kTypicalSpeculativeGroupSize) / |
| 88 Predictor::kMaxSpeculativeParallelResolves; | 88 Predictor::kMaxSpeculativeParallelResolves; |
| 89 | 89 |
| 90 // The default value of the credentials flag when preconnecting. | |
| 91 static bool kAllowCredentialsOnPreconnect = true; | |
|
mmenke
2015/07/09 15:55:56
This is a confusing name. "kAllowCredentialsOnPre
Yoav Weiss
2015/07/09 16:19:39
renamed
| |
| 92 | |
| 90 static int g_max_queueing_delay_ms = | 93 static int g_max_queueing_delay_ms = |
| 91 Predictor::kMaxSpeculativeResolveQueueDelayMs; | 94 Predictor::kMaxSpeculativeResolveQueueDelayMs; |
| 92 static size_t g_max_parallel_resolves = | 95 static size_t g_max_parallel_resolves = |
| 93 Predictor::kMaxSpeculativeParallelResolves; | 96 Predictor::kMaxSpeculativeParallelResolves; |
| 94 | 97 |
| 95 // A version number for prefs that are saved. This should be incremented when | 98 // A version number for prefs that are saved. This should be incremented when |
| 96 // we change the format so that we discard old data. | 99 // we change the format so that we discard old data. |
| 97 static const int kPredictorStartupFormatVersion = 1; | 100 static const int kPredictorStartupFormatVersion = 1; |
| 98 | 101 |
| 99 class Predictor::LookupRequest { | 102 class Predictor::LookupRequest { |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 258 // pool. Currently, we just do a connect, which MAY be reset if we | 261 // pool. Currently, we just do a connect, which MAY be reset if we |
| 259 // don't use it in 10 secondes!!! As a result, we may do more | 262 // don't use it in 10 secondes!!! As a result, we may do more |
| 260 // connections, and actually cost the server more than if we did a real | 263 // connections, and actually cost the server more than if we did a real |
| 261 // get with a fake request (/gen_204 might be the good path on Google). | 264 // get with a fake request (/gen_204 might be the good path on Google). |
| 262 const int kMaxSearchKeepaliveSeconds(10); | 265 const int kMaxSearchKeepaliveSeconds(10); |
| 263 if ((now - last_omnibox_preconnect_).InSeconds() < | 266 if ((now - last_omnibox_preconnect_).InSeconds() < |
| 264 kMaxSearchKeepaliveSeconds) | 267 kMaxSearchKeepaliveSeconds) |
| 265 return; // We've done a preconnect recently. | 268 return; // We've done a preconnect recently. |
| 266 last_omnibox_preconnect_ = now; | 269 last_omnibox_preconnect_ = now; |
| 267 const int kConnectionsNeeded = 1; | 270 const int kConnectionsNeeded = 1; |
| 268 PreconnectUrl( | 271 PreconnectUrl(CanonicalizeUrl(url), GURL(), motivation, |
| 269 CanonicalizeUrl(url), GURL(), motivation, kConnectionsNeeded); | 272 kConnectionsNeeded, kAllowCredentialsOnPreconnect); |
| 270 return; // Skip pre-resolution, since we'll open a connection. | 273 return; // Skip pre-resolution, since we'll open a connection. |
| 271 } | 274 } |
| 272 } else { | 275 } else { |
| 273 consecutive_omnibox_preconnect_count_ = 0; | 276 consecutive_omnibox_preconnect_count_ = 0; |
| 274 } | 277 } |
| 275 } | 278 } |
| 276 | 279 |
| 277 // Fall through and consider pre-resolution. | 280 // Fall through and consider pre-resolution. |
| 278 | 281 |
| 279 // Omnibox tends to call in pairs (just a few milliseconds apart), and we | 282 // Omnibox tends to call in pairs (just a few milliseconds apart), and we |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 298 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 301 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 299 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 302 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 300 if (!predictor_enabled_ || !preconnect_enabled_ || | 303 if (!predictor_enabled_ || !preconnect_enabled_ || |
| 301 !url.is_valid() || !url.has_host()) | 304 !url.is_valid() || !url.has_host()) |
| 302 return; | 305 return; |
| 303 if (!CanPreresolveAndPreconnect()) | 306 if (!CanPreresolveAndPreconnect()) |
| 304 return; | 307 return; |
| 305 | 308 |
| 306 UrlInfo::ResolutionMotivation motivation(UrlInfo::EARLY_LOAD_MOTIVATED); | 309 UrlInfo::ResolutionMotivation motivation(UrlInfo::EARLY_LOAD_MOTIVATED); |
| 307 const int kConnectionsNeeded = 1; | 310 const int kConnectionsNeeded = 1; |
| 308 PreconnectUrl(CanonicalizeUrl(url), first_party_for_cookies, | 311 PreconnectUrl(CanonicalizeUrl(url), first_party_for_cookies, motivation, |
| 309 motivation, kConnectionsNeeded); | 312 kConnectionsNeeded, kAllowCredentialsOnPreconnect); |
| 310 PredictFrameSubresources(url.GetWithEmptyPath(), first_party_for_cookies); | 313 PredictFrameSubresources(url.GetWithEmptyPath(), first_party_for_cookies); |
| 311 } | 314 } |
| 312 | 315 |
| 313 UrlList Predictor::GetPredictedUrlListAtStartup( | 316 UrlList Predictor::GetPredictedUrlListAtStartup( |
| 314 PrefService* user_prefs, | 317 PrefService* user_prefs, |
| 315 PrefService* local_state) { | 318 PrefService* local_state) { |
| 316 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 319 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 317 UrlList urls; | 320 UrlList urls; |
| 318 // Recall list of URLs we learned about during last session. | 321 // Recall list of URLs we learned about during last session. |
| 319 // This may catch secondary hostnames, pulled in by the homepages. It will | 322 // This may catch secondary hostnames, pulled in by the homepages. It will |
| (...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 833 // a frequent occurrence on Android. | 836 // a frequent occurrence on Android. |
| 834 TrimReferrersNow(); | 837 TrimReferrersNow(); |
| 835 SerializeReferrers(referral_list); | 838 SerializeReferrers(referral_list); |
| 836 | 839 |
| 837 completion->Signal(); | 840 completion->Signal(); |
| 838 } | 841 } |
| 839 | 842 |
| 840 void Predictor::PreconnectUrl(const GURL& url, | 843 void Predictor::PreconnectUrl(const GURL& url, |
| 841 const GURL& first_party_for_cookies, | 844 const GURL& first_party_for_cookies, |
| 842 UrlInfo::ResolutionMotivation motivation, | 845 UrlInfo::ResolutionMotivation motivation, |
| 843 int count) { | 846 int count, |
| 847 bool allow_credentials) { | |
| 844 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 848 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 845 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 849 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 846 | 850 |
| 847 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 851 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 848 PreconnectUrlOnIOThread(url, first_party_for_cookies, motivation, count); | 852 PreconnectUrlOnIOThread(url, first_party_for_cookies, motivation, count, |
| 853 allow_credentials); | |
| 849 } else { | 854 } else { |
| 850 BrowserThread::PostTask( | 855 BrowserThread::PostTask( |
| 851 BrowserThread::IO, | 856 BrowserThread::IO, FROM_HERE, |
| 852 FROM_HERE, | 857 base::Bind(&Predictor::PreconnectUrlOnIOThread, base::Unretained(this), |
| 853 base::Bind(&Predictor::PreconnectUrlOnIOThread, | 858 url, first_party_for_cookies, motivation, count, |
| 854 base::Unretained(this), url, first_party_for_cookies, | 859 allow_credentials)); |
| 855 motivation, count)); | |
| 856 } | 860 } |
| 857 } | 861 } |
| 858 | 862 |
| 859 void Predictor::PreconnectUrlOnIOThread( | 863 void Predictor::PreconnectUrlOnIOThread( |
| 860 const GURL& original_url, | 864 const GURL& original_url, |
| 861 const GURL& first_party_for_cookies, | 865 const GURL& first_party_for_cookies, |
| 862 UrlInfo::ResolutionMotivation motivation, | 866 UrlInfo::ResolutionMotivation motivation, |
| 863 int count) { | 867 int count, |
| 868 bool allow_credentials) { | |
| 864 // Skip the HSTS redirect. | 869 // Skip the HSTS redirect. |
| 865 GURL url = GetHSTSRedirectOnIOThread(original_url); | 870 GURL url = GetHSTSRedirectOnIOThread(original_url); |
| 866 | 871 |
| 867 if (observer_) { | 872 if (observer_) { |
| 868 observer_->OnPreconnectUrl( | 873 observer_->OnPreconnectUrl( |
| 869 url, first_party_for_cookies, motivation, count); | 874 url, first_party_for_cookies, motivation, count); |
| 870 } | 875 } |
| 871 | 876 |
| 872 PreconnectOnIOThread(url, | 877 PreconnectOnIOThread(url, first_party_for_cookies, motivation, count, |
| 873 first_party_for_cookies, | 878 url_request_context_getter_.get(), allow_credentials); |
| 874 motivation, | |
| 875 count, | |
| 876 url_request_context_getter_.get()); | |
| 877 } | 879 } |
| 878 | 880 |
| 879 void Predictor::PredictFrameSubresources(const GURL& url, | 881 void Predictor::PredictFrameSubresources(const GURL& url, |
| 880 const GURL& first_party_for_cookies) { | 882 const GURL& first_party_for_cookies) { |
| 881 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 883 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 882 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 884 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 883 if (!predictor_enabled_) | 885 if (!predictor_enabled_) |
| 884 return; | 886 return; |
| 885 if (!CanPreresolveAndPreconnect()) | 887 if (!CanPreresolveAndPreconnect()) |
| 886 return; | 888 return; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 934 Referrers::iterator it = referrers_.find(url); | 936 Referrers::iterator it = referrers_.find(url); |
| 935 if (referrers_.end() == it) { | 937 if (referrers_.end() == it) { |
| 936 // Only when we don't know anything about this url, make 2 connections | 938 // Only when we don't know anything about this url, make 2 connections |
| 937 // available. We could do this completely via learning (by prepopulating | 939 // available. We could do this completely via learning (by prepopulating |
| 938 // the referrer_ list with this expected value), but it would swell the | 940 // the referrer_ list with this expected value), but it would swell the |
| 939 // size of the list with all the "Leaf" nodes in the tree (nodes that don't | 941 // size of the list with all the "Leaf" nodes in the tree (nodes that don't |
| 940 // load any subresources). If we learn about this resource, we will instead | 942 // load any subresources). If we learn about this resource, we will instead |
| 941 // provide a more carefully estimated preconnection count. | 943 // provide a more carefully estimated preconnection count. |
| 942 if (preconnect_enabled_) { | 944 if (preconnect_enabled_) { |
| 943 PreconnectUrlOnIOThread(url, first_party_for_cookies, | 945 PreconnectUrlOnIOThread(url, first_party_for_cookies, |
| 944 UrlInfo::SELF_REFERAL_MOTIVATED, 2); | 946 UrlInfo::SELF_REFERAL_MOTIVATED, 2, |
| 947 kAllowCredentialsOnPreconnect); | |
| 945 } | 948 } |
| 946 return; | 949 return; |
| 947 } | 950 } |
| 948 | 951 |
| 949 Referrer* referrer = &(it->second); | 952 Referrer* referrer = &(it->second); |
| 950 referrer->IncrementUseCount(); | 953 referrer->IncrementUseCount(); |
| 951 const UrlInfo::ResolutionMotivation motivation = | 954 const UrlInfo::ResolutionMotivation motivation = |
| 952 UrlInfo::LEARNED_REFERAL_MOTIVATED; | 955 UrlInfo::LEARNED_REFERAL_MOTIVATED; |
| 953 for (Referrer::iterator future_url = referrer->begin(); | 956 for (Referrer::iterator future_url = referrer->begin(); |
| 954 future_url != referrer->end(); ++future_url) { | 957 future_url != referrer->end(); ++future_url) { |
| 955 SubresourceValue evalution(TOO_NEW); | 958 SubresourceValue evalution(TOO_NEW); |
| 956 double connection_expectation = future_url->second.subresource_use_rate(); | 959 double connection_expectation = future_url->second.subresource_use_rate(); |
| 957 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.PreconnectSubresourceExpectation", | 960 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.PreconnectSubresourceExpectation", |
| 958 static_cast<int>(connection_expectation * 100), | 961 static_cast<int>(connection_expectation * 100), |
| 959 10, 5000, 50); | 962 10, 5000, 50); |
| 960 future_url->second.ReferrerWasObserved(); | 963 future_url->second.ReferrerWasObserved(); |
| 961 if (preconnect_enabled_ && | 964 if (preconnect_enabled_ && |
| 962 connection_expectation > kPreconnectWorthyExpectedValue) { | 965 connection_expectation > kPreconnectWorthyExpectedValue) { |
| 963 evalution = PRECONNECTION; | 966 evalution = PRECONNECTION; |
| 964 future_url->second.IncrementPreconnectionCount(); | 967 future_url->second.IncrementPreconnectionCount(); |
| 965 int count = static_cast<int>(std::ceil(connection_expectation)); | 968 int count = static_cast<int>(std::ceil(connection_expectation)); |
| 966 if (url.host() == future_url->first.host()) | 969 if (url.host() == future_url->first.host()) |
| 967 ++count; | 970 ++count; |
| 968 PreconnectUrlOnIOThread(future_url->first, first_party_for_cookies, | 971 PreconnectUrlOnIOThread(future_url->first, first_party_for_cookies, |
| 969 motivation, count); | 972 motivation, count, kAllowCredentialsOnPreconnect); |
| 970 } else if (connection_expectation > kDNSPreresolutionWorthyExpectedValue) { | 973 } else if (connection_expectation > kDNSPreresolutionWorthyExpectedValue) { |
| 971 evalution = PRERESOLUTION; | 974 evalution = PRERESOLUTION; |
| 972 future_url->second.preresolution_increment(); | 975 future_url->second.preresolution_increment(); |
| 973 UrlInfo* queued_info = AppendToResolutionQueue(future_url->first, | 976 UrlInfo* queued_info = AppendToResolutionQueue(future_url->first, |
| 974 motivation); | 977 motivation); |
| 975 if (queued_info) | 978 if (queued_info) |
| 976 queued_info->SetReferringHostname(url); | 979 queued_info->SetReferringHostname(url); |
| 977 } | 980 } |
| 978 UMA_HISTOGRAM_ENUMERATION("Net.PreconnectSubresourceEval", evalution, | 981 UMA_HISTOGRAM_ENUMERATION("Net.PreconnectSubresourceEval", evalution, |
| 979 SUBRESOURCE_VALUE_MAX); | 982 SUBRESOURCE_VALUE_MAX); |
| (...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1318 } | 1321 } |
| 1319 | 1322 |
| 1320 void SimplePredictor::ShutdownOnUIThread() { | 1323 void SimplePredictor::ShutdownOnUIThread() { |
| 1321 SetShutdown(true); | 1324 SetShutdown(true); |
| 1322 } | 1325 } |
| 1323 | 1326 |
| 1324 bool SimplePredictor::CanPrefetchAndPrerender() const { return true; } | 1327 bool SimplePredictor::CanPrefetchAndPrerender() const { return true; } |
| 1325 bool SimplePredictor::CanPreresolveAndPreconnect() const { return true; } | 1328 bool SimplePredictor::CanPreresolveAndPreconnect() const { return true; } |
| 1326 | 1329 |
| 1327 } // namespace chrome_browser_net | 1330 } // namespace chrome_browser_net |
| OLD | NEW |