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" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 | 24 |
| 25 using bookmarks::BookmarkModel; | 25 using bookmarks::BookmarkModel; |
| 26 using bookmarks::BookmarkNode; | 26 using bookmarks::BookmarkNode; |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 const int kMaxBookmarks = 10; | 30 const int kMaxBookmarks = 10; |
| 31 const int kMinBookmarks = 3; | 31 const int kMinBookmarks = 3; |
| 32 const int kMaxBookmarkAgeInDays = 42; | 32 const int kMaxBookmarkAgeInDays = 42; |
| 33 | 33 |
| 34 const char* kMaxBookmarksParamName = "max_count"; | 34 const char* kMaxBookmarksParamName = "ntp_bookmarks_max_count"; |
| 35 const char* kMinBookmarksParamName = "min_count"; | 35 const char* kMinBookmarksParamName = "ntp_bookmarks_min_count"; |
| 36 const char* kMaxBookmarkAgeInDaysParamName = "max_age_in_days"; | 36 const char* kMaxBookmarkAgeInDaysParamName = "ntp_bookmarks_max_age_in_days"; |
|
Marc Treib
2016/08/19 11:41:11
nit: I'd remove the "ntp_", that much is clear fro
Philipp Keck
2016/08/19 12:49:52
Done.
| |
| 37 | 37 |
| 38 base::Time GetThresholdTime() { | 38 base::Time GetThresholdTime() { |
| 39 std::string age_in_days_string = variations::GetVariationParamValueByFeature( | 39 std::string age_in_days_string = variations::GetVariationParamValueByFeature( |
| 40 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarkAgeInDaysParamName); | 40 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarkAgeInDaysParamName); |
| 41 int age_in_days = 0; | 41 int age_in_days = 0; |
| 42 if (!base::StringToInt(age_in_days_string, &age_in_days)) | 42 if (!base::StringToInt(age_in_days_string, &age_in_days)) |
| 43 age_in_days = kMaxBookmarkAgeInDays; | 43 age_in_days = kMaxBookmarkAgeInDays; |
| 44 | 44 |
| 45 return base::Time::Now() - base::TimeDelta::FromDays(age_in_days); | 45 return base::Time::Now() - base::TimeDelta::FromDays(age_in_days); |
| 46 } | 46 } |
| 47 | 47 |
| 48 int GetMaxCount() { | 48 int GetMaxCount() { |
| 49 std::string max_count_string = variations::GetVariationParamValueByFeature( | 49 std::string max_count_string = variations::GetVariationParamValueByFeature( |
| 50 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarksParamName); | 50 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarksParamName); |
| 51 int max_count = 0; | 51 int max_count = 0; |
| 52 if (base::StringToInt(max_count_string, &max_count)) | 52 if (base::StringToInt(max_count_string, &max_count)) |
| 53 return max_count; | 53 return max_count; |
| 54 | 54 |
| 55 return kMaxBookmarks; | 55 return kMaxBookmarks; |
| 56 } | 56 } |
| 57 | 57 |
| 58 int GetMinCount() { | 58 int GetMinCount() { |
| 59 std::string min_count_string = variations::GetVariationParamValueByFeature( | 59 std::string min_count_string = variations::GetVariationParamValueByFeature( |
| 60 ntp_snippets::kBookmarkSuggestionsFeature, kMinBookmarksParamName); | 60 ntp_snippets::kBookmarkSuggestionsFeature, kMinBookmarksParamName); |
| 61 int min_count = 0; | 61 int min_count = 0; |
| 62 if (base::StringToInt(min_count_string, &min_count)) | 62 if (base::StringToInt(min_count_string, &min_count)) |
| 63 return min_count; | 63 return min_count; |
| 64 if (!min_count_string.empty()) | |
| 65 LOG(WARNING) << "Failed to parse min bookmarks count" << min_count_string; | |
|
Marc Treib
2016/08/19 11:41:11
Add the same above for max_count please
Philipp Keck
2016/08/19 12:49:52
Done. Also for age_in_days_string (GetThresholdTim
| |
| 64 | 66 |
| 65 return kMinBookmarks; | 67 return kMinBookmarks; |
| 66 } | 68 } |
| 67 | 69 |
| 68 } // namespace | 70 } // namespace |
| 69 | 71 |
| 70 namespace ntp_snippets { | 72 namespace ntp_snippets { |
| 71 | 73 |
| 72 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( | 74 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( |
| 73 ContentSuggestionsProvider::Observer* observer, | 75 ContentSuggestionsProvider::Observer* observer, |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 if (fetch_requested_) { | 157 if (fetch_requested_) { |
| 156 fetch_requested_ = false; | 158 fetch_requested_ = false; |
| 157 FetchBookmarksInternal(); | 159 FetchBookmarksInternal(); |
| 158 } | 160 } |
| 159 } | 161 } |
| 160 | 162 |
| 161 void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo( | 163 void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo( |
| 162 BookmarkModel* model, | 164 BookmarkModel* model, |
| 163 const BookmarkNode* node) { | 165 const BookmarkNode* node) { |
| 164 // Store the last visit date of the node that is about to change. | 166 // Store the last visit date of the node that is about to change. |
| 165 node_to_change_last_visit_date_ = | 167 node_to_change_last_visit_date_ = GetLastVisitDateForBookmarkIfNotDismissed( |
| 166 GetLastVisitDateForBookmarkIfNotDismissed(node); | 168 node, /*creation_date_fallback=*/true); |
| 167 } | 169 } |
| 168 | 170 |
| 169 void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged( | 171 void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged( |
| 170 BookmarkModel* model, | 172 BookmarkModel* model, |
| 171 const BookmarkNode* node) { | 173 const BookmarkNode* node) { |
| 172 base::Time time = GetLastVisitDateForBookmarkIfNotDismissed(node); | 174 base::Time time = GetLastVisitDateForBookmarkIfNotDismissed( |
| 175 node, /*creation_date_fallback=*/true); | |
| 173 if (time == node_to_change_last_visit_date_ || | 176 if (time == node_to_change_last_visit_date_ || |
| 174 time < end_of_list_last_visit_date_) | 177 time < end_of_list_last_visit_date_) |
| 175 return; | 178 return; |
| 176 | 179 |
| 177 // Last visit date of a node has changed (and is relevant for the list), we | 180 // Last visit date of a node has changed (and is relevant for the list), we |
| 178 // should update the suggestions. | 181 // should update the suggestions. |
| 179 FetchBookmarks(); | 182 FetchBookmarks(); |
| 180 } | 183 } |
| 181 | 184 |
| 182 void BookmarkSuggestionsProvider::BookmarkNodeRemoved( | 185 void BookmarkSuggestionsProvider::BookmarkNodeRemoved( |
| 183 bookmarks::BookmarkModel* model, | 186 bookmarks::BookmarkModel* model, |
| 184 const bookmarks::BookmarkNode* parent, | 187 const bookmarks::BookmarkNode* parent, |
| 185 int old_index, | 188 int old_index, |
| 186 const bookmarks::BookmarkNode* node, | 189 const bookmarks::BookmarkNode* node, |
| 187 const std::set<GURL>& no_longer_bookmarked) { | 190 const std::set<GURL>& no_longer_bookmarked) { |
| 188 if (GetLastVisitDateForBookmarkIfNotDismissed(node) < | 191 if (GetLastVisitDateForBookmarkIfNotDismissed( |
| 189 end_of_list_last_visit_date_) | 192 node, /*creation_date_fallback=*/true) < |
| 193 end_of_list_last_visit_date_) { | |
| 190 return; | 194 return; |
| 195 } | |
| 191 | 196 |
| 192 // Some node from our list got deleted, we should update the suggestions. | 197 // Some node from our list got deleted, we should update the suggestions. |
| 193 FetchBookmarks(); | 198 FetchBookmarks(); |
| 194 } | 199 } |
| 195 | 200 |
| 196 ContentSuggestion BookmarkSuggestionsProvider::ConvertBookmark( | 201 ContentSuggestion BookmarkSuggestionsProvider::ConvertBookmark( |
| 197 const BookmarkNode* bookmark) { | 202 const BookmarkNode* bookmark) { |
| 198 ContentSuggestion suggestion( | 203 ContentSuggestion suggestion( |
| 199 MakeUniqueID(provided_category_, bookmark->url().spec()), | 204 MakeUniqueID(provided_category_, bookmark->url().spec()), |
| 200 bookmark->url()); | 205 bookmark->url()); |
| 201 | 206 |
| 202 suggestion.set_title(bookmark->GetTitle()); | 207 suggestion.set_title(bookmark->GetTitle()); |
| 203 suggestion.set_snippet_text(base::string16()); | 208 suggestion.set_snippet_text(base::string16()); |
| 204 suggestion.set_publish_date(GetLastVisitDateForBookmark(bookmark)); | 209 suggestion.set_publish_date( |
| 210 GetLastVisitDateForBookmark(bookmark, /*creation_date_fallback=*/true)); | |
| 205 suggestion.set_publisher_name(base::UTF8ToUTF16(bookmark->url().host())); | 211 suggestion.set_publisher_name(base::UTF8ToUTF16(bookmark->url().host())); |
| 206 return suggestion; | 212 return suggestion; |
| 207 } | 213 } |
| 208 | 214 |
| 209 void BookmarkSuggestionsProvider::FetchBookmarksInternal() { | 215 void BookmarkSuggestionsProvider::FetchBookmarksInternal() { |
| 210 DCHECK(bookmark_model_->loaded()); | 216 DCHECK(bookmark_model_->loaded()); |
| 211 | 217 |
| 212 NotifyStatusChanged(CategoryStatus::AVAILABLE); | 218 NotifyStatusChanged(CategoryStatus::AVAILABLE); |
| 213 | 219 |
| 214 base::Time threshold_time = GetThresholdTime(); | 220 base::Time threshold_time = GetThresholdTime(); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 238 | 244 |
| 239 void BookmarkSuggestionsProvider::NotifyStatusChanged( | 245 void BookmarkSuggestionsProvider::NotifyStatusChanged( |
| 240 CategoryStatus new_status) { | 246 CategoryStatus new_status) { |
| 241 if (category_status_ == new_status) | 247 if (category_status_ == new_status) |
| 242 return; | 248 return; |
| 243 category_status_ = new_status; | 249 category_status_ = new_status; |
| 244 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | 250 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
| 245 } | 251 } |
| 246 | 252 |
| 247 } // namespace ntp_snippets | 253 } // namespace ntp_snippets |
| OLD | NEW |