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