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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 return resolver_.Resolve( | 118 return resolver_.Resolve( |
119 resolve_info, | 119 resolve_info, |
120 net::DEFAULT_PRIORITY, | 120 net::DEFAULT_PRIORITY, |
121 &addresses_, | 121 &addresses_, |
122 base::Bind(&LookupRequest::OnLookupFinished, base::Unretained(this)), | 122 base::Bind(&LookupRequest::OnLookupFinished, base::Unretained(this)), |
123 net::BoundNetLog()); | 123 net::BoundNetLog()); |
124 } | 124 } |
125 | 125 |
126 private: | 126 private: |
127 void OnLookupFinished(int result) { | 127 void OnLookupFinished(int result) { |
128 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed. | |
129 tracked_objects::ScopedTracker tracking_profile( | |
130 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
131 "436634 Predictor::LookupRequest::OnLookupFinished")); | |
132 | |
133 predictor_->OnLookupFinished(this, url_, result == net::OK); | 128 predictor_->OnLookupFinished(this, url_, result == net::OK); |
134 } | 129 } |
135 | 130 |
136 Predictor* predictor_; // The predictor which started us. | 131 Predictor* predictor_; // The predictor which started us. |
137 | 132 |
138 const GURL url_; // Hostname to resolve. | 133 const GURL url_; // Hostname to resolve. |
139 net::SingleRequestHostResolver resolver_; | 134 net::SingleRequestHostResolver resolver_; |
140 net::AddressList addresses_; | 135 net::AddressList addresses_; |
141 | 136 |
142 DISALLOW_COPY_AND_ASSIGN(LookupRequest); | 137 DISALLOW_COPY_AND_ASSIGN(LookupRequest); |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 // Put back in the names being worked on. | 431 // Put back in the names being worked on. |
437 for (Results::iterator it = assignees.begin(); assignees.end() != it; ++it) { | 432 for (Results::iterator it = assignees.begin(); assignees.end() != it; ++it) { |
438 DCHECK(it->second.is_marked_to_delete()); | 433 DCHECK(it->second.is_marked_to_delete()); |
439 results_[it->first] = it->second; | 434 results_[it->first] = it->second; |
440 } | 435 } |
441 } | 436 } |
442 | 437 |
443 // Overloaded Resolve() to take a vector of names. | 438 // Overloaded Resolve() to take a vector of names. |
444 void Predictor::ResolveList(const UrlList& urls, | 439 void Predictor::ResolveList(const UrlList& urls, |
445 UrlInfo::ResolutionMotivation motivation) { | 440 UrlInfo::ResolutionMotivation motivation) { |
446 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed. | |
447 tracked_objects::ScopedTracker tracking_profile( | |
448 FROM_HERE_WITH_EXPLICIT_FUNCTION("436671 Predictor::ResolveList")); | |
449 | |
450 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 441 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
451 | 442 |
452 for (UrlList::const_iterator it = urls.begin(); it < urls.end(); ++it) { | 443 for (UrlList::const_iterator it = urls.begin(); it < urls.end(); ++it) { |
453 AppendToResolutionQueue(*it, motivation); | 444 AppendToResolutionQueue(*it, motivation); |
454 } | 445 } |
455 } | 446 } |
456 | 447 |
457 // Basic Resolve() takes an invidual name, and adds it | 448 // Basic Resolve() takes an invidual name, and adds it |
458 // to the queue. | 449 // to the queue. |
459 void Predictor::Resolve(const GURL& url, | 450 void Predictor::Resolve(const GURL& url, |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
682 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 673 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
683 if (initial_observer_.get()) | 674 if (initial_observer_.get()) |
684 initial_observer_->DiscardInitialNavigationHistory(); | 675 initial_observer_->DiscardInitialNavigationHistory(); |
685 } | 676 } |
686 | 677 |
687 void Predictor::FinalizeInitializationOnIOThread( | 678 void Predictor::FinalizeInitializationOnIOThread( |
688 const UrlList& startup_urls, | 679 const UrlList& startup_urls, |
689 base::ListValue* referral_list, | 680 base::ListValue* referral_list, |
690 IOThread* io_thread, | 681 IOThread* io_thread, |
691 ProfileIOData* profile_io_data) { | 682 ProfileIOData* profile_io_data) { |
692 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed. | |
693 tracked_objects::ScopedTracker tracking_profile1( | |
694 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
695 "436671 Predictor::FinalizeInitializationOnIOThread1")); | |
696 | |
697 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 683 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
698 | 684 |
699 profile_io_data_ = profile_io_data; | 685 profile_io_data_ = profile_io_data; |
700 initial_observer_.reset(new InitialObserver()); | 686 initial_observer_.reset(new InitialObserver()); |
701 host_resolver_ = io_thread->globals()->host_resolver.get(); | 687 host_resolver_ = io_thread->globals()->host_resolver.get(); |
702 | 688 |
703 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed. | |
704 tracked_objects::ScopedTracker tracking_profile2( | |
705 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
706 "436671 Predictor::FinalizeInitializationOnIOThread2")); | |
707 | |
708 net::URLRequestContext* context = | 689 net::URLRequestContext* context = |
709 url_request_context_getter_->GetURLRequestContext(); | 690 url_request_context_getter_->GetURLRequestContext(); |
710 transport_security_state_ = context->transport_security_state(); | 691 transport_security_state_ = context->transport_security_state(); |
711 ssl_config_service_ = context->ssl_config_service(); | 692 ssl_config_service_ = context->ssl_config_service(); |
712 proxy_service_ = context->proxy_service(); | 693 proxy_service_ = context->proxy_service(); |
713 | 694 |
714 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed. | |
715 tracked_objects::ScopedTracker tracking_profile3( | |
716 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
717 "436671 Predictor::FinalizeInitializationOnIOThread3")); | |
718 | |
719 // base::WeakPtrFactory instances need to be created and destroyed | 695 // base::WeakPtrFactory instances need to be created and destroyed |
720 // on the same thread. The predictor lives on the IO thread and will die | 696 // on the same thread. The predictor lives on the IO thread and will die |
721 // from there so now that we're on the IO thread we need to properly | 697 // from there so now that we're on the IO thread we need to properly |
722 // initialize the base::WeakPtrFactory. | 698 // initialize the base::WeakPtrFactory. |
723 // TODO(groby): Check if WeakPtrFactory has the same constraint. | 699 // TODO(groby): Check if WeakPtrFactory has the same constraint. |
724 weak_factory_.reset(new base::WeakPtrFactory<Predictor>(this)); | 700 weak_factory_.reset(new base::WeakPtrFactory<Predictor>(this)); |
725 | 701 |
726 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed. | |
727 tracked_objects::ScopedTracker tracking_profile4( | |
728 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
729 "436671 Predictor::FinalizeInitializationOnIOThread4")); | |
730 | |
731 // Prefetch these hostnames on startup. | 702 // Prefetch these hostnames on startup. |
732 DnsPrefetchMotivatedList(startup_urls, UrlInfo::STARTUP_LIST_MOTIVATED); | 703 DnsPrefetchMotivatedList(startup_urls, UrlInfo::STARTUP_LIST_MOTIVATED); |
733 | 704 |
734 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed. | |
735 tracked_objects::ScopedTracker tracking_profile5( | |
736 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
737 "436671 Predictor::FinalizeInitializationOnIOThread5")); | |
738 | |
739 DeserializeReferrersThenDelete(referral_list); | 705 DeserializeReferrersThenDelete(referral_list); |
740 } | 706 } |
741 | 707 |
742 //----------------------------------------------------------------------------- | 708 //----------------------------------------------------------------------------- |
743 // This section intermingles prefetch results with actual browser HTTP | 709 // This section intermingles prefetch results with actual browser HTTP |
744 // network activity. It supports calculating of the benefit of a prefetch, as | 710 // network activity. It supports calculating of the benefit of a prefetch, as |
745 // well as recording what prefetched hostname resolutions might be potentially | 711 // well as recording what prefetched hostname resolutions might be potentially |
746 // helpful during the next chrome-startup. | 712 // helpful during the next chrome-startup. |
747 //----------------------------------------------------------------------------- | 713 //----------------------------------------------------------------------------- |
748 | 714 |
(...skipping 20 matching lines...) Expand all Loading... |
769 urls.push_back(GURL("http://" + *it + ":80")); | 735 urls.push_back(GURL("http://" + *it + ":80")); |
770 } | 736 } |
771 | 737 |
772 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 738 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
773 DnsPrefetchMotivatedList(urls, UrlInfo::PAGE_SCAN_MOTIVATED); | 739 DnsPrefetchMotivatedList(urls, UrlInfo::PAGE_SCAN_MOTIVATED); |
774 } | 740 } |
775 | 741 |
776 void Predictor::DnsPrefetchMotivatedList( | 742 void Predictor::DnsPrefetchMotivatedList( |
777 const UrlList& urls, | 743 const UrlList& urls, |
778 UrlInfo::ResolutionMotivation motivation) { | 744 UrlInfo::ResolutionMotivation motivation) { |
779 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed. | |
780 tracked_objects::ScopedTracker tracking_profile( | |
781 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
782 "436671 Predictor::DnsPrefetchMotivatedList")); | |
783 | |
784 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 745 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
785 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 746 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
786 if (!predictor_enabled_) | 747 if (!predictor_enabled_) |
787 return; | 748 return; |
788 if (!CanPreresolveAndPreconnect()) | 749 if (!CanPreresolveAndPreconnect()) |
789 return; | 750 return; |
790 | 751 |
791 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 752 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
792 ResolveList(urls, motivation); | 753 ResolveList(urls, motivation); |
793 } else { | 754 } else { |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1052 net::ProxyInfo info; | 1013 net::ProxyInfo info; |
1053 bool synchronous_success = proxy_service_->TryResolveProxySynchronously( | 1014 bool synchronous_success = proxy_service_->TryResolveProxySynchronously( |
1054 url, net::LOAD_NORMAL, &info, NULL, net::BoundNetLog()); | 1015 url, net::LOAD_NORMAL, &info, NULL, net::BoundNetLog()); |
1055 | 1016 |
1056 return synchronous_success && !info.is_direct(); | 1017 return synchronous_success && !info.is_direct(); |
1057 } | 1018 } |
1058 | 1019 |
1059 UrlInfo* Predictor::AppendToResolutionQueue( | 1020 UrlInfo* Predictor::AppendToResolutionQueue( |
1060 const GURL& url, | 1021 const GURL& url, |
1061 UrlInfo::ResolutionMotivation motivation) { | 1022 UrlInfo::ResolutionMotivation motivation) { |
1062 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed. | |
1063 tracked_objects::ScopedTracker tracking_profile1( | |
1064 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
1065 "436671 Predictor::AppendToResolutionQueue1")); | |
1066 | |
1067 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 1023 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
1068 DCHECK(url.has_host()); | 1024 DCHECK(url.has_host()); |
1069 | 1025 |
1070 if (shutdown_) | 1026 if (shutdown_) |
1071 return NULL; | 1027 return NULL; |
1072 | 1028 |
1073 UrlInfo* info = &results_[url]; | 1029 UrlInfo* info = &results_[url]; |
1074 info->SetUrl(url); // Initialize or DCHECK. | 1030 info->SetUrl(url); // Initialize or DCHECK. |
1075 // TODO(jar): I need to discard names that have long since expired. | 1031 // TODO(jar): I need to discard names that have long since expired. |
1076 // Currently we only add to the domain map :-/ | 1032 // Currently we only add to the domain map :-/ |
1077 | 1033 |
1078 DCHECK(info->HasUrl(url)); | 1034 DCHECK(info->HasUrl(url)); |
1079 | 1035 |
1080 if (!info->NeedsDnsUpdate()) { | 1036 if (!info->NeedsDnsUpdate()) { |
1081 info->DLogResultsStats("DNS PrefetchNotUpdated"); | 1037 info->DLogResultsStats("DNS PrefetchNotUpdated"); |
1082 return NULL; | 1038 return NULL; |
1083 } | 1039 } |
1084 | 1040 |
1085 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed. | 1041 bool would_likely_proxy; |
1086 tracked_objects::ScopedTracker tracking_profile2( | 1042 { |
1087 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 1043 // TODO(ttuttle): Remove ScopedTracker below once crbug.com/436671 is fixed. |
1088 "436671 Predictor::AppendToResolutionQueue2")); | 1044 tracked_objects::ScopedTracker tracking_profile( |
| 1045 FROM_HERE_WITH_EXPLICIT_FUNCTION("436671 WouldLikelyProxyURL()")); |
| 1046 would_likely_proxy = WouldLikelyProxyURL(url); |
| 1047 } |
1089 | 1048 |
1090 if (WouldLikelyProxyURL(url)) { | 1049 if (would_likely_proxy) { |
1091 info->DLogResultsStats("DNS PrefetchForProxiedRequest"); | 1050 info->DLogResultsStats("DNS PrefetchForProxiedRequest"); |
1092 return NULL; | 1051 return NULL; |
1093 } | 1052 } |
1094 | 1053 |
1095 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed. | |
1096 tracked_objects::ScopedTracker tracking_profile3( | |
1097 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
1098 "436671 Predictor::AppendToResolutionQueue3")); | |
1099 | |
1100 info->SetQueuedState(motivation); | 1054 info->SetQueuedState(motivation); |
1101 work_queue_.Push(url, motivation); | 1055 work_queue_.Push(url, motivation); |
1102 | 1056 |
1103 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed. | |
1104 tracked_objects::ScopedTracker tracking_profile4( | |
1105 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
1106 "436671 Predictor::AppendToResolutionQueue4")); | |
1107 | |
1108 StartSomeQueuedResolutions(); | 1057 StartSomeQueuedResolutions(); |
1109 return info; | 1058 return info; |
1110 } | 1059 } |
1111 | 1060 |
1112 bool Predictor::CongestionControlPerformed(UrlInfo* info) { | 1061 bool Predictor::CongestionControlPerformed(UrlInfo* info) { |
1113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 1062 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
1114 // Note: queue_duration is ONLY valid after we go to assigned state. | 1063 // Note: queue_duration is ONLY valid after we go to assigned state. |
1115 if (info->queue_duration() < max_dns_queue_delay_) | 1064 if (info->queue_duration() < max_dns_queue_delay_) |
1116 return false; | 1065 return false; |
1117 // We need to discard all entries in our queue, as we're keeping them waiting | 1066 // We need to discard all entries in our queue, as we're keeping them waiting |
1118 // too long. By doing this, we'll have a chance to quickly service urgent | 1067 // too long. By doing this, we'll have a chance to quickly service urgent |
1119 // resolutions, and not have a bogged down system. | 1068 // resolutions, and not have a bogged down system. |
1120 while (true) { | 1069 while (true) { |
1121 info->RemoveFromQueue(); | 1070 info->RemoveFromQueue(); |
1122 if (work_queue_.IsEmpty()) | 1071 if (work_queue_.IsEmpty()) |
1123 break; | 1072 break; |
1124 info = &results_[work_queue_.Pop()]; | 1073 info = &results_[work_queue_.Pop()]; |
1125 info->SetAssignedState(); | 1074 info->SetAssignedState(); |
1126 } | 1075 } |
1127 return true; | 1076 return true; |
1128 } | 1077 } |
1129 | 1078 |
1130 void Predictor::StartSomeQueuedResolutions() { | 1079 void Predictor::StartSomeQueuedResolutions() { |
1131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 1080 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
1132 | 1081 |
1133 while (!work_queue_.IsEmpty() && | 1082 while (!work_queue_.IsEmpty() && |
1134 pending_lookups_.size() < max_concurrent_dns_lookups_) { | 1083 pending_lookups_.size() < max_concurrent_dns_lookups_) { |
1135 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed. | |
1136 tracked_objects::ScopedTracker tracking_profile1( | |
1137 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
1138 "436671 Predictor::StartSomeQueuedResolutions1")); | |
1139 | |
1140 const GURL url(work_queue_.Pop()); | 1084 const GURL url(work_queue_.Pop()); |
1141 UrlInfo* info = &results_[url]; | 1085 UrlInfo* info = &results_[url]; |
1142 DCHECK(info->HasUrl(url)); | 1086 DCHECK(info->HasUrl(url)); |
1143 info->SetAssignedState(); | 1087 info->SetAssignedState(); |
1144 | 1088 |
1145 if (CongestionControlPerformed(info)) { | 1089 if (CongestionControlPerformed(info)) { |
1146 DCHECK(work_queue_.IsEmpty()); | 1090 DCHECK(work_queue_.IsEmpty()); |
1147 return; | 1091 return; |
1148 } | 1092 } |
1149 | 1093 |
1150 LookupRequest* request = new LookupRequest(this, host_resolver_, url); | 1094 LookupRequest* request = new LookupRequest(this, host_resolver_, url); |
1151 | 1095 |
1152 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed. | 1096 int status; |
1153 tracked_objects::ScopedTracker tracking_profile2( | 1097 { |
1154 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 1098 // TODO(ttuttle): Remove ScopedTracker below once crbug.com/436671 is |
1155 "436671 Predictor::StartSomeQueuedResolutions2")); | 1099 // fixed. |
1156 | 1100 tracked_objects::ScopedTracker tracking_profile( |
1157 int status = request->Start(); | 1101 FROM_HERE_WITH_EXPLICIT_FUNCTION("436671 LookupRequest::Start()")); |
1158 | 1102 status = request->Start(); |
1159 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed. | 1103 } |
1160 tracked_objects::ScopedTracker tracking_profile3( | |
1161 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
1162 "436671 Predictor::StartSomeQueuedResolutions3")); | |
1163 | 1104 |
1164 if (status == net::ERR_IO_PENDING) { | 1105 if (status == net::ERR_IO_PENDING) { |
1165 // Will complete asynchronously. | 1106 // Will complete asynchronously. |
1166 pending_lookups_.insert(request); | 1107 pending_lookups_.insert(request); |
1167 peak_pending_lookups_ = std::max(peak_pending_lookups_, | 1108 peak_pending_lookups_ = std::max(peak_pending_lookups_, |
1168 pending_lookups_.size()); | 1109 pending_lookups_.size()); |
1169 } else { | 1110 } else { |
1170 // Completed synchronously (was already cached by HostResolver), or else | 1111 // Completed synchronously (was already cached by HostResolver), or else |
1171 // there was (equivalently) some network error that prevents us from | 1112 // there was (equivalently) some network error that prevents us from |
1172 // finding the name. Status net::OK means it was "found." | 1113 // finding the name. Status net::OK means it was "found." |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1378 } | 1319 } |
1379 | 1320 |
1380 void SimplePredictor::ShutdownOnUIThread() { | 1321 void SimplePredictor::ShutdownOnUIThread() { |
1381 SetShutdown(true); | 1322 SetShutdown(true); |
1382 } | 1323 } |
1383 | 1324 |
1384 bool SimplePredictor::CanPrefetchAndPrerender() const { return true; } | 1325 bool SimplePredictor::CanPrefetchAndPrerender() const { return true; } |
1385 bool SimplePredictor::CanPreresolveAndPreconnect() const { return true; } | 1326 bool SimplePredictor::CanPreresolveAndPreconnect() const { return true; } |
1386 | 1327 |
1387 } // namespace chrome_browser_net | 1328 } // namespace chrome_browser_net |
OLD | NEW |