File: test_inmemory_strike_database.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 (109 lines) | stat: -rw-r--r-- 3,379 bytes parent folder | download | duplicates (3)
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
// Copyright 2021 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/test_inmemory_strike_database.h"

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

#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "components/autofill/core/browser/proto/strike_data.pb.h"

namespace autofill {

TestInMemoryStrikeDatabase::TestInMemoryStrikeDatabase() = default;

TestInMemoryStrikeDatabase::~TestInMemoryStrikeDatabase() = default;

int TestInMemoryStrikeDatabase::AddStrikes(int strikes_increase,
                                           const std::string& key) {
  DCHECK_GT(strikes_increase, 0);
  int num_strikes =
      strike_map_cache_.contains(key)
          ? strike_map_cache_[key].num_strikes() + strikes_increase
          : strikes_increase;
  SetStrikeData(key, num_strikes);
  return num_strikes;
}

int TestInMemoryStrikeDatabase::RemoveStrikes(int strikes_decrease,
                                              const std::string& key) {
  int num_strikes = GetStrikes(key);
  num_strikes = std::max(0, num_strikes - strikes_decrease);
  SetStrikeData(key, num_strikes);
  return num_strikes;
}

int TestInMemoryStrikeDatabase::GetStrikes(const std::string& key) {
  auto iter = strike_map_cache_.find(key);
  return (iter != strike_map_cache_.end()) ? iter->second.num_strikes() : 0;
}

void TestInMemoryStrikeDatabase::ClearStrikes(const std::string& key) {
  strike_map_cache_.erase(key);
}

std::map<std::string, StrikeData>&
TestInMemoryStrikeDatabase::GetStrikeCache() {
  return strike_map_cache_;
}

std::vector<std::string> TestInMemoryStrikeDatabase::GetAllStrikeKeysForProject(
    const std::string& project_prefix) {
  std::vector<std::string> project_keys;
  for (const auto& [key, data] : strike_map_cache_) {
    if (key.find(project_prefix) == 0) {
      project_keys.push_back(key);
    }
  }
  return project_keys;
}

void TestInMemoryStrikeDatabase::ClearAllStrikesForProject(
    const std::string& project_prefix) {
  ClearStrikesForKeys(GetAllStrikeKeysForProject(project_prefix));
}

void TestInMemoryStrikeDatabase::ClearStrikesForKeys(
    const std::vector<std::string>& keys_to_remove) {
  for (const auto& key : keys_to_remove) {
    strike_map_cache_.erase(key);
  }
}

void TestInMemoryStrikeDatabase::ClearAllStrikes() {
  strike_map_cache_.clear();
}

std::string TestInMemoryStrikeDatabase::GetPrefixFromKey(
    const std::string& key) const {
  return key.substr(0, key.find(kKeyDeliminator));
}

void TestInMemoryStrikeDatabase::SetStrikeData(const std::string& key,
                                               int num_strikes) {
  if (num_strikes == 0) {
    ClearStrikes(key);
    return;
  }
  StrikeData data;
  data.set_num_strikes(num_strikes);
  data.set_last_update_timestamp(
      base::Time::Now().ToDeltaSinceWindowsEpoch().InMicroseconds());
  strike_map_cache_[key] = data;
}

base::Time TestInMemoryStrikeDatabase::GetLastUpdatedTimestamp(
    const std::string& key) {
  auto iter = strike_map_cache_.find(key);
  return iter != strike_map_cache_.end()
             ? base::Time::FromDeltaSinceWindowsEpoch(
                   base::Microseconds(iter->second.last_update_timestamp()))
             : base::Time();
}

}  // namespace autofill