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"
|
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"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
2013-02-26 12:10:15 -08:00
|
|
|
#include "xpcprivate.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)
|
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-01-28 05:34:29 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CallbackObject)
|
2012-11-09 07:43:57 -08:00
|
|
|
tmp->DropCallback();
|
|
|
|
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
|
|
|
|
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-02-19 08:54:40 -08:00
|
|
|
CallbackObject::CallSetup::CallSetup(JSObject* const aCallback,
|
|
|
|
ErrorResult& aRv,
|
|
|
|
ExceptionHandling aExceptionHandling)
|
2012-11-09 07:43:57 -08:00
|
|
|
: mCx(nullptr)
|
2013-02-19 08:54:40 -08:00
|
|
|
, mErrorResult(aRv)
|
|
|
|
, mExceptionHandling(aExceptionHandling)
|
2012-11-09 07:43:57 -08:00
|
|
|
{
|
2013-01-28 05:34:29 -08:00
|
|
|
xpc_UnmarkGrayObject(aCallback);
|
2012-11-09 07:43:57 -08:00
|
|
|
|
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-04-11 11:50:18 -07:00
|
|
|
JSObject* realCallback = js::UncheckedUnwrap(aCallback);
|
2012-11-09 07:43:57 -08:00
|
|
|
|
2013-01-28 05:34:29 -08:00
|
|
|
// Now get the nsIScriptGlobalObject for this callback.
|
2012-11-09 07:43:57 -08:00
|
|
|
JSContext* cx = nullptr;
|
|
|
|
nsIScriptContext* ctx = nullptr;
|
2013-01-28 05:34:29 -08:00
|
|
|
nsIScriptGlobalObject* sgo = nsJSUtils::GetStaticScriptGlobal(realCallback);
|
2012-11-09 07:43:57 -08:00
|
|
|
if (sgo) {
|
|
|
|
// 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...
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(sgo);
|
|
|
|
if (win) {
|
|
|
|
MOZ_ASSERT(win->IsInnerWindow());
|
|
|
|
nsPIDOMWindow* outer = win->GetOuterWindow();
|
|
|
|
if (!outer || win != outer->GetCurrentInnerWindow()) {
|
|
|
|
// Just bail out from here
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if not a window at all, just press on
|
|
|
|
|
|
|
|
ctx = sgo->GetContext();
|
|
|
|
if (ctx) {
|
|
|
|
// We don't check whether scripts are enabled on ctx, because
|
|
|
|
// CheckFunctionAccess will do that anyway... and because we ignore them
|
|
|
|
// being disabled if the callee is system.
|
|
|
|
cx = ctx->GetNativeContext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cx) {
|
|
|
|
// We didn't manage to hunt down a script global to work with. Just fall
|
|
|
|
// back on using the safe context.
|
|
|
|
cx = nsContentUtils::GetSafeJSContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Victory! We have a JSContext. Now do the things we need a JSContext for.
|
|
|
|
mAr.construct(cx);
|
|
|
|
|
|
|
|
// Make sure our JSContext is pushed on the stack.
|
2013-02-26 11:04:11 -08:00
|
|
|
mCxPusher.Push(cx);
|
2012-11-09 07:43:57 -08:00
|
|
|
|
|
|
|
// After this point we guarantee calling ScriptEvaluated() if we
|
|
|
|
// have an nsIScriptContext.
|
|
|
|
// XXXbz Why, if, say CheckFunctionAccess fails? I know that's how
|
2013-01-03 11:02:42 -08:00
|
|
|
// nsJSContext::CallEventHandler used to work, but is it required?
|
2012-11-09 07:43:57 -08:00
|
|
|
// FIXME: Bug 807369.
|
|
|
|
mCtx = ctx;
|
|
|
|
|
|
|
|
// Check that it's ok to run this callback at all.
|
|
|
|
// FIXME: Bug 807371: we want a less silly check here.
|
2013-01-28 05:34:29 -08:00
|
|
|
// Make sure to unwrap aCallback before passing it in, because
|
2012-11-09 07:43:57 -08:00
|
|
|
// getting principals from wrappers is silly.
|
|
|
|
nsresult rv = nsContentUtils::GetSecurityManager()->
|
2013-04-11 11:50:18 -07:00
|
|
|
CheckFunctionAccess(cx, js::UncheckedUnwrap(aCallback), nullptr);
|
2012-11-09 07:43:57 -08:00
|
|
|
|
|
|
|
// Construct a termination func holder even if we're not planning to
|
|
|
|
// run any script. We need this because we're going to call
|
|
|
|
// ScriptEvaluated even if we don't run the script... See XXX
|
|
|
|
// comment above.
|
|
|
|
if (ctx) {
|
|
|
|
mTerminationFuncHolder.construct(static_cast<nsJSContext*>(ctx));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Security check failed. We're done here.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-28 05:34:29 -08:00
|
|
|
// Enter the compartment of our callback, so we can actually work with it.
|
|
|
|
mAc.construct(cx, aCallback);
|
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
|
|
|
|
if (mExceptionHandling == eRethrowExceptions) {
|
|
|
|
mSavedJSContextOptions = JS_GetOptions(cx);
|
|
|
|
JS_SetOptions(cx, mSavedJSContextOptions | JSOPTION_DONT_REPORT_UNCAUGHT);
|
|
|
|
}
|
2012-11-09 07:43:57 -08: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;
|
|
|
|
if (mExceptionHandling == eRethrowExceptions) {
|
|
|
|
// Restore the old context options
|
|
|
|
JS_SetOptions(mCx, mSavedJSContextOptions);
|
|
|
|
mErrorResult.MightThrowJSException();
|
|
|
|
if (JS_IsExceptionPending(mCx)) {
|
2013-04-11 11:31:06 -07:00
|
|
|
JS::Rooted<JS::Value> exn(mCx);
|
|
|
|
if (JS_GetPendingException(mCx, exn.address())) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// If we have an mCtx, we need to call ScriptEvaluated() on it. But we have
|
|
|
|
// to do that after we pop the JSContext stack (see bug 295983). And to get
|
|
|
|
// our nesting right we have to destroy our JSAutoCompartment first. But be
|
|
|
|
// careful: it might not have been constructed at all!
|
|
|
|
mAc.destroyIfConstructed();
|
|
|
|
|
|
|
|
// XXXbz For that matter why do we need to manually call ScriptEvaluated at
|
|
|
|
// all? nsCxPusher::Pop will do that nowadays if !mScriptIsRunning, so the
|
|
|
|
// concerns from bug 295983 don't seem relevant anymore. Do we want to make
|
|
|
|
// sure it's still called when !mScriptIsRunning? I guess play it safe for
|
|
|
|
// now and do what CallEventHandler did, which is call always.
|
|
|
|
|
|
|
|
// Popping an nsCxPusher is safe even if it never got pushed.
|
|
|
|
mCxPusher.Pop();
|
|
|
|
|
|
|
|
if (mCtx) {
|
|
|
|
mCtx->ScriptEvaluated(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-26 12:10:15 -08:00
|
|
|
already_AddRefed<nsISupports>
|
|
|
|
CallbackObjectHolderBase::ToXPCOMCallback(CallbackObject* aCallback,
|
|
|
|
const nsIID& aIID)
|
|
|
|
{
|
|
|
|
if (!aCallback) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* callback = aCallback->Callback();
|
|
|
|
|
|
|
|
SafeAutoJSContext cx;
|
|
|
|
JSAutoCompartment ac(cx, callback);
|
|
|
|
XPCCallContext ccx(NATIVE_CALLER, cx);
|
|
|
|
if (!ccx.IsValid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsXPCWrappedJS> wrappedJS;
|
|
|
|
nsresult rv =
|
|
|
|
nsXPCWrappedJS::GetNewOrUsed(ccx, callback, aIID,
|
|
|
|
nullptr, getter_AddRefs(wrappedJS));
|
|
|
|
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
|