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

Side by Side Diff: components/ntp_snippets/category_factory.cc

Issue 2568033005: [NTP::SectionOrder] Replace CategoryFactory with a category ranker. (Closed)
Patch Set: rebase. 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 unified diff | Download patch
OLDNEW
(Empty)
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
5 #include "components/ntp_snippets/category_factory.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/stringprintf.h"
12
13 namespace ntp_snippets {
14
15 CategoryFactory::CategoryFactory() {
16 // Add all local categories in a fixed order.
17 AddKnownCategory(KnownCategories::DOWNLOADS);
18 AddKnownCategory(KnownCategories::RECENT_TABS);
19 AddKnownCategory(KnownCategories::FOREIGN_TABS);
20 AddKnownCategory(KnownCategories::BOOKMARKS);
21 AddKnownCategory(KnownCategories::PHYSICAL_WEB_PAGES);
22
23 DCHECK_EQ(static_cast<size_t>(KnownCategories::LOCAL_CATEGORIES_COUNT),
24 ordered_categories_.size());
25
26 // Known remote categories come after. Other remote categories will be ordered
27 // after these depending on when providers notify us about a related change.
28 AddKnownCategory(KnownCategories::ARTICLES);
29 }
30
31 CategoryFactory::~CategoryFactory() = default;
32
33 Category CategoryFactory::FromKnownCategory(KnownCategories known_category) {
34 if (known_category < KnownCategories::LOCAL_CATEGORIES_COUNT) {
35 // Local categories should have been added already.
36 DCHECK(CategoryExists(static_cast<int>(known_category)));
37 } else {
38 DCHECK_GT(known_category, KnownCategories::REMOTE_CATEGORIES_OFFSET);
39 }
40 return InternalFromID(static_cast<int>(known_category));
41 }
42
43 Category CategoryFactory::FromRemoteCategory(int remote_category) {
44 DCHECK_GT(remote_category, 0);
45 return InternalFromID(
46 static_cast<int>(KnownCategories::REMOTE_CATEGORIES_OFFSET) +
47 remote_category);
48 }
49
50 Category CategoryFactory::FromIDValue(int id) {
51 DCHECK_GE(id, 0);
52 DCHECK(id < static_cast<int>(KnownCategories::LOCAL_CATEGORIES_COUNT) ||
53 id > static_cast<int>(KnownCategories::REMOTE_CATEGORIES_OFFSET));
54 return InternalFromID(id);
55 }
56
57 bool CategoryFactory::CompareCategories(const Category& left,
58 const Category& right) const {
59 if (left == right) {
60 return false;
61 }
62 return std::find(ordered_categories_.begin(), ordered_categories_.end(),
63 left) < std::find(ordered_categories_.begin(),
64 ordered_categories_.end(), right);
65 }
66
67 ////////////////////////////////////////////////////////////////////////////////
68 // Private methods
69
70 bool CategoryFactory::CategoryExists(int id) {
71 return std::find(ordered_categories_.begin(), ordered_categories_.end(),
72 Category(id)) != ordered_categories_.end();
73 }
74
75 void CategoryFactory::AddKnownCategory(KnownCategories known_category) {
76 InternalFromID(static_cast<int>(known_category));
77 }
78
79 Category CategoryFactory::InternalFromID(int id) {
80 auto it = std::find(ordered_categories_.begin(), ordered_categories_.end(),
81 Category(id));
82 if (it != ordered_categories_.end()) {
83 return *it;
84 }
85
86 Category category(id);
87 ordered_categories_.push_back(category);
88 return category;
89 }
90
91 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/category_factory.h ('k') | components/ntp_snippets/category_factory_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698