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

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

Issue 2379113002: Extended the ProtoDatabase to provide LoadKeys() functionality. (Closed)
Patch Set: Garbage Collect orphaned images on service start-up. 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
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h" 13 #include "base/files/file_util.h"
14 #include "base/location.h" 14 #include "base/location.h"
15 #include "base/memory/ptr_util.h"
15 #include "base/metrics/histogram_macros.h" 16 #include "base/metrics/histogram_macros.h"
16 #include "base/metrics/sparse_histogram.h" 17 #include "base/metrics/sparse_histogram.h"
17 #include "base/path_service.h" 18 #include "base/path_service.h"
18 #include "base/stl_util.h" 19 #include "base/stl_util.h"
19 #include "base/strings/string_number_conversions.h" 20 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/utf_string_conversions.h" 21 #include "base/strings/utf_string_conversions.h"
21 #include "base/task_runner_util.h" 22 #include "base/task_runner_util.h"
22 #include "base/time/time.h" 23 #include "base/time/time.h"
23 #include "base/values.h" 24 #include "base/values.h"
24 #include "components/data_use_measurement/core/data_use_user_data.h" 25 #include "components/data_use_measurement/core/data_use_user_data.h"
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 database_->SaveImage(id_within_category, image_data); 484 database_->SaveImage(id_within_category, image_data);
484 } 485 }
485 486
486 void NTPSnippetsService::OnDatabaseLoaded(NTPSnippet::PtrVector snippets) { 487 void NTPSnippetsService::OnDatabaseLoaded(NTPSnippet::PtrVector snippets) {
487 if (state_ == State::ERROR_OCCURRED) 488 if (state_ == State::ERROR_OCCURRED)
488 return; 489 return;
489 DCHECK(state_ == State::NOT_INITED); 490 DCHECK(state_ == State::NOT_INITED);
490 DCHECK_EQ(1u, categories_.size()); // Only articles category, so far. 491 DCHECK_EQ(1u, categories_.size()); // Only articles category, so far.
491 DCHECK(categories_.find(articles_category_) != categories_.end()); 492 DCHECK(categories_.find(articles_category_) != categories_.end());
492 493
494 ClearExpiredDismissedSnippets();
495 ClearOrphanedImages(snippets);
496
493 // TODO(sfiera): support non-article categories in database. 497 // TODO(sfiera): support non-article categories in database.
494 CategoryContent* content = &categories_[articles_category_]; 498 CategoryContent* content = &categories_[articles_category_];
495 for (std::unique_ptr<NTPSnippet>& snippet : snippets) { 499 for (std::unique_ptr<NTPSnippet>& snippet : snippets) {
496 if (snippet->is_dismissed()) 500 if (snippet->is_dismissed())
497 content->dismissed.emplace_back(std::move(snippet)); 501 content->dismissed.emplace_back(std::move(snippet));
498 else 502 else
499 content->snippets.emplace_back(std::move(snippet)); 503 content->snippets.emplace_back(std::move(snippet));
500 } 504 }
501 505
502 std::sort(content->snippets.begin(), content->snippets.end(), 506 std::sort(content->snippets.begin(), content->snippets.end(),
503 [](const std::unique_ptr<NTPSnippet>& lhs, 507 [](const std::unique_ptr<NTPSnippet>& lhs,
504 const std::unique_ptr<NTPSnippet>& rhs) { 508 const std::unique_ptr<NTPSnippet>& rhs) {
505 return lhs->score() > rhs->score(); 509 return lhs->score() > rhs->score();
506 }); 510 });
507 511
508 ClearExpiredDismissedSnippets();
509 ClearOrphanedImages();
510 FinishInitialization(); 512 FinishInitialization();
511 } 513 }
512 514
513 void NTPSnippetsService::OnDatabaseError() { 515 void NTPSnippetsService::OnDatabaseError() {
514 EnterState(State::ERROR_OCCURRED); 516 EnterState(State::ERROR_OCCURRED);
515 UpdateAllCategoryStatus(CategoryStatus::LOADING_ERROR); 517 UpdateAllCategoryStatus(CategoryStatus::LOADING_ERROR);
516 } 518 }
517 519
518 // TODO(dgn): name clash between content suggestions and suggestions hosts. 520 // TODO(dgn): name clash between content suggestions and suggestions hosts.
519 // method name should be changed. 521 // method name should be changed.
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 categories_[category].provided_by_server = true; 586 categories_[category].provided_by_server = true;
585 587
586 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); 588 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount));
587 // TODO(sfiera): histograms for server categories. 589 // TODO(sfiera): histograms for server categories.
588 // Sparse histogram used because the number of snippets is small (bound by 590 // Sparse histogram used because the number of snippets is small (bound by
589 // kMaxSnippetCount). 591 // kMaxSnippetCount).
590 if (category == articles_category_) { 592 if (category == articles_category_) {
591 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", 593 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched",
592 fetched_category.snippets.size()); 594 fetched_category.snippets.size());
593 } 595 }
594
595 ReplaceSnippets(category, std::move(fetched_category.snippets)); 596 ReplaceSnippets(category, std::move(fetched_category.snippets));
596 } 597 }
597 } 598 }
598 599
599 for (const auto& item : categories_) { 600 for (const auto& item : categories_) {
600 Category category = item.first; 601 Category category = item.first;
601 UpdateCategoryStatus(category, CategoryStatus::AVAILABLE); 602 UpdateCategoryStatus(category, CategoryStatus::AVAILABLE);
602 } 603 }
603 604
604 // TODO(sfiera): equivalent metrics for non-articles. 605 // TODO(sfiera): equivalent metrics for non-articles.
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
1062 } 1068 }
1063 1069
1064 NTPSnippetsService::CategoryContent::CategoryContent() = default; 1070 NTPSnippetsService::CategoryContent::CategoryContent() = default;
1065 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) = 1071 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) =
1066 default; 1072 default;
1067 NTPSnippetsService::CategoryContent::~CategoryContent() = default; 1073 NTPSnippetsService::CategoryContent::~CategoryContent() = default;
1068 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent:: 1074 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent::
1069 operator=(CategoryContent&&) = default; 1075 operator=(CategoryContent&&) = default;
1070 1076
1071 } // namespace ntp_snippets 1077 } // 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