Chromium Code Reviews| 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/ntp_snippets_service.h" | 5 #include "components/ntp_snippets/ntp_snippets_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 214 image_decoder_(std::move(image_decoder)), | 214 image_decoder_(std::move(image_decoder)), |
| 215 database_(std::move(database)), | 215 database_(std::move(database)), |
| 216 snippets_status_service_(std::move(status_service)), | 216 snippets_status_service_(std::move(status_service)), |
| 217 fetch_after_load_(false), | 217 fetch_after_load_(false), |
| 218 nuke_after_load_(false), | 218 nuke_after_load_(false), |
| 219 thumbnail_requests_throttler_( | 219 thumbnail_requests_throttler_( |
| 220 pref_service, | 220 pref_service, |
| 221 RequestThrottler::RequestType::CONTENT_SUGGESTION_THUMBNAIL) { | 221 RequestThrottler::RequestType::CONTENT_SUGGESTION_THUMBNAIL) { |
| 222 // Articles category always exists; others will be added as needed. | 222 // Articles category always exists; others will be added as needed. |
| 223 categories_[articles_category_] = CategoryContent(); | 223 categories_[articles_category_] = CategoryContent(); |
| 224 categories_[articles_category_].localized_title = | |
| 225 l10n_util::GetStringUTF16(IDS_NTP_ARTICLE_SUGGESTIONS_SECTION_HEADER); | |
| 224 observer->OnCategoryStatusChanged(this, articles_category_, | 226 observer->OnCategoryStatusChanged(this, articles_category_, |
| 225 categories_[articles_category_].status); | 227 categories_[articles_category_].status); |
| 226 if (database_->IsErrorState()) { | 228 if (database_->IsErrorState()) { |
| 227 EnterState(State::ERROR_OCCURRED); | 229 EnterState(State::ERROR_OCCURRED); |
| 228 UpdateAllCategoryStatus(CategoryStatus::LOADING_ERROR); | 230 UpdateAllCategoryStatus(CategoryStatus::LOADING_ERROR); |
| 229 return; | 231 return; |
| 230 } | 232 } |
| 231 | 233 |
| 232 // Can be null in tests. | 234 // Can be null in tests. |
| 233 if (history_service) | 235 if (history_service) |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 } | 301 } |
| 300 } | 302 } |
| 301 | 303 |
| 302 CategoryStatus NTPSnippetsService::GetCategoryStatus(Category category) { | 304 CategoryStatus NTPSnippetsService::GetCategoryStatus(Category category) { |
| 303 DCHECK(categories_.find(category) != categories_.end()); | 305 DCHECK(categories_.find(category) != categories_.end()); |
| 304 return categories_[category].status; | 306 return categories_[category].status; |
| 305 } | 307 } |
| 306 | 308 |
| 307 CategoryInfo NTPSnippetsService::GetCategoryInfo(Category category) { | 309 CategoryInfo NTPSnippetsService::GetCategoryInfo(Category category) { |
| 308 DCHECK(categories_.find(category) != categories_.end()); | 310 DCHECK(categories_.find(category) != categories_.end()); |
| 309 // TODO(sfiera): pass back titles for server categories. | 311 const CategoryContent& content = categories_[category]; |
| 310 return CategoryInfo( | 312 return CategoryInfo(content.localized_title, |
| 311 l10n_util::GetStringUTF16(IDS_NTP_ARTICLE_SUGGESTIONS_SECTION_HEADER), | 313 ContentSuggestionsCardLayout::FULL_CARD, |
| 312 ContentSuggestionsCardLayout::FULL_CARD, | 314 /* has_more_button */ false, |
| 313 /* has_more_button */ false, | 315 /* show_if_empty */ true); |
| 314 /* show_if_empty */ true); | |
| 315 } | 316 } |
| 316 | 317 |
| 317 void NTPSnippetsService::DismissSuggestion(const std::string& suggestion_id) { | 318 void NTPSnippetsService::DismissSuggestion(const std::string& suggestion_id) { |
| 318 if (!ready()) | 319 if (!ready()) |
| 319 return; | 320 return; |
| 320 | 321 |
| 321 Category category = GetCategoryFromUniqueID(suggestion_id); | 322 Category category = GetCategoryFromUniqueID(suggestion_id); |
| 322 std::string snippet_id = GetWithinCategoryIDFromUniqueID(suggestion_id); | 323 std::string snippet_id = GetWithinCategoryIDFromUniqueID(suggestion_id); |
| 323 | 324 |
| 324 DCHECK(categories_.find(category) != categories_.end()); | 325 DCHECK(categories_.find(category) != categories_.end()); |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 551 return; | 552 return; |
| 552 | 553 |
| 553 for (auto& item : categories_) { | 554 for (auto& item : categories_) { |
| 554 CategoryContent* content = &item.second; | 555 CategoryContent* content = &item.second; |
| 555 content->provided_by_server = false; | 556 content->provided_by_server = false; |
| 556 } | 557 } |
| 557 | 558 |
| 558 // If snippets were fetched successfully, update our |categories_| from each | 559 // If snippets were fetched successfully, update our |categories_| from each |
| 559 // category provided by the server. | 560 // category provided by the server. |
| 560 if (snippets) { | 561 if (snippets) { |
| 561 for (std::pair<const Category, NTPSnippet::PtrVector>& item : *snippets) { | 562 for (NTPSnippetsFetcher::FetchedCategory& fetched_category : *snippets) { |
| 562 Category category = item.first; | 563 Category category = fetched_category.category; |
| 563 NTPSnippet::PtrVector& new_snippets = item.second; | 564 |
| 565 if (category != articles_category_) { | |
|
tschumann
2016/08/29 09:59:53
note: special casing the articles category like th
Michael van Ouwerkerk
2016/09/08 13:03:27
The form of "category != articles_category_" is us
| |
| 566 categories_[category].localized_title = | |
| 567 fetched_category.localized_title; | |
| 568 } | |
| 564 | 569 |
| 565 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); | 570 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); |
| 566 // TODO(sfiera): histograms for server categories. | 571 // TODO(sfiera): histograms for server categories. |
| 567 // Sparse histogram used because the number of snippets is small (bound by | 572 // Sparse histogram used because the number of snippets is small (bound by |
| 568 // kMaxSnippetCount). | 573 // kMaxSnippetCount). |
| 569 if (category == articles_category_) { | 574 if (category == articles_category_) { |
| 570 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", | 575 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", |
| 571 new_snippets.size()); | 576 fetched_category.snippets.size()); |
| 572 } | 577 } |
| 573 | 578 |
| 574 MergeSnippets(category, std::move(new_snippets)); | 579 MergeSnippets(category, std::move(fetched_category.snippets)); |
| 575 | 580 |
| 576 // If there are more snippets than we want to show, delete the extra ones. | 581 // If there are more snippets than we want to show, delete the extra ones. |
| 577 CategoryContent* content = &categories_[category]; | 582 CategoryContent* content = &categories_[category]; |
| 578 content->provided_by_server = true; | 583 content->provided_by_server = true; |
| 579 if (content->snippets.size() > kMaxSnippetCount) { | 584 if (content->snippets.size() > kMaxSnippetCount) { |
| 580 NTPSnippet::PtrVector to_delete( | 585 NTPSnippet::PtrVector to_delete( |
| 581 std::make_move_iterator(content->snippets.begin() + | 586 std::make_move_iterator(content->snippets.begin() + |
| 582 kMaxSnippetCount), | 587 kMaxSnippetCount), |
| 583 std::make_move_iterator(content->snippets.end())); | 588 std::make_move_iterator(content->snippets.end())); |
| 584 content->snippets.resize(kMaxSnippetCount); | 589 content->snippets.resize(kMaxSnippetCount); |
| (...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1062 } | 1067 } |
| 1063 | 1068 |
| 1064 NTPSnippetsService::CategoryContent::CategoryContent() = default; | 1069 NTPSnippetsService::CategoryContent::CategoryContent() = default; |
| 1065 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) = | 1070 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) = |
| 1066 default; | 1071 default; |
| 1067 NTPSnippetsService::CategoryContent::~CategoryContent() = default; | 1072 NTPSnippetsService::CategoryContent::~CategoryContent() = default; |
| 1068 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent:: | 1073 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent:: |
| 1069 operator=(CategoryContent&&) = default; | 1074 operator=(CategoryContent&&) = default; |
| 1070 | 1075 |
| 1071 } // namespace ntp_snippets | 1076 } // namespace ntp_snippets |
| OLD | NEW |