category_ranker.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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_CATEGORY_RANKER_H_
  5. #define COMPONENTS_NTP_SNIPPETS_CATEGORY_RANKERS_CATEGORY_RANKER_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/time/time.h"
  9. #include "components/ntp_snippets/category.h"
  10. namespace ntp_snippets {
  11. // Orders categories.
  12. // The order may be dynamic and change at any time.
  13. class CategoryRanker {
  14. public:
  15. virtual ~CategoryRanker() = default;
  16. // Compares two categories. True means that |left| precedes |right|, i.e. it
  17. // must be located earlier (above) on the NTP. This method must satisfy
  18. // "Compare" contract required by sort algorithms.
  19. virtual bool Compare(Category left, Category right) const = 0;
  20. // Deletes all history related data between |begin| and |end|. After this
  21. // call, the category order may not depend on this history range anymore.
  22. virtual void ClearHistory(base::Time begin, base::Time end) = 0;
  23. // If |category| has not been added previously, it is added after all already
  24. // known categories, otherwise nothing is changed.
  25. virtual void AppendCategoryIfNecessary(Category category) = 0;
  26. // If |category_to_insert| has not been added previously, it is added before
  27. // |anchor|, otherwise nothing is changed.
  28. virtual void InsertCategoryBeforeIfNecessary(Category category_to_insert,
  29. Category anchor) = 0;
  30. // If |category_to_insert| has not been added previously, it is added after
  31. // |anchor|, otherwise nothing is changed.
  32. virtual void InsertCategoryAfterIfNecessary(Category category_to_insert,
  33. Category anchor) = 0;
  34. struct DebugDataItem {
  35. std::string label;
  36. std::string content;
  37. DebugDataItem(const std::string& label, const std::string& content)
  38. : label(label), content(content) {}
  39. };
  40. // Returns DebugData in form of pairs of strings (label; content),
  41. // e.g. describing internal state or parameter values.
  42. virtual std::vector<DebugDataItem> GetDebugData() = 0;
  43. // Feedback data from the user to update the ranking.
  44. // Called whenever a suggestion is opened by the user.
  45. virtual void OnSuggestionOpened(Category category) = 0;
  46. // Called whenever a category is dismissed by the user.
  47. virtual void OnCategoryDismissed(Category category) = 0;
  48. };
  49. } // namespace ntp_snippets
  50. #endif // COMPONENTS_NTP_SNIPPETS_CATEGORY_RANKERS_CATEGORY_RANKER_H_