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

Side by Side 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, 9 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 unified diff | Download patch
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/ntp_snippets_service.h" 5 #include "components/ntp_snippets/ntp_snippets_service.h"
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/json/json_reader.h" 9 #include "base/json/json_reader.h"
10 #include "base/location.h" 10 #include "base/location.h"
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 if (!value->GetAsDictionary(&dict)) 141 if (!value->GetAsDictionary(&dict))
142 return false; 142 return false;
143 143
144 const base::DictionaryValue* content = nullptr; 144 const base::DictionaryValue* content = nullptr;
145 if (!dict->GetDictionary(kContentInfo, &content)) 145 if (!dict->GetDictionary(kContentInfo, &content))
146 return false; 146 return false;
147 scoped_ptr<NTPSnippet> snippet = NTPSnippet::CreateFromDictionary(*content); 147 scoped_ptr<NTPSnippet> snippet = NTPSnippet::CreateFromDictionary(*content);
148 if (!snippet) 148 if (!snippet)
149 return false; 149 return false;
150 150
151 // If the snippet has no publish/expiry dates, fill in defaults.
152 if (snippet->publish_date().is_null())
153 snippet->set_publish_date(base::Time::Now());
154 if (snippet->expiry_date().is_null()) {
155 snippet->set_expiry_date(
156 snippet->publish_date() +
157 base::TimeDelta::FromSeconds(2 * kDefaultFetchingIntervalSeconds));
158 }
159
151 // Check if we already have a snippet with the same URL. If so, replace it 160 // Check if we already have a snippet with the same URL. If so, replace it
152 // rather than adding a duplicate. 161 // rather than adding a duplicate.
153 const GURL& url = snippet->url(); 162 const GURL& url = snippet->url();
154 auto it = std::find_if(snippets_.begin(), snippets_.end(), 163 auto it = std::find_if(snippets_.begin(), snippets_.end(),
155 [&url](const scoped_ptr<NTPSnippet>& old_snippet) { 164 [&url](const scoped_ptr<NTPSnippet>& old_snippet) {
156 return old_snippet->url() == url; 165 return old_snippet->url() == url;
157 }); 166 });
158 if (it != snippets_.end()) 167 if (it != snippets_.end())
159 *it = std::move(snippet); 168 *it = std::move(snippet);
160 else 169 else
161 snippets_.push_back(std::move(snippet)); 170 snippets_.push_back(std::move(snippet));
162 } 171 }
163 loaded_ = true; 172 loaded_ = true;
164 173
165 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 174 // Immediately remove any already-expired snippets. This will also notify our
166 NTPSnippetsServiceLoaded(this)); 175 // observers and schedule the expiry timer.
176 RemoveExpiredSnippets();
177
167 return true; 178 return true;
168 } 179 }
169 180
170 void NTPSnippetsService::LoadFromPrefs() { 181 void NTPSnippetsService::LoadFromPrefs() {
171 // |pref_service_| can be null in tests. 182 // |pref_service_| can be null in tests.
172 if (!pref_service_) 183 if (!pref_service_)
173 return; 184 return;
174 if (!LoadFromJSONList(*pref_service_->GetList(prefs::kSnippets))) 185 if (!LoadFromJSONList(*pref_service_->GetList(prefs::kSnippets)))
175 LOG(ERROR) << "Failed to parse snippets from prefs"; 186 LOG(ERROR) << "Failed to parse snippets from prefs";
176 } 187 }
177 188
178 void NTPSnippetsService::StoreToPrefs() { 189 void NTPSnippetsService::StoreToPrefs() {
179 // |pref_service_| can be null in tests. 190 // |pref_service_| can be null in tests.
180 if (!pref_service_) 191 if (!pref_service_)
181 return; 192 return;
182 base::ListValue list; 193 base::ListValue list;
183 for (const auto& snippet : snippets_) { 194 for (const auto& snippet : snippets_) {
184 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); 195 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
185 dict->Set(kContentInfo, snippet->ToDictionary()); 196 dict->Set(kContentInfo, snippet->ToDictionary());
186 list.Append(std::move(dict)); 197 list.Append(std::move(dict));
187 } 198 }
188 pref_service_->Set(prefs::kSnippets, list); 199 pref_service_->Set(prefs::kSnippets, list);
189 } 200 }
190 201
202 void NTPSnippetsService::RemoveExpiredSnippets() {
203 base::Time expiry = base::Time::Now();
204 snippets_.erase(
205 std::remove_if(snippets_.begin(), snippets_.end(),
206 [&expiry](const scoped_ptr<NTPSnippet>& snippet) {
207 return snippet->expiry_date() <= expiry;
208 }),
209 snippets_.end());
210
211 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
212 NTPSnippetsServiceLoaded(this));
213
214 // If there are any snippets left, schedule a timer for the next expiry.
215 if (snippets_.empty())
216 return;
217
218 base::Time next_expiry = base::Time::Max();
219 for (const auto& snippet : snippets_) {
220 if (snippet->expiry_date() < next_expiry)
221 next_expiry = snippet->expiry_date();
222 }
223 DCHECK_GT(next_expiry, expiry);
224 expiry_timer_.Start(FROM_HERE, next_expiry - expiry,
225 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets,
226 base::Unretained(this)));
227 }
228
191 } // namespace ntp_snippets 229 } // namespace ntp_snippets
OLDNEW
« 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