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 |
48 // Any bookmark created or visited after this time will be considered recent. | |
49 // Note that bookmarks can be shown that do not meet this threshold. | |
46 base::Time GetThresholdTime() { | 50 base::Time GetThresholdTime() { |
47 std::string age_in_days_string = variations::GetVariationParamValueByFeature( | 51 return base::Time::Now() - |
48 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarkAgeInDaysParamName); | 52 base::TimeDelta::FromDays(GetParamAsInt( |
49 int age_in_days = 0; | 53 ntp_snippets::kBookmarkSuggestionsFeature, |
50 if (!base::StringToInt(age_in_days_string, &age_in_days)) { | 54 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 } | 55 } |
57 | 56 |
57 // The number of days used as a threshold where if this is larger than the time | |
58 // since M54 started, then the creation timestamp of a bookmark be used as a | |
59 // fallback if no last visited timestamp is present. | |
58 int UseCreationDateFallbackForDays() { | 60 int UseCreationDateFallbackForDays() { |
59 std::string days_string = variations::GetVariationParamValueByFeature( | 61 return GetParamAsInt(ntp_snippets::kBookmarkSuggestionsFeature, |
60 ntp_snippets::kBookmarkSuggestionsFeature, | 62 kUseCreationDateFallbackForDaysParamName, |
61 kUseCreationDateFallbackForDaysParamName); | 63 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 } | 64 } |
70 | 65 |
66 // The maximum number of suggestions ever provided. | |
71 int GetMaxCount() { | 67 int GetMaxCount() { |
72 std::string max_count_string = variations::GetVariationParamValueByFeature( | 68 return GetParamAsInt(ntp_snippets::kBookmarkSuggestionsFeature, |
73 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarksParamName); | 69 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 } | 70 } |
82 | 71 |
72 // The minimum number of suggestions to try to provide. Depending on other | |
73 // parameters this may or not be respected. Currntly creation date fallback must | |
battre
2016/09/19 08:54:34
nit: Currently
skym
2016/09/19 18:54:47
Done.
| |
74 // be active in order for older bookmarks to be incorporated to meet this min. | |
83 int GetMinCount() { | 75 int GetMinCount() { |
84 std::string min_count_string = variations::GetVariationParamValueByFeature( | 76 return GetParamAsInt(ntp_snippets::kBookmarkSuggestionsFeature, |
85 ntp_snippets::kBookmarkSuggestionsFeature, kMinBookmarksParamName); | 77 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 } | 78 } |
94 | 79 |
95 bool ShouldShowIfEmpty() { | 80 bool ShouldShowIfEmpty() { |
96 std::string show_if_empty = variations::GetVariationParamValueByFeature( | 81 std::string show_if_empty = variations::GetVariationParamValueByFeature( |
97 ntp_snippets::kBookmarkSuggestionsFeature, kShowIfEmptyParamName); | 82 ntp_snippets::kBookmarkSuggestionsFeature, kShowIfEmptyParamName); |
98 if (show_if_empty.empty() || show_if_empty == "false") | 83 if (show_if_empty.empty() || show_if_empty == "false") |
99 return false; | 84 return false; |
100 if (show_if_empty == "true") | 85 if (show_if_empty == "true") |
101 return true; | 86 return true; |
102 | 87 |
103 LOG(WARNING) << "Failed to parse show if empty " << show_if_empty; | 88 LOG(WARNING) << "Failed to parse show if empty " << show_if_empty; |
104 return false; | 89 return false; |
105 } | 90 } |
106 | 91 |
107 } // namespace | 92 } // namespace |
108 | 93 |
109 namespace ntp_snippets { | |
110 | |
111 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( | 94 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( |
112 ContentSuggestionsProvider::Observer* observer, | 95 ContentSuggestionsProvider::Observer* observer, |
113 CategoryFactory* category_factory, | 96 CategoryFactory* category_factory, |
114 bookmarks::BookmarkModel* bookmark_model, | 97 bookmarks::BookmarkModel* bookmark_model, |
115 PrefService* pref_service) | 98 PrefService* pref_service) |
116 : ContentSuggestionsProvider(observer, category_factory), | 99 : ContentSuggestionsProvider(observer, category_factory), |
117 category_status_(CategoryStatus::AVAILABLE_LOADING), | 100 category_status_(CategoryStatus::AVAILABLE_LOADING), |
118 provided_category_( | 101 provided_category_( |
119 category_factory->FromKnownCategory(KnownCategories::BOOKMARKS)), | 102 category_factory->FromKnownCategory(KnownCategories::BOOKMARKS)), |
120 bookmark_model_(bookmark_model), | 103 bookmark_model_(bookmark_model), |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
321 | 304 |
322 void BookmarkSuggestionsProvider::NotifyStatusChanged( | 305 void BookmarkSuggestionsProvider::NotifyStatusChanged( |
323 CategoryStatus new_status) { | 306 CategoryStatus new_status) { |
324 if (category_status_ == new_status) | 307 if (category_status_ == new_status) |
325 return; | 308 return; |
326 category_status_ = new_status; | 309 category_status_ = new_status; |
327 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | 310 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
328 } | 311 } |
329 | 312 |
330 } // namespace ntp_snippets | 313 } // namespace ntp_snippets |
OLD | NEW |