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

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: Document remote categories order 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..fcb4e805cea3081c4d16cb6b8d8987d1acdc10c0
--- /dev/null
+++ b/components/ntp_snippets/content_suggestions_category_factory.cc
@@ -0,0 +1,85 @@
+// 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) {
+ if (known_category < KnownSuggestionsCategories::LOCAL_CATEGORIES_COUNT) {
+ // Local categories should have been added already.
+ DCHECK(CategoryExists(static_cast<int>(known_category)));
+ } else {
+ DCHECK_GT(known_category,
+ 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
+
+bool ContentSuggestionsCategoryFactory::CategoryExists(int id) {
+ return std::find(categories_.begin(), categories_.end(),
+ ContentSuggestionsCategory(id)) != categories_.end();
+}
+
+void ContentSuggestionsCategoryFactory::AddKnownCategory(
+ KnownSuggestionsCategories known_category) {
+ InternalFromID(static_cast<int>(known_category));
+}
+
+ContentSuggestionsCategory ContentSuggestionsCategoryFactory::InternalFromID(
+ int id) {
+ auto it = std::find(categories_.begin(), categories_.end(),
+ ContentSuggestionsCategory(id));
+ 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
+ return *it;
+ }
+
+ ContentSuggestionsCategory category(id);
+ categories_.push_back(category);
+ return category;
+}
+
+} // namespace ntp_snippets

Powered by Google App Engine
This is Rietveld 408576698