1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_BROWSING_TOPICS_BROWSING_TOPICS_CALCULATOR_H_
6 #define COMPONENTS_BROWSING_TOPICS_BROWSING_TOPICS_CALCULATOR_H_
11 #include "base/containers/queue.h"
12 #include "base/functional/callback.h"
13 #include "base/memory/raw_ptr.h"
14 #include "base/task/cancelable_task_tracker.h"
15 #include "base/time/time.h"
16 #include "components/browsing_topics/annotator.h"
17 #include "components/browsing_topics/common/common_types.h"
18 #include "components/browsing_topics/epoch_topics.h"
19 #include "components/history/core/browser/history_types.h"
21 namespace privacy_sandbox {
22 class PrivacySandboxSettings;
23 } // namespace privacy_sandbox
27 } // namespace history
30 class BrowsingTopicsSiteDataManager;
31 } // namespace content
33 namespace browsing_topics {
35 // Responsible for doing a one-off browsing topics calculation. It will:
36 // 1) Check the user settings for calculation permissions.
37 // 2) Query the `BrowsingTopicsSiteDataManager` for the contexts where the
38 // Topics API was called on.
39 // 3) Query the `HistoryService` for the hosts of the pages the API was called
41 // 4) Query the `Annotator` with a set of hosts, to get the corresponding
43 // 5) Derive `EpochTopics` (i.e. the top topics and the their observed-by
44 // contexts), and return it as the final result.
45 class BrowsingTopicsCalculator {
47 // These values are persisted to logs. Entries should not be renumbered and
48 // numeric values should never be reused.
49 enum class CalculatorResultStatus {
51 kFailurePermissionDenied = 1,
52 kFailureApiUsageContextQueryError = 2,
53 kFailureAnnotationExecutionError = 3,
54 kFailureTaxonomyVersionNotSupportedInBinary = 4,
56 kMaxValue = kFailureTaxonomyVersionNotSupportedInBinary,
59 using CalculateCompletedCallback = base::OnceCallback<void(EpochTopics)>;
61 BrowsingTopicsCalculator(
62 privacy_sandbox::PrivacySandboxSettings* privacy_sandbox_settings,
63 history::HistoryService* history_service,
64 content::BrowsingTopicsSiteDataManager* site_data_manager,
66 const base::circular_deque<EpochTopics>& epochs,
67 bool is_manually_triggered,
68 CalculateCompletedCallback callback);
70 BrowsingTopicsCalculator(const BrowsingTopicsCalculator&) = delete;
71 BrowsingTopicsCalculator& operator=(const BrowsingTopicsCalculator&) = delete;
72 BrowsingTopicsCalculator(BrowsingTopicsCalculator&&) = delete;
73 BrowsingTopicsCalculator& operator=(BrowsingTopicsCalculator&&) = delete;
75 virtual ~BrowsingTopicsCalculator();
77 bool is_manually_triggered() const { return is_manually_triggered_; }
80 // This method exists for the purposes of overriding in tests.
81 virtual uint64_t GenerateRandUint64();
82 virtual void CheckCanCalculate();
85 // Get the top `kBrowsingTopicsNumberOfTopTopicsPerEpoch` topics. If there
86 // aren't enough topics, pad with random ones. Return the result topics, and
87 // the starting index of the padded topics (or
88 // `kBrowsingTopicsNumberOfTopTopicsPerEpoch` if there's no padded topics),
89 // and the number of topics associated with `history_hosts_count`.
91 const std::map<HashedHost, size_t>& history_hosts_count,
92 const std::map<HashedHost, std::set<Topic>>& host_topics_map,
93 std::vector<Topic>& top_topics,
94 size_t& padded_top_topics_start_index,
95 size_t& history_topics_count);
97 void OnGetRecentBrowsingTopicsApiUsagesCompleted(
98 browsing_topics::ApiUsageContextQueryResult result);
100 void OnGetRecentlyVisitedURLsCompleted(history::QueryResults results);
102 void OnRequestModelCompleted(std::vector<std::string> raw_hosts);
104 void OnGetTopicsForHostsCompleted(const std::vector<Annotation>& results);
106 void OnCalculateCompleted(CalculatorResultStatus status,
107 EpochTopics epoch_topics);
109 // Those pointers are safe to hold and use throughout the lifetime of
110 // `BrowsingTopicsService`, which owns this object.
111 raw_ptr<privacy_sandbox::PrivacySandboxSettings> privacy_sandbox_settings_;
112 raw_ptr<history::HistoryService> history_service_;
113 raw_ptr<content::BrowsingTopicsSiteDataManager> site_data_manager_;
114 raw_ptr<Annotator> annotator_;
116 CalculateCompletedCallback calculate_completed_callback_;
118 // The calculation start time.
119 base::Time calculation_time_;
121 base::Time history_data_start_time_;
122 base::Time api_usage_context_data_start_time_;
124 // The history hosts over
125 // `kBrowsingTopicsNumberOfEpochsOfObservationDataToUseForFiltering` epochs,
126 // and the calling context domains that used the Topics API in each main frame
128 std::map<HashedHost, std::vector<HashedDomain>> host_context_domains_map_;
130 // The hashed history hosts and their count over the last epoch.
131 std::map<HashedHost, size_t> history_hosts_count_;
133 // Used for the async tasks querying the HistoryService.
134 base::CancelableTaskTracker history_task_tracker_;
136 // Whether this calculator was generated via the topics-internals page rather
137 // than via a scheduled task.
138 bool is_manually_triggered_;
140 base::WeakPtrFactory<BrowsingTopicsCalculator> weak_ptr_factory_{this};
143 } // namespace browsing_topics
145 #endif // COMPONENTS_BROWSING_TOPICS_BROWSING_TOPICS_CALCULATOR_H_