OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/ntp_snippets/bookmarks/bookmark_suggestions_provider.h" |
| 6 |
| 7 #include <utility> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "components/bookmarks/browser/bookmark_model.h" |
| 12 #include "components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h" |
| 13 #include "components/ntp_snippets/content_suggestion.h" |
| 14 |
| 15 using bookmarks::BookmarkModel; |
| 16 using bookmarks::BookmarkNode; |
| 17 |
| 18 namespace { |
| 19 const int kMaxBookmarks = 10; |
| 20 const int kMaxBookmarkAgeInDays = 42; |
| 21 |
| 22 base::Time GetThresholdTime() { |
| 23 return base::Time::Now() - base::TimeDelta::FromDays(kMaxBookmarkAgeInDays); |
| 24 } |
| 25 |
| 26 } // namespace |
| 27 |
| 28 namespace ntp_snippets { |
| 29 |
| 30 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( |
| 31 bookmarks::BookmarkModel* bookmark_model) |
| 32 : ContentSuggestionsProvider({ContentSuggestionsCategory::BOOKMARKS}), |
| 33 category_status_(ContentSuggestionsCategoryStatus::AVAILABLE_LOADING), |
| 34 observer_(nullptr), |
| 35 bookmark_model_(bookmark_model), |
| 36 fetch_requested_(false), |
| 37 end_of_list_last_visit_date_(GetThresholdTime()) { |
| 38 bookmark_model_->AddObserver(this); |
| 39 } |
| 40 |
| 41 BookmarkSuggestionsProvider::~BookmarkSuggestionsProvider() {} |
| 42 |
| 43 // Inherited from KeyedService. |
| 44 void BookmarkSuggestionsProvider::Shutdown() { |
| 45 bookmark_model_->RemoveObserver(this); |
| 46 category_status_ = ContentSuggestionsCategoryStatus::NOT_PROVIDED; |
| 47 } |
| 48 |
| 49 //////////////////////////////////////////////////////////////////////////////// |
| 50 // Private methods |
| 51 |
| 52 void BookmarkSuggestionsProvider::SetObserver( |
| 53 ContentSuggestionsProvider::Observer* observer) { |
| 54 observer_ = observer; |
| 55 if (observer) |
| 56 FetchBookmarks(); |
| 57 } |
| 58 |
| 59 ContentSuggestionsCategoryStatus BookmarkSuggestionsProvider::GetCategoryStatus( |
| 60 ContentSuggestionsCategory category) { |
| 61 return category_status_; |
| 62 } |
| 63 |
| 64 void BookmarkSuggestionsProvider::DismissSuggestion( |
| 65 const std::string& suggestion_id) { |
| 66 // TODO(jkrcal): Implement blacklisting bookmarks until they are next visited. |
| 67 // Then also implement ClearDismissedSuggestionsForDebugging. |
| 68 } |
| 69 |
| 70 void BookmarkSuggestionsProvider::FetchSuggestionImage( |
| 71 const std::string& suggestion_id, |
| 72 const ImageFetchedCallback& callback) { |
| 73 callback.Run(suggestion_id, gfx::Image()); |
| 74 } |
| 75 |
| 76 void BookmarkSuggestionsProvider::ClearCachedSuggestionsForDebugging() { |
| 77 // Ignored. |
| 78 } |
| 79 |
| 80 void BookmarkSuggestionsProvider::ClearDismissedSuggestionsForDebugging() { |
| 81 // TODO(jkrcal): Implement when discarded suggestions are supported. |
| 82 } |
| 83 |
| 84 void BookmarkSuggestionsProvider::BookmarkModelLoaded( |
| 85 bookmarks::BookmarkModel* model, |
| 86 bool ids_reassigned) { |
| 87 DCHECK_EQ(bookmark_model_, model); |
| 88 if (fetch_requested_) { |
| 89 fetch_requested_ = false; |
| 90 FetchBookmarksInternal(); |
| 91 } |
| 92 } |
| 93 |
| 94 void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo( |
| 95 BookmarkModel* model, |
| 96 const BookmarkNode* node) { |
| 97 // Store the last visit date of the node that is about to change. |
| 98 node_to_change_last_visit_date_ = GetLastVisitDateForBookmark(node); |
| 99 } |
| 100 |
| 101 void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged( |
| 102 BookmarkModel* model, |
| 103 const BookmarkNode* node) { |
| 104 base::Time time = GetLastVisitDateForBookmark(node); |
| 105 if (time == node_to_change_last_visit_date_ || |
| 106 time < end_of_list_last_visit_date_) |
| 107 return; |
| 108 |
| 109 // Last visit date of a node has changed (and is relevant for the list), we |
| 110 // should update the suggestions. |
| 111 FetchBookmarks(); |
| 112 } |
| 113 |
| 114 void BookmarkSuggestionsProvider::FetchBookmarksInternal() { |
| 115 DCHECK(bookmark_model_->loaded()); |
| 116 |
| 117 NotifyStatusChanged(ContentSuggestionsCategoryStatus::AVAILABLE); |
| 118 if (!observer_) |
| 119 return; |
| 120 |
| 121 std::vector<const BookmarkNode*> bookmarks = GetRecentlyVisitedBookmarks( |
| 122 bookmark_model_, kMaxBookmarks, GetThresholdTime()); |
| 123 |
| 124 std::vector<ContentSuggestion> suggestions; |
| 125 for (const BookmarkNode* bookmark : bookmarks) { |
| 126 ContentSuggestion suggestion( |
| 127 MakeUniqueID(ContentSuggestionsCategory::BOOKMARKS, |
| 128 bookmark->url().spec()), |
| 129 bookmark->url()); |
| 130 |
| 131 // TODO(jkrcal): keep it in UTF16 when ContentSuggestion title adapts to it. |
| 132 suggestion.set_title(base::UTF16ToUTF8(bookmark->GetTitle())); |
| 133 suggestion.set_snippet_text(std::string()); |
| 134 suggestion.set_publish_date(GetLastVisitDateForBookmark(bookmark)); |
| 135 suggestion.set_publisher_name(bookmark->url().host()); |
| 136 suggestions.emplace_back(std::move(suggestion)); |
| 137 } |
| 138 |
| 139 if (suggestions.empty()) |
| 140 end_of_list_last_visit_date_ = GetThresholdTime(); |
| 141 else |
| 142 end_of_list_last_visit_date_ = suggestions.back().publish_date(); |
| 143 |
| 144 observer_->OnNewSuggestions(ContentSuggestionsCategory::BOOKMARKS, |
| 145 std::move(suggestions)); |
| 146 } |
| 147 |
| 148 void BookmarkSuggestionsProvider::FetchBookmarks() { |
| 149 if (bookmark_model_->loaded()) |
| 150 FetchBookmarksInternal(); |
| 151 else |
| 152 fetch_requested_ = true; |
| 153 } |
| 154 |
| 155 void BookmarkSuggestionsProvider::NotifyStatusChanged( |
| 156 ContentSuggestionsCategoryStatus new_status) { |
| 157 if (category_status_ == new_status) |
| 158 return; |
| 159 category_status_ = new_status; |
| 160 |
| 161 if (!observer_) |
| 162 return; |
| 163 observer_->OnCategoryStatusChanged(ContentSuggestionsCategory::BOOKMARKS, |
| 164 new_status); |
| 165 } |
| 166 |
| 167 } // namespace ntp_snippets |
OLD | NEW |