DOMException.h (5760B)
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_DOMException_h__ 8 #define mozilla_dom_DOMException_h__ 9 10 // We intentionally shadow non-virtual methods, but gcc gets confused. 11 #ifdef __GNUC__ 12 # pragma GCC diagnostic push 13 # pragma GCC diagnostic ignored "-Woverloaded-virtual" 14 #endif 15 16 #include <stdint.h> 17 18 #include "js/Value.h" 19 #include "jspubtd.h" 20 #include "mozilla/dom/BindingDeclarations.h" 21 #include "nsCOMPtr.h" 22 #include "nsCycleCollectionParticipant.h" 23 #include "nsID.h" 24 #include "nsIException.h" 25 #include "nsString.h" 26 #include "nsWrapperCache.h" 27 28 class nsIGlobalObject; 29 class nsIStackFrame; 30 31 nsresult NS_GetNameAndMessageForDOMNSResult(nsresult aNSResult, 32 nsACString& aName, 33 nsACString& aMessage, 34 uint16_t* aCode = nullptr); 35 36 namespace mozilla { 37 class ErrorResult; 38 39 namespace dom { 40 41 class GlobalObject; 42 43 #define MOZILLA_EXCEPTION_IID \ 44 {0x55eda557, 0xeba0, 0x4fe3, {0xae, 0x2e, 0xf3, 0x94, 0x49, 0x23, 0x62, 0xd6}} 45 46 class Exception : public nsIException, public nsWrapperCache { 47 public: 48 NS_INLINE_DECL_STATIC_IID(MOZILLA_EXCEPTION_IID) 49 50 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Exception) 51 52 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 53 NS_DECL_NSIEXCEPTION 54 55 const nsCString& GetMessageMoz() const { return mMessage; } 56 nsresult GetResult() const { return mResult; } 57 // DOMException wants different ToString behavior, so allow it to override. 58 virtual void ToString(JSContext* aCx, nsACString& aReturn); 59 60 // Cruft used by XPConnect for exceptions originating in JS implemented 61 // components. 62 bool StealJSVal(JS::Value* aVp); 63 void StowJSVal(JS::Value& aVp); 64 65 // WebIDL API 66 virtual JSObject* WrapObject(JSContext* cx, 67 JS::Handle<JSObject*> aGivenProto) override; 68 69 nsISupports* GetParentObject() const { return nullptr; } 70 71 void GetMessageMoz(nsString& retval); 72 73 uint32_t Result() const; 74 75 void GetName(nsAString& retval); 76 77 virtual void GetErrorMessage(nsAString& aRetVal) { 78 // Since GetName is non non-virtual and deals with different 79 // member variables in Exception vs. DOMException, have a virtual 80 // method to ensure the right error message creation. 81 nsAutoString name; 82 GetName(name); 83 CreateErrorMessage(name, aRetVal); 84 } 85 86 void GetFilename(JSContext* aCx, nsACString& aFilename); 87 88 uint32_t SourceId(JSContext* aCx) const; 89 90 uint32_t LineNumber(JSContext* aCx) const; 91 92 uint32_t ColumnNumber(JSContext* aCx) const; 93 94 already_AddRefed<nsIStackFrame> GetLocation() const; 95 96 nsISupports* GetData() const; 97 98 void GetStack(JSContext* aCx, nsAString& aStack) const; 99 100 void Stringify(JSContext* aCx, nsString& retval); 101 102 Exception(const nsACString& aMessage, nsresult aResult, 103 const nsACString& aName, nsIStackFrame* aLocation, 104 nsISupports* aData); 105 Exception(nsCString&& aMessage, nsresult aResult, nsCString&& aName); 106 107 protected: 108 virtual ~Exception(); 109 110 void CreateErrorMessage(const nsAString& aName, nsAString& aRetVal) { 111 // Create similar error message as what ErrorReport::init does in jsexn.cpp. 112 if (!aName.IsEmpty() && !mMessage.IsEmpty()) { 113 aRetVal.Assign(aName); 114 aRetVal.AppendLiteral(": "); 115 AppendUTF8toUTF16(mMessage, aRetVal); 116 } else if (!aName.IsEmpty()) { 117 aRetVal.Assign(aName); 118 } else if (!mMessage.IsEmpty()) { 119 CopyUTF8toUTF16(mMessage, aRetVal); 120 } else { 121 aRetVal.Truncate(); 122 } 123 } 124 125 nsCString mMessage; 126 nsresult mResult; 127 nsCString mName; 128 nsCOMPtr<nsIStackFrame> mLocation; 129 nsCOMPtr<nsISupports> mData; 130 131 bool mHoldingJSVal; 132 JS::Heap<JS::Value> mThrownJSVal; 133 }; 134 135 class DOMException : public Exception { 136 public: 137 DOMException(nsresult aRv, const nsACString& aMessage, 138 const nsACString& aName, uint16_t aCode, 139 nsIStackFrame* aLocation = nullptr); 140 DOMException(nsresult aRv, nsCString&& aMessage, nsCString&& aName, 141 uint16_t aCode); 142 143 NS_INLINE_DECL_REFCOUNTING_INHERITED(DOMException, Exception) 144 145 // nsWrapperCache overrides 146 virtual JSObject* WrapObject(JSContext* aCx, 147 JS::Handle<JSObject*> aGivenProto) override; 148 149 static already_AddRefed<DOMException> Constructor( 150 GlobalObject& /* unused */, const nsAString& aMessage, 151 const Optional<nsAString>& aName); 152 153 uint16_t Code() const { return mCode; } 154 155 // Intentionally shadow the Exception version. 156 void GetName(nsString& retval); 157 158 // Exception overrides 159 void ToString(JSContext* aCx, nsACString& aReturn) override; 160 161 virtual void GetErrorMessage(nsAString& aRetVal) override { 162 // See the comment in Exception::GetErrorMessage. 163 nsAutoString name; 164 GetName(name); 165 CreateErrorMessage(name, aRetVal); 166 } 167 168 static already_AddRefed<DOMException> Create(nsresult aRv); 169 170 static already_AddRefed<DOMException> Create(nsresult aRv, 171 const nsACString& aMessage); 172 173 static already_AddRefed<DOMException> ReadStructuredClone( 174 JSContext* aCx, nsIGlobalObject* aGlobal, 175 JSStructuredCloneReader* aReader); 176 bool WriteStructuredClone(JSContext* aCx, 177 JSStructuredCloneWriter* aWriter) const; 178 179 protected: 180 virtual ~DOMException() = default; 181 182 uint16_t mCode; 183 }; 184 185 } // namespace dom 186 } // namespace mozilla 187 188 #ifdef __GNUC__ 189 # pragma GCC diagnostic pop 190 #endif 191 192 #endif