CacheStorageParent.cpp (4407B)
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/. */ 6 7 #include "mozilla/dom/cache/CacheStorageParent.h" 8 9 #include "mozilla/ErrorResult.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/PrincipalUtils.h" 14 #include "mozilla/ipc/PBackgroundParent.h" 15 16 namespace mozilla::dom::cache { 17 18 using mozilla::ipc::PBackgroundParent; 19 using mozilla::ipc::PrincipalInfo; 20 21 // declared in ActorUtils.h 22 already_AddRefed<PCacheStorageParent> AllocPCacheStorageParent( 23 mozilla::ipc::PBackgroundParent* aBackgroundIPCActor, 24 PBoundStorageKeyParent* aBoundStorageKeyActor, Namespace aNamespace, 25 const mozilla::ipc::PrincipalInfo& aPrincipalInfo) { 26 if (NS_WARN_IF(!quota::IsPrincipalInfoValid(aPrincipalInfo))) { 27 MOZ_ASSERT(false); 28 return nullptr; 29 } 30 31 return MakeAndAddRef<CacheStorageParent>( 32 aBackgroundIPCActor, aBoundStorageKeyActor, aNamespace, aPrincipalInfo); 33 } 34 35 // declared in ActorUtils.h 36 void DeallocPCacheStorageParent(PCacheStorageParent* aActor) { delete aActor; } 37 38 CacheStorageParent::CacheStorageParent( 39 mozilla::ipc::PBackgroundParent* aIPCActor, 40 PBoundStorageKeyParent* aBoundStorageKeyActor, Namespace aNamespace, 41 const PrincipalInfo& aPrincipalInfo) 42 : mBackgroundIPCActor(aIPCActor), 43 mBoundStorageKeyActor(aBoundStorageKeyActor), 44 mNamespace(aNamespace), 45 mVerifiedStatus(NS_OK) { 46 MOZ_COUNT_CTOR(cache::CacheStorageParent); 47 MOZ_DIAGNOSTIC_ASSERT(mBackgroundIPCActor); 48 49 // Start the async principal verification process immediately. 50 mVerifier = PrincipalVerifier::CreateAndDispatch(*this, mBackgroundIPCActor, 51 aPrincipalInfo); 52 MOZ_DIAGNOSTIC_ASSERT(mVerifier); 53 } 54 55 CacheStorageParent::~CacheStorageParent() { 56 MOZ_COUNT_DTOR(cache::CacheStorageParent); 57 MOZ_DIAGNOSTIC_ASSERT(!mVerifier); 58 } 59 60 void CacheStorageParent::ActorDestroy(ActorDestroyReason aReason) { 61 if (mVerifier) { 62 mVerifier->RemoveListener(*this); 63 mVerifier = nullptr; 64 } 65 } 66 67 PCacheOpParent* CacheStorageParent::AllocPCacheOpParent( 68 const CacheOpArgs& aOpArgs) { 69 if (aOpArgs.type() != CacheOpArgs::TStorageMatchArgs && 70 aOpArgs.type() != CacheOpArgs::TStorageHasArgs && 71 aOpArgs.type() != CacheOpArgs::TStorageOpenArgs && 72 aOpArgs.type() != CacheOpArgs::TStorageDeleteArgs && 73 aOpArgs.type() != CacheOpArgs::TStorageKeysArgs) { 74 MOZ_CRASH("Invalid operation sent to CacheStorage actor!"); 75 } 76 77 return new CacheOpParent(mBoundStorageKeyActor 78 ? WeakRefParentType(mBoundStorageKeyActor) 79 : WeakRefParentType(mBackgroundIPCActor), 80 aOpArgs, INVALID_CACHE_ID, mNamespace); 81 } 82 83 bool CacheStorageParent::DeallocPCacheOpParent(PCacheOpParent* aActor) { 84 delete aActor; 85 return true; 86 } 87 88 mozilla::ipc::IPCResult CacheStorageParent::RecvPCacheOpConstructor( 89 PCacheOpParent* aActor, const CacheOpArgs& aOpArgs) { 90 auto actor = static_cast<CacheOpParent*>(aActor); 91 92 if (mVerifier) { 93 MOZ_DIAGNOSTIC_ASSERT(!mManagerId); 94 actor->WaitForVerification(mVerifier); 95 return IPC_OK(); 96 } 97 98 if (NS_WARN_IF(NS_FAILED(mVerifiedStatus))) { 99 QM_WARNONLY_TRY(OkIf(CacheOpParent::Send__delete__( 100 actor, CopyableErrorResult(mVerifiedStatus), void_t()))); 101 return IPC_OK(); 102 } 103 104 MOZ_DIAGNOSTIC_ASSERT(mManagerId); 105 actor->Execute(mManagerId); 106 return IPC_OK(); 107 } 108 109 mozilla::ipc::IPCResult CacheStorageParent::RecvTeardown() { 110 // If child process is gone, warn and allow actor to clean up normally 111 QM_WARNONLY_TRY(OkIf(Send__delete__(this))); 112 return IPC_OK(); 113 } 114 115 void CacheStorageParent::OnPrincipalVerified( 116 nsresult aRv, const SafeRefPtr<ManagerId>& aManagerId) { 117 MOZ_DIAGNOSTIC_ASSERT(mVerifier); 118 MOZ_DIAGNOSTIC_ASSERT(!mManagerId); 119 MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(mVerifiedStatus)); 120 121 if (NS_WARN_IF(NS_FAILED(aRv))) { 122 mVerifiedStatus = aRv; 123 } 124 125 mManagerId = aManagerId.clonePtr(); 126 mVerifier->RemoveListener(*this); 127 mVerifier = nullptr; 128 } 129 130 } // namespace mozilla::dom::cache