| 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 "base/time/time.h" |
| 16 #include "components/bookmarks/browser/bookmark_model.h" | 16 #include "components/bookmarks/browser/bookmark_model.h" |
| 17 #include "components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h" | 17 #include "components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h" |
| 18 #include "components/ntp_snippets/category.h" | 18 #include "components/ntp_snippets/category.h" |
| 19 #include "components/ntp_snippets/content_suggestion.h" | 19 #include "components/ntp_snippets/content_suggestion.h" |
| 20 #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" | |
| 24 #include "components/strings/grit/components_strings.h" | 21 #include "components/strings/grit/components_strings.h" |
| 25 #include "components/variations/variations_associated_data.h" | 22 #include "components/variations/variations_associated_data.h" |
| 26 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
| 27 #include "ui/gfx/image/image.h" | 24 #include "ui/gfx/image/image.h" |
| 28 | 25 |
| 29 using bookmarks::BookmarkModel; | 26 using bookmarks::BookmarkModel; |
| 30 using bookmarks::BookmarkNode; | 27 using bookmarks::BookmarkNode; |
| 31 | 28 |
| 32 namespace ntp_snippets { | 29 namespace ntp_snippets { |
| 33 | 30 |
| 34 namespace { | 31 namespace { |
| 35 | 32 |
| 36 const int kMaxBookmarks = 10; | 33 const int kMaxBookmarks = 10; |
| 37 const int kMaxBookmarkAgeInDays = 42; | 34 const int kMaxBookmarkAgeInDays = 42; |
| 38 | 35 |
| 39 const char* kMaxBookmarksParamName = "bookmarks_max_count"; | 36 const char* kMaxBookmarksParamName = "bookmarks_max_count"; |
| 40 const char* kMaxBookmarkAgeInDaysParamName = "bookmarks_max_age_in_days"; | 37 const char* kMaxBookmarkAgeInDaysParamName = "bookmarks_max_age_in_days"; |
| 41 const char* kConsiderDesktopVisitsParamName = | 38 const char* kConsiderDesktopVisitsParamName = |
| 42 "bookmarks_consider_desktop_visits"; | 39 "bookmarks_consider_desktop_visits"; |
| 43 | 40 |
| 44 // TODO(treib,jkrcal): Remove this after M57. | |
| 45 const char kDeprecatedBookmarksFirstM54StartPref[] = | |
| 46 "ntp_suggestions.bookmarks.first_M54_start"; | |
| 47 | |
| 48 // Any bookmark created or visited after this time will be considered recent. | 41 // 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. | 42 // Note that bookmarks can be shown that do not meet this threshold. |
| 50 base::Time GetThresholdTime() { | 43 base::Time GetThresholdTime() { |
| 51 return base::Time::Now() - | 44 return base::Time::Now() - |
| 52 base::TimeDelta::FromDays(variations::GetVariationParamByFeatureAsInt( | 45 base::TimeDelta::FromDays(variations::GetVariationParamByFeatureAsInt( |
| 53 ntp_snippets::kBookmarkSuggestionsFeature, | 46 ntp_snippets::kBookmarkSuggestionsFeature, |
| 54 kMaxBookmarkAgeInDaysParamName, kMaxBookmarkAgeInDays)); | 47 kMaxBookmarkAgeInDaysParamName, kMaxBookmarkAgeInDays)); |
| 55 } | 48 } |
| 56 | 49 |
| 57 // The maximum number of suggestions ever provided. | 50 // The maximum number of suggestions ever provided. |
| 58 int GetMaxCount() { | 51 int GetMaxCount() { |
| 59 return variations::GetVariationParamByFeatureAsInt( | 52 return variations::GetVariationParamByFeatureAsInt( |
| 60 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarksParamName, | 53 ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarksParamName, |
| 61 kMaxBookmarks); | 54 kMaxBookmarks); |
| 62 } | 55 } |
| 63 | 56 |
| 64 bool AreDesktopVisitsConsidered() { | 57 bool AreDesktopVisitsConsidered() { |
| 65 return variations::GetVariationParamByFeatureAsBool( | 58 return variations::GetVariationParamByFeatureAsBool( |
| 66 ntp_snippets::kBookmarkSuggestionsFeature, | 59 ntp_snippets::kBookmarkSuggestionsFeature, |
| 67 kConsiderDesktopVisitsParamName, false); | 60 kConsiderDesktopVisitsParamName, false); |
| 68 } | 61 } |
| 69 | 62 |
| 70 } // namespace | 63 } // namespace |
| 71 | 64 |
| 72 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( | 65 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( |
| 73 ContentSuggestionsProvider::Observer* observer, | 66 ContentSuggestionsProvider::Observer* observer, |
| 74 bookmarks::BookmarkModel* bookmark_model, | 67 bookmarks::BookmarkModel* bookmark_model) |
| 75 PrefService* pref_service) | |
| 76 : ContentSuggestionsProvider(observer), | 68 : ContentSuggestionsProvider(observer), |
| 77 category_status_(CategoryStatus::AVAILABLE_LOADING), | 69 category_status_(CategoryStatus::AVAILABLE_LOADING), |
| 78 provided_category_( | 70 provided_category_( |
| 79 Category::FromKnownCategory(KnownCategories::BOOKMARKS)), | 71 Category::FromKnownCategory(KnownCategories::BOOKMARKS)), |
| 80 bookmark_model_(bookmark_model), | 72 bookmark_model_(bookmark_model), |
| 81 fetch_requested_(false), | 73 fetch_requested_(false), |
| 82 end_of_list_last_visit_date_(GetThresholdTime()), | 74 end_of_list_last_visit_date_(GetThresholdTime()), |
| 83 consider_bookmark_visits_from_desktop_(AreDesktopVisitsConsidered()) { | 75 consider_bookmark_visits_from_desktop_(AreDesktopVisitsConsidered()) { |
| 84 observer->OnCategoryStatusChanged(this, provided_category_, category_status_); | 76 observer->OnCategoryStatusChanged(this, provided_category_, category_status_); |
| 85 pref_service->ClearPref(kDeprecatedBookmarksFirstM54StartPref); | |
| 86 bookmark_model_->AddObserver(this); | 77 bookmark_model_->AddObserver(this); |
| 87 FetchBookmarks(); | 78 FetchBookmarks(); |
| 88 } | 79 } |
| 89 | 80 |
| 90 BookmarkSuggestionsProvider::~BookmarkSuggestionsProvider() { | 81 BookmarkSuggestionsProvider::~BookmarkSuggestionsProvider() { |
| 91 bookmark_model_->RemoveObserver(this); | 82 bookmark_model_->RemoveObserver(this); |
| 92 } | 83 } |
| 93 | 84 |
| 94 // static | |
| 95 void BookmarkSuggestionsProvider::RegisterProfilePrefs( | |
| 96 PrefRegistrySimple* registry) { | |
| 97 registry->RegisterInt64Pref(kDeprecatedBookmarksFirstM54StartPref, 0); | |
| 98 } | |
| 99 | |
| 100 //////////////////////////////////////////////////////////////////////////////// | 85 //////////////////////////////////////////////////////////////////////////////// |
| 101 // Private methods | 86 // Private methods |
| 102 | 87 |
| 103 CategoryStatus BookmarkSuggestionsProvider::GetCategoryStatus( | 88 CategoryStatus BookmarkSuggestionsProvider::GetCategoryStatus( |
| 104 Category category) { | 89 Category category) { |
| 105 DCHECK_EQ(category, provided_category_); | 90 DCHECK_EQ(category, provided_category_); |
| 106 return category_status_; | 91 return category_status_; |
| 107 } | 92 } |
| 108 | 93 |
| 109 CategoryInfo BookmarkSuggestionsProvider::GetCategoryInfo(Category category) { | 94 CategoryInfo BookmarkSuggestionsProvider::GetCategoryInfo(Category category) { |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 void BookmarkSuggestionsProvider::NotifyStatusChanged( | 303 void BookmarkSuggestionsProvider::NotifyStatusChanged( |
| 319 CategoryStatus new_status) { | 304 CategoryStatus new_status) { |
| 320 if (category_status_ == new_status) { | 305 if (category_status_ == new_status) { |
| 321 return; | 306 return; |
| 322 } | 307 } |
| 323 category_status_ = new_status; | 308 category_status_ = new_status; |
| 324 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | 309 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
| 325 } | 310 } |
| 326 | 311 |
| 327 } // namespace ntp_snippets | 312 } // namespace ntp_snippets |
| OLD | NEW |