Bug 1866217 - Introduce PersistenceScope class à la OriginScope; r=dom-storage-reviewers,jari

Directory locks currently only support specifying a concrete persistence type
or all persistence types. There are situations like temporary storage
initialization when multiple persistence types need to be specified. It would
be possible to create multiple directory locks for that, but that would also
introduce non-trivial additional complexity. A new PersistenceScope class in
the style of OriginScope will make it easy to specify multiple persistence
types. Support for actual persistence type sets will be added in a separate
patch, this patch just adds basic functionality.

Differential Revision: https://phabricator.services.mozilla.com/D195366
This commit is contained in:
Jan Varga 2024-08-14 12:46:14 +00:00
parent 633ae76c30
commit 1311af9ad2
5 changed files with 173 additions and 0 deletions

View File

@ -120,6 +120,7 @@ class OriginScope {
public:
OriginScope() : mData(Null()) {}
// XXX Consider renaming these static methods to Create
static OriginScope FromOrigin(const nsACString& aOrigin) {
return OriginScope(std::move(Origin(aOrigin)));
}

View File

@ -0,0 +1,109 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 DOM_QUOTA_PERSISTENCESCOPE_H_
#define DOM_QUOTA_PERSISTENCESCOPE_H_
#include "mozilla/Assertions.h"
#include "mozilla/Variant.h"
#include "mozilla/dom/quota/PersistenceType.h"
namespace mozilla::dom::quota {
class PersistenceScope {
class Value {
PersistenceType mValue;
public:
explicit Value(PersistenceType aValue) : mValue(aValue) {}
PersistenceType GetValue() const { return mValue; }
};
struct Null {};
using DataType = Variant<Value, Null>;
DataType mData;
public:
PersistenceScope() : mData(Null()) {}
// XXX Consider renaming these static methods to Create
static PersistenceScope CreateFromValue(PersistenceType aValue) {
return PersistenceScope(std::move(Value(aValue)));
}
static PersistenceScope CreateFromNull() {
return PersistenceScope(std::move(Null()));
}
bool IsValue() const { return mData.is<Value>(); }
bool IsNull() const { return mData.is<Null>(); }
void SetFromValue(PersistenceType aValue) {
mData = AsVariant(Value(aValue));
}
void SetFromNull() { mData = AsVariant(Null()); }
PersistenceType GetValue() const {
MOZ_ASSERT(IsValue());
return mData.as<Value>().GetValue();
}
bool Matches(const PersistenceScope& aOther) const {
struct Matcher {
const PersistenceScope& mThis;
explicit Matcher(const PersistenceScope& aThis) : mThis(aThis) {}
bool operator()(const Value& aOther) {
return mThis.MatchesValue(aOther);
}
bool operator()(const Null& aOther) { return true; }
};
return aOther.mData.match(Matcher(*this));
}
private:
// Move constructors
explicit PersistenceScope(const Value&& aValue) : mData(aValue) {}
explicit PersistenceScope(const Null&& aNull) : mData(aNull) {}
// Copy constructor
explicit PersistenceScope(const DataType& aOther) : mData(aOther) {}
bool MatchesValue(const Value& aOther) const {
struct ValueMatcher {
const Value& mOther;
explicit ValueMatcher(const Value& aOther) : mOther(aOther) {}
bool operator()(const Value& aThis) {
return aThis.GetValue() == mOther.GetValue();
}
bool operator()(const Null& aThis) {
// Null covers everything.
return true;
}
};
return mData.match(ValueMatcher(aOther));
}
bool operator==(const PersistenceScope& aOther) = delete;
};
} // namespace mozilla::dom::quota
#endif // DOM_QUOTA_PERSISTENCESCOPE_H_

View File

@ -64,6 +64,7 @@ EXPORTS.mozilla.dom.quota += [
"NSSCipherStrategy.h",
"OriginOperationCallbacks.h",
"OriginScope.h",
"PersistenceScope.h",
"PersistenceType.h",
"PromiseUtils.h",
"QuotaCommon.h",

View File

@ -0,0 +1,61 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "gtest/gtest.h"
#include "mozilla/dom/quota/PersistenceScope.h"
namespace mozilla::dom::quota {
TEST(DOM_Quota_PersistenceScope, SanityChecks)
{
// Sanity checks.
{
const auto persistenceScope(
PersistenceScope::CreateFromValue(PERSISTENCE_TYPE_PERSISTENT));
ASSERT_TRUE(persistenceScope.IsValue());
ASSERT_EQ(persistenceScope.GetValue(), PERSISTENCE_TYPE_PERSISTENT);
}
{
const auto persistenceScope(PersistenceScope::CreateFromNull());
ASSERT_TRUE(persistenceScope.IsNull());
}
}
TEST(DOM_Quota_PersistenceScope, MatchesValue)
{
// Test each persistence scope type against particular persistence types.
{
const auto persistenceScope(
PersistenceScope::CreateFromValue(PERSISTENCE_TYPE_PERSISTENT));
ASSERT_TRUE(persistenceScope.Matches(
PersistenceScope::CreateFromValue(PERSISTENCE_TYPE_PERSISTENT)));
ASSERT_FALSE(persistenceScope.Matches(
PersistenceScope::CreateFromValue(PERSISTENCE_TYPE_TEMPORARY)));
ASSERT_FALSE(persistenceScope.Matches(
PersistenceScope::CreateFromValue(PERSISTENCE_TYPE_DEFAULT)));
ASSERT_FALSE(persistenceScope.Matches(
PersistenceScope::CreateFromValue(PERSISTENCE_TYPE_PRIVATE)));
}
{
const auto persistenceScope(PersistenceScope::CreateFromNull());
ASSERT_TRUE(persistenceScope.Matches(
PersistenceScope::CreateFromValue(PERSISTENCE_TYPE_PERSISTENT)));
ASSERT_TRUE(persistenceScope.Matches(
PersistenceScope::CreateFromValue(PERSISTENCE_TYPE_TEMPORARY)));
ASSERT_TRUE(persistenceScope.Matches(
PersistenceScope::CreateFromValue(PERSISTENCE_TYPE_DEFAULT)));
ASSERT_TRUE(persistenceScope.Matches(
PersistenceScope::CreateFromValue(PERSISTENCE_TYPE_PRIVATE)));
}
}
} // namespace mozilla::dom::quota

View File

@ -25,6 +25,7 @@ UNIFIED_SOURCES = [
"TestForwardDecls.cpp",
"TestOriginParser.cpp",
"TestOriginScope.cpp",
"TestPersistenceScope.cpp",
"TestPersistenceType.cpp",
"TestQMResult.cpp",
"TestQuotaCommon.cpp",