browsing_topics_calculator.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_BROWSING_TOPICS_BROWSING_TOPICS_CALCULATOR_H_
  5. #define COMPONENTS_BROWSING_TOPICS_BROWSING_TOPICS_CALCULATOR_H_
  6. #include <map>
  7. #include <set>
  8. #include "base/callback.h"
  9. #include "base/containers/queue.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/task/cancelable_task_tracker.h"
  12. #include "base/time/time.h"
  13. #include "components/browsing_topics/common/common_types.h"
  14. #include "components/browsing_topics/epoch_topics.h"
  15. #include "components/history/core/browser/history_types.h"
  16. namespace privacy_sandbox {
  17. class PrivacySandboxSettings;
  18. } // namespace privacy_sandbox
  19. namespace history {
  20. class HistoryService;
  21. } // namespace history
  22. namespace content {
  23. class BrowsingTopicsSiteDataManager;
  24. } // namespace content
  25. namespace optimization_guide {
  26. class PageContentAnnotationsService;
  27. class BatchAnnotationResult;
  28. } // namespace optimization_guide
  29. namespace browsing_topics {
  30. // Responsible for doing a one-off browsing topics calculation. It will:
  31. // 1) Check the user settings for calculation permissions.
  32. // 2) Query the `BrowsingTopicsSiteDataManager` for the contexts where the
  33. // Topics API was called on.
  34. // 3) Query the `HistoryService` for the hosts of the pages the API was called
  35. // on.
  36. // 4) Query the `PageContentAnnotationsService` with a set of hosts, to get the
  37. // corresponding topics.
  38. // 5) Derive `EpochTopics` (i.e. the top topics and the their observed-by
  39. // contexts), and return it as the final result.
  40. class BrowsingTopicsCalculator {
  41. public:
  42. // These values are persisted to logs. Entries should not be renumbered and
  43. // numeric values should never be reused.
  44. enum class CalculatorResultStatus {
  45. kSuccess = 0,
  46. kFailurePermissionDenied = 1,
  47. kFailureApiUsageContextQueryError = 2,
  48. kFailureAnnotationExecutionError = 3,
  49. kFailureTaxonomyVersionNotSupportedInBinary = 4,
  50. kMaxValue = kFailureTaxonomyVersionNotSupportedInBinary,
  51. };
  52. using CalculateCompletedCallback = base::OnceCallback<void(EpochTopics)>;
  53. BrowsingTopicsCalculator(
  54. privacy_sandbox::PrivacySandboxSettings* privacy_sandbox_settings,
  55. history::HistoryService* history_service,
  56. content::BrowsingTopicsSiteDataManager* site_data_manager,
  57. optimization_guide::PageContentAnnotationsService* annotations_service,
  58. const base::circular_deque<EpochTopics>& epochs,
  59. CalculateCompletedCallback callback);
  60. BrowsingTopicsCalculator(const BrowsingTopicsCalculator&) = delete;
  61. BrowsingTopicsCalculator& operator=(const BrowsingTopicsCalculator&) = delete;
  62. BrowsingTopicsCalculator(BrowsingTopicsCalculator&&) = delete;
  63. BrowsingTopicsCalculator& operator=(BrowsingTopicsCalculator&&) = delete;
  64. virtual ~BrowsingTopicsCalculator();
  65. protected:
  66. // This method exists for the purposes of overriding in tests.
  67. virtual uint64_t GenerateRandUint64();
  68. virtual void CheckCanCalculate();
  69. private:
  70. // Get the top `kBrowsingTopicsNumberOfTopTopicsPerEpoch` topics. If there
  71. // aren't enough topics, pad with random ones. Return the result topics, and
  72. // the starting index of the padded topics (or
  73. // `kBrowsingTopicsNumberOfTopTopicsPerEpoch` if there's no padded topics).
  74. // Precondition: the hosts in `history_hosts_count` should exist in
  75. // `host_topics_map`.
  76. void DeriveTopTopics(
  77. const std::map<HashedHost, size_t>& history_hosts_count,
  78. const std::map<HashedHost, std::set<Topic>>& host_topics_map,
  79. size_t taxonomy_size,
  80. std::vector<Topic>& top_topics,
  81. size_t& padded_top_topics_start_index);
  82. void OnGetRecentBrowsingTopicsApiUsagesCompleted(
  83. browsing_topics::ApiUsageContextQueryResult result);
  84. void OnGetRecentlyVisitedURLsCompleted(history::QueryResults results);
  85. void OnRequestModelCompleted(std::vector<std::string> raw_hosts,
  86. bool successful);
  87. void OnGetTopicsForHostsCompleted(
  88. const std::vector<optimization_guide::BatchAnnotationResult>& results);
  89. void OnCalculateCompleted(CalculatorResultStatus status,
  90. EpochTopics epoch_topics);
  91. // Those pointers are safe to hold and use throughout the lifetime of
  92. // `BrowsingTopicsService`, which owns this object.
  93. raw_ptr<privacy_sandbox::PrivacySandboxSettings> privacy_sandbox_settings_;
  94. raw_ptr<history::HistoryService> history_service_;
  95. raw_ptr<content::BrowsingTopicsSiteDataManager> site_data_manager_;
  96. raw_ptr<optimization_guide::PageContentAnnotationsService>
  97. annotations_service_;
  98. CalculateCompletedCallback calculate_completed_callback_;
  99. // The calculation start time.
  100. base::Time calculation_time_;
  101. base::Time history_data_start_time_;
  102. base::Time api_usage_context_data_start_time_;
  103. // The history hosts over
  104. // `kBrowsingTopicsNumberOfEpochsOfObservationDataToUseForFiltering` epochs,
  105. // and the calling context domains that used the Topics API in each main frame
  106. // host.
  107. std::map<HashedHost, std::vector<HashedDomain>> host_context_domains_map_;
  108. // The hashed history hosts and their count over the last epoch.
  109. std::map<HashedHost, size_t> history_hosts_count_;
  110. // Used for the async tasks querying the HistoryService.
  111. base::CancelableTaskTracker history_task_tracker_;
  112. base::WeakPtrFactory<BrowsingTopicsCalculator> weak_ptr_factory_{this};
  113. };
  114. } // namespace browsing_topics
  115. #endif // COMPONENTS_BROWSING_TOPICS_BROWSING_TOPICS_CALCULATOR_H_