Chromium Code Reviews| 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"; |
| 14 | 15 |
| 15 } // namespace | 16 } // namespace |
| 16 | 17 |
| 17 ContentSuggestionsProvider::ContentSuggestionsProvider( | 18 ContentSuggestionsProvider::ContentSuggestionsProvider( |
| 18 const std::vector<ContentSuggestionsCategory>& provided_categories) | 19 const std::vector<ContentSuggestionsCategory>& provided_categories) |
| 19 : provided_categories_(provided_categories) {} | 20 : provided_categories_(provided_categories) {} |
| 20 | 21 |
| 21 ContentSuggestionsProvider::~ContentSuggestionsProvider() {} | 22 ContentSuggestionsProvider::~ContentSuggestionsProvider() {} |
| 22 | 23 |
| 23 std::string ContentSuggestionsProvider::MakeUniqueID( | 24 std::string ContentSuggestionsProvider::MakeUniqueID( |
| 24 ContentSuggestionsCategory category, | 25 ContentSuggestionsCategory category, |
| 25 const std::string& within_category_id) { | 26 const std::string& within_category_id) { |
| 26 return base::StringPrintf(kCombinedIDFormat, | 27 return base::StringPrintf(kCombinedIDFormat, |
| 27 static_cast<int>(category), | 28 static_cast<int>(category), |
| 28 within_category_id.c_str()); | 29 within_category_id.c_str()); |
| 29 } | 30 } |
| 30 | 31 |
| 32 ContentSuggestionsCategory ContentSuggestionsProvider::GetCategoryFromUniqueID( | |
| 33 const std::string& unique_id) { | |
| 34 size_t colon_index = unique_id.find("|"); | |
|
Marc Treib
2016/07/19 09:30:24
Make the pipe a constant? (kSeparator)
Philipp Keck
2016/07/19 12:21:50
Done.
| |
| 35 DCHECK_NE(std::string::npos, colon_index) << "Not a valid unique_id: " | |
| 36 << unique_id; | |
| 37 int category; | |
| 38 DCHECK(base::StringToInt(unique_id.substr(0, colon_index), &category)) | |
| 39 << "Non-numeric category part in unique_id: " << unique_id; | |
| 40 DCHECK(0 <= category && category < int(ContentSuggestionsCategory::COUNT)) | |
| 41 << "Category does not exist: " << category; | |
| 42 return ContentSuggestionsCategory(category); | |
| 43 } | |
| 44 | |
| 45 std::string ContentSuggestionsProvider::GetWithinCategoryIDFromUniqueID( | |
| 46 const std::string& unique_id) { | |
| 47 size_t colon_index = unique_id.find("|"); | |
| 48 DCHECK_NE(std::string::npos, colon_index) << "Not a valid unique_id: " | |
| 49 << unique_id; | |
| 50 return unique_id.substr(colon_index + 1); | |
| 51 } | |
| 52 | |
| 31 } // namespace ntp_snippets | 53 } // namespace ntp_snippets |
| OLD | NEW |