| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/reading_list/reading_list_suggestions_provider
.h" | 5 #include "components/ntp_snippets/reading_list/reading_list_suggestions_provider
.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 IDS_NTP_READING_LIST_SUGGESTIONS_SECTION_HEADER), | 63 IDS_NTP_READING_LIST_SUGGESTIONS_SECTION_HEADER), |
| 64 ContentSuggestionsCardLayout::FULL_CARD, | 64 ContentSuggestionsCardLayout::FULL_CARD, |
| 65 ContentSuggestionsAdditionalAction::VIEW_ALL, | 65 ContentSuggestionsAdditionalAction::VIEW_ALL, |
| 66 /*show_if_empty=*/false, | 66 /*show_if_empty=*/false, |
| 67 l10n_util::GetStringUTF16( | 67 l10n_util::GetStringUTF16( |
| 68 IDS_NTP_READING_LIST_SUGGESTIONS_SECTION_EMPTY)); | 68 IDS_NTP_READING_LIST_SUGGESTIONS_SECTION_EMPTY)); |
| 69 } | 69 } |
| 70 | 70 |
| 71 void ReadingListSuggestionsProvider::DismissSuggestion( | 71 void ReadingListSuggestionsProvider::DismissSuggestion( |
| 72 const ContentSuggestion::ID& suggestion_id) { | 72 const ContentSuggestion::ID& suggestion_id) { |
| 73 // TODO(crbug.com/702241): Implement this method. | 73 if (!reading_list_model_) { |
| 74 return; |
| 75 } |
| 76 |
| 77 DCHECK(reading_list_model_->loaded()); |
| 78 GURL url(suggestion_id.id_within_category()); |
| 79 SetDismissedState(url, true); |
| 74 } | 80 } |
| 75 | 81 |
| 76 void ReadingListSuggestionsProvider::FetchSuggestionImage( | 82 void ReadingListSuggestionsProvider::FetchSuggestionImage( |
| 77 const ContentSuggestion::ID& suggestion_id, | 83 const ContentSuggestion::ID& suggestion_id, |
| 78 const ImageFetchedCallback& callback) { | 84 const ImageFetchedCallback& callback) { |
| 79 base::ThreadTaskRunnerHandle::Get()->PostTask( | 85 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 80 FROM_HERE, base::Bind(callback, gfx::Image())); | 86 FROM_HERE, base::Bind(callback, gfx::Image())); |
| 81 } | 87 } |
| 82 | 88 |
| 83 void ReadingListSuggestionsProvider::Fetch( | 89 void ReadingListSuggestionsProvider::Fetch( |
| 84 const Category& category, | 90 const Category& category, |
| 85 const std::set<std::string>& known_suggestion_ids, | 91 const std::set<std::string>& known_suggestion_ids, |
| 86 const FetchDoneCallback& callback) { | 92 const FetchDoneCallback& callback) { |
| 87 LOG(DFATAL) << "ReadingListSuggestionsProvider has no |Fetch| functionality!"; | 93 LOG(DFATAL) << "ReadingListSuggestionsProvider has no |Fetch| functionality!"; |
| 88 base::ThreadTaskRunnerHandle::Get()->PostTask( | 94 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 89 FROM_HERE, | 95 FROM_HERE, |
| 90 base::Bind(callback, | 96 base::Bind(callback, |
| 91 Status(StatusCode::PERMANENT_ERROR, | 97 Status(StatusCode::PERMANENT_ERROR, |
| 92 "ReadingListSuggestionsProvider has no |Fetch| " | 98 "ReadingListSuggestionsProvider has no |Fetch| " |
| 93 "functionality!"), | 99 "functionality!"), |
| 94 base::Passed(std::vector<ContentSuggestion>()))); | 100 base::Passed(std::vector<ContentSuggestion>()))); |
| 95 } | 101 } |
| 96 | 102 |
| 97 void ReadingListSuggestionsProvider::ClearHistory( | 103 void ReadingListSuggestionsProvider::ClearHistory( |
| 98 base::Time begin, | 104 base::Time begin, |
| 99 base::Time end, | 105 base::Time end, |
| 100 const base::Callback<bool(const GURL& url)>& filter) { | 106 const base::Callback<bool(const GURL& url)>& filter) { |
| 101 // TODO(crbug.com/702241): Implement this method. | 107 // Ignored, Reading List does not depend on history. |
| 102 } | 108 } |
| 103 | 109 |
| 104 void ReadingListSuggestionsProvider::ClearCachedSuggestions(Category category) { | 110 void ReadingListSuggestionsProvider::ClearCachedSuggestions(Category category) { |
| 105 DCHECK_EQ(category, provided_category_); | 111 DCHECK_EQ(category, provided_category_); |
| 106 // Ignored. | 112 // Ignored. |
| 107 } | 113 } |
| 108 | 114 |
| 109 void ReadingListSuggestionsProvider::GetDismissedSuggestionsForDebugging( | 115 void ReadingListSuggestionsProvider::GetDismissedSuggestionsForDebugging( |
| 110 Category category, | 116 Category category, |
| 111 const DismissedSuggestionsCallback& callback) { | 117 const DismissedSuggestionsCallback& callback) { |
| 112 // TODO(crbug.com/702241): Implement this method. | 118 if (!reading_list_model_ || reading_list_model_->IsPerformingBatchUpdates()) { |
| 119 callback.Run(std::vector<ContentSuggestion>()); |
| 120 return; |
| 121 } |
| 122 |
| 123 DCHECK(reading_list_model_->loaded()); |
| 124 std::vector<const ReadingListEntry*> entries; |
| 125 for (const GURL& url : reading_list_model_->Keys()) { |
| 126 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url); |
| 127 if (entry->ContentSuggestionsExtra()->dismissed) { |
| 128 entries.emplace_back(entry); |
| 129 } |
| 130 } |
| 131 |
| 132 std::sort(entries.begin(), entries.end(), CompareEntries); |
| 133 |
| 134 std::vector<ContentSuggestion> suggestions; |
| 135 for (const ReadingListEntry* entry : entries) { |
| 136 suggestions.emplace_back(ConvertEntry(entry)); |
| 137 } |
| 138 |
| 139 callback.Run(std::move(suggestions)); |
| 113 } | 140 } |
| 141 |
| 114 void ReadingListSuggestionsProvider::ClearDismissedSuggestionsForDebugging( | 142 void ReadingListSuggestionsProvider::ClearDismissedSuggestionsForDebugging( |
| 115 Category category) { | 143 Category category) { |
| 116 // TODO(crbug.com/702241): Implement this method. | 144 for (const auto& url : reading_list_model_->Keys()) { |
| 145 SetDismissedState(url, false); |
| 146 } |
| 117 } | 147 } |
| 118 | 148 |
| 119 void ReadingListSuggestionsProvider::ReadingListModelLoaded( | 149 void ReadingListSuggestionsProvider::ReadingListModelLoaded( |
| 120 const ReadingListModel* model) { | 150 const ReadingListModel* model) { |
| 121 DCHECK(model == reading_list_model_); | 151 DCHECK(model == reading_list_model_); |
| 122 FetchReadingListInternal(); | 152 FetchReadingListInternal(); |
| 123 } | 153 } |
| 124 | 154 |
| 125 void ReadingListSuggestionsProvider::ReadingListModelBeingDeleted( | 155 void ReadingListSuggestionsProvider::ReadingListModelBeingDeleted( |
| 126 const ReadingListModel* model) { | 156 const ReadingListModel* model) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 145 | 175 |
| 146 void ReadingListSuggestionsProvider::FetchReadingListInternal() { | 176 void ReadingListSuggestionsProvider::FetchReadingListInternal() { |
| 147 if (!reading_list_model_ || reading_list_model_->IsPerformingBatchUpdates()) { | 177 if (!reading_list_model_ || reading_list_model_->IsPerformingBatchUpdates()) { |
| 148 return; | 178 return; |
| 149 } | 179 } |
| 150 | 180 |
| 151 DCHECK(reading_list_model_->loaded()); | 181 DCHECK(reading_list_model_->loaded()); |
| 152 std::vector<const ReadingListEntry*> entries; | 182 std::vector<const ReadingListEntry*> entries; |
| 153 for (const GURL& url : reading_list_model_->Keys()) { | 183 for (const GURL& url : reading_list_model_->Keys()) { |
| 154 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url); | 184 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url); |
| 155 if (!entry->IsRead()) { | 185 if (!entry->IsRead() && !entry->ContentSuggestionsExtra()->dismissed) { |
| 156 entries.emplace_back(entry); | 186 entries.emplace_back(entry); |
| 157 } | 187 } |
| 158 } | 188 } |
| 159 | 189 |
| 160 if (entries.size() > kMaxEntries) { | 190 if (entries.size() > kMaxEntries) { |
| 161 // Get the |kMaxEntries| most recent entries. | 191 // Get the |kMaxEntries| most recent entries. |
| 162 std::partial_sort(entries.begin(), entries.begin() + kMaxEntries, | 192 std::partial_sort(entries.begin(), entries.begin() + kMaxEntries, |
| 163 entries.end(), CompareEntries); | 193 entries.end(), CompareEntries); |
| 164 entries.resize(kMaxEntries); | 194 entries.resize(kMaxEntries); |
| 165 } else { | 195 } else { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 | 231 |
| 202 void ReadingListSuggestionsProvider::NotifyStatusChanged( | 232 void ReadingListSuggestionsProvider::NotifyStatusChanged( |
| 203 CategoryStatus new_status) { | 233 CategoryStatus new_status) { |
| 204 if (category_status_ == new_status) { | 234 if (category_status_ == new_status) { |
| 205 return; | 235 return; |
| 206 } | 236 } |
| 207 category_status_ = new_status; | 237 category_status_ = new_status; |
| 208 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | 238 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
| 209 } | 239 } |
| 210 | 240 |
| 241 void ReadingListSuggestionsProvider::SetDismissedState(const GURL& url, |
| 242 bool dismissed) { |
| 243 reading_list::ContentSuggestionsExtra extra; |
| 244 extra.dismissed = dismissed; |
| 245 reading_list_model_->SetContentSuggestionsExtra(url, extra); |
| 246 } |
| 247 |
| 211 } // namespace ntp_snippets | 248 } // namespace ntp_snippets |
| OLD | NEW |