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

Unified Diff: components/ntp_snippets/ntp_snippets_service.cc

Issue 1743333002: [NTP Snippets] Implement snippets expiry (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@snippets_persist
Patch Set: review Created 4 years, 10 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 | « components/ntp_snippets/ntp_snippets_service.h ('k') | no next file » | 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 50d8883bab478ee0acb7a34475f8ab1235707612..6a6898298a09ac69032965a80db510a44b1f85c2 100644
--- a/components/ntp_snippets/ntp_snippets_service.cc
+++ b/components/ntp_snippets/ntp_snippets_service.cc
@@ -148,6 +148,15 @@ bool NTPSnippetsService::LoadFromJSONList(const base::ListValue& list) {
if (!snippet)
return false;
+ // If the snippet has no publish/expiry dates, fill in defaults.
+ if (snippet->publish_date().is_null())
+ snippet->set_publish_date(base::Time::Now());
+ if (snippet->expiry_date().is_null()) {
+ snippet->set_expiry_date(
+ snippet->publish_date() +
+ base::TimeDelta::FromSeconds(2 * kDefaultFetchingIntervalSeconds));
+ }
+
// 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();
@@ -162,8 +171,10 @@ bool NTPSnippetsService::LoadFromJSONList(const base::ListValue& list) {
}
loaded_ = true;
- FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
- NTPSnippetsServiceLoaded(this));
+ // Immediately remove any already-expired snippets. This will also notify our
+ // observers and schedule the expiry timer.
+ RemoveExpiredSnippets();
+
return true;
}
@@ -188,4 +199,31 @@ void NTPSnippetsService::StoreToPrefs() {
pref_service_->Set(prefs::kSnippets, list);
}
+void NTPSnippetsService::RemoveExpiredSnippets() {
+ base::Time expiry = base::Time::Now();
+ snippets_.erase(
+ std::remove_if(snippets_.begin(), snippets_.end(),
+ [&expiry](const scoped_ptr<NTPSnippet>& snippet) {
+ return snippet->expiry_date() <= expiry;
+ }),
+ snippets_.end());
+
+ FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
+ NTPSnippetsServiceLoaded(this));
+
+ // If there are any snippets left, schedule a timer for the next expiry.
+ if (snippets_.empty())
+ return;
+
+ base::Time next_expiry = base::Time::Max();
+ for (const auto& snippet : snippets_) {
+ if (snippet->expiry_date() < next_expiry)
+ next_expiry = snippet->expiry_date();
+ }
+ DCHECK_GT(next_expiry, expiry);
+ expiry_timer_.Start(FROM_HERE, next_expiry - expiry,
+ base::Bind(&NTPSnippetsService::RemoveExpiredSnippets,
+ base::Unretained(this)));
+}
+
} // namespace ntp_snippets
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698