summaryrefslogtreecommitdiff
path: root/chromium/components/ntp_snippets/category_rankers/constant_category_ranker.cc
blob: 828cd317e5d766fa5a170e3217e5c706f1b5c391 (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
// 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 "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);
}

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) {
    case CategoryOrderChoice::GENERAL:
      categories.push_back(KnownCategories::PHYSICAL_WEB_PAGES);
      categories.push_back(KnownCategories::READING_LIST);
      categories.push_back(KnownCategories::DOWNLOADS);
      categories.push_back(KnownCategories::RECENT_TABS);
      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::PHYSICAL_WEB_PAGES);
      categories.push_back(KnownCategories::RECENT_TABS);
      categories.push_back(KnownCategories::FOREIGN_TABS);
      break;
  }

  static_assert(
      static_cast<size_t>(KnownCategories::LOCAL_CATEGORIES_COUNT) == 6,
      "All local KnownCategories must be present in 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