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

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

Issue 2355393002: New snippets now replace old snippets and do not merge (Closed)
Patch Set: Minor polish Created 4 years, 3 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/ntp_snippets/category_factory.h" 5 #include "components/ntp_snippets/category_factory.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/stringprintf.h"
10 12
11 namespace ntp_snippets { 13 namespace ntp_snippets {
12 14
15 namespace {
16
17 const char kCombinedIDFormat[] = "%d|%s";
18 const char kSeparator = '|';
19
20 } // namespace
21
13 CategoryFactory::CategoryFactory() { 22 CategoryFactory::CategoryFactory() {
14 // Add all local categories in a fixed order. 23 // Add all local categories in a fixed order.
15 AddKnownCategory(KnownCategories::DOWNLOADS); 24 AddKnownCategory(KnownCategories::DOWNLOADS);
16 AddKnownCategory(KnownCategories::RECENT_TABS); 25 AddKnownCategory(KnownCategories::RECENT_TABS);
17 AddKnownCategory(KnownCategories::FOREIGN_TABS); 26 AddKnownCategory(KnownCategories::FOREIGN_TABS);
18 AddKnownCategory(KnownCategories::BOOKMARKS); 27 AddKnownCategory(KnownCategories::BOOKMARKS);
19 AddKnownCategory(KnownCategories::PHYSICAL_WEB_PAGES); 28 AddKnownCategory(KnownCategories::PHYSICAL_WEB_PAGES);
20 29
21 DCHECK_EQ(static_cast<size_t>(KnownCategories::LOCAL_CATEGORIES_COUNT), 30 DCHECK_EQ(static_cast<size_t>(KnownCategories::LOCAL_CATEGORIES_COUNT),
22 ordered_categories_.size()); 31 ordered_categories_.size());
(...skipping 27 matching lines...) Expand all
50 59
51 bool CategoryFactory::CompareCategories(const Category& left, 60 bool CategoryFactory::CompareCategories(const Category& left,
52 const Category& right) const { 61 const Category& right) const {
53 if (left == right) 62 if (left == right)
54 return false; 63 return false;
55 return std::find(ordered_categories_.begin(), ordered_categories_.end(), 64 return std::find(ordered_categories_.begin(), ordered_categories_.end(),
56 left) < std::find(ordered_categories_.begin(), 65 left) < std::find(ordered_categories_.begin(),
57 ordered_categories_.end(), right); 66 ordered_categories_.end(), right);
58 } 67 }
59 68
69 std::string CategoryFactory::MakeUniqueID(
70 Category category,
71 const std::string& within_category_id) const {
72 return base::StringPrintf(kCombinedIDFormat, category.id(),
73 within_category_id.c_str());
74 }
75
76 Category CategoryFactory::GetCategoryFromUniqueID(
77 const std::string& unique_id) {
78 size_t colon_index = unique_id.find(kSeparator);
79 DCHECK_NE(std::string::npos, colon_index) << "Not a valid unique_id: "
80 << unique_id;
81 int category = -1;
82 bool ret = base::StringToInt(unique_id.substr(0, colon_index), &category);
83 DCHECK(ret) << "Non-numeric category part in unique_id: " << unique_id;
84 return FromIDValue(category);
85 }
86
87 std::string CategoryFactory::GetWithinCategoryIDFromUniqueID(
88 const std::string& unique_id) const {
89 size_t colon_index = unique_id.find(kSeparator);
90 DCHECK_NE(std::string::npos, colon_index) << "Not a valid unique_id: "
91 << unique_id;
92 return unique_id.substr(colon_index + 1);
93 }
94
60 //////////////////////////////////////////////////////////////////////////////// 95 ////////////////////////////////////////////////////////////////////////////////
61 // Private methods 96 // Private methods
62 97
63 bool CategoryFactory::CategoryExists(int id) { 98 bool CategoryFactory::CategoryExists(int id) {
64 return std::find(ordered_categories_.begin(), ordered_categories_.end(), 99 return std::find(ordered_categories_.begin(), ordered_categories_.end(),
65 Category(id)) != ordered_categories_.end(); 100 Category(id)) != ordered_categories_.end();
66 } 101 }
67 102
68 void CategoryFactory::AddKnownCategory(KnownCategories known_category) { 103 void CategoryFactory::AddKnownCategory(KnownCategories known_category) {
69 InternalFromID(static_cast<int>(known_category)); 104 InternalFromID(static_cast<int>(known_category));
70 } 105 }
71 106
72 Category CategoryFactory::InternalFromID(int id) { 107 Category CategoryFactory::InternalFromID(int id) {
73 auto it = std::find(ordered_categories_.begin(), ordered_categories_.end(), 108 auto it = std::find(ordered_categories_.begin(), ordered_categories_.end(),
74 Category(id)); 109 Category(id));
75 if (it != ordered_categories_.end()) 110 if (it != ordered_categories_.end())
76 return *it; 111 return *it;
77 112
78 Category category(id); 113 Category category(id);
79 ordered_categories_.push_back(category); 114 ordered_categories_.push_back(category);
80 return category; 115 return category;
81 } 116 }
82 117
83 } // namespace ntp_snippets 118 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698