/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* 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_Promise_inl_h #define mozilla_dom_Promise_inl_h #include #include #include "mozilla/AlreadyAddRefed.h" #include "mozilla/ResultExtensions.h" #include "mozilla/dom/BindingUtils.h" #include "mozilla/dom/Promise.h" #include "mozilla/dom/PromiseNativeHandler.h" #include "nsCycleCollectionParticipant.h" namespace mozilla::dom { class PromiseNativeThenHandlerBase : public PromiseNativeHandler { NS_DECL_CYCLE_COLLECTING_ISUPPORTS NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(PromiseNativeThenHandlerBase) PromiseNativeThenHandlerBase(Promise* aPromise) : mPromise(aPromise) {} virtual bool HasResolvedCallback() = 0; virtual bool HasRejectedCallback() = 0; MOZ_CAN_RUN_SCRIPT void ResolvedCallback(JSContext* aCx, JS::Handle aValue, ErrorResult& aRv) override; MOZ_CAN_RUN_SCRIPT void RejectedCallback(JSContext* aCx, JS::Handle aValue, ErrorResult& aRv) override; protected: virtual ~PromiseNativeThenHandlerBase() = default; MOZ_CAN_RUN_SCRIPT virtual already_AddRefed CallResolveCallback( JSContext* aCx, JS::Handle aValue, ErrorResult& aRv) = 0; MOZ_CAN_RUN_SCRIPT virtual already_AddRefed CallRejectCallback( JSContext* aCx, JS::Handle aValue, ErrorResult& aRv) = 0; virtual void Traverse(nsCycleCollectionTraversalCallback&) = 0; virtual void Unlink() = 0; virtual void Trace(const TraceCallbacks& aCallbacks, void* aClosure) = 0; RefPtr mPromise; }; namespace { template >::value, bool = (std::is_convertible_v || std::is_convertible_v)> struct StorageTypeHelper { using Type = T; }; template struct StorageTypeHelper { using Type = nsCOMPtr; }; template struct StorageTypeHelper, true, true> { using Type = nsCOMPtr; }; template struct StorageTypeHelper { using Type = RefPtr; }; template struct StorageTypeHelper, false, false> { using Type = JS::Heap; }; template