OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/offline_pages/core/prefetch/suggested_articles_observer.h" |
| 6 |
| 7 #include <unordered_set> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "components/ntp_snippets/category.h" |
| 11 #include "components/ntp_snippets/category_status.h" |
| 12 #include "components/offline_pages/core/client_namespace_constants.h" |
| 13 #include "components/offline_pages/core/offline_page_feature.h" |
| 14 #include "components/offline_pages/core/offline_page_item.h" |
| 15 #include "components/offline_pages/core/prefetch/prefetch_dispatcher.h" |
| 16 #include "components/offline_pages/core/prefetch/prefetch_service_impl.h" |
| 17 |
| 18 using ntp_snippets::Category; |
| 19 using ntp_snippets::ContentSuggestion; |
| 20 |
| 21 namespace offline_pages { |
| 22 |
| 23 namespace { |
| 24 |
| 25 const ntp_snippets::Category& ArticlesCategory() { |
| 26 static ntp_snippets::Category articles = |
| 27 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); |
| 28 return articles; |
| 29 } |
| 30 |
| 31 ClientId CreateClientIDFromSuggestionId(const ContentSuggestion::ID& id) { |
| 32 return ClientId(kSuggestedArticlesNamespace, id.id_within_category()); |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
| 37 SuggestedArticlesObserver::SuggestedArticlesObserver( |
| 38 ntp_snippets::ContentSuggestionsService* content_suggestions_service, |
| 39 PrefetchService* prefetch_service) |
| 40 : content_suggestions_service_(content_suggestions_service), |
| 41 prefetch_service_(prefetch_service) { |
| 42 // The content suggestions service can be |nullptr| in tests. |
| 43 if (content_suggestions_service_) |
| 44 content_suggestions_service_->AddObserver(this); |
| 45 } |
| 46 |
| 47 SuggestedArticlesObserver::~SuggestedArticlesObserver() = default; |
| 48 |
| 49 void SuggestedArticlesObserver::OnNewSuggestions(Category category) { |
| 50 // TODO(dewittj): Change this to check whether a given category is not |
| 51 // a _remote_ category. |
| 52 if (category != ArticlesCategory() || |
| 53 category_status_ != ntp_snippets::CategoryStatus::AVAILABLE) { |
| 54 return; |
| 55 } |
| 56 |
| 57 const std::vector<ContentSuggestion>& suggestions = |
| 58 test_articles_ ? *test_articles_ |
| 59 : content_suggestions_service_->GetSuggestionsForCategory( |
| 60 ArticlesCategory()); |
| 61 if (suggestions.empty()) |
| 62 return; |
| 63 |
| 64 std::vector<PrefetchDispatcher::PrefetchURL> prefetch_urls; |
| 65 for (const ContentSuggestion& suggestion : suggestions) { |
| 66 prefetch_urls.push_back( |
| 67 {CreateClientIDFromSuggestionId(suggestion.id()), suggestion.url()}); |
| 68 } |
| 69 |
| 70 prefetch_service_->GetDispatcher()->AddCandidatePrefetchURLs(prefetch_urls); |
| 71 } |
| 72 |
| 73 void SuggestedArticlesObserver::OnCategoryStatusChanged( |
| 74 Category category, |
| 75 ntp_snippets::CategoryStatus new_status) { |
| 76 if (category != ArticlesCategory() || category_status_ == new_status) |
| 77 return; |
| 78 |
| 79 category_status_ = new_status; |
| 80 |
| 81 if (category_status_ == |
| 82 ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED || |
| 83 category_status_ == |
| 84 ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED) { |
| 85 prefetch_service_->GetDispatcher()->RemoveAllUnprocessedPrefetchURLs( |
| 86 kSuggestedArticlesNamespace); |
| 87 } |
| 88 } |
| 89 |
| 90 void SuggestedArticlesObserver::OnSuggestionInvalidated( |
| 91 const ContentSuggestion::ID& suggestion_id) { |
| 92 prefetch_service_->GetDispatcher()->RemovePrefetchURLsByClientId( |
| 93 CreateClientIDFromSuggestionId(suggestion_id)); |
| 94 } |
| 95 |
| 96 void SuggestedArticlesObserver::OnFullRefreshRequired() { |
| 97 prefetch_service_->GetDispatcher()->RemoveAllUnprocessedPrefetchURLs( |
| 98 kSuggestedArticlesNamespace); |
| 99 OnNewSuggestions(ArticlesCategory()); |
| 100 } |
| 101 |
| 102 void SuggestedArticlesObserver::ContentSuggestionsServiceShutdown() { |
| 103 // No need to do anything here, we will just stop getting events. |
| 104 } |
| 105 |
| 106 std::vector<ntp_snippets::ContentSuggestion>* |
| 107 SuggestedArticlesObserver::GetTestingArticles() { |
| 108 if (!test_articles_) { |
| 109 test_articles_ = |
| 110 base::MakeUnique<std::vector<ntp_snippets::ContentSuggestion>>(); |
| 111 } |
| 112 return test_articles_.get(); |
| 113 } |
| 114 |
| 115 } // namespace offline_pages |
OLD | NEW |