2012-11-09 07:43:57 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* 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 05:34:29 -08:00
|
|
|
#include "mozilla/dom/CallbackObject.h"
|
2013-09-03 05:01:53 -07:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
|
|
|
#include "mozilla/dom/DOMError.h"
|
|
|
|
#include "mozilla/dom/DOMErrorBinding.h"
|
2012-11-09 07:43:57 -08:00
|
|
|
#include "jsfriendapi.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsJSUtils.h"
|
2013-05-22 09:05:26 -07:00
|
|
|
#include "nsCxPusher.h"
|
2012-11-09 07:43:57 -08:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2013-02-26 12:10:15 -08:00
|
|
|
#include "xpcprivate.h"
|
2013-09-09 14:58:29 -07:00
|
|
|
#include "WorkerPrivate.h"
|
2013-11-20 08:48:00 -08:00
|
|
|
#include "nsGlobalWindow.h"
|
2013-12-11 17:51:57 -08:00
|
|
|
#include "WorkerScope.h"
|
2012-11-09 07:43:57 -08:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-01-28 05:34:29 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CallbackObject)
|
2013-04-16 14:14:30 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(mozilla::dom::CallbackObject)
|
2012-11-09 07:43:57 -08:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-01-28 05:34:29 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(CallbackObject)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(CallbackObject)
|
2012-11-09 07:43:57 -08:00
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(CallbackObject)
|
|
|
|
|
2013-01-28 05:34:29 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CallbackObject)
|
2012-11-09 07:43:57 -08:00
|
|
|
tmp->DropCallback();
|
2013-12-11 17:51:58 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mIncumbentGlobal)
|
2012-11-09 07:43:57 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2013-01-28 05:34:29 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(CallbackObject)
|
2012-11-09 07:43:57 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
2013-12-11 17:51:58 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mIncumbentGlobal)
|
2012-11-09 07:43:57 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2013-01-28 05:34:29 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(CallbackObject)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mCallback)
|
2012-11-09 07:43:57 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2013-12-11 17:51:57 -08:00
|
|
|
CallbackObject::CallSetup::CallSetup(CallbackObject* aCallback,
|
2013-02-19 08:54:40 -08:00
|
|
|
ErrorResult& aRv,
|
2013-09-03 05:01:53 -07:00
|
|
|
ExceptionHandling aExceptionHandling,
|
|
|
|
JSCompartment* aCompartment)
|
2012-11-09 07:43:57 -08:00
|
|
|
: mCx(nullptr)
|
2013-09-03 05:01:53 -07:00
|
|
|
, mCompartment(aCompartment)
|
2013-02-19 08:54:40 -08:00
|
|
|
, mErrorResult(aRv)
|
|
|
|
, mExceptionHandling(aExceptionHandling)
|
2013-09-09 14:58:29 -07:00
|
|
|
, mIsMainThread(NS_IsMainThread())
|
2012-11-09 07:43:57 -08:00
|
|
|
{
|
2013-09-09 14:58:29 -07:00
|
|
|
if (mIsMainThread) {
|
|
|
|
nsContentUtils::EnterMicroTask();
|
|
|
|
}
|
2013-01-28 05:34:29 -08:00
|
|
|
// We need to produce a useful JSContext here. Ideally one that the callback
|
2012-11-09 07:43:57 -08:00
|
|
|
// is in some sense associated with, so that we can sort of treat it as a
|
2013-01-28 05:34:29 -08:00
|
|
|
// "script entry point". Though once we actually have script entry points,
|
|
|
|
// we'll need to do the script entry point bits once we have an actual
|
|
|
|
// callable.
|
2012-11-09 07:43:57 -08:00
|
|
|
|
2013-01-28 05:34:29 -08:00
|
|
|
// First, find the real underlying callback.
|
2013-12-11 17:51:57 -08:00
|
|
|
JSObject* realCallback = js::UncheckedUnwrap(aCallback->CallbackPreserveColor());
|
2012-11-09 07:43:57 -08:00
|
|
|
JSContext* cx = nullptr;
|
2013-12-11 17:51:57 -08:00
|
|
|
nsIGlobalObject* globalObject = nullptr;
|
2013-09-09 14:58:29 -07:00
|
|
|
|
2013-12-31 08:30:57 -08:00
|
|
|
{
|
|
|
|
JS::AutoAssertNoGC nogc;
|
|
|
|
if (mIsMainThread) {
|
|
|
|
// Now get the global and JSContext for this callback.
|
|
|
|
nsGlobalWindow* win = xpc::WindowGlobalOrNull(realCallback);
|
|
|
|
if (win) {
|
|
|
|
// Make sure that if this is a window it's the current inner, since the
|
|
|
|
// nsIScriptContext and hence JSContext are associated with the outer
|
|
|
|
// window. Which means that if someone holds on to a function from a
|
|
|
|
// now-unloaded document we'd have the new document as the script entry
|
|
|
|
// point...
|
|
|
|
MOZ_ASSERT(win->IsInnerWindow());
|
|
|
|
nsPIDOMWindow* outer = win->GetOuterWindow();
|
|
|
|
if (!outer || win != outer->GetCurrentInnerWindow()) {
|
|
|
|
// Just bail out from here
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cx = win->GetContext() ? win->GetContext()->GetNativeContext()
|
|
|
|
// This happens - Removing it causes
|
|
|
|
// test_bug293235.xul to go orange.
|
|
|
|
: nsContentUtils::GetSafeJSContext();
|
|
|
|
globalObject = win;
|
|
|
|
} else {
|
|
|
|
// No DOM Window. Store the global and use the SafeJSContext.
|
|
|
|
JSObject* glob = js::GetGlobalForObjectCrossCompartment(realCallback);
|
|
|
|
globalObject = xpc::GetNativeForGlobal(glob);
|
|
|
|
MOZ_ASSERT(globalObject);
|
|
|
|
cx = nsContentUtils::GetSafeJSContext();
|
2013-09-09 14:58:29 -07:00
|
|
|
}
|
2013-11-20 08:48:00 -08:00
|
|
|
} else {
|
2013-12-31 08:30:57 -08:00
|
|
|
cx = workers::GetCurrentThreadJSContext();
|
|
|
|
globalObject = workers::GetCurrentThreadWorkerPrivate()->GlobalScope();
|
2012-11-09 07:43:57 -08:00
|
|
|
}
|
2013-12-11 17:51:57 -08:00
|
|
|
|
2013-12-31 08:30:57 -08:00
|
|
|
// Bail out if there's no useful global. This seems to happen intermittently
|
|
|
|
// on gaia-ui tests, probably because nsInProcessTabChildGlobal is returning
|
|
|
|
// null in some kind of teardown state.
|
|
|
|
if (!globalObject->GetGlobalJSObject()) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-06 12:01:41 -08:00
|
|
|
|
2013-12-31 08:30:57 -08:00
|
|
|
mAutoEntryScript.construct(globalObject, mIsMainThread, cx);
|
|
|
|
if (aCallback->IncumbentGlobalOrNull()) {
|
|
|
|
mAutoIncumbentScript.construct(aCallback->IncumbentGlobalOrNull());
|
|
|
|
}
|
2013-12-11 17:51:57 -08:00
|
|
|
|
2013-12-31 08:30:57 -08:00
|
|
|
// Unmark the callable (by invoking Callback() 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 order the following two
|
|
|
|
// operations _after_ the Push() above, which lets us take advantage of the
|
|
|
|
// JSAutoRequest embedded in the pusher.
|
|
|
|
//
|
|
|
|
// We can do this even though we're not in the right compartment yet, because
|
|
|
|
// Rooted<> does not care about compartments.
|
|
|
|
mRootedCallable.construct(cx, aCallback->Callback());
|
|
|
|
}
|
2013-06-20 11:05:34 -07:00
|
|
|
|
2013-09-09 14:58:29 -07:00
|
|
|
if (mIsMainThread) {
|
|
|
|
// Check that it's ok to run this callback at all.
|
2013-12-11 17:51:57 -08:00
|
|
|
// Make sure to use realCallback to get the global of the callback object,
|
|
|
|
// not the wrapper.
|
2013-11-12 16:43:32 -08:00
|
|
|
bool allowed = nsContentUtils::GetSecurityManager()->
|
2013-12-11 17:51:57 -08:00
|
|
|
ScriptAllowed(js::GetGlobalForObjectCrossCompartment(realCallback));
|
2013-11-12 16:43:32 -08:00
|
|
|
|
|
|
|
if (!allowed) {
|
2013-09-09 14:58:29 -07:00
|
|
|
return;
|
|
|
|
}
|
2012-11-09 07:43:57 -08:00
|
|
|
}
|
|
|
|
|
2013-01-28 05:34:29 -08:00
|
|
|
// Enter the compartment of our callback, so we can actually work with it.
|
2013-12-11 17:51:57 -08:00
|
|
|
//
|
|
|
|
// Note that if the callback is a wrapper, this will not be the same
|
|
|
|
// compartment that we ended up in with mAutoEntryScript above, because the
|
|
|
|
// entry point is based off of the unwrapped callback (realCallback).
|
2013-12-11 17:51:57 -08:00
|
|
|
mAc.construct(cx, mRootedCallable.ref());
|
2012-11-09 07:43:57 -08:00
|
|
|
|
|
|
|
// And now we're ready to go.
|
|
|
|
mCx = cx;
|
2013-02-19 08:54:40 -08:00
|
|
|
|
|
|
|
// Make sure the JS engine doesn't report exceptions we want to re-throw
|
2013-09-03 05:01:53 -07:00
|
|
|
if (mExceptionHandling == eRethrowContentExceptions ||
|
|
|
|
mExceptionHandling == eRethrowExceptions) {
|
2013-10-28 04:51:05 -07:00
|
|
|
mSavedJSContextOptions = JS::ContextOptionsRef(cx);
|
|
|
|
JS::ContextOptionsRef(cx).setDontReportUncaught(true);
|
2013-02-19 08:54:40 -08:00
|
|
|
}
|
2012-11-09 07:43:57 -08:00
|
|
|
}
|
|
|
|
|
2013-09-03 05:01:53 -07:00
|
|
|
bool
|
|
|
|
CallbackObject::CallSetup::ShouldRethrowException(JS::Handle<JS::Value> aException)
|
|
|
|
{
|
|
|
|
if (mExceptionHandling == eRethrowExceptions) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mExceptionHandling == eRethrowContentExceptions);
|
|
|
|
|
|
|
|
// For eRethrowContentExceptions we only want to throw an exception if the
|
|
|
|
// object that was thrown is a DOMError object in the caller compartment
|
|
|
|
// (which we stored in mCompartment).
|
|
|
|
|
|
|
|
if (!aException.isObject()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(mCx, &aException.toObject());
|
|
|
|
obj = js::UncheckedUnwrap(obj, /* stopAtOuter = */ false);
|
|
|
|
if (js::GetObjectCompartment(obj) != mCompartment) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DOMError* domError;
|
2013-11-21 04:51:16 -08:00
|
|
|
return NS_SUCCEEDED(UNWRAP_OBJECT(DOMError, obj, domError));
|
2013-09-03 05:01:53 -07:00
|
|
|
}
|
|
|
|
|
2013-01-28 05:34:29 -08:00
|
|
|
CallbackObject::CallSetup::~CallSetup()
|
2012-11-09 07:43:57 -08:00
|
|
|
{
|
|
|
|
// First things first: if we have a JSContext, report any pending
|
2013-02-19 08:54:40 -08:00
|
|
|
// errors on it, unless we were told to re-throw them.
|
2012-11-09 07:43:57 -08:00
|
|
|
if (mCx) {
|
2013-02-19 08:54:40 -08:00
|
|
|
bool dealtWithPendingException = false;
|
2013-11-27 08:21:00 -08:00
|
|
|
if ((mCompartment && mExceptionHandling == eRethrowContentExceptions) ||
|
|
|
|
mExceptionHandling == eRethrowExceptions) {
|
2013-02-19 08:54:40 -08:00
|
|
|
// Restore the old context options
|
2013-10-28 04:51:05 -07:00
|
|
|
JS::ContextOptionsRef(mCx) = mSavedJSContextOptions;
|
2013-02-19 08:54:40 -08:00
|
|
|
mErrorResult.MightThrowJSException();
|
|
|
|
if (JS_IsExceptionPending(mCx)) {
|
2013-04-11 11:31:06 -07:00
|
|
|
JS::Rooted<JS::Value> exn(mCx);
|
2013-09-19 00:54:01 -07:00
|
|
|
if (JS_GetPendingException(mCx, &exn) &&
|
2013-09-03 05:01:53 -07:00
|
|
|
ShouldRethrowException(exn)) {
|
2013-02-19 08:54:40 -08:00
|
|
|
mErrorResult.ThrowJSException(mCx, exn);
|
|
|
|
JS_ClearPendingException(mCx);
|
|
|
|
dealtWithPendingException = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dealtWithPendingException) {
|
|
|
|
// Either we're supposed to report our exceptions, or we're supposed to
|
|
|
|
// re-throw them but we failed to JS_GetPendingException. Either way,
|
|
|
|
// just report the pending exception, if any.
|
|
|
|
nsJSUtils::ReportPendingException(mCx);
|
|
|
|
}
|
2012-11-09 07:43:57 -08:00
|
|
|
}
|
|
|
|
|
2013-08-14 14:00:03 -07:00
|
|
|
// To get our nesting right we have to destroy our JSAutoCompartment first.
|
|
|
|
// But be careful: it might not have been constructed at all!
|
2012-11-09 07:43:57 -08:00
|
|
|
mAc.destroyIfConstructed();
|
|
|
|
|
2013-12-11 17:51:58 -08:00
|
|
|
mAutoIncumbentScript.destroyIfConstructed();
|
2013-12-11 17:51:57 -08:00
|
|
|
mAutoEntryScript.destroyIfConstructed();
|
2013-09-09 14:58:29 -07:00
|
|
|
|
|
|
|
// It is important that this is the last thing we do, after leaving the
|
2013-12-11 17:51:57 -08:00
|
|
|
// compartment and undoing all our entry/incumbent script changes
|
2013-09-09 14:58:29 -07:00
|
|
|
if (mIsMainThread) {
|
|
|
|
nsContentUtils::LeaveMicroTask();
|
|
|
|
}
|
2012-11-09 07:43:57 -08:00
|
|
|
}
|
|
|
|
|
2013-02-26 12:10:15 -08:00
|
|
|
already_AddRefed<nsISupports>
|
|
|
|
CallbackObjectHolderBase::ToXPCOMCallback(CallbackObject* aCallback,
|
2013-04-16 14:15:57 -07:00
|
|
|
const nsIID& aIID) const
|
2013-02-26 12:10:15 -08:00
|
|
|
{
|
2013-09-09 14:58:29 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-02-26 12:10:15 -08:00
|
|
|
if (!aCallback) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-05-06 06:04:17 -07:00
|
|
|
AutoSafeJSContext cx;
|
2013-05-03 16:29:09 -07:00
|
|
|
|
|
|
|
JS::Rooted<JSObject*> callback(cx, aCallback->Callback());
|
|
|
|
|
2013-02-26 12:10:15 -08:00
|
|
|
JSAutoCompartment ac(cx, callback);
|
|
|
|
nsRefPtr<nsXPCWrappedJS> wrappedJS;
|
|
|
|
nsresult rv =
|
2013-12-19 18:08:07 -08:00
|
|
|
nsXPCWrappedJS::GetNewOrUsed(callback, aIID, getter_AddRefs(wrappedJS));
|
2013-02-26 12:10:15 -08: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();
|
|
|
|
}
|
|
|
|
|
2012-11-09 07:43:57 -08:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|