Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(890)

Side by Side Diff: components/ntp_snippets/remote/ntp_snippets_service.cc

Issue 2387293009: Removes a data-dependent DCHECK(). (Closed)
Patch Set: merged to head Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 481 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 ClearOrphanedImages(); 492 ClearOrphanedImages();
493 FinishInitialization(); 493 FinishInitialization();
494 } 494 }
495 495
496 void NTPSnippetsService::OnDatabaseError() { 496 void NTPSnippetsService::OnDatabaseError() {
497 EnterState(State::ERROR_OCCURRED); 497 EnterState(State::ERROR_OCCURRED);
498 UpdateAllCategoryStatus(CategoryStatus::LOADING_ERROR); 498 UpdateAllCategoryStatus(CategoryStatus::LOADING_ERROR);
499 } 499 }
500 500
501 void NTPSnippetsService::OnFetchFinished( 501 void NTPSnippetsService::OnFetchFinished(
502 NTPSnippetsFetcher::OptionalSnippets snippets) { 502 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories) {
503 if (!ready()) 503 if (!ready())
504 return; 504 return;
505 505
506 for (auto& item : categories_) { 506 for (auto& item : categories_) {
507 CategoryContent* content = &item.second; 507 CategoryContent* content = &item.second;
508 content->provided_by_server = false; 508 content->provided_by_server = false;
509 } 509 }
510 510
511 // Clear up expired dismissed snippets before we use them to filter new ones. 511 // Clear up expired dismissed snippets before we use them to filter new ones.
512 ClearExpiredDismissedSnippets(); 512 ClearExpiredDismissedSnippets();
513 513
514 // If snippets were fetched successfully, update our |categories_| from each 514 // If snippets were fetched successfully, update our |categories_| from each
515 // category provided by the server. 515 // category provided by the server.
516 if (snippets) { 516 if (fetched_categories) {
517 // TODO(jkrcal): A bit hard to understand with so many variables called 517 // TODO(jkrcal): A bit hard to understand with so many variables called
518 // "*categor*". Isn't here some room for simplification? 518 // "*categor*". Isn't here some room for simplification?
519 for (NTPSnippetsFetcher::FetchedCategory& fetched_category : *snippets) { 519 for (NTPSnippetsFetcher::FetchedCategory& fetched_category :
520 *fetched_categories) {
520 Category category = fetched_category.category; 521 Category category = fetched_category.category;
521 522
522 // TODO(sfiera): Avoid hard-coding articles category checks in so many 523 // TODO(sfiera): Avoid hard-coding articles category checks in so many
523 // places. 524 // places.
524 if (category != articles_category_) { 525 if (category != articles_category_) {
525 // Only update titles from server-side provided categories. 526 // Only update titles from server-side provided categories.
526 categories_[category].localized_title = 527 categories_[category].localized_title =
527 fetched_category.localized_title; 528 fetched_category.localized_title;
528 } 529 }
529 categories_[category].provided_by_server = true; 530 categories_[category].provided_by_server = true;
530 531
531 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); 532 // TODO(tschumann): Remove this histogram once we only talk to the content
532 // TODO(sfiera): histograms for server categories. 533 // suggestions cloud backend.
533 // Sparse histogram used because the number of snippets is small (bound by
534 // kMaxSnippetCount).
535 if (category == articles_category_) { 534 if (category == articles_category_) {
536 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", 535 UMA_HISTOGRAM_SPARSE_SLOWLY(
537 fetched_category.snippets.size()); 536 "NewTabPage.Snippets.NumArticlesFetched",
537 std::min(fetched_category.snippets.size(),
538 static_cast<size_t>(kMaxSnippetCount + 1)));
538 } 539 }
539 ReplaceSnippets(category, std::move(fetched_category.snippets)); 540 ReplaceSnippets(category, std::move(fetched_category.snippets));
540 } 541 }
541 } 542 }
542 543
543 for (const auto& item : categories_) { 544 for (const auto& item : categories_) {
544 Category category = item.first; 545 Category category = item.first;
545 UpdateCategoryStatus(category, CategoryStatus::AVAILABLE); 546 UpdateCategoryStatus(category, CategoryStatus::AVAILABLE);
546 } 547 }
547 548
548 // TODO(sfiera): equivalent metrics for non-articles. 549 // TODO(sfiera): equivalent metrics for non-articles.
549 const CategoryContent& content = categories_[articles_category_]; 550 const CategoryContent& content = categories_[articles_category_];
550 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticles", 551 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticles",
551 content.snippets.size()); 552 content.snippets.size());
552 if (content.snippets.empty() && !content.dismissed.empty()) { 553 if (content.snippets.empty() && !content.dismissed.empty()) {
553 UMA_HISTOGRAM_COUNTS("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded", 554 UMA_HISTOGRAM_COUNTS("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded",
554 content.dismissed.size()); 555 content.dismissed.size());
555 } 556 }
556 557
557 // TODO(sfiera): notify only when a category changed above. 558 // TODO(sfiera): notify only when a category changed above.
558 NotifyNewSuggestions(); 559 NotifyNewSuggestions();
559 560
560 // Reschedule after a successful fetch. This resets all currently scheduled 561 // Reschedule after a successful fetch. This resets all currently scheduled
561 // fetches, to make sure the fallback interval triggers only if no wifi fetch 562 // fetches, to make sure the fallback interval triggers only if no wifi fetch
562 // succeeded, and also that we don't do a background fetch immediately after 563 // succeeded, and also that we don't do a background fetch immediately after
563 // a user-initiated one. 564 // a user-initiated one.
564 if (snippets) 565 if (fetched_categories)
565 RescheduleFetching(true); 566 RescheduleFetching(true);
566 } 567 }
567 568
568 void NTPSnippetsService::ArchiveSnippets(Category category, 569 void NTPSnippetsService::ArchiveSnippets(Category category,
569 NTPSnippet::PtrVector* to_archive) { 570 NTPSnippet::PtrVector* to_archive) {
570 CategoryContent* content = &categories_[category]; 571 CategoryContent* content = &categories_[category];
571 572
572 // TODO(sfiera): handle DB for non-articles too. 573 // TODO(sfiera): handle DB for non-articles too.
573 if (category == articles_category_) { 574 if (category == articles_category_) {
574 database_->DeleteSnippets(GetSnippetIDVector(*to_archive)); 575 database_->DeleteSnippets(GetSnippetIDVector(*to_archive));
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after
1003 } 1004 }
1004 1005
1005 NTPSnippetsService::CategoryContent::CategoryContent() = default; 1006 NTPSnippetsService::CategoryContent::CategoryContent() = default;
1006 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) = 1007 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) =
1007 default; 1008 default;
1008 NTPSnippetsService::CategoryContent::~CategoryContent() = default; 1009 NTPSnippetsService::CategoryContent::~CategoryContent() = default;
1009 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent:: 1010 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent::
1010 operator=(CategoryContent&&) = default; 1011 operator=(CategoryContent&&) = default;
1011 1012
1012 } // namespace ntp_snippets 1013 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/remote/ntp_snippets_service.h ('k') | components/ntp_snippets/remote/ntp_snippets_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698