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