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 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/memory/ptr_util.h" | |
| 13 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 14 #include "base/metrics/sparse_histogram.h" | 15 #include "base/metrics/sparse_histogram.h" |
| 15 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 16 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/task_runner_util.h" | 20 #include "base/task_runner_util.h" |
| 20 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 21 #include "base/values.h" | 22 #include "base/values.h" |
| 22 #include "components/data_use_measurement/core/data_use_user_data.h" | 23 #include "components/data_use_measurement/core/data_use_user_data.h" |
| (...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 485 database_->SaveImage(id_within_category, image_data); | 486 database_->SaveImage(id_within_category, image_data); |
| 486 } | 487 } |
| 487 | 488 |
| 488 void NTPSnippetsService::OnDatabaseLoaded(NTPSnippet::PtrVector snippets) { | 489 void NTPSnippetsService::OnDatabaseLoaded(NTPSnippet::PtrVector snippets) { |
| 489 if (state_ == State::ERROR_OCCURRED) | 490 if (state_ == State::ERROR_OCCURRED) |
| 490 return; | 491 return; |
| 491 DCHECK(state_ == State::NOT_INITED); | 492 DCHECK(state_ == State::NOT_INITED); |
| 492 DCHECK_EQ(1u, categories_.size()); // Only articles category, so far. | 493 DCHECK_EQ(1u, categories_.size()); // Only articles category, so far. |
| 493 DCHECK(categories_.find(articles_category_) != categories_.end()); | 494 DCHECK(categories_.find(articles_category_) != categories_.end()); |
| 494 | 495 |
| 496 ClearExpiredDismissedSnippets(); | |
|
Marc Treib
2016/10/06 10:21:30
This won't do anything, since |categories_| is sti
tschumann
2016/10/06 10:56:11
are you sure? we have a DCHECK() above verifying i
Marc Treib
2016/10/06 11:37:20
Ah okay, correction: The category itself will be t
tschumann
2016/10/06 12:21:44
nope -- you're not :-)
added a TODO() to increase
| |
| 497 ClearOrphanedImages(snippets); | |
| 498 | |
| 495 // TODO(sfiera): support non-article categories in database. | 499 // TODO(sfiera): support non-article categories in database. |
| 496 CategoryContent* content = &categories_[articles_category_]; | 500 CategoryContent* content = &categories_[articles_category_]; |
| 497 for (std::unique_ptr<NTPSnippet>& snippet : snippets) { | 501 for (std::unique_ptr<NTPSnippet>& snippet : snippets) { |
| 498 if (snippet->is_dismissed()) | 502 if (snippet->is_dismissed()) |
| 499 content->dismissed.emplace_back(std::move(snippet)); | 503 content->dismissed.emplace_back(std::move(snippet)); |
| 500 else | 504 else |
| 501 content->snippets.emplace_back(std::move(snippet)); | 505 content->snippets.emplace_back(std::move(snippet)); |
| 502 } | 506 } |
| 503 | 507 |
| 504 std::sort(content->snippets.begin(), content->snippets.end(), | 508 std::sort(content->snippets.begin(), content->snippets.end(), |
| 505 [](const std::unique_ptr<NTPSnippet>& lhs, | 509 [](const std::unique_ptr<NTPSnippet>& lhs, |
| 506 const std::unique_ptr<NTPSnippet>& rhs) { | 510 const std::unique_ptr<NTPSnippet>& rhs) { |
| 507 return lhs->score() > rhs->score(); | 511 return lhs->score() > rhs->score(); |
| 508 }); | 512 }); |
| 509 | 513 |
| 510 ClearExpiredDismissedSnippets(); | |
| 511 ClearOrphanedImages(); | |
| 512 FinishInitialization(); | 514 FinishInitialization(); |
| 513 } | 515 } |
| 514 | 516 |
| 515 void NTPSnippetsService::OnDatabaseError() { | 517 void NTPSnippetsService::OnDatabaseError() { |
| 516 EnterState(State::ERROR_OCCURRED); | 518 EnterState(State::ERROR_OCCURRED); |
| 517 UpdateAllCategoryStatus(CategoryStatus::LOADING_ERROR); | 519 UpdateAllCategoryStatus(CategoryStatus::LOADING_ERROR); |
| 518 } | 520 } |
| 519 | 521 |
| 520 // TODO(dgn): name clash between content suggestions and suggestions hosts. | 522 // TODO(dgn): name clash between content suggestions and suggestions hosts. |
| 521 // method name should be changed. | 523 // method name should be changed. |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 586 categories_[category].provided_by_server = true; | 588 categories_[category].provided_by_server = true; |
| 587 | 589 |
| 588 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); | 590 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); |
| 589 // TODO(sfiera): histograms for server categories. | 591 // TODO(sfiera): histograms for server categories. |
| 590 // Sparse histogram used because the number of snippets is small (bound by | 592 // Sparse histogram used because the number of snippets is small (bound by |
| 591 // kMaxSnippetCount). | 593 // kMaxSnippetCount). |
| 592 if (category == articles_category_) { | 594 if (category == articles_category_) { |
| 593 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", | 595 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", |
| 594 fetched_category.snippets.size()); | 596 fetched_category.snippets.size()); |
| 595 } | 597 } |
| 596 | |
| 597 ReplaceSnippets(category, std::move(fetched_category.snippets)); | 598 ReplaceSnippets(category, std::move(fetched_category.snippets)); |
| 598 } | 599 } |
| 599 } | 600 } |
| 600 | 601 |
| 601 for (const auto& item : categories_) { | 602 for (const auto& item : categories_) { |
| 602 Category category = item.first; | 603 Category category = item.first; |
| 603 UpdateCategoryStatus(category, CategoryStatus::AVAILABLE); | 604 UpdateCategoryStatus(category, CategoryStatus::AVAILABLE); |
| 604 } | 605 } |
| 605 | 606 |
| 606 // TODO(sfiera): equivalent metrics for non-articles. | 607 // TODO(sfiera): equivalent metrics for non-articles. |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 760 categories_to_erase.push_back(category); | 761 categories_to_erase.push_back(category); |
| 761 } | 762 } |
| 762 } | 763 } |
| 763 | 764 |
| 764 for (Category category : categories_to_erase) { | 765 for (Category category : categories_to_erase) { |
| 765 UpdateCategoryStatus(category, CategoryStatus::NOT_PROVIDED); | 766 UpdateCategoryStatus(category, CategoryStatus::NOT_PROVIDED); |
| 766 categories_.erase(category); | 767 categories_.erase(category); |
| 767 } | 768 } |
| 768 } | 769 } |
| 769 | 770 |
| 770 void NTPSnippetsService::ClearOrphanedImages() { | 771 void NTPSnippetsService::ClearOrphanedImages( |
| 771 // TODO(jkrcal): Implement. crbug.com/649009 | 772 const NTPSnippet::PtrVector& snippets) { |
| 773 auto alive_snippets = base::MakeUnique<std::set<std::string>>(); | |
| 774 for (const auto& snippet_ptr : snippets) { | |
| 775 alive_snippets->insert(snippet_ptr->id()); | |
| 776 } | |
| 777 database_->GarbageCollectImages(std::move(alive_snippets)); | |
| 772 } | 778 } |
| 773 | 779 |
| 774 void NTPSnippetsService::NukeAllSnippets() { | 780 void NTPSnippetsService::NukeAllSnippets() { |
| 775 std::vector<Category> categories_to_erase; | 781 std::vector<Category> categories_to_erase; |
| 776 | 782 |
| 777 // Empty the ARTICLES category and remove all others, since they may or may | 783 // Empty the ARTICLES category and remove all others, since they may or may |
| 778 // not be personalized. | 784 // not be personalized. |
| 779 for (const auto& item : categories_) { | 785 for (const auto& item : categories_) { |
| 780 Category category = item.first; | 786 Category category = item.first; |
| 781 | 787 |
| (...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1084 } | 1090 } |
| 1085 | 1091 |
| 1086 NTPSnippetsService::CategoryContent::CategoryContent() = default; | 1092 NTPSnippetsService::CategoryContent::CategoryContent() = default; |
| 1087 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) = | 1093 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) = |
| 1088 default; | 1094 default; |
| 1089 NTPSnippetsService::CategoryContent::~CategoryContent() = default; | 1095 NTPSnippetsService::CategoryContent::~CategoryContent() = default; |
| 1090 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent:: | 1096 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent:: |
| 1091 operator=(CategoryContent&&) = default; | 1097 operator=(CategoryContent&&) = default; |
| 1092 | 1098 |
| 1093 } // namespace ntp_snippets | 1099 } // namespace ntp_snippets |
| OLD | NEW |