зеркало из https://github.com/mozilla/pjs.git
Bug 83557, make xmlextras work in embedding. XMLHttpRequest is more like a DOM class now, events are fired and executed on the proper context, and a lot of JS specific code was removed. Patch by jst, r=heikki, sr=vidur, a=asa@mozilla.org.
This commit is contained in:
Родитель
0ef5580b0d
Коммит
b0b3de93e4
|
@ -471,9 +471,8 @@ nsDOMClassInfoData sClassInfoData[] = {
|
||||||
DOM_DEFAULT_SCRIPTABLE_FLAGS)
|
DOM_DEFAULT_SCRIPTABLE_FLAGS)
|
||||||
|
|
||||||
// XML extras classes
|
// XML extras classes
|
||||||
NS_DEFINE_CLASSINFO_DATA(XMLHttpRequest, nsXMLHttpRequestSH,
|
NS_DEFINE_CLASSINFO_DATA(XMLHttpRequest, nsDOMGenericSH,
|
||||||
0 /* Not used, XMLHttpRequest is it's own
|
DOM_DEFAULT_SCRIPTABLE_FLAGS)
|
||||||
helper */)
|
|
||||||
NS_DEFINE_CLASSINFO_DATA(DOMSerializer, nsDOMGenericSH,
|
NS_DEFINE_CLASSINFO_DATA(DOMSerializer, nsDOMGenericSH,
|
||||||
DOM_DEFAULT_SCRIPTABLE_FLAGS)
|
DOM_DEFAULT_SCRIPTABLE_FLAGS)
|
||||||
NS_DEFINE_CLASSINFO_DATA(DOMParser, nsDOMGenericSH,
|
NS_DEFINE_CLASSINFO_DATA(DOMParser, nsDOMGenericSH,
|
||||||
|
@ -3249,20 +3248,6 @@ nsCSSRuleListSH::GetItemAt(nsISupports *aNative, PRUint32 aIndex,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// XMLHttpRequest helper
|
|
||||||
|
|
||||||
// XMLHttpRequest is it's own helper so we don't supply one here.
|
|
||||||
|
|
||||||
NS_IMETHODIMP
|
|
||||||
nsXMLHttpRequestSH::GetHelperForLanguage(PRUint32 language,
|
|
||||||
nsISupports **_retval)
|
|
||||||
{
|
|
||||||
*_retval = nsnull;
|
|
||||||
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// nsIDOMEventListener::HandleEvent() 'this' converter helper
|
// nsIDOMEventListener::HandleEvent() 'this' converter helper
|
||||||
|
|
||||||
NS_INTERFACE_MAP_BEGIN(nsEventListenerThisTranslator)
|
NS_INTERFACE_MAP_BEGIN(nsEventListenerThisTranslator)
|
||||||
|
@ -3293,8 +3278,6 @@ nsEventListenerThisTranslator::TranslateThis(nsISupports *aInitialThis,
|
||||||
|
|
||||||
event->GetCurrentTarget(getter_AddRefs(target));
|
event->GetCurrentTarget(getter_AddRefs(target));
|
||||||
|
|
||||||
NS_WARN_IF_FALSE(target, "Hmm, null target, weird.");
|
|
||||||
|
|
||||||
*_retval = target;
|
*_retval = target;
|
||||||
NS_IF_ADDREF(*_retval);
|
NS_IF_ADDREF(*_retval);
|
||||||
|
|
||||||
|
|
|
@ -864,29 +864,6 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// XMLHttpRequest helper
|
|
||||||
|
|
||||||
class nsXMLHttpRequestSH : public nsDOMGenericSH
|
|
||||||
{
|
|
||||||
protected:
|
|
||||||
nsXMLHttpRequestSH(nsDOMClassInfoID aID) : nsDOMGenericSH(aID)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~nsXMLHttpRequestSH()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
NS_IMETHOD GetHelperForLanguage(PRUint32 language, nsISupports **_retval);
|
|
||||||
|
|
||||||
static nsIClassInfo *doCreate(nsDOMClassInfoID aID)
|
|
||||||
{
|
|
||||||
return new nsXMLHttpRequestSH(aID);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// Event handler 'this' translator class, this is called by XPConnect
|
// Event handler 'this' translator class, this is called by XPConnect
|
||||||
// when a "function interface" (nsIDOMEventListener) is called, this
|
// when a "function interface" (nsIDOMEventListener) is called, this
|
||||||
// class extracts 'this' fomr the first argument to the called
|
// class extracts 'this' fomr the first argument to the called
|
||||||
|
|
|
@ -28,22 +28,6 @@ interface nsIHttpChannel;
|
||||||
|
|
||||||
[scriptable, uuid(b7215e70-4157-11d4-9a42-000064657374)]
|
[scriptable, uuid(b7215e70-4157-11d4-9a42-000064657374)]
|
||||||
interface nsIXMLHttpRequest : nsISupports {
|
interface nsIXMLHttpRequest : nsISupports {
|
||||||
/**
|
|
||||||
* Meant to be a script-only mechanism for setting a load event listener.
|
|
||||||
* The attribute is expected to be JavaScript function object. When
|
|
||||||
* the load event occurs, the function is invoked.
|
|
||||||
* This attribute should not be used from native code!!
|
|
||||||
*/
|
|
||||||
attribute nsISupports onload;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Meant to be a script-only mechanism for setting an error event listener.
|
|
||||||
* The attribute is expected to be JavaScript function object. When
|
|
||||||
* the error event occurs, the function is invoked.
|
|
||||||
* This attribute should not be used from native code!!
|
|
||||||
*/
|
|
||||||
attribute nsISupports onerror;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The HTTP request uses an HTTP channel in order to perform the
|
* The HTTP request uses an HTTP channel in order to perform the
|
||||||
* request. This attribute represents the HTTP channel used
|
* request. This attribute represents the HTTP channel used
|
||||||
|
@ -170,6 +154,26 @@ interface nsIXMLHttpRequest : nsISupports {
|
||||||
void setRequestHeader(in string header, in string value);
|
void setRequestHeader(in string header, in string value);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
[scriptable, uuid(9deabc90-28d5-41d3-a660-474f2254f4ba)]
|
||||||
|
interface nsIJSXMLHttpRequest : nsISupports {
|
||||||
|
/**
|
||||||
|
* Meant to be a script-only mechanism for setting a load event listener.
|
||||||
|
* The attribute is expected to be JavaScript function object. When
|
||||||
|
* the load event occurs, the function is invoked.
|
||||||
|
* This attribute should not be used from native code!!
|
||||||
|
*/
|
||||||
|
attribute nsIDOMEventListener onload;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Meant to be a script-only mechanism for setting an error event listener.
|
||||||
|
* The attribute is expected to be JavaScript function object. When
|
||||||
|
* the error event occurs, the function is invoked.
|
||||||
|
* This attribute should not be used from native code!!
|
||||||
|
*/
|
||||||
|
attribute nsIDOMEventListener onerror;
|
||||||
|
};
|
||||||
|
|
||||||
%{ C++
|
%{ C++
|
||||||
#define NS_XMLHTTPREQUEST_CID \
|
#define NS_XMLHTTPREQUEST_CID \
|
||||||
{ /* d164e770-4157-11d4-9a42-000064657374 */ \
|
{ /* d164e770-4157-11d4-9a42-000064657374 */ \
|
||||||
|
|
|
@ -66,91 +66,38 @@ static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CI
|
||||||
static NS_DEFINE_IID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
static NS_DEFINE_IID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static JSContext*
|
static void
|
||||||
GetSafeContext()
|
GetCurrentContext(nsIScriptContext **aScriptContext)
|
||||||
{
|
{
|
||||||
// Get the "safe" JSContext: our JSContext of last resort
|
*aScriptContext = nsnull;
|
||||||
nsresult rv;
|
|
||||||
nsCOMPtr<nsIThreadJSContextStack> stack =
|
|
||||||
do_GetService("@mozilla.org/js/xpc/ContextStack;1", &rv);
|
|
||||||
if (NS_FAILED(rv))
|
|
||||||
return nsnull;
|
|
||||||
|
|
||||||
JSContext* cx;
|
|
||||||
if (NS_FAILED(stack->GetSafeJSContext(&cx))) {
|
|
||||||
return nsnull;
|
|
||||||
}
|
|
||||||
return cx;
|
|
||||||
}
|
|
||||||
|
|
||||||
static JSContext*
|
|
||||||
GetCurrentContext()
|
|
||||||
{
|
|
||||||
// Get JSContext from stack.
|
// Get JSContext from stack.
|
||||||
nsresult rv;
|
nsCOMPtr<nsIJSContextStack> stack =
|
||||||
NS_WITH_SERVICE(nsIJSContextStack, stack, "@mozilla.org/js/xpc/ContextStack;1",
|
do_GetService("@mozilla.org/js/xpc/ContextStack;1");
|
||||||
&rv);
|
|
||||||
if (NS_FAILED(rv))
|
if (!stack) {
|
||||||
return nsnull;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
JSContext *cx;
|
JSContext *cx;
|
||||||
if (NS_FAILED(stack->Peek(&cx)))
|
|
||||||
return nsnull;
|
|
||||||
return cx;
|
|
||||||
}
|
|
||||||
|
|
||||||
nsXMLHttpRequestScriptListener::nsXMLHttpRequestScriptListener(JSObject* aScopeObj, JSObject* aFunctionObj)
|
if (NS_FAILED(stack->Peek(&cx))) {
|
||||||
{
|
return;
|
||||||
NS_INIT_ISUPPORTS();
|
|
||||||
// We don't have to add a GC root for the scope object
|
|
||||||
// since we'll go away if it goes away
|
|
||||||
mScopeObj = aScopeObj;
|
|
||||||
mFunctionObj = aFunctionObj;
|
|
||||||
JSContext* cx;
|
|
||||||
cx = GetSafeContext();
|
|
||||||
if (cx) {
|
|
||||||
JS_AddNamedRoot(cx, &mFunctionObj, "nsXMLHttpRequest");
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
nsXMLHttpRequestScriptListener::~nsXMLHttpRequestScriptListener()
|
|
||||||
{
|
|
||||||
JSContext* cx;
|
|
||||||
cx = GetSafeContext();
|
|
||||||
if (cx) {
|
|
||||||
JS_RemoveRoot(cx, &mFunctionObj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
NS_IMPL_ISUPPORTS2(nsXMLHttpRequestScriptListener, nsIDOMEventListener, nsIPrivateJSEventListener)
|
|
||||||
|
|
||||||
NS_IMETHODIMP
|
|
||||||
nsXMLHttpRequestScriptListener::HandleEvent(nsIDOMEvent* aEvent)
|
|
||||||
{
|
|
||||||
JSContext* cx;
|
|
||||||
cx = GetCurrentContext();
|
|
||||||
if (!cx) {
|
if (!cx) {
|
||||||
cx = GetSafeContext();
|
return;
|
||||||
}
|
}
|
||||||
if (cx) {
|
|
||||||
jsval val;
|
|
||||||
|
|
||||||
// Hmmm...we can't pass along the nsIDOMEvent because
|
nsISupports *priv = (nsISupports *)::JS_GetContextPrivate(cx);
|
||||||
// we may not have the right type of context (required
|
|
||||||
// to get a JSObject from a nsIScriptObjectOwner)
|
if (!priv) {
|
||||||
JS_CallFunctionValue(cx, mScopeObj,
|
return;
|
||||||
OBJECT_TO_JSVAL(mFunctionObj),
|
|
||||||
0, nsnull, &val);
|
|
||||||
}
|
}
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
NS_IMETHODIMP
|
CallQueryInterface(priv, aScriptContext);
|
||||||
nsXMLHttpRequestScriptListener::GetFunctionObj(JSObject** aObj)
|
|
||||||
{
|
return;
|
||||||
NS_ENSURE_ARG_POINTER(aObj);
|
|
||||||
|
|
||||||
*aObj = mFunctionObj;
|
|
||||||
return NS_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/////////////////////////////////////////////
|
/////////////////////////////////////////////
|
||||||
|
@ -285,6 +232,7 @@ nsXMLHttpRequest::~nsXMLHttpRequest()
|
||||||
// XPConnect interface list for nsXMLHttpRequest
|
// XPConnect interface list for nsXMLHttpRequest
|
||||||
NS_CLASSINFO_MAP_BEGIN(XMLHttpRequest)
|
NS_CLASSINFO_MAP_BEGIN(XMLHttpRequest)
|
||||||
NS_CLASSINFO_MAP_ENTRY(nsIXMLHttpRequest)
|
NS_CLASSINFO_MAP_ENTRY(nsIXMLHttpRequest)
|
||||||
|
NS_CLASSINFO_MAP_ENTRY(nsIJSXMLHttpRequest)
|
||||||
NS_CLASSINFO_MAP_ENTRY(nsIDOMEventTarget)
|
NS_CLASSINFO_MAP_ENTRY(nsIDOMEventTarget)
|
||||||
NS_CLASSINFO_MAP_END
|
NS_CLASSINFO_MAP_END
|
||||||
|
|
||||||
|
@ -293,9 +241,9 @@ NS_CLASSINFO_MAP_END
|
||||||
NS_INTERFACE_MAP_BEGIN(nsXMLHttpRequest)
|
NS_INTERFACE_MAP_BEGIN(nsXMLHttpRequest)
|
||||||
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIXMLHttpRequest)
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIXMLHttpRequest)
|
||||||
NS_INTERFACE_MAP_ENTRY(nsIXMLHttpRequest)
|
NS_INTERFACE_MAP_ENTRY(nsIXMLHttpRequest)
|
||||||
|
NS_INTERFACE_MAP_ENTRY(nsIJSXMLHttpRequest)
|
||||||
NS_INTERFACE_MAP_ENTRY(nsIDOMLoadListener)
|
NS_INTERFACE_MAP_ENTRY(nsIDOMLoadListener)
|
||||||
NS_INTERFACE_MAP_ENTRY(nsIDOMEventTarget)
|
NS_INTERFACE_MAP_ENTRY(nsIDOMEventTarget)
|
||||||
NS_INTERFACE_MAP_ENTRY(nsISecurityCheckedComponent)
|
|
||||||
NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
|
NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
|
||||||
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
|
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
|
||||||
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
||||||
|
@ -307,7 +255,8 @@ NS_IMPL_ADDREF(nsXMLHttpRequest)
|
||||||
NS_IMPL_RELEASE(nsXMLHttpRequest)
|
NS_IMPL_RELEASE(nsXMLHttpRequest)
|
||||||
|
|
||||||
|
|
||||||
/* void addEventListener (in string type, in nsIDOMEventListener listener); */
|
/* void addEventListener (in string type, in nsIDOMEventListener
|
||||||
|
listener); */
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
nsXMLHttpRequest::AddEventListener(const nsAReadableString& type,
|
nsXMLHttpRequest::AddEventListener(const nsAReadableString& type,
|
||||||
nsIDOMEventListener *listener,
|
nsIDOMEventListener *listener,
|
||||||
|
@ -339,7 +288,8 @@ nsXMLHttpRequest::AddEventListener(const nsAReadableString& type,
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* void removeEventListener (in string type, in nsIDOMEventListener listener); */
|
/* void removeEventListener (in string type, in nsIDOMEventListener
|
||||||
|
listener); */
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
nsXMLHttpRequest::RemoveEventListener(const nsAReadableString & type,
|
nsXMLHttpRequest::RemoveEventListener(const nsAReadableString & type,
|
||||||
nsIDOMEventListener *listener,
|
nsIDOMEventListener *listener,
|
||||||
|
@ -373,209 +323,46 @@ nsXMLHttpRequest::DispatchEvent(nsIDOMEvent *evt)
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
nsresult
|
|
||||||
nsXMLHttpRequest::MakeScriptEventListener(nsISupports* aObject,
|
|
||||||
nsIDOMEventListener** aListener)
|
|
||||||
{
|
|
||||||
nsresult rv;
|
|
||||||
|
|
||||||
*aListener = nsnull;
|
|
||||||
|
|
||||||
nsCOMPtr<nsIXPCNativeCallContext> cc;
|
|
||||||
NS_WITH_SERVICE(nsIXPConnect, xpc, nsIXPConnect::GetCID(), &rv);
|
|
||||||
if(NS_SUCCEEDED(rv)) {
|
|
||||||
rv = xpc->GetCurrentNativeCallContext(getter_AddRefs(cc));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (NS_SUCCEEDED(rv) && cc) {
|
|
||||||
nsCOMPtr<nsIXPConnectJSObjectHolder> jsobjholder(do_QueryInterface(aObject));
|
|
||||||
if (jsobjholder) {
|
|
||||||
JSObject* funobj;
|
|
||||||
rv = jsobjholder->GetJSObject(&funobj);
|
|
||||||
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
|
|
||||||
|
|
||||||
JSContext* cx;
|
|
||||||
rv = cc->GetJSContext(&cx);
|
|
||||||
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
|
|
||||||
|
|
||||||
JSFunction* fun = JS_ValueToFunction(cx, OBJECT_TO_JSVAL(funobj));
|
|
||||||
if (!fun) {
|
|
||||||
return NS_ERROR_INVALID_ARG;
|
|
||||||
}
|
|
||||||
|
|
||||||
nsCOMPtr<nsIXPConnectWrappedNative> wrapper;
|
|
||||||
rv = cc->GetCalleeWrapper(getter_AddRefs(wrapper));
|
|
||||||
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
|
|
||||||
|
|
||||||
JSObject* scopeobj;
|
|
||||||
rv = wrapper->GetJSObject(&scopeobj);
|
|
||||||
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
|
|
||||||
|
|
||||||
nsXMLHttpRequestScriptListener* listener = new nsXMLHttpRequestScriptListener(scopeobj, funobj);
|
|
||||||
if (!listener) {
|
|
||||||
return NS_ERROR_OUT_OF_MEMORY;
|
|
||||||
}
|
|
||||||
*aListener = listener;
|
|
||||||
NS_ADDREF(*aListener);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
static PRBool
|
|
||||||
CheckForScriptListener(nsISupports* aElement, void *aData)
|
|
||||||
{
|
|
||||||
nsCOMPtr<nsIPrivateJSEventListener> jsel(do_QueryInterface(aElement));
|
|
||||||
if (jsel) {
|
|
||||||
nsIDOMEventListener** retval = (nsIDOMEventListener**)aData;
|
|
||||||
|
|
||||||
aElement->QueryInterface(NS_GET_IID(nsIDOMEventListener), (void**)retval);
|
|
||||||
return PR_FALSE;
|
|
||||||
}
|
|
||||||
return PR_TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
nsXMLHttpRequest::GetScriptEventListener(nsISupportsArray* aList,
|
|
||||||
nsIDOMEventListener** aListener)
|
|
||||||
{
|
|
||||||
aList->EnumerateForwards(CheckForScriptListener, (void*)aListener);
|
|
||||||
}
|
|
||||||
|
|
||||||
PRBool
|
|
||||||
nsXMLHttpRequest::StuffReturnValue(nsIDOMEventListener* aListener)
|
|
||||||
{
|
|
||||||
nsresult rv;
|
|
||||||
|
|
||||||
nsCOMPtr<nsIXPCNativeCallContext> cc;
|
|
||||||
NS_WITH_SERVICE(nsIXPConnect, xpc, nsIXPConnect::GetCID(), &rv);
|
|
||||||
if(NS_SUCCEEDED(rv)) {
|
|
||||||
rv = xpc->GetCurrentNativeCallContext(getter_AddRefs(cc));
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we're being called through JS, stuff the return value
|
|
||||||
if (NS_SUCCEEDED(rv) && cc) {
|
|
||||||
jsval* val;
|
|
||||||
rv = cc->GetRetValPtr(&val);
|
|
||||||
if (NS_SUCCEEDED(rv)) {
|
|
||||||
JSObject* obj;
|
|
||||||
nsCOMPtr<nsIPrivateJSEventListener> jsel(do_QueryInterface(aListener));
|
|
||||||
if (jsel) {
|
|
||||||
jsel->GetFunctionObj(&obj);
|
|
||||||
*val = OBJECT_TO_JSVAL(obj);
|
|
||||||
cc->SetReturnValueWasSet(JS_TRUE);
|
|
||||||
return PR_TRUE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return PR_FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* attribute nsIDOMEventListener onload; */
|
/* attribute nsIDOMEventListener onload; */
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
nsXMLHttpRequest::GetOnload(nsISupports * *aOnLoad)
|
nsXMLHttpRequest::GetOnload(nsIDOMEventListener * *aOnLoad)
|
||||||
{
|
{
|
||||||
NS_ENSURE_ARG_POINTER(aOnLoad);
|
NS_ENSURE_ARG_POINTER(aOnLoad);
|
||||||
|
|
||||||
*aOnLoad = nsnull;
|
*aOnLoad = mOnLoadListener;
|
||||||
if (mLoadEventListeners) {
|
|
||||||
nsCOMPtr<nsIDOMEventListener> listener;
|
|
||||||
|
|
||||||
GetScriptEventListener(mLoadEventListeners, getter_AddRefs(listener));
|
|
||||||
if (listener) {
|
|
||||||
StuffReturnValue(listener);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
nsXMLHttpRequest::SetOnload(nsISupports * aOnLoad)
|
nsXMLHttpRequest::SetOnload(nsIDOMEventListener * aOnLoad)
|
||||||
{
|
{
|
||||||
NS_ENSURE_ARG(aOnLoad);
|
mOnLoadListener = aOnLoad;
|
||||||
|
|
||||||
nsresult rv;
|
GetCurrentContext(getter_AddRefs(mScriptContext));
|
||||||
nsCOMPtr<nsIDOMEventListener> listener;
|
|
||||||
|
|
||||||
rv = MakeScriptEventListener(aOnLoad, getter_AddRefs(listener));
|
return NS_OK;
|
||||||
if (NS_FAILED(rv)) return rv;
|
|
||||||
|
|
||||||
if (listener) {
|
|
||||||
nsCOMPtr<nsIDOMEventListener> oldListener;
|
|
||||||
|
|
||||||
// Remove any old script event listener that exists since
|
|
||||||
// we can only have one
|
|
||||||
if (mLoadEventListeners) {
|
|
||||||
GetScriptEventListener(mLoadEventListeners, getter_AddRefs(oldListener));
|
|
||||||
RemoveEventListener(LOADSTR, oldListener, PR_TRUE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// If it's not a script event listener, try to directly QI it to
|
|
||||||
// an actual event listener
|
|
||||||
listener = do_QueryInterface(aOnLoad);
|
|
||||||
if (!listener) {
|
|
||||||
return NS_ERROR_INVALID_ARG;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return AddEventListener(LOADSTR, listener, PR_TRUE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* attribute nsIDOMEventListener onerror; */
|
/* attribute nsIDOMEventListener onerror; */
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
nsXMLHttpRequest::GetOnerror(nsISupports * *aOnerror)
|
nsXMLHttpRequest::GetOnerror(nsIDOMEventListener * *aOnerror)
|
||||||
{
|
{
|
||||||
NS_ENSURE_ARG_POINTER(aOnerror);
|
NS_ENSURE_ARG_POINTER(aOnerror);
|
||||||
|
|
||||||
*aOnerror = nsnull;
|
*aOnerror = mOnErrorListener;
|
||||||
if (mErrorEventListeners) {
|
|
||||||
nsCOMPtr<nsIDOMEventListener> listener;
|
|
||||||
|
|
||||||
GetScriptEventListener(mErrorEventListeners, getter_AddRefs(listener));
|
|
||||||
if (listener) {
|
|
||||||
StuffReturnValue(listener);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
nsXMLHttpRequest::SetOnerror(nsISupports * aOnerror)
|
nsXMLHttpRequest::SetOnerror(nsIDOMEventListener * aOnerror)
|
||||||
{
|
{
|
||||||
NS_ENSURE_ARG(aOnerror);
|
mOnErrorListener = aOnerror;
|
||||||
|
|
||||||
nsresult rv;
|
GetCurrentContext(getter_AddRefs(mScriptContext));
|
||||||
nsCOMPtr<nsIDOMEventListener> listener;
|
|
||||||
|
|
||||||
rv = MakeScriptEventListener(aOnerror, getter_AddRefs(listener));
|
return NS_OK;
|
||||||
if (NS_FAILED(rv)) return rv;
|
|
||||||
|
|
||||||
if (listener) {
|
|
||||||
nsCOMPtr<nsIDOMEventListener> oldListener;
|
|
||||||
|
|
||||||
// Remove any old script event listener that exists since
|
|
||||||
// we can only have one
|
|
||||||
if (mErrorEventListeners) {
|
|
||||||
GetScriptEventListener(mErrorEventListeners,
|
|
||||||
getter_AddRefs(oldListener));
|
|
||||||
RemoveEventListener(ERRORSTR, oldListener, PR_TRUE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// If it's not a script event listener, try to directly QI it to
|
|
||||||
// an actual event listener
|
|
||||||
listener = do_QueryInterface(aOnerror);
|
|
||||||
if (!listener) {
|
|
||||||
return NS_ERROR_INVALID_ARG;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return AddEventListener(ERRORSTR, listener, PR_TRUE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* readonly attribute nsIHttpChannel channel; */
|
/* readonly attribute nsIHttpChannel channel; */
|
||||||
|
@ -1366,21 +1153,47 @@ nsXMLHttpRequest::Load(nsIDOMEvent* aEvent)
|
||||||
mChromeWindow = 0;
|
mChromeWindow = 0;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
nsCOMPtr<nsIJSContextStack> stack;
|
||||||
|
JSContext *cx = nsnull;
|
||||||
|
|
||||||
|
if (mScriptContext) {
|
||||||
|
stack = do_GetService("@mozilla.org/js/xpc/ContextStack;1");
|
||||||
|
|
||||||
|
if (stack) {
|
||||||
|
cx = (JSContext *)mScriptContext->GetNativeContext();
|
||||||
|
|
||||||
|
if (cx) {
|
||||||
|
stack->Push(cx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mOnLoadListener) {
|
||||||
|
mOnLoadListener->HandleEvent(aEvent);
|
||||||
|
}
|
||||||
|
|
||||||
if (mLoadEventListeners) {
|
if (mLoadEventListeners) {
|
||||||
PRUint32 index, count;
|
PRUint32 index, count;
|
||||||
|
|
||||||
mLoadEventListeners->Count(&count);
|
mLoadEventListeners->Count(&count);
|
||||||
for (index = 0; index < count; index++) {
|
for (index = 0; index < count; index++) {
|
||||||
nsCOMPtr<nsISupports> current = dont_AddRef(mLoadEventListeners->ElementAt(index));
|
nsCOMPtr<nsIDOMEventListener> listener;
|
||||||
if (current) {
|
|
||||||
nsCOMPtr<nsIDOMEventListener> listener(do_QueryInterface(current));
|
mLoadEventListeners->QueryElementAt(index,
|
||||||
if (listener) {
|
NS_GET_IID(nsIDOMEventListener),
|
||||||
listener->HandleEvent(aEvent);
|
getter_AddRefs(listener));
|
||||||
}
|
|
||||||
|
if (listener) {
|
||||||
|
listener->HandleEvent(aEvent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (cx) {
|
||||||
|
stack->Pop(&cx);
|
||||||
|
}
|
||||||
|
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1414,65 +1227,45 @@ nsXMLHttpRequest::Error(nsIDOMEvent* aEvent)
|
||||||
mChromeWindow = 0;
|
mChromeWindow = 0;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
nsCOMPtr<nsIJSContextStack> stack;
|
||||||
|
JSContext *cx = nsnull;
|
||||||
|
|
||||||
|
if (mScriptContext) {
|
||||||
|
stack = do_GetService("@mozilla.org/js/xpc/ContextStack;1");
|
||||||
|
|
||||||
|
if (stack) {
|
||||||
|
cx = (JSContext *)mScriptContext->GetNativeContext();
|
||||||
|
|
||||||
|
if (cx) {
|
||||||
|
stack->Push(cx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mOnErrorListener) {
|
||||||
|
mOnErrorListener->HandleEvent(aEvent);
|
||||||
|
}
|
||||||
|
|
||||||
if (mErrorEventListeners) {
|
if (mErrorEventListeners) {
|
||||||
PRUint32 index, count;
|
PRUint32 index, count;
|
||||||
|
|
||||||
mErrorEventListeners->Count(&count);
|
mErrorEventListeners->Count(&count);
|
||||||
for (index = 0; index < count; index++) {
|
for (index = 0; index < count; index++) {
|
||||||
nsCOMPtr<nsISupports> current = dont_AddRef(mErrorEventListeners->ElementAt(index));
|
nsCOMPtr<nsIDOMEventListener> listener;
|
||||||
if (current) {
|
|
||||||
nsCOMPtr<nsIDOMEventListener> listener(do_QueryInterface(current));
|
mErrorEventListeners->QueryElementAt(index,
|
||||||
if (listener) {
|
NS_GET_IID(nsIDOMEventListener),
|
||||||
listener->HandleEvent(aEvent);
|
getter_AddRefs(listener));
|
||||||
}
|
|
||||||
|
if (listener) {
|
||||||
|
listener->HandleEvent(aEvent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return NS_OK;
|
if (cx) {
|
||||||
}
|
stack->Pop(&cx);
|
||||||
|
|
||||||
static const char* kAllAccess = "AllAccess";
|
|
||||||
|
|
||||||
/* string canCreateWrapper (in nsIIDPtr iid); */
|
|
||||||
NS_IMETHODIMP
|
|
||||||
nsXMLHttpRequest::CanCreateWrapper(const nsIID * iid, char **_retval)
|
|
||||||
{
|
|
||||||
if (iid->Equals(NS_GET_IID(nsIXMLHttpRequest))) {
|
|
||||||
*_retval = nsCRT::strdup(kAllAccess);
|
|
||||||
}
|
|
||||||
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* string canCallMethod (in nsIIDPtr iid, in wstring methodName); */
|
|
||||||
NS_IMETHODIMP
|
|
||||||
nsXMLHttpRequest::CanCallMethod(const nsIID * iid, const PRUnichar *methodName, char **_retval)
|
|
||||||
{
|
|
||||||
if (iid->Equals(NS_GET_IID(nsIXMLHttpRequest))) {
|
|
||||||
*_retval = nsCRT::strdup(kAllAccess);
|
|
||||||
}
|
|
||||||
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* string canGetProperty (in nsIIDPtr iid, in wstring propertyName); */
|
|
||||||
NS_IMETHODIMP
|
|
||||||
nsXMLHttpRequest::CanGetProperty(const nsIID * iid, const PRUnichar *propertyName, char **_retval)
|
|
||||||
{
|
|
||||||
if (iid->Equals(NS_GET_IID(nsIXMLHttpRequest))) {
|
|
||||||
*_retval = nsCRT::strdup(kAllAccess);
|
|
||||||
}
|
|
||||||
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* string canSetProperty (in nsIIDPtr iid, in wstring propertyName); */
|
|
||||||
NS_IMETHODIMP
|
|
||||||
nsXMLHttpRequest::CanSetProperty(const nsIID * iid, const PRUnichar *propertyName, char **_retval)
|
|
||||||
{
|
|
||||||
if (iid->Equals(NS_GET_IID(nsIXMLHttpRequest))) {
|
|
||||||
*_retval = nsCRT::strdup(kAllAccess);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
|
|
|
@ -32,7 +32,6 @@
|
||||||
#include "nsIDOMLoadListener.h"
|
#include "nsIDOMLoadListener.h"
|
||||||
#include "nsIDOMEventTarget.h"
|
#include "nsIDOMEventTarget.h"
|
||||||
#include "nsIDOMDocument.h"
|
#include "nsIDOMDocument.h"
|
||||||
#include "nsISecurityCheckedComponent.h"
|
|
||||||
#include "nsIURI.h"
|
#include "nsIURI.h"
|
||||||
#include "nsIHttpChannel.h"
|
#include "nsIHttpChannel.h"
|
||||||
#include "nsIDocument.h"
|
#include "nsIDocument.h"
|
||||||
|
@ -43,6 +42,8 @@
|
||||||
#include "nsWeakReference.h"
|
#include "nsWeakReference.h"
|
||||||
#include "nsISupportsArray.h"
|
#include "nsISupportsArray.h"
|
||||||
#include "jsapi.h"
|
#include "jsapi.h"
|
||||||
|
#include "nsIScriptContext.h"
|
||||||
|
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
XML_HTTP_REQUEST_INITIALIZED,
|
XML_HTTP_REQUEST_INITIALIZED,
|
||||||
|
@ -53,9 +54,9 @@ enum {
|
||||||
};
|
};
|
||||||
|
|
||||||
class nsXMLHttpRequest : public nsIXMLHttpRequest,
|
class nsXMLHttpRequest : public nsIXMLHttpRequest,
|
||||||
|
public nsIJSXMLHttpRequest,
|
||||||
public nsIDOMLoadListener,
|
public nsIDOMLoadListener,
|
||||||
public nsIDOMEventTarget,
|
public nsIDOMEventTarget,
|
||||||
public nsISecurityCheckedComponent,
|
|
||||||
public nsIStreamListener,
|
public nsIStreamListener,
|
||||||
public nsSupportsWeakReference
|
public nsSupportsWeakReference
|
||||||
{
|
{
|
||||||
|
@ -68,6 +69,9 @@ public:
|
||||||
// nsIXMLHttpRequest
|
// nsIXMLHttpRequest
|
||||||
NS_DECL_NSIXMLHTTPREQUEST
|
NS_DECL_NSIXMLHTTPREQUEST
|
||||||
|
|
||||||
|
// nsIJSXMLHttpRequest
|
||||||
|
NS_DECL_NSIJSXMLHTTPREQUEST
|
||||||
|
|
||||||
// nsIDOMEventTarget
|
// nsIDOMEventTarget
|
||||||
NS_DECL_NSIDOMEVENTTARGET
|
NS_DECL_NSIDOMEVENTTARGET
|
||||||
|
|
||||||
|
@ -80,9 +84,6 @@ public:
|
||||||
NS_IMETHOD Abort(nsIDOMEvent* aEvent);
|
NS_IMETHOD Abort(nsIDOMEvent* aEvent);
|
||||||
NS_IMETHOD Error(nsIDOMEvent* aEvent);
|
NS_IMETHOD Error(nsIDOMEvent* aEvent);
|
||||||
|
|
||||||
// nsISecurityCheckedComponent
|
|
||||||
NS_DECL_NSISECURITYCHECKEDCOMPONENT
|
|
||||||
|
|
||||||
// nsIStreamListener
|
// nsIStreamListener
|
||||||
NS_DECL_NSISTREAMLISTENER
|
NS_DECL_NSISTREAMLISTENER
|
||||||
|
|
||||||
|
@ -90,13 +91,6 @@ public:
|
||||||
NS_DECL_NSIREQUESTOBSERVER
|
NS_DECL_NSIREQUESTOBSERVER
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
nsresult MakeScriptEventListener(nsISupports* aObject,
|
|
||||||
nsIDOMEventListener** aListener);
|
|
||||||
void GetScriptEventListener(nsISupportsArray* aList,
|
|
||||||
nsIDOMEventListener** aListener);
|
|
||||||
PRBool StuffReturnValue(nsIDOMEventListener* aListener);
|
|
||||||
|
|
||||||
|
|
||||||
nsresult GetStreamForWString(const PRUnichar* aStr,
|
nsresult GetStreamForWString(const PRUnichar* aStr,
|
||||||
PRInt32 aLength,
|
PRInt32 aLength,
|
||||||
nsIInputStream** aStream);
|
nsIInputStream** aStream);
|
||||||
|
@ -109,8 +103,13 @@ protected:
|
||||||
#ifdef IMPLEMENT_SYNC_LOAD
|
#ifdef IMPLEMENT_SYNC_LOAD
|
||||||
nsCOMPtr<nsIWebBrowserChrome> mChromeWindow;
|
nsCOMPtr<nsIWebBrowserChrome> mChromeWindow;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
nsCOMPtr<nsISupportsArray> mLoadEventListeners;
|
nsCOMPtr<nsISupportsArray> mLoadEventListeners;
|
||||||
nsCOMPtr<nsISupportsArray> mErrorEventListeners;
|
nsCOMPtr<nsISupportsArray> mErrorEventListeners;
|
||||||
|
nsCOMPtr<nsIScriptContext> mScriptContext;
|
||||||
|
|
||||||
|
nsCOMPtr<nsIDOMEventListener> mOnLoadListener;
|
||||||
|
nsCOMPtr<nsIDOMEventListener> mOnErrorListener;
|
||||||
|
|
||||||
nsresult DetectCharset(nsAWritableString& aCharset);
|
nsresult DetectCharset(nsAWritableString& aCharset);
|
||||||
nsresult ConvertBodyToText(PRUnichar **aOutBuffer);
|
nsresult ConvertBodyToText(PRUnichar **aOutBuffer);
|
||||||
|
@ -141,36 +140,4 @@ protected:
|
||||||
PRBool mAsync;
|
PRBool mAsync;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define NS_IPRIVATEJSEVENTLISTENER_IID \
|
|
||||||
{ /* d47a6550-4327-11d4-9a45-000064657374 */ \
|
|
||||||
0xd47a6550, 0x4327, 0x11d4, \
|
|
||||||
{0x9a, 0x45, 0x00, 0x00, 0x64, 0x65, 0x73, 0x74} }
|
|
||||||
|
|
||||||
class nsIPrivateJSEventListener : public nsISupports {
|
|
||||||
public:
|
|
||||||
NS_DEFINE_STATIC_IID_ACCESSOR(NS_IPRIVATEJSEVENTLISTENER_IID)
|
|
||||||
|
|
||||||
NS_IMETHOD GetFunctionObj(JSObject** aObj) = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class nsXMLHttpRequestScriptListener : public nsIDOMEventListener,
|
|
||||||
public nsIPrivateJSEventListener
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
nsXMLHttpRequestScriptListener(JSObject* aScopeObj, JSObject* aFunctionObj);
|
|
||||||
virtual ~nsXMLHttpRequestScriptListener();
|
|
||||||
|
|
||||||
NS_DECL_ISUPPORTS
|
|
||||||
|
|
||||||
// nsIDOMEventListener
|
|
||||||
NS_IMETHOD HandleEvent(nsIDOMEvent* aEvent);
|
|
||||||
|
|
||||||
// nsIPrivateJSEventListener
|
|
||||||
NS_IMETHOD GetFunctionObj(JSObject** aObj);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
JSObject* mScopeObj;
|
|
||||||
JSObject* mFunctionObj;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Загрузка…
Ссылка в новой задаче