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 <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 bool enabled, | 186 bool enabled, |
187 PrefService* pref_service, | 187 PrefService* pref_service, |
188 SuggestionsService* suggestions_service, | 188 SuggestionsService* suggestions_service, |
189 const std::string& application_language_code, | 189 const std::string& application_language_code, |
190 NTPSnippetsScheduler* scheduler, | 190 NTPSnippetsScheduler* scheduler, |
191 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 191 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
192 std::unique_ptr<ImageFetcher> image_fetcher, | 192 std::unique_ptr<ImageFetcher> image_fetcher, |
193 std::unique_ptr<ImageDecoder> image_decoder, | 193 std::unique_ptr<ImageDecoder> image_decoder, |
194 std::unique_ptr<NTPSnippetsDatabase> database, | 194 std::unique_ptr<NTPSnippetsDatabase> database, |
195 std::unique_ptr<NTPSnippetsStatusService> status_service) | 195 std::unique_ptr<NTPSnippetsStatusService> status_service) |
196 : state_(State::NOT_INITED), | 196 : ContentSuggestionsProvider({ContentSuggestionsCategory::ARTICLES}), |
| 197 state_(State::NOT_INITED), |
| 198 category_status_(ContentSuggestionsCategoryStatus::INITIALIZING), |
197 pref_service_(pref_service), | 199 pref_service_(pref_service), |
198 suggestions_service_(suggestions_service), | 200 suggestions_service_(suggestions_service), |
199 application_language_code_(application_language_code), | 201 application_language_code_(application_language_code), |
| 202 observer_(nullptr), |
200 scheduler_(scheduler), | 203 scheduler_(scheduler), |
201 snippets_fetcher_(std::move(snippets_fetcher)), | 204 snippets_fetcher_(std::move(snippets_fetcher)), |
202 image_fetcher_(std::move(image_fetcher)), | 205 image_fetcher_(std::move(image_fetcher)), |
203 image_decoder_(std::move(image_decoder)), | 206 image_decoder_(std::move(image_decoder)), |
204 database_(std::move(database)), | 207 database_(std::move(database)), |
205 snippets_status_service_(std::move(status_service)), | 208 snippets_status_service_(std::move(status_service)), |
206 fetch_after_load_(false) { | 209 fetch_after_load_(false) { |
207 if (!enabled || database_->IsErrorState()) { | 210 // In some cases, don't even bother loading the database. |
208 // Don't even bother loading the database. | 211 if (!enabled) { |
209 EnterState(State::SHUT_DOWN); | 212 EnterState(State::SHUT_DOWN, |
| 213 ContentSuggestionsCategoryStatus::CATEGORY_EXPLICITLY_DISABLED); |
| 214 return; |
| 215 } |
| 216 if (database_->IsErrorState()) { |
| 217 EnterState(State::SHUT_DOWN, |
| 218 ContentSuggestionsCategoryStatus::LOADING_ERROR); |
210 return; | 219 return; |
211 } | 220 } |
212 | 221 |
213 database_->SetErrorCallback(base::Bind(&NTPSnippetsService::OnDatabaseError, | 222 database_->SetErrorCallback(base::Bind(&NTPSnippetsService::OnDatabaseError, |
214 base::Unretained(this))); | 223 base::Unretained(this))); |
215 | 224 |
216 // We transition to other states while finalizing the initialization, when the | 225 // We transition to other states while finalizing the initialization, when the |
217 // database is done loading. | 226 // database is done loading. |
218 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, | 227 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, |
219 base::Unretained(this))); | 228 base::Unretained(this))); |
220 } | 229 } |
221 | 230 |
222 NTPSnippetsService::~NTPSnippetsService() { | 231 NTPSnippetsService::~NTPSnippetsService() { |
223 DCHECK(state_ == State::SHUT_DOWN); | 232 DCHECK(state_ == State::SHUT_DOWN); |
224 } | 233 } |
225 | 234 |
226 // static | 235 // static |
227 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { | 236 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
228 registry->RegisterListPref(prefs::kSnippetHosts); | 237 registry->RegisterListPref(prefs::kSnippetHosts); |
229 } | 238 } |
230 | 239 |
231 // Inherited from KeyedService. | 240 // Inherited from KeyedService. |
232 void NTPSnippetsService::Shutdown() { | 241 void NTPSnippetsService::Shutdown() { |
233 EnterState(State::SHUT_DOWN); | 242 EnterState(State::SHUT_DOWN, ContentSuggestionsCategoryStatus::NOT_PROVIDED); |
234 } | 243 } |
235 | 244 |
236 void NTPSnippetsService::FetchSnippets() { | 245 void NTPSnippetsService::FetchSnippets() { |
237 if (ready()) | 246 if (ready()) |
238 FetchSnippetsFromHosts(GetSuggestionsHosts()); | 247 FetchSnippetsFromHosts(GetSuggestionsHosts()); |
239 else | 248 else |
240 fetch_after_load_ = true; | 249 fetch_after_load_ = true; |
241 } | 250 } |
242 | 251 |
243 void NTPSnippetsService::FetchSnippetsFromHosts( | 252 void NTPSnippetsService::FetchSnippetsFromHosts( |
(...skipping 12 matching lines...) Expand all Loading... |
256 if (ready()) { | 265 if (ready()) { |
257 base::Time now = base::Time::Now(); | 266 base::Time now = base::Time::Now(); |
258 scheduler_->Schedule( | 267 scheduler_->Schedule( |
259 GetFetchingIntervalWifiCharging(), GetFetchingIntervalWifi(now), | 268 GetFetchingIntervalWifiCharging(), GetFetchingIntervalWifi(now), |
260 GetFetchingIntervalFallback(), GetRescheduleTime(now)); | 269 GetFetchingIntervalFallback(), GetRescheduleTime(now)); |
261 } else { | 270 } else { |
262 scheduler_->Unschedule(); | 271 scheduler_->Unschedule(); |
263 } | 272 } |
264 } | 273 } |
265 | 274 |
266 void NTPSnippetsService::FetchSnippetImage( | 275 void NTPSnippetsService::FetchSuggestionImage( |
267 const std::string& snippet_id, | 276 const std::string& suggestion_id, |
268 const ImageFetchedCallback& callback) { | 277 const ImageFetchedCallback& callback) { |
269 database_->LoadImage( | 278 database_->LoadImage( |
270 snippet_id, | 279 suggestion_id, |
271 base::Bind(&NTPSnippetsService::OnSnippetImageFetchedFromDatabase, | 280 base::Bind(&NTPSnippetsService::OnSnippetImageFetchedFromDatabase, |
272 base::Unretained(this), snippet_id, callback)); | 281 base::Unretained(this), suggestion_id, callback)); |
273 } | 282 } |
274 | 283 |
275 void NTPSnippetsService::ClearSnippets() { | 284 void NTPSnippetsService::ClearCachedSuggestionsForDebugging() { |
276 if (!initialized()) | 285 if (!initialized()) |
277 return; | 286 return; |
278 | 287 |
279 if (snippets_.empty()) | 288 if (snippets_.empty()) |
280 return; | 289 return; |
281 | 290 |
282 database_->DeleteSnippets(snippets_); | 291 database_->DeleteSnippets(snippets_); |
283 snippets_.clear(); | 292 snippets_.clear(); |
284 | 293 |
285 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 294 NotifyNewSuggestions(); |
286 NTPSnippetsServiceLoaded()); | |
287 } | 295 } |
288 | 296 |
289 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const { | 297 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const { |
290 // |suggestions_service_| can be null in tests. | 298 // |suggestions_service_| can be null in tests. |
291 if (!suggestions_service_) | 299 if (!suggestions_service_) |
292 return std::set<std::string>(); | 300 return std::set<std::string>(); |
293 | 301 |
294 // TODO(treib): This should just call GetSnippetHostsFromPrefs. | 302 // TODO(treib): This should just call GetSnippetHostsFromPrefs. |
295 return GetSuggestionsHostsImpl( | 303 return GetSuggestionsHostsImpl( |
296 suggestions_service_->GetSuggestionsDataFromCache()); | 304 suggestions_service_->GetSuggestionsDataFromCache()); |
297 } | 305 } |
298 | 306 |
299 bool NTPSnippetsService::DiscardSnippet(const std::string& snippet_id) { | 307 void NTPSnippetsService::DiscardSuggestion(const std::string& suggestion_id) { |
300 if (!ready()) | 308 if (!ready()) |
301 return false; | 309 return; |
302 | 310 |
303 auto it = | 311 auto it = std::find_if( |
304 std::find_if(snippets_.begin(), snippets_.end(), | 312 snippets_.begin(), snippets_.end(), |
305 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) { | 313 [&suggestion_id](const std::unique_ptr<NTPSnippet>& snippet) { |
306 return snippet->id() == snippet_id; | 314 return snippet->id() == suggestion_id; |
307 }); | 315 }); |
308 if (it == snippets_.end()) | 316 if (it == snippets_.end()) |
309 return false; | 317 return; |
310 | 318 |
311 (*it)->set_discarded(true); | 319 (*it)->set_discarded(true); |
312 | 320 |
313 database_->SaveSnippet(**it); | 321 database_->SaveSnippet(**it); |
314 database_->DeleteImage((*it)->id()); | 322 database_->DeleteImage((*it)->id()); |
315 | 323 |
316 discarded_snippets_.push_back(std::move(*it)); | 324 discarded_snippets_.push_back(std::move(*it)); |
317 snippets_.erase(it); | 325 snippets_.erase(it); |
318 | 326 |
319 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 327 NotifyNewSuggestions(); |
320 NTPSnippetsServiceLoaded()); | |
321 return true; | |
322 } | 328 } |
323 | 329 |
324 void NTPSnippetsService::ClearDiscardedSnippets() { | 330 void NTPSnippetsService::ClearDiscardedSuggestionsForDebugging() { |
325 if (!initialized()) | 331 if (!initialized()) |
326 return; | 332 return; |
327 | 333 |
328 if (discarded_snippets_.empty()) | 334 if (discarded_snippets_.empty()) |
329 return; | 335 return; |
330 | 336 |
331 database_->DeleteSnippets(discarded_snippets_); | 337 database_->DeleteSnippets(discarded_snippets_); |
332 discarded_snippets_.clear(); | 338 discarded_snippets_.clear(); |
333 } | 339 } |
334 | 340 |
| 341 void NTPSnippetsService::SetObserver(Observer* observer) { |
| 342 observer_ = observer; |
| 343 } |
| 344 |
| 345 ContentSuggestionsCategoryStatus NTPSnippetsService::GetCategoryStatus( |
| 346 ContentSuggestionsCategory category) { |
| 347 DCHECK_EQ(ContentSuggestionsCategory::ARTICLES, category); |
| 348 return category_status_; |
| 349 } |
| 350 |
335 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { | 351 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { |
336 observers_.AddObserver(observer); | 352 observers_.AddObserver(observer); |
337 } | 353 } |
338 | 354 |
339 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { | 355 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { |
340 observers_.RemoveObserver(observer); | 356 observers_.RemoveObserver(observer); |
341 } | 357 } |
342 | 358 |
343 // static | 359 // static |
344 int NTPSnippetsService::GetMaxSnippetCountForTesting() { | 360 int NTPSnippetsService::GetMaxSnippetCountForTesting() { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 [](const std::unique_ptr<NTPSnippet>& lhs, | 399 [](const std::unique_ptr<NTPSnippet>& lhs, |
384 const std::unique_ptr<NTPSnippet>& rhs) { | 400 const std::unique_ptr<NTPSnippet>& rhs) { |
385 return lhs->score() > rhs->score(); | 401 return lhs->score() > rhs->score(); |
386 }); | 402 }); |
387 | 403 |
388 ClearExpiredSnippets(); | 404 ClearExpiredSnippets(); |
389 FinishInitialization(); | 405 FinishInitialization(); |
390 } | 406 } |
391 | 407 |
392 void NTPSnippetsService::OnDatabaseError() { | 408 void NTPSnippetsService::OnDatabaseError() { |
393 EnterState(State::SHUT_DOWN); | 409 EnterState(State::SHUT_DOWN, ContentSuggestionsCategoryStatus::LOADING_ERROR); |
394 } | 410 } |
395 | 411 |
396 void NTPSnippetsService::OnSuggestionsChanged( | 412 void NTPSnippetsService::OnSuggestionsChanged( |
397 const SuggestionsProfile& suggestions) { | 413 const SuggestionsProfile& suggestions) { |
398 DCHECK(initialized()); | 414 DCHECK(initialized()); |
399 | 415 |
400 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions); | 416 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions); |
401 if (hosts == GetSnippetHostsFromPrefs()) | 417 if (hosts == GetSnippetHostsFromPrefs()) |
402 return; | 418 return; |
403 | 419 |
404 // Remove existing snippets that aren't in the suggestions anymore. | 420 // Remove existing snippets that aren't in the suggestions anymore. |
405 // TODO(treib,maybelle): If there is another source with an allowed host, | 421 // TODO(treib,maybelle): If there is another source with an allowed host, |
406 // then we should fall back to that. | 422 // then we should fall back to that. |
407 // First, move them over into |to_delete|. | 423 // First, move them over into |to_delete|. |
408 NTPSnippet::PtrVector to_delete; | 424 NTPSnippet::PtrVector to_delete; |
409 for (std::unique_ptr<NTPSnippet>& snippet : snippets_) { | 425 for (std::unique_ptr<NTPSnippet>& snippet : snippets_) { |
410 if (!hosts.count(snippet->best_source().url.host())) | 426 if (!hosts.count(snippet->best_source().url.host())) |
411 to_delete.emplace_back(std::move(snippet)); | 427 to_delete.emplace_back(std::move(snippet)); |
412 } | 428 } |
413 Compact(&snippets_); | 429 Compact(&snippets_); |
414 // Then delete the removed snippets from the database. | 430 // Then delete the removed snippets from the database. |
415 database_->DeleteSnippets(to_delete); | 431 database_->DeleteSnippets(to_delete); |
416 | 432 |
417 StoreSnippetHostsToPrefs(hosts); | 433 StoreSnippetHostsToPrefs(hosts); |
418 | 434 |
419 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 435 NotifyNewSuggestions(); |
420 NTPSnippetsServiceLoaded()); | |
421 | 436 |
422 FetchSnippetsFromHosts(hosts); | 437 FetchSnippetsFromHosts(hosts); |
423 } | 438 } |
424 | 439 |
425 void NTPSnippetsService::OnFetchFinished( | 440 void NTPSnippetsService::OnFetchFinished( |
426 NTPSnippetsFetcher::OptionalSnippets snippets) { | 441 NTPSnippetsFetcher::OptionalSnippets snippets) { |
427 if (!ready()) | 442 if (!ready()) |
428 return; | 443 return; |
429 | 444 |
430 if (snippets) { | 445 if (snippets) { |
(...skipping 16 matching lines...) Expand all Loading... |
447 database_->DeleteSnippets(to_delete); | 462 database_->DeleteSnippets(to_delete); |
448 } | 463 } |
449 | 464 |
450 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticles", | 465 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticles", |
451 snippets_.size()); | 466 snippets_.size()); |
452 if (snippets_.empty() && !discarded_snippets_.empty()) { | 467 if (snippets_.empty() && !discarded_snippets_.empty()) { |
453 UMA_HISTOGRAM_COUNTS("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded", | 468 UMA_HISTOGRAM_COUNTS("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded", |
454 discarded_snippets_.size()); | 469 discarded_snippets_.size()); |
455 } | 470 } |
456 | 471 |
457 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 472 NotifyNewSuggestions(); |
458 NTPSnippetsServiceLoaded()); | |
459 } | 473 } |
460 | 474 |
461 void NTPSnippetsService::MergeSnippets(NTPSnippet::PtrVector new_snippets) { | 475 void NTPSnippetsService::MergeSnippets(NTPSnippet::PtrVector new_snippets) { |
462 DCHECK(ready()); | 476 DCHECK(ready()); |
463 | 477 |
464 // Remove new snippets that we already have, or that have been discarded. | 478 // Remove new snippets that we already have, or that have been discarded. |
465 std::set<std::string> old_snippet_ids; | 479 std::set<std::string> old_snippet_ids; |
466 InsertAllIDs(discarded_snippets_, &old_snippet_ids); | 480 InsertAllIDs(discarded_snippets_, &old_snippet_ids); |
467 InsertAllIDs(snippets_, &old_snippet_ids); | 481 InsertAllIDs(snippets_, &old_snippet_ids); |
468 new_snippets.erase( | 482 new_snippets.erase( |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
640 if (snippets_fetcher_->UsesHostRestrictions() && suggestions_service_) { | 654 if (snippets_fetcher_->UsesHostRestrictions() && suggestions_service_) { |
641 suggestions_service_subscription_ = | 655 suggestions_service_subscription_ = |
642 suggestions_service_->AddCallback(base::Bind( | 656 suggestions_service_->AddCallback(base::Bind( |
643 &NTPSnippetsService::OnSuggestionsChanged, base::Unretained(this))); | 657 &NTPSnippetsService::OnSuggestionsChanged, base::Unretained(this))); |
644 } | 658 } |
645 | 659 |
646 RescheduleFetching(); | 660 RescheduleFetching(); |
647 } | 661 } |
648 | 662 |
649 void NTPSnippetsService::EnterStateDisabled() { | 663 void NTPSnippetsService::EnterStateDisabled() { |
650 ClearSnippets(); | 664 ClearCachedSuggestionsForDebugging(); |
651 ClearDiscardedSnippets(); | 665 ClearDiscardedSuggestionsForDebugging(); |
652 | 666 |
653 expiry_timer_.Stop(); | 667 expiry_timer_.Stop(); |
654 suggestions_service_subscription_.reset(); | 668 suggestions_service_subscription_.reset(); |
655 RescheduleFetching(); | 669 RescheduleFetching(); |
656 } | 670 } |
657 | 671 |
658 void NTPSnippetsService::EnterStateShutdown() { | 672 void NTPSnippetsService::EnterStateShutdown() { |
659 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 673 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
660 NTPSnippetsServiceShutdown()); | 674 NTPSnippetsServiceShutdown()); |
661 | 675 |
(...skipping 10 matching lines...) Expand all Loading... |
672 | 686 |
673 // |image_fetcher_| can be null in tests. | 687 // |image_fetcher_| can be null in tests. |
674 if (image_fetcher_) | 688 if (image_fetcher_) |
675 image_fetcher_->SetImageFetcherDelegate(this); | 689 image_fetcher_->SetImageFetcherDelegate(this); |
676 | 690 |
677 // Note: Initializing the status service will run the callback right away with | 691 // Note: Initializing the status service will run the callback right away with |
678 // the current state. | 692 // the current state. |
679 snippets_status_service_->Init(base::Bind( | 693 snippets_status_service_->Init(base::Bind( |
680 &NTPSnippetsService::UpdateStateForStatus, base::Unretained(this))); | 694 &NTPSnippetsService::UpdateStateForStatus, base::Unretained(this))); |
681 | 695 |
682 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 696 NotifyNewSuggestions(); |
683 NTPSnippetsServiceLoaded()); | |
684 } | 697 } |
685 | 698 |
686 void NTPSnippetsService::UpdateStateForStatus(DisabledReason disabled_reason) { | 699 void NTPSnippetsService::UpdateStateForStatus(DisabledReason disabled_reason) { |
687 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 700 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
688 NTPSnippetsServiceDisabledReasonChanged(disabled_reason)); | 701 NTPSnippetsServiceDisabledReasonChanged(disabled_reason)); |
689 | 702 |
690 State new_state; | 703 State new_state; |
| 704 ContentSuggestionsCategoryStatus new_status; |
691 switch (disabled_reason) { | 705 switch (disabled_reason) { |
692 case DisabledReason::NONE: | 706 case DisabledReason::NONE: |
693 new_state = State::READY; | 707 new_state = State::READY; |
| 708 new_status = ContentSuggestionsCategoryStatus::AVAILABLE; |
694 break; | 709 break; |
695 | 710 |
696 case DisabledReason::HISTORY_SYNC_STATE_UNKNOWN: | 711 case DisabledReason::HISTORY_SYNC_STATE_UNKNOWN: |
697 // HistorySync is not initialized yet, so we don't know what the actual | 712 // HistorySync is not initialized yet, so we don't know what the actual |
698 // state is and we just return the current one. If things change, | 713 // state is and we just return the current one. If things change, |
699 // |OnStateChanged| will call this function again to update the state. | 714 // |OnStateChanged| will call this function again to update the state. |
700 DVLOG(1) << "Sync configuration incomplete, continuing based on the " | 715 DVLOG(1) << "Sync configuration incomplete, continuing based on the " |
701 "current state."; | 716 "current state."; |
702 new_state = state_; | 717 new_state = state_; |
| 718 new_status = ContentSuggestionsCategoryStatus::INITIALIZING; |
703 break; | 719 break; |
704 | 720 |
705 case DisabledReason::EXPLICITLY_DISABLED: | 721 case DisabledReason::EXPLICITLY_DISABLED: |
| 722 new_state = State::DISABLED; |
| 723 new_status = |
| 724 ContentSuggestionsCategoryStatus::CATEGORY_EXPLICITLY_DISABLED; |
| 725 break; |
| 726 |
706 case DisabledReason::SIGNED_OUT: | 727 case DisabledReason::SIGNED_OUT: |
| 728 new_state = State::DISABLED; |
| 729 new_status = ContentSuggestionsCategoryStatus::SIGNED_OUT; |
| 730 break; |
| 731 |
707 case DisabledReason::SYNC_DISABLED: | 732 case DisabledReason::SYNC_DISABLED: |
| 733 new_state = State::DISABLED; |
| 734 new_status = ContentSuggestionsCategoryStatus::SYNC_DISABLED; |
| 735 break; |
| 736 |
708 case DisabledReason::PASSPHRASE_ENCRYPTION_ENABLED: | 737 case DisabledReason::PASSPHRASE_ENCRYPTION_ENABLED: |
| 738 new_state = State::DISABLED; |
| 739 new_status = |
| 740 ContentSuggestionsCategoryStatus::PASSPHRASE_ENCRYPTION_ENABLED; |
| 741 break; |
| 742 |
709 case DisabledReason::HISTORY_SYNC_DISABLED: | 743 case DisabledReason::HISTORY_SYNC_DISABLED: |
710 new_state = State::DISABLED; | 744 new_state = State::DISABLED; |
| 745 new_status = ContentSuggestionsCategoryStatus::HISTORY_SYNC_DISABLED; |
711 break; | 746 break; |
712 | 747 |
713 default: | 748 default: |
714 // All cases should be handled by the above switch | 749 // All cases should be handled by the above switch |
715 NOTREACHED(); | 750 NOTREACHED(); |
716 new_state = State::DISABLED; | 751 new_state = State::DISABLED; |
| 752 new_status = ContentSuggestionsCategoryStatus::LOADING_ERROR; |
717 break; | 753 break; |
718 } | 754 } |
719 | 755 |
720 EnterState(new_state); | 756 EnterState(new_state, new_status); |
721 } | 757 } |
722 | 758 |
723 void NTPSnippetsService::EnterState(State state) { | 759 void NTPSnippetsService::EnterState(State state, |
| 760 ContentSuggestionsCategoryStatus status) { |
| 761 if (status != category_status_) { |
| 762 category_status_ = status; |
| 763 NotifyCategoryStatusChanged(); |
| 764 } |
| 765 |
724 if (state == state_) | 766 if (state == state_) |
725 return; | 767 return; |
726 | 768 |
727 switch (state) { | 769 switch (state) { |
728 case State::NOT_INITED: | 770 case State::NOT_INITED: |
729 // Initial state, it should not be possible to get back there. | 771 // Initial state, it should not be possible to get back there. |
730 NOTREACHED(); | 772 NOTREACHED(); |
731 return; | 773 return; |
732 | 774 |
733 case State::READY: { | 775 case State::READY: { |
(...skipping 16 matching lines...) Expand all Loading... |
750 return; | 792 return; |
751 | 793 |
752 case State::SHUT_DOWN: | 794 case State::SHUT_DOWN: |
753 DVLOG(1) << "Entering state: SHUT_DOWN"; | 795 DVLOG(1) << "Entering state: SHUT_DOWN"; |
754 state_ = State::SHUT_DOWN; | 796 state_ = State::SHUT_DOWN; |
755 EnterStateShutdown(); | 797 EnterStateShutdown(); |
756 return; | 798 return; |
757 } | 799 } |
758 } | 800 } |
759 | 801 |
| 802 void NTPSnippetsService::NotifyNewSuggestions() { |
| 803 // TODO(pke): Remove this as soon as this becomes a pure provider. |
| 804 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
| 805 NTPSnippetsServiceLoaded()); |
| 806 |
| 807 if (!observer_) |
| 808 return; |
| 809 |
| 810 std::vector<ContentSuggestion> result; |
| 811 for (const std::unique_ptr<NTPSnippet>& snippet : snippets_) { |
| 812 if (!snippet->is_complete()) |
| 813 continue; |
| 814 ContentSuggestion suggestion( |
| 815 MakeUniqueID(ContentSuggestionsCategory::ARTICLES, snippet->id()), |
| 816 snippet->best_source().url); |
| 817 suggestion.set_amp_url(snippet->best_source().amp_url); |
| 818 suggestion.set_title(snippet->title()); |
| 819 suggestion.set_snippet_text(snippet->snippet()); |
| 820 suggestion.set_publish_date(snippet->publish_date()); |
| 821 suggestion.set_publisher_name(snippet->best_source().publisher_name); |
| 822 suggestion.set_score(snippet->score()); |
| 823 result.emplace_back(std::move(suggestion)); |
| 824 } |
| 825 observer_->OnNewSuggestions(ContentSuggestionsCategory::ARTICLES, |
| 826 std::move(result)); |
| 827 } |
| 828 |
| 829 void NTPSnippetsService::NotifyCategoryStatusChanged() { |
| 830 if (observer_) { |
| 831 observer_->OnCategoryStatusChanged(ContentSuggestionsCategory::ARTICLES, |
| 832 category_status_); |
| 833 } |
| 834 } |
| 835 |
760 } // namespace ntp_snippets | 836 } // namespace ntp_snippets |
OLD | NEW |