OLD | NEW |
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" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 | 12 |
13 namespace ntp_snippets { | 13 namespace ntp_snippets { |
14 | 14 |
15 namespace { | |
16 | |
17 const char kCombinedIDFormat[] = "%d|%s"; | |
18 const char kSeparator = '|'; | |
19 | |
20 } // namespace | |
21 | |
22 CategoryFactory::CategoryFactory() { | 15 CategoryFactory::CategoryFactory() { |
23 // Add all local categories in a fixed order. | 16 // Add all local categories in a fixed order. |
24 AddKnownCategory(KnownCategories::DOWNLOADS); | 17 AddKnownCategory(KnownCategories::DOWNLOADS); |
25 AddKnownCategory(KnownCategories::RECENT_TABS); | 18 AddKnownCategory(KnownCategories::RECENT_TABS); |
26 AddKnownCategory(KnownCategories::FOREIGN_TABS); | 19 AddKnownCategory(KnownCategories::FOREIGN_TABS); |
27 AddKnownCategory(KnownCategories::BOOKMARKS); | 20 AddKnownCategory(KnownCategories::BOOKMARKS); |
28 AddKnownCategory(KnownCategories::PHYSICAL_WEB_PAGES); | 21 AddKnownCategory(KnownCategories::PHYSICAL_WEB_PAGES); |
29 | 22 |
30 DCHECK_EQ(static_cast<size_t>(KnownCategories::LOCAL_CATEGORIES_COUNT), | 23 DCHECK_EQ(static_cast<size_t>(KnownCategories::LOCAL_CATEGORIES_COUNT), |
31 ordered_categories_.size()); | 24 ordered_categories_.size()); |
(...skipping 27 matching lines...) Expand all Loading... |
59 | 52 |
60 bool CategoryFactory::CompareCategories(const Category& left, | 53 bool CategoryFactory::CompareCategories(const Category& left, |
61 const Category& right) const { | 54 const Category& right) const { |
62 if (left == right) | 55 if (left == right) |
63 return false; | 56 return false; |
64 return std::find(ordered_categories_.begin(), ordered_categories_.end(), | 57 return std::find(ordered_categories_.begin(), ordered_categories_.end(), |
65 left) < std::find(ordered_categories_.begin(), | 58 left) < std::find(ordered_categories_.begin(), |
66 ordered_categories_.end(), right); | 59 ordered_categories_.end(), right); |
67 } | 60 } |
68 | 61 |
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 | |
95 //////////////////////////////////////////////////////////////////////////////// | 62 //////////////////////////////////////////////////////////////////////////////// |
96 // Private methods | 63 // Private methods |
97 | 64 |
98 bool CategoryFactory::CategoryExists(int id) { | 65 bool CategoryFactory::CategoryExists(int id) { |
99 return std::find(ordered_categories_.begin(), ordered_categories_.end(), | 66 return std::find(ordered_categories_.begin(), ordered_categories_.end(), |
100 Category(id)) != ordered_categories_.end(); | 67 Category(id)) != ordered_categories_.end(); |
101 } | 68 } |
102 | 69 |
103 void CategoryFactory::AddKnownCategory(KnownCategories known_category) { | 70 void CategoryFactory::AddKnownCategory(KnownCategories known_category) { |
104 InternalFromID(static_cast<int>(known_category)); | 71 InternalFromID(static_cast<int>(known_category)); |
105 } | 72 } |
106 | 73 |
107 Category CategoryFactory::InternalFromID(int id) { | 74 Category CategoryFactory::InternalFromID(int id) { |
108 auto it = std::find(ordered_categories_.begin(), ordered_categories_.end(), | 75 auto it = std::find(ordered_categories_.begin(), ordered_categories_.end(), |
109 Category(id)); | 76 Category(id)); |
110 if (it != ordered_categories_.end()) | 77 if (it != ordered_categories_.end()) |
111 return *it; | 78 return *it; |
112 | 79 |
113 Category category(id); | 80 Category category(id); |
114 ordered_categories_.push_back(category); | 81 ordered_categories_.push_back(category); |
115 return category; | 82 return category; |
116 } | 83 } |
117 | 84 |
118 } // namespace ntp_snippets | 85 } // namespace ntp_snippets |
OLD | NEW |