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 | |
|
eroman
2016/06/25 01:12:53
(Not familiar with this previous code, but the pre
Charlie Harrison
2016/06/27 18:57:12
Yeah this was my impression as well. It makes some
| |
| 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, |
| 167 io_thread, profile_io_data)); | 159 io_thread, profile_io_data)); |
| 168 } | 160 } |
| (...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 567 NOTREACHED(); | 559 NOTREACHED(); |
| 568 return; | 560 return; |
| 569 } | 561 } |
| 570 | 562 |
| 571 referrers_.Put(GURL(motivating_url_spec), Referrer()) | 563 referrers_.Put(GURL(motivating_url_spec), Referrer()) |
| 572 ->second.Deserialize(*subresource_list); | 564 ->second.Deserialize(*subresource_list); |
| 573 } | 565 } |
| 574 } | 566 } |
| 575 } | 567 } |
| 576 | 568 |
| 577 void Predictor::DeserializeReferrersThenDelete( | |
| 578 base::ListValue* referral_list) { | |
| 579 DeserializeReferrers(*referral_list); | |
| 580 delete referral_list; | |
| 581 } | |
| 582 | |
| 583 void Predictor::DiscardInitialNavigationHistory() { | 569 void Predictor::DiscardInitialNavigationHistory() { |
| 584 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 570 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 585 if (initial_observer_.get()) | 571 if (initial_observer_.get()) |
| 586 initial_observer_->DiscardInitialNavigationHistory(); | 572 initial_observer_->DiscardInitialNavigationHistory(); |
| 587 } | 573 } |
| 588 | 574 |
| 589 void Predictor::FinalizeInitializationOnIOThread( | 575 void Predictor::FinalizeInitializationOnIOThread( |
| 590 const std::vector<GURL>& startup_urls, | 576 const std::vector<GURL>& startup_urls, |
| 591 base::ListValue* referral_list, | 577 const base::ListValue* referral_list, |
| 592 IOThread* io_thread, | 578 IOThread* io_thread, |
| 593 ProfileIOData* profile_io_data) { | 579 ProfileIOData* profile_io_data) { |
| 594 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 580 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 595 | 581 |
| 596 profile_io_data_ = profile_io_data; | 582 profile_io_data_ = profile_io_data; |
| 597 initial_observer_.reset(new InitialObserver()); | 583 initial_observer_.reset(new InitialObserver()); |
| 598 | 584 |
| 599 net::URLRequestContext* context = | 585 net::URLRequestContext* context = |
| 600 url_request_context_getter_->GetURLRequestContext(); | 586 url_request_context_getter_->GetURLRequestContext(); |
| 601 transport_security_state_ = context->transport_security_state(); | 587 transport_security_state_ = context->transport_security_state(); |
| 602 ssl_config_service_ = context->ssl_config_service(); | 588 ssl_config_service_ = context->ssl_config_service(); |
| 603 proxy_service_ = context->proxy_service(); | 589 proxy_service_ = context->proxy_service(); |
| 604 | 590 |
| 605 // base::WeakPtrFactory instances need to be created and destroyed | 591 // base::WeakPtrFactory instances need to be created and destroyed |
| 606 // on the same thread. The predictor lives on the IO thread and will die | 592 // 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 | 593 // from there so now that we're on the IO thread we need to properly |
| 608 // initialize the base::WeakPtrFactory. | 594 // initialize the base::WeakPtrFactory. |
| 609 // TODO(groby): Check if WeakPtrFactory has the same constraint. | 595 // TODO(groby): Check if WeakPtrFactory has the same constraint. |
| 610 weak_factory_.reset(new base::WeakPtrFactory<Predictor>(this)); | 596 weak_factory_.reset(new base::WeakPtrFactory<Predictor>(this)); |
| 611 | 597 |
| 612 // Prefetch these hostnames on startup. | 598 // Prefetch these hostnames on startup. |
| 613 DnsPrefetchMotivatedList(startup_urls, UrlInfo::STARTUP_LIST_MOTIVATED); | 599 DnsPrefetchMotivatedList(startup_urls, UrlInfo::STARTUP_LIST_MOTIVATED); |
| 614 | 600 |
| 615 DeserializeReferrersThenDelete(referral_list); | 601 DeserializeReferrers(*referral_list); |
| 616 | 602 |
| 617 LogStartupMetrics(); | 603 LogStartupMetrics(); |
| 618 } | 604 } |
| 619 | 605 |
| 620 //----------------------------------------------------------------------------- | 606 //----------------------------------------------------------------------------- |
| 621 // This section intermingles prefetch results with actual browser HTTP | 607 // This section intermingles prefetch results with actual browser HTTP |
| 622 // network activity. It supports calculating of the benefit of a prefetch, as | 608 // network activity. It supports calculating of the benefit of a prefetch, as |
| 623 // well as recording what prefetched hostname resolutions might be potentially | 609 // well as recording what prefetched hostname resolutions might be potentially |
| 624 // helpful during the next chrome-startup. | 610 // helpful during the next chrome-startup. |
| 625 //----------------------------------------------------------------------------- | 611 //----------------------------------------------------------------------------- |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 842 | 828 |
| 843 bool Predictor::CanPreresolveAndPreconnect() const { | 829 bool Predictor::CanPreresolveAndPreconnect() const { |
| 844 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 830 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 845 return chrome_browser_net::CanPreresolveAndPreconnectUI(user_prefs_); | 831 return chrome_browser_net::CanPreresolveAndPreconnectUI(user_prefs_); |
| 846 } else { | 832 } else { |
| 847 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 833 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 848 return chrome_browser_net::CanPreresolveAndPreconnectIO(profile_io_data_); | 834 return chrome_browser_net::CanPreresolveAndPreconnectIO(profile_io_data_); |
| 849 } | 835 } |
| 850 } | 836 } |
| 851 | 837 |
| 838 void Predictor::ClearPrefsOnUIThread() { | |
| 839 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 840 user_prefs_->ClearPref(prefs::kDnsPrefetchingStartupList); | |
| 841 user_prefs_->ClearPref(prefs::kDnsPrefetchingHostReferralList); | |
| 842 } | |
| 843 | |
| 844 void Predictor::DiscardAllResultAndClearPrefs() { | |
| 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 |