| 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 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 void Predictor::InitNetworkPredictor(PrefService* user_prefs, | 153 void Predictor::InitNetworkPredictor(PrefService* user_prefs, |
| 154 IOThread* io_thread, | 154 IOThread* io_thread, |
| 155 net::URLRequestContextGetter* getter, | 155 net::URLRequestContextGetter* getter, |
| 156 ProfileIOData* profile_io_data) { | 156 ProfileIOData* profile_io_data) { |
| 157 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 157 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 158 | 158 |
| 159 user_prefs_ = user_prefs; | 159 user_prefs_ = user_prefs; |
| 160 url_request_context_getter_ = getter; | 160 url_request_context_getter_ = getter; |
| 161 | 161 |
| 162 // Gather the list of hostnames to prefetch on startup. | 162 // Gather the list of hostnames to prefetch on startup. |
| 163 UrlList urls = GetPredictedUrlListAtStartup(user_prefs); | 163 std::vector<GURL> urls = GetPredictedUrlListAtStartup(user_prefs); |
| 164 | 164 |
| 165 base::ListValue* referral_list = | 165 base::ListValue* referral_list = |
| 166 static_cast<base::ListValue*>(user_prefs->GetList( | 166 static_cast<base::ListValue*>(user_prefs->GetList( |
| 167 prefs::kDnsPrefetchingHostReferralList)->DeepCopy()); | 167 prefs::kDnsPrefetchingHostReferralList)->DeepCopy()); |
| 168 | 168 |
| 169 // Now that we have the statistics in memory, wipe them from the Preferences | 169 // Now that we have the statistics in memory, wipe them from the Preferences |
| 170 // file. They will be serialized back on a clean shutdown. This way we only | 170 // file. They will be serialized back on a clean shutdown. This way we only |
| 171 // have to worry about clearing our in-memory state when Clearing Browsing | 171 // have to worry about clearing our in-memory state when Clearing Browsing |
| 172 // Data. | 172 // Data. |
| 173 user_prefs->ClearPref(prefs::kDnsPrefetchingStartupList); | 173 user_prefs->ClearPref(prefs::kDnsPrefetchingStartupList); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 if (!CanPreresolveAndPreconnect()) | 269 if (!CanPreresolveAndPreconnect()) |
| 270 return; | 270 return; |
| 271 | 271 |
| 272 UrlInfo::ResolutionMotivation motivation(UrlInfo::EARLY_LOAD_MOTIVATED); | 272 UrlInfo::ResolutionMotivation motivation(UrlInfo::EARLY_LOAD_MOTIVATED); |
| 273 const int kConnectionsNeeded = 1; | 273 const int kConnectionsNeeded = 1; |
| 274 PreconnectUrl(CanonicalizeUrl(url), first_party_for_cookies, motivation, | 274 PreconnectUrl(CanonicalizeUrl(url), first_party_for_cookies, motivation, |
| 275 kConnectionsNeeded, kAllowCredentialsOnPreconnectByDefault); | 275 kConnectionsNeeded, kAllowCredentialsOnPreconnectByDefault); |
| 276 PredictFrameSubresources(url.GetWithEmptyPath(), first_party_for_cookies); | 276 PredictFrameSubresources(url.GetWithEmptyPath(), first_party_for_cookies); |
| 277 } | 277 } |
| 278 | 278 |
| 279 UrlList Predictor::GetPredictedUrlListAtStartup(PrefService* user_prefs) { | 279 std::vector<GURL> Predictor::GetPredictedUrlListAtStartup( |
| 280 PrefService* user_prefs) { |
| 280 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 281 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 281 UrlList urls; | 282 std::vector<GURL> urls; |
| 282 // Recall list of URLs we learned about during last session. | 283 // Recall list of URLs we learned about during last session. |
| 283 // This may catch secondary hostnames, pulled in by the homepages. It will | 284 // This may catch secondary hostnames, pulled in by the homepages. It will |
| 284 // also catch more of the "primary" home pages, since that was (presumably) | 285 // also catch more of the "primary" home pages, since that was (presumably) |
| 285 // rendered first (and will be rendered first this time too). | 286 // rendered first (and will be rendered first this time too). |
| 286 const base::ListValue* startup_list = | 287 const base::ListValue* startup_list = |
| 287 user_prefs->GetList(prefs::kDnsPrefetchingStartupList); | 288 user_prefs->GetList(prefs::kDnsPrefetchingStartupList); |
| 288 | 289 |
| 289 if (startup_list) { | 290 if (startup_list) { |
| 290 base::ListValue::const_iterator it = startup_list->begin(); | 291 base::ListValue::const_iterator it = startup_list->begin(); |
| 291 int format_version = -1; | 292 int format_version = -1; |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 assignees.size() <= max_concurrent_dns_lookups_); | 392 assignees.size() <= max_concurrent_dns_lookups_); |
| 392 results_.clear(); | 393 results_.clear(); |
| 393 // Put back in the names being worked on. | 394 // Put back in the names being worked on. |
| 394 for (Results::iterator it = assignees.begin(); assignees.end() != it; ++it) { | 395 for (Results::iterator it = assignees.begin(); assignees.end() != it; ++it) { |
| 395 DCHECK(it->second.is_marked_to_delete()); | 396 DCHECK(it->second.is_marked_to_delete()); |
| 396 results_[it->first] = it->second; | 397 results_[it->first] = it->second; |
| 397 } | 398 } |
| 398 } | 399 } |
| 399 | 400 |
| 400 // Overloaded Resolve() to take a vector of names. | 401 // Overloaded Resolve() to take a vector of names. |
| 401 void Predictor::ResolveList(const UrlList& urls, | 402 void Predictor::ResolveList(const std::vector<GURL>& urls, |
| 402 UrlInfo::ResolutionMotivation motivation) { | 403 UrlInfo::ResolutionMotivation motivation) { |
| 403 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 404 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 404 | 405 |
| 405 for (UrlList::const_iterator it = urls.begin(); it < urls.end(); ++it) { | 406 for (std::vector<GURL>::const_iterator it = urls.begin(); it < urls.end(); |
| 407 ++it) { |
| 406 AppendToResolutionQueue(*it, motivation); | 408 AppendToResolutionQueue(*it, motivation); |
| 407 } | 409 } |
| 408 } | 410 } |
| 409 | 411 |
| 410 // Basic Resolve() takes an invidual name, and adds it | 412 // Basic Resolve() takes an invidual name, and adds it |
| 411 // to the queue. | 413 // to the queue. |
| 412 void Predictor::Resolve(const GURL& url, | 414 void Predictor::Resolve(const GURL& url, |
| 413 UrlInfo::ResolutionMotivation motivation) { | 415 UrlInfo::ResolutionMotivation motivation) { |
| 414 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 416 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 415 if (!url.has_host()) | 417 if (!url.has_host()) |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 636 delete referral_list; | 638 delete referral_list; |
| 637 } | 639 } |
| 638 | 640 |
| 639 void Predictor::DiscardInitialNavigationHistory() { | 641 void Predictor::DiscardInitialNavigationHistory() { |
| 640 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 642 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 641 if (initial_observer_.get()) | 643 if (initial_observer_.get()) |
| 642 initial_observer_->DiscardInitialNavigationHistory(); | 644 initial_observer_->DiscardInitialNavigationHistory(); |
| 643 } | 645 } |
| 644 | 646 |
| 645 void Predictor::FinalizeInitializationOnIOThread( | 647 void Predictor::FinalizeInitializationOnIOThread( |
| 646 const UrlList& startup_urls, | 648 const std::vector<GURL>& startup_urls, |
| 647 base::ListValue* referral_list, | 649 base::ListValue* referral_list, |
| 648 IOThread* io_thread, | 650 IOThread* io_thread, |
| 649 ProfileIOData* profile_io_data) { | 651 ProfileIOData* profile_io_data) { |
| 650 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 652 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 651 | 653 |
| 652 profile_io_data_ = profile_io_data; | 654 profile_io_data_ = profile_io_data; |
| 653 initial_observer_.reset(new InitialObserver()); | 655 initial_observer_.reset(new InitialObserver()); |
| 654 | 656 |
| 655 net::URLRequestContext* context = | 657 net::URLRequestContext* context = |
| 656 url_request_context_getter_->GetURLRequestContext(); | 658 url_request_context_getter_->GetURLRequestContext(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 684 !CanPreresolveAndPreconnect()) { | 686 !CanPreresolveAndPreconnect()) { |
| 685 return; | 687 return; |
| 686 } | 688 } |
| 687 initial_observer_->Append(url, this); | 689 initial_observer_->Append(url, this); |
| 688 } | 690 } |
| 689 | 691 |
| 690 // This API is only used in the browser process. | 692 // This API is only used in the browser process. |
| 691 // It is called from an IPC message originating in the renderer. It currently | 693 // It is called from an IPC message originating in the renderer. It currently |
| 692 // includes both Page-Scan, and Link-Hover prefetching. | 694 // includes both Page-Scan, and Link-Hover prefetching. |
| 693 // TODO(jar): Separate out link-hover prefetching, and page-scan results. | 695 // TODO(jar): Separate out link-hover prefetching, and page-scan results. |
| 694 void Predictor::DnsPrefetchList(const NameList& hostnames) { | 696 void Predictor::DnsPrefetchList(const std::vector<std::string>& hostnames) { |
| 695 // TODO(jar): Push GURL transport further back into renderer, but this will | 697 // TODO(jar): Push GURL transport further back into renderer, but this will |
| 696 // require a Webkit change in the observer :-/. | 698 // require a Webkit change in the observer :-/. |
| 697 UrlList urls; | 699 std::vector<GURL> urls; |
| 698 for (NameList::const_iterator it = hostnames.begin(); | 700 for (std::vector<std::string>::const_iterator it = hostnames.begin(); |
| 699 it < hostnames.end(); | 701 it < hostnames.end(); ++it) { |
| 700 ++it) { | |
| 701 urls.push_back(GURL("http://" + *it + ":80")); | 702 urls.push_back(GURL("http://" + *it + ":80")); |
| 702 } | 703 } |
| 703 | 704 |
| 704 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 705 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 705 DnsPrefetchMotivatedList(urls, UrlInfo::PAGE_SCAN_MOTIVATED); | 706 DnsPrefetchMotivatedList(urls, UrlInfo::PAGE_SCAN_MOTIVATED); |
| 706 } | 707 } |
| 707 | 708 |
| 708 void Predictor::DnsPrefetchMotivatedList( | 709 void Predictor::DnsPrefetchMotivatedList( |
| 709 const UrlList& urls, | 710 const std::vector<GURL>& urls, |
| 710 UrlInfo::ResolutionMotivation motivation) { | 711 UrlInfo::ResolutionMotivation motivation) { |
| 711 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 712 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 712 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 713 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 713 if (!predictor_enabled_) | 714 if (!predictor_enabled_) |
| 714 return; | 715 return; |
| 715 if (!CanPreresolveAndPreconnect()) | 716 if (!CanPreresolveAndPreconnect()) |
| 716 return; | 717 return; |
| 717 | 718 |
| 718 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 719 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 719 ResolveList(urls, motivation); | 720 ResolveList(urls, motivation); |
| (...skipping 593 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1313 } | 1314 } |
| 1314 | 1315 |
| 1315 void SimplePredictor::ShutdownOnUIThread() { | 1316 void SimplePredictor::ShutdownOnUIThread() { |
| 1316 SetShutdown(true); | 1317 SetShutdown(true); |
| 1317 } | 1318 } |
| 1318 | 1319 |
| 1319 bool SimplePredictor::CanPrefetchAndPrerender() const { return true; } | 1320 bool SimplePredictor::CanPrefetchAndPrerender() const { return true; } |
| 1320 bool SimplePredictor::CanPreresolveAndPreconnect() const { return true; } | 1321 bool SimplePredictor::CanPreresolveAndPreconnect() const { return true; } |
| 1321 | 1322 |
| 1322 } // namespace chrome_browser_net | 1323 } // namespace chrome_browser_net |
| OLD | NEW |