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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
79 const size_t Predictor::kMaxSpeculativeParallelResolves = 3; | 79 const size_t Predictor::kMaxSpeculativeParallelResolves = 3; |
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 const bool Predictor::kAllowCredentialsOnPreconnect = true; | |
Ryan Sleevi
2015/06/12 21:35:59
I'm not sure I understand why this is a global sta
| |
90 | |
89 | 91 |
90 static int g_max_queueing_delay_ms = | 92 static int g_max_queueing_delay_ms = |
91 Predictor::kMaxSpeculativeResolveQueueDelayMs; | 93 Predictor::kMaxSpeculativeResolveQueueDelayMs; |
92 static size_t g_max_parallel_resolves = | 94 static size_t g_max_parallel_resolves = |
93 Predictor::kMaxSpeculativeParallelResolves; | 95 Predictor::kMaxSpeculativeParallelResolves; |
94 | 96 |
95 // A version number for prefs that are saved. This should be incremented when | 97 // A version number for prefs that are saved. This should be incremented when |
96 // we change the format so that we discard old data. | 98 // we change the format so that we discard old data. |
97 static const int kPredictorStartupFormatVersion = 1; | 99 static const int kPredictorStartupFormatVersion = 1; |
98 | 100 |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
259 // don't use it in 10 secondes!!! As a result, we may do more | 261 // 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 | 262 // 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). | 263 // get with a fake request (/gen_204 might be the good path on Google). |
262 const int kMaxSearchKeepaliveSeconds(10); | 264 const int kMaxSearchKeepaliveSeconds(10); |
263 if ((now - last_omnibox_preconnect_).InSeconds() < | 265 if ((now - last_omnibox_preconnect_).InSeconds() < |
264 kMaxSearchKeepaliveSeconds) | 266 kMaxSearchKeepaliveSeconds) |
265 return; // We've done a preconnect recently. | 267 return; // We've done a preconnect recently. |
266 last_omnibox_preconnect_ = now; | 268 last_omnibox_preconnect_ = now; |
267 const int kConnectionsNeeded = 1; | 269 const int kConnectionsNeeded = 1; |
268 PreconnectUrl( | 270 PreconnectUrl( |
269 CanonicalizeUrl(url), GURL(), motivation, kConnectionsNeeded); | 271 CanonicalizeUrl(url), GURL(), motivation, kConnectionsNeeded, |
272 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 19 matching lines...) Expand all Loading... | |
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, |
309 motivation, kConnectionsNeeded); | 312 motivation, 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, |
853 first_party_for_cookies, | |
854 motivation, | |
855 count, | |
856 allow_credentials); | |
849 } else { | 857 } else { |
850 BrowserThread::PostTask( | 858 BrowserThread::PostTask( |
851 BrowserThread::IO, | 859 BrowserThread::IO, |
852 FROM_HERE, | 860 FROM_HERE, |
853 base::Bind(&Predictor::PreconnectUrlOnIOThread, | 861 base::Bind(&Predictor::PreconnectUrlOnIOThread, |
854 base::Unretained(this), url, first_party_for_cookies, | 862 base::Unretained(this), url, first_party_for_cookies, |
855 motivation, count)); | 863 motivation, count, allow_credentials)); |
856 } | 864 } |
857 } | 865 } |
858 | 866 |
859 void Predictor::PreconnectUrlOnIOThread( | 867 void Predictor::PreconnectUrlOnIOThread( |
860 const GURL& original_url, | 868 const GURL& original_url, |
861 const GURL& first_party_for_cookies, | 869 const GURL& first_party_for_cookies, |
862 UrlInfo::ResolutionMotivation motivation, | 870 UrlInfo::ResolutionMotivation motivation, |
863 int count) { | 871 int count, |
872 bool allow_credentials) { | |
864 // Skip the HSTS redirect. | 873 // Skip the HSTS redirect. |
865 GURL url = GetHSTSRedirectOnIOThread(original_url); | 874 GURL url = GetHSTSRedirectOnIOThread(original_url); |
866 | 875 |
867 if (observer_) { | 876 if (observer_) { |
868 observer_->OnPreconnectUrl( | 877 observer_->OnPreconnectUrl( |
869 url, first_party_for_cookies, motivation, count); | 878 url, first_party_for_cookies, motivation, count); |
870 } | 879 } |
871 | 880 |
872 PreconnectOnIOThread(url, | 881 PreconnectOnIOThread(url, |
873 first_party_for_cookies, | 882 first_party_for_cookies, |
874 motivation, | 883 motivation, |
875 count, | 884 count, |
876 url_request_context_getter_.get()); | 885 url_request_context_getter_.get(), |
886 allow_credentials); | |
877 } | 887 } |
878 | 888 |
879 void Predictor::PredictFrameSubresources(const GURL& url, | 889 void Predictor::PredictFrameSubresources(const GURL& url, |
880 const GURL& first_party_for_cookies) { | 890 const GURL& first_party_for_cookies) { |
881 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 891 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
882 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 892 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
883 if (!predictor_enabled_) | 893 if (!predictor_enabled_) |
884 return; | 894 return; |
885 if (!CanPreresolveAndPreconnect()) | 895 if (!CanPreresolveAndPreconnect()) |
886 return; | 896 return; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
934 Referrers::iterator it = referrers_.find(url); | 944 Referrers::iterator it = referrers_.find(url); |
935 if (referrers_.end() == it) { | 945 if (referrers_.end() == it) { |
936 // Only when we don't know anything about this url, make 2 connections | 946 // Only when we don't know anything about this url, make 2 connections |
937 // available. We could do this completely via learning (by prepopulating | 947 // available. We could do this completely via learning (by prepopulating |
938 // the referrer_ list with this expected value), but it would swell the | 948 // 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 | 949 // 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 | 950 // load any subresources). If we learn about this resource, we will instead |
941 // provide a more carefully estimated preconnection count. | 951 // provide a more carefully estimated preconnection count. |
942 if (preconnect_enabled_) { | 952 if (preconnect_enabled_) { |
943 PreconnectUrlOnIOThread(url, first_party_for_cookies, | 953 PreconnectUrlOnIOThread(url, first_party_for_cookies, |
944 UrlInfo::SELF_REFERAL_MOTIVATED, 2); | 954 UrlInfo::SELF_REFERAL_MOTIVATED, 2, |
955 kAllowCredentialsOnPreconnect); | |
945 } | 956 } |
946 return; | 957 return; |
947 } | 958 } |
948 | 959 |
949 Referrer* referrer = &(it->second); | 960 Referrer* referrer = &(it->second); |
950 referrer->IncrementUseCount(); | 961 referrer->IncrementUseCount(); |
951 const UrlInfo::ResolutionMotivation motivation = | 962 const UrlInfo::ResolutionMotivation motivation = |
952 UrlInfo::LEARNED_REFERAL_MOTIVATED; | 963 UrlInfo::LEARNED_REFERAL_MOTIVATED; |
953 for (Referrer::iterator future_url = referrer->begin(); | 964 for (Referrer::iterator future_url = referrer->begin(); |
954 future_url != referrer->end(); ++future_url) { | 965 future_url != referrer->end(); ++future_url) { |
955 SubresourceValue evalution(TOO_NEW); | 966 SubresourceValue evalution(TOO_NEW); |
956 double connection_expectation = future_url->second.subresource_use_rate(); | 967 double connection_expectation = future_url->second.subresource_use_rate(); |
957 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.PreconnectSubresourceExpectation", | 968 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.PreconnectSubresourceExpectation", |
958 static_cast<int>(connection_expectation * 100), | 969 static_cast<int>(connection_expectation * 100), |
959 10, 5000, 50); | 970 10, 5000, 50); |
960 future_url->second.ReferrerWasObserved(); | 971 future_url->second.ReferrerWasObserved(); |
961 if (preconnect_enabled_ && | 972 if (preconnect_enabled_ && |
962 connection_expectation > kPreconnectWorthyExpectedValue) { | 973 connection_expectation > kPreconnectWorthyExpectedValue) { |
963 evalution = PRECONNECTION; | 974 evalution = PRECONNECTION; |
964 future_url->second.IncrementPreconnectionCount(); | 975 future_url->second.IncrementPreconnectionCount(); |
965 int count = static_cast<int>(std::ceil(connection_expectation)); | 976 int count = static_cast<int>(std::ceil(connection_expectation)); |
966 if (url.host() == future_url->first.host()) | 977 if (url.host() == future_url->first.host()) |
967 ++count; | 978 ++count; |
968 PreconnectUrlOnIOThread(future_url->first, first_party_for_cookies, | 979 PreconnectUrlOnIOThread(future_url->first, first_party_for_cookies, |
969 motivation, count); | 980 motivation, count, kAllowCredentialsOnPreconnect); |
970 } else if (connection_expectation > kDNSPreresolutionWorthyExpectedValue) { | 981 } else if (connection_expectation > kDNSPreresolutionWorthyExpectedValue) { |
971 evalution = PRERESOLUTION; | 982 evalution = PRERESOLUTION; |
972 future_url->second.preresolution_increment(); | 983 future_url->second.preresolution_increment(); |
973 UrlInfo* queued_info = AppendToResolutionQueue(future_url->first, | 984 UrlInfo* queued_info = AppendToResolutionQueue(future_url->first, |
974 motivation); | 985 motivation); |
975 if (queued_info) | 986 if (queued_info) |
976 queued_info->SetReferringHostname(url); | 987 queued_info->SetReferringHostname(url); |
977 } | 988 } |
978 UMA_HISTOGRAM_ENUMERATION("Net.PreconnectSubresourceEval", evalution, | 989 UMA_HISTOGRAM_ENUMERATION("Net.PreconnectSubresourceEval", evalution, |
979 SUBRESOURCE_VALUE_MAX); | 990 SUBRESOURCE_VALUE_MAX); |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1318 } | 1329 } |
1319 | 1330 |
1320 void SimplePredictor::ShutdownOnUIThread() { | 1331 void SimplePredictor::ShutdownOnUIThread() { |
1321 SetShutdown(true); | 1332 SetShutdown(true); |
1322 } | 1333 } |
1323 | 1334 |
1324 bool SimplePredictor::CanPrefetchAndPrerender() const { return true; } | 1335 bool SimplePredictor::CanPrefetchAndPrerender() const { return true; } |
1325 bool SimplePredictor::CanPreresolveAndPreconnect() const { return true; } | 1336 bool SimplePredictor::CanPreresolveAndPreconnect() const { return true; } |
1326 | 1337 |
1327 } // namespace chrome_browser_net | 1338 } // namespace chrome_browser_net |
OLD | NEW |