history_clusters_service_task_update_clusters.h 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #ifndef COMPONENTS_HISTORY_CLUSTERS_CORE_HISTORY_CLUSTERS_SERVICE_TASK_UPDATE_CLUSTERS_H_
  5. #define COMPONENTS_HISTORY_CLUSTERS_CORE_HISTORY_CLUSTERS_SERVICE_TASK_UPDATE_CLUSTERS_H_
  6. #include <vector>
  7. #include "base/memory/weak_ptr.h"
  8. #include "base/task/cancelable_task_tracker.h"
  9. #include "base/time/time.h"
  10. #include "components/history/core/browser/history_types.h"
  11. #include "components/history_clusters/core/clustering_backend.h"
  12. #include "components/history_clusters/core/history_clusters_types.h"
  13. namespace history {
  14. class HistoryService;
  15. }
  16. namespace history_clusters {
  17. // `HistoryClustersServiceTaskUpdateClusters` gets clustered and unclustered
  18. // visits straddling the threshold and clusters them together. It continues
  19. // doing so, moving the threshold forward 1 day each time, until reaching today.
  20. // When re-clustering clustered visits, it takes all visits in their clusters
  21. // and replaces those clusters. This allows existing clusters to grow without
  22. // having to cluster an impractical number of visits simultaneously and without
  23. // creating near-duplicate clusters. The similar
  24. // `HistoryClustersServiceTaskGetMostRecentClusters` will consume the clusters
  25. // this creates. In contrast to this,
  26. // `HistoryClustersServiceTaskGetMostRecentClusters` iterates recent visits 1st
  27. // and does not persist them.
  28. class HistoryClustersServiceTaskUpdateClusters {
  29. public:
  30. HistoryClustersServiceTaskUpdateClusters(
  31. const IncompleteVisitMap incomplete_visit_context_annotations,
  32. ClusteringBackend* const backend,
  33. history::HistoryService* const history_service,
  34. base::OnceClosure callback);
  35. ~HistoryClustersServiceTaskUpdateClusters();
  36. bool Done() { return done_; }
  37. private:
  38. // When there remain unclustered visits, cluster them (possibly in combination
  39. // with clustered visits) and persist the newly created clusters:
  40. // Start() ->
  41. // GetAnnotatedVisitsToCluster() ->
  42. // OnGotModelClusters()
  43. // Invoked during construction and after `OnGotModelClusters()` asyncly
  44. // replaces clusters. Will asyncly request annotated visits from
  45. // `GetAnnotatedVisitsToCluster`. May instead syncly invoke `callback_` if
  46. // there's no `ClusteringBackend` or all visits are exhausted.
  47. void Start();
  48. // Invoked after `Start()` asyncly fetches annotated visits. Will asyncly
  49. // request clusters from `ClusteringBackend`. May instead syncly invoke
  50. // `callback_` if no annotated visits were fetched.
  51. void OnGotAnnotatedVisitsToCluster(
  52. std::vector<int64_t> old_clusters,
  53. std::vector<history::AnnotatedVisit> annotated_visits,
  54. QueryClustersContinuationParams continuation_params);
  55. // Invoked after `OnGotAnnotatedVisitsToCluster()` asyncly obtains clusters.
  56. // Will asyncly request `old_cluster_ids` be replaced with `clusters`.
  57. void OnGotModelClusters(std::vector<int64_t> old_cluster_ids,
  58. QueryClustersContinuationParams continuation_params,
  59. std::vector<history::Cluster> clusters);
  60. const IncompleteVisitMap incomplete_visit_context_annotations_;
  61. // Can be nullptr.
  62. ClusteringBackend* const backend_;
  63. // Non-owning pointer, but never nullptr.
  64. history::HistoryService* const history_service_;
  65. // Used to make requests to `GetAnnotatedVisitsToCluster` and
  66. // `HistoryService`.
  67. QueryClustersContinuationParams continuation_params_;
  68. base::CancelableTaskTracker task_tracker_;
  69. // Invoked after either `Start()` or `OnGotAnnotatedVisitsToCluster()`.
  70. base::OnceClosure callback_;
  71. // Set to true when `callback_` is invoked, either with clusters or no
  72. // clusters.
  73. bool done_ = false;
  74. // Used for async callbacks.
  75. base::WeakPtrFactory<HistoryClustersServiceTaskUpdateClusters>
  76. weak_ptr_factory_{this};
  77. };
  78. } // namespace history_clusters
  79. #endif // COMPONENTS_HISTORY_CLUSTERS_CORE_HISTORY_CLUSTERS_SERVICE_TASK_UPDATE_CLUSTERS_H_