1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/cache/CacheStorageParent.h"
9 #include "mozilla/Unused.h"
10 #include "mozilla/dom/cache/ActorUtils.h"
11 #include "mozilla/dom/cache/CacheOpParent.h"
12 #include "mozilla/dom/cache/ManagerId.h"
13 #include "mozilla/dom/quota/QuotaManager.h"
14 #include "mozilla/ipc/PBackgroundParent.h"
16 namespace mozilla::dom::cache
{
18 using mozilla::dom::quota::QuotaManager
;
19 using mozilla::ipc::PBackgroundParent
;
20 using mozilla::ipc::PrincipalInfo
;
22 // declared in ActorUtils.h
23 already_AddRefed
<PCacheStorageParent
> AllocPCacheStorageParent(
24 PBackgroundParent
* aManagingActor
, Namespace aNamespace
,
25 const mozilla::ipc::PrincipalInfo
& aPrincipalInfo
) {
26 if (NS_WARN_IF(!QuotaManager::IsPrincipalInfoValid(aPrincipalInfo
))) {
31 return MakeAndAddRef
<CacheStorageParent
>(aManagingActor
, aNamespace
,
35 // declared in ActorUtils.h
36 void DeallocPCacheStorageParent(PCacheStorageParent
* aActor
) { delete aActor
; }
38 CacheStorageParent::CacheStorageParent(PBackgroundParent
* aManagingActor
,
40 const PrincipalInfo
& aPrincipalInfo
)
41 : mNamespace(aNamespace
), mVerifiedStatus(NS_OK
) {
42 MOZ_COUNT_CTOR(cache::CacheStorageParent
);
43 MOZ_DIAGNOSTIC_ASSERT(aManagingActor
);
45 // Start the async principal verification process immediately.
46 mVerifier
= PrincipalVerifier::CreateAndDispatch(*this, aManagingActor
,
48 MOZ_DIAGNOSTIC_ASSERT(mVerifier
);
51 CacheStorageParent::~CacheStorageParent() {
52 MOZ_COUNT_DTOR(cache::CacheStorageParent
);
53 MOZ_DIAGNOSTIC_ASSERT(!mVerifier
);
56 void CacheStorageParent::ActorDestroy(ActorDestroyReason aReason
) {
58 mVerifier
->RemoveListener(*this);
63 PCacheOpParent
* CacheStorageParent::AllocPCacheOpParent(
64 const CacheOpArgs
& aOpArgs
) {
65 if (aOpArgs
.type() != CacheOpArgs::TStorageMatchArgs
&&
66 aOpArgs
.type() != CacheOpArgs::TStorageHasArgs
&&
67 aOpArgs
.type() != CacheOpArgs::TStorageOpenArgs
&&
68 aOpArgs
.type() != CacheOpArgs::TStorageDeleteArgs
&&
69 aOpArgs
.type() != CacheOpArgs::TStorageKeysArgs
) {
70 MOZ_CRASH("Invalid operation sent to CacheStorage actor!");
73 return new CacheOpParent(Manager(), mNamespace
, aOpArgs
);
76 bool CacheStorageParent::DeallocPCacheOpParent(PCacheOpParent
* aActor
) {
81 mozilla::ipc::IPCResult
CacheStorageParent::RecvPCacheOpConstructor(
82 PCacheOpParent
* aActor
, const CacheOpArgs
& aOpArgs
) {
83 auto actor
= static_cast<CacheOpParent
*>(aActor
);
86 MOZ_DIAGNOSTIC_ASSERT(!mManagerId
);
87 actor
->WaitForVerification(mVerifier
);
91 if (NS_WARN_IF(NS_FAILED(mVerifiedStatus
))) {
92 ErrorResult
result(mVerifiedStatus
);
95 CacheOpParent::Send__delete__(actor
, std::move(result
), void_t())));
99 MOZ_DIAGNOSTIC_ASSERT(mManagerId
);
100 actor
->Execute(mManagerId
);
104 mozilla::ipc::IPCResult
CacheStorageParent::RecvTeardown() {
105 // If child process is gone, warn and allow actor to clean up normally
106 QM_WARNONLY_TRY(OkIf(Send__delete__(this)));
110 void CacheStorageParent::OnPrincipalVerified(
111 nsresult aRv
, const SafeRefPtr
<ManagerId
>& aManagerId
) {
112 MOZ_DIAGNOSTIC_ASSERT(mVerifier
);
113 MOZ_DIAGNOSTIC_ASSERT(!mManagerId
);
114 MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(mVerifiedStatus
));
116 if (NS_WARN_IF(NS_FAILED(aRv
))) {
117 mVerifiedStatus
= aRv
;
120 mManagerId
= aManagerId
.clonePtr();
121 mVerifier
->RemoveListener(*this);
125 } // namespace mozilla::dom::cache