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

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: Document remote categories order 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_id) {
34 DCHECK_GT(remote_id, 0);
35 return InternalFromID(
36 static_cast<int>(KnownSuggestionsCategories::REMOTE_CATEGORIES_OFFSET) +
37 remote_id);
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(categories_.begin(), categories_.end(), left) <
56 std::find(categories_.begin(), categories_.end(), right);
57 }
58
59 ////////////////////////////////////////////////////////////////////////////////
60 // Private methods
61
62 bool ContentSuggestionsCategoryFactory::CategoryExists(int id) {
63 return std::find(categories_.begin(), categories_.end(),
64 ContentSuggestionsCategory(id)) != categories_.end();
65 }
66
67 void ContentSuggestionsCategoryFactory::AddKnownCategory(
68 KnownSuggestionsCategories known_category) {
69 InternalFromID(static_cast<int>(known_category));
70 }
71
72 ContentSuggestionsCategory ContentSuggestionsCategoryFactory::InternalFromID(
73 int id) {
74 auto it = std::find(categories_.begin(), categories_.end(),
75 ContentSuggestionsCategory(id));
76 if (it != categories_.end()) {
Bernhard Bauer 2016/07/28 15:39:27 Nit: no braces for single-line bodies.
Philipp Keck 2016/07/28 16:59:59 Done. The style guide still allows them "if you li
77 return *it;
78 }
79
80 ContentSuggestionsCategory category(id);
81 categories_.push_back(category);
82 return category;
83 }
84
85 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698