Backed out changeset b462e7b742d8 (bug 1908261) for causing multiple reftest failures...
[gecko.git] / dom / cache / CacheStorageParent.cpp
blob6853949eb8b349b165a20789be33e413300a7cfd
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/ErrorResult.h"
10 #include "mozilla/Unused.h"
11 #include "mozilla/dom/cache/ActorUtils.h"
12 #include "mozilla/dom/cache/CacheOpParent.h"
13 #include "mozilla/dom/cache/ManagerId.h"
14 #include "mozilla/dom/quota/PrincipalUtils.h"
15 #include "mozilla/ipc/PBackgroundParent.h"
17 namespace mozilla::dom::cache {
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(!quota::IsPrincipalInfoValid(aPrincipalInfo))) {
27 MOZ_ASSERT(false);
28 return nullptr;
31 return MakeAndAddRef<CacheStorageParent>(aManagingActor, aNamespace,
32 aPrincipalInfo);
35 // declared in ActorUtils.h
36 void DeallocPCacheStorageParent(PCacheStorageParent* aActor) { delete aActor; }
38 CacheStorageParent::CacheStorageParent(PBackgroundParent* aManagingActor,
39 Namespace aNamespace,
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,
47 aPrincipalInfo);
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) {
57 if (mVerifier) {
58 mVerifier->RemoveListener(*this);
59 mVerifier = nullptr;
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) {
77 delete aActor;
78 return true;
81 mozilla::ipc::IPCResult CacheStorageParent::RecvPCacheOpConstructor(
82 PCacheOpParent* aActor, const CacheOpArgs& aOpArgs) {
83 auto actor = static_cast<CacheOpParent*>(aActor);
85 if (mVerifier) {
86 MOZ_DIAGNOSTIC_ASSERT(!mManagerId);
87 actor->WaitForVerification(mVerifier);
88 return IPC_OK();
91 if (NS_WARN_IF(NS_FAILED(mVerifiedStatus))) {
92 QM_WARNONLY_TRY(OkIf(CacheOpParent::Send__delete__(
93 actor, CopyableErrorResult(mVerifiedStatus), void_t())));
94 return IPC_OK();
97 MOZ_DIAGNOSTIC_ASSERT(mManagerId);
98 actor->Execute(mManagerId);
99 return IPC_OK();
102 mozilla::ipc::IPCResult CacheStorageParent::RecvTeardown() {
103 // If child process is gone, warn and allow actor to clean up normally
104 QM_WARNONLY_TRY(OkIf(Send__delete__(this)));
105 return IPC_OK();
108 void CacheStorageParent::OnPrincipalVerified(
109 nsresult aRv, const SafeRefPtr<ManagerId>& aManagerId) {
110 MOZ_DIAGNOSTIC_ASSERT(mVerifier);
111 MOZ_DIAGNOSTIC_ASSERT(!mManagerId);
112 MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(mVerifiedStatus));
114 if (NS_WARN_IF(NS_FAILED(aRv))) {
115 mVerifiedStatus = aRv;
118 mManagerId = aManagerId.clonePtr();
119 mVerifier->RemoveListener(*this);
120 mVerifier = nullptr;
123 } // namespace mozilla::dom::cache