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"
|
2017-10-26 14:35:08 +03:00
|
|
|
#include "mozilla/dom/cache/CacheWorkerHolder.h"
|
2015-04-16 22:00:15 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace cache {
|
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
using mozilla::ipc::PBackgroundChild;
|
|
|
|
|
2015-04-16 22:00:16 +03:00
|
|
|
namespace {
|
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
void AddWorkerHolderToStreamChild(const CacheReadStream& aReadStream,
|
|
|
|
CacheWorkerHolder* aWorkerHolder) {
|
|
|
|
MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerHolder);
|
2015-04-16 22:00:16 +03:00
|
|
|
CacheStreamControlChild* cacheControl =
|
|
|
|
static_cast<CacheStreamControlChild*>(aReadStream.controlChild());
|
|
|
|
if (cacheControl) {
|
2016-06-23 11:53:14 +03:00
|
|
|
cacheControl->SetWorkerHolder(aWorkerHolder);
|
2015-04-16 22:00:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
void AddWorkerHolderToStreamChild(const CacheResponse& aResponse,
|
|
|
|
CacheWorkerHolder* aWorkerHolder) {
|
|
|
|
MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerHolder);
|
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-03-21 14:09:44 +03:00
|
|
|
AddWorkerHolderToStreamChild(aResponse.body().ref(), aWorkerHolder);
|
2015-04-16 22:00:16 +03:00
|
|
|
}
|
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
void AddWorkerHolderToStreamChild(const CacheRequest& aRequest,
|
|
|
|
CacheWorkerHolder* aWorkerHolder) {
|
|
|
|
MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerHolder);
|
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-03-21 14:09:44 +03:00
|
|
|
AddWorkerHolderToStreamChild(aRequest.body().ref(), aWorkerHolder);
|
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
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
CacheOpChild::CacheOpChild(CacheWorkerHolder* aWorkerHolder,
|
|
|
|
nsIGlobalObject* aGlobal, nsISupports* aParent,
|
2015-05-01 18:13:36 +03:00
|
|
|
Promise* aPromise)
|
|
|
|
: 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
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerHolder);
|
2017-05-10 19:27:10 +03:00
|
|
|
|
|
|
|
RefPtr<CacheWorkerHolder> workerHolder = CacheWorkerHolder::PreferBehavior(
|
|
|
|
aWorkerHolder, CacheWorkerHolder::PreventIdleShutdownStart);
|
|
|
|
|
|
|
|
SetWorkerHolder(workerHolder);
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
RemoveWorkerHolder();
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::ipc::IPCResult CacheOpChild::Recv__delete__(
|
|
|
|
const ErrorResult& aRv, const CacheOpResult& aResult) {
|
|
|
|
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);
|
2015-04-16 22:00:15 +03:00
|
|
|
// TODO: Remove this const_cast (bug 1152078).
|
|
|
|
// It is safe for now since this ErrorResult is handed off to us by IPDL
|
|
|
|
// and is thrown into the trash afterwards.
|
|
|
|
mPromise->MaybeReject(const_cast<ErrorResult&>(aRv));
|
|
|
|
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) {
|
|
|
|
ErrorResult status;
|
|
|
|
status.ThrowTypeError<MSG_CACHE_OPEN_FAILED>();
|
|
|
|
mPromise->MaybeReject(status);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-05-10 19:27:10 +03:00
|
|
|
RefPtr<CacheWorkerHolder> workerHolder =
|
|
|
|
CacheWorkerHolder::PreferBehavior(
|
|
|
|
GetWorkerHolder(), CacheWorkerHolder::AllowIdleShutdownStart);
|
|
|
|
|
|
|
|
actor->SetWorkerHolder(workerHolder);
|
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);
|
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
// Do not cancel on-going operations when WorkerHolder calls this. Instead,
|
|
|
|
// 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
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
AddWorkerHolderToStreamChild(cacheResponse, GetWorkerHolder());
|
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) {
|
2016-06-23 11:53:14 +03:00
|
|
|
AddWorkerHolderToStreamChild(aResponseList[i], GetWorkerHolder());
|
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) {
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<RefPtr<Request>, 256> requests;
|
2015-04-16 22:00:15 +03:00
|
|
|
requests.SetCapacity(aRequestList.Length());
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aRequestList.Length(); ++i) {
|
2016-06-23 11:53:14 +03:00
|
|
|
AddWorkerHolderToStreamChild(aRequestList[i], GetWorkerHolder());
|
2015-04-16 22:00:15 +03:00
|
|
|
requests.AppendElement(ToRequest(aRequestList[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
mPromise->MaybeResolve(requests);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cache
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|