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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
/* -*- 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/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;
responses.SetCapacity(res.Length());
for (uint32_t i = 0; i < res.Length(); ++i) {
AddWorkerRefToStreamChild(res[i], GetWorkerRefPtr());
responses.AppendElement(ToResponse(res[i]));
}
Settle<OP_TYPE>(std::move(responses));
} else if constexpr (std::is_same_v<ValueType, nsTArray<CacheRequest>>) {
AutoTArray<SafeRefPtr<Request>, 256> requests;
requests.SetCapacity(res.Length());
for (uint32_t i = 0; i < res.Length(); ++i) {
AddWorkerRefToStreamChild(res[i], GetWorkerRefPtr());
requests.AppendElement(ToRequest(res[i]));
}
Settle<OP_TYPE>(std::move(requests));
} else if constexpr (std::is_same_v<ValueType, bool> ||
std::is_same_v<ValueType, nsTArray<nsString>> ||
std::is_same_v<ValueType, JS::Handle<JS::Value>>) {
Settle<OP_TYPE>(std::forward<TResponse>(aRes));
} else if constexpr (std::is_same_v<ValueType, ErrorResult>) {
Settle<OP_TYPE>(false /* valid response does not exist */, std::move(res));
} else if constexpr (std::is_same_v<ValueType, StorageOpenResult>) {
auto* actor = static_cast<CacheChild*>(res.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) {
ErrorResult errRes;
errRes.ThrowTypeError(
"CacheStorage.open() failed to access the storage system.");
Settle<OP_TYPE>(StorageOpenResultType{} /* dummy */, std::move(errRes));
}
actor->SetWorkerRef(CacheWorkerRef::PreferBehavior(
GetWorkerRefPtr().clonePtr(), CacheWorkerRef::eIPCWorkerRef));
Settle<OP_TYPE>(StorageOpenResultType{actor, res.ns()});
} else {
static_assert(std::is_same_v<ValueType, void>,
"There should be a block handling this type");
}
}
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);
HandleAndSettle<CacheOpResult::Tvoid_t>(aRv);
mPromise.destroy();
return IPC_OK();
}
switch (aResult.type()) {
case CacheOpResult::TCacheMatchResult: {
const auto& response = aResult.get_CacheMatchResult().maybeResponse();
HandleAndSettle<CacheOpResult::TCacheMatchResult>(response);
break;
}
case CacheOpResult::TCacheMatchAllResult: {
const auto& response = aResult.get_CacheMatchAllResult().responseList();
HandleAndSettle<CacheOpResult::TCacheMatchAllResult>(response);
break;
}
case CacheOpResult::TCachePutAllResult: {
// resolve with undefined
HandleAndSettle<CacheOpResult::TCachePutAllResult>(
JS::UndefinedHandleValue);
break;
}
case CacheOpResult::TCacheDeleteResult: {
const auto& response = aResult.get_CacheDeleteResult().success();
HandleAndSettle<CacheOpResult::TCacheDeleteResult>(response);
break;
}
case CacheOpResult::TCacheKeysResult: {
const auto& response = aResult.get_CacheKeysResult().requestList();
HandleAndSettle<CacheOpResult::TCacheKeysResult>(response);
break;
}
case CacheOpResult::TStorageMatchResult: {
const auto& response = aResult.get_StorageMatchResult().maybeResponse();
HandleAndSettle<CacheOpResult::TStorageMatchResult>(response);
break;
}
case CacheOpResult::TStorageHasResult: {
const auto& response = aResult.get_StorageHasResult().success();
HandleAndSettle<CacheOpResult::TStorageHasResult>(response);
break;
}
case CacheOpResult::TStorageOpenResult: {
const auto& response = aResult.get_StorageOpenResult();
HandleAndSettle<CacheOpResult::TStorageOpenResult>(response);
break;
}
case CacheOpResult::TStorageDeleteResult: {
const auto& response = aResult.get_StorageDeleteResult().success();
HandleAndSettle<CacheOpResult::TStorageDeleteResult>(response);
break;
}
case CacheOpResult::TStorageKeysResult: {
const auto& response = aResult.get_StorageKeysResult().keyList();
HandleAndSettle<CacheOpResult::TStorageKeysResult>(response);
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
|