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