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

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: 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 DCHECK_GE(static_cast<int>(known_category), 0);
Marc Treib 2016/07/28 11:41:45 What is this for? We're getting a KnownSuggestions
Philipp Keck 2016/07/28 13:50:54 This is here because C++ allows you to do things l
Marc Treib 2016/07/28 14:31:16 For some value of "allows" - it might work in prac
Philipp Keck 2016/07/28 14:55:47 True. Done.
tschumann 2016/07/28 15:03:45 nit: I believe it's unspecified ;-) But yes, we'd
Philipp Keck 2016/07/28 15:15:25 Acknowledged.
Bernhard Bauer 2016/07/28 15:39:26 Not to add too much bikeshedding, but if you are a
Philipp Keck 2016/07/28 16:59:58 That's also true.
23 if (known_category < KnownSuggestionsCategories::LOCAL_CATEGORIES_COUNT) {
24 // Local categories should have been added already.
25 DCHECK(categories_by_id_.find(static_cast<int>(known_category)) !=
Marc Treib 2016/07/28 11:41:45 IMO slightly clearer written as DCHECK(categories_
Philipp Keck 2016/07/28 13:50:54 Done. And shorter. But when categories_by_id_ is g
26 categories_by_id_.end());
27 } else {
28 DCHECK_GT(known_category,
Marc Treib 2016/07/28 11:41:45 Hm, so this basically checks that the enum has no
Philipp Keck 2016/07/28 13:50:54 :D As far as I know, and I googled a lot, there is
Marc Treib 2016/07/28 14:31:16 Hm, I guess the closest thing is something like th
Philipp Keck 2016/07/28 14:55:47 Acknowledged.
29 KnownSuggestionsCategories::REMOTE_CATEGORIES_OFFSET);
30 }
31 return InternalFromID(static_cast<int>(known_category));
32 }
33
34 ContentSuggestionsCategory
35 ContentSuggestionsCategoryFactory::FromRemoteCategory(int remote_id) {
36 DCHECK_GT(remote_id, 0);
37 return InternalFromID(
38 static_cast<int>(KnownSuggestionsCategories::REMOTE_CATEGORIES_OFFSET) +
39 remote_id);
40 }
41
42 ContentSuggestionsCategory ContentSuggestionsCategoryFactory::FromIDValue(
43 int id) {
44 DCHECK_GE(id, 0);
45 DCHECK(id < static_cast<int>(
46 KnownSuggestionsCategories::LOCAL_CATEGORIES_COUNT) ||
47 id > static_cast<int>(
48 KnownSuggestionsCategories::REMOTE_CATEGORIES_OFFSET));
49 return InternalFromID(id);
50 }
51
52 bool ContentSuggestionsCategoryFactory::CompareCategories(
53 const ContentSuggestionsCategory& left,
54 const ContentSuggestionsCategory& right) const {
55 if (left == right)
56 return false;
57 return std::find(categories_.begin(), categories_.end(), left) <
58 std::find(categories_.begin(), categories_.end(), right);
59 }
60
61 ////////////////////////////////////////////////////////////////////////////////
62 // Private methods
63
64 void ContentSuggestionsCategoryFactory::AddKnownCategory(
65 KnownSuggestionsCategories known_category) {
66 InternalFromID(static_cast<int>(known_category));
67 }
68
69 ContentSuggestionsCategory ContentSuggestionsCategoryFactory::InternalFromID(
70 int id) {
71 auto entry = categories_by_id_.find(id);
Marc Treib 2016/07/28 11:41:45 nit: call this |it|? As-is, it sounds like it's th
Philipp Keck 2016/07/28 13:50:54 Done.
72 if (entry != categories_by_id_.end()) {
73 return entry->second;
74 }
75
76 ContentSuggestionsCategory category(id);
77 categories_.push_back(category);
78 categories_by_id_.insert(std::make_pair(id, category));
79 return category;
80 }
81
82 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698