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 10 matching lines...) Expand all Loading... | |
| 21 #include "grit/components_strings.h" | 21 #include "grit/components_strings.h" |
| 22 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
| 23 #include "ui/gfx/image/image.h" | 23 #include "ui/gfx/image/image.h" |
| 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 kMaxBookmarksIfNoVisitData = 3; | |
| 31 const int kMaxBookmarkAgeInDays = 42; | 32 const int kMaxBookmarkAgeInDays = 42; |
| 32 | 33 |
| 33 const char* kMaxBookmarksParamName = "max_count"; | 34 const char* kMaxBookmarksParamName = "max_count"; |
| 35 const char* kMaxBookmarksIfNoVisitDataParamName = "max_count_no_visit_data"; | |
| 34 const char* kMaxBookmarkAgeInDaysParamName = "max_age_in_days"; | 36 const char* kMaxBookmarkAgeInDaysParamName = "max_age_in_days"; |
| 35 | 37 |
| 36 base::Time GetThresholdTime() { | 38 base::Time GetThresholdTime(bool no_visit_data) { |
| 39 // If we have no data, we list bookmarks by creation date, no age limit. | |
| 40 if (no_visit_data) | |
| 41 return base::Time(); | |
| 42 | |
| 37 std::string age_in_days_string = variations::GetVariationParamValueByFeature( | 43 std::string age_in_days_string = variations::GetVariationParamValueByFeature( |
| 38 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarkAgeInDaysParamName); | 44 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarkAgeInDaysParamName); |
| 39 int age_in_days = 0; | 45 int age_in_days = 0; |
| 40 if (!base::StringToInt(age_in_days_string, &age_in_days)) | 46 if (!base::StringToInt(age_in_days_string, &age_in_days)) |
| 41 age_in_days = kMaxBookmarkAgeInDays; | 47 age_in_days = kMaxBookmarkAgeInDays; |
| 42 | 48 |
| 43 return base::Time::Now() - base::TimeDelta::FromDays(age_in_days); | 49 return base::Time::Now() - base::TimeDelta::FromDays(age_in_days); |
| 44 } | 50 } |
| 45 | 51 |
| 46 int GetMaxCount() { | 52 int GetMaxCount(bool no_visit_data) { |
| 47 std::string max_count_string = variations::GetVariationParamValueByFeature( | 53 std::string max_count_string = variations::GetVariationParamValueByFeature( |
| 48 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarksParamName); | 54 ntp_snippets::kBookmarkSuggestionsFeature, |
| 55 no_visit_data ? kMaxBookmarksIfNoVisitDataParamName | |
| 56 : kMaxBookmarksParamName); | |
| 49 int max_count = 0; | 57 int max_count = 0; |
| 50 if (base::StringToInt(max_count_string, &max_count)) | 58 if (base::StringToInt(max_count_string, &max_count)) |
| 51 return max_count; | 59 return max_count; |
| 52 | 60 |
| 53 return kMaxBookmarks; | 61 return no_visit_data ? kMaxBookmarksIfNoVisitData : kMaxBookmarks; |
| 54 } | 62 } |
| 55 | 63 |
| 56 } // namespace | 64 } // namespace |
| 57 | 65 |
| 58 namespace ntp_snippets { | 66 namespace ntp_snippets { |
| 59 | 67 |
| 60 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( | 68 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( |
| 61 ContentSuggestionsProvider::Observer* observer, | 69 ContentSuggestionsProvider::Observer* observer, |
| 62 CategoryFactory* category_factory, | 70 CategoryFactory* category_factory, |
| 63 bookmarks::BookmarkModel* bookmark_model) | 71 bookmarks::BookmarkModel* bookmark_model) |
| 64 : ContentSuggestionsProvider(observer, category_factory), | 72 : ContentSuggestionsProvider(observer, category_factory), |
| 65 category_status_(CategoryStatus::AVAILABLE_LOADING), | 73 category_status_(CategoryStatus::AVAILABLE_LOADING), |
| 66 provided_category_( | 74 provided_category_( |
| 67 category_factory->FromKnownCategory(KnownCategories::BOOKMARKS)), | 75 category_factory->FromKnownCategory(KnownCategories::BOOKMARKS)), |
| 68 bookmark_model_(bookmark_model), | 76 bookmark_model_(bookmark_model), |
| 69 fetch_requested_(false), | 77 fetch_requested_(false), |
| 70 end_of_list_last_visit_date_(GetThresholdTime()) { | 78 no_visit_data_(false), |
| 79 end_of_list_last_visit_date_(GetThresholdTime(no_visit_data_)) { | |
| 71 bookmark_model_->AddObserver(this); | 80 bookmark_model_->AddObserver(this); |
| 72 FetchBookmarks(); | 81 FetchBookmarks(); |
| 73 } | 82 } |
| 74 | 83 |
| 75 BookmarkSuggestionsProvider::~BookmarkSuggestionsProvider() { | 84 BookmarkSuggestionsProvider::~BookmarkSuggestionsProvider() { |
| 76 bookmark_model_->RemoveObserver(this); | 85 bookmark_model_->RemoveObserver(this); |
| 77 } | 86 } |
| 78 | 87 |
| 79 //////////////////////////////////////////////////////////////////////////////// | 88 //////////////////////////////////////////////////////////////////////////////// |
| 80 // Private methods | 89 // Private methods |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 if (fetch_requested_) { | 152 if (fetch_requested_) { |
| 144 fetch_requested_ = false; | 153 fetch_requested_ = false; |
| 145 FetchBookmarksInternal(); | 154 FetchBookmarksInternal(); |
| 146 } | 155 } |
| 147 } | 156 } |
| 148 | 157 |
| 149 void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo( | 158 void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo( |
| 150 BookmarkModel* model, | 159 BookmarkModel* model, |
| 151 const BookmarkNode* node) { | 160 const BookmarkNode* node) { |
| 152 // Store the last visit date of the node that is about to change. | 161 // Store the last visit date of the node that is about to change. |
| 153 node_to_change_last_visit_date_ = | 162 node_to_change_last_visit_date_ = GetLastVisitDateForBookmarkIfNotDismissed( |
| 154 GetLastVisitDateForBookmarkIfNotDismissed(node); | 163 node, no_visit_data_); |
| 155 } | 164 } |
| 156 | 165 |
| 157 void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged( | 166 void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged( |
| 158 BookmarkModel* model, | 167 BookmarkModel* model, |
| 159 const BookmarkNode* node) { | 168 const BookmarkNode* node) { |
| 160 base::Time time = GetLastVisitDateForBookmarkIfNotDismissed(node); | 169 base::Time time = GetLastVisitDateForBookmarkIfNotDismissed( |
| 170 node, no_visit_data_); | |
| 161 if (time == node_to_change_last_visit_date_ || | 171 if (time == node_to_change_last_visit_date_ || |
| 162 time < end_of_list_last_visit_date_) | 172 time < end_of_list_last_visit_date_) |
| 163 return; | 173 return; |
| 164 | 174 |
| 165 // Last visit date of a node has changed (and is relevant for the list), we | 175 // Last visit date of a node has changed (and is relevant for the list), we |
| 166 // should update the suggestions. | 176 // should update the suggestions. |
| 167 FetchBookmarks(); | 177 FetchBookmarks(); |
| 168 } | 178 } |
| 169 | 179 |
| 170 void BookmarkSuggestionsProvider::BookmarkNodeRemoved( | 180 void BookmarkSuggestionsProvider::BookmarkNodeRemoved( |
| 171 bookmarks::BookmarkModel* model, | 181 bookmarks::BookmarkModel* model, |
| 172 const bookmarks::BookmarkNode* parent, | 182 const bookmarks::BookmarkNode* parent, |
| 173 int old_index, | 183 int old_index, |
| 174 const bookmarks::BookmarkNode* node, | 184 const bookmarks::BookmarkNode* node, |
| 175 const std::set<GURL>& no_longer_bookmarked) { | 185 const std::set<GURL>& no_longer_bookmarked) { |
| 176 if (GetLastVisitDateForBookmarkIfNotDismissed(node) < | 186 if (GetLastVisitDateForBookmarkIfNotDismissed(node, no_visit_data_) < |
| 177 end_of_list_last_visit_date_) | 187 end_of_list_last_visit_date_) |
| 178 return; | 188 return; |
| 179 | 189 |
| 180 // Some node from our list got deleted, we should update the suggestions. | 190 // Some node from our list got deleted, we should update the suggestions. |
| 181 FetchBookmarks(); | 191 FetchBookmarks(); |
| 182 } | 192 } |
| 183 | 193 |
| 184 ContentSuggestion BookmarkSuggestionsProvider::ConvertBookmark( | 194 ContentSuggestion BookmarkSuggestionsProvider::ConvertBookmark( |
| 185 const BookmarkNode* bookmark) { | 195 const BookmarkNode* bookmark) { |
| 186 ContentSuggestion suggestion( | 196 ContentSuggestion suggestion( |
| 187 MakeUniqueID(provided_category_, bookmark->url().spec()), | 197 MakeUniqueID(provided_category_, bookmark->url().spec()), |
| 188 bookmark->url()); | 198 bookmark->url()); |
| 189 | 199 |
| 190 suggestion.set_title(bookmark->GetTitle()); | 200 suggestion.set_title(bookmark->GetTitle()); |
| 191 suggestion.set_snippet_text(base::string16()); | 201 suggestion.set_snippet_text(base::string16()); |
| 192 suggestion.set_publish_date(GetLastVisitDateForBookmark(bookmark)); | 202 suggestion.set_publish_date( |
| 203 GetLastVisitDateForBookmark(bookmark, no_visit_data_)); | |
| 193 suggestion.set_publisher_name(base::UTF8ToUTF16(bookmark->url().host())); | 204 suggestion.set_publisher_name(base::UTF8ToUTF16(bookmark->url().host())); |
| 194 return suggestion; | 205 return suggestion; |
| 195 } | 206 } |
| 196 | 207 |
| 197 void BookmarkSuggestionsProvider::FetchBookmarksInternal() { | 208 void BookmarkSuggestionsProvider::FetchBookmarksInternal() { |
| 198 DCHECK(bookmark_model_->loaded()); | 209 DCHECK(bookmark_model_->loaded()); |
| 199 | 210 |
| 200 NotifyStatusChanged(CategoryStatus::AVAILABLE); | 211 NotifyStatusChanged(CategoryStatus::AVAILABLE); |
| 201 | 212 |
| 202 base::Time threshold_time = GetThresholdTime(); | 213 no_visit_data_ = !IsLastVisitDataAvailable(bookmark_model_); |
|
Marc Treib
2016/08/17 11:29:06
Hm, so this means, a new user will get 3 bookmarks
| |
| 214 | |
| 215 base::Time threshold_time = GetThresholdTime(no_visit_data_); | |
| 203 std::vector<const BookmarkNode*> bookmarks = GetRecentlyVisitedBookmarks( | 216 std::vector<const BookmarkNode*> bookmarks = GetRecentlyVisitedBookmarks( |
| 204 bookmark_model_, GetMaxCount(), threshold_time); | 217 bookmark_model_, no_visit_data_, GetMaxCount(no_visit_data_), |
| 218 threshold_time); | |
| 205 | 219 |
| 206 std::vector<ContentSuggestion> suggestions; | 220 std::vector<ContentSuggestion> suggestions; |
| 207 for (const BookmarkNode* bookmark : bookmarks) | 221 for (const BookmarkNode* bookmark : bookmarks) |
| 208 suggestions.emplace_back(ConvertBookmark(bookmark)); | 222 suggestions.emplace_back(ConvertBookmark(bookmark)); |
| 209 | 223 |
| 210 if (suggestions.empty()) | 224 if (suggestions.empty()) |
| 211 end_of_list_last_visit_date_ = threshold_time; | 225 end_of_list_last_visit_date_ = threshold_time; |
| 212 else | 226 else |
| 213 end_of_list_last_visit_date_ = suggestions.back().publish_date(); | 227 end_of_list_last_visit_date_ = suggestions.back().publish_date(); |
| 214 | 228 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 225 | 239 |
| 226 void BookmarkSuggestionsProvider::NotifyStatusChanged( | 240 void BookmarkSuggestionsProvider::NotifyStatusChanged( |
| 227 CategoryStatus new_status) { | 241 CategoryStatus new_status) { |
| 228 if (category_status_ == new_status) | 242 if (category_status_ == new_status) |
| 229 return; | 243 return; |
| 230 category_status_ = new_status; | 244 category_status_ = new_status; |
| 231 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | 245 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
| 232 } | 246 } |
| 233 | 247 |
| 234 } // namespace ntp_snippets | 248 } // namespace ntp_snippets |
| OLD | NEW |