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 <iterator> | 9 #include <iterator> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 net::URLRequestContextGetter* getter, | 139 net::URLRequestContextGetter* getter, |
| 140 ProfileIOData* profile_io_data) { | 140 ProfileIOData* profile_io_data) { |
| 141 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 141 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 142 | 142 |
| 143 user_prefs_ = user_prefs; | 143 user_prefs_ = user_prefs; |
| 144 url_request_context_getter_ = getter; | 144 url_request_context_getter_ = getter; |
| 145 | 145 |
| 146 // Gather the list of hostnames to prefetch on startup. | 146 // Gather the list of hostnames to prefetch on startup. |
| 147 std::vector<GURL> urls = GetPredictedUrlListAtStartup(user_prefs); | 147 std::vector<GURL> urls = GetPredictedUrlListAtStartup(user_prefs); |
| 148 | 148 |
| 149 base::ListValue* referral_list = | 149 const base::ListValue* referral_list = |
| 150 static_cast<base::ListValue*>(user_prefs->GetList( | 150 user_prefs->GetList(prefs::kDnsPrefetchingHostReferralList); |
| 151 prefs::kDnsPrefetchingHostReferralList)->DeepCopy()); | |
| 152 | |
| 153 // Now that we have the statistics in memory, wipe them from the Preferences | |
| 154 // file. They will be serialized back on a clean shutdown. This way we only | |
| 155 // have to worry about clearing our in-memory state when Clearing Browsing | |
| 156 // Data. | |
| 157 user_prefs->ClearPref(prefs::kDnsPrefetchingStartupList); | |
| 158 user_prefs->ClearPref(prefs::kDnsPrefetchingHostReferralList); | |
| 159 | 151 |
| 160 BrowserThread::PostTask( | 152 BrowserThread::PostTask( |
| 161 BrowserThread::IO, | 153 BrowserThread::IO, |
| 162 FROM_HERE, | 154 FROM_HERE, |
| 163 base::Bind( | 155 base::Bind( |
| 164 &Predictor::FinalizeInitializationOnIOThread, | 156 &Predictor::FinalizeInitializationOnIOThread, |
| 165 base::Unretained(this), | 157 base::Unretained(this), |
| 166 urls, referral_list, | 158 urls, referral_list, |
|
Bernhard Bauer
2016/06/28 11:43:46
I would be a bit careful here -- you're passing th
| |
| 167 io_thread, profile_io_data)); | 159 io_thread, profile_io_data)); |
| 168 } | 160 } |
| 169 | 161 |
| 170 void Predictor::AnticipateOmniboxUrl(const GURL& url, bool preconnectable) { | 162 void Predictor::AnticipateOmniboxUrl(const GURL& url, bool preconnectable) { |
| 171 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 163 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 172 if (!predictor_enabled_) | 164 if (!predictor_enabled_) |
| 173 return; | 165 return; |
| 174 if (!url.is_valid() || !url.has_host()) | 166 if (!url.is_valid() || !url.has_host()) |
| 175 return; | 167 return; |
| 176 if (!CanPreresolveAndPreconnect()) | 168 if (!CanPreresolveAndPreconnect()) |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 308 urls.push_back(gurl.GetWithEmptyPath()); | 300 urls.push_back(gurl.GetWithEmptyPath()); |
| 309 } | 301 } |
| 310 } | 302 } |
| 311 | 303 |
| 312 if (urls.empty()) | 304 if (urls.empty()) |
| 313 urls.push_back(GURL("http://www.google.com:80")); | 305 urls.push_back(GURL("http://www.google.com:80")); |
| 314 | 306 |
| 315 return urls; | 307 return urls; |
| 316 } | 308 } |
| 317 | 309 |
| 310 void Predictor::ClearPrefsOnUIThread() { | |
| 311 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 312 user_prefs_->ClearPref(prefs::kDnsPrefetchingStartupList); | |
| 313 user_prefs_->ClearPref(prefs::kDnsPrefetchingHostReferralList); | |
| 314 } | |
| 315 | |
| 318 void Predictor::set_max_queueing_delay(int max_queueing_delay_ms) { | 316 void Predictor::set_max_queueing_delay(int max_queueing_delay_ms) { |
| 319 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 317 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 320 g_max_queueing_delay_ms = max_queueing_delay_ms; | 318 g_max_queueing_delay_ms = max_queueing_delay_ms; |
| 321 } | 319 } |
| 322 | 320 |
| 323 void Predictor::set_max_parallel_resolves(size_t max_parallel_resolves) { | 321 void Predictor::set_max_parallel_resolves(size_t max_parallel_resolves) { |
| 324 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 322 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 325 g_max_parallel_resolves = max_parallel_resolves; | 323 g_max_parallel_resolves = max_parallel_resolves; |
| 326 } | 324 } |
| 327 | 325 |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 567 NOTREACHED(); | 565 NOTREACHED(); |
| 568 return; | 566 return; |
| 569 } | 567 } |
| 570 | 568 |
| 571 referrers_.Put(GURL(motivating_url_spec), Referrer()) | 569 referrers_.Put(GURL(motivating_url_spec), Referrer()) |
| 572 ->second.Deserialize(*subresource_list); | 570 ->second.Deserialize(*subresource_list); |
| 573 } | 571 } |
| 574 } | 572 } |
| 575 } | 573 } |
| 576 | 574 |
| 577 void Predictor::DeserializeReferrersThenDelete( | |
| 578 base::ListValue* referral_list) { | |
| 579 DeserializeReferrers(*referral_list); | |
| 580 delete referral_list; | |
| 581 } | |
| 582 | |
| 583 void Predictor::DiscardInitialNavigationHistory() { | 575 void Predictor::DiscardInitialNavigationHistory() { |
| 584 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 576 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 585 if (initial_observer_.get()) | 577 if (initial_observer_.get()) |
| 586 initial_observer_->DiscardInitialNavigationHistory(); | 578 initial_observer_->DiscardInitialNavigationHistory(); |
| 587 } | 579 } |
| 588 | 580 |
| 589 void Predictor::FinalizeInitializationOnIOThread( | 581 void Predictor::FinalizeInitializationOnIOThread( |
| 590 const std::vector<GURL>& startup_urls, | 582 const std::vector<GURL>& startup_urls, |
| 591 base::ListValue* referral_list, | 583 const base::ListValue* referral_list, |
| 592 IOThread* io_thread, | 584 IOThread* io_thread, |
| 593 ProfileIOData* profile_io_data) { | 585 ProfileIOData* profile_io_data) { |
| 594 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 586 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 595 | 587 |
| 596 profile_io_data_ = profile_io_data; | 588 profile_io_data_ = profile_io_data; |
| 597 initial_observer_.reset(new InitialObserver()); | 589 initial_observer_.reset(new InitialObserver()); |
| 598 | 590 |
| 599 net::URLRequestContext* context = | 591 net::URLRequestContext* context = |
| 600 url_request_context_getter_->GetURLRequestContext(); | 592 url_request_context_getter_->GetURLRequestContext(); |
| 601 transport_security_state_ = context->transport_security_state(); | 593 transport_security_state_ = context->transport_security_state(); |
| 602 ssl_config_service_ = context->ssl_config_service(); | 594 ssl_config_service_ = context->ssl_config_service(); |
| 603 proxy_service_ = context->proxy_service(); | 595 proxy_service_ = context->proxy_service(); |
| 604 | 596 |
| 605 // base::WeakPtrFactory instances need to be created and destroyed | 597 // base::WeakPtrFactory instances need to be created and destroyed |
| 606 // on the same thread. The predictor lives on the IO thread and will die | 598 // on the same thread. The predictor lives on the IO thread and will die |
| 607 // from there so now that we're on the IO thread we need to properly | 599 // from there so now that we're on the IO thread we need to properly |
| 608 // initialize the base::WeakPtrFactory. | 600 // initialize the base::WeakPtrFactory. |
| 609 // TODO(groby): Check if WeakPtrFactory has the same constraint. | 601 // TODO(groby): Check if WeakPtrFactory has the same constraint. |
| 610 weak_factory_.reset(new base::WeakPtrFactory<Predictor>(this)); | 602 weak_factory_.reset(new base::WeakPtrFactory<Predictor>(this)); |
| 611 | 603 |
| 612 // Prefetch these hostnames on startup. | 604 // Prefetch these hostnames on startup. |
| 613 DnsPrefetchMotivatedList(startup_urls, UrlInfo::STARTUP_LIST_MOTIVATED); | 605 DnsPrefetchMotivatedList(startup_urls, UrlInfo::STARTUP_LIST_MOTIVATED); |
| 614 | 606 |
| 615 DeserializeReferrersThenDelete(referral_list); | 607 DeserializeReferrers(*referral_list); |
| 616 | 608 |
| 617 LogStartupMetrics(); | 609 LogStartupMetrics(); |
| 618 } | 610 } |
| 619 | 611 |
| 620 //----------------------------------------------------------------------------- | 612 //----------------------------------------------------------------------------- |
| 621 // This section intermingles prefetch results with actual browser HTTP | 613 // This section intermingles prefetch results with actual browser HTTP |
| 622 // network activity. It supports calculating of the benefit of a prefetch, as | 614 // network activity. It supports calculating of the benefit of a prefetch, as |
| 623 // well as recording what prefetched hostname resolutions might be potentially | 615 // well as recording what prefetched hostname resolutions might be potentially |
| 624 // helpful during the next chrome-startup. | 616 // helpful during the next chrome-startup. |
| 625 //----------------------------------------------------------------------------- | 617 //----------------------------------------------------------------------------- |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 842 | 834 |
| 843 bool Predictor::CanPreresolveAndPreconnect() const { | 835 bool Predictor::CanPreresolveAndPreconnect() const { |
| 844 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 836 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 845 return chrome_browser_net::CanPreresolveAndPreconnectUI(user_prefs_); | 837 return chrome_browser_net::CanPreresolveAndPreconnectUI(user_prefs_); |
| 846 } else { | 838 } else { |
| 847 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 839 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 848 return chrome_browser_net::CanPreresolveAndPreconnectIO(profile_io_data_); | 840 return chrome_browser_net::CanPreresolveAndPreconnectIO(profile_io_data_); |
| 849 } | 841 } |
| 850 } | 842 } |
| 851 | 843 |
| 844 void Predictor::DiscardAllResultsAndClearPrefs() { | |
| 845 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 846 DiscardAllResults(); | |
| 847 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 848 base::Bind(&Predictor::ClearPrefsOnUIThread, | |
| 849 weak_factory_->GetWeakPtr())); | |
| 850 } | |
| 851 | |
| 852 enum SubresourceValue { | 852 enum SubresourceValue { |
| 853 PRECONNECTION, | 853 PRECONNECTION, |
| 854 PRERESOLUTION, | 854 PRERESOLUTION, |
| 855 TOO_NEW, | 855 TOO_NEW, |
| 856 SUBRESOURCE_VALUE_MAX | 856 SUBRESOURCE_VALUE_MAX |
| 857 }; | 857 }; |
| 858 | 858 |
| 859 void Predictor::PrepareFrameSubresources(const GURL& original_url, | 859 void Predictor::PrepareFrameSubresources(const GURL& original_url, |
| 860 const GURL& first_party_for_cookies) { | 860 const GURL& first_party_for_cookies) { |
| 861 // Apply HSTS redirect early so it is taken into account when looking up | 861 // Apply HSTS redirect early so it is taken into account when looking up |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1226 } | 1226 } |
| 1227 | 1227 |
| 1228 void SimplePredictor::ShutdownOnUIThread() { | 1228 void SimplePredictor::ShutdownOnUIThread() { |
| 1229 SetShutdown(true); | 1229 SetShutdown(true); |
| 1230 } | 1230 } |
| 1231 | 1231 |
| 1232 bool SimplePredictor::CanPrefetchAndPrerender() const { return true; } | 1232 bool SimplePredictor::CanPrefetchAndPrerender() const { return true; } |
| 1233 bool SimplePredictor::CanPreresolveAndPreconnect() const { return true; } | 1233 bool SimplePredictor::CanPreresolveAndPreconnect() const { return true; } |
| 1234 | 1234 |
| 1235 } // namespace chrome_browser_net | 1235 } // namespace chrome_browser_net |
| OLD | NEW |