OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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/offline_pages/core/prefetch/suggested_articles_observer.h" | 5 #include "components/offline_pages/core/prefetch/suggested_articles_observer.h" |
6 | 6 |
7 #include <unordered_set> | 7 #include <unordered_set> |
8 | 8 |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "components/ntp_snippets/category.h" | 10 #include "components/ntp_snippets/category.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 namespace offline_pages { | 21 namespace offline_pages { |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 const ntp_snippets::Category& ArticlesCategory() { | 25 const ntp_snippets::Category& ArticlesCategory() { |
26 static ntp_snippets::Category articles = | 26 static ntp_snippets::Category articles = |
27 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); | 27 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); |
28 return articles; | 28 return articles; |
29 } | 29 } |
30 | 30 |
31 ClientId CreateClientIDFromSuggestionId(const ContentSuggestion::ID& id) { | |
32 return ClientId(kSuggestedArticlesNamespace, id.id_within_category()); | |
33 } | |
34 | |
35 } // namespace | 31 } // namespace |
36 | 32 |
37 SuggestedArticlesObserver::SuggestedArticlesObserver( | 33 SuggestedArticlesObserver::SuggestedArticlesObserver( |
38 PrefetchDispatcher* dispatcher) | 34 PrefetchDispatcher* dispatcher) |
39 : prefetch_dispatcher_(dispatcher) { | 35 : prefetch_dispatcher_(dispatcher) { |
40 DCHECK(prefetch_dispatcher_); | 36 DCHECK(prefetch_dispatcher_); |
41 } | 37 } |
42 | 38 |
43 SuggestedArticlesObserver::~SuggestedArticlesObserver() { | 39 SuggestedArticlesObserver::~SuggestedArticlesObserver() { |
44 if (content_suggestions_service_) | 40 if (content_suggestions_service_) |
(...skipping 19 matching lines...) Expand all Loading... |
64 const std::vector<ContentSuggestion>& suggestions = | 60 const std::vector<ContentSuggestion>& suggestions = |
65 test_articles_ ? *test_articles_ | 61 test_articles_ ? *test_articles_ |
66 : content_suggestions_service_->GetSuggestionsForCategory( | 62 : content_suggestions_service_->GetSuggestionsForCategory( |
67 ArticlesCategory()); | 63 ArticlesCategory()); |
68 if (suggestions.empty()) | 64 if (suggestions.empty()) |
69 return; | 65 return; |
70 | 66 |
71 std::vector<PrefetchURL> prefetch_urls; | 67 std::vector<PrefetchURL> prefetch_urls; |
72 for (const ContentSuggestion& suggestion : suggestions) { | 68 for (const ContentSuggestion& suggestion : suggestions) { |
73 prefetch_urls.push_back( | 69 prefetch_urls.push_back( |
74 {CreateClientIDFromSuggestionId(suggestion.id()), suggestion.url()}); | 70 {suggestion.id().id_within_category(), suggestion.url()}); |
75 } | 71 } |
76 | 72 |
77 prefetch_dispatcher_->AddCandidatePrefetchURLs(prefetch_urls); | 73 prefetch_dispatcher_->AddCandidatePrefetchURLs(kSuggestedArticlesNamespace, |
| 74 prefetch_urls); |
78 } | 75 } |
79 | 76 |
80 void SuggestedArticlesObserver::OnCategoryStatusChanged( | 77 void SuggestedArticlesObserver::OnCategoryStatusChanged( |
81 Category category, | 78 Category category, |
82 ntp_snippets::CategoryStatus new_status) { | 79 ntp_snippets::CategoryStatus new_status) { |
83 if (category != ArticlesCategory() || category_status_ == new_status) | 80 if (category != ArticlesCategory() || category_status_ == new_status) |
84 return; | 81 return; |
85 | 82 |
86 category_status_ = new_status; | 83 category_status_ = new_status; |
87 | 84 |
88 if (category_status_ == | 85 if (category_status_ == |
89 ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED || | 86 ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED || |
90 category_status_ == | 87 category_status_ == |
91 ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED) { | 88 ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED) { |
92 prefetch_dispatcher_->RemoveAllUnprocessedPrefetchURLs( | 89 prefetch_dispatcher_->RemoveAllUnprocessedPrefetchURLs( |
93 kSuggestedArticlesNamespace); | 90 kSuggestedArticlesNamespace); |
94 } | 91 } |
95 } | 92 } |
96 | 93 |
97 void SuggestedArticlesObserver::OnSuggestionInvalidated( | 94 void SuggestedArticlesObserver::OnSuggestionInvalidated( |
98 const ContentSuggestion::ID& suggestion_id) { | 95 const ContentSuggestion::ID& suggestion_id) { |
99 prefetch_dispatcher_->RemovePrefetchURLsByClientId( | 96 prefetch_dispatcher_->RemovePrefetchURLsByClientId(ClientId( |
100 CreateClientIDFromSuggestionId(suggestion_id)); | 97 kSuggestedArticlesNamespace, suggestion_id.id_within_category())); |
101 } | 98 } |
102 | 99 |
103 void SuggestedArticlesObserver::OnFullRefreshRequired() { | 100 void SuggestedArticlesObserver::OnFullRefreshRequired() { |
104 prefetch_dispatcher_->RemoveAllUnprocessedPrefetchURLs( | 101 prefetch_dispatcher_->RemoveAllUnprocessedPrefetchURLs( |
105 kSuggestedArticlesNamespace); | 102 kSuggestedArticlesNamespace); |
106 OnNewSuggestions(ArticlesCategory()); | 103 OnNewSuggestions(ArticlesCategory()); |
107 } | 104 } |
108 | 105 |
109 void SuggestedArticlesObserver::ContentSuggestionsServiceShutdown() { | 106 void SuggestedArticlesObserver::ContentSuggestionsServiceShutdown() { |
110 // No need to do anything here, we will just stop getting events. | 107 // No need to do anything here, we will just stop getting events. |
111 } | 108 } |
112 | 109 |
113 std::vector<ntp_snippets::ContentSuggestion>* | 110 std::vector<ntp_snippets::ContentSuggestion>* |
114 SuggestedArticlesObserver::GetTestingArticles() { | 111 SuggestedArticlesObserver::GetTestingArticles() { |
115 if (!test_articles_) { | 112 if (!test_articles_) { |
116 test_articles_ = | 113 test_articles_ = |
117 base::MakeUnique<std::vector<ntp_snippets::ContentSuggestion>>(); | 114 base::MakeUnique<std::vector<ntp_snippets::ContentSuggestion>>(); |
118 } | 115 } |
119 return test_articles_.get(); | 116 return test_articles_.get(); |
120 } | 117 } |
121 | 118 |
122 } // namespace offline_pages | 119 } // namespace offline_pages |
OLD | NEW |