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

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

Issue 2585263002: [NTP::SectionOrder] First version of click based category ranker. (Closed)
Patch Set: tschumann@ & bauerb@ comments. Created 4 years 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
Index: components/ntp_snippets/category_rankers/click_based_category_ranker.h
diff --git a/components/ntp_snippets/category_rankers/click_based_category_ranker.h b/components/ntp_snippets/category_rankers/click_based_category_ranker.h
new file mode 100644
index 0000000000000000000000000000000000000000..4b3a8a20f5fea117da0d0b8fe158c74f5f4df477
--- /dev/null
+++ b/components/ntp_snippets/category_rankers/click_based_category_ranker.h
@@ -0,0 +1,72 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef COMPONENTS_NTP_SNIPPETS_SECTION_RANKERS_CLICK_BASED_SECTION_RANKER_H_
+#define COMPONENTS_NTP_SNIPPETS_SECTION_RANKERS_CLICK_BASED_SECTION_RANKER_H_
+
+#include <vector>
+
+#include "base/time/time.h"
+#include "components/ntp_snippets/category.h"
+#include "components/ntp_snippets/category_rankers/category_ranker.h"
+
+class PrefRegistrySimple;
+class PrefService;
+
+namespace ntp_snippets {
+
+// An implementation of a CategoryRanker based on a number of clicks per
+// category. Initial order is hardcoded, but sections with more clicks are moved
+// to the top. The new remote categories must be registered using
+// AppendCategoryIfNecessary. All other categories must be hardcoded in the
+// initial order. The order and category usage data are persisted in prefs and
+// reloaded on startup. TODO(crbug.com/675929): Remove unused categories from
+// prefs.
+class ClickBasedCategoryRanker : public CategoryRanker {
+ public:
+ explicit ClickBasedCategoryRanker(PrefService* pref_service);
+ ~ClickBasedCategoryRanker() override;
+
+ // CategoryRanker implementation.
+ bool Compare(Category left, Category right) const override;
+ void ClearHistory(base::Time begin, base::Time end) override;
+ void AppendCategoryIfNecessary(Category category) override;
+ void OnSuggestionOpened(Category category) override;
+
+ static void RegisterProfilePrefs(PrefRegistrySimple* registry);
+
+ // Returns passing margin, i.e. a number of extra clicks required to move a
+ // category upwards. For testing only.
+ static int GetPassingMargin();
+
+ // Returns number of top categories with extra margin (i.e. with increased
+ // passing margin). For testing only.
+ static int GetNumTopCategoriesWithExtraMargin();
+
+ private:
+ struct RankedCategory {
+ Category category;
+ int clicks;
+
+ RankedCategory(Category category, int clicks);
+ };
+
+ int GetPositionPassingMargin(
+ std::vector<RankedCategory>::const_iterator category_position) const;
+ void RestoreDefaultOrder();
+ void AppendKnownCategory(KnownCategories known_category);
+ bool ReadOrderFromPrefs(std::vector<RankedCategory>* result_categories);
+ void StoreOrderToPrefs(const std::vector<RankedCategory>& ordered_categories);
+ std::vector<RankedCategory>::iterator FindCategory(Category category);
+ bool ContainsCategory(Category category) const;
+
+ std::vector<RankedCategory> ordered_categories_;
+ PrefService* pref_service_;
+
+ DISALLOW_COPY_AND_ASSIGN(ClickBasedCategoryRanker);
+};
+
+} // namespace ntp_snippets
+
+#endif // COMPONENTS_NTP_SNIPPETS_SECTION_RANKERS_CLICK_BASED_SECTION_RANKER_H_
« no previous file with comments | « components/ntp_snippets/BUILD.gn ('k') | components/ntp_snippets/category_rankers/click_based_category_ranker.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698