Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Unified Diff: components/ntp_snippets/ntp_snippets_service.cc

Issue 1992803002: Use multiple IDs when discarding or merging snippets. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixed new tests (they didn't expect de-duping based on corpus-ids.). Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | components/ntp_snippets/ntp_snippets_service_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/ntp_snippets/ntp_snippets_service.cc
diff --git a/components/ntp_snippets/ntp_snippets_service.cc b/components/ntp_snippets/ntp_snippets_service.cc
index 448c31a6acce280820f5d0f8ce06910b45af1311..fc5edefc7194f06a9e925e68032c66acd8b0fc8f 100644
--- a/components/ntp_snippets/ntp_snippets_service.cc
+++ b/components/ntp_snippets/ntp_snippets_service.cc
@@ -161,13 +161,13 @@ std::unique_ptr<base::ListValue> SnippetsToListValue(
return list;
}
-bool ContainsSnippet(const NTPSnippet::PtrVector& haystack,
- const std::unique_ptr<NTPSnippet>& needle) {
- const std::string& id = needle->id();
- return std::find_if(haystack.begin(), haystack.end(),
- [&id](const std::unique_ptr<NTPSnippet>& snippet) {
- return snippet->id() == id;
- }) != haystack.end();
+void InsertAllIDs(const NTPSnippet::PtrVector& snippets,
+ std::set<std::string>* ids) {
+ for (const std::unique_ptr<NTPSnippet>& snippet : snippets) {
+ ids->insert(snippet->id());
+ for (const SnippetSource& source : snippet->sources())
+ ids->insert(source.url.spec());
+ }
}
void WrapImageFetchedCallback(
@@ -393,12 +393,21 @@ void NTPSnippetsService::OnFetchFinished(
void NTPSnippetsService::MergeSnippets(NTPSnippet::PtrVector new_snippets) {
// Remove new snippets that we already have, or that have been discarded.
+ std::set<std::string> old_snippet_ids;
+ InsertAllIDs(discarded_snippets_, &old_snippet_ids);
+ InsertAllIDs(snippets_, &old_snippet_ids);
new_snippets.erase(
- std::remove_if(new_snippets.begin(), new_snippets.end(),
- [this](const std::unique_ptr<NTPSnippet>& snippet) {
- return ContainsSnippet(discarded_snippets_, snippet) ||
- ContainsSnippet(snippets_, snippet);
- }),
+ std::remove_if(
+ new_snippets.begin(), new_snippets.end(),
+ [&old_snippet_ids](const std::unique_ptr<NTPSnippet>& snippet) {
+ if (old_snippet_ids.count(snippet->id()))
+ return true;
+ for (const SnippetSource& source : snippet->sources()) {
+ if (old_snippet_ids.count(source.url.spec()))
+ return true;
+ }
+ return false;
+ }),
new_snippets.end());
// Fill in default publish/expiry dates where required.
@@ -433,7 +442,6 @@ void NTPSnippetsService::MergeSnippets(NTPSnippet::PtrVector new_snippets) {
num_snippets_discarded);
}
}
-
// Insert the new snippets at the front.
snippets_.insert(snippets_.begin(),
std::make_move_iterator(new_snippets.begin()),
« no previous file with comments | « no previous file | components/ntp_snippets/ntp_snippets_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698