| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/ntp_snippets/remote/remote_suggestions_provider_impl.h" | 5 #include "components/ntp_snippets/remote/remote_suggestions_provider_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 if (base::ContainsValue(b, item)) { | 67 if (base::ContainsValue(b, item)) { |
| 68 return true; | 68 return true; |
| 69 } | 69 } |
| 70 } | 70 } |
| 71 return false; | 71 return false; |
| 72 } | 72 } |
| 73 | 73 |
| 74 void EraseByPrimaryID(RemoteSuggestion::PtrVector* suggestions, | 74 void EraseByPrimaryID(RemoteSuggestion::PtrVector* suggestions, |
| 75 const std::vector<std::string>& ids) { | 75 const std::vector<std::string>& ids) { |
| 76 std::set<std::string> ids_lookup(ids.begin(), ids.end()); | 76 std::set<std::string> ids_lookup(ids.begin(), ids.end()); |
| 77 suggestions->erase( | 77 base::EraseIf( |
| 78 std::remove_if( | 78 *suggestions, |
| 79 suggestions->begin(), suggestions->end(), | 79 [&ids_lookup](const std::unique_ptr<RemoteSuggestion>& suggestion) { |
| 80 [&ids_lookup](const std::unique_ptr<RemoteSuggestion>& suggestion) { | 80 return base::ContainsValue(ids_lookup, suggestion->id()); |
| 81 return base::ContainsValue(ids_lookup, suggestion->id()); | 81 }); |
| 82 }), | |
| 83 suggestions->end()); | |
| 84 } | 82 } |
| 85 | 83 |
| 86 void EraseMatchingSuggestions( | 84 void EraseMatchingSuggestions( |
| 87 RemoteSuggestion::PtrVector* suggestions, | 85 RemoteSuggestion::PtrVector* suggestions, |
| 88 const RemoteSuggestion::PtrVector& compare_against) { | 86 const RemoteSuggestion::PtrVector& compare_against) { |
| 89 std::set<std::string> compare_against_ids; | 87 std::set<std::string> compare_against_ids; |
| 90 for (const std::unique_ptr<RemoteSuggestion>& suggestion : compare_against) { | 88 for (const std::unique_ptr<RemoteSuggestion>& suggestion : compare_against) { |
| 91 const std::vector<std::string>& suggestion_ids = suggestion->GetAllIDs(); | 89 const std::vector<std::string>& suggestion_ids = suggestion->GetAllIDs(); |
| 92 compare_against_ids.insert(suggestion_ids.begin(), suggestion_ids.end()); | 90 compare_against_ids.insert(suggestion_ids.begin(), suggestion_ids.end()); |
| 93 } | 91 } |
| 94 suggestions->erase( | 92 base::EraseIf( |
| 95 std::remove_if(suggestions->begin(), suggestions->end(), | 93 *suggestions, [&compare_against_ids]( |
| 96 [&compare_against_ids]( | 94 const std::unique_ptr<RemoteSuggestion>& suggestion) { |
| 97 const std::unique_ptr<RemoteSuggestion>& suggestion) { | 95 return HasIntersection(suggestion->GetAllIDs(), compare_against_ids); |
| 98 return HasIntersection(suggestion->GetAllIDs(), | 96 }); |
| 99 compare_against_ids); | |
| 100 }), | |
| 101 suggestions->end()); | |
| 102 } | 97 } |
| 103 | 98 |
| 104 void RemoveNullPointers(RemoteSuggestion::PtrVector* suggestions) { | 99 void RemoveNullPointers(RemoteSuggestion::PtrVector* suggestions) { |
| 105 suggestions->erase( | 100 base::EraseIf(*suggestions, |
| 106 std::remove_if(suggestions->begin(), suggestions->end(), | 101 [](const std::unique_ptr<RemoteSuggestion>& suggestion) { |
| 107 [](const std::unique_ptr<RemoteSuggestion>& suggestion) { | 102 return !suggestion; |
| 108 return !suggestion; | 103 }); |
| 109 }), | |
| 110 suggestions->end()); | |
| 111 } | 104 } |
| 112 | 105 |
| 113 void RemoveIncompleteSuggestions(RemoteSuggestion::PtrVector* suggestions) { | 106 void RemoveIncompleteSuggestions(RemoteSuggestion::PtrVector* suggestions) { |
| 114 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 107 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 115 switches::kAddIncompleteSnippets)) { | 108 switches::kAddIncompleteSnippets)) { |
| 116 return; | 109 return; |
| 117 } | 110 } |
| 118 int num_suggestions = suggestions->size(); | 111 int num_suggestions = suggestions->size(); |
| 119 // Remove suggestions that do not have all the info we need to display it to | 112 // Remove suggestions that do not have all the info we need to display it to |
| 120 // the user. | 113 // the user. |
| 121 suggestions->erase( | 114 base::EraseIf(*suggestions, |
| 122 std::remove_if(suggestions->begin(), suggestions->end(), | 115 [](const std::unique_ptr<RemoteSuggestion>& suggestion) { |
| 123 [](const std::unique_ptr<RemoteSuggestion>& suggestion) { | 116 return !suggestion->is_complete(); |
| 124 return !suggestion->is_complete(); | 117 }); |
| 125 }), | |
| 126 suggestions->end()); | |
| 127 int num_suggestions_removed = num_suggestions - suggestions->size(); | 118 int num_suggestions_removed = num_suggestions - suggestions->size(); |
| 128 UMA_HISTOGRAM_BOOLEAN("NewTabPage.Snippets.IncompleteSnippetsAfterFetch", | 119 UMA_HISTOGRAM_BOOLEAN("NewTabPage.Snippets.IncompleteSnippetsAfterFetch", |
| 129 num_suggestions_removed > 0); | 120 num_suggestions_removed > 0); |
| 130 if (num_suggestions_removed > 0) { | 121 if (num_suggestions_removed > 0) { |
| 131 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumIncompleteSnippets", | 122 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumIncompleteSnippets", |
| 132 num_suggestions_removed); | 123 num_suggestions_removed); |
| 133 } | 124 } |
| 134 } | 125 } |
| 135 | 126 |
| 136 std::vector<ContentSuggestion> ConvertToContentSuggestions( | 127 std::vector<ContentSuggestion> ConvertToContentSuggestions( |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 } | 228 } |
| 238 // If decoding the image failed, delete the DB entry. | 229 // If decoding the image failed, delete the DB entry. |
| 239 database_->DeleteImage(suggestion_id.id_within_category()); | 230 database_->DeleteImage(suggestion_id.id_within_category()); |
| 240 FetchImageFromNetwork(suggestion_id, url, callback); | 231 FetchImageFromNetwork(suggestion_id, url, callback); |
| 241 } | 232 } |
| 242 | 233 |
| 243 void CachedImageFetcher::FetchImageFromNetwork( | 234 void CachedImageFetcher::FetchImageFromNetwork( |
| 244 const ContentSuggestion::ID& suggestion_id, | 235 const ContentSuggestion::ID& suggestion_id, |
| 245 const GURL& url, | 236 const GURL& url, |
| 246 const ImageFetchedCallback& callback) { | 237 const ImageFetchedCallback& callback) { |
| 247 if (url.is_empty() || | 238 if (url.is_empty() || !thumbnail_requests_throttler_.DemandQuotaForRequest( |
| 248 !thumbnail_requests_throttler_.DemandQuotaForRequest( | 239 /*interactive_request=*/true)) { |
| 249 /*interactive_request=*/true)) { | |
| 250 // Return an empty image. Directly, this is never synchronous with the | 240 // Return an empty image. Directly, this is never synchronous with the |
| 251 // original FetchSuggestionImage() call - an asynchronous database query has | 241 // original FetchSuggestionImage() call - an asynchronous database query has |
| 252 // happened in the meantime. | 242 // happened in the meantime. |
| 253 callback.Run(gfx::Image()); | 243 callback.Run(gfx::Image()); |
| 254 return; | 244 return; |
| 255 } | 245 } |
| 256 | 246 |
| 257 image_fetcher_->StartOrQueueNetworkRequest( | 247 image_fetcher_->StartOrQueueNetworkRequest( |
| 258 suggestion_id.id_within_category(), url, | 248 suggestion_id.id_within_category(), url, |
| 259 base::Bind(&CachedImageFetcher::OnImageDecodingDone, | 249 base::Bind(&CachedImageFetcher::OnImageDecodingDone, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 271 std::unique_ptr<RemoteSuggestionsStatusService> status_service) | 261 std::unique_ptr<RemoteSuggestionsStatusService> status_service) |
| 272 : RemoteSuggestionsProvider(observer), | 262 : RemoteSuggestionsProvider(observer), |
| 273 state_(State::NOT_INITED), | 263 state_(State::NOT_INITED), |
| 274 pref_service_(pref_service), | 264 pref_service_(pref_service), |
| 275 articles_category_( | 265 articles_category_( |
| 276 Category::FromKnownCategory(KnownCategories::ARTICLES)), | 266 Category::FromKnownCategory(KnownCategories::ARTICLES)), |
| 277 application_language_code_(application_language_code), | 267 application_language_code_(application_language_code), |
| 278 category_ranker_(category_ranker), | 268 category_ranker_(category_ranker), |
| 279 suggestions_fetcher_(std::move(suggestions_fetcher)), | 269 suggestions_fetcher_(std::move(suggestions_fetcher)), |
| 280 database_(std::move(database)), | 270 database_(std::move(database)), |
| 281 image_fetcher_(std::move(image_fetcher), | 271 image_fetcher_(std::move(image_fetcher), pref_service, database_.get()), |
| 282 pref_service, | |
| 283 database_.get()), | |
| 284 status_service_(std::move(status_service)), | 272 status_service_(std::move(status_service)), |
| 285 fetch_when_ready_(false), | 273 fetch_when_ready_(false), |
| 286 fetch_when_ready_interactive_(false), | 274 fetch_when_ready_interactive_(false), |
| 287 fetch_when_ready_callback_(nullptr), | 275 fetch_when_ready_callback_(nullptr), |
| 288 remote_suggestions_scheduler_(nullptr), | 276 remote_suggestions_scheduler_(nullptr), |
| 289 clear_history_dependent_state_when_initialized_(false), | 277 clear_history_dependent_state_when_initialized_(false), |
| 290 clock_(base::MakeUnique<base::DefaultClock>()) { | 278 clock_(base::MakeUnique<base::DefaultClock>()) { |
| 291 RestoreCategoriesFromPrefs(); | 279 RestoreCategoriesFromPrefs(); |
| 292 // The articles category always exists. Add it if we didn't get it from prefs. | 280 // The articles category always exists. Add it if we didn't get it from prefs. |
| 293 // TODO(treib): Rethink this. | 281 // TODO(treib): Rethink this. |
| (...skipping 987 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1281 RemoteSuggestionsProviderImpl::CategoryContent::CategoryContent( | 1269 RemoteSuggestionsProviderImpl::CategoryContent::CategoryContent( |
| 1282 CategoryContent&&) = default; | 1270 CategoryContent&&) = default; |
| 1283 | 1271 |
| 1284 RemoteSuggestionsProviderImpl::CategoryContent::~CategoryContent() = default; | 1272 RemoteSuggestionsProviderImpl::CategoryContent::~CategoryContent() = default; |
| 1285 | 1273 |
| 1286 RemoteSuggestionsProviderImpl::CategoryContent& | 1274 RemoteSuggestionsProviderImpl::CategoryContent& |
| 1287 RemoteSuggestionsProviderImpl::CategoryContent::operator=(CategoryContent&&) = | 1275 RemoteSuggestionsProviderImpl::CategoryContent::operator=(CategoryContent&&) = |
| 1288 default; | 1276 default; |
| 1289 | 1277 |
| 1290 } // namespace ntp_snippets | 1278 } // namespace ntp_snippets |
| OLD | NEW |