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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 image_decoder_(std::move(image_decoder)), | 212 image_decoder_(std::move(image_decoder)), |
213 database_(std::move(database)), | 213 database_(std::move(database)), |
214 snippets_status_service_(std::move(status_service)), | 214 snippets_status_service_(std::move(status_service)), |
215 fetch_after_load_(false), | 215 fetch_after_load_(false), |
216 nuke_after_load_(false), | 216 nuke_after_load_(false), |
217 thumbnail_requests_throttler_( | 217 thumbnail_requests_throttler_( |
218 pref_service, | 218 pref_service, |
219 RequestThrottler::RequestType::CONTENT_SUGGESTION_THUMBNAIL) { | 219 RequestThrottler::RequestType::CONTENT_SUGGESTION_THUMBNAIL) { |
220 // Articles category always exists; others will be added as needed. | 220 // Articles category always exists; others will be added as needed. |
221 categories_[articles_category_] = CategoryContent(); | 221 categories_[articles_category_] = CategoryContent(); |
| 222 categories_[articles_category_].localized_title = |
| 223 l10n_util::GetStringUTF16(IDS_NTP_ARTICLE_SUGGESTIONS_SECTION_HEADER); |
222 observer->OnCategoryStatusChanged(this, articles_category_, | 224 observer->OnCategoryStatusChanged(this, articles_category_, |
223 categories_[articles_category_].status); | 225 categories_[articles_category_].status); |
224 if (database_->IsErrorState()) { | 226 if (database_->IsErrorState()) { |
225 EnterState(State::ERROR_OCCURRED); | 227 EnterState(State::ERROR_OCCURRED); |
226 UpdateAllCategoryStatus(CategoryStatus::LOADING_ERROR); | 228 UpdateAllCategoryStatus(CategoryStatus::LOADING_ERROR); |
227 return; | 229 return; |
228 } | 230 } |
229 | 231 |
230 database_->SetErrorCallback(base::Bind(&NTPSnippetsService::OnDatabaseError, | 232 database_->SetErrorCallback(base::Bind(&NTPSnippetsService::OnDatabaseError, |
231 base::Unretained(this))); | 233 base::Unretained(this))); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 } | 295 } |
294 } | 296 } |
295 | 297 |
296 CategoryStatus NTPSnippetsService::GetCategoryStatus(Category category) { | 298 CategoryStatus NTPSnippetsService::GetCategoryStatus(Category category) { |
297 DCHECK(categories_.find(category) != categories_.end()); | 299 DCHECK(categories_.find(category) != categories_.end()); |
298 return categories_[category].status; | 300 return categories_[category].status; |
299 } | 301 } |
300 | 302 |
301 CategoryInfo NTPSnippetsService::GetCategoryInfo(Category category) { | 303 CategoryInfo NTPSnippetsService::GetCategoryInfo(Category category) { |
302 DCHECK(categories_.find(category) != categories_.end()); | 304 DCHECK(categories_.find(category) != categories_.end()); |
303 // TODO(sfiera): pass back titles for server categories. | 305 const CategoryContent& content = categories_[category]; |
304 return CategoryInfo( | 306 return CategoryInfo(content.localized_title, |
305 l10n_util::GetStringUTF16(IDS_NTP_ARTICLE_SUGGESTIONS_SECTION_HEADER), | 307 ContentSuggestionsCardLayout::FULL_CARD, |
306 ContentSuggestionsCardLayout::FULL_CARD, | 308 /* has_more_button */ false, |
307 /* has_more_button */ false, | 309 /* show_if_empty */ true); |
308 /* show_if_empty */ true); | |
309 } | 310 } |
310 | 311 |
311 void NTPSnippetsService::DismissSuggestion(const std::string& suggestion_id) { | 312 void NTPSnippetsService::DismissSuggestion(const std::string& suggestion_id) { |
312 if (!ready()) | 313 if (!ready()) |
313 return; | 314 return; |
314 | 315 |
315 Category category = GetCategoryFromUniqueID(suggestion_id); | 316 Category category = GetCategoryFromUniqueID(suggestion_id); |
316 std::string snippet_id = GetWithinCategoryIDFromUniqueID(suggestion_id); | 317 std::string snippet_id = GetWithinCategoryIDFromUniqueID(suggestion_id); |
317 | 318 |
318 DCHECK(categories_.find(category) != categories_.end()); | 319 DCHECK(categories_.find(category) != categories_.end()); |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 return; | 537 return; |
537 | 538 |
538 for (auto& item : categories_) { | 539 for (auto& item : categories_) { |
539 CategoryContent* content = &item.second; | 540 CategoryContent* content = &item.second; |
540 content->provided_by_server = false; | 541 content->provided_by_server = false; |
541 } | 542 } |
542 | 543 |
543 // If snippets were fetched successfully, update our |categories_| from each | 544 // If snippets were fetched successfully, update our |categories_| from each |
544 // category provided by the server. | 545 // category provided by the server. |
545 if (snippets) { | 546 if (snippets) { |
546 for (std::pair<const Category, NTPSnippet::PtrVector>& item : *snippets) { | 547 for (NTPSnippetsFetcher::FetchedCategory& fetched_category : *snippets) { |
547 Category category = item.first; | 548 Category category = fetched_category.category; |
548 NTPSnippet::PtrVector& new_snippets = item.second; | 549 |
| 550 // TODO(sfiera): Avoid hard-coding articles category checks in so many |
| 551 // places. |
| 552 if (category != articles_category_) { |
| 553 // Only update titles from server-side provided categories. |
| 554 categories_[category].localized_title = |
| 555 fetched_category.localized_title; |
| 556 } |
549 | 557 |
550 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); | 558 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); |
551 // TODO(sfiera): histograms for server categories. | 559 // TODO(sfiera): histograms for server categories. |
552 // Sparse histogram used because the number of snippets is small (bound by | 560 // Sparse histogram used because the number of snippets is small (bound by |
553 // kMaxSnippetCount). | 561 // kMaxSnippetCount). |
554 if (category == articles_category_) { | 562 if (category == articles_category_) { |
555 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", | 563 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", |
556 new_snippets.size()); | 564 fetched_category.snippets.size()); |
557 } | 565 } |
558 | 566 |
559 MergeSnippets(category, std::move(new_snippets)); | 567 MergeSnippets(category, std::move(fetched_category.snippets)); |
560 | 568 |
561 // If there are more snippets than we want to show, delete the extra ones. | 569 // If there are more snippets than we want to show, delete the extra ones. |
562 CategoryContent* content = &categories_[category]; | 570 CategoryContent* content = &categories_[category]; |
563 content->provided_by_server = true; | 571 content->provided_by_server = true; |
564 if (content->snippets.size() > kMaxSnippetCount) { | 572 if (content->snippets.size() > kMaxSnippetCount) { |
565 NTPSnippet::PtrVector to_delete( | 573 NTPSnippet::PtrVector to_delete( |
566 std::make_move_iterator(content->snippets.begin() + | 574 std::make_move_iterator(content->snippets.begin() + |
567 kMaxSnippetCount), | 575 kMaxSnippetCount), |
568 std::make_move_iterator(content->snippets.end())); | 576 std::make_move_iterator(content->snippets.end())); |
569 content->snippets.resize(kMaxSnippetCount); | 577 content->snippets.resize(kMaxSnippetCount); |
(...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1047 } | 1055 } |
1048 | 1056 |
1049 NTPSnippetsService::CategoryContent::CategoryContent() = default; | 1057 NTPSnippetsService::CategoryContent::CategoryContent() = default; |
1050 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) = | 1058 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) = |
1051 default; | 1059 default; |
1052 NTPSnippetsService::CategoryContent::~CategoryContent() = default; | 1060 NTPSnippetsService::CategoryContent::~CategoryContent() = default; |
1053 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent:: | 1061 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent:: |
1054 operator=(CategoryContent&&) = default; | 1062 operator=(CategoryContent&&) = default; |
1055 | 1063 |
1056 } // namespace ntp_snippets | 1064 } // namespace ntp_snippets |
OLD | NEW |