Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(451)

Unified Diff: components/ntp_snippets/category_rankers/click_based_category_ranker.cc

Issue 2605353004: [NTP::SectionOrder] Preserve added categories on ClearHistory. (Closed)
Patch Set: rebase. Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | components/ntp_snippets/category_rankers/click_based_category_ranker_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/ntp_snippets/category_rankers/click_based_category_ranker.cc
diff --git a/components/ntp_snippets/category_rankers/click_based_category_ranker.cc b/components/ntp_snippets/category_rankers/click_based_category_ranker.cc
index 6dbab54b52c0a22bec4dbac162f6ac58da67f956..5b7ca83b02ef93fc436c1589512772024abebc82 100644
--- a/components/ntp_snippets/category_rankers/click_based_category_ranker.cc
+++ b/components/ntp_snippets/category_rankers/click_based_category_ranker.cc
@@ -86,9 +86,33 @@ bool ClickBasedCategoryRanker::Compare(Category left, Category right) const {
}
void ClickBasedCategoryRanker::ClearHistory(base::Time begin, base::Time end) {
- // TODO(crbug.com/675953): Preserve remote categories with 0 counts instead of
- // removing them.
+ // The categories added through |AppendCategoryIfNecessary| cannot be
+ // completely removed, since no one is required to reregister them. Instead
+ // they are preserved in the default order (sorted by id).
+ std::vector<RankedCategory> old_categories = ordered_categories_;
RestoreDefaultOrder();
+
+ std::vector<Category> added_categories;
+ for (const RankedCategory& old_category : old_categories) {
+ auto it =
+ std::find_if(ordered_categories_.begin(), ordered_categories_.end(),
+ [old_category](const RankedCategory& other) {
+ return other.category == old_category.category;
+ });
+ if (it == ordered_categories_.end()) {
+ added_categories.push_back(old_category.category);
+ }
+ }
+
+ // Sort added categories by id to make their order history independent.
+ std::sort(added_categories.begin(), added_categories.end(),
+ Category::CompareByID());
+
+ for (Category added_category : added_categories) {
+ ordered_categories_.push_back(RankedCategory(added_category, /*clicks=*/0));
+ }
+
+ StoreOrderToPrefs(ordered_categories_);
}
void ClickBasedCategoryRanker::AppendCategoryIfNecessary(Category category) {
« no previous file with comments | « no previous file | components/ntp_snippets/category_rankers/click_based_category_ranker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698