Fix emulator build error
[platform/framework/web/chromium-efl.git] / components / browsing_topics / browsing_topics_calculator.h
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.
4
5 #ifndef COMPONENTS_BROWSING_TOPICS_BROWSING_TOPICS_CALCULATOR_H_
6 #define COMPONENTS_BROWSING_TOPICS_BROWSING_TOPICS_CALCULATOR_H_
7
8 #include <map>
9 #include <set>
10
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"
20
21 namespace privacy_sandbox {
22 class PrivacySandboxSettings;
23 }  // namespace privacy_sandbox
24
25 namespace history {
26 class HistoryService;
27 }  // namespace history
28
29 namespace content {
30 class BrowsingTopicsSiteDataManager;
31 }  // namespace content
32
33 namespace browsing_topics {
34
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
40 // on.
41 // 4) Query the `Annotator` with a set of hosts, to get the corresponding
42 // topics.
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 {
46  public:
47   // These values are persisted to logs. Entries should not be renumbered and
48   // numeric values should never be reused.
49   enum class CalculatorResultStatus {
50     kSuccess = 0,
51     kFailurePermissionDenied = 1,
52     kFailureApiUsageContextQueryError = 2,
53     kFailureAnnotationExecutionError = 3,
54     kFailureTaxonomyVersionNotSupportedInBinary = 4,
55
56     kMaxValue = kFailureTaxonomyVersionNotSupportedInBinary,
57   };
58
59   using CalculateCompletedCallback = base::OnceCallback<void(EpochTopics)>;
60
61   BrowsingTopicsCalculator(
62       privacy_sandbox::PrivacySandboxSettings* privacy_sandbox_settings,
63       history::HistoryService* history_service,
64       content::BrowsingTopicsSiteDataManager* site_data_manager,
65       Annotator* annotator,
66       const base::circular_deque<EpochTopics>& epochs,
67       bool is_manually_triggered,
68       CalculateCompletedCallback callback);
69
70   BrowsingTopicsCalculator(const BrowsingTopicsCalculator&) = delete;
71   BrowsingTopicsCalculator& operator=(const BrowsingTopicsCalculator&) = delete;
72   BrowsingTopicsCalculator(BrowsingTopicsCalculator&&) = delete;
73   BrowsingTopicsCalculator& operator=(BrowsingTopicsCalculator&&) = delete;
74
75   virtual ~BrowsingTopicsCalculator();
76
77   bool is_manually_triggered() const { return is_manually_triggered_; }
78
79  protected:
80   // This method exists for the purposes of overriding in tests.
81   virtual uint64_t GenerateRandUint64();
82   virtual void CheckCanCalculate();
83
84  private:
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`.
90   void DeriveTopTopics(
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);
96
97   void OnGetRecentBrowsingTopicsApiUsagesCompleted(
98       browsing_topics::ApiUsageContextQueryResult result);
99
100   void OnGetRecentlyVisitedURLsCompleted(history::QueryResults results);
101
102   void OnRequestModelCompleted(std::vector<std::string> raw_hosts);
103
104   void OnGetTopicsForHostsCompleted(const std::vector<Annotation>& results);
105
106   void OnCalculateCompleted(CalculatorResultStatus status,
107                             EpochTopics epoch_topics);
108
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_;
115
116   CalculateCompletedCallback calculate_completed_callback_;
117
118   // The calculation start time.
119   base::Time calculation_time_;
120
121   base::Time history_data_start_time_;
122   base::Time api_usage_context_data_start_time_;
123
124   // The history hosts over
125   // `kBrowsingTopicsNumberOfEpochsOfObservationDataToUseForFiltering` epochs,
126   // and the calling context domains that used the Topics API in each main frame
127   // host.
128   std::map<HashedHost, std::vector<HashedDomain>> host_context_domains_map_;
129
130   // The hashed history hosts and their count over the last epoch.
131   std::map<HashedHost, size_t> history_hosts_count_;
132
133   // Used for the async tasks querying the HistoryService.
134   base::CancelableTaskTracker history_task_tracker_;
135
136   // Whether this calculator was generated via the topics-internals page rather
137   // than via a scheduled task.
138   bool is_manually_triggered_;
139
140   base::WeakPtrFactory<BrowsingTopicsCalculator> weak_ptr_factory_{this};
141 };
142
143 }  // namespace browsing_topics
144
145 #endif  // COMPONENTS_BROWSING_TOPICS_BROWSING_TOPICS_CALCULATOR_H_