Bug 1866402 - QM: Generalize the helper for mapping MozPromise values to support both exclusive and non-exclusive promises; r=dom-storage-reviewers,jari

Differential Revision: https://phabricator.services.mozilla.com/D223276
This commit is contained in:
Jan Varga 2024-10-09 22:16:49 +00:00
parent dca4a3ceec
commit 61bffae5df
2 changed files with 50 additions and 1 deletions

View File

@ -8,11 +8,31 @@
#define DOM_QUOTA_MOZPROMISEUTILS_H_
#include "mozilla/MozPromise.h"
#include "nsThreadUtils.h"
namespace mozilla::dom::quota {
namespace detail {
template <typename T>
struct IsExclusiveMozPromise {
static constexpr bool value = false;
};
// Specialization for MozPromise
template <typename ResolveValueT, typename RejectValueT, bool IsExclusive>
struct IsExclusiveMozPromise<
MozPromise<ResolveValueT, RejectValueT, IsExclusive>> {
static constexpr bool value = IsExclusive;
};
} // namespace detail
template <typename T, typename U, typename F>
RefPtr<T> Map(RefPtr<U> aPromise, F&& aFunc) {
auto Map(RefPtr<U> aPromise, F&& aFunc)
-> std::enable_if_t<
detail::IsExclusiveMozPromise<RemoveSmartPointer<U>>::value,
RefPtr<T>> {
return aPromise->Then(
GetCurrentSerialEventTarget(), __func__,
[func =
@ -27,6 +47,25 @@ RefPtr<T> Map(RefPtr<U> aPromise, F&& aFunc) {
});
}
template <typename T, typename U, typename F>
auto Map(RefPtr<U> aPromise, F&& aFunc)
-> std::enable_if_t<
!detail::IsExclusiveMozPromise<RemoveSmartPointer<U>>::value,
RefPtr<T>> {
return aPromise->Then(GetCurrentSerialEventTarget(), __func__,
[func = std::forward<F>(aFunc)](
const typename U::ResolveOrRejectValue& aValue) {
if (aValue.IsReject()) {
return T::CreateAndReject(aValue.RejectValue(),
__func__);
}
auto value = func(aValue);
return T::CreateAndResolve(value, __func__);
});
}
} // namespace mozilla::dom::quota
#endif // DOM_QUOTA_MOZPROMISEUTILS_H_

View File

@ -9,6 +9,16 @@
namespace mozilla::dom::quota::test {
TEST(DOM_Quota_MozPromiseUtils, BoolPromiseToBoolPromise)
{
auto value = QuotaManagerDependencyFixture::Await(Map<BoolPromise>(
BoolPromise::CreateAndResolve(true, __func__),
[](const BoolPromise::ResolveOrRejectValue& aValue) { return false; }));
ASSERT_TRUE(value.IsResolve());
ASSERT_FALSE(value.ResolveValue());
}
TEST(DOM_Quota_MozPromiseUtils, ExclusiveBoolPromiseToBoolPromise)
{
auto value = QuotaManagerDependencyFixture::Await(