| 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. Currently creation date fallback |
| 74 // must be active in order for older bookmarks to be incorporated to meet this |
| 75 // min. |
| 83 int GetMinCount() { | 76 int GetMinCount() { |
| 84 std::string min_count_string = variations::GetVariationParamValueByFeature( | 77 return GetParamAsInt(ntp_snippets::kBookmarkSuggestionsFeature, |
| 85 ntp_snippets::kBookmarkSuggestionsFeature, kMinBookmarksParamName); | 78 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 } | 79 } |
| 94 | 80 |
| 95 bool ShouldShowIfEmpty() { | 81 bool ShouldShowIfEmpty() { |
| 96 std::string show_if_empty = variations::GetVariationParamValueByFeature( | 82 std::string show_if_empty = variations::GetVariationParamValueByFeature( |
| 97 ntp_snippets::kBookmarkSuggestionsFeature, kShowIfEmptyParamName); | 83 ntp_snippets::kBookmarkSuggestionsFeature, kShowIfEmptyParamName); |
| 98 if (show_if_empty.empty() || show_if_empty == "false") | 84 if (show_if_empty.empty() || show_if_empty == "false") |
| 99 return false; | 85 return false; |
| 100 if (show_if_empty == "true") | 86 if (show_if_empty == "true") |
| 101 return true; | 87 return true; |
| 102 | 88 |
| 103 LOG(WARNING) << "Failed to parse show if empty " << show_if_empty; | 89 LOG(WARNING) << "Failed to parse show if empty " << show_if_empty; |
| 104 return false; | 90 return false; |
| 105 } | 91 } |
| 106 | 92 |
| 107 } // namespace | 93 } // namespace |
| 108 | 94 |
| 109 namespace ntp_snippets { | |
| 110 | |
| 111 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( | 95 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( |
| 112 ContentSuggestionsProvider::Observer* observer, | 96 ContentSuggestionsProvider::Observer* observer, |
| 113 CategoryFactory* category_factory, | 97 CategoryFactory* category_factory, |
| 114 bookmarks::BookmarkModel* bookmark_model, | 98 bookmarks::BookmarkModel* bookmark_model, |
| 115 PrefService* pref_service) | 99 PrefService* pref_service) |
| 116 : ContentSuggestionsProvider(observer, category_factory), | 100 : ContentSuggestionsProvider(observer, category_factory), |
| 117 category_status_(CategoryStatus::AVAILABLE_LOADING), | 101 category_status_(CategoryStatus::AVAILABLE_LOADING), |
| 118 provided_category_( | 102 provided_category_( |
| 119 category_factory->FromKnownCategory(KnownCategories::BOOKMARKS)), | 103 category_factory->FromKnownCategory(KnownCategories::BOOKMARKS)), |
| 120 bookmark_model_(bookmark_model), | 104 bookmark_model_(bookmark_model), |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 CategoryStatus BookmarkSuggestionsProvider::GetCategoryStatus( | 140 CategoryStatus BookmarkSuggestionsProvider::GetCategoryStatus( |
| 157 Category category) { | 141 Category category) { |
| 158 DCHECK_EQ(category, provided_category_); | 142 DCHECK_EQ(category, provided_category_); |
| 159 return category_status_; | 143 return category_status_; |
| 160 } | 144 } |
| 161 | 145 |
| 162 CategoryInfo BookmarkSuggestionsProvider::GetCategoryInfo(Category category) { | 146 CategoryInfo BookmarkSuggestionsProvider::GetCategoryInfo(Category category) { |
| 163 return CategoryInfo( | 147 return CategoryInfo( |
| 164 l10n_util::GetStringUTF16(IDS_NTP_BOOKMARK_SUGGESTIONS_SECTION_HEADER), | 148 l10n_util::GetStringUTF16(IDS_NTP_BOOKMARK_SUGGESTIONS_SECTION_HEADER), |
| 165 ContentSuggestionsCardLayout::MINIMAL_CARD, | 149 ContentSuggestionsCardLayout::MINIMAL_CARD, |
| 166 /* has_more_button */ true, | 150 /*has_more_button=*/true, |
| 167 /* show_if_empty */ ShouldShowIfEmpty() && | 151 /*show_if_empty=*/ShouldShowIfEmpty() && bookmark_model_->HasBookmarks()); |
| 168 bookmark_model_->HasBookmarks()); | |
| 169 // TODO(treib): Setting show_if_empty to true is a temporary hack, see | 152 // TODO(treib): Setting show_if_empty to true is a temporary hack, see |
| 170 // crbug.com/640568. | 153 // crbug.com/640568. |
| 171 } | 154 } |
| 172 | 155 |
| 173 void BookmarkSuggestionsProvider::DismissSuggestion( | 156 void BookmarkSuggestionsProvider::DismissSuggestion( |
| 174 const std::string& suggestion_id) { | 157 const std::string& suggestion_id) { |
| 175 DCHECK(bookmark_model_->loaded()); | 158 DCHECK(bookmark_model_->loaded()); |
| 176 GURL url(GetWithinCategoryIDFromUniqueID(suggestion_id)); | 159 GURL url(GetWithinCategoryIDFromUniqueID(suggestion_id)); |
| 177 MarkBookmarksDismissed(bookmark_model_, url); | 160 MarkBookmarksDismissed(bookmark_model_, url); |
| 178 } | 161 } |
| (...skipping 142 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 |