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/bookmarks/bookmark_suggestions_provider.h" | 5 #include "components/ntp_snippets/bookmarks/bookmark_suggestions_provider.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 11 matching lines...) Expand all Loading... | |
22 #include "components/prefs/pref_registry_simple.h" | 22 #include "components/prefs/pref_registry_simple.h" |
23 #include "components/prefs/pref_service.h" | 23 #include "components/prefs/pref_service.h" |
24 #include "components/variations/variations_associated_data.h" | 24 #include "components/variations/variations_associated_data.h" |
25 #include "grit/components_strings.h" | 25 #include "grit/components_strings.h" |
26 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
27 #include "ui/gfx/image/image.h" | 27 #include "ui/gfx/image/image.h" |
28 | 28 |
29 using bookmarks::BookmarkModel; | 29 using bookmarks::BookmarkModel; |
30 using bookmarks::BookmarkNode; | 30 using bookmarks::BookmarkNode; |
31 | 31 |
32 namespace ntp_snippets { | |
33 | |
32 namespace { | 34 namespace { |
33 | 35 |
34 const int kMaxBookmarks = 10; | 36 const int kMaxBookmarks = 10; |
35 const int kMinBookmarks = 3; | 37 const int kMinBookmarks = 3; |
36 const int kMaxBookmarkAgeInDays = 42; | 38 const int kMaxBookmarkAgeInDays = 42; |
37 const int kUseCreationDateFallbackForDays = 0; | 39 const int kUseCreationDateFallbackForDays = 0; |
38 | 40 |
39 const char* kMaxBookmarksParamName = "bookmarks_max_count"; | 41 const char* kMaxBookmarksParamName = "bookmarks_max_count"; |
40 const char* kMinBookmarksParamName = "bookmarks_min_count"; | 42 const char* kMinBookmarksParamName = "bookmarks_min_count"; |
41 const char* kMaxBookmarkAgeInDaysParamName = "bookmarks_max_age_in_days"; | 43 const char* kMaxBookmarkAgeInDaysParamName = "bookmarks_max_age_in_days"; |
42 const char* kUseCreationDateFallbackForDaysParamName = | 44 const char* kUseCreationDateFallbackForDaysParamName = |
43 "bookmarks_creation_date_fallback_days"; | 45 "bookmarks_creation_date_fallback_days"; |
44 const char* kShowIfEmptyParamName = "bookmarks_show_if_empty"; | 46 const char* kShowIfEmptyParamName = "bookmarks_show_if_empty"; |
45 | 47 |
46 base::Time GetThresholdTime() { | 48 base::Time GetThresholdTime() { |
battre
2016/08/29 09:48:26
Just FYI. From the perspective of somebody who is
skym
2016/09/15 23:18:17
Added comments!
| |
47 std::string age_in_days_string = variations::GetVariationParamValueByFeature( | 49 return base::Time::Now() - |
48 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarkAgeInDaysParamName); | 50 base::TimeDelta::FromDays(GetParamAsInt( |
49 int age_in_days = 0; | 51 ntp_snippets::kBookmarkSuggestionsFeature, |
50 if (!base::StringToInt(age_in_days_string, &age_in_days)) { | 52 kMaxBookmarkAgeInDaysParamName, kMaxBookmarkAgeInDays)); |
51 if (!age_in_days_string.empty()) | |
52 LOG(WARNING) << "Failed to parse bookmark age " << age_in_days_string; | |
53 age_in_days = kMaxBookmarkAgeInDays; | |
54 } | |
55 return base::Time::Now() - base::TimeDelta::FromDays(age_in_days); | |
56 } | 53 } |
57 | 54 |
58 int UseCreationDateFallbackForDays() { | 55 int UseCreationDateFallbackForDays() { |
59 std::string days_string = variations::GetVariationParamValueByFeature( | 56 return GetParamAsInt(ntp_snippets::kBookmarkSuggestionsFeature, |
60 ntp_snippets::kBookmarkSuggestionsFeature, | 57 kUseCreationDateFallbackForDaysParamName, |
61 kUseCreationDateFallbackForDaysParamName); | 58 kUseCreationDateFallbackForDays); |
62 int days = 0; | |
63 if (!base::StringToInt(days_string, &days)) { | |
64 if (!days_string.empty()) | |
65 LOG(WARNING) << "Failed to parse bookmark fallback days " << days_string; | |
66 days = kUseCreationDateFallbackForDays; | |
67 } | |
68 return days; | |
69 } | 59 } |
70 | 60 |
71 int GetMaxCount() { | 61 int GetMaxCount() { |
72 std::string max_count_string = variations::GetVariationParamValueByFeature( | 62 return GetParamAsInt(ntp_snippets::kBookmarkSuggestionsFeature, |
73 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarksParamName); | 63 kMaxBookmarksParamName, kMaxBookmarks); |
74 int max_count = 0; | |
75 if (base::StringToInt(max_count_string, &max_count)) | |
76 return max_count; | |
77 if (!max_count_string.empty()) | |
78 LOG(WARNING) << "Failed to parse max bookmarks count " << max_count_string; | |
79 | |
80 return kMaxBookmarks; | |
81 } | 64 } |
82 | 65 |
83 int GetMinCount() { | 66 int GetMinCount() { |
84 std::string min_count_string = variations::GetVariationParamValueByFeature( | 67 return GetParamAsInt(ntp_snippets::kBookmarkSuggestionsFeature, |
85 ntp_snippets::kBookmarkSuggestionsFeature, kMinBookmarksParamName); | 68 kMinBookmarksParamName, kMinBookmarks); |
86 int min_count = 0; | |
87 if (base::StringToInt(min_count_string, &min_count)) | |
88 return min_count; | |
89 if (!min_count_string.empty()) | |
90 LOG(WARNING) << "Failed to parse min bookmarks count " << min_count_string; | |
91 | |
92 return kMinBookmarks; | |
93 } | 69 } |
94 | 70 |
95 bool ShouldShowIfEmpty() { | 71 bool ShouldShowIfEmpty() { |
96 std::string show_if_empty = variations::GetVariationParamValueByFeature( | 72 std::string show_if_empty = variations::GetVariationParamValueByFeature( |
97 ntp_snippets::kBookmarkSuggestionsFeature, kShowIfEmptyParamName); | 73 ntp_snippets::kBookmarkSuggestionsFeature, kShowIfEmptyParamName); |
98 if (show_if_empty.empty() || show_if_empty == "false") | 74 if (show_if_empty.empty() || show_if_empty == "false") |
99 return false; | 75 return false; |
100 if (show_if_empty == "true") | 76 if (show_if_empty == "true") |
101 return true; | 77 return true; |
102 | 78 |
103 LOG(WARNING) << "Failed to parse show if empty " << show_if_empty; | 79 LOG(WARNING) << "Failed to parse show if empty " << show_if_empty; |
104 return false; | 80 return false; |
105 } | 81 } |
106 | 82 |
107 } // namespace | 83 } // namespace |
108 | 84 |
109 namespace ntp_snippets { | |
110 | |
111 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( | 85 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( |
112 ContentSuggestionsProvider::Observer* observer, | 86 ContentSuggestionsProvider::Observer* observer, |
113 CategoryFactory* category_factory, | 87 CategoryFactory* category_factory, |
114 bookmarks::BookmarkModel* bookmark_model, | 88 bookmarks::BookmarkModel* bookmark_model, |
115 PrefService* pref_service) | 89 PrefService* pref_service) |
116 : ContentSuggestionsProvider(observer, category_factory), | 90 : ContentSuggestionsProvider(observer, category_factory), |
117 category_status_(CategoryStatus::AVAILABLE_LOADING), | 91 category_status_(CategoryStatus::AVAILABLE_LOADING), |
118 provided_category_( | 92 provided_category_( |
119 category_factory->FromKnownCategory(KnownCategories::BOOKMARKS)), | 93 category_factory->FromKnownCategory(KnownCategories::BOOKMARKS)), |
120 bookmark_model_(bookmark_model), | 94 bookmark_model_(bookmark_model), |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 | 277 |
304 void BookmarkSuggestionsProvider::NotifyStatusChanged( | 278 void BookmarkSuggestionsProvider::NotifyStatusChanged( |
305 CategoryStatus new_status) { | 279 CategoryStatus new_status) { |
306 if (category_status_ == new_status) | 280 if (category_status_ == new_status) |
307 return; | 281 return; |
308 category_status_ = new_status; | 282 category_status_ = new_status; |
309 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | 283 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
310 } | 284 } |
311 | 285 |
312 } // namespace ntp_snippets | 286 } // namespace ntp_snippets |
OLD | NEW |