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_
|