2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-11-09 19:43:57 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-01-28 17:34:29 +04:00
|
|
|
#include "mozilla/dom/CallbackObject.h"
|
2017-10-05 18:34:12 +03:00
|
|
|
#include "mozilla/CycleCollectedJSContext.h"
|
2013-09-03 16:01:53 +04:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2012-11-09 19:43:57 +04:00
|
|
|
#include "jsfriendapi.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsJSUtils.h"
|
2013-02-27 00:10:15 +04:00
|
|
|
#include "xpcprivate.h"
|
2013-09-10 01:58:29 +04:00
|
|
|
#include "WorkerPrivate.h"
|
2018-03-20 22:18:20 +03:00
|
|
|
#include "nsContentUtils.h"
|
2013-11-20 20:48:00 +04:00
|
|
|
#include "nsGlobalWindow.h"
|
2013-12-12 05:51:57 +04:00
|
|
|
#include "WorkerScope.h"
|
2015-01-16 01:39:01 +03:00
|
|
|
#include "jsapi.h"
|
2019-01-24 03:56:56 +03:00
|
|
|
#include "js/ContextOptions.h"
|
2015-01-16 01:39:01 +03:00
|
|
|
#include "nsJSPrincipals.h"
|
2012-11-09 19:43:57 +04:00
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom {
|
2012-11-09 19:43:57 +04:00
|
|
|
|
2013-01-28 17:34:29 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CallbackObject)
|
2013-04-17 01:14:30 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(mozilla::dom::CallbackObject)
|
2012-11-09 19:43:57 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-01-28 17:34:29 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(CallbackObject)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(CallbackObject)
|
2012-11-09 19:43:57 +04:00
|
|
|
|
2022-09-07 14:22:51 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(CallbackObject)
|
2013-08-02 05:29:05 +04:00
|
|
|
|
2013-01-28 17:34:29 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CallbackObject)
|
2017-06-17 00:15:32 +03:00
|
|
|
tmp->ClearJSReferences();
|
2013-12-12 05:51:58 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mIncumbentGlobal)
|
2012-11-09 19:43:57 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2016-11-15 06:49:28 +03:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(CallbackObject)
|
|
|
|
JSObject* callback = tmp->CallbackPreserveColor();
|
|
|
|
|
|
|
|
if (!aRemovingAllowed) {
|
|
|
|
// If our callback has been cleared, we can't be part of a garbage cycle.
|
|
|
|
return !callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
// mCallback is always wrapped for the CallbackObject's incumbent global. In
|
|
|
|
// the case where the real callback is in a different compartment, we have a
|
|
|
|
// cross-compartment wrapper, and it will automatically be cut when its
|
|
|
|
// compartment is nuked. In the case where it is in the same compartment, we
|
|
|
|
// have a reference to the real function. Since that means there are no
|
|
|
|
// wrappers to cut, we need to check whether the compartment is still alive,
|
|
|
|
// and drop the references if it is not.
|
|
|
|
|
|
|
|
if (MOZ_UNLIKELY(!callback)) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-12 11:02:30 +03:00
|
|
|
if (MOZ_LIKELY(tmp->mIncumbentGlobal) &&
|
|
|
|
MOZ_UNLIKELY(js::NukedObjectRealm(tmp->CallbackGlobalPreserveColor()))) {
|
2016-11-15 06:49:28 +03:00
|
|
|
// It's not safe to release our global reference or drop our JS objects at
|
|
|
|
// this point, so defer their finalization until CC is finished.
|
|
|
|
AddForDeferredFinalization(new JSObjectsDropper(tmp));
|
|
|
|
DeferredFinalize(tmp->mIncumbentGlobal.forget().take());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(CallbackObject)
|
|
|
|
return !tmp->mCallback;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(CallbackObject)
|
|
|
|
return !tmp->mCallback;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
|
|
|
|
|
2013-01-28 17:34:29 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(CallbackObject)
|
2013-12-12 05:51:58 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mIncumbentGlobal)
|
2019-07-09 20:04:59 +03:00
|
|
|
// If a new member is added here, don't forget to update IsBlackForCC.
|
2012-11-09 19:43:57 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2013-01-28 17:34:29 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(CallbackObject)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mCallback)
|
2018-08-04 00:11:39 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mCallbackGlobal)
|
2015-07-24 14:01:00 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mCreationStack)
|
2014-03-03 20:53:42 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mIncumbentJSGlobal)
|
2019-07-09 20:04:59 +03:00
|
|
|
// If a new member is added here, don't forget to update IsBlackForCC.
|
2012-11-09 19:43:57 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2016-05-18 19:23:35 +03:00
|
|
|
void CallbackObject::Trace(JSTracer* aTracer) {
|
|
|
|
JS::TraceEdge(aTracer, &mCallback, "CallbackObject.mCallback");
|
2018-08-04 00:11:39 +03:00
|
|
|
JS::TraceEdge(aTracer, &mCallbackGlobal, "CallbackObject.mCallbackGlobal");
|
2016-05-18 19:23:35 +03:00
|
|
|
JS::TraceEdge(aTracer, &mCreationStack, "CallbackObject.mCreationStack");
|
|
|
|
JS::TraceEdge(aTracer, &mIncumbentJSGlobal,
|
|
|
|
"CallbackObject.mIncumbentJSGlobal");
|
|
|
|
}
|
|
|
|
|
2016-12-30 00:19:26 +03:00
|
|
|
void CallbackObject::FinishSlowJSInitIfMoreThanOneOwner(JSContext* aCx) {
|
2016-05-18 19:23:35 +03:00
|
|
|
MOZ_ASSERT(mRefCnt.get() > 0);
|
|
|
|
if (mRefCnt.get() > 1) {
|
|
|
|
mozilla::HoldJSObjects(this);
|
Bug 1601179 - Enable async stacks but limit captured async stacks to debuggees. r=jorendorff,smaug
The 'asyncStack' flag on JS execution contexts is used as a general switch
to enable async stack capture across all locations in SpiderMonkey, but
this causes problems because it can at times be too much of a performance
burden to general and track all of these stacks.
Since the introduction of this option, we have only enabled it on Nightly
and DevEdition for non-mobile builds, which has left a lot of users unable
to take advantage of this data while debugging.
This patch enables async stack traces across all of Firefox, but introduces
a new pref to toggle the scope of the actual expensive part of async stacks,
which is _capturing_ them and keeping them alive in memory. The new pref
limits the capturing of async stack traces to only debuggees, unless an
explicit pref is flipped to capture async traces for all cases.
This means that while async stacks are technically enabled, and code could
manually capture a stack and pass it back to SpiderMonkey and see that stack
reflected in later captured stacks, SpiderMonkey itself and related async
DOM APIs, among others, will not capture stacks or pass them to SpiderMonkey,
so there should be no general change in performance by enabling the broader
feature itself, unless the user is actively debugging the page.
One effect of this patch is that if you have the debugger open and then close
it, objects that have async stacks associated with them will retain those
stacks and they will continue to show up in stack traces, no _new_ stacks
will be captured. jorendorff and I have decided that this is okay because
the expectation that the debugger fully revert every possible effect that it
could have on a page is a nice goal but not a strict requirement.
Differential Revision: https://phabricator.services.mozilla.com/D68503
2020-06-14 05:41:45 +03:00
|
|
|
if (JS::IsAsyncStackCaptureEnabledForRealm(aCx)) {
|
2022-05-18 11:43:09 +03:00
|
|
|
JS::Rooted<JSObject*> stack(aCx);
|
2016-12-30 00:19:26 +03:00
|
|
|
if (!JS::CaptureCurrentStack(aCx, &stack)) {
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
}
|
|
|
|
mCreationStack = stack;
|
|
|
|
}
|
2016-12-30 00:19:26 +03:00
|
|
|
mIncumbentGlobal = GetIncumbentGlobal();
|
|
|
|
if (mIncumbentGlobal) {
|
2019-05-03 18:15:31 +03:00
|
|
|
// We don't want to expose to JS here (change the color). If someone ever
|
|
|
|
// reads mIncumbentJSGlobal, that will expose. If not, no need to expose
|
|
|
|
// here.
|
|
|
|
mIncumbentJSGlobal = mIncumbentGlobal->GetGlobalJSObjectPreserveColor();
|
2016-12-30 00:19:26 +03:00
|
|
|
}
|
2016-05-18 19:23:35 +03:00
|
|
|
} else {
|
|
|
|
// We can just forget all our stuff.
|
|
|
|
ClearJSReferences();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-05 03:21:06 +03:00
|
|
|
JSObject* CallbackObject::Callback(JSContext* aCx) {
|
|
|
|
JSObject* callback = CallbackOrNull();
|
|
|
|
if (!callback) {
|
|
|
|
callback = JS_NewDeadWrapper(aCx);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(callback);
|
|
|
|
return callback;
|
|
|
|
}
|
|
|
|
|
2019-07-10 23:48:14 +03:00
|
|
|
void CallbackObject::GetDescription(nsACString& aOutString) {
|
|
|
|
JSObject* wrappedCallback = CallbackOrNull();
|
|
|
|
if (!wrappedCallback) {
|
|
|
|
aOutString.Append("<callback from a nuked compartment>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> unwrappedCallback(
|
|
|
|
RootingCx(), js::CheckedUnwrapStatic(wrappedCallback));
|
|
|
|
if (!unwrappedCallback) {
|
|
|
|
aOutString.Append("<not a function>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
|
2022-05-18 11:43:09 +03:00
|
|
|
JS::Rooted<JSObject*> rootedCallback(cx, unwrappedCallback);
|
2019-07-10 23:48:14 +03:00
|
|
|
JSAutoRealm ar(cx, rootedCallback);
|
|
|
|
|
|
|
|
JS::Rooted<JSFunction*> rootedFunction(cx,
|
|
|
|
JS_GetObjectFunction(rootedCallback));
|
|
|
|
if (!rootedFunction) {
|
|
|
|
aOutString.Append("<not a function>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSString*> displayId(cx, JS_GetFunctionDisplayId(rootedFunction));
|
|
|
|
if (displayId) {
|
|
|
|
nsAutoJSString funcNameStr;
|
|
|
|
if (funcNameStr.init(cx, displayId)) {
|
|
|
|
if (funcNameStr.IsEmpty()) {
|
|
|
|
aOutString.Append("<empty name>");
|
|
|
|
} else {
|
|
|
|
AppendUTF16toUTF8(funcNameStr, aOutString);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
aOutString.Append("<function name string failed to materialize>");
|
|
|
|
jsapi.ClearException();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
aOutString.Append("<anonymous>");
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSScript*> rootedScript(cx,
|
|
|
|
JS_GetFunctionScript(cx, rootedFunction));
|
|
|
|
if (!rootedScript) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aOutString.Append(" (");
|
|
|
|
aOutString.Append(JS_GetScriptFilename(rootedScript));
|
|
|
|
aOutString.Append(":");
|
|
|
|
aOutString.AppendInt(JS_GetScriptBaseLineNumber(cx, rootedScript));
|
|
|
|
aOutString.Append(")");
|
|
|
|
}
|
|
|
|
|
2013-12-12 05:51:57 +04:00
|
|
|
CallbackObject::CallSetup::CallSetup(CallbackObject* aCallback,
|
2013-02-19 20:54:40 +04:00
|
|
|
ErrorResult& aRv,
|
2015-04-09 04:23:48 +03:00
|
|
|
const char* aExecutionReason,
|
2013-09-03 16:01:53 +04:00
|
|
|
ExceptionHandling aExceptionHandling,
|
2018-05-31 12:28:48 +03:00
|
|
|
JS::Realm* aRealm,
|
2014-02-15 10:36:44 +04:00
|
|
|
bool aIsJSImplementedWebIDL)
|
2012-11-09 19:43:57 +04:00
|
|
|
: mCx(nullptr),
|
2018-05-31 12:28:48 +03:00
|
|
|
mRealm(aRealm),
|
2013-02-19 20:54:40 +04:00
|
|
|
mErrorResult(aRv),
|
|
|
|
mExceptionHandling(aExceptionHandling),
|
2013-09-10 01:58:29 +04:00
|
|
|
mIsMainThread(NS_IsMainThread()) {
|
2020-07-24 11:08:53 +03:00
|
|
|
MOZ_ASSERT_IF(aExceptionHandling == eReportExceptions ||
|
|
|
|
aExceptionHandling == eRethrowExceptions,
|
|
|
|
!aRealm);
|
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
CycleCollectedJSContext* ccjs = CycleCollectedJSContext::Get();
|
|
|
|
if (ccjs) {
|
|
|
|
ccjs->EnterMicroTask();
|
2015-05-29 19:11:09 +03:00
|
|
|
}
|
|
|
|
|
2014-02-15 10:36:44 +04:00
|
|
|
// Compute the caller's subject principal (if necessary) early, before we
|
|
|
|
// do anything that might perturb the relevant state.
|
|
|
|
nsIPrincipal* webIDLCallerPrincipal = nullptr;
|
|
|
|
if (aIsJSImplementedWebIDL) {
|
2014-09-25 13:04:45 +04:00
|
|
|
webIDLCallerPrincipal =
|
|
|
|
nsContentUtils::SubjectPrincipalOrSystemIfNativeCaller();
|
2014-02-15 10:36:44 +04:00
|
|
|
}
|
|
|
|
|
2016-11-15 08:25:37 +03:00
|
|
|
JSObject* wrappedCallback = aCallback->CallbackPreserveColor();
|
|
|
|
if (!wrappedCallback) {
|
2020-02-03 23:19:11 +03:00
|
|
|
aRv.ThrowNotSupportedError(
|
|
|
|
"Cannot execute callback from a nuked compartment.");
|
2016-11-15 08:25:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-12 05:51:57 +04:00
|
|
|
nsIGlobalObject* globalObject = nullptr;
|
2013-09-10 01:58:29 +04:00
|
|
|
|
2017-10-09 20:59:02 +03:00
|
|
|
{
|
|
|
|
// First, find the real underlying callback.
|
2020-03-28 01:40:58 +03:00
|
|
|
JS::Rooted<JSObject*> realCallback(ccjs->RootingCx(),
|
|
|
|
js::UncheckedUnwrap(wrappedCallback));
|
2020-03-27 22:47:15 +03:00
|
|
|
|
2020-03-28 01:40:58 +03:00
|
|
|
// Get the global for this callback. Note that for the case of
|
2017-10-09 20:59:02 +03:00
|
|
|
// JS-implemented WebIDL we never have a window here.
|
2017-11-04 01:25:38 +03:00
|
|
|
nsGlobalWindowInner* win = mIsMainThread && !aIsJSImplementedWebIDL
|
2017-10-09 20:59:02 +03:00
|
|
|
? xpc::WindowGlobalOrNull(realCallback)
|
|
|
|
: nullptr;
|
|
|
|
if (win) {
|
|
|
|
// We don't want to run script in windows that have been navigated away
|
|
|
|
// from.
|
2019-04-11 17:12:43 +03:00
|
|
|
if (!win->HasActiveDocument()) {
|
2020-02-03 23:19:11 +03:00
|
|
|
aRv.ThrowNotSupportedError(
|
2019-09-20 05:17:01 +03:00
|
|
|
"Refusing to execute function from window whose document is no "
|
|
|
|
"longer active.");
|
2017-10-09 20:59:02 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
globalObject = win;
|
|
|
|
} else {
|
|
|
|
// No DOM Window. Store the global.
|
2018-08-01 12:25:50 +03:00
|
|
|
globalObject = xpc::NativeGlobal(realCallback);
|
2017-10-09 20:59:02 +03:00
|
|
|
MOZ_ASSERT(globalObject);
|
2013-12-31 20:30:57 +04:00
|
|
|
}
|
2020-03-28 01:40:58 +03:00
|
|
|
|
|
|
|
// Make sure to use realCallback to get the global of the callback
|
|
|
|
// object, not the wrapper.
|
|
|
|
if (globalObject->IsScriptForbidden(realCallback, aIsJSImplementedWebIDL)) {
|
|
|
|
aRv.ThrowNotSupportedError(
|
|
|
|
"Refusing to execute function from global in which script is "
|
|
|
|
"disabled.");
|
|
|
|
return;
|
|
|
|
}
|
2017-09-09 11:11:25 +03:00
|
|
|
}
|
2013-12-07 00:01:41 +04:00
|
|
|
|
2018-08-11 00:08:07 +03:00
|
|
|
// Bail out if there's no useful global.
|
2019-05-03 00:32:17 +03:00
|
|
|
if (!globalObject->HasJSGlobal()) {
|
2020-02-03 23:19:11 +03:00
|
|
|
aRv.ThrowNotSupportedError(
|
2019-09-20 05:17:01 +03:00
|
|
|
"Refusing to execute function from global which is being torn down.");
|
2017-09-09 11:11:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-28 09:29:44 +03:00
|
|
|
AutoAllowLegacyScriptExecution exemption;
|
2017-09-09 11:11:25 +03:00
|
|
|
mAutoEntryScript.emplace(globalObject, aExecutionReason, mIsMainThread);
|
|
|
|
mAutoEntryScript->SetWebIDLCallerPrincipal(webIDLCallerPrincipal);
|
|
|
|
nsIGlobalObject* incumbent = aCallback->IncumbentGlobalOrNull();
|
|
|
|
if (incumbent) {
|
|
|
|
// The callback object traces its incumbent JS global, so in general it
|
|
|
|
// should be alive here. However, it's possible that we could run afoul
|
|
|
|
// of the same IPC global weirdness described above, wherein the
|
|
|
|
// nsIGlobalObject has severed its reference to the JS global. Let's just
|
|
|
|
// be safe here, so that nobody has to waste a day debugging gaia-ui tests.
|
2019-05-03 00:32:17 +03:00
|
|
|
if (!incumbent->HasJSGlobal()) {
|
2020-02-03 23:19:11 +03:00
|
|
|
aRv.ThrowNotSupportedError(
|
2019-09-20 05:17:01 +03:00
|
|
|
"Refusing to execute function because our incumbent global is being "
|
|
|
|
"torn down.");
|
2017-09-09 11:11:25 +03:00
|
|
|
return;
|
2013-12-31 20:30:57 +04:00
|
|
|
}
|
2017-09-09 11:11:25 +03:00
|
|
|
mAutoIncumbentScript.emplace(incumbent);
|
|
|
|
}
|
2013-12-12 05:51:57 +04:00
|
|
|
|
2017-09-09 11:11:25 +03:00
|
|
|
JSContext* cx = mAutoEntryScript->cx();
|
2016-04-07 10:11:52 +03:00
|
|
|
|
2017-09-09 11:11:25 +03:00
|
|
|
// Unmark the callable (by invoking CallbackOrNull() and not the
|
|
|
|
// CallbackPreserveColor() variant), and stick it in a Rooted before it can
|
|
|
|
// go gray again.
|
|
|
|
// Nothing before us in this function can trigger a CC, so it's safe to wait
|
|
|
|
// until here it do the unmark. This allows us to construct mRootedCallable
|
|
|
|
// with the cx from mAutoEntryScript, avoiding the cost of finding another
|
|
|
|
// JSContext. (Rooted<> does not care about requests or compartments.)
|
|
|
|
mRootedCallable.emplace(cx, aCallback->CallbackOrNull());
|
2018-08-04 00:11:39 +03:00
|
|
|
mRootedCallableGlobal.emplace(cx, aCallback->CallbackGlobalOrNull());
|
2013-06-20 22:05:34 +04:00
|
|
|
|
2015-07-24 14:01:00 +03:00
|
|
|
mAsyncStack.emplace(cx, aCallback->GetCreationStack());
|
|
|
|
if (*mAsyncStack) {
|
2016-03-23 17:40:53 +03:00
|
|
|
mAsyncStackSetter.emplace(cx, *mAsyncStack, aExecutionReason);
|
2015-07-24 14:01:00 +03:00
|
|
|
}
|
|
|
|
|
2018-05-16 11:53:16 +03:00
|
|
|
// Enter the realm of our callback, so we can actually work with it.
|
2013-12-12 05:51:57 +04:00
|
|
|
//
|
|
|
|
// Note that if the callback is a wrapper, this will not be the same
|
2018-05-16 11:53:16 +03:00
|
|
|
// realm that we ended up in with mAutoEntryScript above, because the
|
2013-12-12 05:51:57 +04:00
|
|
|
// entry point is based off of the unwrapped callback (realCallback).
|
2018-08-04 00:11:39 +03:00
|
|
|
mAr.emplace(cx, *mRootedCallableGlobal);
|
2012-11-09 19:43:57 +04:00
|
|
|
|
|
|
|
// And now we're ready to go.
|
|
|
|
mCx = cx;
|
2020-03-07 02:05:16 +03:00
|
|
|
|
|
|
|
// We don't really have a good error message prefix to use for the
|
|
|
|
// BindingCallContext.
|
|
|
|
mCallContext.emplace(cx, nullptr);
|
2012-11-09 19:43:57 +04:00
|
|
|
}
|
|
|
|
|
2013-09-03 16:01:53 +04:00
|
|
|
bool CallbackObject::CallSetup::ShouldRethrowException(
|
|
|
|
JS::Handle<JS::Value> aException) {
|
|
|
|
if (mExceptionHandling == eRethrowExceptions) {
|
2020-07-24 11:08:53 +03:00
|
|
|
MOZ_ASSERT(!mRealm);
|
|
|
|
return true;
|
2013-09-03 16:01:53 +04:00
|
|
|
}
|
|
|
|
|
2018-05-31 12:28:48 +03:00
|
|
|
MOZ_ASSERT(mRealm);
|
2013-09-03 16:01:53 +04:00
|
|
|
|
2015-01-16 01:39:01 +03:00
|
|
|
// Now we only want to throw an exception to the caller if the object that was
|
2018-05-31 12:28:48 +03:00
|
|
|
// thrown is in the caller realm (which we stored in mRealm).
|
2013-09-03 16:01:53 +04:00
|
|
|
|
|
|
|
if (!aException.isObject()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(mCx, &aException.toObject());
|
2015-11-06 21:03:52 +03:00
|
|
|
obj = js::UncheckedUnwrap(obj, /* stopAtWindowProxy = */ false);
|
2018-05-31 12:28:48 +03:00
|
|
|
return js::GetNonCCWObjectRealm(obj) == mRealm;
|
2013-09-03 16:01:53 +04:00
|
|
|
}
|
|
|
|
|
2013-01-28 17:34:29 +04:00
|
|
|
CallbackObject::CallSetup::~CallSetup() {
|
2018-05-16 11:53:16 +03:00
|
|
|
// To get our nesting right we have to destroy our JSAutoRealm first.
|
2014-03-05 17:32:27 +04:00
|
|
|
// In particular, we want to do this before we try reporting any exceptions,
|
2018-05-16 11:53:16 +03:00
|
|
|
// so we end up reporting them while in the realm of our entry point,
|
2014-03-05 17:32:27 +04:00
|
|
|
// not whatever cross-compartment wrappper mCallback might be.
|
2018-05-16 11:53:16 +03:00
|
|
|
// Be careful: the JSAutoRealm might not have been constructed at all!
|
|
|
|
mAr.reset();
|
2014-03-05 17:32:27 +04:00
|
|
|
|
|
|
|
// Now, if we have a JSContext, report any pending errors on it, unless we
|
|
|
|
// were told to re-throw them.
|
2012-11-09 19:43:57 +04:00
|
|
|
if (mCx) {
|
2015-06-08 23:16:27 +03:00
|
|
|
bool needToDealWithException = mAutoEntryScript->HasException();
|
2018-05-31 12:28:48 +03:00
|
|
|
if ((mRealm && mExceptionHandling == eRethrowContentExceptions) ||
|
2013-11-27 20:21:00 +04:00
|
|
|
mExceptionHandling == eRethrowExceptions) {
|
2013-02-19 20:54:40 +04:00
|
|
|
mErrorResult.MightThrowJSException();
|
2014-06-03 19:38:37 +04:00
|
|
|
if (needToDealWithException) {
|
2013-04-11 22:31:06 +04:00
|
|
|
JS::Rooted<JS::Value> exn(mCx);
|
2015-06-08 23:16:27 +03:00
|
|
|
if (mAutoEntryScript->PeekException(&exn) &&
|
2013-09-03 16:01:53 +04:00
|
|
|
ShouldRethrowException(exn)) {
|
2015-06-08 23:16:27 +03:00
|
|
|
mAutoEntryScript->ClearException();
|
|
|
|
MOZ_ASSERT(!mAutoEntryScript->HasException());
|
2013-02-19 20:54:40 +04:00
|
|
|
mErrorResult.ThrowJSException(mCx, exn);
|
2014-06-03 19:38:37 +04:00
|
|
|
needToDealWithException = false;
|
2013-02-19 20:54:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-03 19:38:37 +04:00
|
|
|
if (needToDealWithException) {
|
2013-02-19 20:54:40 +04:00
|
|
|
// Either we're supposed to report our exceptions, or we're supposed to
|
2015-06-08 23:16:27 +03:00
|
|
|
// re-throw them but we failed to get the exception value. Either way,
|
2016-03-25 18:11:20 +03:00
|
|
|
// we'll just report the pending exception, if any, once ~mAutoEntryScript
|
2018-05-16 11:53:16 +03:00
|
|
|
// runs. Note that we've already run ~mAr, effectively, so we don't have
|
2016-03-25 18:11:20 +03:00
|
|
|
// to worry about ordering here.
|
2016-03-10 12:50:56 +03:00
|
|
|
if (mErrorResult.IsJSContextException()) {
|
2016-08-26 22:38:16 +03:00
|
|
|
// XXXkhuey bug 1117269. When this is fixed, please consider fixing
|
|
|
|
// ThrowExceptionValueIfSafe over in Exceptions.cpp in the same way.
|
|
|
|
|
|
|
|
// IsJSContextException shouldn't be true anymore because we will report
|
|
|
|
// the exception on the JSContext ... so throw something else.
|
2019-09-20 05:17:01 +03:00
|
|
|
mErrorResult.Throw(NS_ERROR_UNEXPECTED);
|
2016-03-10 12:50:56 +03:00
|
|
|
}
|
2013-02-19 20:54:40 +04:00
|
|
|
}
|
2012-11-09 19:43:57 +04:00
|
|
|
}
|
|
|
|
|
2014-08-14 02:39:41 +04:00
|
|
|
mAutoIncumbentScript.reset();
|
|
|
|
mAutoEntryScript.reset();
|
2015-05-29 19:11:09 +03:00
|
|
|
|
|
|
|
// It is important that this is the last thing we do, after leaving the
|
2018-05-31 12:28:48 +03:00
|
|
|
// realm and undoing all our entry/incumbent script changes
|
2017-11-17 06:01:27 +03:00
|
|
|
CycleCollectedJSContext* ccjs = CycleCollectedJSContext::Get();
|
|
|
|
if (ccjs) {
|
|
|
|
ccjs->LeaveMicroTask();
|
2015-05-29 19:11:09 +03:00
|
|
|
}
|
2012-11-09 19:43:57 +04:00
|
|
|
}
|
|
|
|
|
2013-02-27 00:10:15 +04:00
|
|
|
already_AddRefed<nsISupports> CallbackObjectHolderBase::ToXPCOMCallback(
|
2013-04-17 01:15:57 +04:00
|
|
|
CallbackObject* aCallback, const nsIID& aIID) const {
|
2013-09-10 01:58:29 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-02-27 00:10:15 +04:00
|
|
|
if (!aCallback) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-18 17:48:38 +03:00
|
|
|
// We don't init the AutoJSAPI with our callback because we don't want it
|
|
|
|
// reporting errors to its global's onerror handlers.
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
|
|
|
JSContext* cx = jsapi.cx();
|
2013-05-04 03:29:09 +04:00
|
|
|
|
2016-11-15 08:25:37 +03:00
|
|
|
JS::Rooted<JSObject*> callback(cx, aCallback->CallbackOrNull());
|
2017-02-06 22:34:56 +03:00
|
|
|
if (!callback) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-05-04 03:29:09 +04:00
|
|
|
|
2018-08-07 15:06:44 +03:00
|
|
|
JSAutoRealm ar(cx, aCallback->CallbackGlobalOrNull());
|
2018-08-07 12:57:41 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsXPCWrappedJS> wrappedJS;
|
2018-08-07 12:57:41 +03:00
|
|
|
nsresult rv = nsXPCWrappedJS::GetNewOrUsed(cx, callback, aIID,
|
|
|
|
getter_AddRefs(wrappedJS));
|
2013-02-27 00:10:15 +04:00
|
|
|
if (NS_FAILED(rv) || !wrappedJS) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> retval;
|
|
|
|
rv = wrappedJS->QueryInterface(aIID, getter_AddRefs(retval));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval.forget();
|
|
|
|
}
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom
|