Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(376)

Side by Side Diff: components/ntp_snippets/bookmarks/bookmark_suggestions_provider.cc

Issue 2256183004: Use bookmark creation date fallback for 6 weeks after installing M54 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Marc's comments Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 int64_t first_M54_start;
Marc Treib 2016/08/19 13:37:17 nit: Make this a base::Time (since that's the "rea
Philipp Keck 2016/08/19 14:27:36 Done.
96 if (pref_service->HasPrefPath(prefs::kBookmarksFirstM54Start)) {
97 first_M54_start = pref_service->GetInt64(prefs::kBookmarksFirstM54Start);
98 } else {
99 first_M54_start = base::Time::Now().ToInternalValue();
100 pref_service->SetInt64(prefs::kBookmarksFirstM54Start, first_M54_start);
101 }
102 base::TimeDelta time_since_first_M54_start =
103 base::Time::Now() - base::Time::FromInternalValue(first_M54_start);
104 creation_date_fallback_ =
105 time_since_first_M54_start.InDays() < kUseCreationDateFallbackForDays;
83 bookmark_model_->AddObserver(this); 106 bookmark_model_->AddObserver(this);
84 FetchBookmarks(); 107 FetchBookmarks();
85 } 108 }
86 109
87 BookmarkSuggestionsProvider::~BookmarkSuggestionsProvider() { 110 BookmarkSuggestionsProvider::~BookmarkSuggestionsProvider() {
88 bookmark_model_->RemoveObserver(this); 111 bookmark_model_->RemoveObserver(this);
89 } 112 }
90 113
114 // static
115 void BookmarkSuggestionsProvider::RegisterProfilePrefs(
116 PrefRegistrySimple* registry) {
117 registry->RegisterInt64Pref(prefs::kBookmarksFirstM54Start, 0);
118 }
119
91 //////////////////////////////////////////////////////////////////////////////// 120 ////////////////////////////////////////////////////////////////////////////////
92 // Private methods 121 // Private methods
93 122
94 std::vector<Category> BookmarkSuggestionsProvider::GetProvidedCategories() { 123 std::vector<Category> BookmarkSuggestionsProvider::GetProvidedCategories() {
95 return std::vector<Category>({provided_category_}); 124 return std::vector<Category>({provided_category_});
96 } 125 }
97 126
98 CategoryStatus BookmarkSuggestionsProvider::GetCategoryStatus( 127 CategoryStatus BookmarkSuggestionsProvider::GetCategoryStatus(
99 Category category) { 128 Category category) {
100 DCHECK_EQ(category, provided_category_); 129 DCHECK_EQ(category, provided_category_);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 fetch_requested_ = false; 185 fetch_requested_ = false;
157 FetchBookmarksInternal(); 186 FetchBookmarksInternal();
158 } 187 }
159 } 188 }
160 189
161 void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo( 190 void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo(
162 BookmarkModel* model, 191 BookmarkModel* model,
163 const BookmarkNode* node) { 192 const BookmarkNode* node) {
164 // Store the last visit date of the node that is about to change. 193 // Store the last visit date of the node that is about to change.
165 node_to_change_last_visit_date_ = 194 node_to_change_last_visit_date_ =
166 GetLastVisitDateForBookmarkIfNotDismissed(node); 195 GetLastVisitDateForBookmarkIfNotDismissed(node, creation_date_fallback_);
167 } 196 }
168 197
169 void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged( 198 void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged(
170 BookmarkModel* model, 199 BookmarkModel* model,
171 const BookmarkNode* node) { 200 const BookmarkNode* node) {
172 base::Time time = GetLastVisitDateForBookmarkIfNotDismissed(node); 201 base::Time time =
202 GetLastVisitDateForBookmarkIfNotDismissed(node, creation_date_fallback_);
173 if (time == node_to_change_last_visit_date_ || 203 if (time == node_to_change_last_visit_date_ ||
174 time < end_of_list_last_visit_date_) 204 time < end_of_list_last_visit_date_)
175 return; 205 return;
176 206
177 // Last visit date of a node has changed (and is relevant for the list), we 207 // Last visit date of a node has changed (and is relevant for the list), we
178 // should update the suggestions. 208 // should update the suggestions.
179 FetchBookmarks(); 209 FetchBookmarks();
180 } 210 }
181 211
182 void BookmarkSuggestionsProvider::BookmarkNodeRemoved( 212 void BookmarkSuggestionsProvider::BookmarkNodeRemoved(
183 bookmarks::BookmarkModel* model, 213 bookmarks::BookmarkModel* model,
184 const bookmarks::BookmarkNode* parent, 214 const bookmarks::BookmarkNode* parent,
185 int old_index, 215 int old_index,
186 const bookmarks::BookmarkNode* node, 216 const bookmarks::BookmarkNode* node,
187 const std::set<GURL>& no_longer_bookmarked) { 217 const std::set<GURL>& no_longer_bookmarked) {
188 if (GetLastVisitDateForBookmarkIfNotDismissed(node) < 218 if (GetLastVisitDateForBookmarkIfNotDismissed(node, creation_date_fallback_) <
189 end_of_list_last_visit_date_) 219 end_of_list_last_visit_date_) {
190 return; 220 return;
221 }
191 222
192 // Some node from our list got deleted, we should update the suggestions. 223 // Some node from our list got deleted, we should update the suggestions.
193 FetchBookmarks(); 224 FetchBookmarks();
194 } 225 }
195 226
196 ContentSuggestion BookmarkSuggestionsProvider::ConvertBookmark( 227 ContentSuggestion BookmarkSuggestionsProvider::ConvertBookmark(
197 const BookmarkNode* bookmark) { 228 const BookmarkNode* bookmark) {
198 ContentSuggestion suggestion( 229 ContentSuggestion suggestion(
199 MakeUniqueID(provided_category_, bookmark->url().spec()), 230 MakeUniqueID(provided_category_, bookmark->url().spec()),
200 bookmark->url()); 231 bookmark->url());
201 232
202 suggestion.set_title(bookmark->GetTitle()); 233 suggestion.set_title(bookmark->GetTitle());
203 suggestion.set_snippet_text(base::string16()); 234 suggestion.set_snippet_text(base::string16());
204 suggestion.set_publish_date(GetLastVisitDateForBookmark(bookmark)); 235 suggestion.set_publish_date(
236 GetLastVisitDateForBookmark(bookmark, creation_date_fallback_));
205 suggestion.set_publisher_name(base::UTF8ToUTF16(bookmark->url().host())); 237 suggestion.set_publisher_name(base::UTF8ToUTF16(bookmark->url().host()));
206 return suggestion; 238 return suggestion;
207 } 239 }
208 240
209 void BookmarkSuggestionsProvider::FetchBookmarksInternal() { 241 void BookmarkSuggestionsProvider::FetchBookmarksInternal() {
210 DCHECK(bookmark_model_->loaded()); 242 DCHECK(bookmark_model_->loaded());
211 243
212 NotifyStatusChanged(CategoryStatus::AVAILABLE); 244 NotifyStatusChanged(CategoryStatus::AVAILABLE);
213 245
214 base::Time threshold_time = GetThresholdTime(); 246 base::Time threshold_time = GetThresholdTime();
215 std::vector<const BookmarkNode*> bookmarks = GetRecentlyVisitedBookmarks( 247 std::vector<const BookmarkNode*> bookmarks =
216 bookmark_model_, GetMinCount(), GetMaxCount(), 248 GetRecentlyVisitedBookmarks(bookmark_model_, GetMinCount(), GetMaxCount(),
217 threshold_time); 249 threshold_time, creation_date_fallback_);
218 250
219 std::vector<ContentSuggestion> suggestions; 251 std::vector<ContentSuggestion> suggestions;
220 for (const BookmarkNode* bookmark : bookmarks) 252 for (const BookmarkNode* bookmark : bookmarks)
221 suggestions.emplace_back(ConvertBookmark(bookmark)); 253 suggestions.emplace_back(ConvertBookmark(bookmark));
222 254
223 if (suggestions.empty()) 255 if (suggestions.empty())
224 end_of_list_last_visit_date_ = threshold_time; 256 end_of_list_last_visit_date_ = threshold_time;
225 else 257 else
226 end_of_list_last_visit_date_ = suggestions.back().publish_date(); 258 end_of_list_last_visit_date_ = suggestions.back().publish_date();
227 259
(...skipping 10 matching lines...) Expand all
238 270
239 void BookmarkSuggestionsProvider::NotifyStatusChanged( 271 void BookmarkSuggestionsProvider::NotifyStatusChanged(
240 CategoryStatus new_status) { 272 CategoryStatus new_status) {
241 if (category_status_ == new_status) 273 if (category_status_ == new_status)
242 return; 274 return;
243 category_status_ = new_status; 275 category_status_ = new_status;
244 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); 276 observer()->OnCategoryStatusChanged(this, provided_category_, new_status);
245 } 277 }
246 278
247 } // namespace ntp_snippets 279 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/bookmarks/bookmark_suggestions_provider.h ('k') | components/ntp_snippets/pref_names.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698