2013-03-26 15:13:17 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-05-03 22:32:37 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-03-26 15:13:17 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_quota_originorpatternstring_h__
|
|
|
|
#define mozilla_dom_quota_originorpatternstring_h__
|
|
|
|
|
|
|
|
#include "mozilla/dom/quota/QuotaCommon.h"
|
|
|
|
|
2016-06-05 22:42:48 +03:00
|
|
|
#include "mozilla/BasePrincipal.h"
|
2018-09-07 19:22:50 +03:00
|
|
|
#include "mozilla/Variant.h"
|
2016-06-05 22:42:48 +03:00
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
BEGIN_QUOTA_NAMESPACE
|
|
|
|
|
2016-06-05 22:42:48 +03:00
|
|
|
class OriginScope {
|
2018-09-07 19:22:50 +03:00
|
|
|
class Origin {
|
2016-08-20 15:50:20 +03:00
|
|
|
nsCString mOrigin;
|
2018-09-07 19:22:50 +03:00
|
|
|
nsCString mOriginNoSuffix;
|
|
|
|
UniquePtr<OriginAttributes> mAttributes;
|
2016-08-20 15:50:20 +03:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
public:
|
|
|
|
explicit Origin(const nsACString& aOrigin) : mOrigin(aOrigin) {
|
|
|
|
InitMembers();
|
|
|
|
}
|
|
|
|
|
|
|
|
Origin(const Origin& aOther)
|
2016-08-20 15:50:20 +03:00
|
|
|
: mOrigin(aOther.mOrigin),
|
2018-09-07 19:22:50 +03:00
|
|
|
mOriginNoSuffix(aOther.mOriginNoSuffix),
|
|
|
|
mAttributes(MakeUnique<OriginAttributes>(*aOther.mAttributes)) {}
|
|
|
|
|
|
|
|
Origin(Origin&& aOther) = default;
|
|
|
|
|
|
|
|
const nsACString& GetOrigin() const { return mOrigin; }
|
2016-08-20 15:50:20 +03:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
void SetOrigin(const nsACString& aOrigin) {
|
|
|
|
mOrigin = aOrigin;
|
|
|
|
|
|
|
|
InitMembers();
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsACString& GetOriginNoSuffix() const { return mOriginNoSuffix; }
|
|
|
|
|
|
|
|
const OriginAttributes& GetAttributes() const {
|
|
|
|
MOZ_ASSERT(mAttributes);
|
2016-08-20 15:50:20 +03:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
return *mAttributes;
|
2016-08-20 15:50:20 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
private:
|
|
|
|
void InitMembers() {
|
|
|
|
mAttributes = MakeUnique<OriginAttributes>();
|
|
|
|
|
|
|
|
MOZ_ALWAYS_TRUE(
|
|
|
|
mAttributes->PopulateFromOrigin(mOrigin, mOriginNoSuffix));
|
2016-08-20 15:50:20 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
class Prefix {
|
|
|
|
nsCString mOriginNoSuffix;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit Prefix(const nsACString& aOriginNoSuffix)
|
|
|
|
: mOriginNoSuffix(aOriginNoSuffix) {}
|
|
|
|
|
|
|
|
const nsCString& GetOriginNoSuffix() const { return mOriginNoSuffix; }
|
|
|
|
|
|
|
|
void SetOriginNoSuffix(const nsACString& aOriginNoSuffix) {
|
|
|
|
mOriginNoSuffix = aOriginNoSuffix;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Pattern {
|
|
|
|
UniquePtr<OriginAttributesPattern> mPattern;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit Pattern(const OriginAttributesPattern& aPattern)
|
|
|
|
: mPattern(MakeUnique<OriginAttributesPattern>(aPattern)) {}
|
|
|
|
|
|
|
|
explicit Pattern(const nsAString& aJSONPattern)
|
|
|
|
: mPattern(MakeUnique<OriginAttributesPattern>()) {
|
|
|
|
MOZ_ALWAYS_TRUE(mPattern->Init(aJSONPattern));
|
|
|
|
}
|
|
|
|
|
|
|
|
Pattern(const Pattern& aOther)
|
|
|
|
: mPattern(MakeUnique<OriginAttributesPattern>(*aOther.mPattern)) {}
|
|
|
|
|
|
|
|
Pattern(Pattern&& aOther) = default;
|
|
|
|
|
|
|
|
const OriginAttributesPattern& GetPattern() const {
|
|
|
|
MOZ_ASSERT(mPattern);
|
|
|
|
|
|
|
|
return *mPattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetPattern(const OriginAttributesPattern& aPattern) {
|
|
|
|
mPattern = MakeUnique<OriginAttributesPattern>(aPattern);
|
|
|
|
}
|
2016-08-20 15:50:20 +03:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
nsString GetJSONPattern() const {
|
|
|
|
MOZ_ASSERT(mPattern);
|
2016-08-20 15:50:20 +03:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
nsString result;
|
|
|
|
MOZ_ALWAYS_TRUE(mPattern->ToJSON(result));
|
2016-08-17 05:57:18 +03:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
return result;
|
|
|
|
}
|
2016-08-20 15:50:20 +03:00
|
|
|
};
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
struct Null {};
|
|
|
|
|
|
|
|
using DataType = Variant<Origin, Prefix, Pattern, Null>;
|
|
|
|
|
|
|
|
DataType mData;
|
2016-08-20 15:50:20 +03:00
|
|
|
|
|
|
|
public:
|
2018-09-07 19:22:50 +03:00
|
|
|
OriginScope() : mData(Null()) {}
|
|
|
|
|
2013-03-26 15:13:17 +04:00
|
|
|
static OriginScope FromOrigin(const nsACString& aOrigin) {
|
2018-09-07 19:22:50 +03:00
|
|
|
return OriginScope(std::move(Origin(aOrigin)));
|
2013-03-26 15:13:17 +04:00
|
|
|
}
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
static OriginScope FromPrefix(const nsACString& aPrefix) {
|
|
|
|
return OriginScope(std::move(Prefix(aPrefix)));
|
2016-06-05 22:42:48 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
static OriginScope FromPattern(const OriginAttributesPattern& aPattern) {
|
|
|
|
return OriginScope(std::move(Pattern(aPattern)));
|
2013-03-26 15:13:17 +04:00
|
|
|
}
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
static OriginScope FromJSONPattern(const nsAString& aJSONPattern) {
|
|
|
|
return OriginScope(std::move(Pattern(aJSONPattern)));
|
2016-08-17 05:57:18 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
static OriginScope FromNull() { return OriginScope(std::move(Null())); }
|
2016-08-20 15:50:20 +03:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
bool IsOrigin() const { return mData.is<Origin>(); }
|
2013-03-26 15:13:17 +04:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
bool IsPrefix() const { return mData.is<Prefix>(); }
|
2013-09-11 08:18:36 +04:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
bool IsPattern() const { return mData.is<Pattern>(); }
|
2016-08-17 05:57:18 +03:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
bool IsNull() const { return mData.is<Null>(); }
|
2013-09-11 08:18:36 +04:00
|
|
|
|
2015-11-22 12:43:55 +03:00
|
|
|
void SetFromOrigin(const nsACString& aOrigin) {
|
2018-09-07 19:22:50 +03:00
|
|
|
mData = AsVariant(Origin(aOrigin));
|
2016-06-05 22:42:48 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
void SetFromPrefix(const nsACString& aPrefix) {
|
|
|
|
mData = AsVariant(Prefix(aPrefix));
|
2015-11-22 12:43:55 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
void SetFromPattern(const OriginAttributesPattern& aPattern) {
|
|
|
|
mData = AsVariant(Pattern(aPattern));
|
2015-11-22 12:43:55 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
void SetFromJSONPattern(const nsAString& aJSONPattern) {
|
|
|
|
mData = AsVariant(Pattern(aJSONPattern));
|
2016-08-17 05:57:18 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
void SetFromNull() { mData = AsVariant(Null()); }
|
2015-11-22 12:43:55 +03:00
|
|
|
|
2016-06-05 22:42:48 +03:00
|
|
|
const nsACString& GetOrigin() const {
|
|
|
|
MOZ_ASSERT(IsOrigin());
|
2016-08-20 15:50:20 +03:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
return mData.as<Origin>().GetOrigin();
|
2016-06-05 22:42:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetOrigin(const nsACString& aOrigin) {
|
|
|
|
MOZ_ASSERT(IsOrigin());
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
mData.as<Origin>().SetOrigin(aOrigin);
|
2016-06-05 22:42:48 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
const nsACString& GetOriginNoSuffix() const {
|
|
|
|
MOZ_ASSERT(IsOrigin() || IsPrefix());
|
2016-08-17 05:57:18 +03:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
if (IsOrigin()) {
|
|
|
|
return mData.as<Origin>().GetOriginNoSuffix();
|
|
|
|
}
|
|
|
|
return mData.as<Prefix>().GetOriginNoSuffix();
|
2016-08-17 05:57:18 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
void SetOriginNoSuffix(const nsACString& aOriginNoSuffix) {
|
2016-08-17 05:57:18 +03:00
|
|
|
MOZ_ASSERT(IsPrefix());
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
mData.as<Prefix>().SetOriginNoSuffix(aOriginNoSuffix);
|
2016-08-17 05:57:18 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
const OriginAttributesPattern& GetPattern() const {
|
|
|
|
MOZ_ASSERT(IsPattern());
|
2016-06-05 22:42:48 +03:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
return mData.as<Pattern>().GetPattern();
|
2016-06-05 22:42:48 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
nsString GetJSONPattern() const {
|
|
|
|
MOZ_ASSERT(IsPattern());
|
2016-08-17 05:57:18 +03:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
return mData.as<Pattern>().GetJSONPattern();
|
2016-08-17 05:57:18 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
void SetPattern(const OriginAttributesPattern& aPattern) {
|
|
|
|
MOZ_ASSERT(IsPattern());
|
2016-06-05 22:42:48 +03:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
mData.as<Pattern>().SetPattern(aPattern);
|
2016-06-05 22:42:48 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
bool Matches(const OriginScope& aOther) const {
|
|
|
|
struct Matcher {
|
|
|
|
const OriginScope& mThis;
|
|
|
|
|
|
|
|
explicit Matcher(const OriginScope& aThis) : mThis(aThis) {}
|
|
|
|
|
2019-04-02 14:53:47 +03:00
|
|
|
bool operator()(const Origin& aOther) {
|
|
|
|
return mThis.MatchesOrigin(aOther);
|
|
|
|
}
|
2018-09-07 19:22:50 +03:00
|
|
|
|
2019-04-02 14:53:47 +03:00
|
|
|
bool operator()(const Prefix& aOther) {
|
|
|
|
return mThis.MatchesPrefix(aOther);
|
|
|
|
}
|
2018-09-07 19:22:50 +03:00
|
|
|
|
2019-04-02 14:53:47 +03:00
|
|
|
bool operator()(const Pattern& aOther) {
|
|
|
|
return mThis.MatchesPattern(aOther);
|
|
|
|
}
|
2018-09-07 19:22:50 +03:00
|
|
|
|
2019-04-02 14:53:47 +03:00
|
|
|
bool operator()(const Null& aOther) { return true; }
|
2018-09-07 19:22:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return aOther.mData.match(Matcher(*this));
|
2016-06-05 22:42:48 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
OriginScope Clone() { return OriginScope(mData); }
|
2016-06-05 22:42:48 +03:00
|
|
|
|
2019-08-22 23:51:09 +03:00
|
|
|
template <typename T, typename U>
|
|
|
|
class Setter {
|
|
|
|
typedef void (OriginScope::*Method)(const U& aOrigin);
|
|
|
|
|
|
|
|
OriginScope* mOriginScope;
|
|
|
|
Method mMethod;
|
|
|
|
T mData;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Setter(OriginScope* aOriginScope, Method aMethod)
|
|
|
|
: mOriginScope(aOriginScope), mMethod(aMethod) {}
|
|
|
|
|
|
|
|
Setter(Setter&& aOther)
|
|
|
|
: mOriginScope(aOther.mOriginScope),
|
|
|
|
mMethod(aOther.mMethod),
|
|
|
|
mData(std::move(aOther.mData)) {}
|
|
|
|
|
|
|
|
~Setter() { ((*mOriginScope).*mMethod)(mData); }
|
|
|
|
|
|
|
|
operator T&() { return mData; }
|
|
|
|
|
|
|
|
operator T*() { return &mData; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Setter() = delete;
|
|
|
|
Setter(const Setter&) = delete;
|
|
|
|
Setter& operator=(const Setter&) = delete;
|
|
|
|
Setter& operator=(const Setter&&) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Magic helper for cases where you are calling a method that takes a
|
|
|
|
* ns(A)CString outparam to write an origin into. This method returns a helper
|
|
|
|
* temporary that returns the underlying string storage, then on the
|
|
|
|
* destruction of the temporary at the conclusion of the call, automatically
|
|
|
|
* invokes SetFromOrigin using the origin value that was written into the
|
|
|
|
* string.
|
|
|
|
*/
|
|
|
|
Setter<nsCString, nsACString> AsOriginSetter() {
|
|
|
|
return Setter<nsCString, nsACString>(this, &OriginScope::SetFromOrigin);
|
|
|
|
}
|
|
|
|
|
2015-06-30 15:59:27 +03:00
|
|
|
private:
|
2018-09-07 19:22:50 +03:00
|
|
|
// Move constructors
|
|
|
|
explicit OriginScope(const Origin&& aOrigin) : mData(aOrigin) {}
|
2016-08-20 15:50:20 +03:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
explicit OriginScope(const Prefix&& aPrefix) : mData(aPrefix) {}
|
2016-06-05 22:42:48 +03:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
explicit OriginScope(const Pattern&& aPattern) : mData(aPattern) {}
|
2016-06-05 22:42:48 +03:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
explicit OriginScope(const Null&& aNull) : mData(aNull) {}
|
|
|
|
|
|
|
|
// Copy constructor
|
|
|
|
explicit OriginScope(const DataType& aOther) : mData(aOther) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
bool MatchesOrigin(const Origin& aOther) const {
|
|
|
|
struct OriginMatcher {
|
|
|
|
const Origin& mOther;
|
|
|
|
|
|
|
|
explicit OriginMatcher(const Origin& aOther) : mOther(aOther) {}
|
|
|
|
|
2019-04-02 14:53:47 +03:00
|
|
|
bool operator()(const Origin& aThis) {
|
2018-09-07 19:22:50 +03:00
|
|
|
return aThis.GetOrigin().Equals(mOther.GetOrigin());
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:53:47 +03:00
|
|
|
bool operator()(const Prefix& aThis) {
|
2018-09-07 19:22:50 +03:00
|
|
|
return aThis.GetOriginNoSuffix().Equals(mOther.GetOriginNoSuffix());
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:53:47 +03:00
|
|
|
bool operator()(const Pattern& aThis) {
|
2018-09-07 19:22:50 +03:00
|
|
|
return aThis.GetPattern().Matches(mOther.GetAttributes());
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:53:47 +03:00
|
|
|
bool operator()(const Null& aThis) {
|
2018-09-07 19:22:50 +03:00
|
|
|
// Null covers everything.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return mData.match(OriginMatcher(aOther));
|
2016-06-05 22:42:48 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
bool MatchesPrefix(const Prefix& aOther) const {
|
|
|
|
struct PrefixMatcher {
|
|
|
|
const Prefix& mOther;
|
|
|
|
|
|
|
|
explicit PrefixMatcher(const Prefix& aOther) : mOther(aOther) {}
|
|
|
|
|
2019-04-02 14:53:47 +03:00
|
|
|
bool operator()(const Origin& aThis) {
|
2018-09-07 19:22:50 +03:00
|
|
|
return aThis.GetOriginNoSuffix().Equals(mOther.GetOriginNoSuffix());
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:53:47 +03:00
|
|
|
bool operator()(const Prefix& aThis) {
|
2018-09-07 19:22:50 +03:00
|
|
|
return aThis.GetOriginNoSuffix().Equals(mOther.GetOriginNoSuffix());
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:53:47 +03:00
|
|
|
bool operator()(const Pattern& aThis) {
|
2018-09-07 19:22:50 +03:00
|
|
|
// The match will be always true here because any origin attributes
|
|
|
|
// pattern overlaps any origin prefix (an origin prefix targets all
|
|
|
|
// origin attributes).
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:53:47 +03:00
|
|
|
bool operator()(const Null& aThis) {
|
2018-09-07 19:22:50 +03:00
|
|
|
// Null covers everything.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return mData.match(PrefixMatcher(aOther));
|
|
|
|
}
|
2013-03-26 15:13:17 +04:00
|
|
|
|
2018-09-07 19:22:50 +03:00
|
|
|
bool MatchesPattern(const Pattern& aOther) const {
|
|
|
|
struct PatternMatcher {
|
|
|
|
const Pattern& mOther;
|
|
|
|
|
|
|
|
explicit PatternMatcher(const Pattern& aOther) : mOther(aOther) {}
|
|
|
|
|
2019-04-02 14:53:47 +03:00
|
|
|
bool operator()(const Origin& aThis) {
|
2018-09-07 19:22:50 +03:00
|
|
|
return mOther.GetPattern().Matches(aThis.GetAttributes());
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:53:47 +03:00
|
|
|
bool operator()(const Prefix& aThis) {
|
2018-09-07 19:22:50 +03:00
|
|
|
// The match will be always true here because any origin attributes
|
|
|
|
// pattern overlaps any origin prefix (an origin prefix targets all
|
|
|
|
// origin attributes).
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:53:47 +03:00
|
|
|
bool operator()(const Pattern& aThis) {
|
2018-09-07 19:22:50 +03:00
|
|
|
return aThis.GetPattern().Overlaps(mOther.GetPattern());
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:53:47 +03:00
|
|
|
bool operator()(const Null& aThis) {
|
2018-09-07 19:22:50 +03:00
|
|
|
// Null covers everything.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
PatternMatcher patternMatcher(aOther);
|
|
|
|
return mData.match(PatternMatcher(aOther));
|
2016-08-20 15:50:20 +03:00
|
|
|
}
|
|
|
|
|
2015-06-30 15:59:27 +03:00
|
|
|
bool operator==(const OriginScope& aOther) = delete;
|
2013-03-26 15:13:17 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
END_QUOTA_NAMESPACE
|
|
|
|
|
|
|
|
#endif // mozilla_dom_quota_originorpatternstring_h__
|