| 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" | 
|   11 #include "components/ntp_snippets/category_status.h" |   11 #include "components/ntp_snippets/category_status.h" | 
|   12 #include "components/offline_pages/core/client_namespace_constants.h" |   12 #include "components/offline_pages/core/client_namespace_constants.h" | 
|   13 #include "components/offline_pages/core/offline_page_feature.h" |   13 #include "components/offline_pages/core/offline_page_feature.h" | 
|   14 #include "components/offline_pages/core/offline_page_item.h" |   14 #include "components/offline_pages/core/offline_page_item.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_impl.h" |   16 #include "components/offline_pages/core/prefetch/prefetch_service_impl.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 const ntp_snippets::Category& ArticlesCategory() { |   26 const ntp_snippets::Category& ArticlesCategory() { | 
|   26   static ntp_snippets::Category articles = |   27   static ntp_snippets::Category articles = | 
| (...skipping 27 matching lines...) Expand all  Loading... | 
|   54     return; |   55     return; | 
|   55   } |   56   } | 
|   56  |   57  | 
|   57   const std::vector<ContentSuggestion>& suggestions = |   58   const std::vector<ContentSuggestion>& suggestions = | 
|   58       test_articles_ ? *test_articles_ |   59       test_articles_ ? *test_articles_ | 
|   59                      : content_suggestions_service_->GetSuggestionsForCategory( |   60                      : content_suggestions_service_->GetSuggestionsForCategory( | 
|   60                            ArticlesCategory()); |   61                            ArticlesCategory()); | 
|   61   if (suggestions.empty()) |   62   if (suggestions.empty()) | 
|   62     return; |   63     return; | 
|   63  |   64  | 
|   64   std::vector<PrefetchDispatcher::PrefetchURL> prefetch_urls; |   65   std::vector<PrefetchURL> prefetch_urls; | 
|   65   for (const ContentSuggestion& suggestion : suggestions) { |   66   for (const ContentSuggestion& suggestion : suggestions) { | 
|   66     prefetch_urls.push_back( |   67     prefetch_urls.push_back( | 
|   67         {CreateClientIDFromSuggestionId(suggestion.id()), suggestion.url()}); |   68         {CreateClientIDFromSuggestionId(suggestion.id()), suggestion.url()}); | 
|   68   } |   69   } | 
|   69  |   70  | 
|   70   prefetch_service_->GetDispatcher()->AddCandidatePrefetchURLs(prefetch_urls); |   71   prefetch_service_->GetPrefetchDispatcher()->AddCandidatePrefetchURLs( | 
 |   72       prefetch_urls); | 
|   71 } |   73 } | 
|   72  |   74  | 
|   73 void SuggestedArticlesObserver::OnCategoryStatusChanged( |   75 void SuggestedArticlesObserver::OnCategoryStatusChanged( | 
|   74     Category category, |   76     Category category, | 
|   75     ntp_snippets::CategoryStatus new_status) { |   77     ntp_snippets::CategoryStatus new_status) { | 
|   76   if (category != ArticlesCategory() || category_status_ == new_status) |   78   if (category != ArticlesCategory() || category_status_ == new_status) | 
|   77     return; |   79     return; | 
|   78  |   80  | 
|   79   category_status_ = new_status; |   81   category_status_ = new_status; | 
|   80  |   82  | 
|   81   if (category_status_ == |   83   if (category_status_ == | 
|   82           ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED || |   84           ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED || | 
|   83       category_status_ == |   85       category_status_ == | 
|   84           ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED) { |   86           ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED) { | 
|   85     prefetch_service_->GetDispatcher()->RemoveAllUnprocessedPrefetchURLs( |   87     prefetch_service_->GetPrefetchDispatcher() | 
|   86         kSuggestedArticlesNamespace); |   88         ->RemoveAllUnprocessedPrefetchURLs(kSuggestedArticlesNamespace); | 
|   87   } |   89   } | 
|   88 } |   90 } | 
|   89  |   91  | 
|   90 void SuggestedArticlesObserver::OnSuggestionInvalidated( |   92 void SuggestedArticlesObserver::OnSuggestionInvalidated( | 
|   91     const ContentSuggestion::ID& suggestion_id) { |   93     const ContentSuggestion::ID& suggestion_id) { | 
|   92   prefetch_service_->GetDispatcher()->RemovePrefetchURLsByClientId( |   94   prefetch_service_->GetPrefetchDispatcher()->RemovePrefetchURLsByClientId( | 
|   93       CreateClientIDFromSuggestionId(suggestion_id)); |   95       CreateClientIDFromSuggestionId(suggestion_id)); | 
|   94 } |   96 } | 
|   95  |   97  | 
|   96 void SuggestedArticlesObserver::OnFullRefreshRequired() { |   98 void SuggestedArticlesObserver::OnFullRefreshRequired() { | 
|   97   prefetch_service_->GetDispatcher()->RemoveAllUnprocessedPrefetchURLs( |   99   prefetch_service_->GetPrefetchDispatcher()->RemoveAllUnprocessedPrefetchURLs( | 
|   98       kSuggestedArticlesNamespace); |  100       kSuggestedArticlesNamespace); | 
|   99   OnNewSuggestions(ArticlesCategory()); |  101   OnNewSuggestions(ArticlesCategory()); | 
|  100 } |  102 } | 
|  101  |  103  | 
|  102 void SuggestedArticlesObserver::ContentSuggestionsServiceShutdown() { |  104 void SuggestedArticlesObserver::ContentSuggestionsServiceShutdown() { | 
|  103   // No need to do anything here, we will just stop getting events. |  105   // No need to do anything here, we will just stop getting events. | 
|  104 } |  106 } | 
|  105  |  107  | 
|  106 std::vector<ntp_snippets::ContentSuggestion>* |  108 std::vector<ntp_snippets::ContentSuggestion>* | 
|  107 SuggestedArticlesObserver::GetTestingArticles() { |  109 SuggestedArticlesObserver::GetTestingArticles() { | 
|  108   if (!test_articles_) { |  110   if (!test_articles_) { | 
|  109     test_articles_ = |  111     test_articles_ = | 
|  110         base::MakeUnique<std::vector<ntp_snippets::ContentSuggestion>>(); |  112         base::MakeUnique<std::vector<ntp_snippets::ContentSuggestion>>(); | 
|  111   } |  113   } | 
|  112   return test_articles_.get(); |  114   return test_articles_.get(); | 
|  113 } |  115 } | 
|  114  |  116  | 
|  115 }  // namespace offline_pages |  117 }  // namespace offline_pages | 
| OLD | NEW |