2008-09-30 16:56:57 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla 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/MPL/
|
|
|
|
*
|
|
|
|
* 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 Original Code is worker threads.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2008
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Ben Turner <bent.mozilla@gmail.com> (Original Author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsDOMWorkerXHRProxy.h"
|
|
|
|
|
|
|
|
// Interfaces
|
2008-10-14 11:16:36 -07:00
|
|
|
#include "nsIDOMEvent.h"
|
2008-10-14 11:16:37 -07:00
|
|
|
#include "nsIDOMEventTarget.h"
|
2008-10-14 11:16:36 -07:00
|
|
|
#include "nsIDOMProgressEvent.h"
|
2008-10-14 11:16:37 -07:00
|
|
|
#include "nsILoadGroup.h"
|
|
|
|
#include "nsIRequest.h"
|
2008-09-30 16:56:57 -07:00
|
|
|
#include "nsIThread.h"
|
|
|
|
#include "nsIVariant.h"
|
|
|
|
#include "nsIXMLHttpRequest.h"
|
|
|
|
|
|
|
|
// Other includes
|
|
|
|
#include "nsAutoLock.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsIClassInfoImpl.h"
|
|
|
|
#include "nsProxyRelease.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsXMLHttpRequest.h"
|
|
|
|
#include "prinrval.h"
|
|
|
|
#include "prthread.h"
|
|
|
|
|
|
|
|
// DOMWorker includes
|
2008-11-04 13:49:04 -08:00
|
|
|
#include "nsDOMThreadService.h"
|
|
|
|
#include "nsDOMWorker.h"
|
|
|
|
#include "nsDOMWorkerEvents.h"
|
|
|
|
#include "nsDOMWorkerMacros.h"
|
2008-09-30 16:56:57 -07:00
|
|
|
#include "nsDOMWorkerPool.h"
|
2008-11-04 13:49:04 -08:00
|
|
|
#include "nsDOMWorkerXHR.h"
|
2008-09-30 16:56:57 -07:00
|
|
|
#include "nsDOMWorkerXHRProxiedFunctions.h"
|
|
|
|
|
|
|
|
#define MAX_XHR_LISTENER_TYPE nsDOMWorkerXHREventTarget::sMaxXHREventTypes
|
|
|
|
#define MAX_UPLOAD_LISTENER_TYPE nsDOMWorkerXHREventTarget::sMaxUploadEventTypes
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
#define RUN_PROXIED_FUNCTION(_name, _args) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!"); \
|
|
|
|
\
|
|
|
|
if (mCanceled) { \
|
|
|
|
return NS_ERROR_ABORT; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
SyncEventQueue queue; \
|
|
|
|
\
|
|
|
|
nsRefPtr< :: _name> method = new :: _name _args; \
|
|
|
|
NS_ENSURE_TRUE(method, NS_ERROR_OUT_OF_MEMORY); \
|
|
|
|
\
|
|
|
|
method->Init(this, &queue); \
|
|
|
|
\
|
|
|
|
nsRefPtr<nsResultReturningRunnable> runnable = \
|
|
|
|
new nsResultReturningRunnable(mMainThread, method, mWorkerXHR->mWorker); \
|
|
|
|
NS_ENSURE_TRUE(runnable, NS_ERROR_OUT_OF_MEMORY); \
|
|
|
|
\
|
|
|
|
nsresult _rv = runnable->Dispatch(); \
|
|
|
|
\
|
|
|
|
if (mCanceled) { \
|
|
|
|
return NS_ERROR_ABORT; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
PRUint32 queueLength = queue.Length(); \
|
|
|
|
for (PRUint32 index = 0; index < queueLength; index++) { \
|
|
|
|
queue[index]->Run(); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
if (NS_FAILED(_rv)) { \
|
|
|
|
return _rv; \
|
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
|
|
|
|
2008-09-30 16:56:57 -07:00
|
|
|
using namespace nsDOMWorkerProxiedXHRFunctions;
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
class nsResultReturningRunnable : public nsIRunnable
|
2008-09-30 16:56:57 -07:00
|
|
|
{
|
|
|
|
public:
|
2008-10-14 11:16:37 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2008-09-30 16:56:57 -07:00
|
|
|
nsResultReturningRunnable(nsIEventTarget* aTarget, nsIRunnable* aRunnable,
|
2008-11-04 13:49:04 -08:00
|
|
|
nsDOMWorker* aWorker)
|
2008-09-30 16:56:57 -07:00
|
|
|
: mTarget(aTarget), mRunnable(aRunnable), mWorker(aWorker),
|
|
|
|
mResult(NS_OK), mDone(PR_FALSE) { }
|
|
|
|
|
|
|
|
nsresult Dispatch() {
|
|
|
|
if (!mWorker) {
|
|
|
|
// Must have been canceled, bail out.
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIThread* currentThread = NS_GetCurrentThread();
|
|
|
|
NS_ASSERTION(currentThread, "This should never be null!");
|
|
|
|
|
|
|
|
nsresult rv = mTarget->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Complicated logic: Spin events while we haven't been canceled (if a
|
|
|
|
// cancel boolean was supplied) and while we're not done.
|
|
|
|
while (!mWorker->IsCanceled() && !mDone) {
|
|
|
|
// Process a single event or yield if no event is pending.
|
|
|
|
if (!NS_ProcessNextEvent(currentThread, PR_FALSE)) {
|
|
|
|
PR_Sleep(PR_INTERVAL_NO_WAIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mWorker->IsCanceled()) {
|
|
|
|
mResult = NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
#ifdef DEBUG
|
|
|
|
PRBool rightThread = PR_FALSE;
|
|
|
|
mTarget->IsOnCurrentThread(&rightThread);
|
|
|
|
NS_ASSERTION(rightThread, "Run called on the wrong thread!");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mResult = mWorker->IsCanceled() ? NS_ERROR_ABORT : mRunnable->Run();
|
|
|
|
mDone = PR_TRUE;
|
|
|
|
|
|
|
|
return mResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIEventTarget> mTarget;
|
|
|
|
nsCOMPtr<nsIRunnable> mRunnable;
|
2008-11-04 13:49:04 -08:00
|
|
|
nsRefPtr<nsDOMWorker> mWorker;
|
2008-09-30 16:56:57 -07:00
|
|
|
nsresult mResult;
|
|
|
|
volatile PRBool mDone;
|
|
|
|
};
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsResultReturningRunnable, nsIRunnable)
|
|
|
|
|
|
|
|
class nsDOMWorkerXHRLastProgressOrLoadEvent : public nsIRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
nsDOMWorkerXHRLastProgressOrLoadEvent(nsDOMWorkerXHRProxy* aProxy)
|
|
|
|
: mProxy(aProxy) {
|
|
|
|
NS_ASSERTION(aProxy, "Null pointer!");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
nsRefPtr<nsDOMWorkerXHREvent> lastProgressOrLoadEvent;
|
|
|
|
|
|
|
|
if (!mProxy->mCanceled) {
|
|
|
|
nsAutoLock lock(mProxy->mWorkerXHR->Lock());
|
|
|
|
mProxy->mLastProgressOrLoadEvent.swap(lastProgressOrLoadEvent);
|
2008-11-04 13:49:04 -08:00
|
|
|
if (mProxy->mCanceled) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
2008-10-14 11:16:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lastProgressOrLoadEvent) {
|
|
|
|
return lastProgressOrLoadEvent->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsDOMWorkerXHRProxy> mProxy;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsDOMWorkerXHRLastProgressOrLoadEvent,
|
|
|
|
nsIRunnable)
|
|
|
|
|
2008-09-30 16:56:57 -07:00
|
|
|
class nsDOMWorkerXHRWrappedListener : public nsIDOMEventListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
nsDOMWorkerXHRWrappedListener(nsIDOMEventListener* aInner)
|
|
|
|
: mInner(aInner) {
|
|
|
|
NS_ASSERTION(aInner, "Null pointer!");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD HandleEvent(nsIDOMEvent* aEvent) {
|
|
|
|
return mInner->HandleEvent(aEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDOMEventListener* Inner() {
|
|
|
|
return mInner;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIDOMEventListener> mInner;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsDOMWorkerXHRWrappedListener,
|
|
|
|
nsIDOMEventListener)
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
class nsDOMWorkerXHRAttachUploadListenersRunnable : public nsIRunnable
|
2008-10-14 11:16:37 -07:00
|
|
|
{
|
|
|
|
public:
|
2008-10-14 11:16:37 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
nsDOMWorkerXHRAttachUploadListenersRunnable(nsDOMWorkerXHRProxy* aProxy)
|
|
|
|
: mProxy(aProxy) {
|
|
|
|
NS_ASSERTION(aProxy, "Null pointer!");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
2008-10-14 11:16:37 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
NS_ASSERTION(!mProxy->mWantUploadListeners, "Huh?!");
|
2008-10-14 11:16:37 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> upload(do_QueryInterface(mProxy->mUpload));
|
|
|
|
NS_ASSERTION(upload, "This shouldn't fail!");
|
|
|
|
|
|
|
|
nsAutoString eventName;
|
|
|
|
for (PRUint32 index = 0; index < MAX_UPLOAD_LISTENER_TYPE; index++) {
|
|
|
|
eventName.AssignASCII(nsDOMWorkerXHREventTarget::sListenerTypes[index]);
|
|
|
|
upload->AddEventListener(eventName, mProxy, PR_FALSE);
|
|
|
|
}
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
mProxy->mWantUploadListeners = PR_TRUE;
|
2008-10-14 11:16:37 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsDOMWorkerXHRProxy> mProxy;
|
|
|
|
};
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsDOMWorkerXHRAttachUploadListenersRunnable,
|
|
|
|
nsIRunnable)
|
|
|
|
|
|
|
|
class nsDOMWorkerXHRFinishSyncXHRRunnable : public nsIRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
nsDOMWorkerXHRFinishSyncXHRRunnable(nsDOMWorkerXHRProxy* aProxy,
|
|
|
|
nsIThread* aTarget)
|
|
|
|
: mProxy(aProxy), mTarget(aTarget) {
|
|
|
|
NS_ASSERTION(aProxy, "Null pointer!");
|
|
|
|
NS_ASSERTION(aTarget, "Null pointer!");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
mProxy->mSyncXHRThread.swap(thread);
|
|
|
|
mProxy = nsnull;
|
|
|
|
|
|
|
|
NS_ASSERTION(thread, "Should have a thread here!");
|
|
|
|
NS_ASSERTION(!NS_IsMainThread() && thread == NS_GetCurrentThread(),
|
|
|
|
"Wrong thread?!");
|
|
|
|
|
|
|
|
return NS_ProcessPendingEvents(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Dispatch() {
|
|
|
|
nsresult rv = mTarget->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
mTarget = nsnull;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsDOMWorkerXHRProxy> mProxy;
|
|
|
|
nsCOMPtr<nsIThread> mTarget;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsDOMWorkerXHRFinishSyncXHRRunnable, nsIRunnable)
|
|
|
|
|
2008-09-30 16:56:57 -07:00
|
|
|
nsDOMWorkerXHRProxy::nsDOMWorkerXHRProxy(nsDOMWorkerXHR* aWorkerXHR)
|
|
|
|
: mWorkerXHR(aWorkerXHR),
|
|
|
|
mXHR(nsnull),
|
|
|
|
mConcreteXHR(nsnull),
|
|
|
|
mUpload(nsnull),
|
2008-10-14 11:16:37 -07:00
|
|
|
mSyncEventQueue(nsnull),
|
2008-10-14 11:16:37 -07:00
|
|
|
mChannelID(-1),
|
2008-09-30 16:56:57 -07:00
|
|
|
mOwnedByXHR(PR_FALSE),
|
2008-10-14 11:16:37 -07:00
|
|
|
mWantUploadListeners(PR_FALSE),
|
2008-10-14 11:16:37 -07:00
|
|
|
mCanceled(PR_FALSE),
|
|
|
|
mSyncRequest(PR_FALSE)
|
2008-09-30 16:56:57 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(MAX_XHR_LISTENER_TYPE >= MAX_UPLOAD_LISTENER_TYPE,
|
|
|
|
"Upload should support a subset of XHR's event types!");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMWorkerXHRProxy::~nsDOMWorkerXHRProxy()
|
|
|
|
{
|
|
|
|
if (mOwnedByXHR) {
|
2008-11-04 13:49:04 -08:00
|
|
|
mWorkerXHRWN = nsnull;
|
2008-09-30 16:56:57 -07:00
|
|
|
}
|
|
|
|
else if (mXHR) {
|
|
|
|
nsCOMPtr<nsIThread> mainThread;
|
|
|
|
NS_GetMainThread(getter_AddRefs(mainThread));
|
|
|
|
NS_ASSERTION(mainThread, "This isn't supposed to fail!");
|
|
|
|
|
|
|
|
// This will release immediately if we're on the main thread.
|
|
|
|
NS_ProxyRelease(mainThread, mXHR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS3(nsDOMWorkerXHRProxy, nsIRunnable,
|
|
|
|
nsIDOMEventListener,
|
|
|
|
nsIRequestObserver)
|
2008-09-30 16:56:57 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::Init()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
NS_ENSURE_FALSE(mXHR, NS_ERROR_ALREADY_INITIALIZED);
|
|
|
|
|
|
|
|
mMainThread = do_GetMainThread();
|
|
|
|
NS_ENSURE_TRUE(mMainThread, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
nsRefPtr<nsResultReturningRunnable> runnable =
|
|
|
|
new nsResultReturningRunnable(mMainThread, this, mWorkerXHR->mWorker);
|
|
|
|
NS_ENSURE_TRUE(runnable, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
nsresult rv = runnable->Dispatch();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Only warn if we didn't get canceled.
|
|
|
|
NS_WARN_IF_FALSE(rv == NS_ERROR_ABORT, "Dispatch failed!");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIXMLHttpRequest*
|
|
|
|
nsDOMWorkerXHRProxy::GetXMLHttpRequest()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
return mCanceled ? nsnull : mXHR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::Destroy()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
{
|
|
|
|
nsAutoLock lock(mWorkerXHR->Lock());
|
2008-11-04 13:49:04 -08:00
|
|
|
|
|
|
|
mCanceled = PR_TRUE;
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
mLastProgressOrLoadEvent = nsnull;
|
2008-11-04 13:49:04 -08:00
|
|
|
mLastXHREvent = nsnull;
|
2008-10-14 11:16:37 -07:00
|
|
|
}
|
2008-09-30 16:56:57 -07:00
|
|
|
|
|
|
|
if (mXHR) {
|
|
|
|
DestroyInternal();
|
|
|
|
}
|
|
|
|
|
2008-11-04 13:49:04 -08:00
|
|
|
NS_ASSERTION(!(mLastProgressOrLoadEvent && mLastXHREvent), "Going to leak!");
|
2008-10-14 11:16:37 -07:00
|
|
|
|
2008-09-30 16:56:57 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::InitInternal()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!mXHR, "InitInternal shouldn't be called twice!");
|
|
|
|
|
2008-11-04 13:49:04 -08:00
|
|
|
nsDOMWorker* worker = mWorkerXHR->mWorker;
|
2008-09-30 16:56:57 -07:00
|
|
|
nsRefPtr<nsDOMWorkerPool> pool = worker->Pool();
|
|
|
|
|
|
|
|
if (worker->IsCanceled()) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIPrincipal* nodePrincipal = pool->ParentDocument()->NodePrincipal();
|
|
|
|
nsIScriptContext* scriptContext = pool->ScriptContext();
|
|
|
|
NS_ASSERTION(nodePrincipal && scriptContext, "Shouldn't be null!");
|
|
|
|
|
|
|
|
nsRefPtr<nsXMLHttpRequest> xhrConcrete = new nsXMLHttpRequest();
|
|
|
|
NS_ENSURE_TRUE(xhrConcrete, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
nsresult rv = xhrConcrete->Init(nodePrincipal, scriptContext, nsnull);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Call QI manually here to avoid keeping up with the cast madness of
|
|
|
|
// nsXMLHttpRequest.
|
|
|
|
nsCOMPtr<nsIXMLHttpRequest> xhr =
|
|
|
|
do_QueryInterface(static_cast<nsIXMLHttpRequest*>(xhrConcrete));
|
|
|
|
NS_ENSURE_TRUE(xhr, NS_ERROR_NO_INTERFACE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIXMLHttpRequestUpload> upload;
|
|
|
|
rv = xhr->GetUpload(getter_AddRefs(upload));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsRefPtr<nsDOMWorkerXHREvent> nullEvent = new nsDOMWorkerXHREvent(this);
|
|
|
|
NS_ENSURE_TRUE(nullEvent, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2008-11-04 13:49:04 -08:00
|
|
|
rv = nullEvent->SnapshotXHRState(xhr);
|
2008-09-30 16:56:57 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-11-04 13:49:04 -08:00
|
|
|
nullEvent->EventHandled();
|
|
|
|
mLastXHREvent.swap(nullEvent);
|
2008-09-30 16:56:57 -07:00
|
|
|
|
|
|
|
xhrConcrete->SetRequestObserver(this);
|
|
|
|
|
|
|
|
// We now own mXHR and it owns upload.
|
|
|
|
xhr.swap(mXHR);
|
|
|
|
|
|
|
|
// Weak refs.
|
|
|
|
mUpload = upload;
|
|
|
|
mConcreteXHR = xhrConcrete;
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
AddRemoveXHRListeners(PR_TRUE);
|
|
|
|
|
2008-09-30 16:56:57 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMWorkerXHRProxy::DestroyInternal()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
nsRefPtr<nsDOMWorkerXHRProxy> kungFuDeathGrip(this);
|
|
|
|
|
|
|
|
if (mConcreteXHR) {
|
|
|
|
mConcreteXHR->SetRequestObserver(nsnull);
|
|
|
|
}
|
2008-09-30 16:56:57 -07:00
|
|
|
|
|
|
|
if (mOwnedByXHR) {
|
|
|
|
mXHR->Abort();
|
|
|
|
}
|
2008-10-14 11:16:37 -07:00
|
|
|
else {
|
|
|
|
// There's a slight chance that Send was called yet we've canceled before
|
|
|
|
// necko has fired its OnStartRequest notification. Guard against that here.
|
|
|
|
nsRefPtr<nsDOMWorkerXHRFinishSyncXHRRunnable> syncFinishedRunnable;
|
|
|
|
{
|
|
|
|
nsAutoLock lock(mWorkerXHR->Lock());
|
|
|
|
mSyncFinishedRunnable.swap(syncFinishedRunnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (syncFinishedRunnable) {
|
|
|
|
syncFinishedRunnable->Dispatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(!mOwnedByXHR, "Should have flipped already!");
|
|
|
|
NS_ASSERTION(!mSyncFinishedRunnable, "Should have fired this already!");
|
2008-09-30 16:56:57 -07:00
|
|
|
|
|
|
|
// mXHR could be null if Init fails.
|
|
|
|
if (mXHR) {
|
2008-10-14 11:16:37 -07:00
|
|
|
AddRemoveXHRListeners(PR_FALSE);
|
|
|
|
|
2008-09-30 16:56:57 -07:00
|
|
|
mXHR->Release();
|
|
|
|
mXHR = nsnull;
|
2008-10-14 11:16:37 -07:00
|
|
|
|
2008-09-30 16:56:57 -07:00
|
|
|
mUpload = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-10-14 11:16:37 -07:00
|
|
|
nsDOMWorkerXHRProxy::AddRemoveXHRListeners(PRBool aAdd)
|
2008-09-30 16:56:57 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> xhrTarget(do_QueryInterface(mXHR));
|
|
|
|
NS_ASSERTION(xhrTarget, "This shouldn't fail!");
|
|
|
|
|
2008-11-04 13:49:04 -08:00
|
|
|
EventListenerFunction addRemoveEventListener =
|
|
|
|
aAdd ?
|
|
|
|
&nsIDOMEventTarget::AddEventListener :
|
|
|
|
&nsIDOMEventTarget::RemoveEventListener;
|
2008-09-30 16:56:57 -07:00
|
|
|
|
|
|
|
nsAutoString eventName;
|
|
|
|
PRUint32 index = 0;
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
if (mWantUploadListeners) {
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> uploadTarget(do_QueryInterface(mUpload));
|
|
|
|
NS_ASSERTION(uploadTarget, "This shouldn't fail!");
|
|
|
|
|
|
|
|
for (; index < MAX_UPLOAD_LISTENER_TYPE; index++) {
|
|
|
|
eventName.AssignASCII(nsDOMWorkerXHREventTarget::sListenerTypes[index]);
|
2008-11-04 13:49:04 -08:00
|
|
|
(xhrTarget.get()->*addRemoveEventListener)(eventName, this, PR_FALSE);
|
|
|
|
(uploadTarget.get()->*addRemoveEventListener)(eventName, this, PR_FALSE);
|
2008-10-14 11:16:37 -07:00
|
|
|
}
|
2008-09-30 16:56:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for (; index < MAX_XHR_LISTENER_TYPE; index++) {
|
|
|
|
eventName.AssignASCII(nsDOMWorkerXHREventTarget::sListenerTypes[index]);
|
2008-11-04 13:49:04 -08:00
|
|
|
(xhrTarget.get()->*addRemoveEventListener)(eventName, this, PR_FALSE);
|
2008-09-30 16:56:57 -07:00
|
|
|
}
|
2008-10-14 11:16:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMWorkerXHRProxy::FlipOwnership()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
// Flip!
|
|
|
|
mOwnedByXHR = !mOwnedByXHR;
|
|
|
|
|
|
|
|
// If mWorkerXHR has no outstanding refs from JS then we are about to die.
|
|
|
|
// Hold an extra ref here to make sure that we live through this call.
|
|
|
|
nsRefPtr<nsDOMWorkerXHRProxy> kungFuDeathGrip(this);
|
2008-09-30 16:56:57 -07:00
|
|
|
|
|
|
|
if (mOwnedByXHR) {
|
2008-11-04 13:49:04 -08:00
|
|
|
mWorkerXHRWN = mWorkerXHR->GetWrappedNative();
|
|
|
|
NS_ASSERTION(mWorkerXHRWN, "Null pointer!");
|
2008-09-30 16:56:57 -07:00
|
|
|
mXHR->Release();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mXHR->AddRef();
|
2008-11-04 13:49:04 -08:00
|
|
|
mWorkerXHRWN = nsnull;
|
2008-09-30 16:56:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2008-11-04 13:49:04 -08:00
|
|
|
nsDOMWorkerXHRProxy::UploadEventListenerAdded()
|
2008-09-30 16:56:57 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
// If this is the first time we're setting an upload listener then we have to
|
2008-11-04 13:49:04 -08:00
|
|
|
// hit the main thread to attach the upload listeners. Otherwise there's
|
|
|
|
// nothing to do here.
|
|
|
|
if (mWantUploadListeners) {
|
2008-09-30 16:56:57 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-11-04 13:49:04 -08:00
|
|
|
nsRefPtr<nsDOMWorkerXHRAttachUploadListenersRunnable> attachRunnable =
|
|
|
|
new nsDOMWorkerXHRAttachUploadListenersRunnable(this);
|
|
|
|
NS_ENSURE_TRUE(attachRunnable, NS_ERROR_OUT_OF_MEMORY);
|
2008-09-30 16:56:57 -07:00
|
|
|
|
2008-11-04 13:49:04 -08:00
|
|
|
nsRefPtr<nsResultReturningRunnable> runnable =
|
|
|
|
new nsResultReturningRunnable(mMainThread, attachRunnable,
|
|
|
|
mWorkerXHR->mWorker);
|
|
|
|
NS_ENSURE_TRUE(runnable, NS_ERROR_OUT_OF_MEMORY);
|
2008-09-30 16:56:57 -07:00
|
|
|
|
2008-11-04 13:49:04 -08:00
|
|
|
nsresult rv = runnable->Dispatch();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2008-09-30 16:56:57 -07:00
|
|
|
}
|
|
|
|
|
2008-11-04 13:49:04 -08:00
|
|
|
NS_ASSERTION(mWantUploadListeners, "Should have set this!");
|
|
|
|
return NS_OK;
|
2008-09-30 16:56:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::HandleWorkerEvent(nsDOMWorkerXHREvent* aEvent,
|
|
|
|
PRBool aUploadEvent)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aEvent, "Should not be null!");
|
|
|
|
|
2008-11-04 13:49:04 -08:00
|
|
|
{
|
|
|
|
nsAutoLock lock(mWorkerXHR->Lock());
|
2008-09-30 16:56:57 -07:00
|
|
|
|
2008-11-04 13:49:04 -08:00
|
|
|
if (mCanceled ||
|
|
|
|
(aEvent->mChannelID != -1 && aEvent->mChannelID != mChannelID)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-09-30 16:56:57 -07:00
|
|
|
|
2008-11-04 13:49:04 -08:00
|
|
|
mLastXHREvent = aEvent;
|
2008-09-30 16:56:57 -07:00
|
|
|
}
|
|
|
|
|
2008-11-04 13:49:04 -08:00
|
|
|
nsIDOMEvent* event = static_cast<nsDOMWorkerEvent*>(aEvent);
|
|
|
|
return HandleEventInternal(aEvent->mXHREventType, event, aUploadEvent);
|
2008-09-30 16:56:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::HandleEventInternal(PRUint32 aType,
|
|
|
|
nsIDOMEvent* aEvent,
|
|
|
|
PRBool aUploadListener)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aEvent, "Should not be null!");
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (aUploadListener) {
|
|
|
|
NS_ASSERTION(aType < MAX_UPLOAD_LISTENER_TYPE, "Bad type!");
|
2008-11-04 13:49:04 -08:00
|
|
|
NS_ASSERTION(mWorkerXHR->mUpload, "No upload object!");
|
2008-09-30 16:56:57 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_ASSERTION(aType < MAX_XHR_LISTENER_TYPE, "Bad type!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (mCanceled) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2008-11-04 13:49:04 -08:00
|
|
|
nsIDOMEventTarget* target = aUploadListener ?
|
|
|
|
static_cast<nsDOMWorkerMessageHandler*>(mWorkerXHR->mUpload) :
|
|
|
|
static_cast<nsDOMWorkerMessageHandler*>(mWorkerXHR);
|
2008-09-30 16:56:57 -07:00
|
|
|
|
2008-11-04 13:49:04 -08:00
|
|
|
return target->DispatchEvent(aEvent, nsnull);
|
2008-09-30 16:56:57 -07:00
|
|
|
}
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
PRBool
|
2008-11-04 13:49:04 -08:00
|
|
|
nsDOMWorkerXHRProxy::HasListenersForType(const nsAString& aType,
|
2008-10-14 11:16:37 -07:00
|
|
|
nsIDOMEvent* aEvent)
|
|
|
|
{
|
2008-11-04 13:49:04 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
PRUint32 type = nsDOMWorkerXHREventTarget::GetListenerTypeFromString(aType);
|
|
|
|
NS_ASSERTION(type < MAX_XHR_LISTENER_TYPE, "Bad type!");
|
|
|
|
#endif
|
2008-10-14 11:16:37 -07:00
|
|
|
|
2008-11-04 13:49:04 -08:00
|
|
|
if (mWorkerXHR->HasListeners(aType)) {
|
2008-10-14 11:16:37 -07:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool checkUploadListeners = PR_FALSE;
|
|
|
|
if (aEvent) {
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target;
|
|
|
|
if (NS_SUCCEEDED(aEvent->GetTarget(getter_AddRefs(target)))) {
|
|
|
|
nsCOMPtr<nsIXMLHttpRequestUpload> upload(do_QueryInterface(target));
|
|
|
|
checkUploadListeners = !!upload;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
checkUploadListeners = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-11-04 13:49:04 -08:00
|
|
|
if (checkUploadListeners && mWorkerXHR->mUpload->HasListeners(aType)) {
|
2008-10-14 11:16:37 -07:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2008-09-30 16:56:57 -07:00
|
|
|
// nsIDOMEventListener
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMWorkerXHRProxy::HandleEvent(nsIDOMEvent* aEvent)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aEvent);
|
|
|
|
|
|
|
|
nsAutoString typeString;
|
|
|
|
nsresult rv = aEvent->GetType(typeString);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRUint32 type =
|
|
|
|
nsDOMWorkerXHREventTarget::GetListenerTypeFromString(typeString);
|
|
|
|
if (type >= MAX_XHR_LISTENER_TYPE) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-09-30 16:56:57 -07:00
|
|
|
if (mCanceled) {
|
2008-11-04 13:49:04 -08:00
|
|
|
// When Abort is called on nsXMLHttpRequest (either from a proxied Abort
|
|
|
|
// call or from DestroyInternal) the OnStopRequest call is not run
|
|
|
|
// synchronously. Thankfully an abort event *is* fired synchronously so we
|
|
|
|
// can flip our ownership around and fire the sync finished runnable if
|
|
|
|
// we're running in sync mode.
|
|
|
|
if (type == LISTENER_TYPE_ABORT) {
|
|
|
|
OnStopRequest(nsnull, nsnull, NS_ERROR_ABORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always bail out if we're canceled.
|
2008-09-30 16:56:57 -07:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2008-11-04 13:49:04 -08:00
|
|
|
PRBool ignoreEvent = !HasListenersForType(typeString, aEvent);
|
|
|
|
if (ignoreEvent && mSyncRequest) {
|
|
|
|
// Only ignore the event if it isn't final.
|
|
|
|
ignoreEvent = type != LISTENER_TYPE_ABORT &&
|
|
|
|
type != LISTENER_TYPE_ERROR &&
|
|
|
|
type != LISTENER_TYPE_LOAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ignoreEvent) {
|
2008-10-14 11:16:37 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-09-30 16:56:57 -07:00
|
|
|
|
|
|
|
nsRefPtr<nsDOMWorkerXHREvent> newEvent = new nsDOMWorkerXHREvent(this);
|
|
|
|
NS_ENSURE_TRUE(newEvent, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
rv = newEvent->Init(type, typeString, aEvent);
|
2008-09-30 16:56:57 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
nsCOMPtr<nsIRunnable> runnable(newEvent);
|
|
|
|
|
|
|
|
if (type == LISTENER_TYPE_LOAD || type == LISTENER_TYPE_PROGRESS) {
|
|
|
|
runnable = new nsDOMWorkerXHRLastProgressOrLoadEvent(this);
|
|
|
|
NS_ENSURE_TRUE(runnable, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
{
|
|
|
|
nsAutoLock lock(mWorkerXHR->Lock());
|
2008-11-04 13:49:04 -08:00
|
|
|
|
|
|
|
if (mCanceled) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
mLastProgressOrLoadEvent.swap(newEvent);
|
|
|
|
|
|
|
|
if (newEvent) {
|
|
|
|
// Already had a saved progress/load event so no need to generate
|
|
|
|
// another. Bail out rather than dispatching runnable.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-10-14 11:16:37 -07:00
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
if (mSyncEventQueue) {
|
|
|
|
// If we're supposed to be capturing events for synchronous execution then
|
|
|
|
// place this event in the queue.
|
2008-10-14 11:16:37 -07:00
|
|
|
nsCOMPtr<nsIRunnable>* newElement =
|
2008-10-14 11:16:37 -07:00
|
|
|
mSyncEventQueue->AppendElement(runnable);
|
2008-10-14 11:16:37 -07:00
|
|
|
NS_ENSURE_TRUE(newElement, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
}
|
2008-10-14 11:16:37 -07:00
|
|
|
else if (mSyncXHRThread) {
|
|
|
|
// If we're running a sync XHR then schedule the event immediately for the
|
|
|
|
// worker's thread.
|
|
|
|
rv = mSyncXHRThread->Dispatch(runnable, NS_DISPATCH_NORMAL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2008-10-14 11:16:37 -07:00
|
|
|
else {
|
2008-10-14 11:16:37 -07:00
|
|
|
// Otherwise schedule it for the worker via the thread service.
|
|
|
|
rv = nsDOMThreadService::get()->Dispatch(mWorkerXHR->mWorker, runnable);
|
2008-10-14 11:16:37 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2008-09-30 16:56:57 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2008-10-14 11:16:37 -07:00
|
|
|
nsDOMWorkerXHRProxy::OpenRequest(const nsACString& aMethod,
|
|
|
|
const nsACString& aUrl,
|
|
|
|
PRBool aAsync,
|
|
|
|
const nsAString& aUser,
|
|
|
|
const nsAString& aPassword)
|
2008-09-30 16:56:57 -07:00
|
|
|
{
|
2008-10-14 11:16:37 -07:00
|
|
|
if (!NS_IsMainThread()) {
|
2008-10-14 11:16:37 -07:00
|
|
|
mSyncRequest = !aAsync;
|
|
|
|
|
|
|
|
// Always do async behind the scenes!
|
|
|
|
RUN_PROXIED_FUNCTION(OpenRequest,
|
|
|
|
(aMethod, aUrl, PR_TRUE, aUser, aPassword));
|
2008-10-14 11:16:37 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-09-30 16:56:57 -07:00
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
if (mCanceled) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
2008-09-30 16:56:57 -07:00
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
nsresult rv = mXHR->OpenRequest(aMethod, aUrl, aAsync, aUser, aPassword);
|
2008-10-14 11:16:37 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
// Do this after OpenRequest is called so that we will continue to run events
|
|
|
|
// from the old channel if OpenRequest fails. Any events generated by the
|
|
|
|
// OpenRequest method will always run regardless of channel ID.
|
2008-10-14 11:16:37 -07:00
|
|
|
mChannelID++;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2008-10-14 11:16:37 -07:00
|
|
|
nsDOMWorkerXHRProxy::Abort()
|
2008-10-14 11:16:37 -07:00
|
|
|
{
|
|
|
|
if (!NS_IsMainThread()) {
|
2008-10-14 11:16:37 -07:00
|
|
|
RUN_PROXIED_FUNCTION(Abort, ());
|
2008-10-14 11:16:37 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mCanceled) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
nsCOMPtr<nsIXMLHttpRequest> xhr = mXHR;
|
|
|
|
|
|
|
|
FlipOwnership();
|
|
|
|
|
|
|
|
nsresult rv = xhr->Abort();
|
2008-10-14 11:16:37 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
// Don't allow further events from this channel.
|
2008-10-14 11:16:37 -07:00
|
|
|
mChannelID++;
|
|
|
|
|
2008-09-30 16:56:57 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
nsDOMWorkerXHRProxy::SyncEventQueue*
|
|
|
|
nsDOMWorkerXHRProxy::SetSyncEventQueue(SyncEventQueue* aQueue)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
SyncEventQueue* oldQueue = mSyncEventQueue;
|
|
|
|
mSyncEventQueue = aQueue;
|
|
|
|
return oldQueue;
|
|
|
|
}
|
|
|
|
|
2008-09-30 16:56:57 -07:00
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::GetAllResponseHeaders(char** _retval)
|
|
|
|
{
|
2008-10-14 11:16:37 -07:00
|
|
|
RUN_PROXIED_FUNCTION(GetAllResponseHeaders, (_retval));
|
2008-09-30 16:56:57 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::GetResponseHeader(const nsACString& aHeader,
|
|
|
|
nsACString& _retval)
|
|
|
|
{
|
2008-10-14 11:16:37 -07:00
|
|
|
RUN_PROXIED_FUNCTION(GetResponseHeader, (aHeader, _retval));
|
2008-09-30 16:56:57 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::Send(nsIVariant* aBody)
|
|
|
|
{
|
2008-10-14 11:16:37 -07:00
|
|
|
NS_ASSERTION(!mSyncXHRThread, "Shouldn't reenter here!");
|
|
|
|
|
|
|
|
if (mSyncRequest) {
|
|
|
|
|
|
|
|
mSyncXHRThread = NS_GetCurrentThread();
|
|
|
|
NS_ENSURE_TRUE(mSyncXHRThread, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsAutoLock lock(mWorkerXHR->Lock());
|
|
|
|
|
|
|
|
if (mCanceled) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSyncFinishedRunnable =
|
|
|
|
new nsDOMWorkerXHRFinishSyncXHRRunnable(this, mSyncXHRThread);
|
|
|
|
NS_ENSURE_TRUE(mSyncFinishedRunnable, NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
RUN_PROXIED_FUNCTION(Send, (aBody));
|
|
|
|
|
|
|
|
return RunSyncEventLoop();
|
2008-09-30 16:56:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::SendAsBinary(const nsAString& aBody)
|
|
|
|
{
|
2008-10-14 11:16:37 -07:00
|
|
|
NS_ASSERTION(!mSyncXHRThread, "Shouldn't reenter here!");
|
|
|
|
|
|
|
|
if (mSyncRequest) {
|
|
|
|
mSyncXHRThread = NS_GetCurrentThread();
|
|
|
|
NS_ENSURE_TRUE(mSyncXHRThread, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsAutoLock lock(mWorkerXHR->Lock());
|
|
|
|
|
|
|
|
if (mCanceled) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSyncFinishedRunnable =
|
|
|
|
new nsDOMWorkerXHRFinishSyncXHRRunnable(this, mSyncXHRThread);
|
|
|
|
NS_ENSURE_TRUE(mSyncFinishedRunnable, NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
RUN_PROXIED_FUNCTION(SendAsBinary, (aBody));
|
|
|
|
|
|
|
|
return RunSyncEventLoop();
|
2008-09-30 16:56:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::SetRequestHeader(const nsACString& aHeader,
|
|
|
|
const nsACString& aValue)
|
|
|
|
{
|
2008-10-14 11:16:37 -07:00
|
|
|
RUN_PROXIED_FUNCTION(SetRequestHeader, (aHeader, aValue));
|
2008-09-30 16:56:57 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::OverrideMimeType(const nsACString& aMimetype)
|
|
|
|
{
|
2008-10-14 11:16:37 -07:00
|
|
|
RUN_PROXIED_FUNCTION(OverrideMimeType, (aMimetype));
|
2008-09-30 16:56:57 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::GetMultipart(PRBool* aMultipart)
|
|
|
|
{
|
2008-10-14 11:16:37 -07:00
|
|
|
NS_ASSERTION(aMultipart, "Null pointer!");
|
|
|
|
|
2008-09-30 16:56:57 -07:00
|
|
|
if (mCanceled) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
RUN_PROXIED_FUNCTION(GetMultipart, (aMultipart));
|
2008-09-30 16:56:57 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::SetMultipart(PRBool aMultipart)
|
|
|
|
{
|
2008-10-14 11:16:37 -07:00
|
|
|
RUN_PROXIED_FUNCTION(SetMultipart, (aMultipart));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::GetWithCredentials(PRBool* aWithCredentials)
|
|
|
|
{
|
|
|
|
RUN_PROXIED_FUNCTION(GetWithCredentials, (aWithCredentials));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::SetWithCredentials(PRBool aWithCredentials)
|
|
|
|
{
|
|
|
|
RUN_PROXIED_FUNCTION(SetWithCredentials, (aWithCredentials));
|
2008-09-30 16:56:57 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::GetResponseText(nsAString& _retval)
|
|
|
|
{
|
|
|
|
if (mCanceled) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
_retval.Assign(mLastXHREvent->mResponseText);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::GetStatusText(nsACString& _retval)
|
|
|
|
{
|
|
|
|
if (mCanceled) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
_retval.Assign(mLastXHREvent->mStatusText);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::GetStatus(nsresult* _retval)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(_retval, "Null pointer!");
|
|
|
|
|
|
|
|
if (mCanceled) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
*_retval = mLastXHREvent->mStatus;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::GetReadyState(PRInt32* _retval)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(_retval, "Null pointer!");
|
|
|
|
|
|
|
|
if (mCanceled) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
*_retval = mLastXHREvent->mReadyState;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
nsresult
|
|
|
|
nsDOMWorkerXHRProxy::RunSyncEventLoop()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
if (!mSyncRequest) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mSyncXHRThread == NS_GetCurrentThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
while (mSyncXHRThread) {
|
|
|
|
if (NS_UNLIKELY(!NS_ProcessNextEvent(mSyncXHRThread))) {
|
|
|
|
NS_ERROR("Something wrong here, this shouldn't fail!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(!NS_HasPendingEvents(NS_GetCurrentThread()),
|
|
|
|
"Unprocessed events remaining!");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-09-30 16:56:57 -07:00
|
|
|
// nsIRunnable
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMWorkerXHRProxy::Run()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!mXHR, "Run twice?!");
|
|
|
|
|
|
|
|
if (mCanceled) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = InitInternal();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("InitInternal failed!");
|
|
|
|
DestroyInternal();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIRequestObserver
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMWorkerXHRProxy::OnStartRequest(nsIRequest* /* aRequest */,
|
|
|
|
nsISupports* /* aContext */)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
NS_ASSERTION(!mOwnedByXHR, "Inconsistent state!");
|
|
|
|
|
2008-09-30 16:56:57 -07:00
|
|
|
if (mCanceled) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
FlipOwnership();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIRequestObserver
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMWorkerXHRProxy::OnStopRequest(nsIRequest* /* aRequest */,
|
|
|
|
nsISupports* /* aContext */,
|
|
|
|
nsresult /* aStatus */)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
NS_ASSERTION(mOwnedByXHR, "Inconsistent state!");
|
|
|
|
|
|
|
|
FlipOwnership();
|
|
|
|
|
2008-10-14 11:16:37 -07:00
|
|
|
nsRefPtr<nsDOMWorkerXHRFinishSyncXHRRunnable> syncFinishedRunnable;
|
|
|
|
{
|
|
|
|
nsAutoLock lock(mWorkerXHR->Lock());
|
|
|
|
mSyncFinishedRunnable.swap(syncFinishedRunnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (syncFinishedRunnable) {
|
|
|
|
nsresult rv = syncFinishedRunnable->Dispatch();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2008-09-30 16:56:57 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|