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

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

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

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

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