js/xpconnect/src/nsCxPusher.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "nsCxPusher.h"
michael@0 8
michael@0 9 #include "nsIScriptContext.h"
michael@0 10 #include "mozilla/dom/EventTarget.h"
michael@0 11 #include "nsDOMJSUtils.h"
michael@0 12 #include "xpcprivate.h"
michael@0 13 #include "WorkerPrivate.h"
michael@0 14
michael@0 15 using mozilla::dom::EventTarget;
michael@0 16 using mozilla::DebugOnly;
michael@0 17
michael@0 18 bool
michael@0 19 nsCxPusher::Push(EventTarget *aCurrentTarget)
michael@0 20 {
michael@0 21 MOZ_ASSERT(mPusher.empty());
michael@0 22 NS_ENSURE_TRUE(aCurrentTarget, false);
michael@0 23 nsresult rv;
michael@0 24 nsIScriptContext* scx =
michael@0 25 aCurrentTarget->GetContextForEventHandlers(&rv);
michael@0 26 #ifdef DEBUG_smaug
michael@0 27 NS_ENSURE_SUCCESS(rv, false);
michael@0 28 #else
michael@0 29 if(NS_FAILED(rv)) {
michael@0 30 return false;
michael@0 31 }
michael@0 32 #endif
michael@0 33
michael@0 34 if (!scx) {
michael@0 35 // The target may have a special JS context for event handlers.
michael@0 36 JSContext* cx = aCurrentTarget->GetJSContextForEventHandlers();
michael@0 37 if (cx) {
michael@0 38 mPusher.construct(cx);
michael@0 39 }
michael@0 40
michael@0 41 // Nothing to do here, I guess. Have to return true so that event firing
michael@0 42 // will still work correctly even if there is no associated JSContext
michael@0 43 return true;
michael@0 44 }
michael@0 45
michael@0 46 mPusher.construct(scx->GetNativeContext());
michael@0 47 return true;
michael@0 48 }
michael@0 49
michael@0 50 bool
michael@0 51 nsCxPusher::RePush(EventTarget *aCurrentTarget)
michael@0 52 {
michael@0 53 if (mPusher.empty()) {
michael@0 54 return Push(aCurrentTarget);
michael@0 55 }
michael@0 56
michael@0 57 if (aCurrentTarget) {
michael@0 58 nsresult rv;
michael@0 59 nsIScriptContext* scx =
michael@0 60 aCurrentTarget->GetContextForEventHandlers(&rv);
michael@0 61 if (NS_FAILED(rv)) {
michael@0 62 mPusher.destroy();
michael@0 63 return false;
michael@0 64 }
michael@0 65
michael@0 66 // If we have the same script context and native context is still
michael@0 67 // alive, no need to Pop/Push.
michael@0 68 if (scx && scx == mPusher.ref().GetScriptContext() &&
michael@0 69 scx->GetNativeContext()) {
michael@0 70 return true;
michael@0 71 }
michael@0 72 }
michael@0 73
michael@0 74 mPusher.destroy();
michael@0 75 return Push(aCurrentTarget);
michael@0 76 }
michael@0 77
michael@0 78 void
michael@0 79 nsCxPusher::Push(JSContext *cx)
michael@0 80 {
michael@0 81 mPusher.construct(cx);
michael@0 82 }
michael@0 83
michael@0 84 void
michael@0 85 nsCxPusher::PushNull()
michael@0 86 {
michael@0 87 // Note: The Maybe<> template magic seems to need the static_cast below to
michael@0 88 // work right on some older compilers.
michael@0 89 mPusher.construct(static_cast<JSContext*>(nullptr), /* aAllowNull = */ true);
michael@0 90 }
michael@0 91
michael@0 92 void
michael@0 93 nsCxPusher::Pop()
michael@0 94 {
michael@0 95 if (!mPusher.empty())
michael@0 96 mPusher.destroy();
michael@0 97 }
michael@0 98
michael@0 99 namespace mozilla {
michael@0 100
michael@0 101 AutoCxPusher::AutoCxPusher(JSContext* cx, bool allowNull)
michael@0 102 {
michael@0 103 MOZ_ASSERT_IF(!allowNull, cx);
michael@0 104
michael@0 105 // Hold a strong ref to the nsIScriptContext, if any. This ensures that we
michael@0 106 // only destroy the mContext of an nsJSContext when it is not on the cx stack
michael@0 107 // (and therefore not in use). See nsJSContext::DestroyJSContext().
michael@0 108 if (cx)
michael@0 109 mScx = GetScriptContextFromJSContext(cx);
michael@0 110
michael@0 111 XPCJSContextStack *stack = XPCJSRuntime::Get()->GetJSContextStack();
michael@0 112 if (!stack->Push(cx)) {
michael@0 113 MOZ_CRASH();
michael@0 114 }
michael@0 115 mStackDepthAfterPush = stack->Count();
michael@0 116
michael@0 117 #ifdef DEBUG
michael@0 118 mPushedContext = cx;
michael@0 119 mCompartmentDepthOnEntry = cx ? js::GetEnterCompartmentDepth(cx) : 0;
michael@0 120 #endif
michael@0 121
michael@0 122 // Enter a request and a compartment for the duration that the cx is on the
michael@0 123 // stack if non-null.
michael@0 124 if (cx) {
michael@0 125 mAutoRequest.construct(cx);
michael@0 126
michael@0 127 // DOM JSContexts don't store their default compartment object on the cx.
michael@0 128 JSObject *compartmentObject = mScx ? mScx->GetWindowProxy()
michael@0 129 : js::DefaultObjectForContextOrNull(cx);
michael@0 130 if (compartmentObject)
michael@0 131 mAutoCompartment.construct(cx, compartmentObject);
michael@0 132 }
michael@0 133 }
michael@0 134
michael@0 135 AutoCxPusher::~AutoCxPusher()
michael@0 136 {
michael@0 137 // GC when we pop a script entry point. This is a useful heuristic that helps
michael@0 138 // us out on certain (flawed) benchmarks like sunspider, because it lets us
michael@0 139 // avoid GCing during the timing loop.
michael@0 140 //
michael@0 141 // NB: We need to take care to only do this if we're in a compartment,
michael@0 142 // otherwise JS_MaybeGC will segfault.
michael@0 143 if (mScx && !mAutoCompartment.empty())
michael@0 144 JS_MaybeGC(nsXPConnect::XPConnect()->GetCurrentJSContext());
michael@0 145
michael@0 146 // Leave the compartment and request before popping.
michael@0 147 mAutoCompartment.destroyIfConstructed();
michael@0 148 mAutoRequest.destroyIfConstructed();
michael@0 149
michael@0 150 // When we push a context, we may save the frame chain and pretend like we
michael@0 151 // haven't entered any compartment. This gets restored on Pop(), but we can
michael@0 152 // run into trouble if a Push/Pop are interleaved with a
michael@0 153 // JSAutoEnterCompartment. Make sure the compartment depth right before we
michael@0 154 // pop is the same as it was right after we pushed.
michael@0 155 MOZ_ASSERT_IF(mPushedContext, mCompartmentDepthOnEntry ==
michael@0 156 js::GetEnterCompartmentDepth(mPushedContext));
michael@0 157 DebugOnly<JSContext*> stackTop;
michael@0 158 MOZ_ASSERT(mPushedContext == nsXPConnect::XPConnect()->GetCurrentJSContext());
michael@0 159 XPCJSRuntime::Get()->GetJSContextStack()->Pop();
michael@0 160 mScx = nullptr;
michael@0 161 }
michael@0 162
michael@0 163 bool
michael@0 164 AutoCxPusher::IsStackTop()
michael@0 165 {
michael@0 166 uint32_t currentDepth = XPCJSRuntime::Get()->GetJSContextStack()->Count();
michael@0 167 MOZ_ASSERT(currentDepth >= mStackDepthAfterPush);
michael@0 168 return currentDepth == mStackDepthAfterPush;
michael@0 169 }
michael@0 170
michael@0 171 AutoJSContext::AutoJSContext(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL)
michael@0 172 : mCx(nullptr)
michael@0 173 {
michael@0 174 Init(false MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT);
michael@0 175 }
michael@0 176
michael@0 177 AutoJSContext::AutoJSContext(bool aSafe MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)
michael@0 178 : mCx(nullptr)
michael@0 179 {
michael@0 180 Init(aSafe MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT);
michael@0 181 }
michael@0 182
michael@0 183 void
michael@0 184 AutoJSContext::Init(bool aSafe MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)
michael@0 185 {
michael@0 186 JS::AutoAssertNoGC nogc;
michael@0 187 MOZ_ASSERT(!mCx, "mCx should not be initialized!");
michael@0 188
michael@0 189 MOZ_GUARD_OBJECT_NOTIFIER_INIT;
michael@0 190
michael@0 191 nsXPConnect *xpc = nsXPConnect::XPConnect();
michael@0 192 if (!aSafe) {
michael@0 193 mCx = xpc->GetCurrentJSContext();
michael@0 194 }
michael@0 195
michael@0 196 if (!mCx) {
michael@0 197 mCx = xpc->GetSafeJSContext();
michael@0 198 mPusher.construct(mCx);
michael@0 199 }
michael@0 200 }
michael@0 201
michael@0 202 AutoJSContext::operator JSContext*() const
michael@0 203 {
michael@0 204 return mCx;
michael@0 205 }
michael@0 206
michael@0 207 ThreadsafeAutoJSContext::ThreadsafeAutoJSContext(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL)
michael@0 208 {
michael@0 209 MOZ_GUARD_OBJECT_NOTIFIER_INIT;
michael@0 210
michael@0 211 if (NS_IsMainThread()) {
michael@0 212 mCx = nullptr;
michael@0 213 mAutoJSContext.construct();
michael@0 214 } else {
michael@0 215 mCx = mozilla::dom::workers::GetCurrentThreadJSContext();
michael@0 216 mRequest.construct(mCx);
michael@0 217 }
michael@0 218 }
michael@0 219
michael@0 220 ThreadsafeAutoJSContext::operator JSContext*() const
michael@0 221 {
michael@0 222 if (mCx) {
michael@0 223 return mCx;
michael@0 224 } else {
michael@0 225 return mAutoJSContext.ref();
michael@0 226 }
michael@0 227 }
michael@0 228
michael@0 229 AutoSafeJSContext::AutoSafeJSContext(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL)
michael@0 230 : AutoJSContext(true MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT)
michael@0 231 , mAc(mCx, XPCJSRuntime::Get()->GetJSContextStack()->GetSafeJSContextGlobal())
michael@0 232 {
michael@0 233 }
michael@0 234
michael@0 235 ThreadsafeAutoSafeJSContext::ThreadsafeAutoSafeJSContext(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL)
michael@0 236 {
michael@0 237 MOZ_GUARD_OBJECT_NOTIFIER_INIT;
michael@0 238
michael@0 239 if (NS_IsMainThread()) {
michael@0 240 mCx = nullptr;
michael@0 241 mAutoSafeJSContext.construct();
michael@0 242 } else {
michael@0 243 mCx = mozilla::dom::workers::GetCurrentThreadJSContext();
michael@0 244 mRequest.construct(mCx);
michael@0 245 }
michael@0 246 }
michael@0 247
michael@0 248 ThreadsafeAutoSafeJSContext::operator JSContext*() const
michael@0 249 {
michael@0 250 if (mCx) {
michael@0 251 return mCx;
michael@0 252 } else {
michael@0 253 return mAutoSafeJSContext.ref();
michael@0 254 }
michael@0 255 }
michael@0 256
michael@0 257 AutoPushJSContext::AutoPushJSContext(JSContext *aCx) : mCx(aCx)
michael@0 258 {
michael@0 259 if (mCx && mCx != nsXPConnect::XPConnect()->GetCurrentJSContext()) {
michael@0 260 mPusher.construct(mCx);
michael@0 261 }
michael@0 262 }
michael@0 263
michael@0 264 } // namespace mozilla

mercurial