1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
/* -*- 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 "mozilla/dom/cache/CacheOpChild.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/Request.h"
#include "mozilla/dom/Response.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;
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,
Promise* aPromise, ActorChild* aParentActor)
: mGlobal(aGlobal),
mParent(aParent),
mPromise(aPromise),
mParentActor(aParentActor) {
MOZ_DIAGNOSTIC_ASSERT(mGlobal);
MOZ_DIAGNOSTIC_ASSERT(mParent);
MOZ_DIAGNOSTIC_ASSERT(mPromise);
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) {
mPromise->MaybeReject(NS_ERROR_FAILURE);
mPromise = nullptr;
}
mParentActor->NoteDeletedActor();
RemoveWorkerRef();
}
mozilla::ipc::IPCResult CacheOpChild::Recv__delete__(
ErrorResult&& aRv, const CacheOpResult& aResult) {
NS_ASSERT_OWNINGTHREAD(CacheOpChild);
if (NS_WARN_IF(aRv.Failed())) {
MOZ_DIAGNOSTIC_ASSERT(aResult.type() == CacheOpResult::Tvoid_t);
mPromise->MaybeReject(std::move(aRv));
mPromise = nullptr;
return IPC_OK();
}
switch (aResult.type()) {
case CacheOpResult::TCacheMatchResult: {
HandleResponse(aResult.get_CacheMatchResult().maybeResponse());
break;
}
case CacheOpResult::TCacheMatchAllResult: {
HandleResponseList(aResult.get_CacheMatchAllResult().responseList());
break;
}
case CacheOpResult::TCachePutAllResult: {
mPromise->MaybeResolveWithUndefined();
break;
}
case CacheOpResult::TCacheDeleteResult: {
mPromise->MaybeResolve(aResult.get_CacheDeleteResult().success());
break;
}
case CacheOpResult::TCacheKeysResult: {
HandleRequestList(aResult.get_CacheKeysResult().requestList());
break;
}
case CacheOpResult::TStorageMatchResult: {
HandleResponse(aResult.get_StorageMatchResult().maybeResponse());
break;
}
case CacheOpResult::TStorageHasResult: {
mPromise->MaybeResolve(aResult.get_StorageHasResult().success());
break;
}
case CacheOpResult::TStorageOpenResult: {
auto result = aResult.get_StorageOpenResult();
auto actor = static_cast<CacheChild*>(result.actor().AsChild());
// If we have a success status then we should have an actor. Gracefully
// reject instead of crashing, though, if we get a nullptr here.
MOZ_DIAGNOSTIC_ASSERT(actor);
if (!actor) {
mPromise->MaybeRejectWithTypeError(
"CacheStorage.open() failed to access the storage system.");
break;
}
actor->SetWorkerRef(CacheWorkerRef::PreferBehavior(
GetWorkerRefPtr().clonePtr(), CacheWorkerRef::eIPCWorkerRef));
RefPtr<Cache> cache = new Cache(mGlobal, actor, result.ns());
mPromise->MaybeResolve(cache);
break;
}
case CacheOpResult::TStorageDeleteResult: {
mPromise->MaybeResolve(aResult.get_StorageDeleteResult().success());
break;
}
case CacheOpResult::TStorageKeysResult: {
mPromise->MaybeResolve(aResult.get_StorageKeysResult().keyList());
break;
}
default:
MOZ_CRASH("Unknown Cache op result type!");
}
mPromise = nullptr;
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
void CacheOpChild::HandleResponse(const Maybe<CacheResponse>& aMaybeResponse) {
if (aMaybeResponse.isNothing()) {
mPromise->MaybeResolveWithUndefined();
return;
}
const CacheResponse& cacheResponse = aMaybeResponse.ref();
AddWorkerRefToStreamChild(cacheResponse, GetWorkerRefPtr());
RefPtr<Response> response = ToResponse(cacheResponse);
mPromise->MaybeResolve(response);
}
void CacheOpChild::HandleResponseList(
const nsTArray<CacheResponse>& aResponseList) {
AutoTArray<RefPtr<Response>, 256> responses;
responses.SetCapacity(aResponseList.Length());
for (uint32_t i = 0; i < aResponseList.Length(); ++i) {
AddWorkerRefToStreamChild(aResponseList[i], GetWorkerRefPtr());
responses.AppendElement(ToResponse(aResponseList[i]));
}
mPromise->MaybeResolve(responses);
}
void CacheOpChild::HandleRequestList(
const nsTArray<CacheRequest>& aRequestList) {
AutoTArray<SafeRefPtr<Request>, 256> requests;
requests.SetCapacity(aRequestList.Length());
for (uint32_t i = 0; i < aRequestList.Length(); ++i) {
AddWorkerRefToStreamChild(aRequestList[i], GetWorkerRefPtr());
requests.AppendElement(ToRequest(aRequestList[i]));
}
mPromise->MaybeResolve(requests);
}
} // namespace cache
} // namespace mozilla::dom
|