File: plus_address_cache.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (68 lines) | stat: -rw-r--r-- 2,382 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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PLUS_ADDRESSES_PLUS_ADDRESS_CACHE_H_
#define COMPONENTS_PLUS_ADDRESSES_PLUS_ADDRESS_CACHE_H_

#include <memory>
#include <string>
#include <vector>

#include "base/containers/flat_set.h"
#include "base/containers/span.h"
#include "components/plus_addresses/plus_address_types.h"

namespace plus_addresses {

// An in-memory cache of PlusProfile(s) utilized by the PlusAddressService to
// store copies of existing plus profiles.
class PlusAddressCache {
 public:
  PlusAddressCache();
  ~PlusAddressCache();

  // Inserts `profile` into the cache. Returns true if a new element was
  // inserted or false if an equivalent element already existed.
  bool InsertProfile(const PlusProfile& profile);

  // Erases `profile` from the cache. Returns true if a new element was erase or
  // false if no equivalent element existed.
  bool EraseProfile(const PlusProfile& profile);

  // Searches the cache for an element with an equivalent facet and returns a
  // profile if found, otherwise it returns `std::nullopt`.
  std::optional<PlusProfile> FindByFacet(
      const affiliations::FacetURI& facet) const;

  // Clears the cache.
  void Clear();

  // Checks whether the passed-in string is a known plus address.
  bool IsPlusAddress(const std::string& plus_address) const;

  // Returns all the cached plus profiles.
  base::span<const PlusProfile> GetPlusProfiles() const;

  // Returns true if the cache is empty, false otherwise.
  bool IsEmpty() const;

  // Returns the number of elements in the cache.
  size_t Size() const;

 private:
  // The user's existing set of `PlusProfile`s, ordered by facet. Since only a
  // single address per facet is supported, this can be used as the comparator.
  // The facets related to stored profiles are always valid web or Android
  // `FacetURI`s. This is guaranteed by the PlusAddress server. Notably, this
  // excludes facets related to http domains.
  base::flat_set<PlusProfile, PlusProfileFacetComparator> plus_profiles_;

  // Used to drive the `IsPlusAddress` function, and derived from the values of
  // `plus_profiles_`.
  base::flat_set<std::string> plus_addresses_;
};

}  // namespace plus_addresses

#endif  // COMPONENTS_PLUS_ADDRESSES_PLUS_ADDRESS_CACHE_H_