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 = "ntp_bookmarks_max_count"; |
35 const char* kMinBookmarksParamName = "min_count"; | 40 const char* kMinBookmarksParamName = "ntp_bookmarks_min_count"; |
36 const char* kMaxBookmarkAgeInDaysParamName = "max_age_in_days"; | 41 const char* kMaxBookmarkAgeInDaysParamName = "ntp_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)) |
43 age_in_days = kMaxBookmarkAgeInDays; | 48 age_in_days = kMaxBookmarkAgeInDays; |
44 | 49 |
45 return base::Time::Now() - base::TimeDelta::FromDays(age_in_days); | 50 return base::Time::Now() - base::TimeDelta::FromDays(age_in_days); |
46 } | 51 } |
47 | 52 |
48 int GetMaxCount() { | 53 int GetMaxCount() { |
49 std::string max_count_string = variations::GetVariationParamValueByFeature( | 54 std::string max_count_string = variations::GetVariationParamValueByFeature( |
50 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarksParamName); | 55 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarksParamName); |
51 int max_count = 0; | 56 int max_count = 0; |
52 if (base::StringToInt(max_count_string, &max_count)) | 57 if (base::StringToInt(max_count_string, &max_count)) |
53 return max_count; | 58 return max_count; |
54 | 59 |
55 return kMaxBookmarks; | 60 return kMaxBookmarks; |
56 } | 61 } |
57 | 62 |
58 int GetMinCount() { | 63 int GetMinCount() { |
59 std::string min_count_string = variations::GetVariationParamValueByFeature( | 64 std::string min_count_string = variations::GetVariationParamValueByFeature( |
60 ntp_snippets::kBookmarkSuggestionsFeature, kMinBookmarksParamName); | 65 ntp_snippets::kBookmarkSuggestionsFeature, kMinBookmarksParamName); |
61 int min_count = 0; | 66 int min_count = 0; |
62 if (base::StringToInt(min_count_string, &min_count)) | 67 if (base::StringToInt(min_count_string, &min_count)) |
63 return min_count; | 68 return min_count; |
69 if (!min_count_string.empty()) | |
70 LOG(WARNING) << "Failed to parse min bookmarks count" << min_count_string; | |
64 | 71 |
65 return kMinBookmarks; | 72 return kMinBookmarks; |
66 } | 73 } |
67 | 74 |
68 } // namespace | 75 } // namespace |
69 | 76 |
70 namespace ntp_snippets { | 77 namespace ntp_snippets { |
71 | 78 |
72 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( | 79 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( |
73 ContentSuggestionsProvider::Observer* observer, | 80 ContentSuggestionsProvider::Observer* observer, |
74 CategoryFactory* category_factory, | 81 CategoryFactory* category_factory, |
75 bookmarks::BookmarkModel* bookmark_model) | 82 bookmarks::BookmarkModel* bookmark_model, |
83 PrefService* pref_service) | |
76 : ContentSuggestionsProvider(observer, category_factory), | 84 : ContentSuggestionsProvider(observer, category_factory), |
77 category_status_(CategoryStatus::AVAILABLE_LOADING), | 85 category_status_(CategoryStatus::AVAILABLE_LOADING), |
78 provided_category_( | 86 provided_category_( |
79 category_factory->FromKnownCategory(KnownCategories::BOOKMARKS)), | 87 category_factory->FromKnownCategory(KnownCategories::BOOKMARKS)), |
80 bookmark_model_(bookmark_model), | 88 bookmark_model_(bookmark_model), |
81 fetch_requested_(false), | 89 fetch_requested_(false), |
82 end_of_list_last_visit_date_(GetThresholdTime()) { | 90 end_of_list_last_visit_date_(GetThresholdTime()) { |
91 int64_t first_M54_start = | |
Marc Treib
2016/08/19 11:41:12
nit: Move all this into a helper function? Then yo
Philipp Keck
2016/08/19 12:49:52
True. Not sure if the code is best placed in a hel
Marc Treib
2016/08/19 13:37:17
Well, you could argue the same for any method/func
Philipp Keck
2016/08/19 14:27:36
My point is that this code is special in that it's
| |
92 pref_service->GetInt64(prefs::kBookmarksFirstM54Start); | |
93 if (first_M54_start == 0) { | |
Marc Treib
2016/08/19 11:41:12
HasPrefPath() might be easier to read than compari
Philipp Keck
2016/08/19 12:49:52
Done.
| |
94 first_M54_start = base::Time::Now().ToInternalValue(); | |
95 pref_service->SetInt64(prefs::kBookmarksFirstM54Start, first_M54_start); | |
96 } | |
97 base::TimeDelta time_since_first_M54_start = | |
98 base::Time::Now() - base::Time::FromInternalValue(first_M54_start); | |
99 creation_date_fallback_ = | |
100 time_since_first_M54_start.InDays() < kUseCreationDateFallbackForDays; | |
Marc Treib
2016/08/19 11:41:11
InDays() rounds down, so this is not strictly corr
Philipp Keck
2016/08/19 12:49:52
Should be correct as is.
| |
83 bookmark_model_->AddObserver(this); | 101 bookmark_model_->AddObserver(this); |
84 FetchBookmarks(); | 102 FetchBookmarks(); |
85 } | 103 } |
86 | 104 |
87 BookmarkSuggestionsProvider::~BookmarkSuggestionsProvider() { | 105 BookmarkSuggestionsProvider::~BookmarkSuggestionsProvider() { |
88 bookmark_model_->RemoveObserver(this); | 106 bookmark_model_->RemoveObserver(this); |
89 } | 107 } |
90 | 108 |
109 // static | |
110 void BookmarkSuggestionsProvider::RegisterProfilePrefs( | |
111 PrefRegistrySimple* registry) { | |
112 registry->RegisterInt64Pref(prefs::kBookmarksFirstM54Start, 0); | |
113 } | |
114 | |
91 //////////////////////////////////////////////////////////////////////////////// | 115 //////////////////////////////////////////////////////////////////////////////// |
92 // Private methods | 116 // Private methods |
93 | 117 |
94 std::vector<Category> BookmarkSuggestionsProvider::GetProvidedCategories() { | 118 std::vector<Category> BookmarkSuggestionsProvider::GetProvidedCategories() { |
95 return std::vector<Category>({provided_category_}); | 119 return std::vector<Category>({provided_category_}); |
96 } | 120 } |
97 | 121 |
98 CategoryStatus BookmarkSuggestionsProvider::GetCategoryStatus( | 122 CategoryStatus BookmarkSuggestionsProvider::GetCategoryStatus( |
99 Category category) { | 123 Category category) { |
100 DCHECK_EQ(category, provided_category_); | 124 DCHECK_EQ(category, provided_category_); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
156 fetch_requested_ = false; | 180 fetch_requested_ = false; |
157 FetchBookmarksInternal(); | 181 FetchBookmarksInternal(); |
158 } | 182 } |
159 } | 183 } |
160 | 184 |
161 void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo( | 185 void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo( |
162 BookmarkModel* model, | 186 BookmarkModel* model, |
163 const BookmarkNode* node) { | 187 const BookmarkNode* node) { |
164 // Store the last visit date of the node that is about to change. | 188 // Store the last visit date of the node that is about to change. |
165 node_to_change_last_visit_date_ = | 189 node_to_change_last_visit_date_ = |
166 GetLastVisitDateForBookmarkIfNotDismissed(node); | 190 GetLastVisitDateForBookmarkIfNotDismissed(node, creation_date_fallback_); |
167 } | 191 } |
168 | 192 |
169 void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged( | 193 void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged( |
170 BookmarkModel* model, | 194 BookmarkModel* model, |
171 const BookmarkNode* node) { | 195 const BookmarkNode* node) { |
172 base::Time time = GetLastVisitDateForBookmarkIfNotDismissed(node); | 196 base::Time time = |
197 GetLastVisitDateForBookmarkIfNotDismissed(node, creation_date_fallback_); | |
173 if (time == node_to_change_last_visit_date_ || | 198 if (time == node_to_change_last_visit_date_ || |
174 time < end_of_list_last_visit_date_) | 199 time < end_of_list_last_visit_date_) |
175 return; | 200 return; |
176 | 201 |
177 // Last visit date of a node has changed (and is relevant for the list), we | 202 // Last visit date of a node has changed (and is relevant for the list), we |
178 // should update the suggestions. | 203 // should update the suggestions. |
179 FetchBookmarks(); | 204 FetchBookmarks(); |
180 } | 205 } |
181 | 206 |
182 void BookmarkSuggestionsProvider::BookmarkNodeRemoved( | 207 void BookmarkSuggestionsProvider::BookmarkNodeRemoved( |
183 bookmarks::BookmarkModel* model, | 208 bookmarks::BookmarkModel* model, |
184 const bookmarks::BookmarkNode* parent, | 209 const bookmarks::BookmarkNode* parent, |
185 int old_index, | 210 int old_index, |
186 const bookmarks::BookmarkNode* node, | 211 const bookmarks::BookmarkNode* node, |
187 const std::set<GURL>& no_longer_bookmarked) { | 212 const std::set<GURL>& no_longer_bookmarked) { |
188 if (GetLastVisitDateForBookmarkIfNotDismissed(node) < | 213 if (GetLastVisitDateForBookmarkIfNotDismissed(node, creation_date_fallback_) < |
189 end_of_list_last_visit_date_) | 214 end_of_list_last_visit_date_) { |
190 return; | 215 return; |
216 } | |
191 | 217 |
192 // Some node from our list got deleted, we should update the suggestions. | 218 // Some node from our list got deleted, we should update the suggestions. |
193 FetchBookmarks(); | 219 FetchBookmarks(); |
194 } | 220 } |
195 | 221 |
196 ContentSuggestion BookmarkSuggestionsProvider::ConvertBookmark( | 222 ContentSuggestion BookmarkSuggestionsProvider::ConvertBookmark( |
197 const BookmarkNode* bookmark) { | 223 const BookmarkNode* bookmark) { |
198 ContentSuggestion suggestion( | 224 ContentSuggestion suggestion( |
199 MakeUniqueID(provided_category_, bookmark->url().spec()), | 225 MakeUniqueID(provided_category_, bookmark->url().spec()), |
200 bookmark->url()); | 226 bookmark->url()); |
201 | 227 |
202 suggestion.set_title(bookmark->GetTitle()); | 228 suggestion.set_title(bookmark->GetTitle()); |
203 suggestion.set_snippet_text(base::string16()); | 229 suggestion.set_snippet_text(base::string16()); |
204 suggestion.set_publish_date(GetLastVisitDateForBookmark(bookmark)); | 230 suggestion.set_publish_date( |
231 GetLastVisitDateForBookmark(bookmark, creation_date_fallback_)); | |
205 suggestion.set_publisher_name(base::UTF8ToUTF16(bookmark->url().host())); | 232 suggestion.set_publisher_name(base::UTF8ToUTF16(bookmark->url().host())); |
206 return suggestion; | 233 return suggestion; |
207 } | 234 } |
208 | 235 |
209 void BookmarkSuggestionsProvider::FetchBookmarksInternal() { | 236 void BookmarkSuggestionsProvider::FetchBookmarksInternal() { |
210 DCHECK(bookmark_model_->loaded()); | 237 DCHECK(bookmark_model_->loaded()); |
211 | 238 |
212 NotifyStatusChanged(CategoryStatus::AVAILABLE); | 239 NotifyStatusChanged(CategoryStatus::AVAILABLE); |
213 | 240 |
214 base::Time threshold_time = GetThresholdTime(); | 241 base::Time threshold_time = GetThresholdTime(); |
215 std::vector<const BookmarkNode*> bookmarks = GetRecentlyVisitedBookmarks( | 242 std::vector<const BookmarkNode*> bookmarks = |
216 bookmark_model_, GetMinCount(), GetMaxCount(), | 243 GetRecentlyVisitedBookmarks(bookmark_model_, GetMinCount(), GetMaxCount(), |
217 threshold_time); | 244 threshold_time, creation_date_fallback_); |
218 | 245 |
219 std::vector<ContentSuggestion> suggestions; | 246 std::vector<ContentSuggestion> suggestions; |
220 for (const BookmarkNode* bookmark : bookmarks) | 247 for (const BookmarkNode* bookmark : bookmarks) |
221 suggestions.emplace_back(ConvertBookmark(bookmark)); | 248 suggestions.emplace_back(ConvertBookmark(bookmark)); |
222 | 249 |
223 if (suggestions.empty()) | 250 if (suggestions.empty()) |
224 end_of_list_last_visit_date_ = threshold_time; | 251 end_of_list_last_visit_date_ = threshold_time; |
225 else | 252 else |
226 end_of_list_last_visit_date_ = suggestions.back().publish_date(); | 253 end_of_list_last_visit_date_ = suggestions.back().publish_date(); |
227 | 254 |
(...skipping 10 matching lines...) Expand all Loading... | |
238 | 265 |
239 void BookmarkSuggestionsProvider::NotifyStatusChanged( | 266 void BookmarkSuggestionsProvider::NotifyStatusChanged( |
240 CategoryStatus new_status) { | 267 CategoryStatus new_status) { |
241 if (category_status_ == new_status) | 268 if (category_status_ == new_status) |
242 return; | 269 return; |
243 category_status_ = new_status; | 270 category_status_ = new_status; |
244 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | 271 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
245 } | 272 } |
246 | 273 |
247 } // namespace ntp_snippets | 274 } // namespace ntp_snippets |
OLD | NEW |