Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / quota / OriginScope.h
bloba7047e5c82a1fc6c72a27c39f48c6fbd57783c81
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 mozilla_dom_quota_originorpatternstring_h__
8 #define mozilla_dom_quota_originorpatternstring_h__
10 #include <utility>
11 #include "mozilla/Assertions.h"
12 #include "mozilla/OriginAttributes.h"
13 #include "mozilla/UniquePtr.h"
14 #include "mozilla/Variant.h"
15 #include "nsStringFlags.h"
16 #include "nsStringFwd.h"
18 namespace mozilla::dom::quota {
20 class OriginScope {
21 class Origin {
22 nsCString mOrigin;
23 nsCString mOriginNoSuffix;
24 UniquePtr<OriginAttributes> mAttributes;
26 public:
27 explicit Origin(const nsACString& aOrigin) : mOrigin(aOrigin) {
28 InitMembers();
31 Origin(const Origin& aOther)
32 : mOrigin(aOther.mOrigin),
33 mOriginNoSuffix(aOther.mOriginNoSuffix),
34 mAttributes(MakeUnique<OriginAttributes>(*aOther.mAttributes)) {}
36 Origin(Origin&& aOther) = default;
38 const nsACString& GetOrigin() const { return mOrigin; }
40 void SetOrigin(const nsACString& aOrigin) {
41 mOrigin = aOrigin;
43 InitMembers();
46 const nsACString& GetOriginNoSuffix() const { return mOriginNoSuffix; }
48 const OriginAttributes& GetAttributes() const {
49 MOZ_ASSERT(mAttributes);
51 return *mAttributes;
54 private:
55 void InitMembers() {
56 mAttributes = MakeUnique<OriginAttributes>();
58 MOZ_ALWAYS_TRUE(
59 mAttributes->PopulateFromOrigin(mOrigin, mOriginNoSuffix));
63 class Prefix {
64 nsCString mOriginNoSuffix;
66 public:
67 explicit Prefix(const nsACString& aOriginNoSuffix)
68 : mOriginNoSuffix(aOriginNoSuffix) {}
70 const nsCString& GetOriginNoSuffix() const { return mOriginNoSuffix; }
72 void SetOriginNoSuffix(const nsACString& aOriginNoSuffix) {
73 mOriginNoSuffix = aOriginNoSuffix;
77 class Pattern {
78 UniquePtr<OriginAttributesPattern> mPattern;
80 public:
81 explicit Pattern(const OriginAttributesPattern& aPattern)
82 : mPattern(MakeUnique<OriginAttributesPattern>(aPattern)) {}
84 explicit Pattern(const nsAString& aJSONPattern)
85 : mPattern(MakeUnique<OriginAttributesPattern>()) {
86 MOZ_ALWAYS_TRUE(mPattern->Init(aJSONPattern));
89 Pattern(const Pattern& aOther)
90 : mPattern(MakeUnique<OriginAttributesPattern>(*aOther.mPattern)) {}
92 Pattern(Pattern&& aOther) = default;
94 const OriginAttributesPattern& GetPattern() const {
95 MOZ_ASSERT(mPattern);
97 return *mPattern;
100 void SetPattern(const OriginAttributesPattern& aPattern) {
101 mPattern = MakeUnique<OriginAttributesPattern>(aPattern);
104 nsString GetJSONPattern() const {
105 MOZ_ASSERT(mPattern);
107 nsString result;
108 MOZ_ALWAYS_TRUE(mPattern->ToJSON(result));
110 return result;
114 struct Null {};
116 using DataType = Variant<Origin, Prefix, Pattern, Null>;
118 DataType mData;
120 public:
121 OriginScope() : mData(Null()) {}
123 static OriginScope FromOrigin(const nsACString& aOrigin) {
124 return OriginScope(std::move(Origin(aOrigin)));
127 static OriginScope FromPrefix(const nsACString& aPrefix) {
128 return OriginScope(std::move(Prefix(aPrefix)));
131 static OriginScope FromPattern(const OriginAttributesPattern& aPattern) {
132 return OriginScope(std::move(Pattern(aPattern)));
135 static OriginScope FromJSONPattern(const nsAString& aJSONPattern) {
136 return OriginScope(std::move(Pattern(aJSONPattern)));
139 static OriginScope FromNull() { return OriginScope(std::move(Null())); }
141 bool IsOrigin() const { return mData.is<Origin>(); }
143 bool IsPrefix() const { return mData.is<Prefix>(); }
145 bool IsPattern() const { return mData.is<Pattern>(); }
147 bool IsNull() const { return mData.is<Null>(); }
149 void SetFromOrigin(const nsACString& aOrigin) {
150 mData = AsVariant(Origin(aOrigin));
153 void SetFromPrefix(const nsACString& aPrefix) {
154 mData = AsVariant(Prefix(aPrefix));
157 void SetFromPattern(const OriginAttributesPattern& aPattern) {
158 mData = AsVariant(Pattern(aPattern));
161 void SetFromJSONPattern(const nsAString& aJSONPattern) {
162 mData = AsVariant(Pattern(aJSONPattern));
165 void SetFromNull() { mData = AsVariant(Null()); }
167 const nsACString& GetOrigin() const {
168 MOZ_ASSERT(IsOrigin());
170 return mData.as<Origin>().GetOrigin();
173 void SetOrigin(const nsACString& aOrigin) {
174 MOZ_ASSERT(IsOrigin());
176 mData.as<Origin>().SetOrigin(aOrigin);
179 const nsACString& GetOriginNoSuffix() const {
180 MOZ_ASSERT(IsOrigin() || IsPrefix());
182 if (IsOrigin()) {
183 return mData.as<Origin>().GetOriginNoSuffix();
185 return mData.as<Prefix>().GetOriginNoSuffix();
188 void SetOriginNoSuffix(const nsACString& aOriginNoSuffix) {
189 MOZ_ASSERT(IsPrefix());
191 mData.as<Prefix>().SetOriginNoSuffix(aOriginNoSuffix);
194 const OriginAttributesPattern& GetPattern() const {
195 MOZ_ASSERT(IsPattern());
197 return mData.as<Pattern>().GetPattern();
200 nsString GetJSONPattern() const {
201 MOZ_ASSERT(IsPattern());
203 return mData.as<Pattern>().GetJSONPattern();
206 void SetPattern(const OriginAttributesPattern& aPattern) {
207 MOZ_ASSERT(IsPattern());
209 mData.as<Pattern>().SetPattern(aPattern);
212 bool Matches(const OriginScope& aOther) const {
213 struct Matcher {
214 const OriginScope& mThis;
216 explicit Matcher(const OriginScope& aThis) : mThis(aThis) {}
218 bool operator()(const Origin& aOther) {
219 return mThis.MatchesOrigin(aOther);
222 bool operator()(const Prefix& aOther) {
223 return mThis.MatchesPrefix(aOther);
226 bool operator()(const Pattern& aOther) {
227 return mThis.MatchesPattern(aOther);
230 bool operator()(const Null& aOther) { return true; }
233 return aOther.mData.match(Matcher(*this));
236 OriginScope Clone() { return OriginScope(mData); }
238 private:
239 // Move constructors
240 explicit OriginScope(const Origin&& aOrigin) : mData(aOrigin) {}
242 explicit OriginScope(const Prefix&& aPrefix) : mData(aPrefix) {}
244 explicit OriginScope(const Pattern&& aPattern) : mData(aPattern) {}
246 explicit OriginScope(const Null&& aNull) : mData(aNull) {}
248 // Copy constructor
249 explicit OriginScope(const DataType& aOther) : mData(aOther) {}
251 bool MatchesOrigin(const Origin& aOther) const {
252 struct OriginMatcher {
253 const Origin& mOther;
255 explicit OriginMatcher(const Origin& aOther) : mOther(aOther) {}
257 bool operator()(const Origin& aThis) {
258 return aThis.GetOrigin().Equals(mOther.GetOrigin());
261 bool operator()(const Prefix& aThis) {
262 return aThis.GetOriginNoSuffix().Equals(mOther.GetOriginNoSuffix());
265 bool operator()(const Pattern& aThis) {
266 return aThis.GetPattern().Matches(mOther.GetAttributes());
269 bool operator()(const Null& aThis) {
270 // Null covers everything.
271 return true;
275 return mData.match(OriginMatcher(aOther));
278 bool MatchesPrefix(const Prefix& aOther) const {
279 struct PrefixMatcher {
280 const Prefix& mOther;
282 explicit PrefixMatcher(const Prefix& aOther) : mOther(aOther) {}
284 bool operator()(const Origin& aThis) {
285 return aThis.GetOriginNoSuffix().Equals(mOther.GetOriginNoSuffix());
288 bool operator()(const Prefix& aThis) {
289 return aThis.GetOriginNoSuffix().Equals(mOther.GetOriginNoSuffix());
292 bool operator()(const Pattern& aThis) {
293 // The match will be always true here because any origin attributes
294 // pattern overlaps any origin prefix (an origin prefix targets all
295 // origin attributes).
296 return true;
299 bool operator()(const Null& aThis) {
300 // Null covers everything.
301 return true;
305 return mData.match(PrefixMatcher(aOther));
308 bool MatchesPattern(const Pattern& aOther) const {
309 struct PatternMatcher {
310 const Pattern& mOther;
312 explicit PatternMatcher(const Pattern& aOther) : mOther(aOther) {}
314 bool operator()(const Origin& aThis) {
315 return mOther.GetPattern().Matches(aThis.GetAttributes());
318 bool operator()(const Prefix& aThis) {
319 // The match will be always true here because any origin attributes
320 // pattern overlaps any origin prefix (an origin prefix targets all
321 // origin attributes).
322 return true;
325 bool operator()(const Pattern& aThis) {
326 return aThis.GetPattern().Overlaps(mOther.GetPattern());
329 bool operator()(const Null& aThis) {
330 // Null covers everything.
331 return true;
335 PatternMatcher patternMatcher(aOther);
336 return mData.match(PatternMatcher(aOther));
339 bool operator==(const OriginScope& aOther) = delete;
342 } // namespace mozilla::dom::quota
344 #endif // mozilla_dom_quota_originorpatternstring_h__