зеркало из https://github.com/mozilla/gecko-dev.git
Fix for 19650 and 15133. Added nsIJSEventListener. JS event listener now stores script context and script object owner for late compilation of script event handlers.
This commit is contained in:
Родитель
3ddb8869ea
Коммит
58c1de27fd
|
@ -1,19 +1,23 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
* The contents of this file are subject to the Netscape Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* The Original Code is mozilla.org code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All
|
||||
* Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
/* AUTO-GENERATED. DO NOT EDIT!!! */
|
||||
|
||||
|
|
|
@ -25,18 +25,26 @@
|
|||
#include "nsIServiceManager.h"
|
||||
#include "nsIJSContextStack.h"
|
||||
#include "nsIScriptSecurityManager.h"
|
||||
#include "nsIScriptObjectOwner.h"
|
||||
|
||||
static NS_DEFINE_IID(kIDOMEventListenerIID, NS_IDOMEVENTLISTENER_IID);
|
||||
static NS_DEFINE_IID(kIJSEventListenerIID, NS_IJSEVENTLISTENER_IID);
|
||||
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
|
||||
|
||||
/*
|
||||
* nsJSEventListener implementation
|
||||
*/
|
||||
nsJSEventListener::nsJSEventListener(JSContext *aContext, JSObject *aObj)
|
||||
nsJSEventListener::nsJSEventListener(nsIScriptContext *aContext,
|
||||
nsIScriptObjectOwner *aOwner)
|
||||
{
|
||||
NS_INIT_REFCNT();
|
||||
|
||||
// Both of these are weak references. We are guaranteed
|
||||
// because of the ownership model that this object will be
|
||||
// freed (and the references dropped) before either the context
|
||||
// or the owner goes away.
|
||||
mContext = aContext;
|
||||
mJSObj = aObj;
|
||||
mOwner = aOwner;
|
||||
}
|
||||
|
||||
nsJSEventListener::~nsJSEventListener()
|
||||
|
@ -53,6 +61,11 @@ nsresult nsJSEventListener::QueryInterface(REFNSIID aIID, void** aInstancePtr)
|
|||
AddRef();
|
||||
return NS_OK;
|
||||
}
|
||||
if (aIID.Equals(kIJSEventListenerIID)) {
|
||||
*aInstancePtr = (void*)(nsIJSEventListener*)this;
|
||||
AddRef();
|
||||
return NS_OK;
|
||||
}
|
||||
if (aIID.Equals(kISupportsIID)) {
|
||||
*aInstancePtr = (void*)(nsISupports*)(nsIDOMEventListener*)this;
|
||||
AddRef();
|
||||
|
@ -74,6 +87,12 @@ nsresult nsJSEventListener::HandleEvent(nsIDOMEvent* aEvent)
|
|||
JSObject *eventObj;
|
||||
char* eventChars;
|
||||
nsAutoString eventString;
|
||||
// XXX This doesn't seem like the correct context on which to execute
|
||||
// the event handler. Might need to get one from the JS thread context
|
||||
// stack.
|
||||
JSContext* cx = (JSContext*)mContext->GetNativeContext();
|
||||
JSObject* obj;
|
||||
nsresult result = NS_OK;
|
||||
|
||||
if (NS_OK != aEvent->GetType(eventString)) {
|
||||
//JS can't handle this event yet or can't handle it at all
|
||||
|
@ -81,36 +100,60 @@ nsresult nsJSEventListener::HandleEvent(nsIDOMEvent* aEvent)
|
|||
}
|
||||
|
||||
eventString.Insert("on", 0, 2);
|
||||
|
||||
eventChars = eventString.ToNewCString();
|
||||
|
||||
result = mOwner->GetScriptObject(mContext, (void**)&obj);
|
||||
if (NS_FAILED(result)) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (!JS_LookupProperty(mContext, mJSObj, eventChars, &funval)) {
|
||||
if (!JS_LookupProperty(cx, obj, eventChars, &funval)) {
|
||||
nsCRT::free(eventChars);
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
nsCRT::free(eventChars);
|
||||
|
||||
if (JS_TypeOfValue(mContext, funval) != JSTYPE_FUNCTION) {
|
||||
if (JS_TypeOfValue(cx, funval) != JSTYPE_FUNCTION) {
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsIScriptContext *mScriptCX = (nsIScriptContext *)JS_GetContextPrivate(mContext);
|
||||
if (NS_OK != NS_NewScriptKeyEvent(mScriptCX, aEvent, nsnull, (void**)&eventObj)) {
|
||||
result = NS_NewScriptKeyEvent(mContext, aEvent, nsnull, (void**)&eventObj);
|
||||
if (NS_FAILED(result)) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
argv[0] = OBJECT_TO_JSVAL(eventObj);
|
||||
JSFunction *jsFun = JS_ValueToFunction(mContext, funval);
|
||||
JSFunction *jsFun = JS_ValueToFunction(cx, funval);
|
||||
PRBool jsBoolResult;
|
||||
if (!jsFun || NS_FAILED(mScriptCX->CallFunction(mJSObj, jsFun, 1, argv,
|
||||
&jsBoolResult)))
|
||||
if (!jsFun)
|
||||
{
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
result = mContext->CallFunction(obj, jsFun, 1, argv, &jsBoolResult);
|
||||
if (NS_FAILED(result)) {
|
||||
return result;
|
||||
}
|
||||
if (!jsBoolResult)
|
||||
aEvent->PreventDefault();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsJSEventListener::GetEventTarget(nsIScriptContext**aContext,
|
||||
nsIScriptObjectOwner** aOwner)
|
||||
{
|
||||
NS_ASSERTION(aContext && aOwner, "null argument");
|
||||
if (aContext) {
|
||||
*aContext = mContext;
|
||||
NS_ADDREF(mContext);
|
||||
}
|
||||
if (aOwner) {
|
||||
*aOwner = mOwner;
|
||||
NS_ADDREF(mOwner);
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
@ -118,11 +161,9 @@ nsresult nsJSEventListener::HandleEvent(nsIDOMEvent* aEvent)
|
|||
* Factory functions
|
||||
*/
|
||||
|
||||
extern "C" NS_DOM nsresult NS_NewJSEventListener(nsIDOMEventListener ** aInstancePtrResult, nsIScriptContext *aContext, void *aObj)
|
||||
extern "C" NS_DOM nsresult NS_NewJSEventListener(nsIDOMEventListener ** aInstancePtrResult, nsIScriptContext *aContext, nsIScriptObjectOwner *aOwner)
|
||||
{
|
||||
JSContext *mCX = (JSContext*)aContext->GetNativeContext();
|
||||
|
||||
nsJSEventListener* it = new nsJSEventListener(mCX, (JSObject*)aObj);
|
||||
nsJSEventListener* it = new nsJSEventListener(aContext, aOwner);
|
||||
if (NULL == it) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
|
|
@ -25,13 +25,16 @@
|
|||
|
||||
#include "nsIDOMKeyEvent.h"
|
||||
#include "nsIScriptEventListener.h"
|
||||
#include "nsIJSEventListener.h"
|
||||
#include "nsIDOMMouseListener.h"
|
||||
#include "jsapi.h"
|
||||
|
||||
//nsIDOMMouseListener interface
|
||||
class nsJSEventListener : public nsIDOMEventListener {
|
||||
class nsJSEventListener : public nsIDOMEventListener,
|
||||
public nsIJSEventListener
|
||||
{
|
||||
public:
|
||||
nsJSEventListener(JSContext *aContext, JSObject *aObj);
|
||||
nsJSEventListener(nsIScriptContext *aContext, nsIScriptObjectOwner* aOwner);
|
||||
virtual ~nsJSEventListener();
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
@ -40,11 +43,13 @@ public:
|
|||
virtual nsresult HandleEvent(nsIDOMEvent* aEvent);
|
||||
|
||||
//nsIJSEventListener interface
|
||||
NS_IMETHOD GetEventTarget(nsIScriptContext** aContext, nsIScriptObjectOwner** aOwner);
|
||||
|
||||
protected:
|
||||
JSContext *mContext;
|
||||
JSObject *mJSObj;
|
||||
|
||||
nsIScriptContext* mContext;
|
||||
nsIScriptObjectOwner* mOwner;
|
||||
};
|
||||
|
||||
|
||||
#endif //nsJSEventListener_h__
|
||||
|
||||
|
|
|
@ -1,19 +1,23 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
* The contents of this file are subject to the Netscape Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* The Original Code is mozilla.org code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All
|
||||
* Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
/* AUTO-GENERATED. DO NOT EDIT!!! */
|
||||
|
||||
|
|
|
@ -1,19 +1,23 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
* The contents of this file are subject to the Netscape Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* The Original Code is mozilla.org code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All
|
||||
* Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
/* AUTO-GENERATED. DO NOT EDIT!!! */
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче