| 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 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 urls, referral_list, | 226 urls, referral_list, |
| 227 io_thread, profile_io_data)); | 227 io_thread, profile_io_data)); |
| 228 } | 228 } |
| 229 | 229 |
| 230 void Predictor::AnticipateOmniboxUrl(const GURL& url, bool preconnectable) { | 230 void Predictor::AnticipateOmniboxUrl(const GURL& url, bool preconnectable) { |
| 231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 232 if (!predictor_enabled_) | 232 if (!predictor_enabled_) |
| 233 return; | 233 return; |
| 234 if (!url.is_valid() || !url.has_host()) | 234 if (!url.is_valid() || !url.has_host()) |
| 235 return; | 235 return; |
| 236 if (!CanPredictNetworkActionsUI()) | |
| 237 return; | |
| 238 | 236 |
| 239 std::string host = url.HostNoBrackets(); | 237 std::string host = url.HostNoBrackets(); |
| 240 bool is_new_host_request = (host != last_omnibox_host_); | 238 bool is_new_host_request = (host != last_omnibox_host_); |
| 241 last_omnibox_host_ = host; | 239 last_omnibox_host_ = host; |
| 242 | 240 |
| 243 UrlInfo::ResolutionMotivation motivation(UrlInfo::OMNIBOX_MOTIVATED); | 241 UrlInfo::ResolutionMotivation motivation(UrlInfo::OMNIBOX_MOTIVATED); |
| 244 base::TimeTicks now = base::TimeTicks::Now(); | 242 base::TimeTicks now = base::TimeTicks::Now(); |
| 245 | 243 |
| 246 if (preconnect_enabled_) { | 244 if (preconnect_enabled_ && CanPreconnectUI()) { |
| 247 if (preconnectable && !is_new_host_request) { | 245 if (preconnectable && !is_new_host_request) { |
| 248 ++consecutive_omnibox_preconnect_count_; | 246 ++consecutive_omnibox_preconnect_count_; |
| 249 // The omnibox suggests a search URL (for which we can preconnect) after | 247 // The omnibox suggests a search URL (for which we can preconnect) after |
| 250 // one or two characters are typed, even though such typing often (1 in | 248 // one or two characters are typed, even though such typing often (1 in |
| 251 // 3?) becomes a real URL. This code waits till is has more evidence of a | 249 // 3?) becomes a real URL. This code waits till is has more evidence of a |
| 252 // preconnectable URL (search URL) before forming a preconnection, so as | 250 // preconnectable URL (search URL) before forming a preconnection, so as |
| 253 // to reduce the useless preconnect rate. | 251 // to reduce the useless preconnect rate. |
| 254 // Perchance this logic should be pushed back into the omnibox, where the | 252 // Perchance this logic should be pushed back into the omnibox, where the |
| 255 // actual characters typed, such as a space, can better forcast whether | 253 // actual characters typed, such as a space, can better forcast whether |
| 256 // we need to search/preconnect or not. By waiting for at least 4 | 254 // we need to search/preconnect or not. By waiting for at least 4 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 | 302 |
| 305 void Predictor::PreconnectUrlAndSubresources(const GURL& url, | 303 void Predictor::PreconnectUrlAndSubresources(const GURL& url, |
| 306 const GURL& first_party_for_cookies) { | 304 const GURL& first_party_for_cookies) { |
| 307 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 305 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 308 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 306 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 309 if (!predictor_enabled_ || !preconnect_enabled_ || | 307 if (!predictor_enabled_ || !preconnect_enabled_ || |
| 310 !url.is_valid() || !url.has_host()) | 308 !url.is_valid() || !url.has_host()) |
| 311 return; | 309 return; |
| 312 | 310 |
| 313 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 311 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 314 if (!CanPredictNetworkActionsUI()) | 312 if (!CanPreconnectUI()) |
| 315 return; | 313 return; |
| 316 } else { | 314 } else { |
| 317 if (!CanPredictNetworkActionsIO()) | 315 if (!CanPreconnectIO()) |
| 318 return; | 316 return; |
| 319 } | 317 } |
| 320 | 318 |
| 321 UrlInfo::ResolutionMotivation motivation(UrlInfo::EARLY_LOAD_MOTIVATED); | 319 UrlInfo::ResolutionMotivation motivation(UrlInfo::EARLY_LOAD_MOTIVATED); |
| 322 const int kConnectionsNeeded = 1; | 320 const int kConnectionsNeeded = 1; |
| 323 PreconnectUrl(CanonicalizeUrl(url), first_party_for_cookies, | 321 PreconnectUrl(CanonicalizeUrl(url), first_party_for_cookies, |
| 324 motivation, kConnectionsNeeded); | 322 motivation, kConnectionsNeeded); |
| 325 PredictFrameSubresources(url.GetWithEmptyPath(), first_party_for_cookies); | 323 PredictFrameSubresources(url.GetWithEmptyPath(), first_party_for_cookies); |
| 326 } | 324 } |
| 327 | 325 |
| (...skipping 621 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 949 } | 947 } |
| 950 | 948 |
| 951 bool Predictor::CanPredictNetworkActionsUI() { | 949 bool Predictor::CanPredictNetworkActionsUI() { |
| 952 return chrome_browser_net::CanPredictNetworkActionsUI(user_prefs_); | 950 return chrome_browser_net::CanPredictNetworkActionsUI(user_prefs_); |
| 953 } | 951 } |
| 954 | 952 |
| 955 bool Predictor::CanPredictNetworkActionsIO() { | 953 bool Predictor::CanPredictNetworkActionsIO() { |
| 956 return chrome_browser_net::CanPredictNetworkActionsIO(profile_io_data_); | 954 return chrome_browser_net::CanPredictNetworkActionsIO(profile_io_data_); |
| 957 } | 955 } |
| 958 | 956 |
| 957 bool Predictor::CanPreconnectUI() { |
| 958 return chrome_browser_net::CanPreconnectUI(user_prefs_); |
| 959 } |
| 960 |
| 961 bool Predictor::CanPreconnectIO() { |
| 962 return chrome_browser_net::CanPreconnectIO(profile_io_data_); |
| 963 } |
| 964 |
| 959 enum SubresourceValue { | 965 enum SubresourceValue { |
| 960 PRECONNECTION, | 966 PRECONNECTION, |
| 961 PRERESOLUTION, | 967 PRERESOLUTION, |
| 962 TOO_NEW, | 968 TOO_NEW, |
| 963 SUBRESOURCE_VALUE_MAX | 969 SUBRESOURCE_VALUE_MAX |
| 964 }; | 970 }; |
| 965 | 971 |
| 966 void Predictor::PrepareFrameSubresources(const GURL& original_url, | 972 void Predictor::PrepareFrameSubresources(const GURL& original_url, |
| 967 const GURL& first_party_for_cookies) { | 973 const GURL& first_party_for_cookies) { |
| 968 // Apply HSTS redirect early so it is taken into account when looking up | 974 // Apply HSTS redirect early so it is taken into account when looking up |
| (...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1340 ProfileIOData* profile_io_data) { | 1346 ProfileIOData* profile_io_data) { |
| 1341 // Empty function for unittests. | 1347 // Empty function for unittests. |
| 1342 } | 1348 } |
| 1343 | 1349 |
| 1344 void SimplePredictor::ShutdownOnUIThread() { | 1350 void SimplePredictor::ShutdownOnUIThread() { |
| 1345 SetShutdown(true); | 1351 SetShutdown(true); |
| 1346 } | 1352 } |
| 1347 | 1353 |
| 1348 bool SimplePredictor::CanPredictNetworkActionsUI() { return true; } | 1354 bool SimplePredictor::CanPredictNetworkActionsUI() { return true; } |
| 1349 bool SimplePredictor::CanPredictNetworkActionsIO() { return true; } | 1355 bool SimplePredictor::CanPredictNetworkActionsIO() { return true; } |
| 1356 bool SimplePredictor::CanPreconnectUI() { return true; } |
| 1357 bool SimplePredictor::CanPreconnectIO() { return true; } |
| 1350 | 1358 |
| 1351 } // namespace chrome_browser_net | 1359 } // namespace chrome_browser_net |
| OLD | NEW |