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

Side by Side Diff: components/ntp_snippets/ntp_snippets_service.cc

Issue 1869023002: [NTP Snippets] SnippetsBridge: register as SnippetsObserver only when we get a Java observer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@snippets_service_cleanup
Patch Set: rebase Created 4 years, 8 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/command_line.h" 7 #include "base/command_line.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 scheduler_->Schedule(GetFetchingIntervalWifiCharging(), 166 scheduler_->Schedule(GetFetchingIntervalWifiCharging(),
167 GetFetchingIntervalWifi(), 167 GetFetchingIntervalWifi(),
168 GetFetchingIntervalFallback()); 168 GetFetchingIntervalFallback());
169 } else { 169 } else {
170 scheduler_->Unschedule(); 170 scheduler_->Unschedule();
171 } 171 }
172 } 172 }
173 173
174 void NTPSnippetsService::Shutdown() { 174 void NTPSnippetsService::Shutdown() {
175 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 175 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
176 NTPSnippetsServiceShutdown(this)); 176 NTPSnippetsServiceShutdown());
177 } 177 }
178 178
179 void NTPSnippetsService::FetchSnippets() { 179 void NTPSnippetsService::FetchSnippets() {
180 // |suggestions_service_| can be null in tests. 180 // |suggestions_service_| can be null in tests.
181 if (!suggestions_service_) 181 if (!suggestions_service_)
182 return; 182 return;
183 183
184 FetchSnippetsImpl(GetSuggestionsHosts( 184 FetchSnippetsImpl(GetSuggestionsHosts(
185 suggestions_service_->GetSuggestionsDataFromCache())); 185 suggestions_service_->GetSuggestionsDataFromCache()));
186 } 186 }
187 187
188 bool NTPSnippetsService::DiscardSnippet(const GURL& url) { 188 bool NTPSnippetsService::DiscardSnippet(const GURL& url) {
189 auto it = std::find_if(snippets_.begin(), snippets_.end(), 189 auto it = std::find_if(snippets_.begin(), snippets_.end(),
190 [&url](const scoped_ptr<NTPSnippet>& snippet) { 190 [&url](const scoped_ptr<NTPSnippet>& snippet) {
191 return snippet->url() == url; 191 return snippet->url() == url;
192 }); 192 });
193 if (it == snippets_.end()) 193 if (it == snippets_.end())
194 return false; 194 return false;
195 discarded_snippets_.push_back(std::move(*it)); 195 discarded_snippets_.push_back(std::move(*it));
196 snippets_.erase(it); 196 snippets_.erase(it);
197 StoreDiscardedSnippetsToPrefs(); 197 StoreDiscardedSnippetsToPrefs();
198 StoreSnippetsToPrefs(); 198 StoreSnippetsToPrefs();
199 return true; 199 return true;
200 } 200 }
201 201
202 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { 202 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) {
203 observers_.AddObserver(observer); 203 observers_.AddObserver(observer);
204 observer->NTPSnippetsServiceLoaded(this); 204 observer->NTPSnippetsServiceLoaded();
205 } 205 }
206 206
207 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { 207 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) {
208 observers_.RemoveObserver(observer); 208 observers_.RemoveObserver(observer);
209 } 209 }
210 210
211 void NTPSnippetsService::OnSuggestionsChanged( 211 void NTPSnippetsService::OnSuggestionsChanged(
212 const SuggestionsProfile& suggestions) { 212 const SuggestionsProfile& suggestions) {
213 std::vector<std::string> hosts = GetSuggestionsHosts(suggestions); 213 std::vector<std::string> hosts = GetSuggestionsHosts(suggestions);
214 214
215 // Remove existing snippets that aren't in the suggestions anymore. 215 // Remove existing snippets that aren't in the suggestions anymore.
216 snippets_.erase( 216 snippets_.erase(
217 std::remove_if(snippets_.begin(), snippets_.end(), 217 std::remove_if(snippets_.begin(), snippets_.end(),
218 [&hosts](const scoped_ptr<NTPSnippet>& snippet) { 218 [&hosts](const scoped_ptr<NTPSnippet>& snippet) {
219 return std::find(hosts.begin(), hosts.end(), 219 return std::find(hosts.begin(), hosts.end(),
220 snippet->url().host()) == hosts.end(); 220 snippet->url().host()) == hosts.end();
221 }), 221 }),
222 snippets_.end()); 222 snippets_.end());
223 223
224 StoreSnippetsToPrefs(); 224 StoreSnippetsToPrefs();
225 225
226 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 226 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
227 NTPSnippetsServiceLoaded(this)); 227 NTPSnippetsServiceLoaded());
228 228
229 FetchSnippetsImpl(hosts); 229 FetchSnippetsImpl(hosts);
230 } 230 }
231 231
232 void NTPSnippetsService::OnSnippetsDownloaded( 232 void NTPSnippetsService::OnSnippetsDownloaded(
233 const std::string& snippets_json) { 233 const std::string& snippets_json) {
234 parse_json_callback_.Run( 234 parse_json_callback_.Run(
235 snippets_json, base::Bind(&NTPSnippetsService::OnJsonParsed, 235 snippets_json, base::Bind(&NTPSnippetsService::OnJsonParsed,
236 weak_ptr_factory_.GetWeakPtr(), snippets_json), 236 weak_ptr_factory_.GetWeakPtr(), snippets_json),
237 base::Bind(&NTPSnippetsService::OnJsonError, 237 base::Bind(&NTPSnippetsService::OnJsonError,
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 353
354 discarded_snippets_.erase( 354 discarded_snippets_.erase(
355 std::remove_if(discarded_snippets_.begin(), discarded_snippets_.end(), 355 std::remove_if(discarded_snippets_.begin(), discarded_snippets_.end(),
356 [&expiry](const scoped_ptr<NTPSnippet>& snippet) { 356 [&expiry](const scoped_ptr<NTPSnippet>& snippet) {
357 return snippet->expiry_date() <= expiry; 357 return snippet->expiry_date() <= expiry;
358 }), 358 }),
359 discarded_snippets_.end()); 359 discarded_snippets_.end());
360 StoreDiscardedSnippetsToPrefs(); 360 StoreDiscardedSnippetsToPrefs();
361 361
362 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 362 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
363 NTPSnippetsServiceLoaded(this)); 363 NTPSnippetsServiceLoaded());
364 364
365 // If there are any snippets left, schedule a timer for the next expiry. 365 // If there are any snippets left, schedule a timer for the next expiry.
366 if (snippets_.empty() && discarded_snippets_.empty()) 366 if (snippets_.empty() && discarded_snippets_.empty())
367 return; 367 return;
368 368
369 base::Time next_expiry = base::Time::Max(); 369 base::Time next_expiry = base::Time::Max();
370 for (const auto& snippet : snippets_) { 370 for (const auto& snippet : snippets_) {
371 if (snippet->expiry_date() < next_expiry) 371 if (snippet->expiry_date() < next_expiry)
372 next_expiry = snippet->expiry_date(); 372 next_expiry = snippet->expiry_date();
373 } 373 }
374 for (const auto& snippet : discarded_snippets_) { 374 for (const auto& snippet : discarded_snippets_) {
375 if (snippet->expiry_date() < next_expiry) 375 if (snippet->expiry_date() < next_expiry)
376 next_expiry = snippet->expiry_date(); 376 next_expiry = snippet->expiry_date();
377 } 377 }
378 DCHECK_GT(next_expiry, expiry); 378 DCHECK_GT(next_expiry, expiry);
379 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, 379 expiry_timer_.Start(FROM_HERE, next_expiry - expiry,
380 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets, 380 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets,
381 base::Unretained(this))); 381 base::Unretained(this)));
382 } 382 }
383 383
384 } // namespace ntp_snippets 384 } // 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