history_clusters_service_task_update_clusters.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "history_clusters_service_task_update_clusters.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "components/history/core/browser/history_service.h"
  8. #include "components/history_clusters/core/clustering_backend.h"
  9. #include "components/history_clusters/core/history_clusters_db_tasks.h"
  10. namespace history_clusters {
  11. HistoryClustersServiceTaskUpdateClusters::
  12. HistoryClustersServiceTaskUpdateClusters(
  13. const IncompleteVisitMap incomplete_visit_context_annotations,
  14. ClusteringBackend* const backend,
  15. history::HistoryService* const history_service,
  16. base::OnceClosure callback)
  17. : incomplete_visit_context_annotations_(
  18. incomplete_visit_context_annotations),
  19. backend_(backend),
  20. history_service_(history_service),
  21. callback_(std::move(callback)) {
  22. Start();
  23. }
  24. HistoryClustersServiceTaskUpdateClusters::
  25. ~HistoryClustersServiceTaskUpdateClusters() = default;
  26. void HistoryClustersServiceTaskUpdateClusters::Start() {
  27. if (!backend_ || continuation_params_.exhausted_all_visits) {
  28. done_ = true;
  29. std::move(callback_).Run();
  30. return;
  31. }
  32. history_service_->ScheduleDBTask(
  33. FROM_HERE,
  34. std::make_unique<GetAnnotatedVisitsToCluster>(
  35. incomplete_visit_context_annotations_, base::Time(),
  36. continuation_params_, false, 2, false,
  37. base::BindOnce(&HistoryClustersServiceTaskUpdateClusters::
  38. OnGotAnnotatedVisitsToCluster,
  39. weak_ptr_factory_.GetWeakPtr())),
  40. &task_tracker_);
  41. }
  42. void HistoryClustersServiceTaskUpdateClusters::OnGotAnnotatedVisitsToCluster(
  43. std::vector<int64_t> old_clusters,
  44. std::vector<history::AnnotatedVisit> annotated_visits,
  45. QueryClustersContinuationParams continuation_params) {
  46. if (annotated_visits.empty()) {
  47. DCHECK(continuation_params.exhausted_all_visits);
  48. done_ = true;
  49. std::move(callback_).Run();
  50. return;
  51. }
  52. // Using `kKeywordCacheGeneration` as that only determines the task priority.
  53. backend_->GetClusters(
  54. ClusteringRequestSource::kKeywordCacheGeneration,
  55. base::BindOnce(
  56. &HistoryClustersServiceTaskUpdateClusters::OnGotModelClusters,
  57. weak_ptr_factory_.GetWeakPtr(), old_clusters, continuation_params),
  58. annotated_visits);
  59. }
  60. void HistoryClustersServiceTaskUpdateClusters::OnGotModelClusters(
  61. std::vector<int64_t> old_cluster_ids,
  62. QueryClustersContinuationParams continuation_params,
  63. std::vector<history::Cluster> clusters) {
  64. continuation_params_ = continuation_params;
  65. history_service_->ReplaceClusters(
  66. old_cluster_ids, clusters,
  67. base::BindOnce(&HistoryClustersServiceTaskUpdateClusters::Start,
  68. weak_ptr_factory_.GetWeakPtr()),
  69. &task_tracker_);
  70. }
  71. } // namespace history_clusters