Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Yao Xiao | 7a1995b | 2022-03-09 08:18:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "components/browsing_topics/browsing_topics_service_impl.h" |
| 6 | |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 7 | #include <random> |
Abigail Katcoff | 02cceaa | 2023-03-27 16:22:32 | [diff] [blame] | 8 | #include <vector> |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 9 | |
Yao Xiao | d15c553 | 2023-02-07 18:50:12 | [diff] [blame] | 10 | #include "base/metrics/histogram_functions.h" |
Josh Karlin | d147c67 | 2023-03-06 20:45:13 | [diff] [blame] | 11 | #include "base/notreached.h" |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 12 | #include "base/rand_util.h" |
Yao Xiao | 57892a2 | 2022-06-28 19:21:41 | [diff] [blame] | 13 | #include "base/ranges/algorithm.h" |
Tommy C. Li | 088b42f | 2022-11-15 00:51:28 | [diff] [blame] | 14 | #include "base/strings/strcat.h" |
Gabriel Charette | d87f10f | 2022-03-31 00:44:22 | [diff] [blame] | 15 | #include "base/time/time.h" |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 16 | #include "components/browsing_topics/browsing_topics_calculator.h" |
| 17 | #include "components/browsing_topics/browsing_topics_page_load_data_tracker.h" |
Yao Xiao | 84826f4 | 2022-10-24 16:13:58 | [diff] [blame] | 18 | #include "components/browsing_topics/common/common_types.h" |
Yao Xiao | 21f1faa | 2022-04-29 06:20:32 | [diff] [blame] | 19 | #include "components/browsing_topics/mojom/browsing_topics_internals.mojom.h" |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 20 | #include "components/browsing_topics/util.h" |
Abigail Katcoff | 02cceaa | 2023-03-27 16:22:32 | [diff] [blame] | 21 | #include "components/privacy_sandbox/canonical_topic.h" |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 22 | #include "content/public/browser/browsing_topics_site_data_manager.h" |
| 23 | #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 24 | #include "services/metrics/public/cpp/ukm_builders.h" |
| 25 | #include "services/metrics/public/cpp/ukm_recorder.h" |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 26 | #include "third_party/blink/public/common/features.h" |
| 27 | #include "third_party/blink/public/mojom/browsing_topics/browsing_topics.mojom.h" |
| 28 | |
Yao Xiao | 7a1995b | 2022-03-09 08:18:55 | [diff] [blame] | 29 | namespace browsing_topics { |
| 30 | |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 31 | namespace { |
| 32 | |
Yao Xiao | bf39e34d | 2022-03-28 21:48:28 | [diff] [blame] | 33 | // Returns whether the topics should all be cleared given |
| 34 | // `browsing_topics_data_accessible_since` and `is_topic_allowed_by_settings`. |
| 35 | // Returns true if `browsing_topics_data_accessible_since` is greater than the |
Abigail Katcoff | 02cceaa | 2023-03-27 16:22:32 | [diff] [blame] | 36 | // last calculation time. |
Yao Xiao | bf39e34d | 2022-03-28 21:48:28 | [diff] [blame] | 37 | bool ShouldClearTopicsOnStartup( |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 38 | const BrowsingTopicsState& browsing_topics_state, |
Abigail Katcoff | 02cceaa | 2023-03-27 16:22:32 | [diff] [blame] | 39 | base::Time browsing_topics_data_accessible_since) { |
Yao Xiao | bf39e34d | 2022-03-28 21:48:28 | [diff] [blame] | 40 | if (browsing_topics_state.epochs().empty()) |
| 41 | return false; |
| 42 | |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 43 | // Here we rely on the fact that `browsing_topics_data_accessible_since` can |
| 44 | // only be updated to base::Time::Now() due to data deletion. So we'll either |
| 45 | // need to clear all topics data, or no-op. If this assumption no longer |
| 46 | // holds, we'd need to iterate over all epochs, check their calculation time, |
| 47 | // and selectively delete the epochs. |
Yao Xiao | bf39e34d | 2022-03-28 21:48:28 | [diff] [blame] | 48 | if (browsing_topics_data_accessible_since > |
| 49 | browsing_topics_state.epochs().back().calculation_time()) { |
| 50 | return true; |
| 51 | } |
| 52 | |
Abigail Katcoff | 02cceaa | 2023-03-27 16:22:32 | [diff] [blame] | 53 | return false; |
| 54 | } |
| 55 | |
| 56 | // Returns a vector of top topics which are disallowed and thus should be |
| 57 | // cleared. This could happen if the topic became disallowed when |
| 58 | // `browsing_topics_state` was still loading (and we didn't get a chance to |
| 59 | // clear it). |
| 60 | std::vector<privacy_sandbox::CanonicalTopic> TopTopicsToClearOnStartup( |
| 61 | const BrowsingTopicsState& browsing_topics_state, |
| 62 | base::RepeatingCallback<bool(const privacy_sandbox::CanonicalTopic&)> |
| 63 | is_topic_allowed_by_settings) { |
| 64 | DCHECK(!is_topic_allowed_by_settings.is_null()); |
| 65 | std::vector<privacy_sandbox::CanonicalTopic> top_topics_to_clear; |
Yao Xiao | bf39e34d | 2022-03-28 21:48:28 | [diff] [blame] | 66 | for (const EpochTopics& epoch : browsing_topics_state.epochs()) { |
| 67 | for (const TopicAndDomains& topic_and_domains : |
| 68 | epoch.top_topics_and_observing_domains()) { |
| 69 | if (!topic_and_domains.IsValid()) |
| 70 | continue; |
Abigail Katcoff | 02cceaa | 2023-03-27 16:22:32 | [diff] [blame] | 71 | privacy_sandbox::CanonicalTopic canonical_topic = |
| 72 | privacy_sandbox::CanonicalTopic(topic_and_domains.topic(), |
| 73 | epoch.taxonomy_version()); |
| 74 | if (!is_topic_allowed_by_settings.Run(canonical_topic)) { |
| 75 | top_topics_to_clear.emplace_back(canonical_topic); |
Yao Xiao | bf39e34d | 2022-03-28 21:48:28 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | } |
Abigail Katcoff | 02cceaa | 2023-03-27 16:22:32 | [diff] [blame] | 79 | return top_topics_to_clear; |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | struct StartupCalculateDecision { |
Abigail Katcoff | 02cceaa | 2023-03-27 16:22:32 | [diff] [blame] | 83 | bool clear_all_topics_data = true; |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 84 | base::TimeDelta next_calculation_delay; |
Abigail Katcoff | 02cceaa | 2023-03-27 16:22:32 | [diff] [blame] | 85 | std::vector<privacy_sandbox::CanonicalTopic> topics_to_clear; |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | StartupCalculateDecision GetStartupCalculationDecision( |
| 89 | const BrowsingTopicsState& browsing_topics_state, |
Yao Xiao | bf39e34d | 2022-03-28 21:48:28 | [diff] [blame] | 90 | base::Time browsing_topics_data_accessible_since, |
| 91 | base::RepeatingCallback<bool(const privacy_sandbox::CanonicalTopic&)> |
| 92 | is_topic_allowed_by_settings) { |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 93 | // The topics have never been calculated. This could happen with a fresh |
| 94 | // profile or the if the config has updated. In case of a config update, the |
| 95 | // topics should have already been cleared when initializing the |
| 96 | // `BrowsingTopicsState`. |
| 97 | if (browsing_topics_state.next_scheduled_calculation_time().is_null()) { |
Abigail Katcoff | 02cceaa | 2023-03-27 16:22:32 | [diff] [blame] | 98 | return StartupCalculateDecision{.clear_all_topics_data = false, |
| 99 | .next_calculation_delay = base::TimeDelta(), |
| 100 | .topics_to_clear = {}}; |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // This could happen when clear-on-exit is turned on and has caused the |
Abigail Katcoff | 02cceaa | 2023-03-27 16:22:32 | [diff] [blame] | 104 | // cookies to be deleted on startup |
| 105 | bool should_clear_all_topics_data = ShouldClearTopicsOnStartup( |
| 106 | browsing_topics_state, browsing_topics_data_accessible_since); |
| 107 | |
| 108 | std::vector<privacy_sandbox::CanonicalTopic> topics_to_clear; |
| 109 | if (!should_clear_all_topics_data) { |
| 110 | topics_to_clear = TopTopicsToClearOnStartup(browsing_topics_state, |
| 111 | is_topic_allowed_by_settings); |
| 112 | } |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 113 | |
| 114 | base::TimeDelta presumed_next_calculation_delay = |
| 115 | browsing_topics_state.next_scheduled_calculation_time() - |
| 116 | base::Time::Now(); |
| 117 | |
| 118 | // The scheduled calculation time was reached before the startup. |
| 119 | if (presumed_next_calculation_delay <= base::TimeDelta()) { |
| 120 | return StartupCalculateDecision{ |
Abigail Katcoff | 02cceaa | 2023-03-27 16:22:32 | [diff] [blame] | 121 | .clear_all_topics_data = should_clear_all_topics_data, |
| 122 | .next_calculation_delay = base::TimeDelta(), |
| 123 | .topics_to_clear = topics_to_clear}; |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // This could happen if the machine time has changed since the last |
| 127 | // calculation. Recalculate immediately to align with the expected schedule |
| 128 | // rather than potentially stop computing for a very long time. |
| 129 | if (presumed_next_calculation_delay >= |
| 130 | 2 * blink::features::kBrowsingTopicsTimePeriodPerEpoch.Get()) { |
| 131 | return StartupCalculateDecision{ |
Abigail Katcoff | 02cceaa | 2023-03-27 16:22:32 | [diff] [blame] | 132 | .clear_all_topics_data = should_clear_all_topics_data, |
| 133 | .next_calculation_delay = base::TimeDelta(), |
| 134 | .topics_to_clear = topics_to_clear}; |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | return StartupCalculateDecision{ |
Abigail Katcoff | 02cceaa | 2023-03-27 16:22:32 | [diff] [blame] | 138 | .clear_all_topics_data = should_clear_all_topics_data, |
| 139 | .next_calculation_delay = presumed_next_calculation_delay, |
| 140 | .topics_to_clear = topics_to_clear}; |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 141 | } |
| 142 | |
Josh Karlin | d147c67 | 2023-03-06 20:45:13 | [diff] [blame] | 143 | void RecordBrowsingTopicsApiResultMetrics(ApiAccessResult result, |
| 144 | content::RenderFrameHost* main_frame, |
| 145 | bool is_get_topics_request) { |
Yao Xiao | 9c789ea | 2022-10-26 14:46:55 | [diff] [blame] | 146 | // The `BrowsingTopics_DocumentBrowsingTopicsApiResult2` event is only |
| 147 | // recorded for request that gets the topics. |
Josh Karlin | d147c67 | 2023-03-06 20:45:13 | [diff] [blame] | 148 | if (!is_get_topics_request) { |
Yao Xiao | 9c789ea | 2022-10-26 14:46:55 | [diff] [blame] | 149 | return; |
Josh Karlin | d147c67 | 2023-03-06 20:45:13 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | base::UmaHistogramEnumeration("BrowsingTopics.Result.Status", result); |
| 153 | |
| 154 | if (result == browsing_topics::ApiAccessResult::kSuccess) { |
| 155 | return; |
| 156 | } |
Yao Xiao | 9c789ea | 2022-10-26 14:46:55 | [diff] [blame] | 157 | |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 158 | ukm::UkmRecorder* ukm_recorder = ukm::UkmRecorder::Get(); |
Yao Xiao | 84826f4 | 2022-10-24 16:13:58 | [diff] [blame] | 159 | ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult2 builder( |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 160 | main_frame->GetPageUkmSourceId()); |
Josh Karlin | d147c67 | 2023-03-06 20:45:13 | [diff] [blame] | 161 | builder.SetFailureReason(static_cast<int64_t>(result)); |
| 162 | |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 163 | builder.Record(ukm_recorder->Get()); |
| 164 | } |
| 165 | |
Josh Karlin | d147c67 | 2023-03-06 20:45:13 | [diff] [blame] | 166 | void RecordBrowsingTopicsApiResultMetrics( |
Yao Xiao | 84826f4 | 2022-10-24 16:13:58 | [diff] [blame] | 167 | const std::vector<CandidateTopic>& valid_candidate_topics, |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 168 | content::RenderFrameHost* main_frame) { |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 169 | ukm::UkmRecorder* ukm_recorder = ukm::UkmRecorder::Get(); |
Yao Xiao | 84826f4 | 2022-10-24 16:13:58 | [diff] [blame] | 170 | ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult2 builder( |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 171 | main_frame->GetPageUkmSourceId()); |
| 172 | |
Josh Karlin | d147c67 | 2023-03-06 20:45:13 | [diff] [blame] | 173 | int real_count = 0; |
| 174 | int fake_count = 0; |
| 175 | int filtered_count = 0; |
| 176 | |
Yao Xiao | 84826f4 | 2022-10-24 16:13:58 | [diff] [blame] | 177 | for (size_t i = 0; i < 3u && valid_candidate_topics.size() > i; ++i) { |
| 178 | const CandidateTopic& candidate_topic = valid_candidate_topics[i]; |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 179 | |
Yao Xiao | 84826f4 | 2022-10-24 16:13:58 | [diff] [blame] | 180 | DCHECK(candidate_topic.IsValid()); |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 181 | |
Josh Karlin | d147c67 | 2023-03-06 20:45:13 | [diff] [blame] | 182 | if (candidate_topic.should_be_filtered()) { |
| 183 | filtered_count += 1; |
| 184 | } else { |
| 185 | candidate_topic.is_true_topic() ? real_count += 1 : fake_count += 1; |
| 186 | } |
| 187 | |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 188 | if (i == 0) { |
Yao Xiao | 84826f4 | 2022-10-24 16:13:58 | [diff] [blame] | 189 | builder.SetCandidateTopic0(candidate_topic.topic().value()) |
| 190 | .SetCandidateTopic0IsTrueTopTopic(candidate_topic.is_true_topic()) |
| 191 | .SetCandidateTopic0ShouldBeFiltered( |
| 192 | candidate_topic.should_be_filtered()) |
| 193 | .SetCandidateTopic0TaxonomyVersion(candidate_topic.taxonomy_version()) |
| 194 | .SetCandidateTopic0ModelVersion(candidate_topic.model_version()); |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 195 | } else if (i == 1) { |
Yao Xiao | 84826f4 | 2022-10-24 16:13:58 | [diff] [blame] | 196 | builder.SetCandidateTopic1(candidate_topic.topic().value()) |
| 197 | .SetCandidateTopic1IsTrueTopTopic(candidate_topic.is_true_topic()) |
| 198 | .SetCandidateTopic1ShouldBeFiltered( |
| 199 | candidate_topic.should_be_filtered()) |
| 200 | .SetCandidateTopic1TaxonomyVersion(candidate_topic.taxonomy_version()) |
| 201 | .SetCandidateTopic1ModelVersion(candidate_topic.model_version()); |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 202 | } else { |
| 203 | DCHECK_EQ(i, 2u); |
Yao Xiao | 84826f4 | 2022-10-24 16:13:58 | [diff] [blame] | 204 | builder.SetCandidateTopic2(candidate_topic.topic().value()) |
| 205 | .SetCandidateTopic2IsTrueTopTopic(candidate_topic.is_true_topic()) |
| 206 | .SetCandidateTopic2ShouldBeFiltered( |
| 207 | candidate_topic.should_be_filtered()) |
| 208 | .SetCandidateTopic2TaxonomyVersion(candidate_topic.taxonomy_version()) |
| 209 | .SetCandidateTopic2ModelVersion(candidate_topic.model_version()); |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
Josh Karlin | d147c67 | 2023-03-06 20:45:13 | [diff] [blame] | 213 | const int kBuckets = 10; |
| 214 | DCHECK_GE(kBuckets, |
| 215 | blink::features::kBrowsingTopicsNumberOfEpochsToExpose.Get()); |
| 216 | |
| 217 | base::UmaHistogramExactLinear("BrowsingTopics.Result.RealTopicCount", |
| 218 | real_count, kBuckets); |
| 219 | base::UmaHistogramExactLinear("BrowsingTopics.Result.FakeTopicCount", |
| 220 | fake_count, kBuckets); |
| 221 | base::UmaHistogramExactLinear("BrowsingTopics.Result.FilteredTopicCount", |
| 222 | filtered_count, kBuckets); |
| 223 | |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 224 | builder.Record(ukm_recorder->Get()); |
| 225 | } |
| 226 | |
Yao Xiao | d15c553 | 2023-02-07 18:50:12 | [diff] [blame] | 227 | // Represents the action type of the request. |
| 228 | // |
| 229 | // These values are persisted to logs. Entries should not be renumbered and |
| 230 | // numeric values should never be reused. |
| 231 | enum class BrowsingTopicsApiActionType { |
| 232 | // Get topics via document.browsingTopics({skipObservation: true}). |
| 233 | kGetViaDocumentApi = 0, |
| 234 | |
| 235 | // Get and observe topics via the document.browsingTopics(). |
| 236 | kGetAndObserveViaDocumentApi = 1, |
| 237 | |
| 238 | // Get topics via fetch(<url>, {browsingTopics: true}) or via the analogous |
| 239 | // XHR request. |
| 240 | kGetViaFetchLikeApi = 2, |
| 241 | |
| 242 | // Observe topics via the "Sec-Browsing-Topics: ?1" response header for the |
| 243 | // fetch(<url>, {browsingTopics: true}) request, or for the analogous XHR |
| 244 | // request. |
| 245 | kObserveViaFetchLikeApi = 3, |
| 246 | |
Yao Xiao | 9f73e88 | 2023-03-27 19:01:41 | [diff] [blame] | 247 | // Get topics via <iframe src=[url] browsingtopics>. |
| 248 | kGetViaIframeAttributeApi = 4, |
| 249 | |
| 250 | // Observe topics via the "Sec-Browsing-Topics: ?1" response header for the |
| 251 | // <iframe src=[url] browsingtopics> request. |
| 252 | kObserveViaIframeAttributeApi = 5, |
| 253 | |
| 254 | kMaxValue = kObserveViaIframeAttributeApi, |
Yao Xiao | d15c553 | 2023-02-07 18:50:12 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | void RecordBrowsingTopicsApiActionTypeMetrics(ApiCallerSource caller_source, |
| 258 | bool get_topics, |
| 259 | bool observe) { |
| 260 | static constexpr char kBrowsingTopicsApiActionTypeHistogramId[] = |
| 261 | "BrowsingTopics.ApiActionType"; |
| 262 | |
| 263 | if (caller_source == ApiCallerSource::kJavaScript) { |
| 264 | DCHECK(get_topics); |
| 265 | |
| 266 | if (!observe) { |
| 267 | base::UmaHistogramEnumeration( |
| 268 | kBrowsingTopicsApiActionTypeHistogramId, |
| 269 | BrowsingTopicsApiActionType::kGetViaDocumentApi); |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | base::UmaHistogramEnumeration( |
| 274 | kBrowsingTopicsApiActionTypeHistogramId, |
| 275 | BrowsingTopicsApiActionType::kGetAndObserveViaDocumentApi); |
| 276 | |
| 277 | return; |
| 278 | } |
| 279 | |
Yao Xiao | 9f73e88 | 2023-03-27 19:01:41 | [diff] [blame] | 280 | if (caller_source == ApiCallerSource::kIframeAttribute) { |
| 281 | if (get_topics) { |
| 282 | DCHECK(!observe); |
| 283 | |
| 284 | base::UmaHistogramEnumeration( |
| 285 | kBrowsingTopicsApiActionTypeHistogramId, |
| 286 | BrowsingTopicsApiActionType::kGetViaIframeAttributeApi); |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | DCHECK(observe); |
| 291 | base::UmaHistogramEnumeration( |
| 292 | kBrowsingTopicsApiActionTypeHistogramId, |
| 293 | BrowsingTopicsApiActionType::kObserveViaIframeAttributeApi); |
| 294 | |
| 295 | return; |
| 296 | } |
| 297 | |
Yao Xiao | d15c553 | 2023-02-07 18:50:12 | [diff] [blame] | 298 | DCHECK_EQ(caller_source, ApiCallerSource::kFetch); |
| 299 | |
| 300 | if (get_topics) { |
| 301 | DCHECK(!observe); |
| 302 | |
| 303 | base::UmaHistogramEnumeration( |
| 304 | kBrowsingTopicsApiActionTypeHistogramId, |
| 305 | BrowsingTopicsApiActionType::kGetViaFetchLikeApi); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | DCHECK(observe); |
| 310 | base::UmaHistogramEnumeration( |
| 311 | kBrowsingTopicsApiActionTypeHistogramId, |
| 312 | BrowsingTopicsApiActionType::kObserveViaFetchLikeApi); |
| 313 | } |
| 314 | |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 315 | } // namespace |
| 316 | |
Yao Xiao | 7a1995b | 2022-03-09 08:18:55 | [diff] [blame] | 317 | BrowsingTopicsServiceImpl::~BrowsingTopicsServiceImpl() = default; |
| 318 | |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 319 | BrowsingTopicsServiceImpl::BrowsingTopicsServiceImpl( |
| 320 | const base::FilePath& profile_path, |
| 321 | privacy_sandbox::PrivacySandboxSettings* privacy_sandbox_settings, |
| 322 | history::HistoryService* history_service, |
| 323 | content::BrowsingTopicsSiteDataManager* site_data_manager, |
Robert Ogden | ad99d6f6 | 2023-05-01 21:40:09 | [diff] [blame] | 324 | std::unique_ptr<Annotator> annotator, |
Christian Dullweber | a4a5802 | 2023-01-27 01:53:42 | [diff] [blame] | 325 | TopicAccessedCallback topic_accessed_callback) |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 326 | : privacy_sandbox_settings_(privacy_sandbox_settings), |
| 327 | history_service_(history_service), |
| 328 | site_data_manager_(site_data_manager), |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 329 | browsing_topics_state_( |
| 330 | profile_path, |
| 331 | base::BindOnce( |
| 332 | &BrowsingTopicsServiceImpl::OnBrowsingTopicsStateLoaded, |
Christian Dullweber | a4a5802 | 2023-01-27 01:53:42 | [diff] [blame] | 333 | base::Unretained(this))), |
Robert Ogden | ad99d6f6 | 2023-05-01 21:40:09 | [diff] [blame] | 334 | annotator_(std::move(annotator)), |
Christian Dullweber | a4a5802 | 2023-01-27 01:53:42 | [diff] [blame] | 335 | topic_accessed_callback_(std::move(topic_accessed_callback)) { |
| 336 | DCHECK(topic_accessed_callback_); |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 337 | privacy_sandbox_settings_observation_.Observe(privacy_sandbox_settings); |
| 338 | history_service_observation_.Observe(history_service); |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 339 | } |
| 340 | |
Yao Xiao | 9c789ea | 2022-10-26 14:46:55 | [diff] [blame] | 341 | bool BrowsingTopicsServiceImpl::HandleTopicsWebApi( |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 342 | const url::Origin& context_origin, |
Yao Xiao | 1d60ed3 | 2022-09-27 16:33:24 | [diff] [blame] | 343 | content::RenderFrameHost* main_frame, |
Yao Xiao | 9c789ea | 2022-10-26 14:46:55 | [diff] [blame] | 344 | ApiCallerSource caller_source, |
| 345 | bool get_topics, |
| 346 | bool observe, |
| 347 | std::vector<blink::mojom::EpochTopicPtr>& topics) { |
| 348 | DCHECK(topics.empty()); |
| 349 | DCHECK(get_topics || observe); |
| 350 | |
Yao Xiao | d15c553 | 2023-02-07 18:50:12 | [diff] [blame] | 351 | RecordBrowsingTopicsApiActionTypeMetrics(caller_source, get_topics, observe); |
| 352 | |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 353 | if (!browsing_topics_state_loaded_) { |
Josh Karlin | d147c67 | 2023-03-06 20:45:13 | [diff] [blame] | 354 | RecordBrowsingTopicsApiResultMetrics(ApiAccessResult::kStateNotReady, |
| 355 | main_frame, get_topics); |
Yao Xiao | 9c789ea | 2022-10-26 14:46:55 | [diff] [blame] | 356 | return false; |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 357 | } |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 358 | |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 359 | if (!privacy_sandbox_settings_->IsTopicsAllowed()) { |
Josh Karlin | d147c67 | 2023-03-06 20:45:13 | [diff] [blame] | 360 | RecordBrowsingTopicsApiResultMetrics( |
| 361 | ApiAccessResult::kAccessDisallowedBySettings, main_frame, get_topics); |
Yao Xiao | 9c789ea | 2022-10-26 14:46:55 | [diff] [blame] | 362 | return false; |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 363 | } |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 364 | |
| 365 | if (!privacy_sandbox_settings_->IsTopicsAllowedForContext( |
Rohit Agarwal | aac12df | 2022-12-20 18:15:23 | [diff] [blame] | 366 | /*top_frame_origin=*/main_frame->GetLastCommittedOrigin(), |
| 367 | context_origin.GetURL())) { |
Josh Karlin | d147c67 | 2023-03-06 20:45:13 | [diff] [blame] | 368 | RecordBrowsingTopicsApiResultMetrics( |
| 369 | ApiAccessResult::kAccessDisallowedBySettings, main_frame, get_topics); |
Yao Xiao | 9c789ea | 2022-10-26 14:46:55 | [diff] [blame] | 370 | return false; |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 371 | } |
| 372 | |
Josh Karlin | d147c67 | 2023-03-06 20:45:13 | [diff] [blame] | 373 | RecordBrowsingTopicsApiResultMetrics(ApiAccessResult::kSuccess, main_frame, |
| 374 | get_topics); |
| 375 | |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 376 | std::string context_domain = |
| 377 | net::registry_controlled_domains::GetDomainAndRegistry( |
| 378 | context_origin.GetURL(), |
| 379 | net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| 380 | |
| 381 | HashedDomain hashed_context_domain = HashContextDomainForStorage( |
| 382 | browsing_topics_state_.hmac_key(), context_domain); |
| 383 | |
Yao Xiao | 1d60ed3 | 2022-09-27 16:33:24 | [diff] [blame] | 384 | if (observe) { |
| 385 | // Track the API usage context after the permissions check. |
| 386 | BrowsingTopicsPageLoadDataTracker::GetOrCreateForPage(main_frame->GetPage()) |
| 387 | ->OnBrowsingTopicsApiUsed(hashed_context_domain, history_service_); |
| 388 | } |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 389 | |
Yao Xiao | 9c789ea | 2022-10-26 14:46:55 | [diff] [blame] | 390 | if (!get_topics) |
| 391 | return true; |
| 392 | |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 393 | std::string top_domain = |
| 394 | net::registry_controlled_domains::GetDomainAndRegistry( |
| 395 | main_frame->GetLastCommittedOrigin().GetURL(), |
| 396 | net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| 397 | |
Yao Xiao | 84826f4 | 2022-10-24 16:13:58 | [diff] [blame] | 398 | std::vector<CandidateTopic> valid_candidate_topics; |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 399 | |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 400 | for (const EpochTopics* epoch : |
| 401 | browsing_topics_state_.EpochsForSite(top_domain)) { |
Yao Xiao | 41431299 | 2022-10-18 20:25:11 | [diff] [blame] | 402 | CandidateTopic candidate_topic = epoch->CandidateTopicForSite( |
| 403 | top_domain, hashed_context_domain, browsing_topics_state_.hmac_key()); |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 404 | |
Yao Xiao | 41431299 | 2022-10-18 20:25:11 | [diff] [blame] | 405 | if (!candidate_topic.IsValid()) |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 406 | continue; |
| 407 | |
Yao Xiao | bf39e34d | 2022-03-28 21:48:28 | [diff] [blame] | 408 | // Although a top topic can never be in the disallowed state, the returned |
Yao Xiao | 41431299 | 2022-10-18 20:25:11 | [diff] [blame] | 409 | // `candidate_topic` may be the random one. Thus we still need this check. |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 410 | if (!privacy_sandbox_settings_->IsTopicAllowed( |
Yao Xiao | 41431299 | 2022-10-18 20:25:11 | [diff] [blame] | 411 | privacy_sandbox::CanonicalTopic( |
| 412 | candidate_topic.topic(), candidate_topic.taxonomy_version()))) { |
| 413 | DCHECK(!candidate_topic.is_true_topic()); |
Yao Xiao | bf39e34d | 2022-03-28 21:48:28 | [diff] [blame] | 414 | continue; |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 415 | } |
| 416 | |
Yao Xiao | 84826f4 | 2022-10-24 16:13:58 | [diff] [blame] | 417 | valid_candidate_topics.push_back(std::move(candidate_topic)); |
| 418 | } |
| 419 | |
Josh Karlin | d147c67 | 2023-03-06 20:45:13 | [diff] [blame] | 420 | RecordBrowsingTopicsApiResultMetrics(valid_candidate_topics, main_frame); |
Yao Xiao | 84826f4 | 2022-10-24 16:13:58 | [diff] [blame] | 421 | |
Yao Xiao | 84826f4 | 2022-10-24 16:13:58 | [diff] [blame] | 422 | for (const CandidateTopic& candidate_topic : valid_candidate_topics) { |
| 423 | if (candidate_topic.should_be_filtered()) |
| 424 | continue; |
| 425 | |
Yao Xiao | 3a03e60 | 2022-10-18 18:17:56 | [diff] [blame] | 426 | // `PageSpecificContentSettings` should only observe true top topics |
| 427 | // accessed on the page. It's okay to notify the same topic multiple |
| 428 | // times even though duplicate topics will be removed in the end. |
Yao Xiao | 41431299 | 2022-10-18 20:25:11 | [diff] [blame] | 429 | if (candidate_topic.is_true_topic()) { |
Yao Xiao | 3a03e60 | 2022-10-18 18:17:56 | [diff] [blame] | 430 | privacy_sandbox::CanonicalTopic canonical_topic( |
Yao Xiao | 41431299 | 2022-10-18 20:25:11 | [diff] [blame] | 431 | candidate_topic.topic(), candidate_topic.taxonomy_version()); |
Christian Dullweber | a4a5802 | 2023-01-27 01:53:42 | [diff] [blame] | 432 | topic_accessed_callback_.Run(main_frame, context_origin, |
| 433 | /*blocked_by_policy=*/false, |
| 434 | canonical_topic); |
Yao Xiao | 3a03e60 | 2022-10-18 18:17:56 | [diff] [blame] | 435 | } |
| 436 | |
Yao Xiao | 21f1faa | 2022-04-29 06:20:32 | [diff] [blame] | 437 | auto result_topic = blink::mojom::EpochTopic::New(); |
Yao Xiao | 41431299 | 2022-10-18 20:25:11 | [diff] [blame] | 438 | result_topic->topic = candidate_topic.topic().value(); |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 439 | result_topic->config_version = base::StrCat( |
| 440 | {"chrome.", base::NumberToString( |
| 441 | blink::features::kBrowsingTopicsConfigVersion.Get())}); |
Yao Xiao | 41431299 | 2022-10-18 20:25:11 | [diff] [blame] | 442 | result_topic->model_version = |
| 443 | base::NumberToString(candidate_topic.model_version()); |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 444 | result_topic->taxonomy_version = |
Yao Xiao | 41431299 | 2022-10-18 20:25:11 | [diff] [blame] | 445 | base::NumberToString(candidate_topic.taxonomy_version()); |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 446 | result_topic->version = base::StrCat({result_topic->config_version, ":", |
| 447 | result_topic->taxonomy_version, ":", |
| 448 | result_topic->model_version}); |
Yao Xiao | 9c789ea | 2022-10-26 14:46:55 | [diff] [blame] | 449 | topics.emplace_back(std::move(result_topic)); |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 450 | } |
| 451 | |
Yao Xiao | 9c789ea | 2022-10-26 14:46:55 | [diff] [blame] | 452 | std::sort(topics.begin(), topics.end()); |
Yao Xiao | 716e481 | 2022-04-20 22:57:19 | [diff] [blame] | 453 | |
Yao Xiao | 84826f4 | 2022-10-24 16:13:58 | [diff] [blame] | 454 | // Remove duplicate entries. |
Yao Xiao | 9c789ea | 2022-10-26 14:46:55 | [diff] [blame] | 455 | topics.erase(std::unique(topics.begin(), topics.end()), topics.end()); |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 456 | |
Yao Xiao | 9c789ea | 2022-10-26 14:46:55 | [diff] [blame] | 457 | return true; |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 458 | } |
Yao Xiao | 7a1995b | 2022-03-09 08:18:55 | [diff] [blame] | 459 | |
Yao Xiao | 57892a2 | 2022-06-28 19:21:41 | [diff] [blame] | 460 | void BrowsingTopicsServiceImpl::GetBrowsingTopicsStateForWebUi( |
| 461 | bool calculate_now, |
| 462 | mojom::PageHandler::GetBrowsingTopicsStateCallback callback) { |
Yao Xiao | 21f1faa | 2022-04-29 06:20:32 | [diff] [blame] | 463 | if (!browsing_topics_state_loaded_) { |
Yao Xiao | 57892a2 | 2022-06-28 19:21:41 | [diff] [blame] | 464 | std::move(callback).Run( |
| 465 | mojom::WebUIGetBrowsingTopicsStateResult::NewOverrideStatusMessage( |
| 466 | "State loading hasn't finished. Please retry shortly.")); |
| 467 | return; |
Yao Xiao | 21f1faa | 2022-04-29 06:20:32 | [diff] [blame] | 468 | } |
| 469 | |
Yao Xiao | 57892a2 | 2022-06-28 19:21:41 | [diff] [blame] | 470 | // If a calculation is already in progress, get the webui topics state after |
| 471 | // the calculation is done. Do this regardless of whether `calculate_now` is |
| 472 | // true, i.e. if `calculate_now` is true, this request is effectively merged |
| 473 | // with the in progress calculation. |
| 474 | if (topics_calculator_) { |
| 475 | get_state_for_webui_callbacks_.push_back(std::move(callback)); |
| 476 | return; |
Yao Xiao | 21f1faa | 2022-04-29 06:20:32 | [diff] [blame] | 477 | } |
| 478 | |
Yao Xiao | 57892a2 | 2022-06-28 19:21:41 | [diff] [blame] | 479 | DCHECK(schedule_calculate_timer_.IsRunning()); |
Yao Xiao | 21f1faa | 2022-04-29 06:20:32 | [diff] [blame] | 480 | |
Yao Xiao | 57892a2 | 2022-06-28 19:21:41 | [diff] [blame] | 481 | if (calculate_now) { |
| 482 | get_state_for_webui_callbacks_.push_back(std::move(callback)); |
Yao Xiao | 21f1faa | 2022-04-29 06:20:32 | [diff] [blame] | 483 | |
Yao Xiao | 57892a2 | 2022-06-28 19:21:41 | [diff] [blame] | 484 | schedule_calculate_timer_.AbandonAndStop(); |
| 485 | CalculateBrowsingTopics(); |
| 486 | return; |
Yao Xiao | 21f1faa | 2022-04-29 06:20:32 | [diff] [blame] | 487 | } |
| 488 | |
Yao Xiao | 57892a2 | 2022-06-28 19:21:41 | [diff] [blame] | 489 | std::move(callback).Run(GetBrowsingTopicsStateForWebUiHelper()); |
Yao Xiao | 21f1faa | 2022-04-29 06:20:32 | [diff] [blame] | 490 | } |
| 491 | |
Yao Xiao | 7a1995b | 2022-03-09 08:18:55 | [diff] [blame] | 492 | std::vector<privacy_sandbox::CanonicalTopic> |
Yao Xiao | 7a1995b | 2022-03-09 08:18:55 | [diff] [blame] | 493 | BrowsingTopicsServiceImpl::GetTopTopicsForDisplay() const { |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 494 | if (!browsing_topics_state_loaded_) |
| 495 | return {}; |
| 496 | |
| 497 | std::vector<privacy_sandbox::CanonicalTopic> result; |
| 498 | |
| 499 | for (const EpochTopics& epoch : browsing_topics_state_.epochs()) { |
Yao Xiao | bf39e34d | 2022-03-28 21:48:28 | [diff] [blame] | 500 | DCHECK_LE(epoch.padded_top_topics_start_index(), |
| 501 | epoch.top_topics_and_observing_domains().size()); |
| 502 | |
| 503 | for (size_t i = 0; i < epoch.padded_top_topics_start_index(); ++i) { |
| 504 | const TopicAndDomains& topic_and_domains = |
| 505 | epoch.top_topics_and_observing_domains()[i]; |
| 506 | |
| 507 | if (!topic_and_domains.IsValid()) |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 508 | continue; |
| 509 | |
Yao Xiao | bf39e34d | 2022-03-28 21:48:28 | [diff] [blame] | 510 | // A top topic can never be in the disallowed state (i.e. it will be |
| 511 | // cleared when it becomes diallowed). |
| 512 | DCHECK(privacy_sandbox_settings_->IsTopicAllowed( |
| 513 | privacy_sandbox::CanonicalTopic(topic_and_domains.topic(), |
| 514 | epoch.taxonomy_version()))); |
| 515 | |
| 516 | result.emplace_back(topic_and_domains.topic(), epoch.taxonomy_version()); |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
| 520 | return result; |
| 521 | } |
| 522 | |
Robert Ogden | ad99d6f6 | 2023-05-01 21:40:09 | [diff] [blame] | 523 | Annotator* BrowsingTopicsServiceImpl::GetAnnotator() { |
| 524 | return annotator_.get(); |
| 525 | } |
| 526 | |
Yao Xiao | bf39e34d | 2022-03-28 21:48:28 | [diff] [blame] | 527 | void BrowsingTopicsServiceImpl::ClearTopic( |
| 528 | const privacy_sandbox::CanonicalTopic& canonical_topic) { |
| 529 | if (!browsing_topics_state_loaded_) |
| 530 | return; |
| 531 | |
| 532 | browsing_topics_state_.ClearTopic(canonical_topic.topic_id(), |
| 533 | canonical_topic.taxonomy_version()); |
| 534 | } |
| 535 | |
| 536 | void BrowsingTopicsServiceImpl::ClearTopicsDataForOrigin( |
| 537 | const url::Origin& origin) { |
| 538 | if (!browsing_topics_state_loaded_) |
| 539 | return; |
| 540 | |
| 541 | std::string context_domain = |
| 542 | net::registry_controlled_domains::GetDomainAndRegistry( |
| 543 | origin.GetURL(), |
| 544 | net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| 545 | |
| 546 | HashedDomain hashed_context_domain = HashContextDomainForStorage( |
| 547 | browsing_topics_state_.hmac_key(), context_domain); |
| 548 | |
| 549 | browsing_topics_state_.ClearContextDomain(hashed_context_domain); |
| 550 | site_data_manager_->ClearContextDomain(hashed_context_domain); |
| 551 | } |
| 552 | |
| 553 | void BrowsingTopicsServiceImpl::ClearAllTopicsData() { |
| 554 | if (!browsing_topics_state_loaded_) |
| 555 | return; |
| 556 | |
| 557 | browsing_topics_state_.ClearAllTopics(); |
| 558 | site_data_manager_->ExpireDataBefore(base::Time::Now()); |
| 559 | } |
| 560 | |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 561 | std::unique_ptr<BrowsingTopicsCalculator> |
| 562 | BrowsingTopicsServiceImpl::CreateCalculator( |
| 563 | privacy_sandbox::PrivacySandboxSettings* privacy_sandbox_settings, |
| 564 | history::HistoryService* history_service, |
| 565 | content::BrowsingTopicsSiteDataManager* site_data_manager, |
Robert Ogden | ad99d6f6 | 2023-05-01 21:40:09 | [diff] [blame] | 566 | Annotator* annotator, |
Yao Xiao | 57892a2 | 2022-06-28 19:21:41 | [diff] [blame] | 567 | const base::circular_deque<EpochTopics>& epochs, |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 568 | BrowsingTopicsCalculator::CalculateCompletedCallback callback) { |
| 569 | return std::make_unique<BrowsingTopicsCalculator>( |
Robert Ogden | ad99d6f6 | 2023-05-01 21:40:09 | [diff] [blame] | 570 | privacy_sandbox_settings, history_service, site_data_manager, annotator, |
| 571 | epochs, std::move(callback)); |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | const BrowsingTopicsState& BrowsingTopicsServiceImpl::browsing_topics_state() { |
| 575 | return browsing_topics_state_; |
| 576 | } |
| 577 | |
| 578 | void BrowsingTopicsServiceImpl::ScheduleBrowsingTopicsCalculation( |
| 579 | base::TimeDelta delay) { |
| 580 | DCHECK(browsing_topics_state_loaded_); |
| 581 | |
| 582 | // `this` owns the timer, which is automatically cancelled on destruction, so |
| 583 | // base::Unretained(this) is safe. |
| 584 | schedule_calculate_timer_.Start( |
| 585 | FROM_HERE, delay, |
| 586 | base::BindOnce(&BrowsingTopicsServiceImpl::CalculateBrowsingTopics, |
| 587 | base::Unretained(this))); |
| 588 | } |
| 589 | |
| 590 | void BrowsingTopicsServiceImpl::CalculateBrowsingTopics() { |
| 591 | DCHECK(browsing_topics_state_loaded_); |
| 592 | |
| 593 | DCHECK(!topics_calculator_); |
| 594 | |
| 595 | // `this` owns `topics_calculator_` so `topics_calculator_` should not invoke |
| 596 | // the callback once it's destroyed. |
| 597 | topics_calculator_ = CreateCalculator( |
| 598 | privacy_sandbox_settings_, history_service_, site_data_manager_, |
Robert Ogden | ad99d6f6 | 2023-05-01 21:40:09 | [diff] [blame] | 599 | annotator_.get(), browsing_topics_state_.epochs(), |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 600 | base::BindOnce( |
| 601 | &BrowsingTopicsServiceImpl::OnCalculateBrowsingTopicsCompleted, |
| 602 | base::Unretained(this))); |
| 603 | } |
| 604 | |
| 605 | void BrowsingTopicsServiceImpl::OnCalculateBrowsingTopicsCompleted( |
| 606 | EpochTopics epoch_topics) { |
| 607 | DCHECK(browsing_topics_state_loaded_); |
| 608 | |
| 609 | DCHECK(topics_calculator_); |
| 610 | topics_calculator_.reset(); |
| 611 | |
| 612 | browsing_topics_state_.AddEpoch(std::move(epoch_topics)); |
| 613 | browsing_topics_state_.UpdateNextScheduledCalculationTime(); |
| 614 | |
| 615 | ScheduleBrowsingTopicsCalculation( |
| 616 | blink::features::kBrowsingTopicsTimePeriodPerEpoch.Get()); |
Yao Xiao | 57892a2 | 2022-06-28 19:21:41 | [diff] [blame] | 617 | |
| 618 | if (!get_state_for_webui_callbacks_.empty()) { |
| 619 | mojom::WebUIGetBrowsingTopicsStateResultPtr webui_state = |
| 620 | GetBrowsingTopicsStateForWebUiHelper(); |
| 621 | |
| 622 | for (auto& callback : get_state_for_webui_callbacks_) { |
| 623 | std::move(callback).Run(webui_state->Clone()); |
| 624 | } |
| 625 | |
| 626 | get_state_for_webui_callbacks_.clear(); |
| 627 | } |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | void BrowsingTopicsServiceImpl::OnBrowsingTopicsStateLoaded() { |
| 631 | DCHECK(!browsing_topics_state_loaded_); |
| 632 | browsing_topics_state_loaded_ = true; |
| 633 | |
| 634 | base::Time browsing_topics_data_sccessible_since = |
| 635 | privacy_sandbox_settings_->TopicsDataAccessibleSince(); |
| 636 | |
| 637 | StartupCalculateDecision decision = GetStartupCalculationDecision( |
Yao Xiao | bf39e34d | 2022-03-28 21:48:28 | [diff] [blame] | 638 | browsing_topics_state_, browsing_topics_data_sccessible_since, |
| 639 | base::BindRepeating( |
| 640 | &privacy_sandbox::PrivacySandboxSettings::IsTopicAllowed, |
| 641 | base::Unretained(privacy_sandbox_settings_))); |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 642 | |
Abigail Katcoff | 02cceaa | 2023-03-27 16:22:32 | [diff] [blame] | 643 | if (decision.clear_all_topics_data) { |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 644 | browsing_topics_state_.ClearAllTopics(); |
Abigail Katcoff | 02cceaa | 2023-03-27 16:22:32 | [diff] [blame] | 645 | } else if (!decision.topics_to_clear.empty()) { |
| 646 | for (const privacy_sandbox::CanonicalTopic& canonical_topic : |
| 647 | decision.topics_to_clear) { |
| 648 | browsing_topics_state_.ClearTopic(canonical_topic.topic_id(), |
| 649 | canonical_topic.taxonomy_version()); |
| 650 | } |
| 651 | } |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 652 | |
| 653 | site_data_manager_->ExpireDataBefore(browsing_topics_data_sccessible_since); |
| 654 | |
| 655 | ScheduleBrowsingTopicsCalculation(decision.next_calculation_delay); |
| 656 | } |
| 657 | |
| 658 | void BrowsingTopicsServiceImpl::Shutdown() { |
| 659 | privacy_sandbox_settings_observation_.Reset(); |
| 660 | history_service_observation_.Reset(); |
| 661 | } |
| 662 | |
| 663 | void BrowsingTopicsServiceImpl::OnTopicsDataAccessibleSinceUpdated() { |
| 664 | if (!browsing_topics_state_loaded_) |
| 665 | return; |
| 666 | |
Yao Xiao | bf39e34d | 2022-03-28 21:48:28 | [diff] [blame] | 667 | // Here we rely on the fact that `browsing_topics_data_accessible_since` can |
| 668 | // only be updated to base::Time::Now() due to data deletion. In this case, we |
| 669 | // should just clear all topics. |
| 670 | browsing_topics_state_.ClearAllTopics(); |
| 671 | site_data_manager_->ExpireDataBefore( |
| 672 | privacy_sandbox_settings_->TopicsDataAccessibleSince()); |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 673 | |
| 674 | // Abort the outstanding topics calculation and restart immediately. |
| 675 | if (topics_calculator_) { |
| 676 | DCHECK(!schedule_calculate_timer_.IsRunning()); |
| 677 | |
| 678 | topics_calculator_.reset(); |
| 679 | CalculateBrowsingTopics(); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | void BrowsingTopicsServiceImpl::OnURLsDeleted( |
| 684 | history::HistoryService* history_service, |
| 685 | const history::DeletionInfo& deletion_info) { |
| 686 | if (!browsing_topics_state_loaded_) |
| 687 | return; |
| 688 | |
| 689 | // Ignore invalid time_range. |
| 690 | if (!deletion_info.IsAllHistory() && !deletion_info.time_range().IsValid()) |
| 691 | return; |
| 692 | |
| 693 | for (size_t i = 0; i < browsing_topics_state_.epochs().size(); ++i) { |
| 694 | const EpochTopics& epoch_topics = browsing_topics_state_.epochs()[i]; |
| 695 | |
| 696 | if (epoch_topics.empty()) |
| 697 | continue; |
| 698 | |
Yao Xiao | 57892a2 | 2022-06-28 19:21:41 | [diff] [blame] | 699 | // The typical case is assumed here. We cannot always derive the original |
| 700 | // history start time, as the necessary data (e.g. its previous epoch's |
| 701 | // calculation time) may have been gone. |
| 702 | base::Time history_data_start_time = |
| 703 | epoch_topics.calculation_time() - |
| 704 | blink::features::kBrowsingTopicsTimePeriodPerEpoch.Get(); |
| 705 | |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 706 | bool time_range_overlap = |
| 707 | epoch_topics.calculation_time() >= deletion_info.time_range().begin() && |
Yao Xiao | 57892a2 | 2022-06-28 19:21:41 | [diff] [blame] | 708 | history_data_start_time <= deletion_info.time_range().end(); |
Yao Xiao | cc37939 | 2022-03-25 21:39:06 | [diff] [blame] | 709 | |
| 710 | if (time_range_overlap) |
| 711 | browsing_topics_state_.ClearOneEpoch(i); |
| 712 | } |
| 713 | |
| 714 | // If there's an outstanding topics calculation, abort and restart it. |
| 715 | if (topics_calculator_) { |
| 716 | DCHECK(!schedule_calculate_timer_.IsRunning()); |
| 717 | |
| 718 | topics_calculator_.reset(); |
| 719 | CalculateBrowsingTopics(); |
| 720 | } |
Yao Xiao | 7a1995b | 2022-03-09 08:18:55 | [diff] [blame] | 721 | } |
| 722 | |
Yao Xiao | 57892a2 | 2022-06-28 19:21:41 | [diff] [blame] | 723 | mojom::WebUIGetBrowsingTopicsStateResultPtr |
| 724 | BrowsingTopicsServiceImpl::GetBrowsingTopicsStateForWebUiHelper() { |
| 725 | DCHECK(browsing_topics_state_loaded_); |
| 726 | DCHECK(!topics_calculator_); |
| 727 | |
| 728 | auto webui_state = mojom::WebUIBrowsingTopicsState::New(); |
| 729 | |
| 730 | webui_state->next_scheduled_calculation_time = |
| 731 | browsing_topics_state_.next_scheduled_calculation_time(); |
| 732 | |
| 733 | for (const EpochTopics& epoch : browsing_topics_state_.epochs()) { |
| 734 | DCHECK_LE(epoch.padded_top_topics_start_index(), |
| 735 | epoch.top_topics_and_observing_domains().size()); |
| 736 | |
| 737 | // Note: for a failed epoch calculation, the default zero-initialized values |
| 738 | // will be displayed in the Web UI. |
| 739 | auto webui_epoch = mojom::WebUIEpoch::New(); |
| 740 | webui_epoch->calculation_time = epoch.calculation_time(); |
| 741 | webui_epoch->model_version = base::NumberToString(epoch.model_version()); |
| 742 | webui_epoch->taxonomy_version = |
| 743 | base::NumberToString(epoch.taxonomy_version()); |
| 744 | |
| 745 | for (size_t i = 0; i < epoch.top_topics_and_observing_domains().size(); |
| 746 | ++i) { |
| 747 | const TopicAndDomains& topic_and_domains = |
| 748 | epoch.top_topics_and_observing_domains()[i]; |
| 749 | |
| 750 | privacy_sandbox::CanonicalTopic canonical_topic = |
| 751 | privacy_sandbox::CanonicalTopic(topic_and_domains.topic(), |
| 752 | epoch.taxonomy_version()); |
| 753 | |
| 754 | std::vector<std::string> webui_observed_by_domains; |
| 755 | webui_observed_by_domains.reserve( |
| 756 | topic_and_domains.hashed_domains().size()); |
| 757 | for (const auto& domain : topic_and_domains.hashed_domains()) { |
| 758 | webui_observed_by_domains.push_back( |
| 759 | base::NumberToString(domain.value())); |
| 760 | } |
| 761 | |
| 762 | // Note: if the topic is invalid (i.e. cleared), the output `topic_id` |
| 763 | // will be 0; if the topic is invalid, or if the taxonomy version isn't |
| 764 | // recognized by this Chrome binary, the output `topic_name` will be |
| 765 | // "Unknown". |
| 766 | auto webui_topic = mojom::WebUITopic::New(); |
| 767 | webui_topic->topic_id = topic_and_domains.topic().value(); |
| 768 | webui_topic->topic_name = canonical_topic.GetLocalizedRepresentation(); |
| 769 | webui_topic->is_real_topic = (i < epoch.padded_top_topics_start_index()); |
| 770 | webui_topic->observed_by_domains = std::move(webui_observed_by_domains); |
| 771 | |
| 772 | webui_epoch->topics.push_back(std::move(webui_topic)); |
| 773 | } |
| 774 | |
| 775 | webui_state->epochs.push_back(std::move(webui_epoch)); |
| 776 | } |
| 777 | |
| 778 | // Reorder the epochs from latest to oldest. |
| 779 | base::ranges::reverse(webui_state->epochs); |
| 780 | |
| 781 | return mojom::WebUIGetBrowsingTopicsStateResult::NewBrowsingTopicsState( |
| 782 | std::move(webui_state)); |
| 783 | } |
| 784 | |
Yao Xiao | 7a1995b | 2022-03-09 08:18:55 | [diff] [blame] | 785 | } // namespace browsing_topics |