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/remote/ntp_snippets_service.h" | 5 #include "components/ntp_snippets/remote/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 551 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 562 | 562 |
| 563 // We removed some suggestions, so we want to let the client know about that. | 563 // We removed some suggestions, so we want to let the client know about that. |
| 564 // The fetch might take a long time or not complete so we don't want to wait | 564 // The fetch might take a long time or not complete so we don't want to wait |
| 565 // for its callback. | 565 // for its callback. |
| 566 NotifyNewSuggestions(); | 566 NotifyNewSuggestions(); |
| 567 | 567 |
| 568 FetchSnippetsFromHosts(hosts, /*interactive_request=*/false); | 568 FetchSnippetsFromHosts(hosts, /*interactive_request=*/false); |
| 569 } | 569 } |
| 570 | 570 |
| 571 void NTPSnippetsService::OnFetchFinished( | 571 void NTPSnippetsService::OnFetchFinished( |
| 572 NTPSnippetsFetcher::OptionalSnippets snippets) { | 572 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories) { |
| 573 if (!ready()) | 573 if (!ready()) |
| 574 return; | 574 return; |
| 575 | 575 |
| 576 for (auto& item : categories_) { | 576 for (auto& item : categories_) { |
| 577 CategoryContent* content = &item.second; | 577 CategoryContent* content = &item.second; |
| 578 content->provided_by_server = false; | 578 content->provided_by_server = false; |
| 579 } | 579 } |
| 580 | 580 |
| 581 // Clear up expired dismissed snippets before we use them to filter new ones. | 581 // Clear up expired dismissed snippets before we use them to filter new ones. |
| 582 ClearExpiredDismissedSnippets(); | 582 ClearExpiredDismissedSnippets(); |
| 583 | 583 |
| 584 // If snippets were fetched successfully, update our |categories_| from each | 584 // If snippets were fetched successfully, update our |categories_| from each |
| 585 // category provided by the server. | 585 // category provided by the server. |
| 586 if (snippets) { | 586 if (fetched_categories) { |
| 587 // TODO(jkrcal): A bit hard to understand with so many variables called | 587 // TODO(jkrcal): A bit hard to understand with so many variables called |
| 588 // "*categor*". Isn't here some room for simplification? | 588 // "*categor*". Isn't here some room for simplification? |
| 589 for (NTPSnippetsFetcher::FetchedCategory& fetched_category : *snippets) { | 589 for (NTPSnippetsFetcher::FetchedCategory& fetched_category : |
| 590 *fetched_categories) { | |
| 590 Category category = fetched_category.category; | 591 Category category = fetched_category.category; |
| 591 | 592 |
| 592 // TODO(sfiera): Avoid hard-coding articles category checks in so many | 593 // TODO(sfiera): Avoid hard-coding articles category checks in so many |
| 593 // places. | 594 // places. |
| 594 if (category != articles_category_) { | 595 if (category != articles_category_) { |
| 595 // Only update titles from server-side provided categories. | 596 // Only update titles from server-side provided categories. |
| 596 categories_[category].localized_title = | 597 categories_[category].localized_title = |
| 597 fetched_category.localized_title; | 598 fetched_category.localized_title; |
| 598 } | 599 } |
| 599 categories_[category].provided_by_server = true; | 600 categories_[category].provided_by_server = true; |
| 600 | 601 |
| 601 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); | 602 // TODO(tschumann): Remove this histogram once we only talk to the content |
| 602 // TODO(sfiera): histograms for server categories. | 603 // suggestions cloud backend. |
| 603 // Sparse histogram used because the number of snippets is small (bound by | 604 if (category == articles_category_ && |
| 604 // kMaxSnippetCount). | 605 fetched_category.snippets.size() <= kMaxSnippetCount) { |
|
Marc Treib
2016/10/06 15:36:13
I'd prefer capping at kMaxSnippetCount, rather tha
tschumann
2016/10/06 17:57:42
Done.
| |
| 605 if (category == articles_category_) { | |
| 606 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", | 606 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", |
| 607 fetched_category.snippets.size()); | 607 fetched_category.snippets.size()); |
| 608 } | 608 } |
| 609 ReplaceSnippets(category, std::move(fetched_category.snippets)); | 609 ReplaceSnippets(category, std::move(fetched_category.snippets)); |
| 610 } | 610 } |
| 611 } | 611 } |
| 612 | 612 |
| 613 for (const auto& item : categories_) { | 613 for (const auto& item : categories_) { |
| 614 Category category = item.first; | 614 Category category = item.first; |
| 615 UpdateCategoryStatus(category, CategoryStatus::AVAILABLE); | 615 UpdateCategoryStatus(category, CategoryStatus::AVAILABLE); |
| 616 } | 616 } |
| 617 | 617 |
| 618 // TODO(sfiera): equivalent metrics for non-articles. | 618 // TODO(sfiera): equivalent metrics for non-articles. |
| 619 const CategoryContent& content = categories_[articles_category_]; | 619 const CategoryContent& content = categories_[articles_category_]; |
| 620 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticles", | 620 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticles", |
| 621 content.snippets.size()); | 621 content.snippets.size()); |
| 622 if (content.snippets.empty() && !content.dismissed.empty()) { | 622 if (content.snippets.empty() && !content.dismissed.empty()) { |
| 623 UMA_HISTOGRAM_COUNTS("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded", | 623 UMA_HISTOGRAM_COUNTS("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded", |
| 624 content.dismissed.size()); | 624 content.dismissed.size()); |
| 625 } | 625 } |
| 626 | 626 |
| 627 // TODO(sfiera): notify only when a category changed above. | 627 // TODO(sfiera): notify only when a category changed above. |
| 628 NotifyNewSuggestions(); | 628 NotifyNewSuggestions(); |
| 629 | 629 |
| 630 // Reschedule after a successful fetch. This resets all currently scheduled | 630 // Reschedule after a successful fetch. This resets all currently scheduled |
| 631 // fetches, to make sure the fallback interval triggers only if no wifi fetch | 631 // fetches, to make sure the fallback interval triggers only if no wifi fetch |
| 632 // succeeded, and also that we don't do a background fetch immediately after | 632 // succeeded, and also that we don't do a background fetch immediately after |
| 633 // a user-initiated one. | 633 // a user-initiated one. |
| 634 if (snippets) | 634 if (fetched_categories) |
| 635 RescheduleFetching(true); | 635 RescheduleFetching(true); |
| 636 } | 636 } |
| 637 | 637 |
| 638 void NTPSnippetsService::ArchiveSnippets(Category category, | 638 void NTPSnippetsService::ArchiveSnippets(Category category, |
| 639 NTPSnippet::PtrVector* to_archive) { | 639 NTPSnippet::PtrVector* to_archive) { |
| 640 CategoryContent* content = &categories_[category]; | 640 CategoryContent* content = &categories_[category]; |
| 641 | 641 |
| 642 // TODO(sfiera): handle DB for non-articles too. | 642 // TODO(sfiera): handle DB for non-articles too. |
| 643 if (category == articles_category_) { | 643 if (category == articles_category_) { |
| 644 database_->DeleteSnippets(GetSnippetIDVector(*to_archive)); | 644 database_->DeleteSnippets(GetSnippetIDVector(*to_archive)); |
| (...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1103 } | 1103 } |
| 1104 | 1104 |
| 1105 NTPSnippetsService::CategoryContent::CategoryContent() = default; | 1105 NTPSnippetsService::CategoryContent::CategoryContent() = default; |
| 1106 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) = | 1106 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) = |
| 1107 default; | 1107 default; |
| 1108 NTPSnippetsService::CategoryContent::~CategoryContent() = default; | 1108 NTPSnippetsService::CategoryContent::~CategoryContent() = default; |
| 1109 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent:: | 1109 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent:: |
| 1110 operator=(CategoryContent&&) = default; | 1110 operator=(CategoryContent&&) = default; |
| 1111 | 1111 |
| 1112 } // namespace ntp_snippets | 1112 } // namespace ntp_snippets |
| OLD | NEW |