DetailedPromise.h (3202B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 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 file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef __DetailedPromise_h__ 8 #define __DetailedPromise_h__ 9 10 #include "EMEUtils.h" 11 #include "mozilla/dom/Promise.h" 12 13 namespace mozilla::dom { 14 15 /* 16 * This is pretty horrible; bug 1160445. 17 * Extend Promise to add custom DOMException messages on rejection. 18 * Get rid of this once we've ironed out EME errors in the wild. 19 */ 20 class DetailedPromise : public Promise { 21 public: 22 static already_AddRefed<DetailedPromise> Create(nsIGlobalObject* aGlobal, 23 ErrorResult& aRv, 24 const nsACString& aName); 25 26 template <typename T> 27 void MaybeResolve(T&& aArg) { 28 EME_LOG("%s promise resolved", mName.get()); 29 Promise::MaybeResolve(std::forward<T>(aArg)); 30 } 31 32 void MaybeReject(nsresult aArg) = delete; 33 void MaybeReject(nsresult aArg, const nsACString& aReason); 34 35 void MaybeReject(ErrorResult&& aArg) = delete; 36 void MaybeReject(ErrorResult&& aArg, const nsACString& aReason); 37 38 // Facilities for rejecting with various spec-defined exception values. 39 #define DOMEXCEPTION(name, err) \ 40 inline void MaybeRejectWith##name(const nsACString& aMessage) { \ 41 LogRejectionReason(static_cast<uint32_t>(err), aMessage); \ 42 Promise::MaybeRejectWith##name(aMessage); \ 43 } \ 44 template <int N> \ 45 void MaybeRejectWith##name(const char(&aMessage)[N]) { \ 46 MaybeRejectWith##name(nsLiteralCString(aMessage)); \ 47 } 48 49 #include "mozilla/dom/DOMExceptionNames.h" 50 51 #undef DOMEXCEPTION 52 53 template <ErrNum errorNumber, typename... Ts> 54 void MaybeRejectWithTypeError(Ts&&... aMessageArgs) = delete; 55 56 inline void MaybeRejectWithTypeError(const nsACString& aMessage) { 57 ErrorResult res; 58 res.ThrowTypeError(aMessage); 59 MaybeReject(std::move(res), aMessage); 60 } 61 62 template <int N> 63 void MaybeRejectWithTypeError(const char (&aMessage)[N]) { 64 MaybeRejectWithTypeError(nsLiteralCString(aMessage)); 65 } 66 67 template <ErrNum errorNumber, typename... Ts> 68 void MaybeRejectWithRangeError(Ts&&... aMessageArgs) = delete; 69 70 inline void MaybeRejectWithRangeError(const nsACString& aMessage) { 71 ErrorResult res; 72 res.ThrowRangeError(aMessage); 73 MaybeReject(std::move(res), aMessage); 74 } 75 76 template <int N> 77 void MaybeRejectWithRangeError(const char (&aMessage)[N]) { 78 MaybeRejectWithRangeError(nsLiteralCString(aMessage)); 79 } 80 81 private: 82 explicit DetailedPromise(nsIGlobalObject* aGlobal, const nsACString& aName); 83 84 virtual ~DetailedPromise(); 85 86 enum eStatus { kSucceeded, kFailed }; 87 void LogRejectionReason(uint32_t aErrorCode, const nsACString& aReason); 88 89 nsCString mName; 90 TimeStamp mStartTime; 91 }; 92 93 } // namespace mozilla::dom 94 95 #endif // __DetailedPromise_h__