nsScriptError.h (4321B)
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 file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_nsScriptError_h 8 #define mozilla_dom_nsScriptError_h 9 10 #include <stdint.h> 11 12 #include "js/RootingAPI.h" 13 #include "jsapi.h" 14 #include "mozilla/Atomics.h" 15 #include "nsCOMArray.h" 16 #include "nsCycleCollectionParticipant.h" 17 #include "nsIScriptError.h" 18 #include "nsString.h" 19 20 class nsGlobalWindowInner; 21 22 class nsScriptErrorNote final : public nsIScriptErrorNote { 23 public: 24 nsScriptErrorNote(); 25 26 NS_DECL_THREADSAFE_ISUPPORTS 27 NS_DECL_NSISCRIPTERRORNOTE 28 29 void Init(const nsAString& message, const nsACString& sourceName, 30 uint32_t sourceId, uint32_t lineNumber, uint32_t columnNumber); 31 32 private: 33 virtual ~nsScriptErrorNote(); 34 35 nsString mMessage; 36 nsCString mSourceName; 37 nsString mCssSelectors; 38 uint32_t mSourceId; 39 uint32_t mLineNumber; 40 uint32_t mColumnNumber; 41 }; 42 43 // Definition of nsScriptError.. 44 class nsScriptErrorBase : public nsIScriptError { 45 public: 46 nsScriptErrorBase(); 47 48 NS_DECL_NSICONSOLEMESSAGE 49 NS_DECL_NSISCRIPTERROR 50 51 void AddNote(nsIScriptErrorNote* note); 52 53 static bool ComputeIsFromPrivateWindow(nsGlobalWindowInner* aWindow); 54 55 static bool ComputeIsFromChromeContext(nsGlobalWindowInner* aWindow); 56 57 protected: 58 virtual ~nsScriptErrorBase(); 59 60 void InitializeOnMainThread(); 61 62 void InitializationHelper(const nsAString& message, uint32_t lineNumber, 63 uint32_t columnNumber, uint32_t flags, 64 const nsACString& category, uint64_t aInnerWindowID, 65 bool aFromChromeContext); 66 67 nsCOMArray<nsIScriptErrorNote> mNotes; 68 nsString mMessage; 69 nsString mMessageName; 70 nsCString mSourceName; 71 nsString mCssSelectors; 72 uint32_t mSourceId; 73 uint32_t mLineNumber; 74 uint32_t mColumnNumber; 75 uint32_t mFlags; 76 nsCString mCategory; 77 // mOuterWindowID is set on the main thread from InitializeOnMainThread(). 78 uint64_t mOuterWindowID; 79 uint64_t mInnerWindowID; 80 int64_t mMicroSecondTimeStamp; 81 // mInitializedOnMainThread, mIsFromPrivateWindow and mIsFromChromeContext are 82 // set on the main thread from InitializeOnMainThread(). 83 mozilla::Atomic<bool> mInitializedOnMainThread; 84 bool mIsFromPrivateWindow; 85 bool mIsFromChromeContext; 86 bool mIsPromiseRejection; 87 bool mIsForwardedFromContentProcess; 88 }; 89 90 class nsScriptError final : public nsScriptErrorBase { 91 public: 92 nsScriptError() = default; 93 NS_DECL_THREADSAFE_ISUPPORTS 94 95 private: 96 virtual ~nsScriptError() = default; 97 }; 98 99 class nsScriptErrorWithStack : public nsScriptErrorBase { 100 public: 101 nsScriptErrorWithStack(JS::Handle<mozilla::Maybe<JS::Value>> aException, 102 JS::Handle<JSObject*> aStack, 103 JS::Handle<JSObject*> aStackGlobal); 104 105 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 106 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsScriptErrorWithStack) 107 108 NS_IMETHOD GetHasException(bool*) override; 109 NS_IMETHOD GetException(JS::MutableHandle<JS::Value>) override; 110 111 NS_IMETHOD GetStack(JS::MutableHandle<JS::Value>) override; 112 NS_IMETHOD GetStackGlobal(JS::MutableHandle<JS::Value>) override; 113 NS_IMETHOD ToString(nsACString& aResult) override; 114 115 private: 116 virtual ~nsScriptErrorWithStack(); 117 118 // The "exception" value. 119 JS::Heap<JS::Value> mException; 120 bool mHasException; 121 122 // Complete stackframe where the error happened. 123 // Must be a (possibly wrapped) SavedFrame object. 124 JS::Heap<JSObject*> mStack; 125 // Global object that must be same-compartment with mStack. 126 JS::Heap<JSObject*> mStackGlobal; 127 }; 128 129 // Creates either nsScriptErrorWithStack or nsScriptError, 130 // depending on whether |aStack| or |aException| is passed. 131 // Additionally when the first (optional) |win| argument is 132 // provided this function makes sure that the GlobalWindow 133 // isn't already dying to prevent leaks. 134 already_AddRefed<nsScriptErrorBase> CreateScriptError( 135 nsGlobalWindowInner* win, JS::Handle<mozilla::Maybe<JS::Value>> aException, 136 JS::Handle<JSObject*> aStack, JS::Handle<JSObject*> aStackGlobal); 137 138 #endif /* mozilla_dom_nsScriptError_h */