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/content_suggestions_provider.h" | 5 #include "components/ntp_snippets/content_suggestions_provider.h" |
6 | 6 |
| 7 #include "base/strings/string_number_conversions.h" |
7 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
8 | 9 |
9 namespace ntp_snippets { | 10 namespace ntp_snippets { |
10 | 11 |
11 namespace { | 12 namespace { |
12 | 13 |
13 const char kCombinedIDFormat[] = "%d:%s"; | 14 const char kCombinedIDFormat[] = "%d|%s"; |
| 15 const char kSeparator = '|'; |
14 | 16 |
15 } // namespace | 17 } // namespace |
16 | 18 |
17 ContentSuggestionsProvider::ContentSuggestionsProvider( | 19 ContentSuggestionsProvider::ContentSuggestionsProvider( |
18 const std::vector<ContentSuggestionsCategory>& provided_categories) | 20 const std::vector<ContentSuggestionsCategory>& provided_categories) |
19 : provided_categories_(provided_categories) {} | 21 : provided_categories_(provided_categories) {} |
20 | 22 |
21 ContentSuggestionsProvider::~ContentSuggestionsProvider() {} | 23 ContentSuggestionsProvider::~ContentSuggestionsProvider() {} |
22 | 24 |
| 25 // static |
23 std::string ContentSuggestionsProvider::MakeUniqueID( | 26 std::string ContentSuggestionsProvider::MakeUniqueID( |
24 ContentSuggestionsCategory category, | 27 ContentSuggestionsCategory category, |
25 const std::string& within_category_id) { | 28 const std::string& within_category_id) { |
26 return base::StringPrintf(kCombinedIDFormat, | 29 return base::StringPrintf(kCombinedIDFormat, |
27 static_cast<int>(category), | 30 static_cast<int>(category), |
28 within_category_id.c_str()); | 31 within_category_id.c_str()); |
29 } | 32 } |
30 | 33 |
| 34 // static |
| 35 ContentSuggestionsCategory ContentSuggestionsProvider::GetCategoryFromUniqueID( |
| 36 const std::string& unique_id) { |
| 37 size_t colon_index = unique_id.find(kSeparator); |
| 38 DCHECK_NE(std::string::npos, colon_index) << "Not a valid unique_id: " |
| 39 << unique_id; |
| 40 int category = -1; |
| 41 DCHECK(base::StringToInt(unique_id.substr(0, colon_index), &category)) |
| 42 << "Non-numeric category part in unique_id: " << unique_id; |
| 43 DCHECK(0 <= category && category < int(ContentSuggestionsCategory::COUNT)) |
| 44 << "Category does not exist: " << category; |
| 45 return ContentSuggestionsCategory(category); |
| 46 } |
| 47 |
| 48 // static |
| 49 std::string ContentSuggestionsProvider::GetWithinCategoryIDFromUniqueID( |
| 50 const std::string& unique_id) { |
| 51 size_t colon_index = unique_id.find(kSeparator); |
| 52 DCHECK_NE(std::string::npos, colon_index) << "Not a valid unique_id: " |
| 53 << unique_id; |
| 54 return unique_id.substr(colon_index + 1); |
| 55 } |
| 56 |
31 } // namespace ntp_snippets | 57 } // namespace ntp_snippets |
OLD | NEW |