michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsCxPusher.h" michael@0: michael@0: #include "nsIScriptContext.h" michael@0: #include "mozilla/dom/EventTarget.h" michael@0: #include "nsDOMJSUtils.h" michael@0: #include "xpcprivate.h" michael@0: #include "WorkerPrivate.h" michael@0: michael@0: using mozilla::dom::EventTarget; michael@0: using mozilla::DebugOnly; michael@0: michael@0: bool michael@0: nsCxPusher::Push(EventTarget *aCurrentTarget) michael@0: { michael@0: MOZ_ASSERT(mPusher.empty()); michael@0: NS_ENSURE_TRUE(aCurrentTarget, false); michael@0: nsresult rv; michael@0: nsIScriptContext* scx = michael@0: aCurrentTarget->GetContextForEventHandlers(&rv); michael@0: #ifdef DEBUG_smaug michael@0: NS_ENSURE_SUCCESS(rv, false); michael@0: #else michael@0: if(NS_FAILED(rv)) { michael@0: return false; michael@0: } michael@0: #endif michael@0: michael@0: if (!scx) { michael@0: // The target may have a special JS context for event handlers. michael@0: JSContext* cx = aCurrentTarget->GetJSContextForEventHandlers(); michael@0: if (cx) { michael@0: mPusher.construct(cx); michael@0: } michael@0: michael@0: // Nothing to do here, I guess. Have to return true so that event firing michael@0: // will still work correctly even if there is no associated JSContext michael@0: return true; michael@0: } michael@0: michael@0: mPusher.construct(scx->GetNativeContext()); michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: nsCxPusher::RePush(EventTarget *aCurrentTarget) michael@0: { michael@0: if (mPusher.empty()) { michael@0: return Push(aCurrentTarget); michael@0: } michael@0: michael@0: if (aCurrentTarget) { michael@0: nsresult rv; michael@0: nsIScriptContext* scx = michael@0: aCurrentTarget->GetContextForEventHandlers(&rv); michael@0: if (NS_FAILED(rv)) { michael@0: mPusher.destroy(); michael@0: return false; michael@0: } michael@0: michael@0: // If we have the same script context and native context is still michael@0: // alive, no need to Pop/Push. michael@0: if (scx && scx == mPusher.ref().GetScriptContext() && michael@0: scx->GetNativeContext()) { michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: mPusher.destroy(); michael@0: return Push(aCurrentTarget); michael@0: } michael@0: michael@0: void michael@0: nsCxPusher::Push(JSContext *cx) michael@0: { michael@0: mPusher.construct(cx); michael@0: } michael@0: michael@0: void michael@0: nsCxPusher::PushNull() michael@0: { michael@0: // Note: The Maybe<> template magic seems to need the static_cast below to michael@0: // work right on some older compilers. michael@0: mPusher.construct(static_cast(nullptr), /* aAllowNull = */ true); michael@0: } michael@0: michael@0: void michael@0: nsCxPusher::Pop() michael@0: { michael@0: if (!mPusher.empty()) michael@0: mPusher.destroy(); michael@0: } michael@0: michael@0: namespace mozilla { michael@0: michael@0: AutoCxPusher::AutoCxPusher(JSContext* cx, bool allowNull) michael@0: { michael@0: MOZ_ASSERT_IF(!allowNull, cx); michael@0: michael@0: // Hold a strong ref to the nsIScriptContext, if any. This ensures that we michael@0: // only destroy the mContext of an nsJSContext when it is not on the cx stack michael@0: // (and therefore not in use). See nsJSContext::DestroyJSContext(). michael@0: if (cx) michael@0: mScx = GetScriptContextFromJSContext(cx); michael@0: michael@0: XPCJSContextStack *stack = XPCJSRuntime::Get()->GetJSContextStack(); michael@0: if (!stack->Push(cx)) { michael@0: MOZ_CRASH(); michael@0: } michael@0: mStackDepthAfterPush = stack->Count(); michael@0: michael@0: #ifdef DEBUG michael@0: mPushedContext = cx; michael@0: mCompartmentDepthOnEntry = cx ? js::GetEnterCompartmentDepth(cx) : 0; michael@0: #endif michael@0: michael@0: // Enter a request and a compartment for the duration that the cx is on the michael@0: // stack if non-null. michael@0: if (cx) { michael@0: mAutoRequest.construct(cx); michael@0: michael@0: // DOM JSContexts don't store their default compartment object on the cx. michael@0: JSObject *compartmentObject = mScx ? mScx->GetWindowProxy() michael@0: : js::DefaultObjectForContextOrNull(cx); michael@0: if (compartmentObject) michael@0: mAutoCompartment.construct(cx, compartmentObject); michael@0: } michael@0: } michael@0: michael@0: AutoCxPusher::~AutoCxPusher() michael@0: { michael@0: // GC when we pop a script entry point. This is a useful heuristic that helps michael@0: // us out on certain (flawed) benchmarks like sunspider, because it lets us michael@0: // avoid GCing during the timing loop. michael@0: // michael@0: // NB: We need to take care to only do this if we're in a compartment, michael@0: // otherwise JS_MaybeGC will segfault. michael@0: if (mScx && !mAutoCompartment.empty()) michael@0: JS_MaybeGC(nsXPConnect::XPConnect()->GetCurrentJSContext()); michael@0: michael@0: // Leave the compartment and request before popping. michael@0: mAutoCompartment.destroyIfConstructed(); michael@0: mAutoRequest.destroyIfConstructed(); michael@0: michael@0: // When we push a context, we may save the frame chain and pretend like we michael@0: // haven't entered any compartment. This gets restored on Pop(), but we can michael@0: // run into trouble if a Push/Pop are interleaved with a michael@0: // JSAutoEnterCompartment. Make sure the compartment depth right before we michael@0: // pop is the same as it was right after we pushed. michael@0: MOZ_ASSERT_IF(mPushedContext, mCompartmentDepthOnEntry == michael@0: js::GetEnterCompartmentDepth(mPushedContext)); michael@0: DebugOnly stackTop; michael@0: MOZ_ASSERT(mPushedContext == nsXPConnect::XPConnect()->GetCurrentJSContext()); michael@0: XPCJSRuntime::Get()->GetJSContextStack()->Pop(); michael@0: mScx = nullptr; michael@0: } michael@0: michael@0: bool michael@0: AutoCxPusher::IsStackTop() michael@0: { michael@0: uint32_t currentDepth = XPCJSRuntime::Get()->GetJSContextStack()->Count(); michael@0: MOZ_ASSERT(currentDepth >= mStackDepthAfterPush); michael@0: return currentDepth == mStackDepthAfterPush; michael@0: } michael@0: michael@0: AutoJSContext::AutoJSContext(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL) michael@0: : mCx(nullptr) michael@0: { michael@0: Init(false MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT); michael@0: } michael@0: michael@0: AutoJSContext::AutoJSContext(bool aSafe MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL) michael@0: : mCx(nullptr) michael@0: { michael@0: Init(aSafe MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT); michael@0: } michael@0: michael@0: void michael@0: AutoJSContext::Init(bool aSafe MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL) michael@0: { michael@0: JS::AutoAssertNoGC nogc; michael@0: MOZ_ASSERT(!mCx, "mCx should not be initialized!"); michael@0: michael@0: MOZ_GUARD_OBJECT_NOTIFIER_INIT; michael@0: michael@0: nsXPConnect *xpc = nsXPConnect::XPConnect(); michael@0: if (!aSafe) { michael@0: mCx = xpc->GetCurrentJSContext(); michael@0: } michael@0: michael@0: if (!mCx) { michael@0: mCx = xpc->GetSafeJSContext(); michael@0: mPusher.construct(mCx); michael@0: } michael@0: } michael@0: michael@0: AutoJSContext::operator JSContext*() const michael@0: { michael@0: return mCx; michael@0: } michael@0: michael@0: ThreadsafeAutoJSContext::ThreadsafeAutoJSContext(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL) michael@0: { michael@0: MOZ_GUARD_OBJECT_NOTIFIER_INIT; michael@0: michael@0: if (NS_IsMainThread()) { michael@0: mCx = nullptr; michael@0: mAutoJSContext.construct(); michael@0: } else { michael@0: mCx = mozilla::dom::workers::GetCurrentThreadJSContext(); michael@0: mRequest.construct(mCx); michael@0: } michael@0: } michael@0: michael@0: ThreadsafeAutoJSContext::operator JSContext*() const michael@0: { michael@0: if (mCx) { michael@0: return mCx; michael@0: } else { michael@0: return mAutoJSContext.ref(); michael@0: } michael@0: } michael@0: michael@0: AutoSafeJSContext::AutoSafeJSContext(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL) michael@0: : AutoJSContext(true MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT) michael@0: , mAc(mCx, XPCJSRuntime::Get()->GetJSContextStack()->GetSafeJSContextGlobal()) michael@0: { michael@0: } michael@0: michael@0: ThreadsafeAutoSafeJSContext::ThreadsafeAutoSafeJSContext(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL) michael@0: { michael@0: MOZ_GUARD_OBJECT_NOTIFIER_INIT; michael@0: michael@0: if (NS_IsMainThread()) { michael@0: mCx = nullptr; michael@0: mAutoSafeJSContext.construct(); michael@0: } else { michael@0: mCx = mozilla::dom::workers::GetCurrentThreadJSContext(); michael@0: mRequest.construct(mCx); michael@0: } michael@0: } michael@0: michael@0: ThreadsafeAutoSafeJSContext::operator JSContext*() const michael@0: { michael@0: if (mCx) { michael@0: return mCx; michael@0: } else { michael@0: return mAutoSafeJSContext.ref(); michael@0: } michael@0: } michael@0: michael@0: AutoPushJSContext::AutoPushJSContext(JSContext *aCx) : mCx(aCx) michael@0: { michael@0: if (mCx && mCx != nsXPConnect::XPConnect()->GetCurrentJSContext()) { michael@0: mPusher.construct(mCx); michael@0: } michael@0: } michael@0: michael@0: } // namespace mozilla