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

Unified Diff: components/ntp_snippets/ntp_snippets_service.cc

Issue 1908973002: [NTP Snippets] Add newly-fetched snippets at the front instead of at the end (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: spaces! Created 4 years, 8 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
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 781604d83829cd94d44a3f2bbc93004931897e8c..84dfaecdbeaba33b4e7e12317311c991a182ecad 100644
--- a/components/ntp_snippets/ntp_snippets_service.cc
+++ b/components/ntp_snippets/ntp_snippets_service.cc
@@ -5,6 +5,7 @@
#include "components/ntp_snippets/ntp_snippets_service.h"
#include <algorithm>
+#include <iterator>
#include <utility>
#include "base/command_line.h"
@@ -151,6 +152,15 @@ scoped_ptr<base::ListValue> SnippetsToListValue(
return list;
}
+bool ContainsSnippet(const NTPSnippetsService::NTPSnippetStorage& haystack,
+ const scoped_ptr<NTPSnippet>& needle) {
+ const GURL& url = needle->url();
+ return std::find_if(haystack.begin(), haystack.end(),
+ [&url](const scoped_ptr<NTPSnippet>& snippet) {
+ return snippet->url() == url;
+ }) != haystack.end();
+}
+
} // namespace
NTPSnippetsService::NTPSnippetsService(
@@ -349,12 +359,18 @@ bool NTPSnippetsService::LoadFromListValue(const base::ListValue& list) {
NTPSnippetStorage new_snippets;
if (!AddSnippetsFromListValue(list, &new_snippets))
return false;
- for (scoped_ptr<NTPSnippet>& snippet : new_snippets) {
- // If this snippet has previously been discarded, don't add it again.
- if (HasDiscardedSnippet(snippet->url()))
- continue;
- // If the snippet has no publish/expiry dates, fill in defaults.
+ // Remove new snippets that we already have, or that have been discarded.
+ new_snippets.erase(
+ std::remove_if(new_snippets.begin(), new_snippets.end(),
+ [this](const scoped_ptr<NTPSnippet>& snippet) {
+ return ContainsSnippet(discarded_snippets_, snippet) ||
+ ContainsSnippet(snippets_, snippet);
+ }),
+ new_snippets.end());
+
+ // Fill in default publish/expiry dates where required.
+ for (scoped_ptr<NTPSnippet>& snippet : new_snippets) {
if (snippet->publish_date().is_null())
snippet->set_publish_date(base::Time::Now());
if (snippet->expiry_date().is_null()) {
@@ -362,20 +378,13 @@ bool NTPSnippetsService::LoadFromListValue(const base::ListValue& list) {
snippet->publish_date() +
base::TimeDelta::FromMinutes(kDefaultExpiryTimeMins));
}
-
- // Check if we already have a snippet with the same URL. If so, replace it
- // rather than adding a duplicate.
- const GURL& url = snippet->url();
- auto it = std::find_if(snippets_.begin(), snippets_.end(),
- [&url](const scoped_ptr<NTPSnippet>& old_snippet) {
- return old_snippet->url() == url;
- });
- if (it != snippets_.end())
- *it = std::move(snippet);
- else
- snippets_.push_back(std::move(snippet));
}
+ // Insert the new snippets at the front.
+ snippets_.insert(snippets_.begin(),
+ std::make_move_iterator(new_snippets.begin()),
+ std::make_move_iterator(new_snippets.end()));
+
// Immediately remove any already-expired snippets. This will also notify our
// observers and schedule the expiry timer.
RemoveExpiredSnippets();
@@ -425,14 +434,6 @@ void NTPSnippetsService::StoreSnippetHostsToPrefs(
pref_service_->Set(prefs::kSnippetHosts, list);
}
-bool NTPSnippetsService::HasDiscardedSnippet(const GURL& url) const {
- auto it = std::find_if(discarded_snippets_.begin(), discarded_snippets_.end(),
- [&url](const scoped_ptr<NTPSnippet>& snippet) {
- return snippet->url() == url;
- });
- return it != discarded_snippets_.end();
-}
-
void NTPSnippetsService::RemoveExpiredSnippets() {
base::Time expiry = base::Time::Now();
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.h ('k') | components/ntp_snippets/ntp_snippets_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698