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 11 matching lines...) Expand all Loading... | |
22 #include "components/prefs/pref_registry_simple.h" | 22 #include "components/prefs/pref_registry_simple.h" |
23 #include "components/prefs/pref_service.h" | 23 #include "components/prefs/pref_service.h" |
24 #include "components/variations/variations_associated_data.h" | 24 #include "components/variations/variations_associated_data.h" |
25 #include "grit/components_strings.h" | 25 #include "grit/components_strings.h" |
26 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
27 #include "ui/gfx/image/image.h" | 27 #include "ui/gfx/image/image.h" |
28 | 28 |
29 using bookmarks::BookmarkModel; | 29 using bookmarks::BookmarkModel; |
30 using bookmarks::BookmarkNode; | 30 using bookmarks::BookmarkNode; |
31 | 31 |
32 namespace ntp_snippets { | |
33 | |
32 namespace { | 34 namespace { |
33 | 35 |
34 const int kMaxBookmarks = 10; | 36 const int kMaxBookmarks = 10; |
35 const int kMinBookmarks = 3; | 37 const int kMinBookmarks = 3; |
36 const int kMaxBookmarkAgeInDays = 42; | 38 const int kMaxBookmarkAgeInDays = 42; |
37 const int kUseCreationDateFallbackForDays = 0; | 39 const int kUseCreationDateFallbackForDays = 0; |
38 | 40 |
39 const char* kMaxBookmarksParamName = "bookmarks_max_count"; | 41 const char* kMaxBookmarksParamName = "bookmarks_max_count"; |
40 const char* kMinBookmarksParamName = "bookmarks_min_count"; | 42 const char* kMinBookmarksParamName = "bookmarks_min_count"; |
41 const char* kMaxBookmarkAgeInDaysParamName = "bookmarks_max_age_in_days"; | 43 const char* kMaxBookmarkAgeInDaysParamName = "bookmarks_max_age_in_days"; |
42 const char* kUseCreationDateFallbackForDaysParamName = | 44 const char* kUseCreationDateFallbackForDaysParamName = |
43 "bookmarks_creation_date_fallback_days"; | 45 "bookmarks_creation_date_fallback_days"; |
44 const char* kShowIfEmptyParamName = "bookmarks_show_if_empty"; | 46 const char* kShowIfEmptyParamName = "bookmarks_show_if_empty"; |
45 | 47 |
48 // Any bookmark created or visited after this time will be considered recent. | |
49 // Note that bookmarks can be shown that do not meet this threshold. | |
46 base::Time GetThresholdTime() { | 50 base::Time GetThresholdTime() { |
47 std::string age_in_days_string = variations::GetVariationParamValueByFeature( | 51 return base::Time::Now() - |
48 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarkAgeInDaysParamName); | 52 base::TimeDelta::FromDays(GetParamAsInt( |
49 int age_in_days = 0; | 53 ntp_snippets::kBookmarkSuggestionsFeature, |
50 if (!base::StringToInt(age_in_days_string, &age_in_days)) { | 54 kMaxBookmarkAgeInDaysParamName, kMaxBookmarkAgeInDays)); |
51 if (!age_in_days_string.empty()) | |
52 LOG(WARNING) << "Failed to parse bookmark age " << age_in_days_string; | |
53 age_in_days = kMaxBookmarkAgeInDays; | |
54 } | |
55 return base::Time::Now() - base::TimeDelta::FromDays(age_in_days); | |
56 } | 55 } |
57 | 56 |
57 // Should the creation timestamp of a bookmark be used as a fallback if no last | |
58 // visited timestamp is present. | |
Marc Treib
2016/09/16 12:26:48
nit: This sounds like it would return a bool.
skym
2016/09/16 18:18:47
This value is confusing. Updated.
| |
58 int UseCreationDateFallbackForDays() { | 59 int UseCreationDateFallbackForDays() { |
59 std::string days_string = variations::GetVariationParamValueByFeature( | 60 return GetParamAsInt(ntp_snippets::kBookmarkSuggestionsFeature, |
60 ntp_snippets::kBookmarkSuggestionsFeature, | 61 kUseCreationDateFallbackForDaysParamName, |
61 kUseCreationDateFallbackForDaysParamName); | 62 kUseCreationDateFallbackForDays); |
62 int days = 0; | |
63 if (!base::StringToInt(days_string, &days)) { | |
64 if (!days_string.empty()) | |
65 LOG(WARNING) << "Failed to parse bookmark fallback days " << days_string; | |
66 days = kUseCreationDateFallbackForDays; | |
67 } | |
68 return days; | |
69 } | 63 } |
70 | 64 |
65 // The maximum number of suggestions ever provided. | |
71 int GetMaxCount() { | 66 int GetMaxCount() { |
72 std::string max_count_string = variations::GetVariationParamValueByFeature( | 67 return GetParamAsInt(ntp_snippets::kBookmarkSuggestionsFeature, |
73 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarksParamName); | 68 kMaxBookmarksParamName, kMaxBookmarks); |
74 int max_count = 0; | |
75 if (base::StringToInt(max_count_string, &max_count)) | |
76 return max_count; | |
77 if (!max_count_string.empty()) | |
78 LOG(WARNING) << "Failed to parse max bookmarks count " << max_count_string; | |
79 | |
80 return kMaxBookmarks; | |
81 } | 69 } |
82 | 70 |
71 // The minimum number of suggestions ever provided. If there are not enough | |
72 // recent bookmarks to meet this minimum, then bookmarks more stale than the | |
73 // threshold are used. | |
Marc Treib
2016/09/16 12:26:48
I don't think this quite matches the behavior (whi
skym
2016/09/16 18:18:47
Okay, I've updated this, let me know if you thinks
| |
83 int GetMinCount() { | 74 int GetMinCount() { |
84 std::string min_count_string = variations::GetVariationParamValueByFeature( | 75 return GetParamAsInt(ntp_snippets::kBookmarkSuggestionsFeature, |
85 ntp_snippets::kBookmarkSuggestionsFeature, kMinBookmarksParamName); | 76 kMinBookmarksParamName, kMinBookmarks); |
86 int min_count = 0; | |
87 if (base::StringToInt(min_count_string, &min_count)) | |
88 return min_count; | |
89 if (!min_count_string.empty()) | |
90 LOG(WARNING) << "Failed to parse min bookmarks count " << min_count_string; | |
91 | |
92 return kMinBookmarks; | |
93 } | 77 } |
94 | 78 |
95 bool ShouldShowIfEmpty() { | 79 bool ShouldShowIfEmpty() { |
96 std::string show_if_empty = variations::GetVariationParamValueByFeature( | 80 std::string show_if_empty = variations::GetVariationParamValueByFeature( |
97 ntp_snippets::kBookmarkSuggestionsFeature, kShowIfEmptyParamName); | 81 ntp_snippets::kBookmarkSuggestionsFeature, kShowIfEmptyParamName); |
98 if (show_if_empty.empty() || show_if_empty == "false") | 82 if (show_if_empty.empty() || show_if_empty == "false") |
99 return false; | 83 return false; |
100 if (show_if_empty == "true") | 84 if (show_if_empty == "true") |
101 return true; | 85 return true; |
102 | 86 |
103 LOG(WARNING) << "Failed to parse show if empty " << show_if_empty; | 87 LOG(WARNING) << "Failed to parse show if empty " << show_if_empty; |
104 return false; | 88 return false; |
105 } | 89 } |
106 | 90 |
107 } // namespace | 91 } // namespace |
108 | 92 |
109 namespace ntp_snippets { | |
110 | |
111 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( | 93 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( |
112 ContentSuggestionsProvider::Observer* observer, | 94 ContentSuggestionsProvider::Observer* observer, |
113 CategoryFactory* category_factory, | 95 CategoryFactory* category_factory, |
114 bookmarks::BookmarkModel* bookmark_model, | 96 bookmarks::BookmarkModel* bookmark_model, |
115 PrefService* pref_service) | 97 PrefService* pref_service) |
116 : ContentSuggestionsProvider(observer, category_factory), | 98 : ContentSuggestionsProvider(observer, category_factory), |
117 category_status_(CategoryStatus::AVAILABLE_LOADING), | 99 category_status_(CategoryStatus::AVAILABLE_LOADING), |
118 provided_category_( | 100 provided_category_( |
119 category_factory->FromKnownCategory(KnownCategories::BOOKMARKS)), | 101 category_factory->FromKnownCategory(KnownCategories::BOOKMARKS)), |
120 bookmark_model_(bookmark_model), | 102 bookmark_model_(bookmark_model), |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
321 | 303 |
322 void BookmarkSuggestionsProvider::NotifyStatusChanged( | 304 void BookmarkSuggestionsProvider::NotifyStatusChanged( |
323 CategoryStatus new_status) { | 305 CategoryStatus new_status) { |
324 if (category_status_ == new_status) | 306 if (category_status_ == new_status) |
325 return; | 307 return; |
326 category_status_ = new_status; | 308 category_status_ = new_status; |
327 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | 309 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
328 } | 310 } |
329 | 311 |
330 } // namespace ntp_snippets | 312 } // namespace ntp_snippets |
OLD | NEW |