Backed out changeset f594e6f00208 (bug 1940883) for causing crashes in bug 1941164.
[gecko.git] / dom / quota / PersistenceScope.h
blob1d9dd54fd63c73f74045b3799861242896eee810
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/. */
7 #ifndef DOM_QUOTA_PERSISTENCESCOPE_H_
8 #define DOM_QUOTA_PERSISTENCESCOPE_H_
10 #include "mozilla/Assertions.h"
11 #include "mozilla/EnumSet.h"
12 #include "mozilla/Variant.h"
13 #include "mozilla/dom/quota/PersistenceType.h"
15 namespace mozilla::dom::quota {
17 class PersistenceScope {
18 class Value {
19 PersistenceType mValue;
21 public:
22 explicit Value(PersistenceType aValue) : mValue(aValue) {}
24 PersistenceType GetValue() const { return mValue; }
27 class Set {
28 EnumSet<PersistenceType> mSet;
30 public:
31 explicit Set(const EnumSet<PersistenceType>& aSet) : mSet(aSet) {}
33 const EnumSet<PersistenceType>& GetSet() const { return mSet; }
36 struct Null {};
38 using DataType = Variant<Value, Set, Null>;
40 DataType mData;
42 public:
43 PersistenceScope() : mData(Null()) {}
45 // XXX Consider renaming these static methods to Create
46 static PersistenceScope CreateFromValue(PersistenceType aValue) {
47 return PersistenceScope(std::move(Value(aValue)));
50 template <typename... Args>
51 static PersistenceScope CreateFromSet(Args... aArgs) {
52 return PersistenceScope(std::move(Set(EnumSet<PersistenceType>(aArgs...))));
55 static PersistenceScope CreateFromNull() {
56 return PersistenceScope(std::move(Null()));
59 bool IsValue() const { return mData.is<Value>(); }
61 bool IsSet() const { return mData.is<Set>(); }
63 bool IsNull() const { return mData.is<Null>(); }
65 void SetFromValue(PersistenceType aValue) {
66 mData = AsVariant(Value(aValue));
69 void SetFromNull() { mData = AsVariant(Null()); }
71 PersistenceType GetValue() const {
72 MOZ_ASSERT(IsValue());
74 return mData.as<Value>().GetValue();
77 const EnumSet<PersistenceType>& GetSet() const {
78 MOZ_ASSERT(IsSet());
80 return mData.as<Set>().GetSet();
83 bool Matches(const PersistenceScope& aOther) const {
84 struct Matcher {
85 const PersistenceScope& mThis;
87 explicit Matcher(const PersistenceScope& aThis) : mThis(aThis) {}
89 bool operator()(const Value& aOther) {
90 return mThis.MatchesValue(aOther);
93 bool operator()(const Set& aOther) { return mThis.MatchesSet(aOther); }
95 bool operator()(const Null& aOther) { return true; }
98 return aOther.mData.match(Matcher(*this));
101 private:
102 // Move constructors
103 explicit PersistenceScope(const Value&& aValue) : mData(aValue) {}
105 explicit PersistenceScope(const Set&& aSet) : mData(aSet) {}
107 explicit PersistenceScope(const Null&& aNull) : mData(aNull) {}
109 // Copy constructor
110 explicit PersistenceScope(const DataType& aOther) : mData(aOther) {}
112 bool MatchesValue(const Value& aOther) const {
113 struct ValueMatcher {
114 const Value& mOther;
116 explicit ValueMatcher(const Value& aOther) : mOther(aOther) {}
118 bool operator()(const Value& aThis) {
119 return aThis.GetValue() == mOther.GetValue();
122 bool operator()(const Set& aThis) {
123 return aThis.GetSet().contains(mOther.GetValue());
126 bool operator()(const Null& aThis) {
127 // Null covers everything.
128 return true;
132 return mData.match(ValueMatcher(aOther));
135 bool MatchesSet(const Set& aOther) const {
136 struct SetMatcher {
137 const Set& mOther;
139 explicit SetMatcher(const Set& aOther) : mOther(aOther) {}
141 bool operator()(const Value& aThis) {
142 return mOther.GetSet().contains(aThis.GetValue());
145 bool operator()(const Set& aThis) {
146 for (auto persistenceType : aThis.GetSet()) {
147 if (mOther.GetSet().contains(persistenceType)) {
148 return true;
151 return false;
154 bool operator()(const Null& aThis) {
155 // Null covers everything.
156 return true;
160 return mData.match(SetMatcher(aOther));
163 bool operator==(const PersistenceScope& aOther) = delete;
166 bool MatchesPersistentPersistenceScope(
167 const PersistenceScope& aPersistenceScope);
169 bool MatchesBestEffortPersistenceScope(
170 const PersistenceScope& aPersistenceScope);
172 } // namespace mozilla::dom::quota
174 #endif // DOM_QUOTA_PERSISTENCESCOPE_H_