Chromium Code Reviews| 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" | 11 #include "base/command_line.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/metrics/sparse_histogram.h" | 15 #include "base/metrics/sparse_histogram.h" |
| 16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "base/trace_event/trace_event.h" | |
| 19 #include "chrome/browser/history/history_service_factory.h" | 20 #include "chrome/browser/history/history_service_factory.h" |
| 20 #include "chrome/browser/predictors/predictor_database.h" | 21 #include "chrome/browser/predictors/predictor_database.h" |
| 21 #include "chrome/browser/predictors/predictor_database_factory.h" | 22 #include "chrome/browser/predictors/predictor_database_factory.h" |
| 22 #include "chrome/browser/predictors/resource_prefetcher_manager.h" | 23 #include "chrome/browser/predictors/resource_prefetcher_manager.h" |
| 23 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
| 24 #include "chrome/common/chrome_switches.h" | 25 #include "chrome/common/chrome_switches.h" |
| 25 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
| 26 #include "components/history/core/browser/history_database.h" | 27 #include "components/history/core/browser/history_database.h" |
| 27 #include "components/history/core/browser/history_service.h" | 28 #include "components/history/core/browser/history_service.h" |
| 28 #include "components/mime_util/mime_util.h" | 29 #include "components/mime_util/mime_util.h" |
| (...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 579 config_.min_resource_hits_to_trigger_prefetch) | 580 config_.min_resource_hits_to_trigger_prefetch) |
| 580 continue; | 581 continue; |
| 581 | 582 |
| 582 urls->push_back(GURL(resource.resource_url())); | 583 urls->push_back(GURL(resource.resource_url())); |
| 583 } | 584 } |
| 584 | 585 |
| 585 return urls->size() > initial_size; | 586 return urls->size() > initial_size; |
| 586 } | 587 } |
| 587 | 588 |
| 588 void ResourcePrefetchPredictor::StartPrefetching(const GURL& url) { | 589 void ResourcePrefetchPredictor::StartPrefetching(const GURL& url) { |
| 590 TRACE_EVENT1("browser", "ResourcePrefetchPredictor::StartPrefetching", "url", | |
|
pasko
2016/10/14 15:59:47
traces in category "browser" generally look less r
| |
| 591 url.spec()); | |
| 589 if (!prefetch_manager_.get()) // Prefetching not enabled. | 592 if (!prefetch_manager_.get()) // Prefetching not enabled. |
| 590 return; | 593 return; |
| 591 | 594 |
| 592 std::vector<GURL> subresource_urls; | 595 std::vector<GURL> subresource_urls; |
| 593 if (!GetPrefetchData(url, &subresource_urls)) { | 596 if (!GetPrefetchData(url, &subresource_urls)) { |
| 594 // No prefetching data at host or URL level. | 597 // No prefetching data at host or URL level. |
| 595 return; | 598 return; |
| 596 } | 599 } |
| 597 | 600 |
| 598 BrowserThread::PostTask( | 601 BrowserThread::PostTask( |
| 599 BrowserThread::IO, FROM_HERE, | 602 BrowserThread::IO, FROM_HERE, |
| 600 base::Bind(&ResourcePrefetcherManager::MaybeAddPrefetch, | 603 base::Bind(&ResourcePrefetcherManager::MaybeAddPrefetch, |
| 601 prefetch_manager_, url, subresource_urls)); | 604 prefetch_manager_, url, subresource_urls)); |
| 602 } | 605 } |
| 603 | 606 |
| 604 void ResourcePrefetchPredictor::StopPrefetching(const GURL& url) { | 607 void ResourcePrefetchPredictor::StopPrefetching(const GURL& url) { |
| 608 TRACE_EVENT1("browser", "ResourcePrefetchPredictor::StopPrefetching", "url", | |
| 609 url.spec()); | |
| 605 if (!prefetch_manager_.get()) // Not enabled. | 610 if (!prefetch_manager_.get()) // Not enabled. |
| 606 return; | 611 return; |
| 607 | 612 |
| 608 BrowserThread::PostTask( | 613 BrowserThread::PostTask( |
| 609 BrowserThread::IO, FROM_HERE, | 614 BrowserThread::IO, FROM_HERE, |
| 610 base::Bind(&ResourcePrefetcherManager::MaybeRemovePrefetch, | 615 base::Bind(&ResourcePrefetcherManager::MaybeRemovePrefetch, |
| 611 prefetch_manager_, url)); | 616 prefetch_manager_, url)); |
| 612 } | 617 } |
| 613 | 618 |
| 614 void ResourcePrefetchPredictor::StartInitialization() { | 619 void ResourcePrefetchPredictor::StartInitialization() { |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 840 } | 845 } |
| 841 | 846 |
| 842 void ResourcePrefetchPredictor::LearnNavigation( | 847 void ResourcePrefetchPredictor::LearnNavigation( |
| 843 const std::string& key, | 848 const std::string& key, |
| 844 PrefetchKeyType key_type, | 849 PrefetchKeyType key_type, |
| 845 const std::vector<URLRequestSummary>& new_resources, | 850 const std::vector<URLRequestSummary>& new_resources, |
| 846 size_t max_data_map_size, | 851 size_t max_data_map_size, |
| 847 PrefetchDataMap* data_map, | 852 PrefetchDataMap* data_map, |
| 848 const std::string& key_before_redirects, | 853 const std::string& key_before_redirects, |
| 849 RedirectDataMap* redirect_map) { | 854 RedirectDataMap* redirect_map) { |
| 855 TRACE_EVENT1("browser", "ResourcePrefetchPredictor::LearnNavigation", "key", | |
| 856 key); | |
| 850 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 857 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 851 | 858 |
| 852 // If the primary key is too long reject it. | 859 // If the primary key is too long reject it. |
| 853 if (key.length() > ResourcePrefetchPredictorTables::kMaxStringLength) | 860 if (key.length() > ResourcePrefetchPredictorTables::kMaxStringLength) |
| 854 return; | 861 return; |
| 855 | 862 |
| 856 PrefetchDataMap::iterator cache_entry = data_map->find(key); | 863 PrefetchDataMap::iterator cache_entry = data_map->find(key); |
| 857 if (cache_entry == data_map->end()) { | 864 if (cache_entry == data_map->end()) { |
| 858 // If the table is full, delete an entry. | 865 // If the table is full, delete an entry. |
| 859 if (data_map->size() >= max_data_map_size) | 866 if (data_map->size() >= max_data_map_size) |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1117 // HistoryService is already loaded. Continue with Initialization. | 1124 // HistoryService is already loaded. Continue with Initialization. |
| 1118 OnHistoryAndCacheLoaded(); | 1125 OnHistoryAndCacheLoaded(); |
| 1119 return; | 1126 return; |
| 1120 } | 1127 } |
| 1121 DCHECK(!history_service_observer_.IsObserving(history_service)); | 1128 DCHECK(!history_service_observer_.IsObserving(history_service)); |
| 1122 history_service_observer_.Add(history_service); | 1129 history_service_observer_.Add(history_service); |
| 1123 return; | 1130 return; |
| 1124 } | 1131 } |
| 1125 | 1132 |
| 1126 } // namespace predictors | 1133 } // namespace predictors |
| OLD | NEW |