2015-04-16 22:00:15 +03:00
|
|
|
/* -*- 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"
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
#include "mozilla/dom/Request.h"
|
|
|
|
#include "mozilla/dom/Response.h"
|
2015-04-16 22:00:15 +03:00
|
|
|
#include "mozilla/dom/cache/Cache.h"
|
|
|
|
#include "mozilla/dom/cache/CacheChild.h"
|
2015-04-16 22:00:16 +03:00
|
|
|
#include "mozilla/dom/cache/CacheStreamControlChild.h"
|
2019-07-12 14:17:59 +03:00
|
|
|
#include "mozilla/dom/cache/CacheWorkerRef.h"
|
2015-04-16 22:00:15 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2020-05-11 15:13:48 +03:00
|
|
|
// XXX Move this to ToJSValue.h
|
|
|
|
template <typename T>
|
|
|
|
MOZ_MUST_USE bool ToJSValue(JSContext* aCx, const SafeRefPtr<T>& aArgument,
|
|
|
|
JS::MutableHandle<JS::Value> aValue) {
|
|
|
|
return ToJSValue(aCx, *aArgument.unsafeGetRawPtr(), aValue);
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
namespace cache {
|
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
using mozilla::ipc::PBackgroundChild;
|
|
|
|
|
2015-04-16 22:00:16 +03:00
|
|
|
namespace {
|
|
|
|
|
2019-07-12 14:17:59 +03:00
|
|
|
void AddWorkerRefToStreamChild(const CacheReadStream& aReadStream,
|
2020-05-11 15:12:22 +03:00
|
|
|
const SafeRefPtr<CacheWorkerRef>& aWorkerRef) {
|
2019-07-12 14:17:59 +03:00
|
|
|
MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerRef);
|
2015-04-16 22:00:16 +03:00
|
|
|
CacheStreamControlChild* cacheControl =
|
|
|
|
static_cast<CacheStreamControlChild*>(aReadStream.controlChild());
|
|
|
|
if (cacheControl) {
|
2020-05-11 15:12:22 +03:00
|
|
|
cacheControl->SetWorkerRef(aWorkerRef.clonePtr());
|
2015-04-16 22:00:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 14:17:59 +03:00
|
|
|
void AddWorkerRefToStreamChild(const CacheResponse& aResponse,
|
2020-05-11 15:12:22 +03:00
|
|
|
const SafeRefPtr<CacheWorkerRef>& aWorkerRef) {
|
2019-07-12 14:17:59 +03:00
|
|
|
MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerRef);
|
2015-04-16 22:00:16 +03:00
|
|
|
|
2019-03-21 14:09:44 +03:00
|
|
|
if (aResponse.body().isNothing()) {
|
2015-04-16 22:00:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-12 14:17:59 +03:00
|
|
|
AddWorkerRefToStreamChild(aResponse.body().ref(), aWorkerRef);
|
2015-04-16 22:00:16 +03:00
|
|
|
}
|
|
|
|
|
2019-07-12 14:17:59 +03:00
|
|
|
void AddWorkerRefToStreamChild(const CacheRequest& aRequest,
|
2020-05-11 15:12:22 +03:00
|
|
|
const SafeRefPtr<CacheWorkerRef>& aWorkerRef) {
|
2019-07-12 14:17:59 +03:00
|
|
|
MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerRef);
|
2015-04-16 22:00:16 +03:00
|
|
|
|
2019-03-21 14:09:44 +03:00
|
|
|
if (aRequest.body().isNothing()) {
|
2015-04-16 22:00:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-12 14:17:59 +03:00
|
|
|
AddWorkerRefToStreamChild(aRequest.body().ref(), aWorkerRef);
|
2015-04-16 22:00:16 +03:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-04-16 22:00:16 +03:00
|
|
|
|
2020-05-11 15:12:22 +03:00
|
|
|
CacheOpChild::CacheOpChild(SafeRefPtr<CacheWorkerRef> aWorkerRef,
|
|
|
|
nsIGlobalObject* aGlobal, nsISupports* aParent,
|
|
|
|
Promise* aPromise)
|
2015-05-01 18:13:36 +03:00
|
|
|
: mGlobal(aGlobal), mParent(aParent), mPromise(aPromise) {
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mGlobal);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mParent);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mPromise);
|
2015-04-16 22:00:15 +03:00
|
|
|
|
2019-07-12 14:17:59 +03:00
|
|
|
MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerRef);
|
2017-05-10 19:27:10 +03:00
|
|
|
|
2020-05-11 15:12:22 +03:00
|
|
|
SetWorkerRef(CacheWorkerRef::PreferBehavior(
|
|
|
|
std::move(aWorkerRef), CacheWorkerRef::eStrongWorkerRef));
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CacheOpChild::~CacheOpChild() {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheOpChild);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mPromise);
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-12 14:17:59 +03:00
|
|
|
RemoveWorkerRef();
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::ipc::IPCResult CacheOpChild::Recv__delete__(
|
2020-02-05 19:15:07 +03:00
|
|
|
ErrorResult&& aRv, const CacheOpResult& aResult) {
|
2015-04-16 22:00:15 +03:00
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheOpChild);
|
|
|
|
|
2015-05-01 18:15:52 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aResult.type() == CacheOpResult::Tvoid_t);
|
2020-02-14 18:42:30 +03:00
|
|
|
mPromise->MaybeReject(std::move(aRv));
|
2015-04-16 22:00:15 +03:00
|
|
|
mPromise = nullptr;
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (aResult.type()) {
|
|
|
|
case CacheOpResult::TCacheMatchResult: {
|
2019-03-21 14:09:44 +03:00
|
|
|
HandleResponse(aResult.get_CacheMatchResult().maybeResponse());
|
2015-04-16 22:00:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpResult::TCacheMatchAllResult: {
|
|
|
|
HandleResponseList(aResult.get_CacheMatchAllResult().responseList());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpResult::TCachePutAllResult: {
|
2016-08-09 12:15:13 +03:00
|
|
|
mPromise->MaybeResolveWithUndefined();
|
2015-04-16 22:00:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpResult::TCacheDeleteResult: {
|
|
|
|
mPromise->MaybeResolve(aResult.get_CacheDeleteResult().success());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpResult::TCacheKeysResult: {
|
|
|
|
HandleRequestList(aResult.get_CacheKeysResult().requestList());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpResult::TStorageMatchResult: {
|
2019-03-21 14:09:44 +03:00
|
|
|
HandleResponse(aResult.get_StorageMatchResult().maybeResponse());
|
2015-04-16 22:00:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpResult::TStorageHasResult: {
|
|
|
|
mPromise->MaybeResolve(aResult.get_StorageHasResult().success());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpResult::TStorageOpenResult: {
|
2017-09-15 22:25:41 +03:00
|
|
|
auto result = aResult.get_StorageOpenResult();
|
|
|
|
auto actor = static_cast<CacheChild*>(result.actorChild());
|
2017-01-10 19:20:15 +03:00
|
|
|
|
|
|
|
// 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) {
|
2019-09-19 16:02:39 +03:00
|
|
|
mPromise->MaybeRejectWithTypeError(
|
2020-03-07 00:05:48 +03:00
|
|
|
"CacheStorage.open() failed to access the storage system.");
|
2017-01-10 19:20:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-05-11 15:12:22 +03:00
|
|
|
actor->SetWorkerRef(CacheWorkerRef::PreferBehavior(
|
|
|
|
GetWorkerRefPtr().clonePtr(), CacheWorkerRef::eIPCWorkerRef));
|
2017-09-15 22:25:41 +03:00
|
|
|
RefPtr<Cache> cache = new Cache(mGlobal, actor, result.ns());
|
2015-04-16 22:00:15 +03:00
|
|
|
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;
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CacheOpChild::StartDestroy() {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheOpChild);
|
|
|
|
|
2019-07-12 14:17:59 +03:00
|
|
|
// Do not cancel on-going operations when WorkerRef calls this. Instead,
|
2016-06-23 11:53:14 +03:00
|
|
|
// keep the Worker alive until we are done.
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIGlobalObject* CacheOpChild::GetGlobalObject() const { return mGlobal; }
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
void CacheOpChild::AssertOwningThread() const {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheOpChild);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
PBackgroundChild* CacheOpChild::GetIPCManager() {
|
|
|
|
MOZ_CRASH("CacheOpChild does not implement TypeUtils::GetIPCManager()");
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
2019-03-21 14:09:44 +03:00
|
|
|
void CacheOpChild::HandleResponse(const Maybe<CacheResponse>& aMaybeResponse) {
|
|
|
|
if (aMaybeResponse.isNothing()) {
|
2016-08-09 12:15:13 +03:00
|
|
|
mPromise->MaybeResolveWithUndefined();
|
2015-04-16 22:00:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-21 14:09:44 +03:00
|
|
|
const CacheResponse& cacheResponse = aMaybeResponse.ref();
|
2015-04-16 22:00:16 +03:00
|
|
|
|
2020-05-11 15:12:22 +03:00
|
|
|
AddWorkerRefToStreamChild(cacheResponse, GetWorkerRefPtr());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Response> response = ToResponse(cacheResponse);
|
2015-04-16 22:00:16 +03:00
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
mPromise->MaybeResolve(response);
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
void CacheOpChild::HandleResponseList(
|
|
|
|
const nsTArray<CacheResponse>& aResponseList) {
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<RefPtr<Response>, 256> responses;
|
2015-04-16 22:00:15 +03:00
|
|
|
responses.SetCapacity(aResponseList.Length());
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aResponseList.Length(); ++i) {
|
2020-05-11 15:12:22 +03:00
|
|
|
AddWorkerRefToStreamChild(aResponseList[i], GetWorkerRefPtr());
|
2015-04-16 22:00:15 +03:00
|
|
|
responses.AppendElement(ToResponse(aResponseList[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
mPromise->MaybeResolve(responses);
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
void CacheOpChild::HandleRequestList(
|
|
|
|
const nsTArray<CacheRequest>& aRequestList) {
|
2020-05-11 15:13:48 +03:00
|
|
|
AutoTArray<SafeRefPtr<Request>, 256> requests;
|
2015-04-16 22:00:15 +03:00
|
|
|
requests.SetCapacity(aRequestList.Length());
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aRequestList.Length(); ++i) {
|
2020-05-11 15:12:22 +03:00
|
|
|
AddWorkerRefToStreamChild(aRequestList[i], GetWorkerRefPtr());
|
2015-04-16 22:00:15 +03:00
|
|
|
requests.AppendElement(ToRequest(aRequestList[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
mPromise->MaybeResolve(requests);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cache
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|