tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

PersistenceScope.h (4465B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef DOM_QUOTA_PERSISTENCESCOPE_H_
      8 #define DOM_QUOTA_PERSISTENCESCOPE_H_
      9 
     10 #include "mozilla/Assertions.h"
     11 #include "mozilla/EnumSet.h"
     12 #include "mozilla/Variant.h"
     13 #include "mozilla/dom/quota/PersistenceType.h"
     14 
     15 namespace mozilla::dom::quota {
     16 
     17 class PersistenceScope {
     18  class Value {
     19    PersistenceType mValue;
     20 
     21   public:
     22    explicit Value(PersistenceType aValue) : mValue(aValue) {}
     23 
     24    PersistenceType GetValue() const { return mValue; }
     25  };
     26 
     27  class Set {
     28    EnumSet<PersistenceType> mSet;
     29 
     30   public:
     31    explicit Set(const EnumSet<PersistenceType>& aSet) : mSet(aSet) {}
     32 
     33    const EnumSet<PersistenceType>& GetSet() const { return mSet; }
     34  };
     35 
     36  struct Null {};
     37 
     38  using DataType = Variant<Value, Set, Null>;
     39 
     40  DataType mData;
     41 
     42 public:
     43  PersistenceScope() : mData(Null()) {}
     44 
     45  // XXX Consider renaming these static methods to Create
     46  static PersistenceScope CreateFromValue(PersistenceType aValue) {
     47    return PersistenceScope(std::move(Value(aValue)));
     48  }
     49 
     50  template <typename... Args>
     51  static PersistenceScope CreateFromSet(Args... aArgs) {
     52    return PersistenceScope(std::move(Set(EnumSet<PersistenceType>(aArgs...))));
     53  }
     54 
     55  static PersistenceScope CreateFromNull() {
     56    return PersistenceScope(std::move(Null()));
     57  }
     58 
     59  bool IsValue() const { return mData.is<Value>(); }
     60 
     61  bool IsSet() const { return mData.is<Set>(); }
     62 
     63  bool IsNull() const { return mData.is<Null>(); }
     64 
     65  void SetFromValue(PersistenceType aValue) {
     66    mData = AsVariant(Value(aValue));
     67  }
     68 
     69  void SetFromNull() { mData = AsVariant(Null()); }
     70 
     71  PersistenceType GetValue() const {
     72    MOZ_ASSERT(IsValue());
     73 
     74    return mData.as<Value>().GetValue();
     75  }
     76 
     77  const EnumSet<PersistenceType>& GetSet() const {
     78    MOZ_ASSERT(IsSet());
     79 
     80    return mData.as<Set>().GetSet();
     81  }
     82 
     83  bool Matches(const PersistenceScope& aOther) const {
     84    struct Matcher {
     85      const PersistenceScope& mThis;
     86 
     87      explicit Matcher(const PersistenceScope& aThis) : mThis(aThis) {}
     88 
     89      bool operator()(const Value& aOther) {
     90        return mThis.MatchesValue(aOther);
     91      }
     92 
     93      bool operator()(const Set& aOther) { return mThis.MatchesSet(aOther); }
     94 
     95      bool operator()(const Null& aOther) { return true; }
     96    };
     97 
     98    return aOther.mData.match(Matcher(*this));
     99  }
    100 
    101 private:
    102  // Move constructors
    103  explicit PersistenceScope(const Value&& aValue) : mData(aValue) {}
    104 
    105  explicit PersistenceScope(const Set&& aSet) : mData(aSet) {}
    106 
    107  explicit PersistenceScope(const Null&& aNull) : mData(aNull) {}
    108 
    109  // Copy constructor
    110  explicit PersistenceScope(const DataType& aOther) : mData(aOther) {}
    111 
    112  bool MatchesValue(const Value& aOther) const {
    113    struct ValueMatcher {
    114      const Value& mOther;
    115 
    116      explicit ValueMatcher(const Value& aOther) : mOther(aOther) {}
    117 
    118      bool operator()(const Value& aThis) {
    119        return aThis.GetValue() == mOther.GetValue();
    120      }
    121 
    122      bool operator()(const Set& aThis) {
    123        return aThis.GetSet().contains(mOther.GetValue());
    124      }
    125 
    126      bool operator()(const Null& aThis) {
    127        // Null covers everything.
    128        return true;
    129      }
    130    };
    131 
    132    return mData.match(ValueMatcher(aOther));
    133  }
    134 
    135  bool MatchesSet(const Set& aOther) const {
    136    struct SetMatcher {
    137      const Set& mOther;
    138 
    139      explicit SetMatcher(const Set& aOther) : mOther(aOther) {}
    140 
    141      bool operator()(const Value& aThis) {
    142        return mOther.GetSet().contains(aThis.GetValue());
    143      }
    144 
    145      bool operator()(const Set& aThis) {
    146        for (auto persistenceType : aThis.GetSet()) {
    147          if (mOther.GetSet().contains(persistenceType)) {
    148            return true;
    149          }
    150        }
    151        return false;
    152      }
    153 
    154      bool operator()(const Null& aThis) {
    155        // Null covers everything.
    156        return true;
    157      }
    158    };
    159 
    160    return mData.match(SetMatcher(aOther));
    161  }
    162 
    163  bool operator==(const PersistenceScope& aOther) = delete;
    164 };
    165 
    166 bool MatchesPersistentPersistenceScope(
    167    const PersistenceScope& aPersistenceScope);
    168 
    169 bool MatchesBestEffortPersistenceScope(
    170    const PersistenceScope& aPersistenceScope);
    171 
    172 }  // namespace mozilla::dom::quota
    173 
    174 #endif  // DOM_QUOTA_PERSISTENCESCOPE_H_