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"
|
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 {
|
|
|
|
|
|
|
|
void
|
2016-06-23 11:53:14 +03:00
|
|
|
AddWorkerHolderToStreamChild(const CacheReadStream& aReadStream,
|
|
|
|
CacheWorkerHolder* aWorkerHolder)
|
2015-04-16 22:00:16 +03:00
|
|
|
{
|
2016-06-23 11:53:14 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-06-23 11:53:14 +03:00
|
|
|
AddWorkerHolderToStreamChild(const CacheResponse& aResponse,
|
|
|
|
CacheWorkerHolder* aWorkerHolder)
|
2015-04-16 22:00:16 +03:00
|
|
|
{
|
2016-06-23 11:53:14 +03:00
|
|
|
MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerHolder);
|
2015-04-16 22:00:16 +03:00
|
|
|
|
|
|
|
if (aResponse.body().type() == CacheReadStreamOrVoid::Tvoid_t) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
AddWorkerHolderToStreamChild(aResponse.body().get_CacheReadStream(),
|
|
|
|
aWorkerHolder);
|
2015-04-16 22:00:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-06-23 11:53:14 +03:00
|
|
|
AddWorkerHolderToStreamChild(const CacheRequest& aRequest,
|
|
|
|
CacheWorkerHolder* aWorkerHolder)
|
2015-04-16 22:00:16 +03:00
|
|
|
{
|
2016-06-23 11:53:14 +03:00
|
|
|
MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerHolder);
|
2015-04-16 22:00:16 +03:00
|
|
|
|
|
|
|
if (aRequest.body().type() == CacheReadStreamOrVoid::Tvoid_t) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
AddWorkerHolderToStreamChild(aRequest.body().get_CacheReadStream(),
|
|
|
|
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,
|
2015-05-01 18:13:36 +03:00
|
|
|
nsISupports* aParent, Promise* aPromise)
|
2015-04-16 22:00:15 +03:00
|
|
|
: mGlobal(aGlobal)
|
2015-05-01 18:13:36 +03:00
|
|
|
, mParent(aParent)
|
2015-04-16 22:00:15 +03:00
|
|
|
, mPromise(aPromise)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mGlobal);
|
2015-05-01 18:13:36 +03:00
|
|
|
MOZ_ASSERT(mParent);
|
2015-04-16 22:00:15 +03:00
|
|
|
MOZ_ASSERT(mPromise);
|
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
MOZ_ASSERT_IF(!NS_IsMainThread(), aWorkerHolder);
|
|
|
|
SetWorkerHolder(aWorkerHolder);
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CacheOpChild::~CacheOpChild()
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheOpChild);
|
|
|
|
MOZ_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;
|
|
|
|
}
|
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
RemoveWorkerHolder();
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
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())) {
|
2015-04-16 22:00:16 +03:00
|
|
|
MOZ_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;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (aResult.type()) {
|
|
|
|
case CacheOpResult::TCacheMatchResult:
|
|
|
|
{
|
|
|
|
HandleResponse(aResult.get_CacheMatchResult().responseOrVoid());
|
|
|
|
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:
|
|
|
|
{
|
|
|
|
HandleResponse(aResult.get_StorageMatchResult().responseOrVoid());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpResult::TStorageHasResult:
|
|
|
|
{
|
|
|
|
mPromise->MaybeResolve(aResult.get_StorageHasResult().success());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpResult::TStorageOpenResult:
|
|
|
|
{
|
|
|
|
auto actor = static_cast<CacheChild*>(
|
|
|
|
aResult.get_StorageOpenResult().actorChild());
|
2016-06-23 11:53:14 +03:00
|
|
|
actor->SetWorkerHolder(GetWorkerHolder());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Cache> cache = new Cache(mGlobal, actor);
|
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;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2015-04-16 22:00:15 +03:00
|
|
|
{
|
2016-05-15 20:32:09 +03:00
|
|
|
MOZ_CRASH("CacheOpChild does not implement TypeUtils::GetIPCManager()");
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-16 22:00:15 +03:00
|
|
|
CacheOpChild::HandleResponse(const CacheResponseOrVoid& aResponseOrVoid)
|
2015-04-16 22:00:15 +03:00
|
|
|
{
|
2015-04-16 22:00:16 +03:00
|
|
|
if (aResponseOrVoid.type() == CacheResponseOrVoid::Tvoid_t) {
|
2016-08-09 12:15:13 +03:00
|
|
|
mPromise->MaybeResolveWithUndefined();
|
2015-04-16 22:00:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:16 +03:00
|
|
|
const CacheResponse& cacheResponse = aResponseOrVoid.get_CacheResponse();
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-16 22:00:15 +03:00
|
|
|
CacheOpChild::HandleResponseList(const nsTArray<CacheResponse>& aResponseList)
|
2015-04-16 22:00:15 +03:00
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-16 22:00:15 +03:00
|
|
|
CacheOpChild::HandleRequestList(const nsTArray<CacheRequest>& aRequestList)
|
2015-04-16 22:00:15 +03:00
|
|
|
{
|
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
|