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

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: 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
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 // Check if we already have a snippet with the same URL. If so, replace it 151 // Check if we already have a snippet with the same URL.
152 // rather than adding a duplicate.
153 const GURL& url = snippet->url(); 152 const GURL& url = snippet->url();
154 auto it = std::find_if(snippets_.begin(), snippets_.end(), 153 auto it = std::find_if(snippets_.begin(), snippets_.end(),
155 [&url](const scoped_ptr<NTPSnippet>& old_snippet) { 154 [&url](const scoped_ptr<NTPSnippet>& old_snippet) {
156 return old_snippet->url() == url; 155 return old_snippet->url() == url;
157 }); 156 });
158 if (it != snippets_.end()) 157 if (it == snippets_.end()) {
159 *it = std::move(snippet); 158 // If the snippet has no publish/expiry dates, fill in defaults.
160 else 159 if (snippet->publish_date().is_null())
160 snippet->set_publish_date(base::Time::Now());
161 if (snippet->expiry_date().is_null()) {
162 snippet->set_expiry_date(
163 snippet->publish_date() +
164 base::TimeDelta::FromSeconds(2 * kDefaultFetchingIntervalSeconds));
165 }
166
161 snippets_.push_back(std::move(snippet)); 167 snippets_.push_back(std::move(snippet));
168 }
162 } 169 }
163 loaded_ = true; 170 loaded_ = true;
164 171
165 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 172 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
166 NTPSnippetsServiceLoaded(this)); 173 NTPSnippetsServiceLoaded(this));
174
175 ScheduleExpiryTimer();
Bernhard Bauer 2016/03/01 11:05:04 Do we also want to expire old items, if this is ca
Marc Treib 2016/03/01 11:11:39 Haha, I just uploaded a new patch set that does th
176
167 return true; 177 return true;
168 } 178 }
169 179
170 void NTPSnippetsService::LoadFromPrefs() { 180 void NTPSnippetsService::LoadFromPrefs() {
171 // |pref_service_| can be null in tests. 181 // |pref_service_| can be null in tests.
172 if (!pref_service_) 182 if (!pref_service_)
173 return; 183 return;
174 if (!LoadFromJSONList(*pref_service_->GetList(prefs::kSnippets))) 184 if (!LoadFromJSONList(*pref_service_->GetList(prefs::kSnippets)))
175 LOG(ERROR) << "Failed to parse snippets from prefs"; 185 LOG(ERROR) << "Failed to parse snippets from prefs";
176 } 186 }
177 187
178 void NTPSnippetsService::StoreToPrefs() { 188 void NTPSnippetsService::StoreToPrefs() {
179 // |pref_service_| can be null in tests. 189 // |pref_service_| can be null in tests.
180 if (!pref_service_) 190 if (!pref_service_)
181 return; 191 return;
182 base::ListValue list; 192 base::ListValue list;
183 for (const auto& snippet : snippets_) { 193 for (const auto& snippet : snippets_) {
184 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); 194 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
185 dict->Set(kContentInfo, snippet->ToDictionary()); 195 dict->Set(kContentInfo, snippet->ToDictionary());
186 list.Append(std::move(dict)); 196 list.Append(std::move(dict));
187 } 197 }
188 pref_service_->Set(prefs::kSnippets, list); 198 pref_service_->Set(prefs::kSnippets, list);
189 } 199 }
190 200
201 void NTPSnippetsService::ScheduleExpiryTimer() {
202 if (snippets_.empty())
203 return;
204
205 base::Time next_expiry = base::Time::Max();
206 for (const auto& snippet : snippets_) {
Bernhard Bauer 2016/03/01 11:05:04 You could go all-out here as well, and use std::mi
Marc Treib 2016/03/01 11:22:45 Hm, I could, but then I'd have to define a compara
Bernhard Bauer 2016/03/01 11:54:27 Well, you already use a predicate lambda below...
Marc Treib 2016/03/01 13:16:49 The code with min_element would look like this:
Bernhard Bauer 2016/03/01 15:13:18 Eh, I don't think it's worse than what we currentl
207 if (snippet->expiry_date() < next_expiry)
208 next_expiry = snippet->expiry_date();
209 }
210 // Note: If |next_expiry| is in the past, this will run immediately.
211 expiry_timer_.Start(FROM_HERE, next_expiry - base::Time::Now(),
212 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets,
213 base::Unretained(this)));
214 }
215
216 void NTPSnippetsService::RemoveExpiredSnippets() {
217 base::Time expiry = base::Time::Now();
218 snippets_.erase(
Bernhard Bauer 2016/03/01 11:05:04 This only erases a single element, but remove_if c
Marc Treib 2016/03/01 11:11:39 Nope, this erases all expired elements. std::remov
Bernhard Bauer 2016/03/01 11:54:27 Oh, your indentation on line 223 is misleading --
Marc Treib 2016/03/01 13:16:49 Fixed the indentation.
219 std::remove_if(snippets_.begin(), snippets_.end(),
220 [&expiry](const scoped_ptr<NTPSnippet>& snippet) {
221 return snippet->expiry_date() <= expiry;
222 }),
223 snippets_.end());
224
225 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
226 NTPSnippetsServiceLoaded(this));
227
228 ScheduleExpiryTimer();
229 }
230
191 } // namespace ntp_snippets 231 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698