| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/predictors/resource_prefetch_predictor.h" | 5 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/command_line.h" | |
| 12 #include "base/macros.h" | 11 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 14 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/metrics/sparse_histogram.h" | |
| 16 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
| 17 #include "base/strings/string_number_conversions.h" | |
| 18 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 19 #include "base/trace_event/trace_event.h" | 16 #include "base/trace_event/trace_event.h" |
| 20 #include "chrome/browser/history/history_service_factory.h" | 17 #include "chrome/browser/history/history_service_factory.h" |
| 21 #include "chrome/browser/predictors/predictor_database.h" | 18 #include "chrome/browser/predictors/predictor_database.h" |
| 22 #include "chrome/browser/predictors/predictor_database_factory.h" | 19 #include "chrome/browser/predictors/predictor_database_factory.h" |
| 23 #include "chrome/browser/predictors/resource_prefetcher_manager.h" | 20 #include "chrome/browser/predictors/resource_prefetcher_manager.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/common/chrome_switches.h" | |
| 26 #include "chrome/common/url_constants.h" | |
| 27 #include "components/history/core/browser/history_database.h" | 22 #include "components/history/core/browser/history_database.h" |
| 28 #include "components/history/core/browser/history_service.h" | 23 #include "components/history/core/browser/history_service.h" |
| 29 #include "components/mime_util/mime_util.h" | 24 #include "components/mime_util/mime_util.h" |
| 30 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
| 31 #include "content/public/browser/navigation_controller.h" | |
| 32 #include "content/public/browser/resource_request_info.h" | 26 #include "content/public/browser/resource_request_info.h" |
| 33 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 34 #include "net/base/mime_util.h" | |
| 35 #include "net/base/network_change_notifier.h" | |
| 36 #include "net/http/http_response_headers.h" | 28 #include "net/http/http_response_headers.h" |
| 37 #include "net/url_request/url_request.h" | 29 #include "net/url_request/url_request.h" |
| 38 #include "net/url_request/url_request_context_getter.h" | |
| 39 | 30 |
| 40 using content::BrowserThread; | 31 using content::BrowserThread; |
| 41 | 32 |
| 42 namespace predictors { | 33 namespace predictors { |
| 43 | 34 |
| 44 namespace { | 35 namespace { |
| 45 | 36 |
| 46 // Sorted by decreasing likelihood according to HTTP archive. | 37 // Sorted by decreasing likelihood according to HTTP archive. |
| 47 const char* kFontMimeTypes[] = {"font/woff2", | 38 const char* kFontMimeTypes[] = {"font/woff2", |
| 48 "application/x-font-woff", | 39 "application/x-font-woff", |
| (...skipping 726 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 775 | 766 |
| 776 bool ResourcePrefetchPredictor::GetPrefetchData( | 767 bool ResourcePrefetchPredictor::GetPrefetchData( |
| 777 const GURL& main_frame_url, | 768 const GURL& main_frame_url, |
| 778 ResourcePrefetchPredictor::Prediction* prediction) const { | 769 ResourcePrefetchPredictor::Prediction* prediction) const { |
| 779 std::vector<GURL>* urls = | 770 std::vector<GURL>* urls = |
| 780 prediction ? &prediction->subresource_urls : nullptr; | 771 prediction ? &prediction->subresource_urls : nullptr; |
| 781 DCHECK(!urls || urls->empty()); | 772 DCHECK(!urls || urls->empty()); |
| 782 | 773 |
| 783 // Fetch URLs based on a redirect endpoint for URL/host first. | 774 // Fetch URLs based on a redirect endpoint for URL/host first. |
| 784 std::string redirect_endpoint; | 775 std::string redirect_endpoint; |
| 785 if (GetRedirectEndpoint(main_frame_url.spec(), *url_redirect_table_cache_, | 776 if (config_.is_url_learning_enabled && |
| 777 GetRedirectEndpoint(main_frame_url.spec(), *url_redirect_table_cache_, |
| 786 &redirect_endpoint) && | 778 &redirect_endpoint) && |
| 787 PopulatePrefetcherRequest(redirect_endpoint, *url_table_cache_, urls)) { | 779 PopulatePrefetcherRequest(redirect_endpoint, *url_table_cache_, urls)) { |
| 788 if (prediction) { | 780 if (prediction) { |
| 789 prediction->is_host = false; | 781 prediction->is_host = false; |
| 790 prediction->is_redirected = true; | 782 prediction->is_redirected = true; |
| 791 prediction->main_frame_key = redirect_endpoint; | 783 prediction->main_frame_key = redirect_endpoint; |
| 792 } | 784 } |
| 793 return true; | 785 return true; |
| 794 } | 786 } |
| 795 | 787 |
| 796 if (GetRedirectEndpoint(main_frame_url.host(), *host_redirect_table_cache_, | 788 if (GetRedirectEndpoint(main_frame_url.host(), *host_redirect_table_cache_, |
| 797 &redirect_endpoint) && | 789 &redirect_endpoint) && |
| 798 PopulatePrefetcherRequest(redirect_endpoint, *host_table_cache_, urls)) { | 790 PopulatePrefetcherRequest(redirect_endpoint, *host_table_cache_, urls)) { |
| 799 if (prediction) { | 791 if (prediction) { |
| 800 prediction->is_host = true; | 792 prediction->is_host = true; |
| 801 prediction->is_redirected = true; | 793 prediction->is_redirected = true; |
| 802 prediction->main_frame_key = redirect_endpoint; | 794 prediction->main_frame_key = redirect_endpoint; |
| 803 } | 795 } |
| 804 return true; | 796 return true; |
| 805 } | 797 } |
| 806 | 798 |
| 807 // Fallback to fetching URLs based on the incoming URL/host. | 799 // Fallback to fetching URLs based on the incoming URL/host. |
| 808 if (PopulatePrefetcherRequest(main_frame_url.spec(), *url_table_cache_, | 800 if (config_.is_url_learning_enabled && |
| 801 PopulatePrefetcherRequest(main_frame_url.spec(), *url_table_cache_, |
| 809 urls)) { | 802 urls)) { |
| 810 if (prediction) { | 803 if (prediction) { |
| 811 prediction->is_host = false; | 804 prediction->is_host = false; |
| 812 prediction->is_redirected = false; | 805 prediction->is_redirected = false; |
| 813 prediction->main_frame_key = main_frame_url.spec(); | 806 prediction->main_frame_key = main_frame_url.spec(); |
| 814 } | 807 } |
| 815 return true; | 808 return true; |
| 816 } | 809 } |
| 817 | 810 |
| 818 if (PopulatePrefetcherRequest(main_frame_url.host(), *host_table_cache_, | 811 if (PopulatePrefetcherRequest(main_frame_url.host(), *host_table_cache_, |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1032 void ResourcePrefetchPredictor::OnVisitCountLookup( | 1025 void ResourcePrefetchPredictor::OnVisitCountLookup( |
| 1033 size_t url_visit_count, | 1026 size_t url_visit_count, |
| 1034 const PageRequestSummary& summary) { | 1027 const PageRequestSummary& summary) { |
| 1035 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1028 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1036 | 1029 |
| 1037 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HistoryVisitCountForUrl", | 1030 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HistoryVisitCountForUrl", |
| 1038 url_visit_count); | 1031 url_visit_count); |
| 1039 | 1032 |
| 1040 // TODO(alexilin): make only one request to DB thread. | 1033 // TODO(alexilin): make only one request to DB thread. |
| 1041 | 1034 |
| 1042 // URL level data - merge only if we already saved the data, or it | 1035 if (config_.is_url_learning_enabled) { |
| 1043 // meets the cutoff requirement. | 1036 // URL level data - merge only if we already saved the data, or it |
| 1044 const std::string url_spec = summary.main_frame_url.spec(); | 1037 // meets the cutoff requirement. |
| 1045 bool already_tracking = url_table_cache_->find(url_spec) != | 1038 const std::string url_spec = summary.main_frame_url.spec(); |
| 1046 url_table_cache_->end(); | 1039 bool already_tracking = |
| 1047 bool should_track_url = | 1040 url_table_cache_->find(url_spec) != url_table_cache_->end(); |
| 1048 already_tracking || (url_visit_count >= config_.min_url_visit_count); | 1041 bool should_track_url = |
| 1042 already_tracking || (url_visit_count >= config_.min_url_visit_count); |
| 1049 | 1043 |
| 1050 if (should_track_url) { | 1044 if (should_track_url) { |
| 1051 LearnNavigation(url_spec, PREFETCH_KEY_TYPE_URL, | 1045 LearnNavigation(url_spec, PREFETCH_KEY_TYPE_URL, |
| 1052 summary.subresource_requests, config_.max_urls_to_track, | 1046 summary.subresource_requests, config_.max_urls_to_track, |
| 1053 url_table_cache_.get(), summary.initial_url.spec(), | 1047 url_table_cache_.get(), summary.initial_url.spec(), |
| 1054 url_redirect_table_cache_.get()); | 1048 url_redirect_table_cache_.get()); |
| 1049 } |
| 1055 } | 1050 } |
| 1056 | 1051 |
| 1057 // Host level data - no cutoff, always learn the navigation if enabled. | 1052 // Host level data - no cutoff, always learn the navigation if enabled. |
| 1058 const std::string host = summary.main_frame_url.host(); | 1053 const std::string host = summary.main_frame_url.host(); |
| 1059 LearnNavigation(host, PREFETCH_KEY_TYPE_HOST, summary.subresource_requests, | 1054 LearnNavigation(host, PREFETCH_KEY_TYPE_HOST, summary.subresource_requests, |
| 1060 config_.max_hosts_to_track, host_table_cache_.get(), | 1055 config_.max_hosts_to_track, host_table_cache_.get(), |
| 1061 summary.initial_url.host(), host_redirect_table_cache_.get()); | 1056 summary.initial_url.host(), host_redirect_table_cache_.get()); |
| 1062 | 1057 |
| 1063 if (observer_) | 1058 if (observer_) |
| 1064 observer_->OnNavigationLearned(url_visit_count, summary); | 1059 observer_->OnNavigationLearned(url_visit_count, summary); |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1393 TestObserver::~TestObserver() { | 1388 TestObserver::~TestObserver() { |
| 1394 predictor_->SetObserverForTesting(nullptr); | 1389 predictor_->SetObserverForTesting(nullptr); |
| 1395 } | 1390 } |
| 1396 | 1391 |
| 1397 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) | 1392 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) |
| 1398 : predictor_(predictor) { | 1393 : predictor_(predictor) { |
| 1399 predictor_->SetObserverForTesting(this); | 1394 predictor_->SetObserverForTesting(this); |
| 1400 } | 1395 } |
| 1401 | 1396 |
| 1402 } // namespace predictors | 1397 } // namespace predictors |
| OLD | NEW |