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

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: Rebase and address comments 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 that be removed after branch point?
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 the snippets service.
206 if (sync_service_) 216 if (sync_service_)
207 sync_service_observer_.Add(sync_service_); 217 sync_service_observer_.Add(sync_service_);
208 218
209 if (enabled_) { 219 // Will trigger the transition to the READY state.
210 database_->Load(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, 220 database_->Load(base::Bind(&NTPSnippetsService::OnDatabaseLoaded,
211 base::Unretained(this))); 221 base::Unretained(this)));
212 }
213 222
214 RescheduleFetching(); 223 // Significant change: we now call Reschedule fetching during the
dgn 2016/06/03 19:42:22 Significant change: we now call Reschedule fetchin
Marc Treib 2016/06/06 08:38:32 I think this is fine.
dgn 2016/06/03 19:42:22 Was supposed to be a review comment. To be removed
Marc Treib 2016/06/06 08:38:32 Acknowledged.
215 224 // transition to READY, rather than in the constructor. When the service
216 ClearDeprecatedPrefs(); 225 // is disabled, it happens during the transition to disable, synchronously
226 // from the constructor, so changes are minimal there. However here, it
227 // would happen only once the DB is done loading. Worth modifying?
217 } 228 }
218 229
219 NTPSnippetsService::~NTPSnippetsService() { 230 NTPSnippetsService::~NTPSnippetsService() {
220 DCHECK(state_ == State::SHUT_DOWN); 231 DCHECK(state_ == State::SHUT_DOWN);
221 } 232 }
222 233
223 // static 234 // static
224 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { 235 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
225 registry->RegisterListPref(prefs::kDeprecatedSnippets); 236 registry->RegisterListPref(prefs::kDeprecatedSnippets);
226 registry->RegisterListPref(prefs::kDeprecatedDiscardedSnippets); 237 registry->RegisterListPref(prefs::kDeprecatedDiscardedSnippets);
227 registry->RegisterListPref(prefs::kSnippetHosts); 238 registry->RegisterListPref(prefs::kSnippetHosts);
228 } 239 }
229 240
241 // Inherited from KeyedService.
230 void NTPSnippetsService::Shutdown() { 242 void NTPSnippetsService::Shutdown() {
231 DCHECK(state_ == State::INITED || state_ == State::LOADED); 243 EnterState(State::SHUT_DOWN);
232 state_ = State::SHUT_DOWN;
233 244
234 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 245 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
235 NTPSnippetsServiceShutdown()); 246 NTPSnippetsServiceShutdown());
236 expiry_timer_.Stop(); 247 expiry_timer_.Stop();
237 suggestions_service_subscription_.reset(); 248 suggestions_service_subscription_.reset();
238 enabled_ = false;
239 } 249 }
240 250
241 void NTPSnippetsService::FetchSnippets() { 251 void NTPSnippetsService::FetchSnippets() {
242 if (loaded()) 252 if (ready())
243 FetchSnippetsFromHosts(GetSuggestionsHosts()); 253 FetchSnippetsFromHosts(GetSuggestionsHosts());
244 else 254 else
245 fetch_after_load_ = true; 255 fetch_after_load_ = true;
246 } 256 }
247 257
248 void NTPSnippetsService::FetchSnippetsFromHosts( 258 void NTPSnippetsService::FetchSnippetsFromHosts(
249 const std::set<std::string>& hosts) { 259 const std::set<std::string>& hosts) {
250 if (!loaded()) 260 if (!ready())
251 return; 261 return;
252 snippets_fetcher_->FetchSnippetsFromHosts(hosts, application_language_code_, 262 snippets_fetcher_->FetchSnippetsFromHosts(hosts, application_language_code_,
253 kMaxSnippetCount); 263 kMaxSnippetCount);
254 } 264 }
255 265
256 void NTPSnippetsService::RescheduleFetching() { 266 void NTPSnippetsService::RescheduleFetching() {
257 // The scheduler only exists on Android so far, it's null on other platforms. 267 // The scheduler only exists on Android so far, it's null on other platforms.
258 if (!scheduler_) 268 if (!scheduler_)
259 return; 269 return;
260 270
261 if (enabled_) { 271 if (ready()) {
262 base::Time now = base::Time::Now(); 272 base::Time now = base::Time::Now();
263 scheduler_->Schedule( 273 scheduler_->Schedule(
264 GetFetchingIntervalWifiCharging(), GetFetchingIntervalWifi(now), 274 GetFetchingIntervalWifiCharging(), GetFetchingIntervalWifi(now),
265 GetFetchingIntervalFallback(), GetRescheduleTime(now)); 275 GetFetchingIntervalFallback(), GetRescheduleTime(now));
266 } else { 276 } else {
267 scheduler_->Unschedule(); 277 scheduler_->Unschedule();
268 } 278 }
269 } 279 }
270 280
271 void NTPSnippetsService::FetchSnippetImage( 281 void NTPSnippetsService::FetchSnippetImage(
(...skipping 10 matching lines...) Expand all
282 return; 292 return;
283 } 293 }
284 294
285 const NTPSnippet& snippet = *it->get(); 295 const NTPSnippet& snippet = *it->get();
286 image_fetcher_->StartOrQueueNetworkRequest( 296 image_fetcher_->StartOrQueueNetworkRequest(
287 snippet.id(), snippet.salient_image_url(), callback); 297 snippet.id(), snippet.salient_image_url(), callback);
288 // TODO(treib): Cache/persist the snippet image. 298 // TODO(treib): Cache/persist the snippet image.
289 } 299 }
290 300
291 void NTPSnippetsService::ClearSnippets() { 301 void NTPSnippetsService::ClearSnippets() {
292 if (!loaded()) 302 if (!initialized())
303 return;
304
305 if (snippets_.empty())
293 return; 306 return;
294 307
295 database_->Delete(snippets_); 308 database_->Delete(snippets_);
296 snippets_.clear(); 309 snippets_.clear();
297 310
298 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 311 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
299 NTPSnippetsServiceLoaded()); 312 NTPSnippetsServiceLoaded());
300 } 313 }
301 314
302 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const { 315 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const {
303 // |suggestions_service_| can be null in tests. 316 // |suggestions_service_| can be null in tests.
304 if (!suggestions_service_) 317 if (!suggestions_service_)
305 return std::set<std::string>(); 318 return std::set<std::string>();
306 319
307 // TODO(treib): This should just call GetSnippetHostsFromPrefs. 320 // TODO(treib): This should just call GetSnippetHostsFromPrefs.
308 return GetSuggestionsHostsImpl( 321 return GetSuggestionsHostsImpl(
309 suggestions_service_->GetSuggestionsDataFromCache()); 322 suggestions_service_->GetSuggestionsDataFromCache());
310 } 323 }
311 324
312 bool NTPSnippetsService::DiscardSnippet(const std::string& snippet_id) { 325 bool NTPSnippetsService::DiscardSnippet(const std::string& snippet_id) {
313 if (!loaded()) 326 if (!ready())
314 return false; 327 return false;
315 328
316 auto it = 329 auto it =
317 std::find_if(snippets_.begin(), snippets_.end(), 330 std::find_if(snippets_.begin(), snippets_.end(),
318 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) { 331 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) {
319 return snippet->id() == snippet_id; 332 return snippet->id() == snippet_id;
320 }); 333 });
321 if (it == snippets_.end()) 334 if (it == snippets_.end())
322 return false; 335 return false;
323 336
324 (*it)->set_discarded(true); 337 (*it)->set_discarded(true);
325 338
326 database_->Save(**it); 339 database_->Save(**it);
327 340
328 discarded_snippets_.push_back(std::move(*it)); 341 discarded_snippets_.push_back(std::move(*it));
329 snippets_.erase(it); 342 snippets_.erase(it);
330 343
331 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 344 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
332 NTPSnippetsServiceLoaded()); 345 NTPSnippetsServiceLoaded());
333 return true; 346 return true;
334 } 347 }
335 348
336 void NTPSnippetsService::ClearDiscardedSnippets() { 349 void NTPSnippetsService::ClearDiscardedSnippets() {
337 if (!loaded()) 350 if (!initialized())
338 return; 351 return;
339 352
340 database_->Delete(discarded_snippets_); 353 database_->Delete(discarded_snippets_);
341 discarded_snippets_.clear(); 354 discarded_snippets_.clear();
342 355
343 FetchSnippets(); 356 FetchSnippets();
344 } 357 }
345 358
346 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { 359 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) {
347 observers_.AddObserver(observer); 360 observers_.AddObserver(observer);
348 } 361 }
349 362
350 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { 363 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) {
351 observers_.RemoveObserver(observer); 364 observers_.RemoveObserver(observer);
352 } 365 }
353 366
367 DisabledReason NTPSnippetsService::GetDisabledReason() {
368 if (explicitly_disabled_)
369 return DisabledReason::EXPLICITLY_DISABLED;
370
371 if (!sync_service_ || !sync_service_->CanSyncStart()) {
372 DVLOG(1) << "[GetDisabledReason] Sync disabled";
373 return DisabledReason::HISTORY_SYNC_DISABLED;
374 }
375
376 // !IsSyncActive in cases where CanSyncStart is true hints at the backend not
377 // being initialized.
378 // ConfigurationDone() verifies that the sync service has properly loaded its
379 // configuration and is aware of the different components to sync.
380 if (!sync_service_->IsSyncActive() || !sync_service_->ConfigurationDone()) {
381 DVLOG(1) << "[GetDisabledReason] Sync initialization is not complete.";
382 return DisabledReason::HISTORY_SYNC_STATE_UNKNOWN;
383 }
384
385 if (!sync_service_->GetActiveDataTypes().Has(
386 syncer::HISTORY_DELETE_DIRECTIVES)) {
387 DVLOG(1) << "[GetDisabledReason] History sync disabled";
388 return DisabledReason::HISTORY_SYNC_DISABLED;
389 }
390
391 DVLOG(1) << "[GetDisabledReason] Enabled";
392 return DisabledReason::NONE;
393 }
394
354 // static 395 // static
355 int NTPSnippetsService::GetMaxSnippetCountForTesting() { 396 int NTPSnippetsService::GetMaxSnippetCountForTesting() {
356 return kMaxSnippetCount; 397 return kMaxSnippetCount;
357 } 398 }
358 399
359 //////////////////////////////////////////////////////////////////////////////// 400 ////////////////////////////////////////////////////////////////////////////////
360 // Private methods 401 // Private methods
361 402
362 // sync_driver::SyncServiceObserver implementation.
363 void NTPSnippetsService::OnStateChanged() { 403 void NTPSnippetsService::OnStateChanged() {
364 if (IsSyncStateIncompatible()) { 404 if (!initialized())
365 ClearSnippets();
366 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
367 NTPSnippetsServiceDisabled());
368 return; 405 return;
369 }
370 406
371 // TODO(dgn): When the data sources change, we may want to not fetch here, 407 DVLOG(1) << "[OnStateChanged]";
372 // as we will get notified of changes from the snippet sources as well, and 408 EnterState(GetStateForDependenciesStatus());
373 // start multiple fetches.
374 FetchSnippets();
375 } 409 }
376 410
377 void NTPSnippetsService::OnDatabaseLoaded(NTPSnippet::PtrVector snippets) { 411 void NTPSnippetsService::OnDatabaseLoaded(NTPSnippet::PtrVector snippets) {
378 DCHECK(state_ == State::INITED || state_ == State::SHUT_DOWN);
379 if (state_ == State::SHUT_DOWN) 412 if (state_ == State::SHUT_DOWN)
380 return; 413 return;
381 state_ = State::LOADED;
382 414
383 DCHECK(snippets_.empty()); 415 DCHECK(snippets_.empty());
384 DCHECK(discarded_snippets_.empty()); 416 DCHECK(discarded_snippets_.empty());
385 for (std::unique_ptr<NTPSnippet>& snippet : snippets) { 417 for (std::unique_ptr<NTPSnippet>& snippet : snippets) {
386 if (snippet->is_discarded()) 418 if (snippet->is_discarded())
387 discarded_snippets_.emplace_back(std::move(snippet)); 419 discarded_snippets_.emplace_back(std::move(snippet));
388 else 420 else
389 snippets_.emplace_back(std::move(snippet)); 421 snippets_.emplace_back(std::move(snippet));
390 } 422 }
391 std::sort(snippets_.begin(), snippets_.end(), 423 std::sort(snippets_.begin(), snippets_.end(),
392 [](const std::unique_ptr<NTPSnippet>& lhs, 424 [](const std::unique_ptr<NTPSnippet>& lhs,
393 const std::unique_ptr<NTPSnippet>& rhs) { 425 const std::unique_ptr<NTPSnippet>& rhs) {
394 return lhs->score() > rhs->score(); 426 return lhs->score() > rhs->score();
395 }); 427 });
396 LoadingSnippetsFinished();
397 428
398 // If host restrictions are enabled, register for host list updates. 429 EnterState(GetStateForDependenciesStatus());
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
407 // earlier.
408 if (snippets_.empty() || fetch_after_load_) {
409 fetch_after_load_ = false;
410 FetchSnippets();
411 }
412 } 430 }
413 431
414 void NTPSnippetsService::OnSuggestionsChanged( 432 void NTPSnippetsService::OnSuggestionsChanged(
415 const SuggestionsProfile& suggestions) { 433 const SuggestionsProfile& suggestions) {
416 DCHECK(loaded()); 434 DCHECK(initialized());
417 435
418 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions); 436 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions);
419 if (hosts == GetSnippetHostsFromPrefs()) 437 if (hosts == GetSnippetHostsFromPrefs())
420 return; 438 return;
421 439
422 // Remove existing snippets that aren't in the suggestions anymore. 440 // Remove existing snippets that aren't in the suggestions anymore.
423 // TODO(treib,maybelle): If there is another source with an allowed host, 441 // TODO(treib,maybelle): If there is another source with an allowed host,
424 // then we should fall back to that. 442 // then we should fall back to that.
425 // First, move them over into |to_delete|. 443 // First, move them over into |to_delete|.
426 NTPSnippet::PtrVector to_delete; 444 NTPSnippet::PtrVector to_delete;
427 for (std::unique_ptr<NTPSnippet>& snippet : snippets_) { 445 for (std::unique_ptr<NTPSnippet>& snippet : snippets_) {
428 if (!hosts.count(snippet->best_source().url.host())) 446 if (!hosts.count(snippet->best_source().url.host()))
429 to_delete.emplace_back(std::move(snippet)); 447 to_delete.emplace_back(std::move(snippet));
430 } 448 }
431 Compact(&snippets_); 449 Compact(&snippets_);
432 // Then delete the removed snippets from the database. 450 // Then delete the removed snippets from the database.
433 database_->Delete(to_delete); 451 database_->Delete(to_delete);
434 452
435 StoreSnippetHostsToPrefs(hosts); 453 StoreSnippetHostsToPrefs(hosts);
436 454
437 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 455 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
438 NTPSnippetsServiceLoaded()); 456 NTPSnippetsServiceLoaded());
439 457
440 FetchSnippetsFromHosts(hosts); 458 FetchSnippetsFromHosts(hosts);
441 } 459 }
442 460
443 void NTPSnippetsService::OnFetchFinished( 461 void NTPSnippetsService::OnFetchFinished(
444 NTPSnippetsFetcher::OptionalSnippets snippets) { 462 NTPSnippetsFetcher::OptionalSnippets snippets) {
445 if (!loaded()) 463 if (!ready())
446 return; 464 return;
447 465
448 if (snippets) { 466 if (snippets) {
449 // Sparse histogram used because the number of snippets is small (bound by 467 // Sparse histogram used because the number of snippets is small (bound by
450 // kMaxSnippetCount). 468 // kMaxSnippetCount).
451 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); 469 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount));
452 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", 470 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched",
453 snippets->size()); 471 snippets->size());
454 MergeSnippets(std::move(*snippets)); 472 MergeSnippets(std::move(*snippets));
455 } 473 }
456 LoadingSnippetsFinished(); 474 LoadingSnippetsFinished();
457 } 475 }
458 476
459 void NTPSnippetsService::MergeSnippets(NTPSnippet::PtrVector new_snippets) { 477 void NTPSnippetsService::MergeSnippets(NTPSnippet::PtrVector new_snippets) {
460 DCHECK(loaded()); 478 DCHECK(ready());
461 479
462 // Remove new snippets that we already have, or that have been discarded. 480 // Remove new snippets that we already have, or that have been discarded.
463 std::set<std::string> old_snippet_ids; 481 std::set<std::string> old_snippet_ids;
464 InsertAllIDs(discarded_snippets_, &old_snippet_ids); 482 InsertAllIDs(discarded_snippets_, &old_snippet_ids);
465 InsertAllIDs(snippets_, &old_snippet_ids); 483 InsertAllIDs(snippets_, &old_snippet_ids);
466 new_snippets.erase( 484 new_snippets.erase(
467 std::remove_if( 485 std::remove_if(
468 new_snippets.begin(), new_snippets.end(), 486 new_snippets.begin(), new_snippets.end(),
469 [&old_snippet_ids](const std::unique_ptr<NTPSnippet>& snippet) { 487 [&old_snippet_ids](const std::unique_ptr<NTPSnippet>& snippet) {
470 if (old_snippet_ids.count(snippet->id())) 488 if (old_snippet_ids.count(snippet->id()))
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 551
534 void NTPSnippetsService::StoreSnippetHostsToPrefs( 552 void NTPSnippetsService::StoreSnippetHostsToPrefs(
535 const std::set<std::string>& hosts) { 553 const std::set<std::string>& hosts) {
536 base::ListValue list; 554 base::ListValue list;
537 for (const std::string& host : hosts) 555 for (const std::string& host : hosts)
538 list.AppendString(host); 556 list.AppendString(host);
539 pref_service_->Set(prefs::kSnippetHosts, list); 557 pref_service_->Set(prefs::kSnippetHosts, list);
540 } 558 }
541 559
542 void NTPSnippetsService::LoadingSnippetsFinished() { 560 void NTPSnippetsService::LoadingSnippetsFinished() {
543 DCHECK(loaded()); 561 DCHECK(ready());
544 562
545 // Remove expired snippets. 563 // Remove expired snippets.
546 base::Time expiry = base::Time::Now(); 564 base::Time expiry = base::Time::Now();
547 565
548 // Move expired snippets over into |to_delete|. 566 // Move expired snippets over into |to_delete|.
549 NTPSnippet::PtrVector to_delete; 567 NTPSnippet::PtrVector to_delete;
550 for (std::unique_ptr<NTPSnippet>& snippet : snippets_) { 568 for (std::unique_ptr<NTPSnippet>& snippet : snippets_) {
551 if (snippet->expiry_date() <= expiry) 569 if (snippet->expiry_date() <= expiry)
552 to_delete.emplace_back(std::move(snippet)); 570 to_delete.emplace_back(std::move(snippet));
553 } 571 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 for (const auto& snippet : discarded_snippets_) { 613 for (const auto& snippet : discarded_snippets_) {
596 if (snippet->expiry_date() < next_expiry) 614 if (snippet->expiry_date() < next_expiry)
597 next_expiry = snippet->expiry_date(); 615 next_expiry = snippet->expiry_date();
598 } 616 }
599 DCHECK_GT(next_expiry, expiry); 617 DCHECK_GT(next_expiry, expiry);
600 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, 618 expiry_timer_.Start(FROM_HERE, next_expiry - expiry,
601 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, 619 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished,
602 base::Unretained(this))); 620 base::Unretained(this)));
603 } 621 }
604 622
605 bool NTPSnippetsService::IsSyncStateIncompatible() { 623 void NTPSnippetsService::Enable() {
606 if (!sync_service_ || !sync_service_->CanSyncStart()) 624 // Start a fetch if we don't have any snippets yet, or a fetch was requested
607 return true; 625 // earlier.
608 if (!sync_service_->IsSyncActive() || !sync_service_->ConfigurationDone()) 626 if (snippets_.empty() || fetch_after_load_) {
609 return false; // Sync service is not initialized, yet not disabled. 627 fetch_after_load_ = false;
610 return !sync_service_->GetActiveDataTypes().Has( 628 FetchSnippets();
611 syncer::HISTORY_DELETE_DIRECTIVES); 629 }
630
631 // If host restrictions are enabled, register for host list updates.
632 // |suggestions_service_| can be null in tests.
633 if (snippets_fetcher_->UsesHostRestrictions() && suggestions_service_) {
634 suggestions_service_subscription_ =
635 suggestions_service_->AddCallback(base::Bind(
636 &NTPSnippetsService::OnSuggestionsChanged, base::Unretained(this)));
637 }
638
639 RescheduleFetching();
640 }
641
642 void NTPSnippetsService::Disable() {
643 // TODO(dgn) should we also clear the discarded snippets?
644 ClearSnippets();
645
646 suggestions_service_subscription_.reset();
647 expiry_timer_.Stop();
648
649 RescheduleFetching();
650 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
651 NTPSnippetsServiceDisabled());
652 }
653
654 NTPSnippetsService::State NTPSnippetsService::GetStateForDependenciesStatus() {
655 DisabledReason dr = GetDisabledReason();
656
657 if (dr == DisabledReason::NONE)
658 return State::READY;
659
660 // HistorySync is not initialized yet, so we don't know what the actual state
661 // is. However, because we retreived the previous snippets from the database,
662 // if we got something, we know that the service was previously enabled, so
663 // we just restore that state. If things changed, |OnStateChanged| will call
664 // this function again to fix the state.
665 if (dr == DisabledReason::HISTORY_SYNC_STATE_UNKNOWN) {
666 DVLOG(1) << "Sync configuration not done, continuing based on the current "
667 "state.";
668 return snippets_.empty() ? State::DISABLED : State::READY;
669 }
670
671 return State::DISABLED;
672 }
673
674 void NTPSnippetsService::EnterState(State state) {
675 switch (state) {
676 case State::NOT_INITED:
677 // Initial state, it should not be possible to get back there.
678 NOTREACHED();
679 return;
680
681 case State::READY: {
682 DCHECK(state_ == State::NOT_INITED || state_ == State::READY ||
683 state_ == State::DISABLED);
684 if (state_ == State::READY)
685 return;
686
687 bool is_first_init = state_ == State::NOT_INITED;
688 DVLOG(1) << "Entering state: READY";
689 state_ = State::READY;
690
691 // At the first initialization, we loaded snippets from the database.
692 // We should notify listeners about them. We do so here because we have
693 // to be in the READY state.
694 if (is_first_init)
695 LoadingSnippetsFinished();
696
697 Enable();
698 return;
699 }
700
701 case State::DISABLED:
702 DCHECK(state_ == State::NOT_INITED || state_ == State::READY ||
703 state_ == State::DISABLED);
704 if (state_ == State::DISABLED)
705 return;
706
707 DVLOG(1) << "Entering state: DISABLED";
708 state_ = State::DISABLED;
709 Disable();
710 return;
711
712 case State::SHUT_DOWN:
713 DCHECK(state_ == State::NOT_INITED || state_ == State::DISABLED ||
714 state_ == State::READY);
715 DVLOG(1) << "Entering state: SHUT_DOWN";
716 state_ = State::SHUT_DOWN;
717 return;
718 }
612 } 719 }
613 720
614 void NTPSnippetsService::ClearDeprecatedPrefs() { 721 void NTPSnippetsService::ClearDeprecatedPrefs() {
615 pref_service_->ClearPref(prefs::kDeprecatedSnippets); 722 pref_service_->ClearPref(prefs::kDeprecatedSnippets);
616 pref_service_->ClearPref(prefs::kDeprecatedDiscardedSnippets); 723 pref_service_->ClearPref(prefs::kDeprecatedDiscardedSnippets);
617 } 724 }
618 725
619 } // namespace ntp_snippets 726 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.h ('k') | components/ntp_snippets/ntp_snippets_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698