OLD | NEW |
---|---|
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 Loading... | |
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::ScheduleExpiryTimer() { | |
203 if (snippets_.empty()) | |
204 return; | |
205 | |
206 base::Time next_expiry = base::Time::Max(); | |
207 for (const auto& snippet : snippets_) { | |
208 if (snippet->expiry_date() < next_expiry) | |
209 next_expiry = snippet->expiry_date(); | |
210 } | |
211 // 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.
| |
212 expiry_timer_.Start(FROM_HERE, next_expiry - base::Time::Now(), | |
213 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets, | |
214 base::Unretained(this))); | |
215 } | |
216 | |
217 void NTPSnippetsService::RemoveExpiredSnippets() { | |
218 base::Time expiry = base::Time::Now(); | |
219 snippets_.erase( | |
220 std::remove_if(snippets_.begin(), snippets_.end(), | |
221 [&expiry](const scoped_ptr<NTPSnippet>& snippet) { | |
222 return snippet->expiry_date() <= expiry; | |
223 }), | |
224 snippets_.end()); | |
225 | |
226 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | |
227 NTPSnippetsServiceLoaded(this)); | |
228 | |
229 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.
| |
230 } | |
231 | |
191 } // namespace ntp_snippets | 232 } // namespace ntp_snippets |
OLD | NEW |