Bug 458320 - 'Workers: Support synchronous events from within XHR methods'. r+sr=jst.

This commit is contained in:
Ben Turner 2008-10-14 11:16:37 -07:00
Родитель 09633b3c46
Коммит 178b8b7405
3 изменённых файлов: 107 добавлений и 69 удалений

Просмотреть файл

@ -40,16 +40,13 @@
#define __NSDOMWORKERXHRPROXIEDFUNCTIONS_H__
#define MAKE_PROXIED_FUNCTION0(_name) \
class _name : public nsRunnable \
class _name : public SyncEventCapturingRunnable \
{ \
public: \
_name (nsDOMWorkerXHRProxy* aXHR) \
: mXHR(aXHR) \
{ \
NS_ASSERTION(aXHR, "Null pointer!"); \
} \
_name (nsDOMWorkerXHRProxy* aXHR, SyncEventQueue* aQueue) \
: SyncEventCapturingRunnable(aXHR, aQueue) { } \
\
NS_IMETHOD Run() \
virtual nsresult RunInternal() \
{ \
nsCOMPtr<nsIXMLHttpRequest> xhr = mXHR->GetXMLHttpRequest(); \
if (xhr) { \
@ -57,21 +54,16 @@
} \
return NS_OK; \
} \
private: \
nsRefPtr<nsDOMWorkerXHRProxy> mXHR; \
}
#define MAKE_PROXIED_FUNCTION1(_name, _arg1) \
class _name : public nsRunnable \
class _name : public SyncEventCapturingRunnable \
{ \
public: \
_name (nsDOMWorkerXHRProxy* aXHR, _arg1 aArg1) \
: mXHR(aXHR), mArg1(aArg1) \
{ \
NS_ASSERTION(aXHR, "Null pointer!"); \
} \
_name (nsDOMWorkerXHRProxy* aXHR, SyncEventQueue* aQueue, _arg1 aArg1) \
: SyncEventCapturingRunnable(aXHR, aQueue), mArg1(aArg1) { } \
\
NS_IMETHOD Run() \
virtual nsresult RunInternal() \
{ \
nsCOMPtr<nsIXMLHttpRequest> xhr = mXHR->GetXMLHttpRequest(); \
if (xhr) { \
@ -80,21 +72,18 @@
return NS_OK; \
} \
private: \
nsRefPtr<nsDOMWorkerXHRProxy> mXHR; \
_arg1 mArg1; \
}
#define MAKE_PROXIED_FUNCTION2(_name, _arg1, _arg2) \
class _name : public nsRunnable \
class _name : public SyncEventCapturingRunnable \
{ \
public: \
_name (nsDOMWorkerXHRProxy* aXHR, _arg1 aArg1, _arg2 aArg2) \
: mXHR(aXHR), mArg1(aArg1), mArg2(aArg2) \
{ \
NS_ASSERTION(aXHR, "Null pointer!"); \
} \
_name (nsDOMWorkerXHRProxy* aXHR, SyncEventQueue* aQueue, _arg1 aArg1, \
_arg2 aArg2) \
: SyncEventCapturingRunnable(aXHR, aQueue), mArg1(aArg1), mArg2(aArg2) { } \
\
NS_IMETHOD Run() \
virtual nsresult RunInternal() \
{ \
nsCOMPtr<nsIXMLHttpRequest> xhr = mXHR->GetXMLHttpRequest(); \
if (xhr) { \
@ -103,22 +92,20 @@
return NS_OK; \
} \
private: \
nsRefPtr<nsDOMWorkerXHRProxy> mXHR; \
_arg1 mArg1; \
_arg2 mArg2; \
}
#define MAKE_PROXIED_FUNCTION3(_name, _arg1, _arg2, _arg3) \
class _name : public nsRunnable \
class _name : public SyncEventCapturingRunnable \
{ \
public: \
_name (nsDOMWorkerXHRProxy* aXHR, _arg1 aArg1, _arg2 aArg2, _arg3 aArg3) \
: mXHR(aXHR), mArg1(aArg1), mArg2(aArg2), mArg3(aArg3) \
{ \
NS_ASSERTION(aXHR, "Null pointer!"); \
} \
_name (nsDOMWorkerXHRProxy* aXHR, SyncEventQueue* aQueue, _arg1 aArg1, \
_arg2 aArg2, _arg3 aArg3) \
: SyncEventCapturingRunnable(aXHR, aQueue), mArg1(aArg1), mArg2(aArg2), \
mArg3(aArg3) { } \
\
NS_IMETHOD Run() \
virtual nsresult RunInternal() \
{ \
nsCOMPtr<nsIXMLHttpRequest> xhr = mXHR->GetXMLHttpRequest(); \
if (xhr) { \
@ -127,24 +114,21 @@
return NS_OK; \
} \
private: \
nsRefPtr<nsDOMWorkerXHRProxy> mXHR; \
_arg1 mArg1; \
_arg2 mArg2; \
_arg3 mArg3; \
}
#define MAKE_PROXIED_FUNCTION4(_name, _arg1, _arg2, _arg3, _arg4) \
class _name : public nsRunnable \
class _name : public SyncEventCapturingRunnable \
{ \
public: \
_name (nsDOMWorkerXHRProxy* aXHR, _arg1 aArg1, _arg2 aArg2, _arg3 aArg3, \
_arg4 aArg4) \
: mXHR(aXHR), mArg1(aArg1), mArg2(aArg2), mArg3(aArg3), mArg4(aArg4) \
{ \
NS_ASSERTION(aXHR, "Null pointer!"); \
} \
_name (nsDOMWorkerXHRProxy* aXHR, SyncEventQueue* aQueue, _arg1 aArg1, \
_arg2 aArg2, _arg3 aArg3, _arg4 aArg4) \
: SyncEventCapturingRunnable(aXHR, aQueue), mArg1(aArg1), mArg2(aArg2), \
mArg3(aArg3), mArg4(aArg4) { } \
\
NS_IMETHOD Run() \
virtual nsresult RunInternal() \
{ \
nsCOMPtr<nsIXMLHttpRequest> xhr = mXHR->GetXMLHttpRequest(); \
if (xhr) { \
@ -153,7 +137,6 @@
return NS_OK; \
} \
private: \
nsRefPtr<nsDOMWorkerXHRProxy> mXHR; \
_arg1 mArg1; \
_arg2 mArg2; \
_arg3 mArg3; \
@ -161,18 +144,15 @@
}
#define MAKE_PROXIED_FUNCTION5(_name, _arg1, _arg2, _arg3, _arg4, _arg5) \
class _name : public nsRunnable \
class _name : public SyncEventCapturingRunnable \
{ \
public: \
_name (nsDOMWorkerXHRProxy* aXHR, _arg1 aArg1, _arg2 aArg2, _arg3 aArg3, \
_arg4 aArg4, _arg5 aArg5) \
: mXHR(aXHR), mArg1(aArg1), mArg2(aArg2), mArg3(aArg3), mArg4(aArg4), \
mArg5(aArg5) \
{ \
NS_ASSERTION(aXHR, "Null pointer!"); \
} \
_name (nsDOMWorkerXHRProxy* aXHR, SyncEventQueue* aQueue, _arg1 aArg1, \
_arg2 aArg2, _arg3 aArg3, _arg4 aArg4, _arg5 aArg5) \
: SyncEventCapturingRunnable(aXHR, aQueue), mArg1(aArg1), mArg2(aArg2), \
mArg3(aArg3), mArg4(aArg4), mArg5(aArg5) { } \
\
NS_IMETHOD Run() \
virtual nsresult RunInternal() \
{ \
nsCOMPtr<nsIXMLHttpRequest> xhr = mXHR->GetXMLHttpRequest(); \
if (xhr) { \
@ -181,7 +161,6 @@
return NS_OK; \
} \
private: \
nsRefPtr<nsDOMWorkerXHRProxy> mXHR; \
_arg1 mArg1; \
_arg2 mArg2; \
_arg3 mArg3; \
@ -191,9 +170,12 @@
#define RUN_PROXIED_FUNCTION(_name, _args) \
PR_BEGIN_MACRO \
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!"); \
\
if (mCanceled) { \
return NS_ERROR_ABORT; \
} \
SyncEventQueue queue; \
\
nsCOMPtr<nsIRunnable> method = new :: _name _args; \
NS_ENSURE_TRUE(method, NS_ERROR_OUT_OF_MEMORY); \
@ -203,6 +185,12 @@
NS_ENSURE_TRUE(runnable, NS_ERROR_OUT_OF_MEMORY); \
\
nsresult _rv = runnable->Dispatch(); \
\
PRUint32 queueLength = queue.Length(); \
for (PRUint32 index = 0; index < queueLength; index++) { \
queue[index]->Run(); \
} \
\
if (NS_FAILED(_rv)) { \
return _rv; \
} \
@ -210,20 +198,44 @@
namespace nsDOMWorkerProxiedXHRFunctions
{
class Abort : public nsRunnable
typedef nsDOMWorkerXHRProxy::SyncEventQueue SyncEventQueue;
class SyncEventCapturingRunnable : public nsRunnable
{
public:
Abort (nsDOMWorkerXHRProxy* aXHR)
: mXHR(aXHR)
{
SyncEventCapturingRunnable(nsDOMWorkerXHRProxy* aXHR,
SyncEventQueue* aQueue)
: mXHR(aXHR), mQueue(aQueue) {
NS_ASSERTION(aXHR, "Null pointer!");
NS_ASSERTION(aQueue, "Null pointer!");
}
virtual nsresult RunInternal() = 0;
NS_IMETHOD Run() {
SyncEventQueue* oldQueue = mXHR->SetSyncEventQueue(mQueue);
nsresult rv = RunInternal();
mXHR->SetSyncEventQueue(oldQueue);
return rv;
}
protected:
nsRefPtr<nsDOMWorkerXHRProxy> mXHR;
SyncEventQueue* mQueue;
};
class Abort : public SyncEventCapturingRunnable
{
public:
Abort (nsDOMWorkerXHRProxy* aXHR, SyncEventQueue* aQueue)
: SyncEventCapturingRunnable(aXHR, aQueue) { }
virtual nsresult RunInternal() {
return mXHR->Abort();
}
private:
nsRefPtr<nsDOMWorkerXHRProxy> mXHR;
};
MAKE_PROXIED_FUNCTION1(GetAllResponseHeaders, char**);

Просмотреть файл

@ -442,6 +442,7 @@ nsDOMWorkerXHRProxy::nsDOMWorkerXHRProxy(nsDOMWorkerXHR* aWorkerXHR)
mXHR(nsnull),
mConcreteXHR(nsnull),
mUpload(nsnull),
mSyncEventQueue(nsnull),
mOwnedByXHR(PR_FALSE),
mMultipart(PR_FALSE),
mCanceled(PR_FALSE)
@ -910,8 +911,18 @@ nsDOMWorkerXHRProxy::HandleEvent(nsIDOMEvent* aEvent)
nsresult rv = newEvent->Init(aEvent);
NS_ENSURE_SUCCESS(rv, rv);
rv = nsDOMThreadService::get()->Dispatch(mWorkerXHR->mWorker, newEvent);
NS_ENSURE_SUCCESS(rv, rv);
// If we're supposed to be capturing events for synchronous execution then
// place this event in the queue. Otherwise schedule it for the worker via
// the thread service.
if (mSyncEventQueue) {
nsCOMPtr<nsIRunnable>* newElement =
mSyncEventQueue->AppendElement(newEvent);
NS_ENSURE_TRUE(newElement, NS_ERROR_OUT_OF_MEMORY);
}
else {
rv = nsDOMThreadService::get()->Dispatch(mWorkerXHR->mWorker, newEvent);
NS_ENSURE_SUCCESS(rv, rv);
}
return NS_OK;
}
@ -932,14 +943,23 @@ nsDOMWorkerXHRProxy::Abort()
return xhr->Abort();
}
RUN_PROXIED_FUNCTION(Abort, (this));
RUN_PROXIED_FUNCTION(Abort, (this, &queue));
return NS_OK;
}
nsDOMWorkerXHRProxy::SyncEventQueue*
nsDOMWorkerXHRProxy::SetSyncEventQueue(SyncEventQueue* aQueue)
{
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
SyncEventQueue* oldQueue = mSyncEventQueue;
mSyncEventQueue = aQueue;
return oldQueue;
}
nsresult
nsDOMWorkerXHRProxy::GetAllResponseHeaders(char** _retval)
{
RUN_PROXIED_FUNCTION(GetAllResponseHeaders, (this, _retval));
RUN_PROXIED_FUNCTION(GetAllResponseHeaders, (this, &queue, _retval));
return NS_OK;
}
@ -947,7 +967,7 @@ nsresult
nsDOMWorkerXHRProxy::GetResponseHeader(const nsACString& aHeader,
nsACString& _retval)
{
RUN_PROXIED_FUNCTION(GetResponseHeader, (this, aHeader, _retval));
RUN_PROXIED_FUNCTION(GetResponseHeader, (this, &queue, aHeader, _retval));
return NS_OK;
}
@ -958,7 +978,7 @@ nsDOMWorkerXHRProxy::OpenRequest(const nsACString& aMethod,
const nsAString& aUser,
const nsAString& aPassword)
{
RUN_PROXIED_FUNCTION(OpenRequest, (this, aMethod, aUrl, aAsync, aUser,
RUN_PROXIED_FUNCTION(OpenRequest, (this, &queue, aMethod, aUrl, aAsync, aUser,
aPassword));
return NS_OK;
}
@ -966,14 +986,14 @@ nsDOMWorkerXHRProxy::OpenRequest(const nsACString& aMethod,
nsresult
nsDOMWorkerXHRProxy::Send(nsIVariant* aBody)
{
RUN_PROXIED_FUNCTION(Send, (this, aBody));
RUN_PROXIED_FUNCTION(Send, (this, &queue, aBody));
return NS_OK;
}
nsresult
nsDOMWorkerXHRProxy::SendAsBinary(const nsAString& aBody)
{
RUN_PROXIED_FUNCTION(SendAsBinary, (this, aBody));
RUN_PROXIED_FUNCTION(SendAsBinary, (this, &queue, aBody));
return NS_OK;
}
@ -981,14 +1001,14 @@ nsresult
nsDOMWorkerXHRProxy::SetRequestHeader(const nsACString& aHeader,
const nsACString& aValue)
{
RUN_PROXIED_FUNCTION(SetRequestHeader, (this, aHeader, aValue));
RUN_PROXIED_FUNCTION(SetRequestHeader, (this, &queue, aHeader, aValue));
return NS_OK;
}
nsresult
nsDOMWorkerXHRProxy::OverrideMimeType(const nsACString& aMimetype)
{
RUN_PROXIED_FUNCTION(OverrideMimeType, (this, aMimetype));
RUN_PROXIED_FUNCTION(OverrideMimeType, (this, &queue, aMimetype));
return NS_OK;
}
@ -1006,7 +1026,7 @@ nsDOMWorkerXHRProxy::GetMultipart(PRBool* aMultipart)
nsresult
nsDOMWorkerXHRProxy::SetMultipart(PRBool aMultipart)
{
RUN_PROXIED_FUNCTION(SetMultipart, (this, aMultipart));
RUN_PROXIED_FUNCTION(SetMultipart, (this, &queue, aMultipart));
mMultipart = aMultipart;
return NS_OK;
}

Просмотреть файл

@ -78,6 +78,8 @@ class nsDOMWorkerXHRProxy : public nsRunnable,
(const nsAString&, nsIDOMEventListener*, PRBool);
public:
typedef nsAutoTArray<nsCOMPtr<nsIRunnable>, 5> SyncEventQueue;
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSIDOMEVENTLISTENER
NS_DECL_NSIRUNNABLE
@ -92,6 +94,8 @@ public:
nsresult Abort();
SyncEventQueue* SetSyncEventQueue(SyncEventQueue* aQueue);
protected:
nsresult InitInternal();
void DestroyInternal();
@ -163,6 +167,8 @@ protected:
nsTArray<ListenerArray> mUploadListeners;
nsTArray<WrappedListener> mUploadOnXListeners;
SyncEventQueue* mSyncEventQueue;
// Whether or not this object is owned by the real XHR object.
PRPackedBool mOwnedByXHR;