Backed out changeset 1d9301697aa0 (bug 1887752) for causing failures on browser_all_f...
[gecko.git] / dom / cache / CacheStorageParent.cpp
blob13eb0eb1809cb361fd419f49e2a1a8c0de2664ad
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/QuotaManager.h"
15 #include "mozilla/ipc/PBackgroundParent.h"
17 namespace mozilla::dom::cache {
19 using mozilla::dom::quota::QuotaManager;
20 using mozilla::ipc::PBackgroundParent;
21 using mozilla::ipc::PrincipalInfo;
23 // declared in ActorUtils.h
24 already_AddRefed<PCacheStorageParent> AllocPCacheStorageParent(
25 PBackgroundParent* aManagingActor, Namespace aNamespace,
26 const mozilla::ipc::PrincipalInfo& aPrincipalInfo) {
27 if (NS_WARN_IF(!QuotaManager::IsPrincipalInfoValid(aPrincipalInfo))) {
28 MOZ_ASSERT(false);
29 return nullptr;
32 return MakeAndAddRef<CacheStorageParent>(aManagingActor, aNamespace,
33 aPrincipalInfo);
36 // declared in ActorUtils.h
37 void DeallocPCacheStorageParent(PCacheStorageParent* aActor) { delete aActor; }
39 CacheStorageParent::CacheStorageParent(PBackgroundParent* aManagingActor,
40 Namespace aNamespace,
41 const PrincipalInfo& aPrincipalInfo)
42 : mNamespace(aNamespace), mVerifiedStatus(NS_OK) {
43 MOZ_COUNT_CTOR(cache::CacheStorageParent);
44 MOZ_DIAGNOSTIC_ASSERT(aManagingActor);
46 // Start the async principal verification process immediately.
47 mVerifier = PrincipalVerifier::CreateAndDispatch(*this, aManagingActor,
48 aPrincipalInfo);
49 MOZ_DIAGNOSTIC_ASSERT(mVerifier);
52 CacheStorageParent::~CacheStorageParent() {
53 MOZ_COUNT_DTOR(cache::CacheStorageParent);
54 MOZ_DIAGNOSTIC_ASSERT(!mVerifier);
57 void CacheStorageParent::ActorDestroy(ActorDestroyReason aReason) {
58 if (mVerifier) {
59 mVerifier->RemoveListener(*this);
60 mVerifier = nullptr;
64 PCacheOpParent* CacheStorageParent::AllocPCacheOpParent(
65 const CacheOpArgs& aOpArgs) {
66 if (aOpArgs.type() != CacheOpArgs::TStorageMatchArgs &&
67 aOpArgs.type() != CacheOpArgs::TStorageHasArgs &&
68 aOpArgs.type() != CacheOpArgs::TStorageOpenArgs &&
69 aOpArgs.type() != CacheOpArgs::TStorageDeleteArgs &&
70 aOpArgs.type() != CacheOpArgs::TStorageKeysArgs) {
71 MOZ_CRASH("Invalid operation sent to CacheStorage actor!");
74 return new CacheOpParent(Manager(), mNamespace, aOpArgs);
77 bool CacheStorageParent::DeallocPCacheOpParent(PCacheOpParent* aActor) {
78 delete aActor;
79 return true;
82 mozilla::ipc::IPCResult CacheStorageParent::RecvPCacheOpConstructor(
83 PCacheOpParent* aActor, const CacheOpArgs& aOpArgs) {
84 auto actor = static_cast<CacheOpParent*>(aActor);
86 if (mVerifier) {
87 MOZ_DIAGNOSTIC_ASSERT(!mManagerId);
88 actor->WaitForVerification(mVerifier);
89 return IPC_OK();
92 if (NS_WARN_IF(NS_FAILED(mVerifiedStatus))) {
93 QM_WARNONLY_TRY(OkIf(CacheOpParent::Send__delete__(
94 actor, CopyableErrorResult(mVerifiedStatus), void_t())));
95 return IPC_OK();
98 MOZ_DIAGNOSTIC_ASSERT(mManagerId);
99 actor->Execute(mManagerId);
100 return IPC_OK();
103 mozilla::ipc::IPCResult CacheStorageParent::RecvTeardown() {
104 // If child process is gone, warn and allow actor to clean up normally
105 QM_WARNONLY_TRY(OkIf(Send__delete__(this)));
106 return IPC_OK();
109 void CacheStorageParent::OnPrincipalVerified(
110 nsresult aRv, const SafeRefPtr<ManagerId>& aManagerId) {
111 MOZ_DIAGNOSTIC_ASSERT(mVerifier);
112 MOZ_DIAGNOSTIC_ASSERT(!mManagerId);
113 MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(mVerifiedStatus));
115 if (NS_WARN_IF(NS_FAILED(aRv))) {
116 mVerifiedStatus = aRv;
119 mManagerId = aManagerId.clonePtr();
120 mVerifier->RemoveListener(*this);
121 mVerifier = nullptr;
124 } // namespace mozilla::dom::cache