| 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/content/suggested_articles_observer.h" | 5 #include "components/offline_pages/content/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" |
| 11 #include "components/ntp_snippets/category_status.h" | 11 #include "components/ntp_snippets/category_status.h" |
| 12 #include "components/offline_pages/content/prefetch_service_factory.h" | 12 #include "components/offline_pages/content/prefetch_service_factory.h" |
| 13 #include "components/offline_pages/core/client_namespace_constants.h" | 13 #include "components/offline_pages/core/client_namespace_constants.h" |
| 14 #include "components/offline_pages/core/offline_page_feature.h" | 14 #include "components/offline_pages/core/offline_page_feature.h" |
| 15 #include "components/offline_pages/core/prefetch/prefetch_dispatcher.h" | 15 #include "components/offline_pages/core/prefetch/prefetch_dispatcher.h" |
| 16 #include "components/offline_pages/core/prefetch/prefetch_service.h" | 16 #include "components/offline_pages/core/prefetch/prefetch_service.h" |
| 17 #include "components/offline_pages/core/prefetch/prefetch_types.h" |
| 17 | 18 |
| 18 using ntp_snippets::Category; | 19 using ntp_snippets::Category; |
| 19 using ntp_snippets::ContentSuggestion; | 20 using ntp_snippets::ContentSuggestion; |
| 20 | 21 |
| 21 namespace offline_pages { | 22 namespace offline_pages { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 int kOfflinePageSuggestedArticlesObserverUserDataKey; | 26 int kOfflinePageSuggestedArticlesObserverUserDataKey; |
| 26 | 27 |
| 27 const ntp_snippets::Category& ArticlesCategory() { | 28 const ntp_snippets::Category& ArticlesCategory() { |
| 28 static ntp_snippets::Category articles = | 29 static ntp_snippets::Category articles = |
| 29 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); | 30 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); |
| 30 return articles; | 31 return articles; |
| 31 } | 32 } |
| 32 | 33 |
| 33 ClientId CreateClientIDFromSuggestionId(const ContentSuggestion::ID& id) { | |
| 34 return ClientId(kSuggestedArticlesNamespace, id.id_within_category()); | |
| 35 } | |
| 36 | |
| 37 // The default delegate that contains external dependencies for the Offline Page | 34 // The default delegate that contains external dependencies for the Offline Page |
| 38 // Suggestions Observer. This is unused in tests, which implement their own | 35 // Suggestions Observer. This is unused in tests, which implement their own |
| 39 // Delegate. | 36 // Delegate. |
| 40 class DefaultDelegate : public SuggestedArticlesObserver::Delegate { | 37 class DefaultDelegate : public SuggestedArticlesObserver::Delegate { |
| 41 public: | 38 public: |
| 42 explicit DefaultDelegate(ntp_snippets::ContentSuggestionsService* service); | 39 explicit DefaultDelegate(ntp_snippets::ContentSuggestionsService* service); |
| 43 ~DefaultDelegate() override = default; | 40 ~DefaultDelegate() override = default; |
| 44 | 41 |
| 45 const std::vector<ContentSuggestion>& GetSuggestions( | 42 const std::vector<ContentSuggestion>& GetSuggestions( |
| 46 const Category& category) override; | 43 const Category& category) override; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 if (category != ArticlesCategory() || | 91 if (category != ArticlesCategory() || |
| 95 category_status_ != ntp_snippets::CategoryStatus::AVAILABLE) { | 92 category_status_ != ntp_snippets::CategoryStatus::AVAILABLE) { |
| 96 return; | 93 return; |
| 97 } | 94 } |
| 98 | 95 |
| 99 const std::vector<ContentSuggestion>& suggestions = | 96 const std::vector<ContentSuggestion>& suggestions = |
| 100 delegate_->GetSuggestions(ArticlesCategory()); | 97 delegate_->GetSuggestions(ArticlesCategory()); |
| 101 if (suggestions.empty()) | 98 if (suggestions.empty()) |
| 102 return; | 99 return; |
| 103 | 100 |
| 104 std::vector<PrefetchDispatcher::PrefetchURL> prefetch_urls; | 101 std::vector<PrefetchURL> prefetch_urls; |
| 105 for (const ContentSuggestion& suggestion : suggestions) { | 102 for (const ContentSuggestion& suggestion : suggestions) { |
| 106 prefetch_urls.push_back( | 103 prefetch_urls.push_back( |
| 107 {CreateClientIDFromSuggestionId(suggestion.id()), suggestion.url()}); | 104 {suggestion.id().id_within_category(), suggestion.url()}); |
| 108 } | 105 } |
| 109 | 106 |
| 110 PrefetchService* service = delegate_->GetPrefetchService(browser_context_); | 107 PrefetchService* service = delegate_->GetPrefetchService(browser_context_); |
| 111 if (service == nullptr) { | 108 if (service == nullptr) { |
| 112 DVLOG(1) << "PrefetchService unavailable to the " | 109 DVLOG(1) << "PrefetchService unavailable to the " |
| 113 "SuggestedArticlesObserver."; | 110 "SuggestedArticlesObserver."; |
| 114 return; | 111 return; |
| 115 } | 112 } |
| 116 service->GetDispatcher()->AddCandidatePrefetchURLs(prefetch_urls); | 113 service->GetDispatcher()->AddCandidatePrefetchURLs( |
| 114 kSuggestedArticlesNamespace, prefetch_urls); |
| 117 } | 115 } |
| 118 | 116 |
| 119 void SuggestedArticlesObserver::OnCategoryStatusChanged( | 117 void SuggestedArticlesObserver::OnCategoryStatusChanged( |
| 120 Category category, | 118 Category category, |
| 121 ntp_snippets::CategoryStatus new_status) { | 119 ntp_snippets::CategoryStatus new_status) { |
| 122 if (category != ArticlesCategory() || category_status_ == new_status) | 120 if (category != ArticlesCategory() || category_status_ == new_status) |
| 123 return; | 121 return; |
| 124 | 122 |
| 125 category_status_ = new_status; | 123 category_status_ = new_status; |
| 126 | 124 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 141 | 139 |
| 142 void SuggestedArticlesObserver::OnSuggestionInvalidated( | 140 void SuggestedArticlesObserver::OnSuggestionInvalidated( |
| 143 const ContentSuggestion::ID& suggestion_id) { | 141 const ContentSuggestion::ID& suggestion_id) { |
| 144 PrefetchService* service = delegate_->GetPrefetchService(browser_context_); | 142 PrefetchService* service = delegate_->GetPrefetchService(browser_context_); |
| 145 if (service == nullptr) { | 143 if (service == nullptr) { |
| 146 DVLOG(1) << "PrefetchService unavailable to the " | 144 DVLOG(1) << "PrefetchService unavailable to the " |
| 147 "SuggestedArticlesObserver."; | 145 "SuggestedArticlesObserver."; |
| 148 return; | 146 return; |
| 149 } | 147 } |
| 150 service->GetDispatcher()->RemovePrefetchURLsByClientId( | 148 service->GetDispatcher()->RemovePrefetchURLsByClientId( |
| 151 CreateClientIDFromSuggestionId(suggestion_id)); | 149 kSuggestedArticlesNamespace, suggestion_id.id_within_category()); |
| 152 } | 150 } |
| 153 | 151 |
| 154 void SuggestedArticlesObserver::OnFullRefreshRequired() { | 152 void SuggestedArticlesObserver::OnFullRefreshRequired() { |
| 155 PrefetchService* service = delegate_->GetPrefetchService(browser_context_); | 153 PrefetchService* service = delegate_->GetPrefetchService(browser_context_); |
| 156 if (service == nullptr) { | 154 if (service == nullptr) { |
| 157 DVLOG(1) << "PrefetchService unavailable to the " | 155 DVLOG(1) << "PrefetchService unavailable to the " |
| 158 "SuggestedArticlesObserver."; | 156 "SuggestedArticlesObserver."; |
| 159 return; | 157 return; |
| 160 } | 158 } |
| 161 service->GetDispatcher()->RemoveAllUnprocessedPrefetchURLs( | 159 service->GetDispatcher()->RemoveAllUnprocessedPrefetchURLs( |
| 162 kSuggestedArticlesNamespace); | 160 kSuggestedArticlesNamespace); |
| 163 OnNewSuggestions(ArticlesCategory()); | 161 OnNewSuggestions(ArticlesCategory()); |
| 164 } | 162 } |
| 165 | 163 |
| 166 void SuggestedArticlesObserver::ContentSuggestionsServiceShutdown() { | 164 void SuggestedArticlesObserver::ContentSuggestionsServiceShutdown() { |
| 167 // No need to do anything here, we will just stop getting events. | 165 // No need to do anything here, we will just stop getting events. |
| 168 } | 166 } |
| 169 | 167 |
| 170 } // namespace offline_pages | 168 } // namespace offline_pages |
| OLD | NEW |