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