Bug 1526591 - Remove devtools.inspector.shapesHighlighter.enabled pref. r=rcaliman
[gecko.git] / dom / quota / OriginScope.h
blob909eea63780a6c6dbf2b9e83a9d79949b7c91ae0
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 "mozilla/dom/quota/QuotaCommon.h"
12 #include "mozilla/BasePrincipal.h"
13 #include "mozilla/Variant.h"
15 BEGIN_QUOTA_NAMESPACE
17 class OriginScope {
18 class Origin {
19 nsCString mOrigin;
20 nsCString mOriginNoSuffix;
21 UniquePtr<OriginAttributes> mAttributes;
23 public:
24 explicit Origin(const nsACString& aOrigin) : mOrigin(aOrigin) {
25 InitMembers();
28 Origin(const Origin& aOther)
29 : mOrigin(aOther.mOrigin),
30 mOriginNoSuffix(aOther.mOriginNoSuffix),
31 mAttributes(MakeUnique<OriginAttributes>(*aOther.mAttributes)) {}
33 Origin(Origin&& aOther) = default;
35 const nsACString& GetOrigin() const { return mOrigin; }
37 void SetOrigin(const nsACString& aOrigin) {
38 mOrigin = aOrigin;
40 InitMembers();
43 const nsACString& GetOriginNoSuffix() const { return mOriginNoSuffix; }
45 const OriginAttributes& GetAttributes() const {
46 MOZ_ASSERT(mAttributes);
48 return *mAttributes;
51 private:
52 void InitMembers() {
53 mAttributes = MakeUnique<OriginAttributes>();
55 MOZ_ALWAYS_TRUE(
56 mAttributes->PopulateFromOrigin(mOrigin, mOriginNoSuffix));
60 class Prefix {
61 nsCString mOriginNoSuffix;
63 public:
64 explicit Prefix(const nsACString& aOriginNoSuffix)
65 : mOriginNoSuffix(aOriginNoSuffix) {}
67 const nsCString& GetOriginNoSuffix() const { return mOriginNoSuffix; }
69 void SetOriginNoSuffix(const nsACString& aOriginNoSuffix) {
70 mOriginNoSuffix = aOriginNoSuffix;
74 class Pattern {
75 UniquePtr<OriginAttributesPattern> mPattern;
77 public:
78 explicit Pattern(const OriginAttributesPattern& aPattern)
79 : mPattern(MakeUnique<OriginAttributesPattern>(aPattern)) {}
81 explicit Pattern(const nsAString& aJSONPattern)
82 : mPattern(MakeUnique<OriginAttributesPattern>()) {
83 MOZ_ALWAYS_TRUE(mPattern->Init(aJSONPattern));
86 Pattern(const Pattern& aOther)
87 : mPattern(MakeUnique<OriginAttributesPattern>(*aOther.mPattern)) {}
89 Pattern(Pattern&& aOther) = default;
91 const OriginAttributesPattern& GetPattern() const {
92 MOZ_ASSERT(mPattern);
94 return *mPattern;
97 void SetPattern(const OriginAttributesPattern& aPattern) {
98 mPattern = MakeUnique<OriginAttributesPattern>(aPattern);
101 nsString GetJSONPattern() const {
102 MOZ_ASSERT(mPattern);
104 nsString result;
105 MOZ_ALWAYS_TRUE(mPattern->ToJSON(result));
107 return result;
111 struct Null {};
113 using DataType = Variant<Origin, Prefix, Pattern, Null>;
115 DataType mData;
117 public:
118 OriginScope() : mData(Null()) {}
120 static OriginScope FromOrigin(const nsACString& aOrigin) {
121 return OriginScope(std::move(Origin(aOrigin)));
124 static OriginScope FromPrefix(const nsACString& aPrefix) {
125 return OriginScope(std::move(Prefix(aPrefix)));
128 static OriginScope FromPattern(const OriginAttributesPattern& aPattern) {
129 return OriginScope(std::move(Pattern(aPattern)));
132 static OriginScope FromJSONPattern(const nsAString& aJSONPattern) {
133 return OriginScope(std::move(Pattern(aJSONPattern)));
136 static OriginScope FromNull() { return OriginScope(std::move(Null())); }
138 bool IsOrigin() const { return mData.is<Origin>(); }
140 bool IsPrefix() const { return mData.is<Prefix>(); }
142 bool IsPattern() const { return mData.is<Pattern>(); }
144 bool IsNull() const { return mData.is<Null>(); }
146 void SetFromOrigin(const nsACString& aOrigin) {
147 mData = AsVariant(Origin(aOrigin));
150 void SetFromPrefix(const nsACString& aPrefix) {
151 mData = AsVariant(Prefix(aPrefix));
154 void SetFromPattern(const OriginAttributesPattern& aPattern) {
155 mData = AsVariant(Pattern(aPattern));
158 void SetFromJSONPattern(const nsAString& aJSONPattern) {
159 mData = AsVariant(Pattern(aJSONPattern));
162 void SetFromNull() { mData = AsVariant(Null()); }
164 const nsACString& GetOrigin() const {
165 MOZ_ASSERT(IsOrigin());
167 return mData.as<Origin>().GetOrigin();
170 void SetOrigin(const nsACString& aOrigin) {
171 MOZ_ASSERT(IsOrigin());
173 mData.as<Origin>().SetOrigin(aOrigin);
176 const nsACString& GetOriginNoSuffix() const {
177 MOZ_ASSERT(IsOrigin() || IsPrefix());
179 if (IsOrigin()) {
180 return mData.as<Origin>().GetOriginNoSuffix();
182 return mData.as<Prefix>().GetOriginNoSuffix();
185 void SetOriginNoSuffix(const nsACString& aOriginNoSuffix) {
186 MOZ_ASSERT(IsPrefix());
188 mData.as<Prefix>().SetOriginNoSuffix(aOriginNoSuffix);
191 const OriginAttributesPattern& GetPattern() const {
192 MOZ_ASSERT(IsPattern());
194 return mData.as<Pattern>().GetPattern();
197 nsString GetJSONPattern() const {
198 MOZ_ASSERT(IsPattern());
200 return mData.as<Pattern>().GetJSONPattern();
203 void SetPattern(const OriginAttributesPattern& aPattern) {
204 MOZ_ASSERT(IsPattern());
206 mData.as<Pattern>().SetPattern(aPattern);
209 bool Matches(const OriginScope& aOther) const {
210 struct Matcher {
211 const OriginScope& mThis;
213 explicit Matcher(const OriginScope& aThis) : mThis(aThis) {}
215 bool match(const Origin& aOther) { return mThis.MatchesOrigin(aOther); }
217 bool match(const Prefix& aOther) { return mThis.MatchesPrefix(aOther); }
219 bool match(const Pattern& aOther) { return mThis.MatchesPattern(aOther); }
221 bool match(const Null& aOther) { return true; }
224 return aOther.mData.match(Matcher(*this));
227 OriginScope Clone() { return OriginScope(mData); }
229 private:
230 // Move constructors
231 explicit OriginScope(const Origin&& aOrigin) : mData(aOrigin) {}
233 explicit OriginScope(const Prefix&& aPrefix) : mData(aPrefix) {}
235 explicit OriginScope(const Pattern&& aPattern) : mData(aPattern) {}
237 explicit OriginScope(const Null&& aNull) : mData(aNull) {}
239 // Copy constructor
240 explicit OriginScope(const DataType& aOther) : mData(aOther) {}
242 bool MatchesOrigin(const Origin& aOther) const {
243 struct OriginMatcher {
244 const Origin& mOther;
246 explicit OriginMatcher(const Origin& aOther) : mOther(aOther) {}
248 bool match(const Origin& aThis) {
249 return aThis.GetOrigin().Equals(mOther.GetOrigin());
252 bool match(const Prefix& aThis) {
253 return aThis.GetOriginNoSuffix().Equals(mOther.GetOriginNoSuffix());
256 bool match(const Pattern& aThis) {
257 return aThis.GetPattern().Matches(mOther.GetAttributes());
260 bool match(const Null& aThis) {
261 // Null covers everything.
262 return true;
266 return mData.match(OriginMatcher(aOther));
269 bool MatchesPrefix(const Prefix& aOther) const {
270 struct PrefixMatcher {
271 const Prefix& mOther;
273 explicit PrefixMatcher(const Prefix& aOther) : mOther(aOther) {}
275 bool match(const Origin& aThis) {
276 return aThis.GetOriginNoSuffix().Equals(mOther.GetOriginNoSuffix());
279 bool match(const Prefix& aThis) {
280 return aThis.GetOriginNoSuffix().Equals(mOther.GetOriginNoSuffix());
283 bool match(const Pattern& aThis) {
284 // The match will be always true here because any origin attributes
285 // pattern overlaps any origin prefix (an origin prefix targets all
286 // origin attributes).
287 return true;
290 bool match(const Null& aThis) {
291 // Null covers everything.
292 return true;
296 return mData.match(PrefixMatcher(aOther));
299 bool MatchesPattern(const Pattern& aOther) const {
300 struct PatternMatcher {
301 const Pattern& mOther;
303 explicit PatternMatcher(const Pattern& aOther) : mOther(aOther) {}
305 bool match(const Origin& aThis) {
306 return mOther.GetPattern().Matches(aThis.GetAttributes());
309 bool match(const Prefix& aThis) {
310 // The match will be always true here because any origin attributes
311 // pattern overlaps any origin prefix (an origin prefix targets all
312 // origin attributes).
313 return true;
316 bool match(const Pattern& aThis) {
317 return aThis.GetPattern().Overlaps(mOther.GetPattern());
320 bool match(const Null& aThis) {
321 // Null covers everything.
322 return true;
326 PatternMatcher patternMatcher(aOther);
327 return mData.match(PatternMatcher(aOther));
330 bool operator==(const OriginScope& aOther) = delete;
333 END_QUOTA_NAMESPACE
335 #endif // mozilla_dom_quota_originorpatternstring_h__