/* 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 "mozilla/dom/cache/CacheOpChild.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/Request.h"
#include "mozilla/dom/Response.h"
#include "mozilla/dom/cache/BoundStorageKey.h"
#include "mozilla/dom/cache/BoundStorageKeyCache.h"
#include "mozilla/dom/cache/Cache.h"
#include "mozilla/dom/cache/CacheChild.h"
#include "mozilla/dom/cache/CacheStreamControlChild.h"
#include "mozilla/dom/cache/CacheWorkerRef.h"
namespace mozilla::dom {
// XXX Move this to ToJSValue.h
template <
typename T>
[[nodiscard]]
bool ToJSValue(JSContext* aCx,
const SafeRefPtr<T>& aArgument,
JS::MutableHandle<JS::Value> aValue) {
return ToJSValue(aCx, *aArgument.unsafeGetRawPtr(), aValue);
}
namespace cache {
using mozilla::ipc::PBackgroundChild;
using Promise = mozilla::dom::Promise;
namespace {
void AddWorkerRefToStreamChild(
const CacheReadStream& aReadStream,
const SafeRefPtr<CacheWorkerRef>& aWorkerRef) {
MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerRef);
CacheStreamControlChild* cacheControl =
static_cast<CacheStreamControlChild*>(aReadStream.control().AsChild());
if (cacheControl) {
cacheControl->SetWorkerRef(aWorkerRef.clonePtr());
}
}
void AddWorkerRefToStreamChild(
const CacheResponse& aResponse,
const SafeRefPtr<CacheWorkerRef>& aWorkerRef) {
MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerRef);
if (aResponse.body().isNothing()) {
return;
}
AddWorkerRefToStreamChild(aResponse.body().ref(), aWorkerRef);
}
void AddWorkerRefToStreamChild(
const CacheRequest& aRequest,
const SafeRefPtr<CacheWorkerRef>& aWorkerRef) {
MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerRef);
if (aRequest.body().isNothing()) {
return;
}
AddWorkerRefToStreamChild(aRequest.body().ref(), aWorkerRef);
}
}
// namespace
CacheOpChild::CacheOpChild(SafeRefPtr<CacheWorkerRef> aWorkerRef,
nsIGlobalObject* aGlobal, nsISupports* aParent,
RefPtr<Promise>& aPromise, ActorChild* aParentActor)
: mGlobal(aGlobal),
mParent(aParent),
mPromise(aPromise),
mParentActor(aParentActor) {
MOZ_DIAGNOSTIC_ASSERT(mGlobal);
MOZ_DIAGNOSTIC_ASSERT(mParent);
MOZ_DIAGNOSTIC_ASSERT(aPromise);
MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerRef);
SetWorkerRef(CacheWorkerRef::PreferBehavior(
std::move(aWorkerRef), CacheWorkerRef::eStrongWorkerRef));
}
CacheOpChild::CacheOpChild(SafeRefPtr<CacheWorkerRef> aWorkerRef,
nsIGlobalObject* aGlobal, nsISupports* aParent,
RefPtr<CacheStoragePromise>& aPromise,
ActorChild* aParentActor)
: mGlobal(aGlobal),
mParent(aParent),
mPromise(aPromise),
mParentActor(aParentActor) {
MOZ_DIAGNOSTIC_ASSERT(mGlobal);
MOZ_DIAGNOSTIC_ASSERT(mParent);
MOZ_DIAGNOSTIC_ASSERT(aPromise);
MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerRef);
SetWorkerRef(CacheWorkerRef::PreferBehavior(
std::move(aWorkerRef), CacheWorkerRef::eStrongWorkerRef));
}
CacheOpChild::~CacheOpChild() {
NS_ASSERT_OWNINGTHREAD(CacheOpChild);
MOZ_DIAGNOSTIC_ASSERT(!mPromise);
}
void CacheOpChild::ActorDestroy(ActorDestroyReason aReason) {
NS_ASSERT_OWNINGTHREAD(CacheOpChild);
// If the actor was terminated for some unknown reason, then indicate the
// operation is dead.
if (mPromise) {
HandleAndSettle<CacheOpResult::Tvoid_t>(ErrorResult(NS_ERROR_FAILURE));
mPromise.destroy();
}
mParentActor->NoteDeletedActor();
RemoveWorkerRef();
}
using StorageOpenResultType = std::pair<CacheChild*,
Namespace>;
template <CacheOpResult::Type OP_TYPE,
typename ResultType>
void CacheOpChild::SettlePromise(
ResultType&& aRes, ErrorResult&& aRv,
const RefPtr<CacheStoragePromise>& aThePromise) {
// picks the correct promise type using traits defined in BoundStorageKey.h
// and BoundStorageKeyCache.h
using TargetPromiseType =
typename dom::cachestorage_traits<OP_TYPE>::PromiseType;
auto* target =
static_cast<TargetPromiseType*>(aThePromise.get());
auto&& res = std::forward<ResultType>(aRes);
if (aRv.Failed()) {
target->Reject(std::move(aRv), __func__);
return;
}
using ValueType =
typename std::decay_t<decltype(aRes)>;
if constexpr (std::is_same_v<ValueType, JS::HandleValue>) {
// Not serializing JS types into MozPromise. Need to collapse JS values
// into their raw types here. Since this is internal private method and
// based on it's usage yet; just expecting Undefined or null values here.
MOZ_ASSERT(res.isNullOrUndefined());
target->Resolve(nullptr
/*implicitly converts to false for boolean types */,
__func__);
}
else if constexpr (std::is_same_v<ValueType, StorageOpenResultType>) {
// result would be of type CacheChild here and we need to properly wrap into
// holder class BoundStorageKeyCache before resolving promise
auto [cacheChild, ns] = res;
auto* cache =
new BoundStorageKeyCache(mGlobal, cacheChild, ns);
target->Resolve(RefPtr(cache), __func__);
}
else {
target->Resolve(std::forward<ResultType>(aRes), __func__);
}
}
template <CacheOpResult::Type OP_TYPE,
typename ResultType>
void CacheOpChild::SettlePromise(ResultType&& aRes, ErrorResult&& aRv,
const RefPtr<Promise>& aThePromise) {
if (aRv.Failed()) {
aThePromise->MaybeReject(aRv.StealNSResult());
}
else {
using ValueType =
typename std::decay_t<decltype(aRes)>;
if constexpr (std::is_same_v<ValueType, StorageOpenResultType>) {
// result would be of type CacheChild here and we need to properly wrap
// into holder class Cache before resolving promise
auto&& res = std::forward<ResultType>(aRes);
auto [cacheChild, ns] = res;
auto* cache =
new Cache(mGlobal, cacheChild, ns);
aThePromise->MaybeResolve(RefPtr(cache));
}
else {
aThePromise->MaybeResolve(std::forward<ResultType>(aRes));
}
}
}
template <CacheOpResult::Type OP_TYPE,
typename ResultType>
void CacheOpChild::Settle(ResultType&& aRes, ErrorResult&& aRv) {
if (mPromise->is<RefPtr<mozilla::dom::Promise>>()) {
auto targetPromise = mPromise->as<RefPtr<mozilla::dom::Promise>>();
MOZ_ASSERT(targetPromise);
SettlePromise<OP_TYPE>(std::forward<ResultType>(aRes), std::move(aRv),
targetPromise);
}
else if (mPromise->is<RefPtr<CacheStoragePromise>>()) {
auto targetPromise = mPromise->as<RefPtr<CacheStoragePromise>>();
MOZ_ASSERT(targetPromise);
SettlePromise<OP_TYPE>(std::forward<ResultType>(aRes), std::move(aRv),
targetPromise);
}
}
template <CacheOpResult::Type OP_TYPE,
typename TResponse>
void CacheOpChild::HandleAndSettle(TResponse&& aRes) {
using ValueType =
typename std::decay_t<decltype(aRes)>;
auto&& res = std::forward<TResponse>(aRes);
if constexpr (std::is_same_v<ValueType, Maybe<CacheResponse>>) {
if (res.isNothing()) {
Settle<OP_TYPE>(JS::UndefinedHandleValue);
return;
}
const CacheResponse& cacheResponse
= res.ref();
AddWorkerRefToStreamChild(cacheResponse, GetWorkerRefPtr());
RefPtr<Response> response = ToResponse(cacheResponse);
Settle<OP_TYPE>(response);
}
else if constexpr (std::is_same_v<ValueType, nsTArray<CacheResponse>>) {
AutoTArray<RefPtr<Response>,
256> responses;
/* This Source Code Form is subject to the terms of the Mozilla Public ,v .0 a of
();
for (uint32_t i template <ypename T>
AddWorkerRefToStreamChild(res[i], GetWorkerRefPtr());
responses.AppendElement(ToResponse(res[i]));
}
Settle<OP_TYPE>(std::move(responses)[nodiscard] (SContext aCx,T&aArgument, :<JS:>)java.lang.StringIndexOutOfBoundsException: Range [67, 68) out of bounds for length 67
namespace
56>
requests.SetCapacity(res.Length());
forRefPtrPromise> * )
java.lang.StringIndexOutOfBoundsException: Range [36, 31) out of bounds for length 59
requests.AppendElementjava.lang.StringIndexOutOfBoundsException: Range [1, 2) out of bounds for length 1
}
Settle< RefPtrCacheStoragePromise&aPromise,
} else java.lang.StringIndexOutOfBoundsException: Range [14, 13) out of bounds for length 23
: java.lang.StringIndexOutOfBoundsException: Range [58, 57) out of bounds for length 71
std:java.lang.StringIndexOutOfBoundsException: Range [38, 37) out of bounds for length 74
>(:()
}if:java.lang.StringIndexOutOfBoundsException: Range [38, 37) out of bounds for length 64
Settle<OP_TYPE>(false /* valid response does not exist */ java.lang.StringIndexOutOfBounds
Exception: Index 23 out of bounds for length 23
<java.lang.StringIndexOutOfBoundsException: Range [41, 40) out of bounds for length 66
/Ifwe java.lang.StringIndexOutOfBoundsException: Range [28, 27) out of bounds for length 76
MOZ_DIAGNOSTIC_ASSERT(voidjava.lang.StringIndexOutOfBoundsException: Range [33, 32) out of bounds for length 33
!)java.lang.StringIndexOutOfBoundsException: Index 17 out of bounds for length 17
ErrorResult;
errRes.ThrowTypeError(
:java.lang.StringIndexOutOfBoundsException: Range [40, 39) out of bounds for length 62
Settle>}/* dummy */, std::move(errRes));
actor->SetWorkerRef( target->Reject(std::move(aRv)
.)java.lang.StringIndexOutOfBoundsException: Range [54, 52) out of bounds for length 70
Settle(java.lang.StringIndexOutOfBoundsException: Range [42, 41) out of bounds for length 60
} else
static_assertjava.lang.StringIndexOutOfBoundsException: Index 75 out of bounds for length 75
"There MOZ_ASSERT(res.isNullOrUndefined
}
}
mozilla::ipcelseif s: java.lang.StringIndexOutOfBoundsException: Range [71, 70) out of bounds for length 74
ErrorResult&& aRv, ns] java.lang.StringIndexOutOfBoundsException: Range [32, 31) out of bounds for length 32
target->Resolvejava.lang.StringIndexOutOfBoundsException: Range [27, 26) out of bounds for length 45
}
ajava.lang.StringIndexOutOfBoundsException: Range [34, 33) out of bounds for length 68
::(java.lang.StringIndexOutOfBoundsException: Range [48, 47) out of bounds for length 49
mPromise.destroy();
return if(
java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
switch (aResult.type()) {
const java.lang.StringIndexOutOfBoundsException: Range [27, 26) out of bounds for length 76
<java.lang.StringIndexOutOfBoundsException: Range [36, 35) out of bounds for length 66
break;
}
:Tjava.lang.StringIndexOutOfBoundsException: Range [46, 44) out of bounds for length 47
const auto& response>java.lang.StringIndexOutOfBoundsException: Range [32, 31) out of bounds for length 47
t()java.lang.StringIndexOutOfBoundsException: Index 69 out of bounds for length 69
template :OP_TYPEtypename java.lang.StringIndexOutOfBoundsException: Range [59, 58) out of bounds for length 59
}
case java.lang.StringIndexOutOfBoundsException: Index 45 out of bounds for length 45
// resolve with undefined
HandleAndSettle<CacheOpResult::java.lang.StringIndexOutOfBoundsException: Range [0, 55) out of bounds for length 30
break targetPromisejava.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 42
}
case CacheOpResult::auto targetPromise = mPromise>(;
SettlePromise(std:java.lang.StringIndexOutOfBoundsException: Range [40, 39) out of bounds for length 74
HandleAndSettle<CacheOpResult java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
java.lang.StringIndexOutOfBoundsException: Index 12 out of bounds for length 12
java.lang.StringIndexOutOfBoundsException: Range [5, 6) out of bounds for length 5
case CacheOpResult:: & =:java.lang.StringIndexOutOfBoundsException: Range [38, 37) out of bounds for length 45
(java.lang.StringIndexOutOfBoundsException: Index 73 out of bounds for length 73
Settle<java.lang.StringIndexOutOfBoundsException: Range [21, 20) out of bounds for length 48
java.lang.StringIndexOutOfBoundsException: Index 12 out of bounds for length 12
}
case CacheOpResult::TStorageMatchResult:RefPtr (java.lang.StringIndexOutOfBoundsException: Range [57, 56) out of bounds for length 58
constauto=.(.(;
HandleAndSettle<CacheOpResultAutoTArray<RefPtr<Response>, 256> responses;
.SetCapacity(res.Length());
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
const AddWorkerRefToStreamChild
:java.lang.StringIndexOutOfBoundsException: Range [55, 54) out of bounds for length 66
java.lang.StringIndexOutOfBoundsException: Range [5, 6) out of bounds for length 5
case CacheOpResult:: AddWorkerRefToStreamChildi])
java.lang.StringIndexOutOfBoundsException: Index 8 out of bounds for length 5
java.lang.StringIndexOutOfBoundsException: Range [22, 21) out of bounds for length 67
java.lang.StringIndexOutOfBoundsException: Range [12, 11) out of bounds for length 12
}
case CacheOpResult::Settle<OP_TYPE>(std::forward();
nse=java.lang.StringIndexOutOfBoundsException: Range [72, 70) out of bounds for length 73
HandleAndSettle<CacheOpResult: if(:<java.lang.StringIndexOutOfBoundsException: Range [48, 47) out of bounds for length 70
break;
}
case java.lang.StringIndexOutOfBoundsException: Index 17 out of bounds for length 0
const auto=))
HandleAndSettle<java.lang.StringIndexOutOfBoundsException: Index 34 out of bounds for length 33
break;
}
default:
MOZ_CRASH("Unknown Cache op result type!");
}
mPromise.destroy();
return IPC_OK();
}
void CacheOpChild::StartDestroy() {
NS_ASSERT_OWNINGTHREAD(CacheOpChild);
// Do not cancel on-going operations when WorkerRef calls this. Instead,
// keep the Worker alive until we are done.
}
nsIGlobalObject* CacheOpChild::GetGlobalObject() const { return mGlobal; }
#ifdef DEBUG
void CacheOpChild::AssertOwningThread() const {
NS_ASSERT_OWNINGTHREAD(CacheOpChild);
}
#endif
} // namespace cache
} // namespace mozilla::dom