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/prefetch/prefetch_service_impl.h" |
| 15 |
| 16 using ntp_snippets::Category; |
| 17 using ntp_snippets::ContentSuggestion; |
| 18 |
| 19 namespace offline_pages { |
| 20 |
| 21 namespace { |
| 22 |
| 23 const ntp_snippets::Category& ArticlesCategory() { |
| 24 static ntp_snippets::Category articles = |
| 25 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); |
| 26 return articles; |
| 27 } |
| 28 |
| 29 ClientId CreateClientIDFromSuggestionId(const ContentSuggestion::ID& id) { |
| 30 return ClientId(kSuggestedArticlesNamespace, id.id_within_category()); |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
| 35 SuggestedArticlesObserver::SuggestedArticlesObserver( |
| 36 ntp_snippets::ContentSuggestionsService* content_suggestions_service, |
| 37 PrefetchService* prefetch_service) |
| 38 : content_suggestions_service_(content_suggestions_service), |
| 39 prefetch_service_(prefetch_service) { |
| 40 // The content suggestions service can be |nullptr| in tests. |
| 41 if (content_suggestions_service_) |
| 42 content_suggestions_service_->AddObserver(this); |
| 43 } |
| 44 |
| 45 SuggestedArticlesObserver::~SuggestedArticlesObserver() = default; |
| 46 |
| 47 void SuggestedArticlesObserver::OnNewSuggestions(Category category) { |
| 48 // TODO(dewittj): Change this to check whether a given category is not |
| 49 // a _remote_ category. |
| 50 if (category != ArticlesCategory() || |
| 51 category_status_ != ntp_snippets::CategoryStatus::AVAILABLE) { |
| 52 return; |
| 53 } |
| 54 |
| 55 const std::vector<ContentSuggestion>& suggestions = |
| 56 test_articles_ ? *test_articles_ |
| 57 : content_suggestions_service_->GetSuggestionsForCategory( |
| 58 ArticlesCategory()); |
| 59 if (suggestions.empty()) |
| 60 return; |
| 61 |
| 62 std::vector<PrefetchService::PrefetchURL> prefetch_urls; |
| 63 for (const ContentSuggestion& suggestion : suggestions) { |
| 64 prefetch_urls.push_back( |
| 65 {CreateClientIDFromSuggestionId(suggestion.id()), suggestion.url()}); |
| 66 } |
| 67 |
| 68 prefetch_service_->AddCandidatePrefetchURLs(prefetch_urls); |
| 69 } |
| 70 |
| 71 void SuggestedArticlesObserver::OnCategoryStatusChanged( |
| 72 Category category, |
| 73 ntp_snippets::CategoryStatus new_status) { |
| 74 if (category != ArticlesCategory() || category_status_ == new_status) |
| 75 return; |
| 76 |
| 77 category_status_ = new_status; |
| 78 |
| 79 if (category_status_ == |
| 80 ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED || |
| 81 category_status_ == |
| 82 ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED) { |
| 83 prefetch_service_->RemoveAllUnprocessedPrefetchURLs( |
| 84 kSuggestedArticlesNamespace); |
| 85 } |
| 86 } |
| 87 |
| 88 void SuggestedArticlesObserver::OnSuggestionInvalidated( |
| 89 const ContentSuggestion::ID& suggestion_id) { |
| 90 prefetch_service_->RemovePrefetchURLsByClientId( |
| 91 CreateClientIDFromSuggestionId(suggestion_id)); |
| 92 } |
| 93 |
| 94 void SuggestedArticlesObserver::OnFullRefreshRequired() { |
| 95 prefetch_service_->RemoveAllUnprocessedPrefetchURLs( |
| 96 kSuggestedArticlesNamespace); |
| 97 OnNewSuggestions(ArticlesCategory()); |
| 98 } |
| 99 |
| 100 void SuggestedArticlesObserver::ContentSuggestionsServiceShutdown() { |
| 101 // No need to do anything here, we will just stop getting events. |
| 102 } |
| 103 |
| 104 std::vector<ntp_snippets::ContentSuggestion>* |
| 105 SuggestedArticlesObserver::GetTestingArticles() { |
| 106 if (!test_articles_) { |
| 107 test_articles_ = |
| 108 base::MakeUnique<std::vector<ntp_snippets::ContentSuggestion>>(); |
| 109 } |
| 110 return test_articles_.get(); |
| 111 } |
| 112 |
| 113 } // namespace offline_pages |
OLD | NEW |