File: interest_group_k_anonymity_manager.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (316 lines) | stat: -rw-r--r-- 12,062 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/interest_group/interest_group_k_anonymity_manager.h"

#include <algorithm>
#include <tuple>
#include <vector>

#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "content/browser/interest_group/interest_group_caching_storage.h"
#include "content/browser/interest_group/interest_group_features.h"
#include "content/browser/interest_group/interest_group_manager_impl.h"
#include "content/browser/interest_group/interest_group_storage.h"
#include "third_party/blink/public/common/interest_group/interest_group.h"

namespace content {

namespace {

constexpr base::TimeDelta kKAnonymityExpiration = base::Days(7);

}  // namespace

bool IsKAnonDataExpired(const base::Time last_updated, const base::Time now) {
  bool result = last_updated + kKAnonymityExpiration < now;
  base::UmaHistogramBoolean("Ads.InterestGroup.Auction.KAnonymityDataExpired",
                            result);
  base::UmaHistogramCustomTimes("Ads.InterestGroup.Auction.KAnonymityDataAge",
                                /*sample=*/now - last_updated,
                                /*min=*/base::Seconds(0),
                                /*max=*/base::Days(7),
                                /*buckets=*/50);
  return result;
}

// TODO(orrb): Use caching_storage_ directly instead of
// interest_group_manager_, and remove the InterestGroupManagerImpl methods
// used to delegate to InterestGroupCachingStorage.
InterestGroupKAnonymityManager::InterestGroupKAnonymityManager(
    InterestGroupManagerImpl* interest_group_manager,
    InterestGroupCachingStorage* caching_storage,
    GetKAnonymityServiceDelegateCallback k_anonymity_service_callback)
    : interest_group_manager_(interest_group_manager),
      caching_storage_(caching_storage),
      k_anonymity_service_callback_(k_anonymity_service_callback),
      weak_ptr_factory_(this) {}

InterestGroupKAnonymityManager::~InterestGroupKAnonymityManager() = default;

InterestGroupKAnonymityManager::InProgressQueryState::InProgressQueryState(
    base::Time update_time,
    bool replace_existing_values)
    : update_time(update_time),
      replace_existing_values(replace_existing_values) {}
InterestGroupKAnonymityManager::InProgressQueryState::InProgressQueryState(
    const InProgressQueryState&) = default;
InterestGroupKAnonymityManager::InProgressQueryState::~InProgressQueryState() =
    default;

void InterestGroupKAnonymityManager::QueryKAnonymityOfOwners(
    base::span<const url::Origin> owners) {
  if (!base::FeatureList::IsEnabled(features::kFledgeQueryKAnonymity)) {
    return;
  }
  CHECK(caching_storage_);
  for (const auto& owner : owners) {
    caching_storage_->GetInterestGroupsForOwner(
        owner, base::BindOnce(
                   &InterestGroupKAnonymityManager::OnGotInterestGroupsOfOwner,
                   weak_ptr_factory_.GetWeakPtr()));
  }
}

void InterestGroupKAnonymityManager::OnGotInterestGroupsOfOwner(
    scoped_refptr<StorageInterestGroups> groups) {
  for (const SingleStorageInterestGroup& group : groups->GetInterestGroups()) {
    InterestGroupKanonUpdateParameter kanon_update(group->last_k_anon_updated);
    kanon_update.hashed_keys = group->interest_group.GetAllKAnonKeys();
    QueryKAnonymityData(blink::InterestGroupKey(group->interest_group.owner,
                                                group->interest_group.name),
                        kanon_update);
  }
}

void InterestGroupKAnonymityManager::QueryKAnonymityData(
    const blink::InterestGroupKey& interest_group_key,
    const InterestGroupKanonUpdateParameter& k_anon_data) {
  if (!base::FeatureList::IsEnabled(features::kFledgeQueryKAnonymity)) {
    return;
  }

  KAnonymityServiceDelegate* k_anonymity_service =
      k_anonymity_service_callback_.Run();
  if (!k_anonymity_service) {
    return;
  }

  base::Time check_time = base::Time::Now();
  bool replace_existing_values = true;
  base::TimeDelta min_wait = k_anonymity_service->GetQueryInterval();
  if (k_anon_data.update_time > check_time - min_wait) {
    // The last update happened recently but we should still update the
    // newly-added keys if needed.
    replace_existing_values = false;
  }

  // TODO(orrb): All of the logic distinguishing new keys from keys being
  // refreshed may be obsoleted by the k-anonymity keys cache queried below,
  // since keys are only held in the cache for a TTL that matches the query
  // interval, so that the cache can mimic the limitation of how frequently
  // each key is fetched from the k-anonymity server.
  const std::vector<std::string>& ids_to_query =
      replace_existing_values ? k_anon_data.hashed_keys
                              : k_anon_data.newly_added_hashed_keys;
  if (ids_to_query.size() == 0) {
    return;
  }
  InProgressQueryState new_query_state(check_time, replace_existing_values);
  auto in_progress_query_it =
      queries_in_progress_.insert({interest_group_key, new_query_state});
  if (!in_progress_query_it.second) {
    if (replace_existing_values ||
        in_progress_query_it.first->second.update_time <
            check_time - min_wait) {
      // This new request replaces all existing requests or the outstanding
      // request is outdated.
      in_progress_query_it.first->second = new_query_state;
    }
  }

  if (base::FeatureList::IsEnabled(features::kFledgeCacheKAnonHashedKeys) &&
      caching_storage_ != nullptr) {
    caching_storage_->LoadPositiveHashedKAnonymityKeysFromCache(
        ids_to_query, check_time,
        base::BindOnce(
            &InterestGroupKAnonymityManager::FetchUncachedKAnonymityData,
            weak_ptr_factory_.GetWeakPtr(),
            in_progress_query_it.first->second.update_time,
            interest_group_key));
  } else {
    FetchUncachedKAnonymityData(
        in_progress_query_it.first->second.update_time, interest_group_key,
        InterestGroupStorage::KAnonymityCacheResponse(
            /*_positive_hashed_keys_from_cache=*/{},
            /*_ids_to_query_from_server=*/ids_to_query));
  }
}

void InterestGroupKAnonymityManager::FetchUncachedKAnonymityData(
    base::Time update_time,
    const blink::InterestGroupKey& interest_group_key,
    InterestGroupStorage::KAnonymityCacheResponse cache_response) {
  auto it = queries_in_progress_.find(interest_group_key);
  if (it == queries_in_progress_.end() ||
      update_time != it->second.update_time) {
    return;
  }

  // If all of the keys we need were fetched from the cache, update k-anonymous
  // keys on the interest group and clean up the `InProgressQueryState`.
  if (cache_response.ids_to_query_from_server.empty()) {
    interest_group_manager_->UpdateKAnonymity(
        interest_group_key, cache_response.positive_hashed_keys_from_cache,
        update_time, it->second.replace_existing_values);
    queries_in_progress_.erase(it);
    return;
  }

  it->second.positive_hashed_keys_from_received_responses =
      std::move(cache_response.positive_hashed_keys_from_cache);

  KAnonymityServiceDelegate* k_anonymity_service =
      k_anonymity_service_callback_.Run();
  if (!k_anonymity_service) {
    // Cancel the update.
    queries_in_progress_.erase(interest_group_key);
    return;
  }

  std::vector<std::string> ids_to_query_in_next_batch;
  for (std::string& key : cache_response.ids_to_query_from_server) {
    ids_to_query_in_next_batch.emplace_back(std::move(key));

    if (ids_to_query_in_next_batch.size() >= kQueryBatchSizeLimit) {
      it->second.remaining_responses++;
      k_anonymity_service->QuerySets(
          ids_to_query_in_next_batch,
          base::BindOnce(&InterestGroupKAnonymityManager::QuerySetsCallback,
                         weak_ptr_factory_.GetWeakPtr(),
                         ids_to_query_in_next_batch, it->second.update_time,
                         interest_group_key));
      ids_to_query_in_next_batch.clear();
    }
  }

  if (ids_to_query_in_next_batch.empty()) {
    return;
  }
  it->second.remaining_responses++;
  k_anonymity_service->QuerySets(
      ids_to_query_in_next_batch,
      base::BindOnce(&InterestGroupKAnonymityManager::QuerySetsCallback,
                     weak_ptr_factory_.GetWeakPtr(), ids_to_query_in_next_batch,
                     it->second.update_time, interest_group_key));
}

void InterestGroupKAnonymityManager::QuerySetsCallback(
    std::vector<std::string> hashed_query,
    base::Time update_time,
    const blink::InterestGroupKey& interest_group_key,
    std::vector<bool> status) {
  DCHECK_LE(status.size(), hashed_query.size());
  if (status.empty()) {
    // There was an error. Cancel the update.
    queries_in_progress_.erase(interest_group_key);
    return;
  }

  auto it = queries_in_progress_.find(interest_group_key);
  if (it == queries_in_progress_.end() ||
      update_time != it->second.update_time) {
    return;
  }

  std::vector<std::string> negative_hashed_keys_from_received_responses;
  int size = std::min(hashed_query.size(), status.size());
  for (int i = 0; i < size; i++) {
    if (status[i]) {
      it->second.positive_hashed_keys_from_received_responses.emplace_back(
          hashed_query[i]);
    } else {
      negative_hashed_keys_from_received_responses.emplace_back(
          hashed_query[i]);
    }
  }

  if (base::FeatureList::IsEnabled(features::kFledgeCacheKAnonHashedKeys) &&
      caching_storage_ != nullptr) {
    caching_storage_->WriteHashedKAnonymityKeysToCache(
        it->second.positive_hashed_keys_from_received_responses,
        negative_hashed_keys_from_received_responses, update_time);
  }

  it->second.remaining_responses--;
  if (it->second.remaining_responses == 0) {
    interest_group_manager_->UpdateKAnonymity(
        interest_group_key,
        it->second.positive_hashed_keys_from_received_responses, update_time,
        it->second.replace_existing_values);
    queries_in_progress_.erase(it);
  }
}

void InterestGroupKAnonymityManager::RegisterAdKeysAsJoined(
    base::flat_set<std::string> hashed_keys) {
  for (const auto& key : hashed_keys) {
    RegisterIDAsJoined(key);
  }
  // TODO(behamilton): Consider proactively starting a query here to improve the
  // speed that browsers see new ads. We will likely want to rate limit this
  // somehow though.
}

void InterestGroupKAnonymityManager::RegisterIDAsJoined(
    const std::string& hashed_key) {
  if (joins_in_progress_.contains(hashed_key)) {
    return;
  }
  joins_in_progress_.insert(hashed_key);
  interest_group_manager_->GetLastKAnonymityReported(
      hashed_key,
      base::BindOnce(&InterestGroupKAnonymityManager::OnGotLastReportedTime,
                     weak_ptr_factory_.GetWeakPtr(), hashed_key));
}

void InterestGroupKAnonymityManager::OnGotLastReportedTime(
    std::string hashed_key,
    std::optional<base::Time> last_update_time) {
  KAnonymityServiceDelegate* k_anonymity_service =
      k_anonymity_service_callback_.Run();
  if (!k_anonymity_service) {
    return;
  }
  if (!last_update_time) {
    joins_in_progress_.erase(hashed_key);
    return;
  }

  // If it has been long enough since we last joined
  if (base::Time::Now() < last_update_time.value_or(base::Time()) +
                              k_anonymity_service->GetJoinInterval()) {
    joins_in_progress_.erase(hashed_key);
    return;
  }

  k_anonymity_service->JoinSet(
      hashed_key,
      base::BindOnce(&InterestGroupKAnonymityManager::JoinSetCallback,
                     weak_ptr_factory_.GetWeakPtr(), hashed_key));
}

void InterestGroupKAnonymityManager::JoinSetCallback(std::string hashed_key,
                                                     bool status) {
  if (status) {
    // Update the last reported time if the request succeeded.
    interest_group_manager_->UpdateLastKAnonymityReported(hashed_key);
  }
  joins_in_progress_.erase(hashed_key);
}

}  // namespace content