/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #ifndef mozilla_dom_cache_BoundStorageKeyCache_h #define mozilla_dom_cache_BoundStorageKeyCache_h #include "mozilla/dom/cache/CacheTypes.h" #include "mozilla/dom/cache/TypeUtils.h" #include "mozilla/dom/cache/Types.h" #include "nsCOMPtr.h" #include "nsISupportsImpl.h" class nsIGlobalObject; namespace mozilla { class ErrorResult; namespace dom { class OwningRequestOrUTF8String; struct CacheQueryOptions; class RequestOrUTF8String; class Response; class Request; template class Optional; template class Sequence; enum class CallerType : uint32_t; namespace cache { class AutoChildOpArgs; class CacheChild; using CachePromise = MozPromiseBase; /* This is similar to Cache class as BoundStorageKeyCacheStorage is to * CacheStorage i.e. * 1. Exposes and implements Cache APIs but uses MozPromise as it's return value * rather than JS Promise * 2. IPC communication can be retargetted to any event target as it's protocol * gets created on a top-level actor */ class BoundStorageKeyCache final : public nsISupports, public TypeUtils, public CacheChildListener { public: NS_DECL_ISUPPORTS using MatchResultPromise = mozilla::MozPromise, ErrorResult, true /* IsExclusive= */>; using MatchAllResultPromise = mozilla::MozPromise>, ErrorResult, true /* IsExclusive= */>; using AddResultPromise = mozilla::MozPromise; using AddAllResultPromise = mozilla::MozPromise; using PutResultPromise = mozilla::MozPromise; using PutAllResultPromise = mozilla::MozPromise; using DeleteResultPromise = mozilla::MozPromise; using KeysResultPromise = mozilla::MozPromise>, ErrorResult, true /* IsExclusive= */>; BoundStorageKeyCache(nsIGlobalObject* aGlobal, CacheChild* aActor, Namespace aNamespace); static bool CachesEnabled(JSContext* aCx, JSObject*); already_AddRefed Match(JSContext* aCx, const RequestOrUTF8String& aRequest, const CacheQueryOptions& aOptions, ErrorResult& aRv); already_AddRefed MatchAll( JSContext* aCx, const Optional& aRequest, const CacheQueryOptions& aOptions, ErrorResult& aRv); already_AddRefed Add(JSContext* aContext, const RequestOrUTF8String& aRequest, CallerType aCallerType, ErrorResult& aRv); already_AddRefed AddAll( JSContext* aContext, const Sequence& aRequestList, CallerType aCallerType, ErrorResult& aRv); already_AddRefed Put(JSContext* aCx, const RequestOrUTF8String& aRequest, Response& aResponse, ErrorResult& aRv); already_AddRefed Delete(JSContext* aCx, const RequestOrUTF8String& aRequest, const CacheQueryOptions& aOptions, ErrorResult& aRv); already_AddRefed Keys( JSContext* aCx, const Optional& aRequest, const CacheQueryOptions& aOptions, ErrorResult& aRv); // Called when CacheChild actor is being destroyed void OnActorDestroy(CacheChild* aActor) override; // TypeUtils methods virtual nsIGlobalObject* GetGlobalObject() const override; #ifdef DEBUG virtual void AssertOwningThread() const override; #endif private: ~BoundStorageKeyCache(); // Called when we're destroyed or CCed. void DisconnectFromActor(); void ExecuteOp(AutoChildOpArgs& aOpArgs, RefPtr& aPromise, ErrorResult& aRv); already_AddRefed AddAll( const GlobalObject& aGlobal, nsTArray>&& aRequestList, CallerType aCallerType, ErrorResult& aRv); already_AddRefed PutAll( JSContext* aCx, const nsTArray>& aRequestList, const nsTArray>& aResponseList, ErrorResult& aRv); OpenMode GetOpenMode() const; nsCOMPtr mGlobal; CacheChild* mActor; const Namespace mNamespace; }; } // namespace cache template struct cachestorage_traits; template <> struct cachestorage_traits { using PromiseType = cache::BoundStorageKeyCache::MatchResultPromise::Private; }; template <> struct cachestorage_traits< dom::cache::CacheOpResult::Type::TCacheMatchAllResult> { using PromiseType = cache::BoundStorageKeyCache::MatchAllResultPromise::Private; }; template <> struct cachestorage_traits< dom::cache::CacheOpResult::Type::TCachePutAllResult> { using PromiseType = cache::BoundStorageKeyCache::PutAllResultPromise::Private; }; template <> struct cachestorage_traits< dom::cache::CacheOpResult::Type::TCacheDeleteResult> { using PromiseType = cache::BoundStorageKeyCache::DeleteResultPromise::Private; }; template <> struct cachestorage_traits { using PromiseType = cache::BoundStorageKeyCache::KeysResultPromise::Private; }; } // namespace dom } // namespace mozilla #endif // mozilla_dom_cache_BoundStorageKeyCache_h