Chromium Code Reviews| 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" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "base/time/time.h" | |
| 15 #include "components/bookmarks/browser/bookmark_model.h" | 16 #include "components/bookmarks/browser/bookmark_model.h" |
| 16 #include "components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h" | 17 #include "components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h" |
| 17 #include "components/ntp_snippets/category_factory.h" | 18 #include "components/ntp_snippets/category_factory.h" |
| 18 #include "components/ntp_snippets/content_suggestion.h" | 19 #include "components/ntp_snippets/content_suggestion.h" |
| 19 #include "components/ntp_snippets/features.h" | 20 #include "components/ntp_snippets/features.h" |
| 21 #include "components/ntp_snippets/pref_names.h" | |
| 22 #include "components/prefs/pref_registry_simple.h" | |
| 23 #include "components/prefs/pref_service.h" | |
| 20 #include "components/variations/variations_associated_data.h" | 24 #include "components/variations/variations_associated_data.h" |
| 21 #include "grit/components_strings.h" | 25 #include "grit/components_strings.h" |
| 22 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
| 23 #include "ui/gfx/image/image.h" | 27 #include "ui/gfx/image/image.h" |
| 24 | 28 |
| 25 using bookmarks::BookmarkModel; | 29 using bookmarks::BookmarkModel; |
| 26 using bookmarks::BookmarkNode; | 30 using bookmarks::BookmarkNode; |
| 27 | 31 |
| 28 namespace { | 32 namespace { |
| 29 | 33 |
| 30 const int kMaxBookmarks = 10; | 34 const int kMaxBookmarks = 10; |
| 31 const int kMinBookmarks = 3; | 35 const int kMinBookmarks = 3; |
| 32 const int kMaxBookmarkAgeInDays = 42; | 36 const int kMaxBookmarkAgeInDays = 42; |
| 37 const int kUseCreationDateFallbackForDays = 42; | |
| 33 | 38 |
| 34 const char* kMaxBookmarksParamName = "max_count"; | 39 const char* kMaxBookmarksParamName = "bookmarks_max_count"; |
| 35 const char* kMinBookmarksParamName = "min_count"; | 40 const char* kMinBookmarksParamName = "bookmarks_min_count"; |
| 36 const char* kMaxBookmarkAgeInDaysParamName = "max_age_in_days"; | 41 const char* kMaxBookmarkAgeInDaysParamName = "bookmarks_max_age_in_days"; |
| 37 | 42 |
| 38 base::Time GetThresholdTime() { | 43 base::Time GetThresholdTime() { |
| 39 std::string age_in_days_string = variations::GetVariationParamValueByFeature( | 44 std::string age_in_days_string = variations::GetVariationParamValueByFeature( |
| 40 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarkAgeInDaysParamName); | 45 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarkAgeInDaysParamName); |
| 41 int age_in_days = 0; | 46 int age_in_days = 0; |
| 42 if (!base::StringToInt(age_in_days_string, &age_in_days)) | 47 if (!base::StringToInt(age_in_days_string, &age_in_days)) { |
| 48 if (!age_in_days_string.empty()) | |
| 49 LOG(WARNING) << "Failed to parse bookmark age " << age_in_days_string; | |
| 43 age_in_days = kMaxBookmarkAgeInDays; | 50 age_in_days = kMaxBookmarkAgeInDays; |
| 44 | 51 } |
| 45 return base::Time::Now() - base::TimeDelta::FromDays(age_in_days); | 52 return base::Time::Now() - base::TimeDelta::FromDays(age_in_days); |
| 46 } | 53 } |
| 47 | 54 |
| 48 int GetMaxCount() { | 55 int GetMaxCount() { |
| 49 std::string max_count_string = variations::GetVariationParamValueByFeature( | 56 std::string max_count_string = variations::GetVariationParamValueByFeature( |
| 50 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarksParamName); | 57 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarksParamName); |
| 51 int max_count = 0; | 58 int max_count = 0; |
| 52 if (base::StringToInt(max_count_string, &max_count)) | 59 if (base::StringToInt(max_count_string, &max_count)) |
| 53 return max_count; | 60 return max_count; |
| 61 if (!max_count_string.empty()) | |
| 62 LOG(WARNING) << "Failed to parse max bookmarks count" << max_count_string; | |
| 54 | 63 |
| 55 return kMaxBookmarks; | 64 return kMaxBookmarks; |
| 56 } | 65 } |
| 57 | 66 |
| 58 int GetMinCount() { | 67 int GetMinCount() { |
| 59 std::string min_count_string = variations::GetVariationParamValueByFeature( | 68 std::string min_count_string = variations::GetVariationParamValueByFeature( |
| 60 ntp_snippets::kBookmarkSuggestionsFeature, kMinBookmarksParamName); | 69 ntp_snippets::kBookmarkSuggestionsFeature, kMinBookmarksParamName); |
| 61 int min_count = 0; | 70 int min_count = 0; |
| 62 if (base::StringToInt(min_count_string, &min_count)) | 71 if (base::StringToInt(min_count_string, &min_count)) |
| 63 return min_count; | 72 return min_count; |
| 73 if (!min_count_string.empty()) | |
| 74 LOG(WARNING) << "Failed to parse min bookmarks count" << min_count_string; | |
| 64 | 75 |
| 65 return kMinBookmarks; | 76 return kMinBookmarks; |
| 66 } | 77 } |
| 67 | 78 |
| 68 } // namespace | 79 } // namespace |
| 69 | 80 |
| 70 namespace ntp_snippets { | 81 namespace ntp_snippets { |
| 71 | 82 |
| 72 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( | 83 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( |
| 73 ContentSuggestionsProvider::Observer* observer, | 84 ContentSuggestionsProvider::Observer* observer, |
| 74 CategoryFactory* category_factory, | 85 CategoryFactory* category_factory, |
| 75 bookmarks::BookmarkModel* bookmark_model) | 86 bookmarks::BookmarkModel* bookmark_model, |
| 87 PrefService* pref_service) | |
| 76 : ContentSuggestionsProvider(observer, category_factory), | 88 : ContentSuggestionsProvider(observer, category_factory), |
| 77 category_status_(CategoryStatus::AVAILABLE_LOADING), | 89 category_status_(CategoryStatus::AVAILABLE_LOADING), |
| 78 provided_category_( | 90 provided_category_( |
| 79 category_factory->FromKnownCategory(KnownCategories::BOOKMARKS)), | 91 category_factory->FromKnownCategory(KnownCategories::BOOKMARKS)), |
| 80 bookmark_model_(bookmark_model), | 92 bookmark_model_(bookmark_model), |
| 81 fetch_requested_(false), | 93 fetch_requested_(false), |
| 82 end_of_list_last_visit_date_(GetThresholdTime()) { | 94 end_of_list_last_visit_date_(GetThresholdTime()) { |
| 95 base::Time first_M54_start; | |
|
Bernhard Bauer
2016/08/19 16:46:41
Nit: the "m" would still be lowercase.
Philipp Keck
2016/08/19 17:15:51
Done.
| |
| 96 if (pref_service->HasPrefPath(prefs::kBookmarksFirstM54Start)) { | |
| 97 first_M54_start = base::Time::FromInternalValue( | |
| 98 pref_service->GetInt64(prefs::kBookmarksFirstM54Start)); | |
|
Bernhard Bauer
2016/08/19 16:46:41
You could just get the value unconditionally and s
Philipp Keck
2016/08/19 17:15:51
:D That's what I did before. Marc argued for this
| |
| 99 } else { | |
| 100 first_M54_start = base::Time::Now(); | |
| 101 pref_service->SetInt64(prefs::kBookmarksFirstM54Start, | |
| 102 first_M54_start.ToInternalValue()); | |
| 103 } | |
| 104 base::TimeDelta time_since_first_M54_start = | |
| 105 base::Time::Now() - first_M54_start; | |
|
Bernhard Bauer
2016/08/19 16:46:41
OCD-me is a bit concerned about having two separat
Philipp Keck
2016/08/19 17:15:51
Done.
| |
| 106 creation_date_fallback_ = | |
| 107 time_since_first_M54_start.InDays() < kUseCreationDateFallbackForDays; | |
| 83 bookmark_model_->AddObserver(this); | 108 bookmark_model_->AddObserver(this); |
| 84 FetchBookmarks(); | 109 FetchBookmarks(); |
| 85 } | 110 } |
| 86 | 111 |
| 87 BookmarkSuggestionsProvider::~BookmarkSuggestionsProvider() { | 112 BookmarkSuggestionsProvider::~BookmarkSuggestionsProvider() { |
| 88 bookmark_model_->RemoveObserver(this); | 113 bookmark_model_->RemoveObserver(this); |
| 89 } | 114 } |
| 90 | 115 |
| 116 // static | |
| 117 void BookmarkSuggestionsProvider::RegisterProfilePrefs( | |
| 118 PrefRegistrySimple* registry) { | |
| 119 registry->RegisterInt64Pref(prefs::kBookmarksFirstM54Start, 0); | |
| 120 } | |
| 121 | |
| 91 //////////////////////////////////////////////////////////////////////////////// | 122 //////////////////////////////////////////////////////////////////////////////// |
| 92 // Private methods | 123 // Private methods |
| 93 | 124 |
| 94 std::vector<Category> BookmarkSuggestionsProvider::GetProvidedCategories() { | 125 std::vector<Category> BookmarkSuggestionsProvider::GetProvidedCategories() { |
| 95 return std::vector<Category>({provided_category_}); | 126 return std::vector<Category>({provided_category_}); |
| 96 } | 127 } |
| 97 | 128 |
| 98 CategoryStatus BookmarkSuggestionsProvider::GetCategoryStatus( | 129 CategoryStatus BookmarkSuggestionsProvider::GetCategoryStatus( |
| 99 Category category) { | 130 Category category) { |
| 100 DCHECK_EQ(category, provided_category_); | 131 DCHECK_EQ(category, provided_category_); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 fetch_requested_ = false; | 187 fetch_requested_ = false; |
| 157 FetchBookmarksInternal(); | 188 FetchBookmarksInternal(); |
| 158 } | 189 } |
| 159 } | 190 } |
| 160 | 191 |
| 161 void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo( | 192 void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo( |
| 162 BookmarkModel* model, | 193 BookmarkModel* model, |
| 163 const BookmarkNode* node) { | 194 const BookmarkNode* node) { |
| 164 // Store the last visit date of the node that is about to change. | 195 // Store the last visit date of the node that is about to change. |
| 165 node_to_change_last_visit_date_ = | 196 node_to_change_last_visit_date_ = |
| 166 GetLastVisitDateForBookmarkIfNotDismissed(node); | 197 GetLastVisitDateForBookmarkIfNotDismissed(node, creation_date_fallback_); |
| 167 } | 198 } |
| 168 | 199 |
| 169 void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged( | 200 void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged( |
| 170 BookmarkModel* model, | 201 BookmarkModel* model, |
| 171 const BookmarkNode* node) { | 202 const BookmarkNode* node) { |
| 172 base::Time time = GetLastVisitDateForBookmarkIfNotDismissed(node); | 203 base::Time time = |
| 204 GetLastVisitDateForBookmarkIfNotDismissed(node, creation_date_fallback_); | |
| 173 if (time == node_to_change_last_visit_date_ || | 205 if (time == node_to_change_last_visit_date_ || |
| 174 time < end_of_list_last_visit_date_) | 206 time < end_of_list_last_visit_date_) |
| 175 return; | 207 return; |
| 176 | 208 |
| 177 // Last visit date of a node has changed (and is relevant for the list), we | 209 // Last visit date of a node has changed (and is relevant for the list), we |
| 178 // should update the suggestions. | 210 // should update the suggestions. |
| 179 FetchBookmarks(); | 211 FetchBookmarks(); |
| 180 } | 212 } |
| 181 | 213 |
| 182 void BookmarkSuggestionsProvider::BookmarkNodeRemoved( | 214 void BookmarkSuggestionsProvider::BookmarkNodeRemoved( |
| 183 bookmarks::BookmarkModel* model, | 215 bookmarks::BookmarkModel* model, |
| 184 const bookmarks::BookmarkNode* parent, | 216 const bookmarks::BookmarkNode* parent, |
| 185 int old_index, | 217 int old_index, |
| 186 const bookmarks::BookmarkNode* node, | 218 const bookmarks::BookmarkNode* node, |
| 187 const std::set<GURL>& no_longer_bookmarked) { | 219 const std::set<GURL>& no_longer_bookmarked) { |
| 188 if (GetLastVisitDateForBookmarkIfNotDismissed(node) < | 220 if (GetLastVisitDateForBookmarkIfNotDismissed(node, creation_date_fallback_) < |
| 189 end_of_list_last_visit_date_) | 221 end_of_list_last_visit_date_) { |
| 190 return; | 222 return; |
| 223 } | |
| 191 | 224 |
| 192 // Some node from our list got deleted, we should update the suggestions. | 225 // Some node from our list got deleted, we should update the suggestions. |
| 193 FetchBookmarks(); | 226 FetchBookmarks(); |
| 194 } | 227 } |
| 195 | 228 |
| 196 ContentSuggestion BookmarkSuggestionsProvider::ConvertBookmark( | 229 ContentSuggestion BookmarkSuggestionsProvider::ConvertBookmark( |
| 197 const BookmarkNode* bookmark) { | 230 const BookmarkNode* bookmark) { |
| 198 ContentSuggestion suggestion( | 231 ContentSuggestion suggestion( |
| 199 MakeUniqueID(provided_category_, bookmark->url().spec()), | 232 MakeUniqueID(provided_category_, bookmark->url().spec()), |
| 200 bookmark->url()); | 233 bookmark->url()); |
| 201 | 234 |
| 202 suggestion.set_title(bookmark->GetTitle()); | 235 suggestion.set_title(bookmark->GetTitle()); |
| 203 suggestion.set_snippet_text(base::string16()); | 236 suggestion.set_snippet_text(base::string16()); |
| 204 suggestion.set_publish_date(GetLastVisitDateForBookmark(bookmark)); | 237 suggestion.set_publish_date( |
| 238 GetLastVisitDateForBookmark(bookmark, creation_date_fallback_)); | |
| 205 suggestion.set_publisher_name(base::UTF8ToUTF16(bookmark->url().host())); | 239 suggestion.set_publisher_name(base::UTF8ToUTF16(bookmark->url().host())); |
| 206 return suggestion; | 240 return suggestion; |
| 207 } | 241 } |
| 208 | 242 |
| 209 void BookmarkSuggestionsProvider::FetchBookmarksInternal() { | 243 void BookmarkSuggestionsProvider::FetchBookmarksInternal() { |
| 210 DCHECK(bookmark_model_->loaded()); | 244 DCHECK(bookmark_model_->loaded()); |
| 211 | 245 |
| 212 NotifyStatusChanged(CategoryStatus::AVAILABLE); | 246 NotifyStatusChanged(CategoryStatus::AVAILABLE); |
| 213 | 247 |
| 214 base::Time threshold_time = GetThresholdTime(); | 248 base::Time threshold_time = GetThresholdTime(); |
| 215 std::vector<const BookmarkNode*> bookmarks = GetRecentlyVisitedBookmarks( | 249 std::vector<const BookmarkNode*> bookmarks = |
| 216 bookmark_model_, GetMinCount(), GetMaxCount(), | 250 GetRecentlyVisitedBookmarks(bookmark_model_, GetMinCount(), GetMaxCount(), |
| 217 threshold_time); | 251 threshold_time, creation_date_fallback_); |
| 218 | 252 |
| 219 std::vector<ContentSuggestion> suggestions; | 253 std::vector<ContentSuggestion> suggestions; |
| 220 for (const BookmarkNode* bookmark : bookmarks) | 254 for (const BookmarkNode* bookmark : bookmarks) |
| 221 suggestions.emplace_back(ConvertBookmark(bookmark)); | 255 suggestions.emplace_back(ConvertBookmark(bookmark)); |
| 222 | 256 |
| 223 if (suggestions.empty()) | 257 if (suggestions.empty()) |
| 224 end_of_list_last_visit_date_ = threshold_time; | 258 end_of_list_last_visit_date_ = threshold_time; |
| 225 else | 259 else |
| 226 end_of_list_last_visit_date_ = suggestions.back().publish_date(); | 260 end_of_list_last_visit_date_ = suggestions.back().publish_date(); |
| 227 | 261 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 238 | 272 |
| 239 void BookmarkSuggestionsProvider::NotifyStatusChanged( | 273 void BookmarkSuggestionsProvider::NotifyStatusChanged( |
| 240 CategoryStatus new_status) { | 274 CategoryStatus new_status) { |
| 241 if (category_status_ == new_status) | 275 if (category_status_ == new_status) |
| 242 return; | 276 return; |
| 243 category_status_ = new_status; | 277 category_status_ = new_status; |
| 244 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | 278 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
| 245 } | 279 } |
| 246 | 280 |
| 247 } // namespace ntp_snippets | 281 } // namespace ntp_snippets |
| OLD | NEW |