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

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: . 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..6cf18305e283ce5c635a9f62d0c85b6e8c71ef80 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,34 @@ void NTPSnippetsService::StoreToPrefs() {
pref_service_->Set(prefs::kSnippets, list);
}
+void NTPSnippetsService::ScheduleExpiryTimer() {
+ 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();
+ }
+ // Note: If |next_expiry| is in the past, this will run immediately.
Bernhard Bauer 2016/03/01 11:54:27 Would this still happen now? Should we rather DCHE
Marc Treib 2016/03/01 13:16:49 Done.
+ expiry_timer_.Start(FROM_HERE, next_expiry - base::Time::Now(),
+ base::Bind(&NTPSnippetsService::RemoveExpiredSnippets,
+ base::Unretained(this)));
+}
+
+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));
+
+ ScheduleExpiryTimer();
Bernhard Bauer 2016/03/01 11:54:27 You could also merge these two methods now.
Marc Treib 2016/03/01 13:16:49 Done.
+}
+
} // 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