Bug 986304 - Stop using (and kill) BindCompileEventHandler. r=bz

This commit is contained in:
Bobby Holley 2014-03-21 23:31:02 -03:00
parent 9ec0cadd8f
commit 615c77320d
4 changed files with 8 additions and 147 deletions

View File

@ -27,8 +27,8 @@ class nsIDOMWindow;
class nsIURI;
#define NS_ISCRIPTCONTEXT_IID \
{ 0x7cf47061, 0x745d, 0x4c6c, \
{ 0xa0, 0xe5, 0x9f, 0xef, 0xa8, 0xcc, 0x2a, 0xf0 } }
{ 0x274840b6, 0x7349, 0x4798, \
{ 0xbe, 0x24, 0xbd, 0x75, 0xa6, 0x46, 0x99, 0xb7 } }
/* This MUST match JSVERSION_DEFAULT. This version stuff if we don't
know what language we have is a little silly... */
@ -45,32 +45,6 @@ class nsIScriptContext : public nsISupports
public:
NS_DECLARE_STATIC_IID_ACCESSOR(NS_ISCRIPTCONTEXT_IID)
/**
* Bind an already-compiled event handler function to the given
* target. Scripting languages with static scoping must re-bind the
* scope chain for aHandler to begin (after the activation scope for
* aHandler itself, typically) with aTarget's scope.
*
* The result of the bind operation is a new handler object, with
* principals now set and scope set as above. This is returned in
* aBoundHandler. When this function is called, aBoundHandler is
* expected to not be holding an object.
*
* @param aTarget an object telling the scope in which to bind the compiled
* event handler function. The context will presumably associate
* this nsISupports with a native script object.
* @param aScope the scope in which the script object for aTarget should be
* looked for.
* @param aHandler the function object to bind, created by an earlier call to
* CompileEventHandler
* @param aBoundHandler [out] the result of the bind operation.
* @return NS_OK if the function was successfully bound
*/
virtual nsresult BindCompiledEventHandler(nsISupports* aTarget,
JS::Handle<JSObject*> aScope,
JS::Handle<JSObject*> aHandler,
JS::MutableHandle<JSObject*> aBoundHandler) = 0;
/**
* Return the global object.
*

View File

@ -909,95 +909,6 @@ AtomIsEventHandlerName(nsIAtom *aName)
}
#endif
// Helper function to find the JSObject associated with a (presumably DOM)
// interface.
nsresult
nsJSContext::JSObjectFromInterface(nsISupports* aTarget,
JS::Handle<JSObject*> aScope,
JSObject** aRet)
{
// It is legal to specify a null target.
if (!aTarget) {
*aRet = nullptr;
return NS_OK;
}
AutoJSContext cx;
// Get the jsobject associated with this target
// We don't wrap here because we trust the JS engine to wrap the target
// later.
JS::Rooted<JS::Value> v(cx);
nsresult rv = nsContentUtils::WrapNative(cx, aScope, aTarget, &v);
NS_ENSURE_SUCCESS(rv, rv);
JSObject* obj = v.toObjectOrNull();
if (obj) {
JS::ExposeObjectToActiveJS(obj);
}
#ifdef DEBUG
JS::Rooted<JSObject*> rootedObj(cx, obj);
nsCOMPtr<nsISupports> targetSupp = do_QueryInterface(aTarget);
nsCOMPtr<nsISupports> native =
nsContentUtils::XPConnect()->GetNativeOfWrapper(cx, rootedObj);
NS_ASSERTION(native == targetSupp, "Native should be the target!");
obj = rootedObj;
#endif
*aRet = obj;
return NS_OK;
}
nsresult
nsJSContext::BindCompiledEventHandler(nsISupports* aTarget,
JS::Handle<JSObject*> aScope,
JS::Handle<JSObject*> aHandler,
JS::MutableHandle<JSObject*> aBoundHandler)
{
NS_ENSURE_ARG(aHandler);
NS_ENSURE_TRUE(mIsInitialized, NS_ERROR_NOT_INITIALIZED);
NS_PRECONDITION(!aBoundHandler, "Shouldn't already have a bound handler!");
if (aScope) {
JS::ExposeObjectToActiveJS(aScope);
}
JS::ExposeObjectToActiveJS(aHandler);
AutoPushJSContext cx(mContext);
// Get the jsobject associated with this target
JS::Rooted<JSObject*> target(cx);
JS::Rooted<JSObject*> scope(cx, aScope);
nsresult rv = JSObjectFromInterface(aTarget, scope, target.address());
NS_ENSURE_SUCCESS(rv, rv);
#ifdef DEBUG
{
JSAutoCompartment ac(cx, aHandler);
JS::Rooted<JS::Value> val(cx, JS::ObjectValue(*aHandler));
NS_ASSERTION(JS_TypeOfValue(cx, val) == JSTYPE_FUNCTION,
"Event handler object not a function");
}
#endif
JSAutoCompartment ac(cx, target);
JSObject* funobj;
// Make sure the handler function is parented by its event target object
if (aHandler) {
funobj = JS_CloneFunctionObject(cx, aHandler, target);
if (!funobj) {
rv = NS_ERROR_OUT_OF_MEMORY;
}
} else {
funobj = nullptr;
}
aBoundHandler.set(funobj);
return rv;
}
nsIScriptGlobalObject *
nsJSContext::GetGlobalObject()
{

View File

@ -44,11 +44,6 @@ public:
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(nsJSContext,
nsIScriptContext)
virtual nsresult BindCompiledEventHandler(nsISupports *aTarget,
JS::Handle<JSObject*> aScope,
JS::Handle<JSObject*> aHandler,
JS::MutableHandle<JSObject*> aBoundHandler) MOZ_OVERRIDE;
virtual nsIScriptGlobalObject *GetGlobalObject() MOZ_OVERRIDE;
inline nsIScriptGlobalObject *GetGlobalObjectRef() { return mGlobalObjectRef; }
@ -145,12 +140,6 @@ protected:
nsresult AddSupportsPrimitiveTojsvals(nsISupports *aArg, JS::Value *aArgv);
// given an nsISupports object (presumably an event target or some other
// DOM object), get (or create) the JSObject wrapping it.
nsresult JSObjectFromInterface(nsISupports *aSup,
JS::Handle<JSObject*> aScript,
JSObject **aRet);
// Report the pending exception on our mContext, if any. This
// function will set aside the frame chain on mContext before
// reporting.
@ -168,8 +157,6 @@ private:
bool mGCOnDestruction;
bool mProcessingScriptTag;
PRTime mOperationCallbackTime;
PRTime mModalStateTime;
uint32_t mModalStateDepth;

View File

@ -807,7 +807,6 @@ EventListenerManager::CompileEventHandlerInternal(Listener* aListener,
// Push a context to make sure exceptions are reported in the right place.
AutoPushJSContextForErrorReporting cx(context->GetNativeContext());
JS::Rooted<JSObject*> handler(cx);
JS::Rooted<JSObject*> scope(cx, jsListener->GetEventScope());
nsCOMPtr<nsIAtom> typeAtom = aListener->mTypeAtom;
@ -909,35 +908,25 @@ EventListenerManager::CompileEventHandlerInternal(Listener* aListener,
.setElementAttributeName(jsStr)
.setDefineOnScope(false);
JS::Rooted<JSObject*> handlerFun(cx);
JS::Rooted<JSObject*> handler(cx);
result = nsJSUtils::CompileFunction(cx, target, options,
nsAtomCString(typeAtom),
argCount, argNames, *body, handlerFun.address());
argCount, argNames, *body, handler.address());
NS_ENSURE_SUCCESS(result, result);
handler = handlerFun;
NS_ENSURE_TRUE(handler, NS_ERROR_FAILURE);
nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(mTarget);
// Bind it
JS::Rooted<JSObject*> boundHandler(cx);
context->BindCompiledEventHandler(mTarget, scope, handler, &boundHandler);
// Note - We pass null for aIncumbentGlobal below. We could also pass the
// compilation global, but since the handler is guaranteed to be scripted,
// there's no need to use an override, since the JS engine will always give
// us the right answer.
if (!boundHandler) {
jsListener->ForgetHandler();
} else if (jsListener->EventName() == nsGkAtoms::onerror && win) {
if (jsListener->EventName() == nsGkAtoms::onerror && win) {
nsRefPtr<OnErrorEventHandlerNonNull> handlerCallback =
new OnErrorEventHandlerNonNull(boundHandler, /* aIncumbentGlobal = */ nullptr);
new OnErrorEventHandlerNonNull(handler, /* aIncumbentGlobal = */ nullptr);
jsListener->SetHandler(handlerCallback);
} else if (jsListener->EventName() == nsGkAtoms::onbeforeunload && win) {
nsRefPtr<OnBeforeUnloadEventHandlerNonNull> handlerCallback =
new OnBeforeUnloadEventHandlerNonNull(boundHandler, /* aIncumbentGlobal = */ nullptr);
new OnBeforeUnloadEventHandlerNonNull(handler, /* aIncumbentGlobal = */ nullptr);
jsListener->SetHandler(handlerCallback);
} else {
nsRefPtr<EventHandlerNonNull> handlerCallback =
new EventHandlerNonNull(boundHandler, /* aIncumbentGlobal = */ nullptr);
new EventHandlerNonNull(handler, /* aIncumbentGlobal = */ nullptr);
jsListener->SetHandler(handlerCallback);
}