| 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 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 if (!CanPreresolveAndPreconnect()) | 228 if (!CanPreresolveAndPreconnect()) |
| 229 return; | 229 return; |
| 230 | 230 |
| 231 std::string host = url.HostNoBrackets(); | 231 std::string host = url.HostNoBrackets(); |
| 232 bool is_new_host_request = (host != last_omnibox_host_); | 232 bool is_new_host_request = (host != last_omnibox_host_); |
| 233 last_omnibox_host_ = host; | 233 last_omnibox_host_ = host; |
| 234 | 234 |
| 235 UrlInfo::ResolutionMotivation motivation(UrlInfo::OMNIBOX_MOTIVATED); | 235 UrlInfo::ResolutionMotivation motivation(UrlInfo::OMNIBOX_MOTIVATED); |
| 236 base::TimeTicks now = base::TimeTicks::Now(); | 236 base::TimeTicks now = base::TimeTicks::Now(); |
| 237 | 237 |
| 238 if (preconnect_enabled_) { | 238 if (preconnect_enabled()) { |
| 239 if (preconnectable && !is_new_host_request) { | 239 if (preconnectable && !is_new_host_request) { |
| 240 ++consecutive_omnibox_preconnect_count_; | 240 ++consecutive_omnibox_preconnect_count_; |
| 241 // The omnibox suggests a search URL (for which we can preconnect) after | 241 // The omnibox suggests a search URL (for which we can preconnect) after |
| 242 // one or two characters are typed, even though such typing often (1 in | 242 // one or two characters are typed, even though such typing often (1 in |
| 243 // 3?) becomes a real URL. This code waits till is has more evidence of a | 243 // 3?) becomes a real URL. This code waits till is has more evidence of a |
| 244 // preconnectable URL (search URL) before forming a preconnection, so as | 244 // preconnectable URL (search URL) before forming a preconnection, so as |
| 245 // to reduce the useless preconnect rate. | 245 // to reduce the useless preconnect rate. |
| 246 // Perchance this logic should be pushed back into the omnibox, where the | 246 // Perchance this logic should be pushed back into the omnibox, where the |
| 247 // actual characters typed, such as a space, can better forcast whether | 247 // actual characters typed, such as a space, can better forcast whether |
| 248 // we need to search/preconnect or not. By waiting for at least 4 | 248 // we need to search/preconnect or not. By waiting for at least 4 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 BrowserThread::IO, | 292 BrowserThread::IO, |
| 293 FROM_HERE, | 293 FROM_HERE, |
| 294 base::Bind(&Predictor::Resolve, base::Unretained(this), | 294 base::Bind(&Predictor::Resolve, base::Unretained(this), |
| 295 CanonicalizeUrl(url), motivation)); | 295 CanonicalizeUrl(url), motivation)); |
| 296 } | 296 } |
| 297 | 297 |
| 298 void Predictor::PreconnectUrlAndSubresources(const GURL& url, | 298 void Predictor::PreconnectUrlAndSubresources(const GURL& url, |
| 299 const GURL& first_party_for_cookies) { | 299 const GURL& first_party_for_cookies) { |
| 300 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 300 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 301 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 301 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 302 if (!predictor_enabled_ || !preconnect_enabled_ || | 302 if (!predictor_enabled_ || !preconnect_enabled() || !url.is_valid() || |
| 303 !url.is_valid() || !url.has_host()) | 303 !url.has_host()) |
| 304 return; | 304 return; |
| 305 if (!CanPreresolveAndPreconnect()) | 305 if (!CanPreresolveAndPreconnect()) |
| 306 return; | 306 return; |
| 307 | 307 |
| 308 UrlInfo::ResolutionMotivation motivation(UrlInfo::EARLY_LOAD_MOTIVATED); | 308 UrlInfo::ResolutionMotivation motivation(UrlInfo::EARLY_LOAD_MOTIVATED); |
| 309 const int kConnectionsNeeded = 1; | 309 const int kConnectionsNeeded = 1; |
| 310 PreconnectUrl(CanonicalizeUrl(url), first_party_for_cookies, motivation, | 310 PreconnectUrl(CanonicalizeUrl(url), first_party_for_cookies, motivation, |
| 311 kConnectionsNeeded, kAllowCredentialsOnPreconnectByDefault); | 311 kConnectionsNeeded, kAllowCredentialsOnPreconnectByDefault); |
| 312 PredictFrameSubresources(url.GetWithEmptyPath(), first_party_for_cookies); | 312 PredictFrameSubresources(url.GetWithEmptyPath(), first_party_for_cookies); |
| 313 } | 313 } |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 void Predictor::LearnFromNavigation(const GURL& referring_url, | 458 void Predictor::LearnFromNavigation(const GURL& referring_url, |
| 459 const GURL& target_url) { | 459 const GURL& target_url) { |
| 460 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 460 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 461 if (!predictor_enabled_ || !CanPreresolveAndPreconnect()) | 461 if (!predictor_enabled_ || !CanPreresolveAndPreconnect()) |
| 462 return; | 462 return; |
| 463 DCHECK_EQ(referring_url, Predictor::CanonicalizeUrl(referring_url)); | 463 DCHECK_EQ(referring_url, Predictor::CanonicalizeUrl(referring_url)); |
| 464 DCHECK_NE(referring_url, GURL::EmptyGURL()); | 464 DCHECK_NE(referring_url, GURL::EmptyGURL()); |
| 465 DCHECK_EQ(target_url, Predictor::CanonicalizeUrl(target_url)); | 465 DCHECK_EQ(target_url, Predictor::CanonicalizeUrl(target_url)); |
| 466 DCHECK_NE(target_url, GURL::EmptyGURL()); | 466 DCHECK_NE(target_url, GURL::EmptyGURL()); |
| 467 | 467 |
| 468 if (observer_) |
| 469 observer_->OnLearnFromNavigation(referring_url, target_url); |
| 468 referrers_[referring_url].SuggestHost(target_url); | 470 referrers_[referring_url].SuggestHost(target_url); |
| 469 // Possibly do some referrer trimming. | 471 // Possibly do some referrer trimming. |
| 470 TrimReferrers(); | 472 TrimReferrers(); |
| 471 } | 473 } |
| 472 | 474 |
| 473 //----------------------------------------------------------------------------- | 475 //----------------------------------------------------------------------------- |
| 474 // This section supports the about:dns page. | 476 // This section supports the about:dns page. |
| 475 | 477 |
| 476 void Predictor::PredictorGetHtmlInfo(Predictor* predictor, | 478 void Predictor::PredictorGetHtmlInfo(Predictor* predictor, |
| 477 std::string* output) { | 479 std::string* output) { |
| (...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 964 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 966 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 965 DCHECK_EQ(url.GetWithEmptyPath(), url); | 967 DCHECK_EQ(url.GetWithEmptyPath(), url); |
| 966 Referrers::iterator it = referrers_.find(url); | 968 Referrers::iterator it = referrers_.find(url); |
| 967 if (referrers_.end() == it) { | 969 if (referrers_.end() == it) { |
| 968 // Only when we don't know anything about this url, make 2 connections | 970 // Only when we don't know anything about this url, make 2 connections |
| 969 // available. We could do this completely via learning (by prepopulating | 971 // available. We could do this completely via learning (by prepopulating |
| 970 // the referrer_ list with this expected value), but it would swell the | 972 // the referrer_ list with this expected value), but it would swell the |
| 971 // size of the list with all the "Leaf" nodes in the tree (nodes that don't | 973 // size of the list with all the "Leaf" nodes in the tree (nodes that don't |
| 972 // load any subresources). If we learn about this resource, we will instead | 974 // load any subresources). If we learn about this resource, we will instead |
| 973 // provide a more carefully estimated preconnection count. | 975 // provide a more carefully estimated preconnection count. |
| 974 if (preconnect_enabled_) { | 976 if (preconnect_enabled()) { |
| 975 PreconnectUrlOnIOThread(url, first_party_for_cookies, | 977 PreconnectUrlOnIOThread(url, first_party_for_cookies, |
| 976 UrlInfo::SELF_REFERAL_MOTIVATED, | 978 UrlInfo::SELF_REFERAL_MOTIVATED, |
| 977 kAllowCredentialsOnPreconnectByDefault, 2); | 979 kAllowCredentialsOnPreconnectByDefault, 2); |
| 978 } | 980 } |
| 979 return; | 981 return; |
| 980 } | 982 } |
| 981 | 983 |
| 982 Referrer* referrer = &(it->second); | 984 Referrer* referrer = &(it->second); |
| 983 referrer->IncrementUseCount(); | 985 referrer->IncrementUseCount(); |
| 984 const UrlInfo::ResolutionMotivation motivation = | 986 const UrlInfo::ResolutionMotivation motivation = |
| 985 UrlInfo::LEARNED_REFERAL_MOTIVATED; | 987 UrlInfo::LEARNED_REFERAL_MOTIVATED; |
| 986 for (Referrer::iterator future_url = referrer->begin(); | 988 for (Referrer::iterator future_url = referrer->begin(); |
| 987 future_url != referrer->end(); ++future_url) { | 989 future_url != referrer->end(); ++future_url) { |
| 988 SubresourceValue evalution(TOO_NEW); | 990 SubresourceValue evalution(TOO_NEW); |
| 989 double connection_expectation = future_url->second.subresource_use_rate(); | 991 double connection_expectation = future_url->second.subresource_use_rate(); |
| 990 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.PreconnectSubresourceExpectation", | 992 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.PreconnectSubresourceExpectation", |
| 991 static_cast<int>(connection_expectation * 100), | 993 static_cast<int>(connection_expectation * 100), |
| 992 10, 5000, 50); | 994 10, 5000, 50); |
| 993 future_url->second.ReferrerWasObserved(); | 995 future_url->second.ReferrerWasObserved(); |
| 994 if (preconnect_enabled_ && | 996 if (preconnect_enabled() && |
| 995 connection_expectation > kPreconnectWorthyExpectedValue) { | 997 connection_expectation > kPreconnectWorthyExpectedValue) { |
| 996 evalution = PRECONNECTION; | 998 evalution = PRECONNECTION; |
| 997 future_url->second.IncrementPreconnectionCount(); | 999 future_url->second.IncrementPreconnectionCount(); |
| 998 int count = static_cast<int>(std::ceil(connection_expectation)); | 1000 int count = static_cast<int>(std::ceil(connection_expectation)); |
| 999 if (url.host_piece() == future_url->first.host_piece()) | 1001 if (url.host_piece() == future_url->first.host_piece()) |
| 1000 ++count; | 1002 ++count; |
| 1001 PreconnectUrlOnIOThread(future_url->first, first_party_for_cookies, | 1003 PreconnectUrlOnIOThread(future_url->first, first_party_for_cookies, |
| 1002 motivation, | 1004 motivation, |
| 1003 kAllowCredentialsOnPreconnectByDefault, count); | 1005 kAllowCredentialsOnPreconnectByDefault, count); |
| 1004 } else if (connection_expectation > kDNSPreresolutionWorthyExpectedValue) { | 1006 } else if (connection_expectation > kDNSPreresolutionWorthyExpectedValue) { |
| (...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1335 } | 1337 } |
| 1336 | 1338 |
| 1337 void SimplePredictor::ShutdownOnUIThread() { | 1339 void SimplePredictor::ShutdownOnUIThread() { |
| 1338 SetShutdown(true); | 1340 SetShutdown(true); |
| 1339 } | 1341 } |
| 1340 | 1342 |
| 1341 bool SimplePredictor::CanPrefetchAndPrerender() const { return true; } | 1343 bool SimplePredictor::CanPrefetchAndPrerender() const { return true; } |
| 1342 bool SimplePredictor::CanPreresolveAndPreconnect() const { return true; } | 1344 bool SimplePredictor::CanPreresolveAndPreconnect() const { return true; } |
| 1343 | 1345 |
| 1344 } // namespace chrome_browser_net | 1346 } // namespace chrome_browser_net |
| OLD | NEW |