Index: components/ntp_snippets/category_factory.cc |
diff --git a/components/ntp_snippets/category_factory.cc b/components/ntp_snippets/category_factory.cc |
index b45fc4a8a8bb785403167dfe29e29d4b1ff2c1c1..0c01dfee8d59045e0ac4e598c198fe8fbbde6a88 100644 |
--- a/components/ntp_snippets/category_factory.cc |
+++ b/components/ntp_snippets/category_factory.cc |
@@ -7,9 +7,18 @@ |
#include <algorithm> |
#include "base/logging.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/strings/stringprintf.h" |
namespace ntp_snippets { |
+namespace { |
+ |
+const char kCombinedIDFormat[] = "%d|%s"; |
+const char kSeparator = '|'; |
+ |
+} // namespace |
+ |
CategoryFactory::CategoryFactory() { |
// Add all local categories in a fixed order. |
AddKnownCategory(KnownCategories::DOWNLOADS); |
@@ -57,6 +66,32 @@ bool CategoryFactory::CompareCategories(const Category& left, |
ordered_categories_.end(), right); |
} |
+std::string CategoryFactory::MakeUniqueID( |
+ Category category, |
+ const std::string& within_category_id) const { |
+ return base::StringPrintf(kCombinedIDFormat, category.id(), |
+ within_category_id.c_str()); |
+} |
+ |
+Category CategoryFactory::GetCategoryFromUniqueID( |
+ const std::string& unique_id) { |
+ size_t colon_index = unique_id.find(kSeparator); |
+ DCHECK_NE(std::string::npos, colon_index) << "Not a valid unique_id: " |
+ << unique_id; |
+ int category = -1; |
+ bool ret = base::StringToInt(unique_id.substr(0, colon_index), &category); |
+ DCHECK(ret) << "Non-numeric category part in unique_id: " << unique_id; |
+ return FromIDValue(category); |
+} |
+ |
+std::string CategoryFactory::GetWithinCategoryIDFromUniqueID( |
+ const std::string& unique_id) const { |
+ size_t colon_index = unique_id.find(kSeparator); |
+ DCHECK_NE(std::string::npos, colon_index) << "Not a valid unique_id: " |
+ << unique_id; |
+ return unique_id.substr(colon_index + 1); |
+} |
+ |
//////////////////////////////////////////////////////////////////////////////// |
// Private methods |