CacheOpChild.h (3951B)
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 #ifndef mozilla_dom_cache_CacheOpChild_h 8 #define mozilla_dom_cache_CacheOpChild_h 9 10 #include "mozilla/InitializedOnce.h" 11 #include "mozilla/RefPtr.h" 12 #include "mozilla/dom/cache/ActorChild.h" 13 #include "mozilla/dom/cache/BoundStorageKey.h" 14 #include "mozilla/dom/cache/PCacheOpChild.h" 15 #include "mozilla/dom/cache/TypeUtils.h" 16 17 class nsIGlobalObject; 18 19 namespace mozilla::dom { 20 class Promise; 21 22 namespace cache { 23 class CacheOpChild final : public PCacheOpChild, 24 public CacheActorChild, 25 public TypeUtils { 26 friend class CacheChild; 27 friend class CacheStorageChild; 28 friend class PCacheOpChild; 29 30 public: 31 NS_INLINE_DECL_REFCOUNTING(CacheOpChild, override) 32 33 private: 34 // CacheOpChild can be used by Cache, CacheStorage and BoundStorageKey APIs. 35 // It can handle two promise types; where Cache works with dom::Promise, 36 // BoundStorageKey APIs works with MozPromise (represented by 37 // CacheStoragePromise below) 38 using PromiseType = 39 Variant<RefPtr<mozilla::dom::Promise>, RefPtr<CacheStoragePromise>>; 40 41 template <typename T> 42 struct PromiseTrait; 43 44 // This class must be constructed by CacheChild or CacheStorageChild using 45 // their ExecuteOp() factory method. 46 CacheOpChild(SafeRefPtr<CacheWorkerRef> aWorkerRef, nsIGlobalObject* aGlobal, 47 nsISupports* aParent, RefPtr<Promise>& aPromise, 48 ActorChild* aParentActor); 49 50 // Below overload is used by BoundStorageKey APIs; passing in 51 // CacheStoragePromise 52 CacheOpChild(SafeRefPtr<CacheWorkerRef> aWorkerRef, nsIGlobalObject* aGlobal, 53 nsISupports* aParent, RefPtr<CacheStoragePromise>& aPromise, 54 ActorChild* aParentActor); 55 56 ~CacheOpChild(); 57 58 // PCacheOpChild methods 59 virtual void ActorDestroy(ActorDestroyReason aReason) override; 60 61 mozilla::ipc::IPCResult Recv__delete__(ErrorResult&& aRv, 62 const CacheOpResult& aResult); 63 64 // ActorChild methods 65 virtual void StartDestroy() override; 66 67 // TypeUtils methods 68 virtual nsIGlobalObject* GetGlobalObject() const override; 69 70 #ifdef DEBUG 71 virtual void AssertOwningThread() const override; 72 #endif 73 74 // Utility methods 75 76 /* Generic method to handle all response types, formats responses before 77 * resolving for the underlying promises */ 78 template <CacheOpResult::Type OP_TYPE, typename TResponse> 79 void HandleAndSettle(TResponse&& aRes); 80 81 // generic settle overload which routes the control to one of two 82 // SettlePromise method overloads below depending if we are resolving for 83 // native JS promise vs MozPromise 84 template <CacheOpResult::Type OP_TYPE, typename ResultType> 85 void Settle(ResultType&& aRes, ErrorResult&& aRv = ErrorResult(NS_OK)); 86 87 // settles promise for BoundStorageKeyCache; which is of type MozPromise 88 template <CacheOpResult::Type OP_TYPE, typename ResultType> 89 void SettlePromise(ResultType&& aRes, ErrorResult&& aRv, 90 const RefPtr<CacheStoragePromise>& aThePromise); 91 92 // settles promise for cache, which is of type dom::Promise 93 template <typename CacheOpResult::Type OP_TYPE, typename ResultType> 94 void SettlePromise(ResultType&& aRes, ErrorResult&& aRv, 95 const RefPtr<Promise>& aThePromise); 96 97 nsCOMPtr<nsIGlobalObject> mGlobal; 98 99 // Hold the parent Cache or CacheStorage object alive until this async 100 // operation completes. 101 nsCOMPtr<nsISupports> mParent; 102 LazyInitializedOnceEarlyDestructible<const PromiseType> mPromise; 103 ActorChild* mParentActor; 104 }; 105 106 } // namespace cache 107 } // namespace mozilla::dom 108 109 #endif // mozilla_dom_cache_CacheOpChild_h