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__
11 #include "mozilla/Assertions.h"
12 #include "mozilla/OriginAttributes.h"
13 #include "mozilla/UniquePtr.h"
14 #include "mozilla/Variant.h"
15 #include "mozilla/dom/quota/CommonMetadata.h"
16 #include "nsStringFlags.h"
17 #include "nsStringFwd.h"
19 namespace mozilla::dom::quota
{
23 const PrincipalMetadata mPrincipalMetadata
;
24 nsCString mOriginNoSuffix
;
25 UniquePtr
<OriginAttributes
> mAttributes
;
28 explicit Origin(const PrincipalMetadata
& aPrincipalMetadata
)
29 : mPrincipalMetadata(aPrincipalMetadata
) {
33 Origin(const Origin
& aOther
)
34 : mPrincipalMetadata(aOther
.mPrincipalMetadata
),
35 mOriginNoSuffix(aOther
.mOriginNoSuffix
),
36 mAttributes(MakeUnique
<OriginAttributes
>(*aOther
.mAttributes
)) {}
38 Origin(Origin
&& aOther
) = default;
40 const PrincipalMetadata
& GetPrincipalMetadata() const {
41 return mPrincipalMetadata
;
44 const nsACString
& GetGroup() const { return mPrincipalMetadata
.mGroup
; }
46 const nsACString
& GetOrigin() const { return mPrincipalMetadata
.mOrigin
; }
48 const nsACString
& GetOriginNoSuffix() const { return mOriginNoSuffix
; }
50 const OriginAttributes
& GetAttributes() const {
51 MOZ_ASSERT(mAttributes
);
58 mAttributes
= MakeUnique
<OriginAttributes
>();
60 MOZ_ALWAYS_TRUE(mAttributes
->PopulateFromOrigin(
61 mPrincipalMetadata
.mOrigin
, mOriginNoSuffix
));
66 const PrincipalMetadata mPrincipalMetadata
;
67 nsCString mGroupNoSuffix
;
68 nsCString mOriginNoSuffix
;
71 explicit Prefix(const PrincipalMetadata
& aPrincipalMetadata
)
72 : mGroupNoSuffix(aPrincipalMetadata
.mGroup
),
73 mOriginNoSuffix(aPrincipalMetadata
.mOrigin
) {}
75 const nsACString
& GetGroupNoSuffix() const { return mGroupNoSuffix
; }
77 const nsCString
& GetOriginNoSuffix() const { return mOriginNoSuffix
; }
82 nsCString mGroupNoSuffix
;
83 UniquePtr
<OriginAttributes
> mAttributes
;
86 explicit Group(const nsACString
& aGroup
) : mGroup(aGroup
) { InitMembers(); }
88 Group(const Group
& aOther
)
89 : mGroup(aOther
.mGroup
),
90 mGroupNoSuffix(aOther
.mGroupNoSuffix
),
91 mAttributes(MakeUnique
<OriginAttributes
>(*aOther
.mAttributes
)) {}
93 Group(Group
&& aOther
) = default;
95 const nsACString
& GetGroup() const { return mGroup
; }
97 const nsACString
& GetGroupNoSuffix() const { return mGroupNoSuffix
; }
99 const OriginAttributes
& GetAttributes() const {
100 MOZ_ASSERT(mAttributes
);
107 mAttributes
= MakeUnique
<OriginAttributes
>();
109 MOZ_ALWAYS_TRUE(mAttributes
->PopulateFromOrigin(mGroup
, mGroupNoSuffix
));
114 UniquePtr
<OriginAttributesPattern
> mPattern
;
117 explicit Pattern(const OriginAttributesPattern
& aPattern
)
118 : mPattern(MakeUnique
<OriginAttributesPattern
>(aPattern
)) {}
120 explicit Pattern(const nsAString
& aJSONPattern
)
121 : mPattern(MakeUnique
<OriginAttributesPattern
>()) {
122 MOZ_ALWAYS_TRUE(mPattern
->Init(aJSONPattern
));
125 Pattern(const Pattern
& aOther
)
126 : mPattern(MakeUnique
<OriginAttributesPattern
>(*aOther
.mPattern
)) {}
128 Pattern(Pattern
&& aOther
) = default;
130 const OriginAttributesPattern
& GetPattern() const {
131 MOZ_ASSERT(mPattern
);
136 nsString
GetJSONPattern() const {
137 MOZ_ASSERT(mPattern
);
140 MOZ_ALWAYS_TRUE(mPattern
->ToJSON(result
));
148 using DataType
= Variant
<Origin
, Prefix
, Group
, Pattern
, Null
>;
153 OriginScope() : mData(Null()) {}
155 // XXX Consider renaming these static methods to Create
156 static OriginScope
FromOrigin(const PrincipalMetadata
& aPrincipalMetadata
) {
157 return OriginScope(std::move(Origin(aPrincipalMetadata
)));
160 static OriginScope
FromPrefix(const PrincipalMetadata
& aPrincipalMetadata
) {
161 return OriginScope(std::move(Prefix(aPrincipalMetadata
)));
164 static OriginScope
FromGroup(const nsACString
& aGroup
) {
165 return OriginScope(std::move(Group(aGroup
)));
168 static OriginScope
FromPattern(const OriginAttributesPattern
& aPattern
) {
169 return OriginScope(std::move(Pattern(aPattern
)));
172 static OriginScope
FromJSONPattern(const nsAString
& aJSONPattern
) {
173 return OriginScope(std::move(Pattern(aJSONPattern
)));
176 static OriginScope
FromNull() { return OriginScope(std::move(Null())); }
178 bool IsOrigin() const { return mData
.is
<Origin
>(); }
180 bool IsPrefix() const { return mData
.is
<Prefix
>(); }
182 bool IsPattern() const { return mData
.is
<Pattern
>(); }
184 bool IsNull() const { return mData
.is
<Null
>(); }
186 void SetFromOrigin(const PrincipalMetadata
& aPrincipalMetadata
) {
187 mData
= AsVariant(Origin(aPrincipalMetadata
));
190 void SetFromPrefix(const PrincipalMetadata
& aPrincipalMetadata
) {
191 mData
= AsVariant(Prefix(aPrincipalMetadata
));
194 void SetFromPattern(const OriginAttributesPattern
& aPattern
) {
195 mData
= AsVariant(Pattern(aPattern
));
198 void SetFromJSONPattern(const nsAString
& aJSONPattern
) {
199 mData
= AsVariant(Pattern(aJSONPattern
));
202 void SetFromNull() { mData
= AsVariant(Null()); }
204 const PrincipalMetadata
& GetPrincipalMetadata() const {
205 MOZ_ASSERT(IsOrigin());
207 return mData
.as
<Origin
>().GetPrincipalMetadata();
210 const nsACString
& GetOrigin() const {
211 MOZ_ASSERT(IsOrigin());
213 return mData
.as
<Origin
>().GetOrigin();
216 const nsACString
& GetOriginNoSuffix() const {
217 MOZ_ASSERT(IsOrigin() || IsPrefix());
220 return mData
.as
<Origin
>().GetOriginNoSuffix();
222 return mData
.as
<Prefix
>().GetOriginNoSuffix();
225 const OriginAttributesPattern
& GetPattern() const {
226 MOZ_ASSERT(IsPattern());
228 return mData
.as
<Pattern
>().GetPattern();
231 nsString
GetJSONPattern() const {
232 MOZ_ASSERT(IsPattern());
234 return mData
.as
<Pattern
>().GetJSONPattern();
237 bool Matches(const OriginScope
& aOther
) const {
239 const OriginScope
& mThis
;
241 explicit Matcher(const OriginScope
& aThis
) : mThis(aThis
) {}
243 bool operator()(const Origin
& aOther
) {
244 return mThis
.MatchesOrigin(aOther
);
247 bool operator()(const Prefix
& aOther
) {
248 return mThis
.MatchesPrefix(aOther
);
251 bool operator()(const Group
& aOther
) {
252 return mThis
.MatchesGroup(aOther
);
255 bool operator()(const Pattern
& aOther
) {
256 return mThis
.MatchesPattern(aOther
);
259 bool operator()(const Null
& aOther
) { return true; }
262 return aOther
.mData
.match(Matcher(*this));
265 OriginScope
Clone() { return OriginScope(mData
); }
269 explicit OriginScope(const Origin
&& aOrigin
) : mData(aOrigin
) {}
271 explicit OriginScope(const Prefix
&& aPrefix
) : mData(aPrefix
) {}
273 explicit OriginScope(const Group
&& aGroup
) : mData(aGroup
) {}
275 explicit OriginScope(const Pattern
&& aPattern
) : mData(aPattern
) {}
277 explicit OriginScope(const Null
&& aNull
) : mData(aNull
) {}
280 explicit OriginScope(const DataType
& aOther
) : mData(aOther
) {}
282 bool MatchesOrigin(const Origin
& aOther
) const {
283 struct OriginMatcher
{
284 const Origin
& mOther
;
286 explicit OriginMatcher(const Origin
& aOther
) : mOther(aOther
) {}
288 bool operator()(const Origin
& aThis
) {
289 return aThis
.GetOrigin().Equals(mOther
.GetOrigin());
292 bool operator()(const Prefix
& aThis
) {
293 return aThis
.GetOriginNoSuffix().Equals(mOther
.GetOriginNoSuffix());
296 bool operator()(const Group
& aThis
) {
297 return aThis
.GetGroup().Equals(mOther
.GetGroup());
300 bool operator()(const Pattern
& aThis
) {
301 return aThis
.GetPattern().Matches(mOther
.GetAttributes());
304 bool operator()(const Null
& aThis
) {
305 // Null covers everything.
310 return mData
.match(OriginMatcher(aOther
));
313 bool MatchesPrefix(const Prefix
& aOther
) const {
314 struct PrefixMatcher
{
315 const Prefix
& mOther
;
317 explicit PrefixMatcher(const Prefix
& aOther
) : mOther(aOther
) {}
319 bool operator()(const Origin
& aThis
) {
320 return aThis
.GetOriginNoSuffix().Equals(mOther
.GetOriginNoSuffix());
323 bool operator()(const Prefix
& aThis
) {
324 return aThis
.GetOriginNoSuffix().Equals(mOther
.GetOriginNoSuffix());
327 bool operator()(const Group
& aThis
) {
328 return aThis
.GetGroupNoSuffix().Equals(mOther
.GetGroupNoSuffix());
331 bool operator()(const Pattern
& aThis
) {
332 // The match will be always true here because any origin attributes
333 // pattern overlaps any origin prefix (an origin prefix targets all
334 // origin attributes).
338 bool operator()(const Null
& aThis
) {
339 // Null covers everything.
344 return mData
.match(PrefixMatcher(aOther
));
347 bool MatchesGroup(const Group
& aOther
) const {
348 struct GroupMatcher
{
351 explicit GroupMatcher(const Group
& aOther
) : mOther(aOther
) {}
353 bool operator()(const Origin
& aThis
) {
354 return aThis
.GetGroup().Equals(mOther
.GetGroup());
357 bool operator()(const Prefix
& aThis
) {
358 return aThis
.GetGroupNoSuffix().Equals(mOther
.GetGroupNoSuffix());
361 bool operator()(const Group
& aThis
) {
362 return aThis
.GetGroup().Equals(mOther
.GetGroup());
365 bool operator()(const Pattern
& aThis
) {
366 return aThis
.GetPattern().Matches(mOther
.GetAttributes());
369 bool operator()(const Null
& aThis
) {
370 // Null covers everything.
375 return mData
.match(GroupMatcher(aOther
));
378 bool MatchesPattern(const Pattern
& aOther
) const {
379 struct PatternMatcher
{
380 const Pattern
& mOther
;
382 explicit PatternMatcher(const Pattern
& aOther
) : mOther(aOther
) {}
384 bool operator()(const Origin
& aThis
) {
385 return mOther
.GetPattern().Matches(aThis
.GetAttributes());
388 bool operator()(const Prefix
& aThis
) {
389 // The match will be always true here because any origin attributes
390 // pattern overlaps any origin prefix (an origin prefix targets all
391 // origin attributes).
395 bool operator()(const Group
& aThis
) {
396 return mOther
.GetPattern().Matches(aThis
.GetAttributes());
399 bool operator()(const Pattern
& aThis
) {
400 return aThis
.GetPattern().Overlaps(mOther
.GetPattern());
403 bool operator()(const Null
& aThis
) {
404 // Null covers everything.
409 PatternMatcher
patternMatcher(aOther
);
410 return mData
.match(PatternMatcher(aOther
));
413 bool operator==(const OriginScope
& aOther
) = delete;
416 } // namespace mozilla::dom::quota
418 #endif // mozilla_dom_quota_originorpatternstring_h__