File: strike_database_integrator_base.cc

package info (click to toggle)
chromium 141.0.7390.107-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,246,132 kB
  • sloc: cpp: 35,264,965; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,635; asm: 950,788; xml: 751,751; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,313; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (289 lines) | stat: -rw-r--r-- 9,854 bytes parent folder | download | duplicates (4)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/strike_database/strike_database_integrator_base.h"

#include <algorithm>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "base/containers/to_vector.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/strcat.h"
#include "base/time/time.h"
#include "components/autofill/core/browser/proto/strike_data.pb.h"
#include "components/autofill/core/common/autofill_payments_features.h"
#include "components/leveldb_proto/public/proto_database_provider.h"
#include "components/strike_database/strike_database_base.h"

namespace autofill {

StrikeDatabaseIntegratorBase::StrikeDatabaseIntegratorBase(
    StrikeDatabaseBase* strike_database)
    : strike_database_(strike_database) {}

StrikeDatabaseIntegratorBase::~StrikeDatabaseIntegratorBase() = default;

StrikeDatabaseIntegratorBase::StrikeDatabaseDecision
StrikeDatabaseIntegratorBase::GetStrikeDatabaseDecision(
    std::string_view id) const {
  CheckIdUniqueness(id);

  if (base::FeatureList::IsEnabled(features::kDisableAutofillStrikeSystem)) {
    // Debug/test user has disabled the strike database.
    return StrikeDatabaseDecision::kDoNotBlock;
  }

  // Returns whether or not strike count for `id` has reached the strike limit
  // set by GetMaxStrikesLimit().
  if (GetStrikes(id) >= GetMaxStrikesLimit()) {
    return StrikeDatabaseDecision::kMaxStrikeLimitReached;
  }

  // Returns whether or not `GetRequiredDelaySinceLastStrike()` has passed since
  // the last strike was logged for candidate with `id`. Note that some features
  // don't specify a required delay.
  if (GetRequiredDelaySinceLastStrike().has_value() &&
      base::Time::Now() -
              strike_database_->GetLastUpdatedTimestamp(GetKey(id)) <
          GetRequiredDelaySinceLastStrike()) {
    return StrikeDatabaseDecision::kRequiredDelayNotPassed;
  }

  return StrikeDatabaseDecision::kDoNotBlock;
}

StrikeDatabaseIntegratorBase::StrikeDatabaseDecision
StrikeDatabaseIntegratorBase::GetStrikeDatabaseDecision() const {
  return GetStrikeDatabaseDecision(kSharedId);
}

bool StrikeDatabaseIntegratorBase::ShouldBlockFeature(
    std::string_view id) const {
  return GetStrikeDatabaseDecision(id) != StrikeDatabaseDecision::kDoNotBlock;
}

bool StrikeDatabaseIntegratorBase::ShouldBlockFeature() const {
  return GetStrikeDatabaseDecision() != StrikeDatabaseDecision::kDoNotBlock;
}

int StrikeDatabaseIntegratorBase::AddStrike(std::string_view id) {
  CheckIdUniqueness(id);
  return AddStrikes(1, id);
}

int StrikeDatabaseIntegratorBase::AddStrikes(int strikes_increase,
                                             std::string_view id) {
  CheckIdUniqueness(id);
  int num_strikes = strike_database_->AddStrikes(strikes_increase, GetKey(id));
  // If a new strike entry was created, run the routine to limit the number of
  // stored entries. This is a noop for most strike counters.
  if (num_strikes == strikes_increase) {
    LimitNumberOfStoredEntries();
  }

  base::UmaHistogramCounts1000(
      "Autofill.StrikeDatabase.NthStrikeAdded." + GetProjectPrefix(),
      num_strikes);
  return num_strikes;
}

int StrikeDatabaseIntegratorBase::RemoveStrike(std::string_view id) {
  CheckIdUniqueness(id);
  return strike_database_->RemoveStrikes(1, GetKey(id));
}

int StrikeDatabaseIntegratorBase::RemoveStrikes(int strike_decrease,
                                                std::string_view id) {
  CheckIdUniqueness(id);
  return strike_database_->RemoveStrikes(strike_decrease, GetKey(id));
}

int StrikeDatabaseIntegratorBase::GetStrikes(std::string_view id) const {
  CheckIdUniqueness(id);
  return strike_database_->GetStrikes(GetKey(id));
}

void StrikeDatabaseIntegratorBase::ClearStrikes(std::string_view id) {
  CheckIdUniqueness(id);
  strike_database_->ClearStrikes(GetKey(id));
}

void StrikeDatabaseIntegratorBase::ClearAllStrikes() {
  strike_database_->ClearAllStrikesForProject(GetProjectPrefix());
}

size_t StrikeDatabaseIntegratorBase::CountEntries() const {
  return std::ranges::count_if(GetStrikeCache(), [&](const auto& entry) {
    return strike_database_->GetPrefixFromKey(entry.first) ==
           GetProjectPrefix();
  });
}

void StrikeDatabaseIntegratorBase::LimitNumberOfStoredEntries() {
  if (!NumberOfEntriesExceedsLimits()) {
    return;
  }

  DCHECK(GetMaximumEntries().has_value());
  DCHECK(!GetMaximumEntriesAfterCleanup().has_value() ||
         GetMaximumEntriesAfterCleanup() <= GetMaximumEntries());

  size_t maximum_size = GetMaximumEntriesAfterCleanup().has_value()
                            ? GetMaximumEntriesAfterCleanup().value()
                            : GetMaximumEntries().value();

  std::vector<std::pair<std::string, int64_t>> entries;
  entries.reserve(GetStrikeCache().size());
  for (const auto& [key, data] : GetStrikeCache()) {
    if (strike_database_->GetPrefixFromKey(key) != GetProjectPrefix()) {
      continue;
    }
    entries.emplace_back(key, data.last_update_timestamp());
  }

  if (entries.size() <= maximum_size) {
    return;
  }

  // Sort by timestamp.
  std::ranges::sort(entries,
                    [](auto& a, auto& b) { return a.second < b.second; });
  const size_t elements_to_delete = entries.size() - maximum_size;
  const std::vector<std::string> keys_to_delete =
      base::ToVector(base::span(entries).first(elements_to_delete),
                     &std::pair<std::string, int64_t>::first);
  ClearStrikesForKeys(keys_to_delete);
}

bool StrikeDatabaseIntegratorBase::NumberOfEntriesExceedsLimits() const {
  if (!GetMaximumEntries().has_value()) {
    return false;
  }

  return CountEntries() > GetMaximumEntries();
}

void StrikeDatabaseIntegratorBase::RemoveExpiredStrikes() {
  if (!GetExpiryTimeDelta().has_value()) {
    // Strikes don't expire.
    return;
  }
  std::vector<std::string> expired_keys;
  for (const auto& [key, data] : strike_database_->GetStrikeCache()) {
    // Only consider keys from the current strike database integrator.
    if (strike_database_->GetPrefixFromKey(key) != GetProjectPrefix()) {
      continue;
    }
    if (GetEntryAge(data) > GetExpiryTimeDelta().value()) {
      if (strike_database_->GetStrikes(key) > 0) {
        expired_keys.push_back(key);
        base::UmaHistogramCounts1000(
            "Autofill.StrikeDatabase.StrikesPresentWhenStrikeExpired." +
                strike_database_->GetPrefixFromKey(key),
            strike_database_->GetStrikes(key));
      }
    }
  }
  for (std::string key : expired_keys) {
    int strikes_to_remove = 1;
    // If the key is already over the limit, remove additional strikes to
    // emulate setting it back to the limit. These are done together to avoid
    // multiple calls to the file system ProtoDatabase.
    strikes_to_remove +=
        std::max(0, strike_database_->GetStrikes(key) - GetMaxStrikesLimit());
    strike_database_->RemoveStrikes(strikes_to_remove, key);
  }
}

void StrikeDatabaseIntegratorBase::ClearStrikesByIdMatching(
    const std::set<std::string>& ids_to_delete,
    base::FunctionRef<std::string(const std::string&)> id_map) {
  ClearStrikesByIdMatchingAndTime(ids_to_delete, base::Time::Min(),
                                  base::Time::Max(), id_map);
}

void StrikeDatabaseIntegratorBase::ClearStrikesByIdMatchingAndTime(
    const std::set<std::string>& ids_to_delete,
    base::Time delete_begin,
    base::Time delete_end,
    base::FunctionRef<std::string(const std::string&)> id_map) {
  if (delete_begin.is_null()) {
    delete_begin = base::Time::Min();
  }

  if (delete_end.is_null()) {
    delete_end = base::Time::Max();
  }

  std::vector<std::string> keys_to_delete;
  keys_to_delete.reserve(GetStrikeCache().size());

  for (auto const& [key, strike_data] : GetStrikeCache()) {
    const std::string_view strike_id = GetIdFromKey(key);
    if (strike_id.empty()) {
      continue;
    }

    base::Time last_update = base::Time::FromDeltaSinceWindowsEpoch(
        base::Microseconds(strike_data.last_update_timestamp()));

    // Check if the time stamp of the record is within deletion range and if the
    // domain is deleted.
    if (last_update >= delete_begin && last_update <= delete_end &&
        ids_to_delete.contains(id_map(std::string(strike_id)))) {
      keys_to_delete.push_back(key);
    }
  }

  ClearStrikesForKeys(keys_to_delete);
}

void StrikeDatabaseIntegratorBase::ClearStrikesForKeys(
    const std::vector<std::string>& keys) {
  strike_database_->ClearStrikesForKeys(keys);
}

std::string_view StrikeDatabaseIntegratorBase::GetIdFromKey(
    std::string_view key) const {
  std::string prefix =
      base::StrCat({GetProjectPrefix(), StrikeDatabaseBase::kKeyDeliminator});
  if (!key.starts_with(prefix)) {
    return {};
  }
  return key.substr(prefix.length(), std::string::npos);
}

base::TimeDelta StrikeDatabaseIntegratorBase::GetEntryAge(
    const StrikeData& strike_data) {
  return base::Time::Now() -
         base::Time::FromDeltaSinceWindowsEpoch(
             base::Microseconds(strike_data.last_update_timestamp()));
}

std::string StrikeDatabaseIntegratorBase::GetKey(std::string_view id) const {
  return base::StrCat(
      {GetProjectPrefix(), StrikeDatabaseBase::kKeyDeliminator, id});
}

std::optional<size_t> StrikeDatabaseIntegratorBase::GetMaximumEntries() const {
  return std::nullopt;
}

std::optional<size_t>
StrikeDatabaseIntegratorBase::GetMaximumEntriesAfterCleanup() const {
  return std::nullopt;
}

std::optional<base::TimeDelta>
StrikeDatabaseIntegratorBase::GetRequiredDelaySinceLastStrike() const {
  return std::nullopt;
}

}  // namespace autofill