summaryrefslogtreecommitdiff
path: root/chromium/components/ntp_snippets/category_rankers/constant_category_ranker.cc
blob: 00a6ae3f21382ced51c49c041eabcd06056628c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// 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.

#include "components/ntp_snippets/category_rankers/constant_category_ranker.h"

#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "components/ntp_snippets/features.h"

namespace ntp_snippets {

ConstantCategoryRanker::ConstantCategoryRanker() {
  std::vector<KnownCategories> ordered_known_categories =
      GetKnownCategoriesDefaultOrder();
  for (KnownCategories known_category : ordered_known_categories) {
    AppendKnownCategory(known_category);
  }
}

ConstantCategoryRanker::~ConstantCategoryRanker() = default;

bool ConstantCategoryRanker::Compare(Category left, Category right) const {
  if (!base::ContainsValue(ordered_categories_, left)) {
    LOG(DFATAL) << "The category with ID " << left.id()
                << " has not been added using AppendCategoryIfNecessary.";
  }
  if (!base::ContainsValue(ordered_categories_, right)) {
    LOG(DFATAL) << "The category with ID " << right.id()
                << " has not been added using AppendCategoryIfNecessary.";
  }
  if (left == right) {
    return false;
  }
  for (Category category : ordered_categories_) {
    if (category == left) {
      return true;
    }
    if (category == right) {
      return false;
    }
  }
  // This fallback is provided only to satisfy "Compare" contract if by mistake
  // categories are not added using AppendCategoryIfNecessary. One should not
  // rely on this, instead the order must be defined explicitly using
  // AppendCategoryIfNecessary.
  return left.id() < right.id();
}

void ConstantCategoryRanker::ClearHistory(base::Time begin, base::Time end) {
  // Ignored, because this implementation doesn't store any history-related
  // data.
}

void ConstantCategoryRanker::AppendCategoryIfNecessary(Category category) {
  if (!base::ContainsValue(ordered_categories_, category)) {
    ordered_categories_.push_back(category);
  }
}

void ConstantCategoryRanker::InsertCategoryBeforeIfNecessary(
    Category category_to_insert,
    Category anchor) {
  // TODO(vitaliii): Implement.
  LOG(DFATAL) << "Not implemented, use ClickBasedCategoryRanker instead for "
                 "inserting categories relative to other categories.";
  AppendCategoryIfNecessary(category_to_insert);
}

void ConstantCategoryRanker::InsertCategoryAfterIfNecessary(
    Category category_to_insert,
    Category anchor) {
  // TODO(vitaliii): Implement.
  LOG(DFATAL) << "Not implemented, use ClickBasedCategoryRanker instead for "
                 "inserting categories relative to other categories.";
  AppendCategoryIfNecessary(category_to_insert);
}

std::vector<CategoryRanker::DebugDataItem>
ConstantCategoryRanker::GetDebugData() {
  std::vector<CategoryRanker::DebugDataItem> result;
  result.push_back(
      CategoryRanker::DebugDataItem("Type", "ConstantCategoryRanker"));

  std::string initial_order_type;
  CategoryOrderChoice choice = GetSelectedCategoryOrder();
  if (choice == CategoryOrderChoice::GENERAL) {
    initial_order_type = "GENERAL";
  }
  if (choice == CategoryOrderChoice::EMERGING_MARKETS_ORIENTED) {
    initial_order_type = "EMERGING_MARKETS_ORIENTED;";
  }
  result.push_back(
      CategoryRanker::DebugDataItem("Initial order type", initial_order_type));

  std::vector<std::string> category_strings;
  for (Category category : ordered_categories_) {
    category_strings.push_back(base::IntToString(category.id()));
  }
  result.push_back(CategoryRanker::DebugDataItem(
      "Current order", base::JoinString(category_strings, ", ")));

  return result;
}

void ConstantCategoryRanker::OnSuggestionOpened(Category category) {
  // Ignored. The order is constant.
}

void ConstantCategoryRanker::OnCategoryDismissed(Category category) {
  // Ignored. The order is constant.
}

// static
std::vector<KnownCategories>
ConstantCategoryRanker::GetKnownCategoriesDefaultOrder() {
  std::vector<KnownCategories> categories;
  CategoryOrderChoice choice = GetSelectedCategoryOrder();
  switch (choice) {
    // All categories must be present. An exception is
    // KnownCategories::CONTEXTUAL because it is not handled by
    // ContentSuggestionsService.
    case CategoryOrderChoice::GENERAL:
      categories.push_back(KnownCategories::READING_LIST);
      categories.push_back(KnownCategories::DOWNLOADS);
      categories.push_back(KnownCategories::FOREIGN_TABS);
      categories.push_back(KnownCategories::BOOKMARKS);
      categories.push_back(KnownCategories::ARTICLES);
      break;
    case CategoryOrderChoice::EMERGING_MARKETS_ORIENTED:
      categories.push_back(KnownCategories::ARTICLES);
      categories.push_back(KnownCategories::READING_LIST);
      categories.push_back(KnownCategories::DOWNLOADS);
      categories.push_back(KnownCategories::BOOKMARKS);

      categories.push_back(KnownCategories::FOREIGN_TABS);
      break;
  }

  static_assert(
      static_cast<size_t>(KnownCategories::LOCAL_CATEGORIES_COUNT) == 7,
      "Number of local categories has changed, please update "
      "ConstantCategoryRanker::GetKnownCategoriesDefaultOrder to list all "
      "local KnownCategories for all orders.");

  // Other remote categories will be ordered after these depending on when
  // providers notify us about them using AppendCategoryIfNecessary.
  return categories;
}

void ConstantCategoryRanker::AppendKnownCategory(
    KnownCategories known_category) {
  Category category = Category::FromKnownCategory(known_category);
  DCHECK(!base::ContainsValue(ordered_categories_, category));
  ordered_categories_.push_back(category);
}

}  // namespace ntp_snippets