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

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

Issue 2187233002: Add ContentSuggestionsCategoryFactory; Store categories as ints (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 4 months 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/content_suggestions_category_factory.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10
11 namespace ntp_snippets {
12
13 ContentSuggestionsCategoryFactory::ContentSuggestionsCategoryFactory() {
14 // Add all local categories in a fixed order.
15 AddKnownCategory(KnownSuggestionsCategories::OFFLINE_PAGES);
16 }
17
18 ContentSuggestionsCategoryFactory::~ContentSuggestionsCategoryFactory() {}
19
20 ContentSuggestionsCategory ContentSuggestionsCategoryFactory::FromKnownCategory(
21 KnownSuggestionsCategories known_category) {
22 if (known_category < KnownSuggestionsCategories::LOCAL_CATEGORIES_COUNT) {
23 // Local categories should have been added already.
24 DCHECK(CategoryExists(static_cast<int>(known_category)));
25 } else {
26 DCHECK_GT(known_category,
27 KnownSuggestionsCategories::REMOTE_CATEGORIES_OFFSET);
28 }
29 return InternalFromID(static_cast<int>(known_category));
30 }
31
32 ContentSuggestionsCategory
33 ContentSuggestionsCategoryFactory::FromRemoteCategory(int remote_category) {
34 DCHECK_GT(remote_category, 0);
35 return InternalFromID(
36 static_cast<int>(KnownSuggestionsCategories::REMOTE_CATEGORIES_OFFSET) +
37 remote_category);
38 }
39
40 ContentSuggestionsCategory ContentSuggestionsCategoryFactory::FromIDValue(
41 int id) {
42 DCHECK_GE(id, 0);
43 DCHECK(id < static_cast<int>(
44 KnownSuggestionsCategories::LOCAL_CATEGORIES_COUNT) ||
45 id > static_cast<int>(
46 KnownSuggestionsCategories::REMOTE_CATEGORIES_OFFSET));
47 return InternalFromID(id);
48 }
49
50 bool ContentSuggestionsCategoryFactory::CompareCategories(
51 const ContentSuggestionsCategory& left,
52 const ContentSuggestionsCategory& right) const {
53 if (left == right)
54 return false;
55 return std::find(ordered_categories_.begin(), ordered_categories_.end(),
56 left) < std::find(ordered_categories_.begin(),
57 ordered_categories_.end(), right);
58 }
59
60 ////////////////////////////////////////////////////////////////////////////////
61 // Private methods
62
63 bool ContentSuggestionsCategoryFactory::CategoryExists(int id) {
64 return std::find(ordered_categories_.begin(), ordered_categories_.end(),
65 ContentSuggestionsCategory(id)) != ordered_categories_.end();
66 }
67
68 void ContentSuggestionsCategoryFactory::AddKnownCategory(
69 KnownSuggestionsCategories known_category) {
70 InternalFromID(static_cast<int>(known_category));
71 }
72
73 ContentSuggestionsCategory ContentSuggestionsCategoryFactory::InternalFromID(
74 int id) {
75 auto it = std::find(ordered_categories_.begin(), ordered_categories_.end(),
76 ContentSuggestionsCategory(id));
77 if (it != ordered_categories_.end())
78 return *it;
79
80 ContentSuggestionsCategory category(id);
81 ordered_categories_.push_back(category);
82 return category;
83 }
84
85 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698