Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | #ifndef nsJSEnvironment_h |
michael@0 | 6 | #define nsJSEnvironment_h |
michael@0 | 7 | |
michael@0 | 8 | #include "nsIScriptContext.h" |
michael@0 | 9 | #include "nsIScriptGlobalObject.h" |
michael@0 | 10 | #include "nsCOMPtr.h" |
michael@0 | 11 | #include "nsIObserver.h" |
michael@0 | 12 | #include "prtime.h" |
michael@0 | 13 | #include "nsCycleCollectionParticipant.h" |
michael@0 | 14 | #include "nsIXPConnect.h" |
michael@0 | 15 | #include "nsIArray.h" |
michael@0 | 16 | #include "mozilla/Attributes.h" |
michael@0 | 17 | #include "nsThreadUtils.h" |
michael@0 | 18 | |
michael@0 | 19 | class nsICycleCollectorListener; |
michael@0 | 20 | class nsIXPConnectJSObjectHolder; |
michael@0 | 21 | class nsScriptNameSpaceManager; |
michael@0 | 22 | class nsCycleCollectionNoteRootCallback; |
michael@0 | 23 | |
michael@0 | 24 | namespace JS { |
michael@0 | 25 | class AutoValueVector; |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | namespace mozilla { |
michael@0 | 29 | template <class> class Maybe; |
michael@0 | 30 | struct CycleCollectorResults; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | // The amount of time we wait between a request to GC (due to leaving |
michael@0 | 34 | // a page) and doing the actual GC. |
michael@0 | 35 | #define NS_GC_DELAY 4000 // ms |
michael@0 | 36 | |
michael@0 | 37 | class nsJSContext : public nsIScriptContext |
michael@0 | 38 | { |
michael@0 | 39 | public: |
michael@0 | 40 | nsJSContext(bool aGCOnDestruction, nsIScriptGlobalObject* aGlobalObject); |
michael@0 | 41 | virtual ~nsJSContext(); |
michael@0 | 42 | |
michael@0 | 43 | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
michael@0 | 44 | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(nsJSContext, |
michael@0 | 45 | nsIScriptContext) |
michael@0 | 46 | |
michael@0 | 47 | virtual nsIScriptGlobalObject *GetGlobalObject() MOZ_OVERRIDE; |
michael@0 | 48 | inline nsIScriptGlobalObject *GetGlobalObjectRef() { return mGlobalObjectRef; } |
michael@0 | 49 | |
michael@0 | 50 | virtual JSContext* GetNativeContext() MOZ_OVERRIDE; |
michael@0 | 51 | virtual nsresult InitContext() MOZ_OVERRIDE; |
michael@0 | 52 | virtual bool IsContextInitialized() MOZ_OVERRIDE; |
michael@0 | 53 | |
michael@0 | 54 | virtual nsresult SetProperty(JS::Handle<JSObject*> aTarget, const char* aPropName, nsISupports* aVal) MOZ_OVERRIDE; |
michael@0 | 55 | |
michael@0 | 56 | virtual bool GetProcessingScriptTag() MOZ_OVERRIDE; |
michael@0 | 57 | virtual void SetProcessingScriptTag(bool aResult) MOZ_OVERRIDE; |
michael@0 | 58 | |
michael@0 | 59 | virtual nsresult InitClasses(JS::Handle<JSObject*> aGlobalObj) MOZ_OVERRIDE; |
michael@0 | 60 | |
michael@0 | 61 | virtual void WillInitializeContext() MOZ_OVERRIDE; |
michael@0 | 62 | virtual void DidInitializeContext() MOZ_OVERRIDE; |
michael@0 | 63 | |
michael@0 | 64 | virtual void SetWindowProxy(JS::Handle<JSObject*> aWindowProxy) MOZ_OVERRIDE; |
michael@0 | 65 | virtual JSObject* GetWindowProxy() MOZ_OVERRIDE; |
michael@0 | 66 | virtual JSObject* GetWindowProxyPreserveColor() MOZ_OVERRIDE; |
michael@0 | 67 | |
michael@0 | 68 | static void LoadStart(); |
michael@0 | 69 | static void LoadEnd(); |
michael@0 | 70 | |
michael@0 | 71 | enum IsCompartment { |
michael@0 | 72 | CompartmentGC, |
michael@0 | 73 | NonCompartmentGC |
michael@0 | 74 | }; |
michael@0 | 75 | |
michael@0 | 76 | enum IsShrinking { |
michael@0 | 77 | ShrinkingGC, |
michael@0 | 78 | NonShrinkingGC |
michael@0 | 79 | }; |
michael@0 | 80 | |
michael@0 | 81 | enum IsIncremental { |
michael@0 | 82 | IncrementalGC, |
michael@0 | 83 | NonIncrementalGC |
michael@0 | 84 | }; |
michael@0 | 85 | |
michael@0 | 86 | // Setup all the statics etc - safe to call multiple times after Startup(). |
michael@0 | 87 | void EnsureStatics(); |
michael@0 | 88 | |
michael@0 | 89 | static void GarbageCollectNow(JS::gcreason::Reason reason, |
michael@0 | 90 | IsIncremental aIncremental = NonIncrementalGC, |
michael@0 | 91 | IsCompartment aCompartment = NonCompartmentGC, |
michael@0 | 92 | IsShrinking aShrinking = NonShrinkingGC, |
michael@0 | 93 | int64_t aSliceMillis = 0); |
michael@0 | 94 | static void ShrinkGCBuffersNow(); |
michael@0 | 95 | |
michael@0 | 96 | // If aExtraForgetSkippableCalls is -1, forgetSkippable won't be |
michael@0 | 97 | // called even if the previous collection was GC. |
michael@0 | 98 | static void CycleCollectNow(nsICycleCollectorListener *aListener = nullptr, |
michael@0 | 99 | int32_t aExtraForgetSkippableCalls = 0); |
michael@0 | 100 | |
michael@0 | 101 | // Run a cycle collector slice, using a heuristic to decide how long to run it. |
michael@0 | 102 | static void RunCycleCollectorSlice(); |
michael@0 | 103 | |
michael@0 | 104 | static void BeginCycleCollectionCallback(); |
michael@0 | 105 | static void EndCycleCollectionCallback(mozilla::CycleCollectorResults &aResults); |
michael@0 | 106 | |
michael@0 | 107 | static void RunNextCollectorTimer(); |
michael@0 | 108 | |
michael@0 | 109 | static void PokeGC(JS::gcreason::Reason aReason, int aDelay = 0); |
michael@0 | 110 | static void KillGCTimer(); |
michael@0 | 111 | |
michael@0 | 112 | static void PokeShrinkGCBuffers(); |
michael@0 | 113 | static void KillShrinkGCBuffersTimer(); |
michael@0 | 114 | |
michael@0 | 115 | static void MaybePokeCC(); |
michael@0 | 116 | static void KillCCTimer(); |
michael@0 | 117 | static void KillICCTimer(); |
michael@0 | 118 | static void KillFullGCTimer(); |
michael@0 | 119 | static void KillInterSliceGCTimer(); |
michael@0 | 120 | |
michael@0 | 121 | // Calling LikelyShortLivingObjectCreated() makes a GC more likely. |
michael@0 | 122 | static void LikelyShortLivingObjectCreated(); |
michael@0 | 123 | |
michael@0 | 124 | virtual void GC(JS::gcreason::Reason aReason) MOZ_OVERRIDE; |
michael@0 | 125 | |
michael@0 | 126 | static uint32_t CleanupsSinceLastGC(); |
michael@0 | 127 | |
michael@0 | 128 | nsIScriptGlobalObject* GetCachedGlobalObject() |
michael@0 | 129 | { |
michael@0 | 130 | // Verify that we have a global so that this |
michael@0 | 131 | // does always return a null when GetGlobalObject() is null. |
michael@0 | 132 | JSObject* global = GetWindowProxy(); |
michael@0 | 133 | return global ? mGlobalObjectRef.get() : nullptr; |
michael@0 | 134 | } |
michael@0 | 135 | protected: |
michael@0 | 136 | nsresult InitializeExternalClasses(); |
michael@0 | 137 | |
michael@0 | 138 | // Helper to convert xpcom datatypes to jsvals. |
michael@0 | 139 | nsresult ConvertSupportsTojsvals(nsISupports *aArgs, |
michael@0 | 140 | JS::Handle<JSObject*> aScope, |
michael@0 | 141 | JS::AutoValueVector &aArgsOut); |
michael@0 | 142 | |
michael@0 | 143 | nsresult AddSupportsPrimitiveTojsvals(nsISupports *aArg, JS::Value *aArgv); |
michael@0 | 144 | |
michael@0 | 145 | // Report the pending exception on our mContext, if any. This |
michael@0 | 146 | // function will set aside the frame chain on mContext before |
michael@0 | 147 | // reporting. |
michael@0 | 148 | void ReportPendingException(); |
michael@0 | 149 | |
michael@0 | 150 | private: |
michael@0 | 151 | void DestroyJSContext(); |
michael@0 | 152 | |
michael@0 | 153 | nsrefcnt GetCCRefcnt(); |
michael@0 | 154 | |
michael@0 | 155 | JSContext *mContext; |
michael@0 | 156 | JS::Heap<JSObject*> mWindowProxy; |
michael@0 | 157 | |
michael@0 | 158 | bool mIsInitialized; |
michael@0 | 159 | bool mGCOnDestruction; |
michael@0 | 160 | bool mProcessingScriptTag; |
michael@0 | 161 | |
michael@0 | 162 | PRTime mModalStateTime; |
michael@0 | 163 | uint32_t mModalStateDepth; |
michael@0 | 164 | |
michael@0 | 165 | // mGlobalObjectRef ensures that the outer window stays alive as long as the |
michael@0 | 166 | // context does. It is eventually collected by the cycle collector. |
michael@0 | 167 | nsCOMPtr<nsIScriptGlobalObject> mGlobalObjectRef; |
michael@0 | 168 | |
michael@0 | 169 | static void JSOptionChangedCallback(const char *pref, void *data); |
michael@0 | 170 | |
michael@0 | 171 | static bool DOMOperationCallback(JSContext *cx); |
michael@0 | 172 | }; |
michael@0 | 173 | |
michael@0 | 174 | class nsIJSRuntimeService; |
michael@0 | 175 | class nsIPrincipal; |
michael@0 | 176 | class nsPIDOMWindow; |
michael@0 | 177 | |
michael@0 | 178 | namespace mozilla { |
michael@0 | 179 | namespace dom { |
michael@0 | 180 | |
michael@0 | 181 | void StartupJSEnvironment(); |
michael@0 | 182 | void ShutdownJSEnvironment(); |
michael@0 | 183 | |
michael@0 | 184 | // Get the NameSpaceManager, creating if necessary |
michael@0 | 185 | nsScriptNameSpaceManager* GetNameSpaceManager(); |
michael@0 | 186 | |
michael@0 | 187 | // Runnable that's used to do async error reporting |
michael@0 | 188 | class AsyncErrorReporter : public nsRunnable |
michael@0 | 189 | { |
michael@0 | 190 | public: |
michael@0 | 191 | // aWindow may be null if this error report is not associated with a window |
michael@0 | 192 | AsyncErrorReporter(JSRuntime* aRuntime, |
michael@0 | 193 | JSErrorReport* aErrorReport, |
michael@0 | 194 | const char* aFallbackMessage, |
michael@0 | 195 | bool aIsChromeError, // To determine category |
michael@0 | 196 | nsPIDOMWindow* aWindow); |
michael@0 | 197 | |
michael@0 | 198 | NS_IMETHOD Run() |
michael@0 | 199 | { |
michael@0 | 200 | ReportError(); |
michael@0 | 201 | return NS_OK; |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | protected: |
michael@0 | 205 | // Do the actual error reporting |
michael@0 | 206 | void ReportError(); |
michael@0 | 207 | |
michael@0 | 208 | nsString mErrorMsg; |
michael@0 | 209 | nsString mFileName; |
michael@0 | 210 | nsString mSourceLine; |
michael@0 | 211 | nsCString mCategory; |
michael@0 | 212 | uint32_t mLineNumber; |
michael@0 | 213 | uint32_t mColumn; |
michael@0 | 214 | uint32_t mFlags; |
michael@0 | 215 | uint64_t mInnerWindowID; |
michael@0 | 216 | }; |
michael@0 | 217 | |
michael@0 | 218 | } // namespace dom |
michael@0 | 219 | } // namespace mozilla |
michael@0 | 220 | |
michael@0 | 221 | // An interface for fast and native conversion to/from nsIArray. If an object |
michael@0 | 222 | // supports this interface, JS can reach directly in for the argv, and avoid |
michael@0 | 223 | // nsISupports conversion. If this interface is not supported, the object will |
michael@0 | 224 | // be queried for nsIArray, and everything converted via xpcom objects. |
michael@0 | 225 | #define NS_IJSARGARRAY_IID \ |
michael@0 | 226 | { 0xb6acdac8, 0xf5c6, 0x432c, \ |
michael@0 | 227 | { 0xa8, 0x6e, 0x33, 0xee, 0xb1, 0xb0, 0xcd, 0xdc } } |
michael@0 | 228 | |
michael@0 | 229 | class nsIJSArgArray : public nsIArray |
michael@0 | 230 | { |
michael@0 | 231 | public: |
michael@0 | 232 | NS_DECLARE_STATIC_IID_ACCESSOR(NS_IJSARGARRAY_IID) |
michael@0 | 233 | // Bug 312003 describes why this must be "void **", but after calling argv |
michael@0 | 234 | // may be cast to JS::Value* and the args found at: |
michael@0 | 235 | // ((JS::Value*)argv)[0], ..., ((JS::Value*)argv)[argc - 1] |
michael@0 | 236 | virtual nsresult GetArgs(uint32_t *argc, void **argv) = 0; |
michael@0 | 237 | }; |
michael@0 | 238 | |
michael@0 | 239 | NS_DEFINE_STATIC_IID_ACCESSOR(nsIJSArgArray, NS_IJSARGARRAY_IID) |
michael@0 | 240 | |
michael@0 | 241 | /* prototypes */ |
michael@0 | 242 | void NS_ScriptErrorReporter(JSContext *cx, const char *message, JSErrorReport *report); |
michael@0 | 243 | |
michael@0 | 244 | JSObject* NS_DOMReadStructuredClone(JSContext* cx, |
michael@0 | 245 | JSStructuredCloneReader* reader, uint32_t tag, |
michael@0 | 246 | uint32_t data, void* closure); |
michael@0 | 247 | |
michael@0 | 248 | bool NS_DOMWriteStructuredClone(JSContext* cx, |
michael@0 | 249 | JSStructuredCloneWriter* writer, |
michael@0 | 250 | JS::Handle<JSObject*> obj, void *closure); |
michael@0 | 251 | |
michael@0 | 252 | void NS_DOMStructuredCloneError(JSContext* cx, uint32_t errorid); |
michael@0 | 253 | |
michael@0 | 254 | #endif /* nsJSEnvironment_h */ |