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

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

Issue 2000233002: [NTP Snippets] Unschedule fetches when the service should be disabled (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@SnippetsDB
Patch Set: fix typos Created 4 years, 6 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 <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 NTPSnippetsService::NTPSnippetsService( 180 NTPSnippetsService::NTPSnippetsService(
181 bool enabled, 181 bool enabled,
182 PrefService* pref_service, 182 PrefService* pref_service,
183 sync_driver::SyncService* sync_service, 183 sync_driver::SyncService* sync_service,
184 SuggestionsService* suggestions_service, 184 SuggestionsService* suggestions_service,
185 const std::string& application_language_code, 185 const std::string& application_language_code,
186 NTPSnippetsScheduler* scheduler, 186 NTPSnippetsScheduler* scheduler,
187 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, 187 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher,
188 std::unique_ptr<ImageFetcher> image_fetcher, 188 std::unique_ptr<ImageFetcher> image_fetcher,
189 std::unique_ptr<NTPSnippetsDatabase> database) 189 std::unique_ptr<NTPSnippetsDatabase> database)
190 : state_(State::INITED), 190 : state_(State::NOT_INITED),
191 enabled_(enabled), 191 explicitly_disabled_(!enabled),
192 pref_service_(pref_service), 192 pref_service_(pref_service),
193 sync_service_(sync_service), 193 sync_service_(sync_service),
194 sync_service_observer_(this), 194 sync_service_observer_(this),
195 suggestions_service_(suggestions_service), 195 suggestions_service_(suggestions_service),
196 application_language_code_(application_language_code), 196 application_language_code_(application_language_code),
197 scheduler_(scheduler), 197 scheduler_(scheduler),
198 snippets_fetcher_(std::move(snippets_fetcher)), 198 snippets_fetcher_(std::move(snippets_fetcher)),
199 image_fetcher_(std::move(image_fetcher)), 199 image_fetcher_(std::move(image_fetcher)),
200 database_(std::move(database)), 200 database_(std::move(database)),
201 fetch_after_load_(false) { 201 fetch_after_load_(false) {
202 // TODO(dgn) should be removed after branch point (https:://crbug.com/617585).
203 ClearDeprecatedPrefs();
204
205 if (explicitly_disabled_) {
206 EnterState(State::DISABLED);
207 return;
208 }
209
202 snippets_fetcher_->SetCallback(base::Bind( 210 snippets_fetcher_->SetCallback(base::Bind(
203 &NTPSnippetsService::OnFetchFinished, base::Unretained(this))); 211 &NTPSnippetsService::OnFetchFinished, base::Unretained(this)));
204 212
205 // |sync_service_| can be null in tests or if sync is disabled. 213 // |sync_service_| can be null in tests or if sync is disabled.
214 // This is a service we want to keep listening to all the time, independently
215 // from the state, since it will allow us to enable or disabled the snippets
Marc Treib 2016/06/07 08:55:39 nit: disable, no "d"
dgn 2016/06/07 11:22:46 Done.
216 // service.
206 if (sync_service_) 217 if (sync_service_)
207 sync_service_observer_.Add(sync_service_); 218 sync_service_observer_.Add(sync_service_);
208 219
209 if (enabled_) { 220 // Will trigger the transition to the READY state.
210 database_->Load(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, 221 database_->Load(base::Bind(&NTPSnippetsService::OnDatabaseLoaded,
211 base::Unretained(this))); 222 base::Unretained(this)));
212 }
213
214 RescheduleFetching();
215
216 ClearDeprecatedPrefs();
217 } 223 }
218 224
219 NTPSnippetsService::~NTPSnippetsService() { 225 NTPSnippetsService::~NTPSnippetsService() {
220 DCHECK(state_ == State::SHUT_DOWN); 226 DCHECK(state_ == State::SHUT_DOWN);
221 } 227 }
222 228
223 // static 229 // static
224 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { 230 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
225 registry->RegisterListPref(prefs::kDeprecatedSnippets); 231 registry->RegisterListPref(prefs::kDeprecatedSnippets);
226 registry->RegisterListPref(prefs::kDeprecatedDiscardedSnippets); 232 registry->RegisterListPref(prefs::kDeprecatedDiscardedSnippets);
227 registry->RegisterListPref(prefs::kSnippetHosts); 233 registry->RegisterListPref(prefs::kSnippetHosts);
228 } 234 }
229 235
236 // Inherited from KeyedService.
230 void NTPSnippetsService::Shutdown() { 237 void NTPSnippetsService::Shutdown() {
231 DCHECK(state_ == State::INITED || state_ == State::LOADED); 238 EnterState(State::SHUT_DOWN);
232 state_ = State::SHUT_DOWN;
233
234 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
235 NTPSnippetsServiceShutdown());
236 expiry_timer_.Stop();
237 suggestions_service_subscription_.reset();
238 enabled_ = false;
239 } 239 }
240 240
241 void NTPSnippetsService::FetchSnippets() { 241 void NTPSnippetsService::FetchSnippets() {
242 if (loaded()) 242 if (ready())
243 FetchSnippetsFromHosts(GetSuggestionsHosts()); 243 FetchSnippetsFromHosts(GetSuggestionsHosts());
244 else 244 else
245 fetch_after_load_ = true; 245 fetch_after_load_ = true;
246 } 246 }
247 247
248 void NTPSnippetsService::FetchSnippetsFromHosts( 248 void NTPSnippetsService::FetchSnippetsFromHosts(
249 const std::set<std::string>& hosts) { 249 const std::set<std::string>& hosts) {
250 if (!loaded()) 250 if (!ready())
251 return; 251 return;
252 snippets_fetcher_->FetchSnippetsFromHosts(hosts, application_language_code_, 252 snippets_fetcher_->FetchSnippetsFromHosts(hosts, application_language_code_,
253 kMaxSnippetCount); 253 kMaxSnippetCount);
254 } 254 }
255 255
256 void NTPSnippetsService::RescheduleFetching() { 256 void NTPSnippetsService::RescheduleFetching() {
257 // The scheduler only exists on Android so far, it's null on other platforms. 257 // The scheduler only exists on Android so far, it's null on other platforms.
258 if (!scheduler_) 258 if (!scheduler_)
259 return; 259 return;
260 260
261 if (enabled_) { 261 if (ready()) {
262 base::Time now = base::Time::Now(); 262 base::Time now = base::Time::Now();
263 scheduler_->Schedule( 263 scheduler_->Schedule(
264 GetFetchingIntervalWifiCharging(), GetFetchingIntervalWifi(now), 264 GetFetchingIntervalWifiCharging(), GetFetchingIntervalWifi(now),
265 GetFetchingIntervalFallback(), GetRescheduleTime(now)); 265 GetFetchingIntervalFallback(), GetRescheduleTime(now));
266 } else { 266 } else {
267 scheduler_->Unschedule(); 267 scheduler_->Unschedule();
268 } 268 }
269 } 269 }
270 270
271 void NTPSnippetsService::FetchSnippetImage( 271 void NTPSnippetsService::FetchSnippetImage(
(...skipping 10 matching lines...) Expand all
282 return; 282 return;
283 } 283 }
284 284
285 const NTPSnippet& snippet = *it->get(); 285 const NTPSnippet& snippet = *it->get();
286 image_fetcher_->StartOrQueueNetworkRequest( 286 image_fetcher_->StartOrQueueNetworkRequest(
287 snippet.id(), snippet.salient_image_url(), callback); 287 snippet.id(), snippet.salient_image_url(), callback);
288 // TODO(treib): Cache/persist the snippet image. 288 // TODO(treib): Cache/persist the snippet image.
289 } 289 }
290 290
291 void NTPSnippetsService::ClearSnippets() { 291 void NTPSnippetsService::ClearSnippets() {
292 if (!loaded()) 292 if (!initialized())
293 return;
294
295 if (snippets_.empty())
293 return; 296 return;
294 297
295 database_->Delete(snippets_); 298 database_->Delete(snippets_);
296 snippets_.clear(); 299 snippets_.clear();
297 300
298 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 301 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
299 NTPSnippetsServiceLoaded()); 302 NTPSnippetsServiceLoaded());
300 } 303 }
301 304
302 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const { 305 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const {
303 // |suggestions_service_| can be null in tests. 306 // |suggestions_service_| can be null in tests.
304 if (!suggestions_service_) 307 if (!suggestions_service_)
305 return std::set<std::string>(); 308 return std::set<std::string>();
306 309
307 // TODO(treib): This should just call GetSnippetHostsFromPrefs. 310 // TODO(treib): This should just call GetSnippetHostsFromPrefs.
308 return GetSuggestionsHostsImpl( 311 return GetSuggestionsHostsImpl(
309 suggestions_service_->GetSuggestionsDataFromCache()); 312 suggestions_service_->GetSuggestionsDataFromCache());
310 } 313 }
311 314
312 bool NTPSnippetsService::DiscardSnippet(const std::string& snippet_id) { 315 bool NTPSnippetsService::DiscardSnippet(const std::string& snippet_id) {
313 if (!loaded()) 316 if (!ready())
314 return false; 317 return false;
315 318
316 auto it = 319 auto it =
317 std::find_if(snippets_.begin(), snippets_.end(), 320 std::find_if(snippets_.begin(), snippets_.end(),
318 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) { 321 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) {
319 return snippet->id() == snippet_id; 322 return snippet->id() == snippet_id;
320 }); 323 });
321 if (it == snippets_.end()) 324 if (it == snippets_.end())
322 return false; 325 return false;
323 326
324 (*it)->set_discarded(true); 327 (*it)->set_discarded(true);
325 328
326 database_->Save(**it); 329 database_->Save(**it);
327 330
328 discarded_snippets_.push_back(std::move(*it)); 331 discarded_snippets_.push_back(std::move(*it));
329 snippets_.erase(it); 332 snippets_.erase(it);
330 333
331 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 334 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
332 NTPSnippetsServiceLoaded()); 335 NTPSnippetsServiceLoaded());
333 return true; 336 return true;
334 } 337 }
335 338
336 void NTPSnippetsService::ClearDiscardedSnippets() { 339 void NTPSnippetsService::ClearDiscardedSnippets() {
337 if (!loaded()) 340 if (!initialized())
338 return; 341 return;
339 342
340 database_->Delete(discarded_snippets_); 343 database_->Delete(discarded_snippets_);
341 discarded_snippets_.clear(); 344 discarded_snippets_.clear();
342
343 FetchSnippets();
344 } 345 }
345 346
346 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { 347 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) {
347 observers_.AddObserver(observer); 348 observers_.AddObserver(observer);
348 } 349 }
349 350
350 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { 351 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) {
351 observers_.RemoveObserver(observer); 352 observers_.RemoveObserver(observer);
352 } 353 }
353 354
355 DisabledReason NTPSnippetsService::GetDisabledReason() const {
356 if (explicitly_disabled_)
357 return DisabledReason::EXPLICITLY_DISABLED;
358
359 if (!sync_service_ || !sync_service_->CanSyncStart()) {
360 DVLOG(1) << "[GetDisabledReason] Sync disabled";
361 return DisabledReason::HISTORY_SYNC_DISABLED;
362 }
363
364 // !IsSyncActive in cases where CanSyncStart is true hints at the backend not
365 // being initialized.
366 // ConfigurationDone() verifies that the sync service has properly loaded its
367 // configuration and is aware of the different data types to sync.
368 if (!sync_service_->IsSyncActive() || !sync_service_->ConfigurationDone()) {
369 DVLOG(1) << "[GetDisabledReason] Sync initialization is not complete.";
370 return DisabledReason::HISTORY_SYNC_STATE_UNKNOWN;
371 }
372
373 if (!sync_service_->GetActiveDataTypes().Has(
374 syncer::HISTORY_DELETE_DIRECTIVES)) {
375 DVLOG(1) << "[GetDisabledReason] History sync disabled";
376 return DisabledReason::HISTORY_SYNC_DISABLED;
377 }
378
379 DVLOG(1) << "[GetDisabledReason] Enabled";
380 return DisabledReason::NONE;
381 }
382
354 // static 383 // static
355 int NTPSnippetsService::GetMaxSnippetCountForTesting() { 384 int NTPSnippetsService::GetMaxSnippetCountForTesting() {
356 return kMaxSnippetCount; 385 return kMaxSnippetCount;
357 } 386 }
358 387
359 //////////////////////////////////////////////////////////////////////////////// 388 ////////////////////////////////////////////////////////////////////////////////
360 // Private methods 389 // Private methods
361 390
362 // sync_driver::SyncServiceObserver implementation.
363 void NTPSnippetsService::OnStateChanged() { 391 void NTPSnippetsService::OnStateChanged() {
364 if (IsSyncStateIncompatible()) { 392 if (!initialized())
365 ClearSnippets();
366 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
367 NTPSnippetsServiceDisabled());
368 return; 393 return;
369 }
370 394
371 // TODO(dgn): When the data sources change, we may want to not fetch here, 395 DVLOG(1) << "[OnStateChanged]";
372 // as we will get notified of changes from the snippet sources as well, and 396 EnterState(GetStateForDependenciesStatus());
373 // start multiple fetches.
374 FetchSnippets();
375 } 397 }
376 398
377 void NTPSnippetsService::OnDatabaseLoaded(NTPSnippet::PtrVector snippets) { 399 void NTPSnippetsService::OnDatabaseLoaded(NTPSnippet::PtrVector snippets) {
378 DCHECK(state_ == State::INITED || state_ == State::SHUT_DOWN); 400 DCHECK(state_ == State::NOT_INITED || state_ == State::SHUT_DOWN);
379 if (state_ == State::SHUT_DOWN) 401 if (state_ == State::SHUT_DOWN)
380 return; 402 return;
381 state_ = State::LOADED;
382 403
383 DCHECK(snippets_.empty()); 404 DCHECK(snippets_.empty());
384 DCHECK(discarded_snippets_.empty()); 405 DCHECK(discarded_snippets_.empty());
385 for (std::unique_ptr<NTPSnippet>& snippet : snippets) { 406 for (std::unique_ptr<NTPSnippet>& snippet : snippets) {
386 if (snippet->is_discarded()) 407 if (snippet->is_discarded())
387 discarded_snippets_.emplace_back(std::move(snippet)); 408 discarded_snippets_.emplace_back(std::move(snippet));
388 else 409 else
389 snippets_.emplace_back(std::move(snippet)); 410 snippets_.emplace_back(std::move(snippet));
390 } 411 }
391 std::sort(snippets_.begin(), snippets_.end(), 412 std::sort(snippets_.begin(), snippets_.end(),
392 [](const std::unique_ptr<NTPSnippet>& lhs, 413 [](const std::unique_ptr<NTPSnippet>& lhs,
393 const std::unique_ptr<NTPSnippet>& rhs) { 414 const std::unique_ptr<NTPSnippet>& rhs) {
394 return lhs->score() > rhs->score(); 415 return lhs->score() > rhs->score();
395 }); 416 });
417
418 // Change state after we started loading the snippets. During startup, the
419 // Sync service might not be completely loaded when we initialize this
420 // service. Whether we had snippets in the last season is used to guess if
421 // we should enable the service or not. See |GetStateForDependenciesStatus|.
422 EnterState(GetStateForDependenciesStatus());
423
396 LoadingSnippetsFinished(); 424 LoadingSnippetsFinished();
397 425
398 // If host restrictions are enabled, register for host list updates.
399 // |suggestions_service_| can be null in tests.
400 if (snippets_fetcher_->UsesHostRestrictions() && suggestions_service_) {
401 suggestions_service_subscription_ =
402 suggestions_service_->AddCallback(base::Bind(
403 &NTPSnippetsService::OnSuggestionsChanged, base::Unretained(this)));
404 }
405
406 // Start a fetch if we don't have any snippets yet, or a fetch was requested 426 // Start a fetch if we don't have any snippets yet, or a fetch was requested
407 // earlier. 427 // earlier.
408 if (snippets_.empty() || fetch_after_load_) { 428 if (snippets_.empty() || fetch_after_load_) {
409 fetch_after_load_ = false; 429 fetch_after_load_ = false;
410 FetchSnippets(); 430 FetchSnippets();
411 } 431 }
412 } 432 }
413 433
414 void NTPSnippetsService::OnSuggestionsChanged( 434 void NTPSnippetsService::OnSuggestionsChanged(
415 const SuggestionsProfile& suggestions) { 435 const SuggestionsProfile& suggestions) {
416 DCHECK(loaded()); 436 DCHECK(initialized());
417 437
418 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions); 438 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions);
419 if (hosts == GetSnippetHostsFromPrefs()) 439 if (hosts == GetSnippetHostsFromPrefs())
420 return; 440 return;
421 441
422 // Remove existing snippets that aren't in the suggestions anymore. 442 // Remove existing snippets that aren't in the suggestions anymore.
423 // TODO(treib,maybelle): If there is another source with an allowed host, 443 // TODO(treib,maybelle): If there is another source with an allowed host,
424 // then we should fall back to that. 444 // then we should fall back to that.
425 // First, move them over into |to_delete|. 445 // First, move them over into |to_delete|.
426 NTPSnippet::PtrVector to_delete; 446 NTPSnippet::PtrVector to_delete;
427 for (std::unique_ptr<NTPSnippet>& snippet : snippets_) { 447 for (std::unique_ptr<NTPSnippet>& snippet : snippets_) {
428 if (!hosts.count(snippet->best_source().url.host())) 448 if (!hosts.count(snippet->best_source().url.host()))
429 to_delete.emplace_back(std::move(snippet)); 449 to_delete.emplace_back(std::move(snippet));
430 } 450 }
431 Compact(&snippets_); 451 Compact(&snippets_);
432 // Then delete the removed snippets from the database. 452 // Then delete the removed snippets from the database.
433 database_->Delete(to_delete); 453 database_->Delete(to_delete);
434 454
435 StoreSnippetHostsToPrefs(hosts); 455 StoreSnippetHostsToPrefs(hosts);
436 456
437 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 457 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
438 NTPSnippetsServiceLoaded()); 458 NTPSnippetsServiceLoaded());
439 459
440 FetchSnippetsFromHosts(hosts); 460 FetchSnippetsFromHosts(hosts);
441 } 461 }
442 462
443 void NTPSnippetsService::OnFetchFinished( 463 void NTPSnippetsService::OnFetchFinished(
444 NTPSnippetsFetcher::OptionalSnippets snippets) { 464 NTPSnippetsFetcher::OptionalSnippets snippets) {
445 if (!loaded()) 465 if (!ready())
446 return; 466 return;
447 467
448 if (snippets) { 468 if (snippets) {
449 // Sparse histogram used because the number of snippets is small (bound by 469 // Sparse histogram used because the number of snippets is small (bound by
450 // kMaxSnippetCount). 470 // kMaxSnippetCount).
451 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); 471 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount));
452 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", 472 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched",
453 snippets->size()); 473 snippets->size());
454 MergeSnippets(std::move(*snippets)); 474 MergeSnippets(std::move(*snippets));
455 } 475 }
456 LoadingSnippetsFinished(); 476 LoadingSnippetsFinished();
457 } 477 }
458 478
459 void NTPSnippetsService::MergeSnippets(NTPSnippet::PtrVector new_snippets) { 479 void NTPSnippetsService::MergeSnippets(NTPSnippet::PtrVector new_snippets) {
460 DCHECK(loaded()); 480 DCHECK(ready());
461 481
462 // Remove new snippets that we already have, or that have been discarded. 482 // Remove new snippets that we already have, or that have been discarded.
463 std::set<std::string> old_snippet_ids; 483 std::set<std::string> old_snippet_ids;
464 InsertAllIDs(discarded_snippets_, &old_snippet_ids); 484 InsertAllIDs(discarded_snippets_, &old_snippet_ids);
465 InsertAllIDs(snippets_, &old_snippet_ids); 485 InsertAllIDs(snippets_, &old_snippet_ids);
466 new_snippets.erase( 486 new_snippets.erase(
467 std::remove_if( 487 std::remove_if(
468 new_snippets.begin(), new_snippets.end(), 488 new_snippets.begin(), new_snippets.end(),
469 [&old_snippet_ids](const std::unique_ptr<NTPSnippet>& snippet) { 489 [&old_snippet_ids](const std::unique_ptr<NTPSnippet>& snippet) {
470 if (old_snippet_ids.count(snippet->id())) 490 if (old_snippet_ids.count(snippet->id()))
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 553
534 void NTPSnippetsService::StoreSnippetHostsToPrefs( 554 void NTPSnippetsService::StoreSnippetHostsToPrefs(
535 const std::set<std::string>& hosts) { 555 const std::set<std::string>& hosts) {
536 base::ListValue list; 556 base::ListValue list;
537 for (const std::string& host : hosts) 557 for (const std::string& host : hosts)
538 list.AppendString(host); 558 list.AppendString(host);
539 pref_service_->Set(prefs::kSnippetHosts, list); 559 pref_service_->Set(prefs::kSnippetHosts, list);
540 } 560 }
541 561
542 void NTPSnippetsService::LoadingSnippetsFinished() { 562 void NTPSnippetsService::LoadingSnippetsFinished() {
543 DCHECK(loaded()); 563 DCHECK(ready());
544 564
545 // Remove expired snippets. 565 // Remove expired snippets.
546 base::Time expiry = base::Time::Now(); 566 base::Time expiry = base::Time::Now();
547 567
548 // Move expired snippets over into |to_delete|. 568 // Move expired snippets over into |to_delete|.
549 NTPSnippet::PtrVector to_delete; 569 NTPSnippet::PtrVector to_delete;
550 for (std::unique_ptr<NTPSnippet>& snippet : snippets_) { 570 for (std::unique_ptr<NTPSnippet>& snippet : snippets_) {
551 if (snippet->expiry_date() <= expiry) 571 if (snippet->expiry_date() <= expiry)
552 to_delete.emplace_back(std::move(snippet)); 572 to_delete.emplace_back(std::move(snippet));
553 } 573 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 for (const auto& snippet : discarded_snippets_) { 615 for (const auto& snippet : discarded_snippets_) {
596 if (snippet->expiry_date() < next_expiry) 616 if (snippet->expiry_date() < next_expiry)
597 next_expiry = snippet->expiry_date(); 617 next_expiry = snippet->expiry_date();
598 } 618 }
599 DCHECK_GT(next_expiry, expiry); 619 DCHECK_GT(next_expiry, expiry);
600 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, 620 expiry_timer_.Start(FROM_HERE, next_expiry - expiry,
601 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, 621 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished,
602 base::Unretained(this))); 622 base::Unretained(this)));
603 } 623 }
604 624
605 bool NTPSnippetsService::IsSyncStateIncompatible() { 625 void NTPSnippetsService::EnterStateEnabled(bool fetch_snippets) {
606 if (!sync_service_ || !sync_service_->CanSyncStart()) 626 if (fetch_snippets)
607 return true; 627 FetchSnippets();
608 if (!sync_service_->IsSyncActive() || !sync_service_->ConfigurationDone()) 628
609 return false; // Sync service is not initialized, yet not disabled. 629 // If host restrictions are enabled, register for host list updates.
610 return !sync_service_->GetActiveDataTypes().Has( 630 // |suggestions_service_| can be null in tests.
611 syncer::HISTORY_DELETE_DIRECTIVES); 631 if (snippets_fetcher_->UsesHostRestrictions() && suggestions_service_) {
632 suggestions_service_subscription_ =
633 suggestions_service_->AddCallback(base::Bind(
634 &NTPSnippetsService::OnSuggestionsChanged, base::Unretained(this)));
635 }
636
637 RescheduleFetching();
638 }
639
640 void NTPSnippetsService::EnterStateDisabled() {
641 ClearSnippets();
642 ClearDiscardedSnippets();
643
644 suggestions_service_subscription_.reset();
645 expiry_timer_.Stop();
646
647 RescheduleFetching();
648 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
649 NTPSnippetsServiceDisabled());
650 }
651
652 void NTPSnippetsService::EnterStateShutdown() {
653 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
654 NTPSnippetsServiceShutdown());
655
656 expiry_timer_.Stop();
657 suggestions_service_subscription_.reset();
658
659 if (sync_service_)
660 sync_service_observer_.Remove(sync_service_);
661 }
662
663 NTPSnippetsService::State NTPSnippetsService::GetStateForDependenciesStatus() {
664 switch (GetDisabledReason()) {
665 case DisabledReason::NONE:
666 return State::READY;
667
668 case DisabledReason::HISTORY_SYNC_STATE_UNKNOWN:
669 // HistorySync is not initialized yet, so we don't know what the actual
670 // state is. However, because we retreived the previous snippets from the
671 // database, if we got something, we know that the service was previously
672 // enabled, so we just restore that state. If things changed,
673 // |OnStateChanged| will call this function again to fix the state.
674 DVLOG(1) << "Sync configuration incomplete, continuing based on the "
675 "current state.";
676 return snippets_.empty() ? State::DISABLED : State::READY;
677
678 default:
Marc Treib 2016/06/07 08:55:39 Can we list the remaining cases explicitly here? T
dgn 2016/06/07 11:22:46 Done.
Marc Treib 2016/06/07 11:50:37 You'll have to add a "dummy return" after the swit
dgn 2016/06/07 13:15:00 Doh. And I thought using clang locally would help
679 return State::DISABLED;
680 }
681 }
682
683 void NTPSnippetsService::EnterState(State state) {
684 switch (state) {
685 case State::NOT_INITED:
686 // Initial state, it should not be possible to get back there.
687 NOTREACHED();
688 return;
689
690 case State::READY: {
691 DCHECK(state_ == State::NOT_INITED || state_ == State::READY ||
692 state_ == State::DISABLED);
693 if (state_ == State::READY)
694 return;
695
696 // During the initialization sequence we will fetch snippets when
697 // necessary. So we skip it during the state transition.
698 bool fetch_snippets = state_ != State::NOT_INITED;
Marc Treib 2016/06/07 08:55:39 ...so this will only be true when we're going from
dgn 2016/06/07 11:22:46 The snippet DB would be cleared in that case (we d
Marc Treib 2016/06/07 11:50:37 Ah, right - I missed that we're clearing the DB in
dgn 2016/06/07 13:15:00 Done.
699 DVLOG(1) << "Entering state: READY";
700 state_ = State::READY;
701 EnterStateEnabled(fetch_snippets);
702 return;
703 }
704
705 case State::DISABLED:
706 DCHECK(state_ == State::NOT_INITED || state_ == State::READY ||
707 state_ == State::DISABLED);
708 if (state_ == State::DISABLED)
709 return;
710
711 DVLOG(1) << "Entering state: DISABLED";
712 state_ = State::DISABLED;
713 EnterStateDisabled();
714 return;
715
716 case State::SHUT_DOWN:
717 DCHECK(state_ == State::NOT_INITED || state_ == State::DISABLED ||
718 state_ == State::READY);
719 DVLOG(1) << "Entering state: SHUT_DOWN";
720 state_ = State::SHUT_DOWN;
721 EnterStateShutdown();
722 return;
723 }
612 } 724 }
613 725
614 void NTPSnippetsService::ClearDeprecatedPrefs() { 726 void NTPSnippetsService::ClearDeprecatedPrefs() {
615 pref_service_->ClearPref(prefs::kDeprecatedSnippets); 727 pref_service_->ClearPref(prefs::kDeprecatedSnippets);
616 pref_service_->ClearPref(prefs::kDeprecatedDiscardedSnippets); 728 pref_service_->ClearPref(prefs::kDeprecatedDiscardedSnippets);
617 } 729 }
618 730
619 } // namespace ntp_snippets 731 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698