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> |
| 11 #include <sstream> | 11 #include <sstream> |
| 12 #include <utility> | 12 #include <utility> |
| 13 | 13 |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/callback.h" | |
| 15 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
| 16 #include "base/containers/mru_cache.h" | 17 #include "base/containers/mru_cache.h" |
| 17 #include "base/location.h" | 18 #include "base/location.h" |
| 18 #include "base/logging.h" | 19 #include "base/logging.h" |
| 19 #include "base/macros.h" | 20 #include "base/macros.h" |
| 20 #include "base/memory/ptr_util.h" | 21 #include "base/memory/ptr_util.h" |
| 21 #include "base/metrics/histogram.h" | 22 #include "base/metrics/histogram.h" |
| 22 #include "base/single_thread_task_runner.h" | 23 #include "base/single_thread_task_runner.h" |
| 23 #include "base/stl_util.h" | 24 #include "base/stl_util.h" |
| 24 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 Predictor* Predictor::CreatePredictor(bool preconnect_enabled, | 125 Predictor* Predictor::CreatePredictor(bool preconnect_enabled, |
| 125 bool predictor_enabled, | 126 bool predictor_enabled, |
| 126 bool simple_shutdown) { | 127 bool simple_shutdown) { |
| 127 if (simple_shutdown) | 128 if (simple_shutdown) |
| 128 return new SimplePredictor(preconnect_enabled, predictor_enabled); | 129 return new SimplePredictor(preconnect_enabled, predictor_enabled); |
| 129 return new Predictor(preconnect_enabled, predictor_enabled); | 130 return new Predictor(preconnect_enabled, predictor_enabled); |
| 130 } | 131 } |
| 131 | 132 |
| 132 void Predictor::RegisterProfilePrefs( | 133 void Predictor::RegisterProfilePrefs( |
| 133 user_prefs::PrefRegistrySyncable* registry) { | 134 user_prefs::PrefRegistrySyncable* registry) { |
| 134 registry->RegisterListPref(prefs::kDnsPrefetchingStartupList); | 135 registry->RegisterListPref(prefs::kDnsPrefetchingStartupList, |
| 135 registry->RegisterListPref(prefs::kDnsPrefetchingHostReferralList); | 136 PrefRegistry::LOSSY_PREF); |
| 137 registry->RegisterListPref(prefs::kDnsPrefetchingHostReferralList, | |
| 138 PrefRegistry::LOSSY_PREF); | |
| 136 } | 139 } |
| 137 | 140 |
| 138 // --------------------- Start UI methods. ------------------------------------ | 141 // --------------------- Start UI methods. ------------------------------------ |
| 139 | 142 |
| 140 void Predictor::InitNetworkPredictor(PrefService* user_prefs, | 143 void Predictor::InitNetworkPredictor(PrefService* user_prefs, |
| 141 IOThread* io_thread, | 144 IOThread* io_thread, |
| 142 net::URLRequestContextGetter* getter, | 145 net::URLRequestContextGetter* getter, |
| 143 ProfileIOData* profile_io_data) { | 146 ProfileIOData* profile_io_data) { |
| 144 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 147 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 145 | 148 |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 311 void Predictor::DiscardAllResultsAndClearPrefsOnUIThread() { | 314 void Predictor::DiscardAllResultsAndClearPrefsOnUIThread() { |
| 312 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 315 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 313 BrowserThread::PostTask( | 316 BrowserThread::PostTask( |
| 314 BrowserThread::IO, FROM_HERE, | 317 BrowserThread::IO, FROM_HERE, |
| 315 base::Bind(&Predictor::DiscardAllResults, weak_factory_->GetWeakPtr())); | 318 base::Bind(&Predictor::DiscardAllResults, weak_factory_->GetWeakPtr())); |
| 316 ClearPrefsOnUIThread(); | 319 ClearPrefsOnUIThread(); |
| 317 } | 320 } |
| 318 | 321 |
| 319 void Predictor::ClearPrefsOnUIThread() { | 322 void Predictor::ClearPrefsOnUIThread() { |
| 320 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 323 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 324 DCHECK(user_prefs_); | |
| 321 user_prefs_->ClearPref(prefs::kDnsPrefetchingStartupList); | 325 user_prefs_->ClearPref(prefs::kDnsPrefetchingStartupList); |
| 322 user_prefs_->ClearPref(prefs::kDnsPrefetchingHostReferralList); | 326 user_prefs_->ClearPref(prefs::kDnsPrefetchingHostReferralList); |
| 323 } | 327 } |
| 324 | 328 |
| 325 void Predictor::set_max_queueing_delay(int max_queueing_delay_ms) { | 329 void Predictor::set_max_queueing_delay(int max_queueing_delay_ms) { |
| 326 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 330 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 327 g_max_queueing_delay_ms = max_queueing_delay_ms; | 331 g_max_queueing_delay_ms = max_queueing_delay_ms; |
| 328 } | 332 } |
| 329 | 333 |
| 330 void Predictor::set_max_parallel_resolves(size_t max_parallel_resolves) { | 334 void Predictor::set_max_parallel_resolves(size_t max_parallel_resolves) { |
| 331 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 335 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 332 g_max_parallel_resolves = max_parallel_resolves; | 336 g_max_parallel_resolves = max_parallel_resolves; |
| 333 } | 337 } |
| 334 | 338 |
| 335 void Predictor::ShutdownOnUIThread() { | 339 void Predictor::ShutdownOnUIThread() { |
| 336 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 340 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 341 user_prefs_ = nullptr; | |
| 337 BrowserThread::PostTask( | 342 BrowserThread::PostTask( |
| 338 BrowserThread::IO, | 343 BrowserThread::IO, |
| 339 FROM_HERE, | 344 FROM_HERE, |
| 340 base::Bind(&Predictor::Shutdown, base::Unretained(this))); | 345 base::Bind(&Predictor::Shutdown, base::Unretained(this))); |
| 341 } | 346 } |
| 342 | 347 |
| 343 // ---------------------- End UI methods. ------------------------------------- | 348 // ---------------------- End UI methods. ------------------------------------- |
| 344 | 349 |
| 345 // --------------------- Start IO methods. ------------------------------------ | 350 // --------------------- Start IO methods. ------------------------------------ |
| 346 | 351 |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 669 FROM_HERE, | 674 FROM_HERE, |
| 670 base::Bind(&Predictor::ResolveList, base::Unretained(this), | 675 base::Bind(&Predictor::ResolveList, base::Unretained(this), |
| 671 urls, motivation)); | 676 urls, motivation)); |
| 672 } | 677 } |
| 673 } | 678 } |
| 674 | 679 |
| 675 //----------------------------------------------------------------------------- | 680 //----------------------------------------------------------------------------- |
| 676 // Functions to handle saving of hostnames from one session to the next, to | 681 // Functions to handle saving of hostnames from one session to the next, to |
| 677 // expedite startup times. | 682 // expedite startup times. |
| 678 | 683 |
| 679 static void SaveDnsPrefetchStateForNextStartupOnIOThread( | 684 void Predictor::SaveStateForNextStartup() { |
| 680 base::ListValue* startup_list, | 685 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 681 base::ListValue* referral_list, | 686 if (!predictor_enabled_ || !CanPreresolveAndPreconnect()) |
| 682 base::WaitableEvent* completion, | 687 return; |
| 683 Predictor* predictor) { | |
| 684 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 685 | 688 |
| 686 if (nullptr == predictor) { | 689 base::ListValue* referral_list = |
|
Bernhard Bauer
2016/07/29 14:08:14
Could you store this in a unique_ptr<>? DeepCopy()
Charlie Harrison
2016/07/29 16:50:12
Done.
| |
| 687 completion->Signal(); | 690 user_prefs_->GetList(prefs::kDnsPrefetchingHostReferralList)->DeepCopy(); |
| 688 return; | 691 base::ListValue* startup_list = |
| 689 } | 692 user_prefs_->GetList(prefs::kDnsPrefetchingStartupList)->DeepCopy(); |
| 690 predictor->SaveDnsPrefetchStateForNextStartup(startup_list, referral_list, | 693 BrowserThread::PostTaskAndReply( |
| 691 completion); | 694 BrowserThread::IO, FROM_HERE, |
| 695 base::Bind(&Predictor::WriteDnsPrefetchState, weak_factory_->GetWeakPtr(), | |
| 696 startup_list, referral_list), | |
| 697 // TODO(csharrison): This should not be unretained! | |
| 698 base::Bind(&Predictor::UpdatePrefsOnUIThread, base::Unretained(this), | |
| 699 base::Owned(startup_list), base::Owned(referral_list))); | |
| 692 } | 700 } |
| 693 | 701 |
| 694 void Predictor::SaveStateForNextStartup() { | 702 // The ListValues should be Owned by the closure created in |
| 695 if (!predictor_enabled_) | 703 // SaveStateForNextStartup. |
| 704 void Predictor::UpdatePrefsOnUIThread(base::ListValue* startup_list, | |
| 705 base::ListValue* referral_list) { | |
| 706 // Don't perform an update if the Predictor has been shut down on the UI | |
| 707 // thread. | |
| 708 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 709 if (!user_prefs_) | |
| 696 return; | 710 return; |
| 697 if (!CanPreresolveAndPreconnect()) | 711 ListPrefUpdate startup_update(user_prefs_, prefs::kDnsPrefetchingStartupList); |
| 698 return; | 712 ListPrefUpdate referral_update(user_prefs_, |
| 713 prefs::kDnsPrefetchingHostReferralList); | |
| 699 | 714 |
| 700 base::WaitableEvent completion( | 715 startup_update->Swap(startup_list); |
|
Bernhard Bauer
2016/07/29 14:08:14
You can just call user_prefs_->Set(key, value). Li
Charlie Harrison
2016/07/29 16:50:12
Much better :) done.
| |
| 701 base::WaitableEvent::ResetPolicy::MANUAL, | 716 referral_update->Swap(referral_list); |
| 702 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
| 703 | |
| 704 ListPrefUpdate update_startup_list(user_prefs_, | |
| 705 prefs::kDnsPrefetchingStartupList); | |
| 706 ListPrefUpdate update_referral_list(user_prefs_, | |
| 707 prefs::kDnsPrefetchingHostReferralList); | |
| 708 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
| 709 SaveDnsPrefetchStateForNextStartupOnIOThread(update_startup_list.Get(), | |
| 710 update_referral_list.Get(), | |
| 711 &completion, this); | |
| 712 } else { | |
| 713 bool posted = BrowserThread::PostTask( | |
| 714 BrowserThread::IO, FROM_HERE, | |
| 715 base::Bind(&SaveDnsPrefetchStateForNextStartupOnIOThread, | |
| 716 update_startup_list.Get(), update_referral_list.Get(), | |
| 717 &completion, this)); | |
| 718 | |
| 719 // TODO(jar): Synchronous waiting for the IO thread is a potential source | |
| 720 // to deadlocks and should be investigated. See http://crbug.com/78451. | |
| 721 DCHECK(posted); | |
| 722 if (posted) { | |
| 723 // http://crbug.com/124954 | |
| 724 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 725 completion.Wait(); | |
| 726 } | |
| 727 } | |
| 728 } | 717 } |
| 729 | 718 |
| 730 void Predictor::SaveDnsPrefetchStateForNextStartup( | 719 void Predictor::WriteDnsPrefetchState(base::ListValue* startup_list, |
| 731 base::ListValue* startup_list, | 720 base::ListValue* referral_list) { |
| 732 base::ListValue* referral_list, | |
| 733 base::WaitableEvent* completion) { | |
| 734 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 721 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 735 if (initial_observer_.get()) | 722 if (initial_observer_.get()) |
| 736 initial_observer_->GetInitialDnsResolutionList(startup_list); | 723 initial_observer_->GetInitialDnsResolutionList(startup_list); |
| 737 | 724 |
| 738 SerializeReferrers(referral_list); | 725 SerializeReferrers(referral_list); |
| 739 | |
| 740 completion->Signal(); | |
| 741 } | 726 } |
| 742 | 727 |
| 743 void Predictor::PreconnectUrl(const GURL& url, | 728 void Predictor::PreconnectUrl(const GURL& url, |
| 744 const GURL& first_party_for_cookies, | 729 const GURL& first_party_for_cookies, |
| 745 UrlInfo::ResolutionMotivation motivation, | 730 UrlInfo::ResolutionMotivation motivation, |
| 746 bool allow_credentials, | 731 bool allow_credentials, |
| 747 int count) { | 732 int count) { |
| 748 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 733 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 749 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 734 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 750 | 735 |
| (...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1222 } | 1207 } |
| 1223 | 1208 |
| 1224 void SimplePredictor::ShutdownOnUIThread() { | 1209 void SimplePredictor::ShutdownOnUIThread() { |
| 1225 SetShutdown(true); | 1210 SetShutdown(true); |
| 1226 } | 1211 } |
| 1227 | 1212 |
| 1228 bool SimplePredictor::CanPrefetchAndPrerender() const { return true; } | 1213 bool SimplePredictor::CanPrefetchAndPrerender() const { return true; } |
| 1229 bool SimplePredictor::CanPreresolveAndPreconnect() const { return true; } | 1214 bool SimplePredictor::CanPreresolveAndPreconnect() const { return true; } |
| 1230 | 1215 |
| 1231 } // namespace chrome_browser_net | 1216 } // namespace chrome_browser_net |
| OLD | NEW |