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 |
| (...skipping 960 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 971 | 971 |
| 972 // Don't prefetch resource if it has false bit in any of the following | 972 // Don't prefetch resource if it has false bit in any of the following |
| 973 // bitsets. All bits assumed to be true if an optional has no value. | 973 // bitsets. All bits assumed to be true if an optional has no value. |
| 974 base::Optional<std::vector<bool>> not_unused = | 974 base::Optional<std::vector<bool>> not_unused = |
| 975 precache::GetResourceBitset(manifest, internal::kUnusedRemovedExperiment); | 975 precache::GetResourceBitset(manifest, internal::kUnusedRemovedExperiment); |
| 976 base::Optional<std::vector<bool>> not_versioned = precache::GetResourceBitset( | 976 base::Optional<std::vector<bool>> not_versioned = precache::GetResourceBitset( |
| 977 manifest, internal::kVersionedRemovedExperiment); | 977 manifest, internal::kVersionedRemovedExperiment); |
| 978 base::Optional<std::vector<bool>> not_no_store = precache::GetResourceBitset( | 978 base::Optional<std::vector<bool>> not_no_store = precache::GetResourceBitset( |
| 979 manifest, internal::kNoStoreRemovedExperiment); | 979 manifest, internal::kNoStoreRemovedExperiment); |
| 980 | 980 |
| 981 std::vector<const precache::PrecacheResource*> filtered_resources; | |
| 982 | |
| 981 bool has_prefetchable_resource = false; | 983 bool has_prefetchable_resource = false; |
| 982 for (int i = 0; i < manifest.resource_size(); ++i) { | 984 for (int i = 0; i < manifest.resource_size(); ++i) { |
| 983 const precache::PrecacheResource& resource = manifest.resource(i); | 985 const precache::PrecacheResource& resource = manifest.resource(i); |
| 984 if (resource.weight_ratio() > kMinWeight && | 986 if (resource.weight_ratio() > kMinWeight && |
| 985 (!not_unused.has_value() || not_unused.value()[i]) && | 987 (!not_unused.has_value() || not_unused.value()[i]) && |
| 986 (!not_versioned.has_value() || not_versioned.value()[i]) && | 988 (!not_versioned.has_value() || not_versioned.value()[i]) && |
| 987 (!not_no_store.has_value() || not_no_store.value()[i])) { | 989 (!not_no_store.has_value() || not_no_store.value()[i])) { |
| 988 has_prefetchable_resource = true; | 990 has_prefetchable_resource = true; |
| 989 if (urls) | 991 if (urls) |
| 990 urls->emplace_back(resource.url()); | 992 filtered_resources.push_back(&resource); |
| 991 } | 993 } |
| 992 } | 994 } |
| 993 | 995 |
| 996 if (urls) { | |
| 997 std::sort( | |
| 998 filtered_resources.begin(), filtered_resources.end(), | |
| 999 [](const precache::PrecacheResource* x, | |
| 1000 const precache::PrecacheResource* y) { | |
| 1001 return ResourcePrefetchPredictorTables::ComputePrecacheResourceScore( | |
|
Benoit L
2017/04/25 08:59:21
nit: use a local "using" to make this a bit easier
alexilin
2017/04/25 13:04:25
C++ doesn't have using declaration for static clas
Benoit L
2017/04/25 15:12:39
Ah, too bad. Let's keep it simple then.
alexilin
2017/04/25 17:37:05
Acknowledged.
| |
| 1002 *x) > | |
| 1003 ResourcePrefetchPredictorTables::ComputePrecacheResourceScore( | |
| 1004 *y); | |
| 1005 }); | |
| 1006 for (auto* resource : filtered_resources) | |
| 1007 urls->emplace_back(resource->url()); | |
| 1008 } | |
| 1009 | |
| 994 return has_prefetchable_resource; | 1010 return has_prefetchable_resource; |
| 995 } | 1011 } |
| 996 | 1012 |
| 997 void ResourcePrefetchPredictor::CreateCaches( | 1013 void ResourcePrefetchPredictor::CreateCaches( |
| 998 std::unique_ptr<PrefetchDataMap> url_data_map, | 1014 std::unique_ptr<PrefetchDataMap> url_data_map, |
| 999 std::unique_ptr<PrefetchDataMap> host_data_map, | 1015 std::unique_ptr<PrefetchDataMap> host_data_map, |
| 1000 std::unique_ptr<RedirectDataMap> url_redirect_data_map, | 1016 std::unique_ptr<RedirectDataMap> url_redirect_data_map, |
| 1001 std::unique_ptr<RedirectDataMap> host_redirect_data_map, | 1017 std::unique_ptr<RedirectDataMap> host_redirect_data_map, |
| 1002 std::unique_ptr<ManifestDataMap> manifest_data_map, | 1018 std::unique_ptr<ManifestDataMap> manifest_data_map, |
| 1003 std::unique_ptr<OriginDataMap> origin_data_map) { | 1019 std::unique_ptr<OriginDataMap> origin_data_map) { |
| (...skipping 711 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1715 // The manifest is too large to store. | 1731 // The manifest is too large to store. |
| 1716 if (host.length() > ResourcePrefetchPredictorTables::kMaxStringLength || | 1732 if (host.length() > ResourcePrefetchPredictorTables::kMaxStringLength || |
| 1717 static_cast<uint32_t>(manifest.ByteSize()) > kMaxManifestByteSize) { | 1733 static_cast<uint32_t>(manifest.ByteSize()) > kMaxManifestByteSize) { |
| 1718 return; | 1734 return; |
| 1719 } | 1735 } |
| 1720 | 1736 |
| 1721 auto cache_entry = manifest_table_cache_->find(host); | 1737 auto cache_entry = manifest_table_cache_->find(host); |
| 1722 if (cache_entry == manifest_table_cache_->end()) { | 1738 if (cache_entry == manifest_table_cache_->end()) { |
| 1723 if (manifest_table_cache_->size() >= config_.max_hosts_to_track) | 1739 if (manifest_table_cache_->size() >= config_.max_hosts_to_track) |
| 1724 RemoveOldestEntryInManifestDataMap(manifest_table_cache_.get()); | 1740 RemoveOldestEntryInManifestDataMap(manifest_table_cache_.get()); |
| 1725 cache_entry = | 1741 cache_entry = manifest_table_cache_->insert({host, manifest}).first; |
| 1726 manifest_table_cache_->insert(std::make_pair(host, manifest)).first; | |
| 1727 } else { | 1742 } else { |
| 1728 cache_entry->second = manifest; | 1743 cache_entry->second = manifest; |
| 1729 } | 1744 } |
| 1730 | 1745 |
| 1731 precache::RemoveUnknownFields(&cache_entry->second); | 1746 auto& data = cache_entry->second; |
| 1747 precache::RemoveUnknownFields(&data); | |
| 1732 | 1748 |
| 1733 BrowserThread::PostTask( | 1749 BrowserThread::PostTask( |
| 1734 BrowserThread::DB, FROM_HERE, | 1750 BrowserThread::DB, FROM_HERE, |
| 1735 base::BindOnce(&ResourcePrefetchPredictorTables::UpdateManifestData, | 1751 base::BindOnce(&ResourcePrefetchPredictorTables::UpdateManifestData, |
| 1736 tables_, host, cache_entry->second)); | 1752 tables_, host, data)); |
| 1737 } | 1753 } |
| 1738 | 1754 |
| 1739 void ResourcePrefetchPredictor::UpdatePrefetchDataByManifest( | 1755 void ResourcePrefetchPredictor::UpdatePrefetchDataByManifest( |
| 1740 const std::string& key, | 1756 const std::string& key, |
| 1741 PrefetchKeyType key_type, | 1757 PrefetchKeyType key_type, |
| 1742 PrefetchDataMap* data_map, | 1758 PrefetchDataMap* data_map, |
| 1743 const precache::PrecacheManifest& manifest) { | 1759 const precache::PrecacheManifest& manifest) { |
| 1744 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1760 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1745 DCHECK_EQ(INITIALIZED, initialization_state_); | 1761 DCHECK_EQ(INITIALIZED, initialization_state_); |
| 1746 | 1762 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1800 TestObserver::~TestObserver() { | 1816 TestObserver::~TestObserver() { |
| 1801 predictor_->SetObserverForTesting(nullptr); | 1817 predictor_->SetObserverForTesting(nullptr); |
| 1802 } | 1818 } |
| 1803 | 1819 |
| 1804 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) | 1820 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) |
| 1805 : predictor_(predictor) { | 1821 : predictor_(predictor) { |
| 1806 predictor_->SetObserverForTesting(this); | 1822 predictor_->SetObserverForTesting(this); |
| 1807 } | 1823 } |
| 1808 | 1824 |
| 1809 } // namespace predictors | 1825 } // namespace predictors |
| OLD | NEW |