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

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

Issue 2190353002: 📰Fix SnippetsService's status reporting (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/StatusListItem.java ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/ntp_snippets/ntp_snippets_service.h" 5 #include "components/ntp_snippets/ntp_snippets_service.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 FetchSnippetsFromHosts(GetSuggestionsHosts(), force_request); 252 FetchSnippetsFromHosts(GetSuggestionsHosts(), force_request);
253 else 253 else
254 fetch_after_load_ = true; 254 fetch_after_load_ = true;
255 } 255 }
256 256
257 void NTPSnippetsService::FetchSnippetsFromHosts( 257 void NTPSnippetsService::FetchSnippetsFromHosts(
258 const std::set<std::string>& hosts, 258 const std::set<std::string>& hosts,
259 bool force_request) { 259 bool force_request) {
260 if (!ready()) 260 if (!ready())
261 return; 261 return;
262 LOG(INFO) << "DGN - Initiating fetch";
263 EnterState(state_, ContentSuggestionsCategoryStatus::AVAILABLE_LOADING);
Marc Treib 2016/07/29 10:19:47 I don't think this is right. AVAILABLE_LOADING is
dgn 2016/07/29 10:42:48 Ah ok I didn't understand AVAILABLE_LOADING proper
Marc Treib 2016/07/29 10:45:46 Also while we're waiting for results from the DB (
dgn 2016/07/29 16:37:46 What about when the service was disabled, so we cl
Marc Treib 2016/08/01 09:03:31 Yes, that sounds right.
262 snippets_fetcher_->FetchSnippetsFromHosts(hosts, application_language_code_, 264 snippets_fetcher_->FetchSnippetsFromHosts(hosts, application_language_code_,
263 kMaxSnippetCount, force_request); 265 kMaxSnippetCount, force_request);
264 } 266 }
265 267
266 void NTPSnippetsService::RescheduleFetching() { 268 void NTPSnippetsService::RescheduleFetching() {
267 // The scheduler only exists on Android so far, it's null on other platforms. 269 // The scheduler only exists on Android so far, it's null on other platforms.
268 if (!scheduler_) 270 if (!scheduler_)
269 return; 271 return;
270 272
271 if (ready()) { 273 if (ready()) {
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 }); 411 });
410 412
411 ClearExpiredSnippets(); 413 ClearExpiredSnippets();
412 FinishInitialization(); 414 FinishInitialization();
413 } 415 }
414 416
415 void NTPSnippetsService::OnDatabaseError() { 417 void NTPSnippetsService::OnDatabaseError() {
416 EnterState(State::SHUT_DOWN, ContentSuggestionsCategoryStatus::LOADING_ERROR); 418 EnterState(State::SHUT_DOWN, ContentSuggestionsCategoryStatus::LOADING_ERROR);
417 } 419 }
418 420
421 // TODO(dgn): name clash between content suggestions and suggestions hosts.
422 // method name should be changed.
419 void NTPSnippetsService::OnSuggestionsChanged( 423 void NTPSnippetsService::OnSuggestionsChanged(
420 const SuggestionsProfile& suggestions) { 424 const SuggestionsProfile& suggestions) {
421 DCHECK(initialized()); 425 DCHECK(initialized());
422 426
423 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions); 427 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions);
424 if (hosts == GetSnippetHostsFromPrefs()) 428 if (hosts == GetSnippetHostsFromPrefs())
425 return; 429 return;
426 430
427 // Remove existing snippets that aren't in the suggestions anymore. 431 // Remove existing snippets that aren't in the suggestions anymore.
428 // TODO(treib,maybelle): If there is another source with an allowed host, 432 // TODO(treib,maybelle): If there is another source with an allowed host,
429 // then we should fall back to that. 433 // then we should fall back to that.
430 // First, move them over into |to_delete|. 434 // First, move them over into |to_delete|.
431 NTPSnippet::PtrVector to_delete; 435 NTPSnippet::PtrVector to_delete;
432 for (std::unique_ptr<NTPSnippet>& snippet : snippets_) { 436 for (std::unique_ptr<NTPSnippet>& snippet : snippets_) {
433 if (!hosts.count(snippet->best_source().url.host())) 437 if (!hosts.count(snippet->best_source().url.host()))
434 to_delete.emplace_back(std::move(snippet)); 438 to_delete.emplace_back(std::move(snippet));
435 } 439 }
436 Compact(&snippets_); 440 Compact(&snippets_);
437 // Then delete the removed snippets from the database. 441 // Then delete the removed snippets from the database.
438 database_->DeleteSnippets(to_delete); 442 database_->DeleteSnippets(to_delete);
439 443
440 StoreSnippetHostsToPrefs(hosts); 444 StoreSnippetHostsToPrefs(hosts);
441 445
446 // TODO(dgn) Why do we do that BEFORE fetching?
442 NotifyNewSuggestions(); 447 NotifyNewSuggestions();
dgn 2016/07/29 09:58:36 Why do we do that BEFORE fetching?
Marc Treib 2016/07/29 10:19:47 We potentially deleted some snippets above, so we
dgn 2016/07/29 10:42:48 Wouldn't they be notified of the new list when the
Marc Treib 2016/07/29 10:45:46 True, but that might take any amount of time, and
dgn 2016/07/29 16:37:46 Added a comment about this.
443 448
444 FetchSnippetsFromHosts(hosts, /*force_request=*/false); 449 FetchSnippetsFromHosts(hosts, /*force_request=*/false);
445 } 450 }
446 451
447 void NTPSnippetsService::OnFetchFinished( 452 void NTPSnippetsService::OnFetchFinished(
448 NTPSnippetsFetcher::OptionalSnippets snippets) { 453 NTPSnippetsFetcher::OptionalSnippets snippets) {
449 if (!ready()) 454 if (!ready())
450 return; 455 return;
451 456
452 if (snippets) { 457 if (snippets) {
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
698 image_fetcher_->SetImageFetcherDelegate(this); 703 image_fetcher_->SetImageFetcherDelegate(this);
699 image_fetcher_->SetDataUseServiceName( 704 image_fetcher_->SetDataUseServiceName(
700 data_use_measurement::DataUseUserData::NTP_SNIPPETS); 705 data_use_measurement::DataUseUserData::NTP_SNIPPETS);
701 } 706 }
702 707
703 // Note: Initializing the status service will run the callback right away with 708 // Note: Initializing the status service will run the callback right away with
704 // the current state. 709 // the current state.
705 snippets_status_service_->Init(base::Bind( 710 snippets_status_service_->Init(base::Bind(
706 &NTPSnippetsService::OnDisabledReasonChanged, base::Unretained(this))); 711 &NTPSnippetsService::OnDisabledReasonChanged, base::Unretained(this)));
707 712
713 // TODO(dgn): If we started a fetch as a result of the state being updated
714 // and not having snippets from the DB, because of the call below we would
715 // still notify the client with no snippets and force the "AVAILABLE" status.
716 // WAI?
717 // IMO that seems wrong. Skipping this call will put us either:
718 // - in AVAILABLE_LOADING: because of the pending fetch started above.
719 // - in INITIALIZING status: If it's right on startup and because we don't
720 // have sync data, the fetch can't start. We appropriately report that we
721 // are still initializing.
708 NotifyNewSuggestions(); 722 NotifyNewSuggestions();
dgn 2016/07/29 09:58:36 If we started a fetch as a result of the state bei
Marc Treib 2016/07/29 10:19:47 I guess this is here because we "probably" loaded
dgn 2016/07/29 10:42:48 So the intent is that while a fetch is pending and
Marc Treib 2016/07/29 10:45:46 I think so, yes. We should also be in _LOADING if
709 } 723 }
710 724
711 void NTPSnippetsService::OnDisabledReasonChanged( 725 void NTPSnippetsService::OnDisabledReasonChanged(
712 DisabledReason disabled_reason) { 726 DisabledReason disabled_reason) {
713 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 727 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
714 NTPSnippetsServiceDisabledReasonChanged(disabled_reason)); 728 NTPSnippetsServiceDisabledReasonChanged(disabled_reason));
715 729
716 switch (disabled_reason) { 730 switch (disabled_reason) {
717 case DisabledReason::NONE: 731 case DisabledReason::NONE: {
718 EnterState(State::READY, ContentSuggestionsCategoryStatus::AVAILABLE); 732 // Do not change the status. As the service becomes enabled, it will
733 // either start a new fetch (and transition to |AVAILABLE_LOADING|), or
734 // send cached suggestions (and transition to |AVAILABLE|).
735 EnterState(State::READY, category_status_);
719 break; 736 break;
737 }
720 738
721 case DisabledReason::HISTORY_SYNC_STATE_UNKNOWN: 739 case DisabledReason::HISTORY_SYNC_STATE_UNKNOWN:
722 // HistorySync is not initialized yet, so we don't know what the actual 740 // HistorySync is not initialized yet, so we don't know what the actual
723 // state is and we just return the current one. If things change, 741 // state is and we just return the current one. If things change,
724 // |OnStateChanged| will call this function again to update the state. 742 // |OnStateChanged| will call this function again to update the state.
725 DVLOG(1) << "Sync configuration incomplete, continuing based on the " 743 DVLOG(1) << "Sync configuration incomplete, continuing based on the "
726 "current state."; 744 "current state.";
727 EnterState(state_, ContentSuggestionsCategoryStatus::INITIALIZING); 745 EnterState(state_, ContentSuggestionsCategoryStatus::INITIALIZING);
728 break; 746 break;
729 747
(...skipping 20 matching lines...) Expand all
750 768
751 case DisabledReason::HISTORY_SYNC_DISABLED: 769 case DisabledReason::HISTORY_SYNC_DISABLED:
752 EnterState(State::DISABLED, 770 EnterState(State::DISABLED,
753 ContentSuggestionsCategoryStatus::HISTORY_SYNC_DISABLED); 771 ContentSuggestionsCategoryStatus::HISTORY_SYNC_DISABLED);
754 break; 772 break;
755 } 773 }
756 } 774 }
757 775
758 void NTPSnippetsService::EnterState(State state, 776 void NTPSnippetsService::EnterState(State state,
759 ContentSuggestionsCategoryStatus status) { 777 ContentSuggestionsCategoryStatus status) {
778 LOG(INFO) << "DGN - EnterState(" << (int)state << ", " << (int)status << ")";
779
760 if (status != category_status_) { 780 if (status != category_status_) {
761 category_status_ = status; 781 category_status_ = status;
762 NotifyCategoryStatusChanged(); 782 NotifyCategoryStatusChanged();
763 } 783 }
764 784
765 if (state == state_) 785 if (state == state_)
766 return; 786 return;
767 787
768 switch (state) { 788 switch (state) {
769 case State::NOT_INITED: 789 case State::NOT_INITED:
(...skipping 22 matching lines...) Expand all
792 812
793 case State::SHUT_DOWN: 813 case State::SHUT_DOWN:
794 DVLOG(1) << "Entering state: SHUT_DOWN"; 814 DVLOG(1) << "Entering state: SHUT_DOWN";
795 state_ = State::SHUT_DOWN; 815 state_ = State::SHUT_DOWN;
796 EnterStateShutdown(); 816 EnterStateShutdown();
797 return; 817 return;
798 } 818 }
799 } 819 }
800 820
801 void NTPSnippetsService::NotifyNewSuggestions() { 821 void NTPSnippetsService::NotifyNewSuggestions() {
822 EnterState(state_, ContentSuggestionsCategoryStatus::AVAILABLE);
Marc Treib 2016/07/29 10:19:47 Huh, seems like we should already be in this state
823
802 // TODO(pke): Remove this as soon as this becomes a pure provider. 824 // TODO(pke): Remove this as soon as this becomes a pure provider.
803 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 825 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
804 NTPSnippetsServiceLoaded()); 826 NTPSnippetsServiceLoaded());
805 827
806 if (!observer_) 828 if (!observer_)
807 return; 829 return;
808 830
809 std::vector<ContentSuggestion> result; 831 std::vector<ContentSuggestion> result;
810 for (const std::unique_ptr<NTPSnippet>& snippet : snippets_) { 832 for (const std::unique_ptr<NTPSnippet>& snippet : snippets_) {
811 if (!snippet->is_complete()) 833 if (!snippet->is_complete())
812 continue; 834 continue;
813 ContentSuggestion suggestion( 835 ContentSuggestion suggestion(
814 MakeUniqueID(ContentSuggestionsCategory::ARTICLES, snippet->id()), 836 MakeUniqueID(ContentSuggestionsCategory::ARTICLES, snippet->id()),
815 snippet->best_source().url); 837 snippet->best_source().url);
816 suggestion.set_amp_url(snippet->best_source().amp_url); 838 suggestion.set_amp_url(snippet->best_source().amp_url);
817 suggestion.set_title(snippet->title()); 839 suggestion.set_title(snippet->title());
818 suggestion.set_snippet_text(snippet->snippet()); 840 suggestion.set_snippet_text(snippet->snippet());
819 suggestion.set_publish_date(snippet->publish_date()); 841 suggestion.set_publish_date(snippet->publish_date());
820 suggestion.set_publisher_name(snippet->best_source().publisher_name); 842 suggestion.set_publisher_name(snippet->best_source().publisher_name);
821 suggestion.set_score(snippet->score()); 843 suggestion.set_score(snippet->score());
822 result.emplace_back(std::move(suggestion)); 844 result.emplace_back(std::move(suggestion));
823 } 845 }
824 observer_->OnNewSuggestions(ContentSuggestionsCategory::ARTICLES, 846 observer_->OnNewSuggestions(ContentSuggestionsCategory::ARTICLES,
825 std::move(result)); 847 std::move(result));
826 } 848 }
827 849
828 void NTPSnippetsService::NotifyCategoryStatusChanged() { 850 void NTPSnippetsService::NotifyCategoryStatusChanged() {
851 LOG(INFO) << "DGN - NotifyCategoryStatusChanged " << (int)category_status_;
829 if (observer_) { 852 if (observer_) {
830 observer_->OnCategoryStatusChanged(ContentSuggestionsCategory::ARTICLES, 853 observer_->OnCategoryStatusChanged(ContentSuggestionsCategory::ARTICLES,
831 category_status_); 854 category_status_);
832 } 855 }
833 } 856 }
834 857
835 } // namespace ntp_snippets 858 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/StatusListItem.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698