| 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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 } | 82 } |
| 83 | 83 |
| 84 CategoryStatus BookmarkSuggestionsProvider::GetCategoryStatus( | 84 CategoryStatus BookmarkSuggestionsProvider::GetCategoryStatus( |
| 85 Category category) { | 85 Category category) { |
| 86 DCHECK_EQ(category, provided_category_); | 86 DCHECK_EQ(category, provided_category_); |
| 87 return category_status_; | 87 return category_status_; |
| 88 } | 88 } |
| 89 | 89 |
| 90 void BookmarkSuggestionsProvider::DismissSuggestion( | 90 void BookmarkSuggestionsProvider::DismissSuggestion( |
| 91 const std::string& suggestion_id) { | 91 const std::string& suggestion_id) { |
| 92 // TODO(jkrcal): Implement blacklisting bookmarks until they are next visited. | 92 DCHECK(bookmark_model_->loaded()); |
| 93 // Then also implement ClearDismissedSuggestionsForDebugging. | 93 GURL url(GetWithinCategoryIDFromUniqueID(suggestion_id)); |
| 94 MarkBookmarksDismissed(bookmark_model_, url); |
| 94 } | 95 } |
| 95 | 96 |
| 96 void BookmarkSuggestionsProvider::FetchSuggestionImage( | 97 void BookmarkSuggestionsProvider::FetchSuggestionImage( |
| 97 const std::string& suggestion_id, | 98 const std::string& suggestion_id, |
| 98 const ImageFetchedCallback& callback) { | 99 const ImageFetchedCallback& callback) { |
| 99 base::ThreadTaskRunnerHandle::Get()->PostTask( | 100 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 100 FROM_HERE, base::Bind(callback, suggestion_id, gfx::Image())); | 101 FROM_HERE, base::Bind(callback, suggestion_id, gfx::Image())); |
| 101 } | 102 } |
| 102 | 103 |
| 103 void BookmarkSuggestionsProvider::ClearCachedSuggestionsForDebugging( | 104 void BookmarkSuggestionsProvider::ClearCachedSuggestionsForDebugging( |
| 104 Category category) { | 105 Category category) { |
| 105 DCHECK_EQ(category, provided_category_); | 106 DCHECK_EQ(category, provided_category_); |
| 106 // Ignored. | 107 // Ignored. |
| 107 } | 108 } |
| 108 | 109 |
| 109 std::vector<ContentSuggestion> | 110 std::vector<ContentSuggestion> |
| 110 BookmarkSuggestionsProvider::GetDismissedSuggestionsForDebugging( | 111 BookmarkSuggestionsProvider::GetDismissedSuggestionsForDebugging( |
| 111 Category category) { | 112 Category category) { |
| 112 DCHECK_EQ(category, provided_category_); | 113 DCHECK_EQ(category, provided_category_); |
| 113 // TODO(pke): Implement when discarded suggestions are supported. | 114 std::vector<const BookmarkNode*> bookmarks = |
| 114 return std::vector<ContentSuggestion>(); | 115 GetDismissedBookmarksForDebugging(bookmark_model_); |
| 116 |
| 117 std::vector<ContentSuggestion> suggestions; |
| 118 for (const BookmarkNode* bookmark : bookmarks) |
| 119 suggestions.emplace_back(ConvertBookmark(bookmark)); |
| 120 return suggestions; |
| 115 } | 121 } |
| 116 | 122 |
| 117 void BookmarkSuggestionsProvider::ClearDismissedSuggestionsForDebugging( | 123 void BookmarkSuggestionsProvider::ClearDismissedSuggestionsForDebugging( |
| 118 Category category) { | 124 Category category) { |
| 119 DCHECK_EQ(category, provided_category_); | 125 DCHECK_EQ(category, provided_category_); |
| 120 // TODO(pke): Implement when discarded suggestions are supported. | 126 if (!bookmark_model_->loaded()) |
| 127 return; |
| 128 MarkAllBookmarksUndismissed(bookmark_model_); |
| 121 } | 129 } |
| 122 | 130 |
| 123 void BookmarkSuggestionsProvider::BookmarkModelLoaded( | 131 void BookmarkSuggestionsProvider::BookmarkModelLoaded( |
| 124 bookmarks::BookmarkModel* model, | 132 bookmarks::BookmarkModel* model, |
| 125 bool ids_reassigned) { | 133 bool ids_reassigned) { |
| 126 DCHECK_EQ(bookmark_model_, model); | 134 DCHECK_EQ(bookmark_model_, model); |
| 127 if (fetch_requested_) { | 135 if (fetch_requested_) { |
| 128 fetch_requested_ = false; | 136 fetch_requested_ = false; |
| 129 FetchBookmarksInternal(); | 137 FetchBookmarksInternal(); |
| 130 } | 138 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 143 base::Time time = GetLastVisitDateForBookmark(node); | 151 base::Time time = GetLastVisitDateForBookmark(node); |
| 144 if (time == node_to_change_last_visit_date_ || | 152 if (time == node_to_change_last_visit_date_ || |
| 145 time < end_of_list_last_visit_date_) | 153 time < end_of_list_last_visit_date_) |
| 146 return; | 154 return; |
| 147 | 155 |
| 148 // Last visit date of a node has changed (and is relevant for the list), we | 156 // Last visit date of a node has changed (and is relevant for the list), we |
| 149 // should update the suggestions. | 157 // should update the suggestions. |
| 150 FetchBookmarks(); | 158 FetchBookmarks(); |
| 151 } | 159 } |
| 152 | 160 |
| 161 ContentSuggestion BookmarkSuggestionsProvider::ConvertBookmark( |
| 162 const BookmarkNode* bookmark) { |
| 163 ContentSuggestion suggestion( |
| 164 MakeUniqueID(provided_category_, bookmark->url().spec()), |
| 165 bookmark->url()); |
| 166 |
| 167 suggestion.set_title(bookmark->GetTitle()); |
| 168 suggestion.set_snippet_text(base::string16()); |
| 169 suggestion.set_publish_date(GetLastVisitDateForBookmark(bookmark)); |
| 170 suggestion.set_publisher_name(base::UTF8ToUTF16(bookmark->url().host())); |
| 171 return suggestion; |
| 172 } |
| 173 |
| 153 void BookmarkSuggestionsProvider::FetchBookmarksInternal() { | 174 void BookmarkSuggestionsProvider::FetchBookmarksInternal() { |
| 154 DCHECK(bookmark_model_->loaded()); | 175 DCHECK(bookmark_model_->loaded()); |
| 155 | 176 |
| 156 NotifyStatusChanged(CategoryStatus::AVAILABLE); | 177 NotifyStatusChanged(CategoryStatus::AVAILABLE); |
| 157 | 178 |
| 158 base::Time threshold_time = GetThresholdTime(); | 179 base::Time threshold_time = GetThresholdTime(); |
| 159 std::vector<const BookmarkNode*> bookmarks = GetRecentlyVisitedBookmarks( | 180 std::vector<const BookmarkNode*> bookmarks = GetRecentlyVisitedBookmarks( |
| 160 bookmark_model_, GetMaxCount(), threshold_time); | 181 bookmark_model_, GetMaxCount(), threshold_time); |
| 161 | 182 |
| 162 std::vector<ContentSuggestion> suggestions; | 183 std::vector<ContentSuggestion> suggestions; |
| 163 for (const BookmarkNode* bookmark : bookmarks) { | 184 for (const BookmarkNode* bookmark : bookmarks) |
| 164 ContentSuggestion suggestion( | 185 suggestions.emplace_back(ConvertBookmark(bookmark)); |
| 165 MakeUniqueID(provided_category_, bookmark->url().spec()), | |
| 166 bookmark->url()); | |
| 167 | |
| 168 suggestion.set_title(bookmark->GetTitle()); | |
| 169 suggestion.set_snippet_text(base::string16()); | |
| 170 suggestion.set_publish_date(GetLastVisitDateForBookmark(bookmark)); | |
| 171 suggestion.set_publisher_name(base::UTF8ToUTF16(bookmark->url().host())); | |
| 172 suggestions.emplace_back(std::move(suggestion)); | |
| 173 } | |
| 174 | 186 |
| 175 if (suggestions.empty()) | 187 if (suggestions.empty()) |
| 176 end_of_list_last_visit_date_ = threshold_time; | 188 end_of_list_last_visit_date_ = threshold_time; |
| 177 else | 189 else |
| 178 end_of_list_last_visit_date_ = suggestions.back().publish_date(); | 190 end_of_list_last_visit_date_ = suggestions.back().publish_date(); |
| 179 | 191 |
| 180 observer()->OnNewSuggestions(this, provided_category_, | 192 observer()->OnNewSuggestions(this, provided_category_, |
| 181 std::move(suggestions)); | 193 std::move(suggestions)); |
| 182 } | 194 } |
| 183 | 195 |
| 184 void BookmarkSuggestionsProvider::FetchBookmarks() { | 196 void BookmarkSuggestionsProvider::FetchBookmarks() { |
| 185 if (bookmark_model_->loaded()) | 197 if (bookmark_model_->loaded()) |
| 186 FetchBookmarksInternal(); | 198 FetchBookmarksInternal(); |
| 187 else | 199 else |
| 188 fetch_requested_ = true; | 200 fetch_requested_ = true; |
| 189 } | 201 } |
| 190 | 202 |
| 191 void BookmarkSuggestionsProvider::NotifyStatusChanged( | 203 void BookmarkSuggestionsProvider::NotifyStatusChanged( |
| 192 CategoryStatus new_status) { | 204 CategoryStatus new_status) { |
| 193 if (category_status_ == new_status) | 205 if (category_status_ == new_status) |
| 194 return; | 206 return; |
| 195 category_status_ = new_status; | 207 category_status_ = new_status; |
| 196 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | 208 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
| 197 } | 209 } |
| 198 | 210 |
| 199 } // namespace ntp_snippets | 211 } // namespace ntp_snippets |
| OLD | NEW |