click_based_category_ranker.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_NTP_SNIPPETS_CATEGORY_RANKERS_CLICK_BASED_CATEGORY_RANKER_H_
  5. #define COMPONENTS_NTP_SNIPPETS_CATEGORY_RANKERS_CLICK_BASED_CATEGORY_RANKER_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/time/clock.h"
  10. #include "base/time/time.h"
  11. #include "components/ntp_snippets/category.h"
  12. #include "components/ntp_snippets/category_rankers/category_ranker.h"
  13. class PrefRegistrySimple;
  14. class PrefService;
  15. namespace ntp_snippets {
  16. // An implementation of a CategoryRanker based on a number of clicks per
  17. // category. Initial order is hardcoded, but sections with more clicks are moved
  18. // to the top. The new remote categories must be registered using
  19. // AppendCategoryIfNecessary. All other categories must be hardcoded in the
  20. // initial order. The order and category usage data are persisted in prefs and
  21. // reloaded on startup.
  22. // TODO(crbug.com/675929): Remove unused categories from prefs.
  23. class ClickBasedCategoryRanker : public CategoryRanker {
  24. public:
  25. explicit ClickBasedCategoryRanker(PrefService* pref_service,
  26. base::Clock* clock);
  27. ClickBasedCategoryRanker(const ClickBasedCategoryRanker&) = delete;
  28. ClickBasedCategoryRanker& operator=(const ClickBasedCategoryRanker&) = delete;
  29. ~ClickBasedCategoryRanker() override;
  30. // CategoryRanker implementation.
  31. bool Compare(Category left, Category right) const override;
  32. void ClearHistory(base::Time begin, base::Time end) override;
  33. void AppendCategoryIfNecessary(Category category) override;
  34. void InsertCategoryBeforeIfNecessary(Category category_to_insert,
  35. Category anchor) override;
  36. void InsertCategoryAfterIfNecessary(Category category_to_insert,
  37. Category anchor) override;
  38. std::vector<CategoryRanker::DebugDataItem> GetDebugData() override;
  39. void OnSuggestionOpened(Category category) override;
  40. void OnCategoryDismissed(Category category) override;
  41. // Returns time when last decay occured. For testing only.
  42. base::Time GetLastDecayTime() const;
  43. static void RegisterProfilePrefs(PrefRegistrySimple* registry);
  44. // Returns passing margin, i.e. a number of extra clicks required to move a
  45. // category upwards. For testing only.
  46. static int GetPassingMargin();
  47. // Returns number of top categories with extra margin (i.e. with increased
  48. // passing margin). For testing only.
  49. static int GetNumTopCategoriesWithExtraMargin();
  50. // Returns number of positions by which a dismissed category is downgraded.
  51. // For testing only.
  52. static int GetDismissedCategoryPenalty();
  53. private:
  54. struct RankedCategory {
  55. Category category;
  56. int clicks;
  57. base::Time last_dismissed;
  58. RankedCategory(Category category,
  59. int clicks,
  60. const base::Time& last_dismissed);
  61. };
  62. int GetPositionPassingMargin(
  63. std::vector<RankedCategory>::const_iterator category_position) const;
  64. void RestoreDefaultOrder();
  65. void AppendKnownCategory(KnownCategories known_category);
  66. bool ReadOrderFromPrefs(std::vector<RankedCategory>* result_categories) const;
  67. void StoreOrderToPrefs(const std::vector<RankedCategory>& ordered_categories);
  68. std::vector<RankedCategory>::iterator FindCategory(Category category);
  69. bool ContainsCategory(Category category) const;
  70. void InsertCategoryRelativeToIfNecessary(Category category_to_insert,
  71. Category anchor,
  72. bool after);
  73. base::Time ReadLastDecayTimeFromPrefs() const;
  74. void StoreLastDecayTimeToPrefs(base::Time last_decay_time);
  75. bool IsEnoughClicksToDecay() const;
  76. bool DecayClicksIfNeeded();
  77. std::vector<RankedCategory> ordered_categories_;
  78. raw_ptr<PrefService> pref_service_;
  79. raw_ptr<base::Clock> clock_;
  80. };
  81. } // namespace ntp_snippets
  82. #endif // COMPONENTS_NTP_SNIPPETS_CATEGORY_RANKERS_CLICK_BASED_CATEGORY_RANKER_H_