| 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 21 matching lines...) Expand all Loading... |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 const int kMaxBookmarks = 10; | 34 const int kMaxBookmarks = 10; |
| 35 const int kMinBookmarks = 3; | 35 const int kMinBookmarks = 3; |
| 36 const int kMaxBookmarkAgeInDays = 42; | 36 const int kMaxBookmarkAgeInDays = 42; |
| 37 const int kUseCreationDateFallbackForDays = 42; | 37 const int kUseCreationDateFallbackForDays = 42; |
| 38 | 38 |
| 39 const char* kMaxBookmarksParamName = "bookmarks_max_count"; | 39 const char* kMaxBookmarksParamName = "bookmarks_max_count"; |
| 40 const char* kMinBookmarksParamName = "bookmarks_min_count"; | 40 const char* kMinBookmarksParamName = "bookmarks_min_count"; |
| 41 const char* kMaxBookmarkAgeInDaysParamName = "bookmarks_max_age_in_days"; | 41 const char* kMaxBookmarkAgeInDaysParamName = "bookmarks_max_age_in_days"; |
| 42 const char* kUseCreationDateFallbackForDaysParamName = |
| 43 "bookmarks_creation_date_fallback_days"; |
| 42 | 44 |
| 43 base::Time GetThresholdTime() { | 45 base::Time GetThresholdTime() { |
| 44 std::string age_in_days_string = variations::GetVariationParamValueByFeature( | 46 std::string age_in_days_string = variations::GetVariationParamValueByFeature( |
| 45 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarkAgeInDaysParamName); | 47 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarkAgeInDaysParamName); |
| 46 int age_in_days = 0; | 48 int age_in_days = 0; |
| 47 if (!base::StringToInt(age_in_days_string, &age_in_days)) { | 49 if (!base::StringToInt(age_in_days_string, &age_in_days)) { |
| 48 if (!age_in_days_string.empty()) | 50 if (!age_in_days_string.empty()) |
| 49 LOG(WARNING) << "Failed to parse bookmark age " << age_in_days_string; | 51 LOG(WARNING) << "Failed to parse bookmark age " << age_in_days_string; |
| 50 age_in_days = kMaxBookmarkAgeInDays; | 52 age_in_days = kMaxBookmarkAgeInDays; |
| 51 } | 53 } |
| 52 return base::Time::Now() - base::TimeDelta::FromDays(age_in_days); | 54 return base::Time::Now() - base::TimeDelta::FromDays(age_in_days); |
| 53 } | 55 } |
| 54 | 56 |
| 57 int UseCreationDateFallbackForDays() { |
| 58 std::string days_string = variations::GetVariationParamValueByFeature( |
| 59 ntp_snippets::kBookmarkSuggestionsFeature, |
| 60 kUseCreationDateFallbackForDaysParamName); |
| 61 int days = 0; |
| 62 if (!base::StringToInt(days_string, &days)) { |
| 63 if (!days_string.empty()) |
| 64 LOG(WARNING) << "Failed to parse bookmark fallback days " << days_string; |
| 65 days = kUseCreationDateFallbackForDays; |
| 66 } |
| 67 return days; |
| 68 } |
| 69 |
| 55 int GetMaxCount() { | 70 int GetMaxCount() { |
| 56 std::string max_count_string = variations::GetVariationParamValueByFeature( | 71 std::string max_count_string = variations::GetVariationParamValueByFeature( |
| 57 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarksParamName); | 72 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarksParamName); |
| 58 int max_count = 0; | 73 int max_count = 0; |
| 59 if (base::StringToInt(max_count_string, &max_count)) | 74 if (base::StringToInt(max_count_string, &max_count)) |
| 60 return max_count; | 75 return max_count; |
| 61 if (!max_count_string.empty()) | 76 if (!max_count_string.empty()) |
| 62 LOG(WARNING) << "Failed to parse max bookmarks count" << max_count_string; | 77 LOG(WARNING) << "Failed to parse max bookmarks count" << max_count_string; |
| 63 | 78 |
| 64 return kMaxBookmarks; | 79 return kMaxBookmarks; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 base::Time now = base::Time::Now(); | 112 base::Time now = base::Time::Now(); |
| 98 if (pref_service->HasPrefPath(prefs::kBookmarksFirstM54Start)) { | 113 if (pref_service->HasPrefPath(prefs::kBookmarksFirstM54Start)) { |
| 99 first_m54_start = base::Time::FromInternalValue( | 114 first_m54_start = base::Time::FromInternalValue( |
| 100 pref_service->GetInt64(prefs::kBookmarksFirstM54Start)); | 115 pref_service->GetInt64(prefs::kBookmarksFirstM54Start)); |
| 101 } else { | 116 } else { |
| 102 first_m54_start = now; | 117 first_m54_start = now; |
| 103 pref_service->SetInt64(prefs::kBookmarksFirstM54Start, | 118 pref_service->SetInt64(prefs::kBookmarksFirstM54Start, |
| 104 first_m54_start.ToInternalValue()); | 119 first_m54_start.ToInternalValue()); |
| 105 } | 120 } |
| 106 base::TimeDelta time_since_first_m54_start = now - first_m54_start; | 121 base::TimeDelta time_since_first_m54_start = now - first_m54_start; |
| 122 // Note: Setting the fallback timeout to zero effectively turns off the |
| 123 // fallback entirely. |
| 107 creation_date_fallback_ = | 124 creation_date_fallback_ = |
| 108 time_since_first_m54_start.InDays() < kUseCreationDateFallbackForDays; | 125 time_since_first_m54_start.InDays() < UseCreationDateFallbackForDays(); |
| 109 bookmark_model_->AddObserver(this); | 126 bookmark_model_->AddObserver(this); |
| 110 FetchBookmarks(); | 127 FetchBookmarks(); |
| 111 } | 128 } |
| 112 | 129 |
| 113 BookmarkSuggestionsProvider::~BookmarkSuggestionsProvider() { | 130 BookmarkSuggestionsProvider::~BookmarkSuggestionsProvider() { |
| 114 bookmark_model_->RemoveObserver(this); | 131 bookmark_model_->RemoveObserver(this); |
| 115 } | 132 } |
| 116 | 133 |
| 117 // static | 134 // static |
| 118 void BookmarkSuggestionsProvider::RegisterProfilePrefs( | 135 void BookmarkSuggestionsProvider::RegisterProfilePrefs( |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 | 288 |
| 272 void BookmarkSuggestionsProvider::NotifyStatusChanged( | 289 void BookmarkSuggestionsProvider::NotifyStatusChanged( |
| 273 CategoryStatus new_status) { | 290 CategoryStatus new_status) { |
| 274 if (category_status_ == new_status) | 291 if (category_status_ == new_status) |
| 275 return; | 292 return; |
| 276 category_status_ = new_status; | 293 category_status_ = new_status; |
| 277 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | 294 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
| 278 } | 295 } |
| 279 | 296 |
| 280 } // namespace ntp_snippets | 297 } // namespace ntp_snippets |
| OLD | NEW |