Index: chrome/browser/ntp_snippets/download_suggestions_provider.cc |
diff --git a/chrome/browser/ntp_snippets/download_suggestions_provider.cc b/chrome/browser/ntp_snippets/download_suggestions_provider.cc |
index 1189dd79942bbbc9192d5a45bfca4d5275f14de7..8790fe18860b21ceba83a070467a23915c793135 100644 |
--- a/chrome/browser/ntp_snippets/download_suggestions_provider.cc |
+++ b/chrome/browser/ntp_snippets/download_suggestions_provider.cc |
@@ -48,6 +48,11 @@ const char kOfflinePageDownloadsPrefix = 'O'; |
const char* kMaxSuggestionsCountParamName = "downloads_max_count"; |
+bool CompareOfflinePagesMostRecentlyCreatedFirst(const OfflinePageItem& left, |
+ const OfflinePageItem& right) { |
+ return left.creation_time > right.creation_time; |
+} |
+ |
int GetMaxSuggestionsCount() { |
bool assets_enabled = |
base::FeatureList::IsEnabled(features::kAssetDownloadSuggestionsFeature); |
@@ -94,12 +99,11 @@ base::Time GetAssetDownloadPublishedTime(const DownloadItem& item) { |
return item.GetStartTime(); |
} |
-struct OrderDownloadsMostRecentlyDownloadedFirst { |
- bool operator()(const DownloadItem* left, const DownloadItem* right) const { |
- return GetAssetDownloadPublishedTime(*left) > |
- GetAssetDownloadPublishedTime(*right); |
- } |
-}; |
+bool CompareDownloadsMostRecentlyDownloadedFirst(const DownloadItem* left, |
+ const DownloadItem* right) { |
+ return GetAssetDownloadPublishedTime(*left) > |
+ GetAssetDownloadPublishedTime(*right); |
+} |
bool IsClientIdForOfflinePageDownload( |
offline_pages::ClientPolicyController* policy_controller, |
@@ -309,9 +313,31 @@ void DownloadSuggestionsProvider::OfflinePageModelLoaded( |
void DownloadSuggestionsProvider::OfflinePageAdded( |
offline_pages::OfflinePageModel* model, |
const offline_pages::OfflinePageItem& added_page) { |
- // TODO(dewittj, vitaliii): Don't refetch everything when this is called. |
DCHECK_EQ(offline_page_model_, model); |
- AsynchronouslyFetchOfflinePagesDownloads(/*notify=*/true); |
+ if (!IsClientIdForOfflinePageDownload(model->GetPolicyController(), |
+ added_page.client_id)) { |
+ return; |
+ } |
+ |
+ // This is all in one statement so that it is completely compiled out in |
+ // release builds. |
+ DCHECK_EQ(ReadOfflinePageDismissedIDsFromPrefs().count( |
+ GetOfflinePagePerCategoryID(added_page.offline_id)), |
+ 0U); |
+ |
+ int max_suggestions_count = GetMaxSuggestionsCount(); |
+ if (static_cast<int>(cached_offline_page_downloads_.size()) < |
+ max_suggestions_count) { |
+ cached_offline_page_downloads_.push_back(added_page); |
+ } else if (max_suggestions_count > 0) { |
+ auto oldest_page_iterator = |
+ std::max_element(cached_offline_page_downloads_.begin(), |
+ cached_offline_page_downloads_.end(), |
+ &CompareOfflinePagesMostRecentlyCreatedFirst); |
+ *oldest_page_iterator = added_page; |
+ } |
+ |
+ SubmitContentSuggestions(); |
} |
void DownloadSuggestionsProvider::OfflinePageDeleted( |
@@ -453,7 +479,7 @@ void DownloadSuggestionsProvider::FetchAssetsDownloads() { |
std::nth_element(cached_asset_downloads_.begin(), |
cached_asset_downloads_.begin() + max_suggestions_count, |
cached_asset_downloads_.end(), |
- OrderDownloadsMostRecentlyDownloadedFirst()); |
+ &CompareDownloadsMostRecentlyDownloadedFirst); |
cached_asset_downloads_.resize(max_suggestions_count); |
} |
} |
@@ -552,9 +578,9 @@ bool DownloadSuggestionsProvider::CacheAssetDownloadIfNeeded( |
GetMaxSuggestionsCount()); |
if (static_cast<int>(cached_asset_downloads_.size()) == |
GetMaxSuggestionsCount()) { |
- auto oldest = std::max_element(cached_asset_downloads_.begin(), |
- cached_asset_downloads_.end(), |
- OrderDownloadsMostRecentlyDownloadedFirst()); |
+ auto oldest = std::max_element( |
+ cached_asset_downloads_.begin(), cached_asset_downloads_.end(), |
+ &CompareDownloadsMostRecentlyDownloadedFirst); |
if (GetAssetDownloadPublishedTime(*item) <= |
GetAssetDownloadPublishedTime(**oldest)) { |
return false; |
@@ -658,7 +684,7 @@ void DownloadSuggestionsProvider::UpdateOfflinePagesCache( |
std::nth_element( |
items.begin(), items.begin() + max_suggestions_count, items.end(), |
[](const OfflinePageItem* left, const OfflinePageItem* right) { |
- return left->creation_time > right->creation_time; |
+ return CompareOfflinePagesMostRecentlyCreatedFirst(*left, *right); |
}); |
items.resize(max_suggestions_count); |
} |