Commit 804d3453 authored by dewittj's avatar dewittj Committed by Commit bot

[Offline Pages] Convert Download Suggestions Provider to using OfflinePageAdded

Recently OfflinePageModelChanged was renamed to OfflinePageAdded to reflect
the reality of when that function was being called.  As a result, the
OfflinePageAdded function now receives as a parameter the OfflinePageItem
that was added.  This allows for a much simpler observer function that
directly operates on the new page in the download cache.

BUG=638276

Review-Url: https://codereview.chromium.org/2555013003
Cr-Commit-Position: refs/heads/master@{#437616}
parent 6206d82a
...@@ -49,6 +49,11 @@ const char kOfflinePageDownloadsPrefix = 'O'; ...@@ -49,6 +49,11 @@ const char kOfflinePageDownloadsPrefix = 'O';
const char* kMaxSuggestionsCountParamName = "downloads_max_count"; const char* kMaxSuggestionsCountParamName = "downloads_max_count";
bool CompareOfflinePagesMostRecentlyCreatedFirst(const OfflinePageItem& left,
const OfflinePageItem& right) {
return left.creation_time > right.creation_time;
}
int GetMaxSuggestionsCount() { int GetMaxSuggestionsCount() {
bool assets_enabled = bool assets_enabled =
base::FeatureList::IsEnabled(features::kAssetDownloadSuggestionsFeature); base::FeatureList::IsEnabled(features::kAssetDownloadSuggestionsFeature);
...@@ -95,12 +100,11 @@ base::Time GetAssetDownloadPublishedTime(const DownloadItem& item) { ...@@ -95,12 +100,11 @@ base::Time GetAssetDownloadPublishedTime(const DownloadItem& item) {
return item.GetStartTime(); return item.GetStartTime();
} }
struct OrderDownloadsMostRecentlyDownloadedFirst { bool CompareDownloadsMostRecentlyDownloadedFirst(const DownloadItem* left,
bool operator()(const DownloadItem* left, const DownloadItem* right) const { const DownloadItem* right) {
return GetAssetDownloadPublishedTime(*left) > return GetAssetDownloadPublishedTime(*left) >
GetAssetDownloadPublishedTime(*right); GetAssetDownloadPublishedTime(*right);
} }
};
bool IsClientIdForOfflinePageDownload( bool IsClientIdForOfflinePageDownload(
offline_pages::ClientPolicyController* policy_controller, offline_pages::ClientPolicyController* policy_controller,
...@@ -310,9 +314,31 @@ void DownloadSuggestionsProvider::OfflinePageModelLoaded( ...@@ -310,9 +314,31 @@ void DownloadSuggestionsProvider::OfflinePageModelLoaded(
void DownloadSuggestionsProvider::OfflinePageAdded( void DownloadSuggestionsProvider::OfflinePageAdded(
offline_pages::OfflinePageModel* model, offline_pages::OfflinePageModel* model,
const offline_pages::OfflinePageItem& added_page) { const offline_pages::OfflinePageItem& added_page) {
// TODO(dewittj, vitaliii): Don't refetch everything when this is called.
DCHECK_EQ(offline_page_model_, model); 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( void DownloadSuggestionsProvider::OfflinePageDeleted(
...@@ -454,7 +480,7 @@ void DownloadSuggestionsProvider::FetchAssetsDownloads() { ...@@ -454,7 +480,7 @@ void DownloadSuggestionsProvider::FetchAssetsDownloads() {
std::nth_element(cached_asset_downloads_.begin(), std::nth_element(cached_asset_downloads_.begin(),
cached_asset_downloads_.begin() + max_suggestions_count, cached_asset_downloads_.begin() + max_suggestions_count,
cached_asset_downloads_.end(), cached_asset_downloads_.end(),
OrderDownloadsMostRecentlyDownloadedFirst()); &CompareDownloadsMostRecentlyDownloadedFirst);
cached_asset_downloads_.resize(max_suggestions_count); cached_asset_downloads_.resize(max_suggestions_count);
} }
} }
...@@ -553,9 +579,9 @@ bool DownloadSuggestionsProvider::CacheAssetDownloadIfNeeded( ...@@ -553,9 +579,9 @@ bool DownloadSuggestionsProvider::CacheAssetDownloadIfNeeded(
GetMaxSuggestionsCount()); GetMaxSuggestionsCount());
if (static_cast<int>(cached_asset_downloads_.size()) == if (static_cast<int>(cached_asset_downloads_.size()) ==
GetMaxSuggestionsCount()) { GetMaxSuggestionsCount()) {
auto oldest = std::max_element(cached_asset_downloads_.begin(), auto oldest = std::max_element(
cached_asset_downloads_.end(), cached_asset_downloads_.begin(), cached_asset_downloads_.end(),
OrderDownloadsMostRecentlyDownloadedFirst()); &CompareDownloadsMostRecentlyDownloadedFirst);
if (GetAssetDownloadPublishedTime(*item) <= if (GetAssetDownloadPublishedTime(*item) <=
GetAssetDownloadPublishedTime(**oldest)) { GetAssetDownloadPublishedTime(**oldest)) {
return false; return false;
...@@ -659,7 +685,7 @@ void DownloadSuggestionsProvider::UpdateOfflinePagesCache( ...@@ -659,7 +685,7 @@ void DownloadSuggestionsProvider::UpdateOfflinePagesCache(
std::nth_element( std::nth_element(
items.begin(), items.begin() + max_suggestions_count, items.end(), items.begin(), items.begin() + max_suggestions_count, items.end(),
[](const OfflinePageItem* left, const OfflinePageItem* right) { [](const OfflinePageItem* left, const OfflinePageItem* right) {
return left->creation_time > right->creation_time; return CompareOfflinePagesMostRecentlyCreatedFirst(*left, *right);
}); });
items.resize(max_suggestions_count); items.resize(max_suggestions_count);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment