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

Unified 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, 5 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 side-by-side diff with in-line comments
Download patch
Index: components/ntp_snippets/content_suggestions_category_factory.cc
diff --git a/components/ntp_snippets/content_suggestions_category_factory.cc b/components/ntp_snippets/content_suggestions_category_factory.cc
new file mode 100644
index 0000000000000000000000000000000000000000..58e9521b8a5690fdf2eb9c7ec37dc342226f1078
--- /dev/null
+++ b/components/ntp_snippets/content_suggestions_category_factory.cc
@@ -0,0 +1,82 @@
+// 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/content_suggestions_category_factory.h"
+
+#include <algorithm>
+
+#include "base/logging.h"
+
+namespace ntp_snippets {
+
+ContentSuggestionsCategoryFactory::ContentSuggestionsCategoryFactory() {
+ // Add all local categories in a fixed order.
+ AddKnownCategory(KnownSuggestionsCategories::OFFLINE_PAGES);
+}
+
+ContentSuggestionsCategoryFactory::~ContentSuggestionsCategoryFactory() {}
+
+ContentSuggestionsCategory ContentSuggestionsCategoryFactory::FromKnownCategory(
+ KnownSuggestionsCategories known_category) {
+ 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.
+ if (known_category < KnownSuggestionsCategories::LOCAL_CATEGORIES_COUNT) {
+ // Local categories should have been added already.
+ 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
+ categories_by_id_.end());
+ } else {
+ 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.
+ KnownSuggestionsCategories::REMOTE_CATEGORIES_OFFSET);
+ }
+ return InternalFromID(static_cast<int>(known_category));
+}
+
+ContentSuggestionsCategory
+ContentSuggestionsCategoryFactory::FromRemoteCategory(int remote_id) {
+ DCHECK_GT(remote_id, 0);
+ return InternalFromID(
+ static_cast<int>(KnownSuggestionsCategories::REMOTE_CATEGORIES_OFFSET) +
+ remote_id);
+}
+
+ContentSuggestionsCategory ContentSuggestionsCategoryFactory::FromIDValue(
+ int id) {
+ DCHECK_GE(id, 0);
+ DCHECK(id < static_cast<int>(
+ KnownSuggestionsCategories::LOCAL_CATEGORIES_COUNT) ||
+ id > static_cast<int>(
+ KnownSuggestionsCategories::REMOTE_CATEGORIES_OFFSET));
+ return InternalFromID(id);
+}
+
+bool ContentSuggestionsCategoryFactory::CompareCategories(
+ const ContentSuggestionsCategory& left,
+ const ContentSuggestionsCategory& right) const {
+ if (left == right)
+ return false;
+ return std::find(categories_.begin(), categories_.end(), left) <
+ std::find(categories_.begin(), categories_.end(), right);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// Private methods
+
+void ContentSuggestionsCategoryFactory::AddKnownCategory(
+ KnownSuggestionsCategories known_category) {
+ InternalFromID(static_cast<int>(known_category));
+}
+
+ContentSuggestionsCategory ContentSuggestionsCategoryFactory::InternalFromID(
+ int id) {
+ 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.
+ if (entry != categories_by_id_.end()) {
+ return entry->second;
+ }
+
+ ContentSuggestionsCategory category(id);
+ categories_.push_back(category);
+ categories_by_id_.insert(std::make_pair(id, category));
+ return category;
+}
+
+} // namespace ntp_snippets

Powered by Google App Engine
This is Rietveld 408576698