2011-07-17 19:09:13 +00:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2012-03-31 04:42:20 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
#include "XMLHttpRequest.h"
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
#include "nsIDOMEvent.h"
|
|
|
|
#include "nsIDOMEventListener.h"
|
|
|
|
#include "nsIDOMProgressEvent.h"
|
|
|
|
#include "nsIRunnable.h"
|
|
|
|
#include "nsIVariant.h"
|
|
|
|
#include "nsIXMLHttpRequest.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
|
2012-01-14 17:43:00 +00:00
|
|
|
#include "jsfriendapi.h"
|
2013-10-23 13:16:49 +00:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
|
|
|
#include "mozilla/dom/Exceptions.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2012-03-31 04:42:20 +00:00
|
|
|
#include "nsContentUtils.h"
|
2013-05-22 16:05:26 +00:00
|
|
|
#include "nsCxPusher.h"
|
2013-11-05 14:16:26 +00:00
|
|
|
#include "nsEventDispatcher.h"
|
2012-03-31 04:42:20 +00:00
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
#include "File.h"
|
|
|
|
#include "RuntimeService.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
#include "WorkerPrivate.h"
|
2013-10-23 13:16:49 +00:00
|
|
|
#include "WorkerRunnable.h"
|
2012-03-31 04:42:20 +00:00
|
|
|
#include "XMLHttpRequestUpload.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-09-20 07:55:36 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2012-11-10 15:45:52 +00:00
|
|
|
using namespace mozilla::dom;
|
2012-03-31 04:42:20 +00:00
|
|
|
USING_WORKERS_NAMESPACE
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
// XXX Need to figure this out...
|
|
|
|
#define UNCATCHABLE_EXCEPTION NS_ERROR_OUT_OF_MEMORY
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
/**
|
|
|
|
* XMLHttpRequest in workers
|
|
|
|
*
|
|
|
|
* XHR in workers is implemented by proxying calls/events/etc between the
|
|
|
|
* worker thread and an nsXMLHttpRequest on the main thread. The glue
|
|
|
|
* object here is the Proxy, which lives on both threads. All other objects
|
|
|
|
* live on either the main thread (the nsXMLHttpRequest) or the worker thread
|
|
|
|
* (the worker and XHR private objects).
|
|
|
|
*
|
|
|
|
* The main thread XHR is always operated in async mode, even for sync XHR
|
|
|
|
* in workers. Calls made on the worker thread are proxied to the main thread
|
|
|
|
* synchronously (meaning the worker thread is blocked until the call
|
|
|
|
* returns). Each proxied call spins up a sync queue, which captures any
|
|
|
|
* synchronously dispatched events and ensures that they run synchronously
|
|
|
|
* on the worker as well. Asynchronously dispatched events are posted to the
|
|
|
|
* worker thread to run asynchronously. Some of the XHR state is mirrored on
|
|
|
|
* the worker thread to avoid needing a cross-thread call on every property
|
|
|
|
* access.
|
|
|
|
*
|
|
|
|
* The XHR private is stored in the private slot of the XHR JSObject on the
|
|
|
|
* worker thread. It is destroyed when that JSObject is GCd. The private
|
|
|
|
* roots its JSObject while network activity is in progress. It also
|
|
|
|
* adds itself as a feature to the worker to give itself a chance to clean up
|
|
|
|
* if the worker goes away during an XHR call. It is important that the
|
|
|
|
* rooting and feature registration (collectively called pinning) happens at
|
|
|
|
* the proper times. If we pin for too long we can cause memory leaks or even
|
|
|
|
* shutdown hangs. If we don't pin for long enough we introduce a GC hazard.
|
|
|
|
*
|
|
|
|
* The XHR is pinned from the time Send is called to roughly the time loadend
|
|
|
|
* is received. There are some complications involved with Abort and XHR
|
|
|
|
* reuse. We maintain a counter on the main thread of how many times Send was
|
|
|
|
* called on this XHR, and we decrement the counter every time we receive a
|
|
|
|
* loadend event. When the counter reaches zero we dispatch a runnable to the
|
|
|
|
* worker thread to unpin the XHR. We only decrement the counter if the
|
|
|
|
* dispatch was successful, because the worker may no longer be accepting
|
|
|
|
* regular runnables. In the event that we reach Proxy::Teardown and there
|
|
|
|
* the outstanding Send count is still non-zero, we dispatch a control
|
|
|
|
* runnable which is guaranteed to run.
|
|
|
|
*
|
|
|
|
* NB: Some of this could probably be simplified now that we have the
|
|
|
|
* inner/outer channel ids.
|
|
|
|
*/
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
BEGIN_WORKERS_NAMESPACE
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-06-15 02:31:55 +00:00
|
|
|
class Proxy MOZ_FINAL : public nsIDOMEventListener
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-03-31 04:42:20 +00:00
|
|
|
public:
|
|
|
|
// Read on multiple threads.
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
XMLHttpRequest* mXMLHttpRequestPrivate;
|
|
|
|
|
2012-09-17 00:20:16 +00:00
|
|
|
// XHR Params:
|
|
|
|
bool mMozAnon;
|
|
|
|
bool mMozSystem;
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
// Only touched on the main thread.
|
|
|
|
nsRefPtr<nsXMLHttpRequest> mXHR;
|
|
|
|
nsCOMPtr<nsIXMLHttpRequestUpload> mXHRUpload;
|
2013-10-23 13:16:49 +00:00
|
|
|
nsCOMPtr<nsIEventTarget> mSyncLoopTarget;
|
|
|
|
nsCOMPtr<nsIEventTarget> mSyncEventResponseTarget;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mInnerEventStreamId;
|
|
|
|
uint32_t mInnerChannelId;
|
|
|
|
uint32_t mOutstandingSendCount;
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
// Only touched on the worker thread.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mOuterEventStreamId;
|
|
|
|
uint32_t mOuterChannelId;
|
|
|
|
uint64_t mLastLoaded;
|
|
|
|
uint64_t mLastTotal;
|
|
|
|
uint64_t mLastUploadLoaded;
|
|
|
|
uint64_t mLastUploadTotal;
|
2012-03-31 04:42:20 +00:00
|
|
|
bool mIsSyncXHR;
|
|
|
|
bool mLastLengthComputable;
|
|
|
|
bool mLastUploadLengthComputable;
|
|
|
|
bool mSeenLoadStart;
|
|
|
|
bool mSeenUploadLoadStart;
|
|
|
|
|
|
|
|
// Only touched on the main thread.
|
|
|
|
bool mUploadEventListenersAttached;
|
|
|
|
bool mMainThreadSeenLoadStart;
|
|
|
|
bool mInOpen;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
public:
|
2012-09-17 00:20:16 +00:00
|
|
|
Proxy(XMLHttpRequest* aXHRPrivate, bool aMozAnon, bool aMozSystem)
|
2012-07-30 14:20:58 +00:00
|
|
|
: mWorkerPrivate(nullptr), mXMLHttpRequestPrivate(aXHRPrivate),
|
2012-09-17 00:20:16 +00:00
|
|
|
mMozAnon(aMozAnon), mMozSystem(aMozSystem),
|
2012-03-31 04:42:20 +00:00
|
|
|
mInnerEventStreamId(0), mInnerChannelId(0), mOutstandingSendCount(0),
|
|
|
|
mOuterEventStreamId(0), mOuterChannelId(0), mLastLoaded(0), mLastTotal(0),
|
|
|
|
mLastUploadLoaded(0), mLastUploadTotal(0), mIsSyncXHR(false),
|
|
|
|
mLastLengthComputable(false), mLastUploadLengthComputable(false),
|
|
|
|
mSeenLoadStart(false), mSeenUploadLoadStart(false),
|
|
|
|
mUploadEventListenersAttached(false), mMainThreadSeenLoadStart(false),
|
|
|
|
mInOpen(false)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIDOMEVENTLISTENER
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
bool
|
2013-10-23 13:16:49 +00:00
|
|
|
Init();
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
void
|
|
|
|
Teardown();
|
|
|
|
|
|
|
|
bool
|
|
|
|
AddRemoveEventListeners(bool aUpload, bool aAdd);
|
|
|
|
|
|
|
|
void
|
|
|
|
Reset()
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-03-31 04:42:20 +00:00
|
|
|
AssertIsOnMainThread();
|
2011-11-04 02:51:33 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
if (mUploadEventListenersAttached) {
|
|
|
|
AddRemoveEventListeners(true, false);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
already_AddRefed<nsIEventTarget>
|
|
|
|
GetEventTarget()
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-03-31 04:42:20 +00:00
|
|
|
AssertIsOnMainThread();
|
2013-10-23 13:16:49 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> target = mSyncEventResponseTarget ?
|
|
|
|
mSyncEventResponseTarget :
|
|
|
|
mSyncLoopTarget;
|
|
|
|
return target.forget();
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
~Proxy()
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2013-10-23 13:16:49 +00:00
|
|
|
MOZ_ASSERT(!mXHR);
|
|
|
|
MOZ_ASSERT(!mXHRUpload);
|
|
|
|
MOZ_ASSERT(!mOutstandingSendCount);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
2012-03-31 04:42:20 +00:00
|
|
|
};
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
END_WORKERS_NAMESPACE
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
namespace {
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
inline void
|
2012-05-03 04:35:38 +00:00
|
|
|
ConvertResponseTypeToString(XMLHttpRequestResponseType aType,
|
|
|
|
nsString& aString)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
using namespace
|
2012-05-03 04:35:38 +00:00
|
|
|
mozilla::dom::XMLHttpRequestResponseTypeValues;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
size_t index = static_cast<size_t>(aType);
|
|
|
|
MOZ_ASSERT(index < ArrayLength(strings), "Codegen gave us a bad value!");
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
aString.AssignASCII(strings[index].value, strings[index].length);
|
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
inline XMLHttpRequestResponseType
|
|
|
|
ConvertStringToResponseType(const nsAString& aString)
|
|
|
|
{
|
|
|
|
using namespace
|
2012-05-03 04:35:38 +00:00
|
|
|
mozilla::dom::XMLHttpRequestResponseTypeValues;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
for (size_t index = 0; index < ArrayLength(strings) - 1; index++) {
|
|
|
|
if (aString.EqualsASCII(strings[index].value, strings[index].length)) {
|
|
|
|
return static_cast<XMLHttpRequestResponseType>(index);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
MOZ_ASSUME_UNREACHABLE("Don't know anything about this response type!");
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
STRING_abort = 0,
|
|
|
|
STRING_error,
|
|
|
|
STRING_load,
|
|
|
|
STRING_loadstart,
|
|
|
|
STRING_progress,
|
|
|
|
STRING_timeout,
|
|
|
|
STRING_readystatechange,
|
|
|
|
STRING_loadend,
|
|
|
|
|
|
|
|
STRING_COUNT,
|
|
|
|
|
|
|
|
STRING_LAST_XHR = STRING_loadend,
|
|
|
|
STRING_LAST_EVENTTARGET = STRING_timeout
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
static_assert(STRING_LAST_XHR >= STRING_LAST_EVENTTARGET, "Bad string setup!");
|
|
|
|
static_assert(STRING_LAST_XHR == STRING_COUNT - 1, "Bad string setup!");
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
const char* const sEventStrings[] = {
|
|
|
|
// nsIXMLHttpRequestEventTarget event types, supported by both XHR and Upload.
|
|
|
|
"abort",
|
|
|
|
"error",
|
|
|
|
"load",
|
|
|
|
"loadstart",
|
|
|
|
"progress",
|
|
|
|
"timeout",
|
|
|
|
|
|
|
|
// nsIXMLHttpRequest event types, supported only by XHR.
|
|
|
|
"readystatechange",
|
|
|
|
"loadend",
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
static_assert(MOZ_ARRAY_LENGTH(sEventStrings) == STRING_COUNT,
|
|
|
|
"Bad string count!");
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class MainThreadProxyRunnable : public MainThreadWorkerSyncRunnable
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
nsRefPtr<Proxy> mProxy;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
MainThreadProxyRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy)
|
|
|
|
: MainThreadWorkerSyncRunnable(aWorkerPrivate, aProxy->GetEventTarget()),
|
2012-03-31 04:42:20 +00:00
|
|
|
mProxy(aProxy)
|
2013-10-23 13:16:49 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aProxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~MainThreadProxyRunnable()
|
2012-03-31 04:42:20 +00:00
|
|
|
{ }
|
|
|
|
};
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class XHRUnpinRunnable MOZ_FINAL : public MainThreadWorkerControlRunnable
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
XMLHttpRequest* mXMLHttpRequestPrivate;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
public:
|
|
|
|
XHRUnpinRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
XMLHttpRequest* aXHRPrivate)
|
2013-10-23 13:16:49 +00:00
|
|
|
: MainThreadWorkerControlRunnable(aWorkerPrivate),
|
2012-03-31 04:42:20 +00:00
|
|
|
mXMLHttpRequestPrivate(aXHRPrivate)
|
|
|
|
{
|
2013-10-23 13:16:49 +00:00
|
|
|
MOZ_ASSERT(aXHRPrivate);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
~XHRUnpinRunnable()
|
|
|
|
{ }
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
mXMLHttpRequestPrivate->Unpin();
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-31 04:42:20 +00:00
|
|
|
};
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class AsyncTeardownRunnable MOZ_FINAL : public nsRunnable
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
nsRefPtr<Proxy> mProxy;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
public:
|
|
|
|
AsyncTeardownRunnable(Proxy* aProxy)
|
2013-10-23 13:16:49 +00:00
|
|
|
: mProxy(aProxy)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2013-10-23 13:16:49 +00:00
|
|
|
MOZ_ASSERT(aProxy);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
private:
|
|
|
|
~AsyncTeardownRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() MOZ_OVERRIDE
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-03-31 04:42:20 +00:00
|
|
|
AssertIsOnMainThread();
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
mProxy->Teardown();
|
2012-07-30 14:20:58 +00:00
|
|
|
mProxy = nullptr;
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
2012-03-31 04:42:20 +00:00
|
|
|
};
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class LoadStartDetectionRunnable MOZ_FINAL : public nsRunnable,
|
2012-06-15 02:31:55 +00:00
|
|
|
public nsIDOMEventListener
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
nsRefPtr<Proxy> mProxy;
|
|
|
|
nsRefPtr<nsXMLHttpRequest> mXHR;
|
|
|
|
XMLHttpRequest* mXMLHttpRequestPrivate;
|
|
|
|
nsString mEventType;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mChannelId;
|
2013-10-23 13:16:49 +00:00
|
|
|
bool mReceivedLoadStart;
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class ProxyCompleteRunnable MOZ_FINAL : public MainThreadProxyRunnable
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-03-31 04:42:20 +00:00
|
|
|
XMLHttpRequest* mXMLHttpRequestPrivate;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mChannelId;
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
ProxyCompleteRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
2012-08-22 15:56:38 +00:00
|
|
|
XMLHttpRequest* aXHRPrivate, uint32_t aChannelId)
|
2013-10-23 13:16:49 +00:00
|
|
|
: MainThreadProxyRunnable(aWorkerPrivate, aProxy),
|
2012-03-31 04:42:20 +00:00
|
|
|
mXMLHttpRequestPrivate(aXHRPrivate), mChannelId(aChannelId)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
~ProxyCompleteRunnable()
|
|
|
|
{ }
|
2011-11-08 01:01:29 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
virtual bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) MOZ_OVERRIDE
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
if (mChannelId != mProxy->mOuterChannelId) {
|
|
|
|
// Threads raced, this event is now obsolete.
|
|
|
|
return true;
|
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
if (mSyncLoopTarget) {
|
|
|
|
aWorkerPrivate->StopSyncLoop(mSyncLoopTarget, true);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
mXMLHttpRequestPrivate->Unpin();
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-10-23 13:16:49 +00:00
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Cancel() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
// This must run!
|
|
|
|
nsresult rv = MainThreadProxyRunnable::Cancel();
|
|
|
|
nsresult rv2 = Run();
|
|
|
|
return NS_FAILED(rv) ? rv : rv2;
|
|
|
|
}
|
2012-03-31 04:42:20 +00:00
|
|
|
};
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
public:
|
|
|
|
LoadStartDetectionRunnable(Proxy* aProxy, XMLHttpRequest* aXHRPrivate)
|
|
|
|
: mWorkerPrivate(aProxy->mWorkerPrivate), mProxy(aProxy), mXHR(aProxy->mXHR),
|
2013-10-23 13:16:49 +00:00
|
|
|
mXMLHttpRequestPrivate(aXHRPrivate), mChannelId(mProxy->mInnerChannelId),
|
|
|
|
mReceivedLoadStart(false)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-03-31 04:42:20 +00:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
mEventType.AssignWithConversion(sEventStrings[STRING_loadstart]);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
NS_DECL_NSIDOMEVENTLISTENER
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
bool
|
|
|
|
RegisterAndDispatch()
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-03-31 04:42:20 +00:00
|
|
|
AssertIsOnMainThread();
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
if (NS_FAILED(mXHR->AddEventListener(mEventType, this, false, false, 2))) {
|
|
|
|
NS_WARNING("Failed to add event listener!");
|
2011-07-17 19:09:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
return NS_SUCCEEDED(NS_DispatchToCurrentThread(this));
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
~LoadStartDetectionRunnable()
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
2012-03-31 04:42:20 +00:00
|
|
|
};
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class EventRunnable MOZ_FINAL : public MainThreadProxyRunnable
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
nsString mType;
|
|
|
|
nsString mResponseType;
|
|
|
|
JSAutoStructuredCloneBuffer mResponseBuffer;
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > mClonedObjects;
|
2013-07-23 09:58:27 +00:00
|
|
|
JS::Heap<JS::Value> mResponse;
|
2012-03-31 04:42:20 +00:00
|
|
|
nsString mResponseText;
|
2013-06-13 05:20:10 +00:00
|
|
|
nsCString mStatusText;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint64_t mLoaded;
|
|
|
|
uint64_t mTotal;
|
|
|
|
uint32_t mEventStreamId;
|
|
|
|
uint32_t mStatus;
|
|
|
|
uint16_t mReadyState;
|
2012-03-31 04:42:20 +00:00
|
|
|
bool mUploadEvent;
|
|
|
|
bool mProgressEvent;
|
|
|
|
bool mLengthComputable;
|
|
|
|
nsresult mResponseTextResult;
|
|
|
|
nsresult mStatusResult;
|
|
|
|
nsresult mResponseResult;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
public:
|
2013-07-23 09:58:27 +00:00
|
|
|
class StateDataAutoRooter : private JS::CustomAutoRooter
|
|
|
|
{
|
2013-10-23 13:16:49 +00:00
|
|
|
XMLHttpRequest::StateData* mStateData;
|
|
|
|
js::SkipRoot mSkip;
|
|
|
|
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
|
|
|
|
2013-07-23 09:58:27 +00:00
|
|
|
public:
|
|
|
|
explicit StateDataAutoRooter(JSContext* aCx, XMLHttpRequest::StateData* aData
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
: CustomAutoRooter(aCx), mStateData(aData), mSkip(aCx, mStateData)
|
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual void trace(JSTracer* aTrc)
|
|
|
|
{
|
|
|
|
JS_CallHeapValueTracer(aTrc, &mStateData->mResponse,
|
|
|
|
"XMLHttpRequest::StateData::mResponse");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
EventRunnable(Proxy* aProxy, bool aUploadEvent, const nsString& aType,
|
|
|
|
bool aLengthComputable, uint64_t aLoaded, uint64_t aTotal)
|
|
|
|
: MainThreadProxyRunnable(aProxy->mWorkerPrivate, aProxy), mType(aType),
|
|
|
|
mResponse(JSVAL_VOID), mLoaded(aLoaded), mTotal(aTotal),
|
|
|
|
mEventStreamId(aProxy->mInnerEventStreamId), mStatus(0), mReadyState(0),
|
|
|
|
mUploadEvent(aUploadEvent), mProgressEvent(true),
|
|
|
|
mLengthComputable(aLengthComputable), mResponseTextResult(NS_OK),
|
|
|
|
mStatusResult(NS_OK), mResponseResult(NS_OK)
|
|
|
|
{ }
|
2013-11-05 14:16:26 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
EventRunnable(Proxy* aProxy, bool aUploadEvent, const nsString& aType)
|
|
|
|
: MainThreadProxyRunnable(aProxy->mWorkerPrivate, aProxy), mType(aType),
|
|
|
|
mResponse(JSVAL_VOID), mLoaded(0), mTotal(0),
|
|
|
|
mEventStreamId(aProxy->mInnerEventStreamId), mStatus(0), mReadyState(0),
|
|
|
|
mUploadEvent(aUploadEvent), mProgressEvent(false), mLengthComputable(0),
|
|
|
|
mResponseTextResult(NS_OK), mStatusResult(NS_OK), mResponseResult(NS_OK)
|
|
|
|
{ }
|
2013-11-05 14:16:26 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
~EventRunnable()
|
|
|
|
{ }
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
virtual bool
|
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate) MOZ_OVERRIDE;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
virtual bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) MOZ_OVERRIDE;
|
2012-03-31 04:42:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class WorkerThreadProxySyncRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
nsRefPtr<Proxy> mProxy;
|
2013-10-23 13:16:49 +00:00
|
|
|
nsCOMPtr<nsIEventTarget> mSyncLoopTarget;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
private:
|
2013-10-23 13:16:49 +00:00
|
|
|
class ResponseRunnable MOZ_FINAL: public MainThreadStopSyncLoopRunnable
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2013-10-23 13:16:49 +00:00
|
|
|
nsRefPtr<Proxy> mProxy;
|
2012-04-24 23:50:00 +00:00
|
|
|
nsresult mErrorCode;
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
ResponseRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
2013-10-23 13:16:49 +00:00
|
|
|
nsresult aErrorCode)
|
|
|
|
: MainThreadStopSyncLoopRunnable(aWorkerPrivate, aProxy->GetEventTarget(),
|
|
|
|
NS_SUCCEEDED(aErrorCode)),
|
|
|
|
mProxy(aProxy), mErrorCode(aErrorCode)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2013-10-23 13:16:49 +00:00
|
|
|
MOZ_ASSERT(aProxy);
|
2011-12-01 21:30:28 +00:00
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
~ResponseRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
MaybeSetException(JSContext* aCx) MOZ_OVERRIDE
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2013-10-23 13:16:49 +00:00
|
|
|
MOZ_ASSERT(NS_FAILED(mErrorCode));
|
|
|
|
|
2014-01-11 00:37:47 +00:00
|
|
|
Throw(aCx, mErrorCode);
|
|
|
|
}
|
2012-03-31 04:42:20 +00:00
|
|
|
};
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
public:
|
|
|
|
WorkerThreadProxySyncRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy)
|
2013-10-23 13:16:49 +00:00
|
|
|
: mWorkerPrivate(aWorkerPrivate), mProxy(aProxy)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2013-10-23 13:16:49 +00:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
MOZ_ASSERT(aProxy);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2011-08-13 19:43:51 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
bool
|
|
|
|
Dispatch(JSContext* aCx)
|
2011-08-13 19:43:51 +00:00
|
|
|
{
|
2012-03-31 04:42:20 +00:00
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2012-12-21 20:14:47 +00:00
|
|
|
AutoSyncLoopHolder syncLoop(mWorkerPrivate);
|
2013-10-23 13:16:49 +00:00
|
|
|
mSyncLoopTarget = syncLoop.EventTarget();
|
2011-08-13 19:43:51 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(this, NS_DISPATCH_NORMAL))) {
|
|
|
|
JS_ReportError(aCx, "Failed to dispatch to main thread!");
|
2011-08-13 19:43:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
return syncLoop.Run();
|
2011-08-13 19:43:51 +00:00
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
protected:
|
|
|
|
virtual ~WorkerThreadProxySyncRunnable()
|
|
|
|
{ }
|
|
|
|
|
2012-04-24 23:50:00 +00:00
|
|
|
virtual nsresult
|
2012-03-31 04:42:20 +00:00
|
|
|
MainThreadRun() = 0;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
NS_DECL_NSIRUNNABLE
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class SyncTeardownRunnable MOZ_FINAL : public WorkerThreadProxySyncRunnable
|
2011-11-04 02:51:33 +00:00
|
|
|
{
|
2012-03-31 04:42:20 +00:00
|
|
|
public:
|
|
|
|
SyncTeardownRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy)
|
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy)
|
2013-10-23 13:16:49 +00:00
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
~SyncTeardownRunnable()
|
|
|
|
{ }
|
2011-11-04 02:51:33 +00:00
|
|
|
|
2012-04-24 23:50:00 +00:00
|
|
|
virtual nsresult
|
2013-10-23 13:16:49 +00:00
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mProxy->Teardown();
|
|
|
|
return NS_OK;
|
2011-11-04 02:51:33 +00:00
|
|
|
}
|
2012-03-31 04:42:20 +00:00
|
|
|
};
|
2011-11-04 02:51:33 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class SetBackgroundRequestRunnable MOZ_FINAL :
|
|
|
|
public WorkerThreadProxySyncRunnable
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
bool mValue;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
public:
|
|
|
|
SetBackgroundRequestRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
|
|
bool aValue)
|
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mValue(aValue)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
~SetBackgroundRequestRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2012-04-24 23:50:00 +00:00
|
|
|
return mProxy->mXHR->SetMozBackgroundRequest(mValue);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class SetWithCredentialsRunnable MOZ_FINAL :
|
|
|
|
public WorkerThreadProxySyncRunnable
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
bool mValue;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SetWithCredentialsRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
|
|
bool aValue)
|
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mValue(aValue)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
~SetWithCredentialsRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2012-04-24 23:50:00 +00:00
|
|
|
return mProxy->mXHR->SetWithCredentials(mValue);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class SetResponseTypeRunnable MOZ_FINAL : public WorkerThreadProxySyncRunnable
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
nsString mResponseType;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SetResponseTypeRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
|
|
const nsAString& aResponseType)
|
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy),
|
|
|
|
mResponseType(aResponseType)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
void
|
|
|
|
GetResponseType(nsAString& aResponseType)
|
|
|
|
{
|
|
|
|
aResponseType.Assign(mResponseType);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~SetResponseTypeRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
nsresult rv = mProxy->mXHR->SetResponseType(mResponseType);
|
|
|
|
mResponseType.Truncate();
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = mProxy->mXHR->GetResponseType(mResponseType);
|
|
|
|
}
|
2012-04-24 23:50:00 +00:00
|
|
|
return rv;
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class SetTimeoutRunnable MOZ_FINAL : public WorkerThreadProxySyncRunnable
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mTimeout;
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
SetTimeoutRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aTimeout)
|
2013-10-23 13:16:49 +00:00
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mTimeout(aTimeout)
|
2012-03-31 04:42:20 +00:00
|
|
|
{ }
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
~SetTimeoutRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2012-04-24 23:50:00 +00:00
|
|
|
return mProxy->mXHR->SetTimeout(mTimeout);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class AbortRunnable MOZ_FINAL : public WorkerThreadProxySyncRunnable
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
AbortRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy)
|
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
~AbortRunnable()
|
|
|
|
{ }
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE;
|
2012-03-31 04:42:20 +00:00
|
|
|
};
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class GetAllResponseHeadersRunnable MOZ_FINAL :
|
|
|
|
public WorkerThreadProxySyncRunnable
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2013-06-13 05:20:10 +00:00
|
|
|
nsCString& mResponseHeaders;
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
GetAllResponseHeadersRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
2013-06-13 05:20:10 +00:00
|
|
|
nsCString& aResponseHeaders)
|
2012-03-31 04:42:20 +00:00
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy),
|
|
|
|
mResponseHeaders(aResponseHeaders)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
~GetAllResponseHeadersRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mProxy->mXHR->GetAllResponseHeaders(mResponseHeaders);
|
2012-04-24 23:50:00 +00:00
|
|
|
return NS_OK;
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class GetResponseHeaderRunnable MOZ_FINAL : public WorkerThreadProxySyncRunnable
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
const nsCString mHeader;
|
|
|
|
nsCString& mValue;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GetResponseHeaderRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
2013-06-13 05:20:10 +00:00
|
|
|
const nsACString& aHeader, nsCString& aValue)
|
2012-03-31 04:42:20 +00:00
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mHeader(aHeader),
|
|
|
|
mValue(aValue)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
~GetResponseHeaderRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2012-04-24 23:50:00 +00:00
|
|
|
return mProxy->mXHR->GetResponseHeader(mHeader, mValue);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class OpenRunnable MOZ_FINAL : public WorkerThreadProxySyncRunnable
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2013-06-13 05:20:10 +00:00
|
|
|
nsCString mMethod;
|
2012-05-30 03:45:18 +00:00
|
|
|
nsString mURL;
|
|
|
|
Optional<nsAString> mUser;
|
|
|
|
nsString mUserStr;
|
|
|
|
Optional<nsAString> mPassword;
|
|
|
|
nsString mPasswordStr;
|
2012-03-31 04:42:20 +00:00
|
|
|
bool mBackgroundRequest;
|
|
|
|
bool mWithCredentials;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mTimeout;
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
OpenRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
2013-06-13 05:20:10 +00:00
|
|
|
const nsACString& aMethod, const nsAString& aURL,
|
2012-05-30 03:45:18 +00:00
|
|
|
const Optional<nsAString>& aUser,
|
|
|
|
const Optional<nsAString>& aPassword,
|
2013-03-04 09:06:15 +00:00
|
|
|
bool aBackgroundRequest, bool aWithCredentials,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aTimeout)
|
2012-03-31 04:42:20 +00:00
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mMethod(aMethod),
|
2013-10-23 13:16:49 +00:00
|
|
|
mURL(aURL), mBackgroundRequest(aBackgroundRequest),
|
|
|
|
mWithCredentials(aWithCredentials), mTimeout(aTimeout)
|
2012-05-30 03:45:18 +00:00
|
|
|
{
|
|
|
|
if (aUser.WasPassed()) {
|
|
|
|
mUserStr = aUser.Value();
|
|
|
|
mUser = &mUserStr;
|
|
|
|
}
|
|
|
|
if (aPassword.WasPassed()) {
|
|
|
|
mPasswordStr = aPassword.Value();
|
|
|
|
mPassword = &mPasswordStr;
|
|
|
|
}
|
|
|
|
}
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
~OpenRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
WorkerPrivate* oldWorker = mProxy->mWorkerPrivate;
|
|
|
|
mProxy->mWorkerPrivate = mWorkerPrivate;
|
|
|
|
|
2012-04-24 23:50:00 +00:00
|
|
|
nsresult rv = MainThreadRunInternal();
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
mProxy->mWorkerPrivate = oldWorker;
|
2012-04-24 23:50:00 +00:00
|
|
|
return rv;
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2012-04-24 23:50:00 +00:00
|
|
|
nsresult
|
2013-10-23 13:16:49 +00:00
|
|
|
MainThreadRunInternal();
|
2012-03-31 04:42:20 +00:00
|
|
|
};
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class SendRunnable MOZ_FINAL : public WorkerThreadProxySyncRunnable
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-03-31 04:42:20 +00:00
|
|
|
nsString mStringBody;
|
|
|
|
JSAutoStructuredCloneBuffer mBody;
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > mClonedObjects;
|
2013-10-23 13:16:49 +00:00
|
|
|
nsCOMPtr<nsIEventTarget> mSyncLoopTarget;
|
2012-03-31 04:42:20 +00:00
|
|
|
bool mHasUploadListeners;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SendRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
2014-02-01 02:50:07 +00:00
|
|
|
const nsAString& aStringBody, JSAutoStructuredCloneBuffer&& aBody,
|
|
|
|
nsTArray<nsCOMPtr<nsISupports>>& aClonedObjects,
|
2013-10-23 13:16:49 +00:00
|
|
|
nsIEventTarget* aSyncLoopTarget, bool aHasUploadListeners)
|
2014-02-01 02:50:07 +00:00
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy)
|
|
|
|
, mStringBody(aStringBody)
|
|
|
|
, mBody(Move(aBody))
|
|
|
|
, mSyncLoopTarget(aSyncLoopTarget)
|
|
|
|
, mHasUploadListeners(aHasUploadListeners)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mClonedObjects.SwapElements(aClonedObjects);
|
|
|
|
}
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
~SendRunnable()
|
|
|
|
{ }
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE;
|
2012-03-31 04:42:20 +00:00
|
|
|
};
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class SetRequestHeaderRunnable MOZ_FINAL : public WorkerThreadProxySyncRunnable
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
nsCString mHeader;
|
|
|
|
nsCString mValue;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SetRequestHeaderRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
2013-06-13 05:20:10 +00:00
|
|
|
const nsACString& aHeader, const nsACString& aValue)
|
2012-03-31 04:42:20 +00:00
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mHeader(aHeader),
|
|
|
|
mValue(aValue)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
~SetRequestHeaderRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2012-04-24 23:50:00 +00:00
|
|
|
return mProxy->mXHR->SetRequestHeader(mHeader, mValue);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class OverrideMimeTypeRunnable MOZ_FINAL : public WorkerThreadProxySyncRunnable
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
nsString mMimeType;
|
|
|
|
|
|
|
|
public:
|
|
|
|
OverrideMimeTypeRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
|
|
const nsAString& aMimeType)
|
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mMimeType(aMimeType)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
~OverrideMimeTypeRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mProxy->mXHR->OverrideMimeType(mMimeType);
|
2012-04-24 23:50:00 +00:00
|
|
|
return NS_OK;
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class AutoUnpinXHR
|
|
|
|
{
|
2013-10-23 13:16:49 +00:00
|
|
|
XMLHttpRequest* mXMLHttpRequestPrivate;
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
public:
|
|
|
|
AutoUnpinXHR(XMLHttpRequest* aXMLHttpRequestPrivate)
|
|
|
|
: mXMLHttpRequestPrivate(aXMLHttpRequestPrivate)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aXMLHttpRequestPrivate);
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoUnpinXHR()
|
|
|
|
{
|
|
|
|
if (mXMLHttpRequestPrivate) {
|
|
|
|
mXMLHttpRequestPrivate->Unpin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
2013-10-28 14:04:47 +00:00
|
|
|
mXMLHttpRequestPrivate = nullptr;
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
bool
|
|
|
|
Proxy::Init()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
|
|
|
if (mXHR) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPIDOMWindow* ownerWindow = mWorkerPrivate->GetWindow();
|
|
|
|
if (ownerWindow) {
|
|
|
|
ownerWindow = ownerWindow->GetOuterWindow();
|
|
|
|
if (!ownerWindow) {
|
|
|
|
NS_ERROR("No outer window?!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPIDOMWindow* innerWindow = ownerWindow->GetCurrentInnerWindow();
|
|
|
|
if (mWorkerPrivate->GetWindow() != innerWindow) {
|
|
|
|
NS_WARNING("Window has navigated, cannot create XHR here.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mXHR = new nsXMLHttpRequest();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(ownerWindow);
|
|
|
|
if (NS_FAILED(mXHR->Init(mWorkerPrivate->GetPrincipal(),
|
|
|
|
mWorkerPrivate->GetScriptContext(),
|
|
|
|
global, mWorkerPrivate->GetBaseURI()))) {
|
|
|
|
mXHR = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mXHR->SetParameters(mMozAnon, mMozSystem);
|
|
|
|
|
|
|
|
if (NS_FAILED(mXHR->GetUpload(getter_AddRefs(mXHRUpload)))) {
|
|
|
|
mXHR = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AddRemoveEventListeners(false, true)) {
|
|
|
|
mXHRUpload = nullptr;
|
|
|
|
mXHR = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
void
|
|
|
|
Proxy::Teardown()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (mXHR) {
|
|
|
|
Reset();
|
|
|
|
|
|
|
|
// NB: We are intentionally dropping events coming from xhr.abort on the
|
|
|
|
// floor.
|
|
|
|
AddRemoveEventListeners(false, false);
|
|
|
|
mXHR->Abort();
|
|
|
|
|
|
|
|
if (mOutstandingSendCount) {
|
|
|
|
nsRefPtr<XHRUnpinRunnable> runnable =
|
|
|
|
new XHRUnpinRunnable(mWorkerPrivate, mXMLHttpRequestPrivate);
|
2012-07-30 14:20:58 +00:00
|
|
|
if (!runnable->Dispatch(nullptr)) {
|
2012-03-31 04:42:20 +00:00
|
|
|
NS_RUNTIMEABORT("We're going to hang at shutdown anyways.");
|
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mWorkerPrivate = nullptr;
|
2012-03-31 04:42:20 +00:00
|
|
|
mOutstandingSendCount = 0;
|
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mXHRUpload = nullptr;
|
|
|
|
mXHR = nullptr;
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-03-31 04:42:20 +00:00
|
|
|
Proxy::AddRemoveEventListeners(bool aUpload, bool aAdd)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
NS_ASSERTION(!aUpload ||
|
|
|
|
(mUploadEventListenersAttached && !aAdd) ||
|
|
|
|
(!mUploadEventListenersAttached && aAdd),
|
|
|
|
"Messed up logic for upload listeners!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target =
|
|
|
|
aUpload ?
|
|
|
|
do_QueryInterface(mXHRUpload) :
|
|
|
|
do_QueryInterface(static_cast<nsIXMLHttpRequest*>(mXHR.get()));
|
|
|
|
NS_ASSERTION(target, "This should never fail!");
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t lastEventType = aUpload ? STRING_LAST_EVENTTARGET : STRING_LAST_XHR;
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
nsAutoString eventType;
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t index = 0; index <= lastEventType; index++) {
|
2012-03-31 04:42:20 +00:00
|
|
|
eventType = NS_ConvertASCIItoUTF16(sEventStrings[index]);
|
|
|
|
if (aAdd) {
|
|
|
|
if (NS_FAILED(target->AddEventListener(eventType, this, false))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (NS_FAILED(target->RemoveEventListener(eventType, this, false))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aUpload) {
|
|
|
|
mUploadEventListenersAttached = aAdd;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-19 02:21:20 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(Proxy, nsIDOMEventListener)
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Proxy::HandleEvent(nsIDOMEvent* aEvent)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (!mWorkerPrivate || !mXMLHttpRequestPrivate) {
|
|
|
|
NS_ERROR("Shouldn't get here!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString type;
|
|
|
|
if (NS_FAILED(aEvent->GetType(type))) {
|
|
|
|
NS_WARNING("Failed to get event type!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target;
|
|
|
|
if (NS_FAILED(aEvent->GetTarget(getter_AddRefs(target)))) {
|
|
|
|
NS_WARNING("Failed to get target!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIXMLHttpRequestUpload> uploadTarget = do_QueryInterface(target);
|
|
|
|
nsCOMPtr<nsIDOMProgressEvent> progressEvent = do_QueryInterface(aEvent);
|
|
|
|
|
|
|
|
nsRefPtr<EventRunnable> runnable;
|
|
|
|
|
|
|
|
if (mInOpen && type.EqualsASCII(sEventStrings[STRING_readystatechange])) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t readyState = 0;
|
2012-03-31 04:42:20 +00:00
|
|
|
if (NS_SUCCEEDED(mXHR->GetReadyState(&readyState)) &&
|
|
|
|
readyState == nsIXMLHttpRequest::OPENED) {
|
|
|
|
mInnerEventStreamId++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (progressEvent) {
|
|
|
|
bool lengthComputable;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint64_t loaded, total;
|
2012-03-31 04:42:20 +00:00
|
|
|
if (NS_FAILED(progressEvent->GetLengthComputable(&lengthComputable)) ||
|
|
|
|
NS_FAILED(progressEvent->GetLoaded(&loaded)) ||
|
|
|
|
NS_FAILED(progressEvent->GetTotal(&total))) {
|
|
|
|
NS_WARNING("Bad progress event!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
runnable = new EventRunnable(this, !!uploadTarget, type, lengthComputable,
|
|
|
|
loaded, total);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
runnable = new EventRunnable(this, !!uploadTarget, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-05-06 13:04:17 +00:00
|
|
|
AutoSafeJSContext cx;
|
2013-04-18 15:36:04 +00:00
|
|
|
JSAutoRequest ar(cx);
|
2012-03-31 04:42:20 +00:00
|
|
|
runnable->Dispatch(cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!uploadTarget) {
|
|
|
|
if (type.EqualsASCII(sEventStrings[STRING_loadstart])) {
|
|
|
|
NS_ASSERTION(!mMainThreadSeenLoadStart, "Huh?!");
|
|
|
|
mMainThreadSeenLoadStart = true;
|
|
|
|
}
|
|
|
|
else if (mMainThreadSeenLoadStart &&
|
|
|
|
type.EqualsASCII(sEventStrings[STRING_loadend])) {
|
|
|
|
mMainThreadSeenLoadStart = false;
|
|
|
|
|
|
|
|
nsRefPtr<LoadStartDetectionRunnable> runnable =
|
|
|
|
new LoadStartDetectionRunnable(this, mXMLHttpRequestPrivate);
|
2014-01-29 20:32:50 +00:00
|
|
|
if (!runnable->RegisterAndDispatch()) {
|
2012-03-31 04:42:20 +00:00
|
|
|
NS_WARNING("Failed to dispatch LoadStartDetectionRunnable!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(WorkerThreadProxySyncRunnable, nsRunnable)
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(AsyncTeardownRunnable, nsRunnable)
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED1(LoadStartDetectionRunnable, nsRunnable,
|
|
|
|
nsIDOMEventListener)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LoadStartDetectionRunnable::Run()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (NS_FAILED(mXHR->RemoveEventListener(mEventType, this, false))) {
|
|
|
|
NS_WARNING("Failed to remove event listener!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mReceivedLoadStart) {
|
|
|
|
if (mProxy->mOutstandingSendCount > 1) {
|
|
|
|
mProxy->mOutstandingSendCount--;
|
|
|
|
} else if (mProxy->mOutstandingSendCount == 1) {
|
|
|
|
mProxy->Reset();
|
|
|
|
|
|
|
|
nsRefPtr<ProxyCompleteRunnable> runnable =
|
|
|
|
new ProxyCompleteRunnable(mWorkerPrivate, mProxy,
|
|
|
|
mXMLHttpRequestPrivate, mChannelId);
|
|
|
|
if (runnable->Dispatch(nullptr)) {
|
|
|
|
mProxy->mWorkerPrivate = nullptr;
|
|
|
|
mProxy->mOutstandingSendCount--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mProxy = nullptr;
|
|
|
|
mXHR = nullptr;
|
|
|
|
mXMLHttpRequestPrivate = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LoadStartDetectionRunnable::HandleEvent(nsIDOMEvent* aEvent)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
nsString type;
|
|
|
|
if (NS_SUCCEEDED(aEvent->GetType(type))) {
|
|
|
|
MOZ_ASSERT(type == mEventType);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_WARNING("Failed to get event type!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mReceivedLoadStart = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
EventRunnable::PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsRefPtr<nsXMLHttpRequest>& xhr = mProxy->mXHR;
|
|
|
|
MOZ_ASSERT(xhr);
|
|
|
|
|
|
|
|
if (NS_FAILED(xhr->GetResponseType(mResponseType))) {
|
|
|
|
MOZ_ASSERT(false, "This should never fail!");
|
|
|
|
}
|
|
|
|
|
|
|
|
mResponseTextResult = xhr->GetResponseText(mResponseText);
|
|
|
|
if (NS_SUCCEEDED(mResponseTextResult)) {
|
|
|
|
mResponseResult = mResponseTextResult;
|
|
|
|
if (mResponseText.IsVoid()) {
|
|
|
|
mResponse = JSVAL_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
JS::Rooted<JS::Value> response(aCx);
|
2014-01-09 17:39:36 +00:00
|
|
|
mResponseResult = xhr->GetResponse(aCx, &response);
|
2013-10-23 13:16:49 +00:00
|
|
|
if (NS_SUCCEEDED(mResponseResult)) {
|
|
|
|
if (JSVAL_IS_UNIVERSAL(response)) {
|
|
|
|
mResponse = response;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Anything subject to GC must be cloned.
|
|
|
|
JSStructuredCloneCallbacks* callbacks =
|
|
|
|
aWorkerPrivate->IsChromeWorker() ?
|
|
|
|
ChromeWorkerStructuredCloneCallbacks(true) :
|
|
|
|
WorkerStructuredCloneCallbacks(true);
|
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > clonedObjects;
|
|
|
|
|
|
|
|
if (mResponseBuffer.write(aCx, response, callbacks, &clonedObjects)) {
|
|
|
|
mClonedObjects.SwapElements(clonedObjects);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_WARNING("Failed to clone response!");
|
|
|
|
mResponseResult = NS_ERROR_DOM_DATA_CLONE_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mStatusResult = xhr->GetStatus(&mStatus);
|
|
|
|
|
|
|
|
xhr->GetStatusText(mStatusText);
|
|
|
|
|
|
|
|
mReadyState = xhr->ReadyState();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
EventRunnable::WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
if (mEventStreamId != mProxy->mOuterEventStreamId) {
|
|
|
|
// Threads raced, this event is now obsolete.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mProxy->mXMLHttpRequestPrivate) {
|
|
|
|
// Object was finalized, bail.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mType.EqualsASCII(sEventStrings[STRING_loadstart])) {
|
|
|
|
if (mUploadEvent) {
|
|
|
|
mProxy->mSeenUploadLoadStart = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mProxy->mSeenLoadStart = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (mType.EqualsASCII(sEventStrings[STRING_loadend])) {
|
|
|
|
if (mUploadEvent) {
|
|
|
|
mProxy->mSeenUploadLoadStart = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mProxy->mSeenLoadStart = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (mType.EqualsASCII(sEventStrings[STRING_abort])) {
|
|
|
|
if ((mUploadEvent && !mProxy->mSeenUploadLoadStart) ||
|
|
|
|
(!mUploadEvent && !mProxy->mSeenLoadStart)) {
|
|
|
|
// We've already dispatched premature abort events.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (mType.EqualsASCII(sEventStrings[STRING_readystatechange])) {
|
|
|
|
if (mReadyState == 4 && !mUploadEvent && !mProxy->mSeenLoadStart) {
|
|
|
|
// We've already dispatched premature abort events.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mProgressEvent) {
|
|
|
|
// Cache these for premature abort events.
|
|
|
|
if (mUploadEvent) {
|
|
|
|
mProxy->mLastUploadLengthComputable = mLengthComputable;
|
|
|
|
mProxy->mLastUploadLoaded = mLoaded;
|
|
|
|
mProxy->mLastUploadTotal = mTotal;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mProxy->mLastLengthComputable = mLengthComputable;
|
|
|
|
mProxy->mLastLoaded = mLoaded;
|
|
|
|
mProxy->mLastTotal = mTotal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoPtr<XMLHttpRequest::StateData> state(new XMLHttpRequest::StateData());
|
|
|
|
StateDataAutoRooter rooter(aCx, state);
|
|
|
|
|
|
|
|
state->mResponseTextResult = mResponseTextResult;
|
|
|
|
state->mResponseText = mResponseText;
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(mResponseTextResult)) {
|
|
|
|
MOZ_ASSERT(JSVAL_IS_VOID(mResponse) || JSVAL_IS_NULL(mResponse));
|
|
|
|
state->mResponseResult = mResponseTextResult;
|
|
|
|
state->mResponse = mResponse;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state->mResponseResult = mResponseResult;
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(mResponseResult)) {
|
|
|
|
if (mResponseBuffer.data()) {
|
|
|
|
MOZ_ASSERT(JSVAL_IS_VOID(mResponse));
|
|
|
|
|
2014-02-01 02:50:07 +00:00
|
|
|
JSAutoStructuredCloneBuffer responseBuffer(Move(mResponseBuffer));
|
2013-10-23 13:16:49 +00:00
|
|
|
|
|
|
|
JSStructuredCloneCallbacks* callbacks =
|
|
|
|
aWorkerPrivate->IsChromeWorker() ?
|
|
|
|
ChromeWorkerStructuredCloneCallbacks(false) :
|
|
|
|
WorkerStructuredCloneCallbacks(false);
|
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > clonedObjects;
|
|
|
|
clonedObjects.SwapElements(mClonedObjects);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> response(aCx);
|
|
|
|
if (!responseBuffer.read(aCx, &response, callbacks, &clonedObjects)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
state->mResponse = response;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state->mResponse = mResponse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state->mStatusResult = mStatusResult;
|
|
|
|
state->mStatus = mStatus;
|
|
|
|
|
|
|
|
state->mStatusText = mStatusText;
|
|
|
|
|
|
|
|
state->mReadyState = mReadyState;
|
|
|
|
|
|
|
|
XMLHttpRequest* xhr = mProxy->mXMLHttpRequestPrivate;
|
|
|
|
xhr->UpdateState(*state);
|
|
|
|
|
|
|
|
if (mUploadEvent && !xhr->GetUploadObjectNoCreate()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSString*> type(aCx,
|
|
|
|
JS_NewUCStringCopyN(aCx, mType.get(), mType.Length()));
|
|
|
|
if (!type) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXHREventTarget* target;
|
|
|
|
if (mUploadEvent) {
|
|
|
|
target = xhr->GetUploadObjectNoCreate();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
target = xhr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(target);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
if (mProgressEvent) {
|
|
|
|
NS_NewDOMProgressEvent(getter_AddRefs(event), target, nullptr, nullptr);
|
|
|
|
nsCOMPtr<nsIDOMProgressEvent> progress = do_QueryInterface(event);
|
|
|
|
|
|
|
|
if (progress) {
|
|
|
|
progress->InitProgressEvent(mType, false, false, mLengthComputable,
|
|
|
|
mLoaded, mTotal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NewDOMEvent(getter_AddRefs(event), target, nullptr, nullptr);
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
event->InitEvent(mType, false, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
|
|
target->DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
|
|
|
|
|
|
|
// After firing the event set mResponse to JSVAL_NULL for chunked response
|
|
|
|
// types.
|
|
|
|
if (StringBeginsWith(mResponseType, NS_LITERAL_STRING("moz-chunked-"))) {
|
|
|
|
xhr->NullResponseText();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerThreadProxySyncRunnable::Run()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> tempTarget;
|
|
|
|
mSyncLoopTarget.swap(tempTarget);
|
|
|
|
|
|
|
|
mProxy->mSyncEventResponseTarget.swap(tempTarget);
|
|
|
|
|
|
|
|
nsresult rv = MainThreadRun();
|
|
|
|
|
|
|
|
nsRefPtr<ResponseRunnable> response =
|
|
|
|
new ResponseRunnable(mWorkerPrivate, mProxy, rv);
|
|
|
|
if (!response->Dispatch(nullptr)) {
|
|
|
|
MOZ_ASSERT(false, "Failed to dispatch response!");
|
|
|
|
}
|
|
|
|
|
|
|
|
mProxy->mSyncEventResponseTarget.swap(tempTarget);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
AbortRunnable::MainThreadRun()
|
|
|
|
{
|
|
|
|
mProxy->mInnerEventStreamId++;
|
|
|
|
|
|
|
|
WorkerPrivate* oldWorker = mProxy->mWorkerPrivate;
|
|
|
|
mProxy->mWorkerPrivate = mWorkerPrivate;
|
|
|
|
|
|
|
|
mProxy->mXHR->Abort();
|
|
|
|
|
|
|
|
mProxy->mWorkerPrivate = oldWorker;
|
|
|
|
|
|
|
|
mProxy->Reset();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
OpenRunnable::MainThreadRunInternal()
|
|
|
|
{
|
|
|
|
if (!mProxy->Init()) {
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (mBackgroundRequest) {
|
|
|
|
rv = mProxy->mXHR->SetMozBackgroundRequest(mBackgroundRequest);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mWithCredentials) {
|
|
|
|
rv = mProxy->mXHR->SetWithCredentials(mWithCredentials);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mTimeout) {
|
|
|
|
rv = mProxy->mXHR->SetTimeout(mTimeout);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mProxy->mInOpen);
|
|
|
|
mProxy->mInOpen = true;
|
|
|
|
|
|
|
|
ErrorResult rv2;
|
|
|
|
mProxy->mXHR->Open(mMethod, mURL, true, mUser, mPassword, rv2);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mProxy->mInOpen);
|
|
|
|
mProxy->mInOpen = false;
|
|
|
|
|
|
|
|
if (rv2.Failed()) {
|
|
|
|
return rv2.ErrorCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
return mProxy->mXHR->SetResponseType(NS_LITERAL_STRING("text"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
SendRunnable::MainThreadRun()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIVariant> variant;
|
|
|
|
|
|
|
|
if (mBody.data()) {
|
|
|
|
AutoSafeJSContext cx;
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
|
|
|
|
nsIXPConnect* xpc = nsContentUtils::XPConnect();
|
|
|
|
MOZ_ASSERT(xpc);
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
JSStructuredCloneCallbacks* callbacks =
|
|
|
|
mWorkerPrivate->IsChromeWorker() ?
|
|
|
|
ChromeWorkerStructuredCloneCallbacks(true) :
|
|
|
|
WorkerStructuredCloneCallbacks(true);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> body(cx);
|
|
|
|
if (mBody.read(cx, &body, callbacks, &mClonedObjects)) {
|
2014-01-09 17:39:36 +00:00
|
|
|
if (NS_FAILED(xpc->JSValToVariant(cx, body, getter_AddRefs(variant)))) {
|
2013-10-23 13:16:49 +00:00
|
|
|
rv = NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rv = NS_ERROR_DOM_DATA_CLONE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBody.clear();
|
|
|
|
mClonedObjects.Clear();
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nsCOMPtr<nsIWritableVariant> wvariant =
|
|
|
|
do_CreateInstance(NS_VARIANT_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(wvariant, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
if (NS_FAILED(wvariant->SetAsAString(mStringBody))) {
|
|
|
|
MOZ_ASSERT(false, "This should never fail!");
|
|
|
|
}
|
|
|
|
|
|
|
|
variant = wvariant;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mProxy->mWorkerPrivate);
|
|
|
|
mProxy->mWorkerPrivate = mWorkerPrivate;
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mProxy->mSyncLoopTarget);
|
|
|
|
mProxy->mSyncLoopTarget.swap(mSyncLoopTarget);
|
|
|
|
|
|
|
|
if (mHasUploadListeners) {
|
|
|
|
NS_ASSERTION(!mProxy->mUploadEventListenersAttached, "Huh?!");
|
|
|
|
if (!mProxy->AddRemoveEventListeners(true, true)) {
|
|
|
|
MOZ_ASSERT(false, "This should never fail!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mProxy->mInnerChannelId++;
|
|
|
|
|
|
|
|
nsresult rv = mProxy->mXHR->Send(variant);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mProxy->mOutstandingSendCount++;
|
|
|
|
|
|
|
|
if (!mHasUploadListeners) {
|
|
|
|
NS_ASSERTION(!mProxy->mUploadEventListenersAttached, "Huh?!");
|
|
|
|
if (!mProxy->AddRemoveEventListeners(true, true)) {
|
|
|
|
MOZ_ASSERT(false, "This should never fail!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
XMLHttpRequest::XMLHttpRequest(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate),
|
2013-05-06 19:28:13 +00:00
|
|
|
mResponseType(XMLHttpRequestResponseType::Text), mTimeout(0),
|
2013-11-05 14:16:26 +00:00
|
|
|
mRooted(false), mBackgroundRequest(false), mWithCredentials(false),
|
|
|
|
mCanceled(false), mMozAnon(false), mMozSystem(false)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-11-05 14:16:26 +00:00
|
|
|
|
|
|
|
SetIsDOMBinding();
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
XMLHttpRequest::~XMLHttpRequest()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
ReleaseProxy(XHRIsGoingAway);
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mRooted);
|
|
|
|
|
|
|
|
mozilla::DropJSObjects(this);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(XMLHttpRequest, nsXHREventTarget)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(XMLHttpRequest, nsXHREventTarget)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(XMLHttpRequest)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsXHREventTarget)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(XMLHttpRequest)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(XMLHttpRequest,
|
|
|
|
nsXHREventTarget)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mUpload)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(XMLHttpRequest,
|
|
|
|
nsXHREventTarget)
|
|
|
|
tmp->ReleaseProxy(XHRIsGoingAway);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mUpload)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(XMLHttpRequest,
|
|
|
|
nsXHREventTarget)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JSVAL_MEMBER_CALLBACK(mStateData.mResponse)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
XMLHttpRequest::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2013-11-05 14:16:26 +00:00
|
|
|
return XMLHttpRequestBinding_workers::Wrap(aCx, aScope, this);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2013-11-05 14:16:26 +00:00
|
|
|
already_AddRefed<XMLHttpRequest>
|
2013-08-23 05:17:08 +00:00
|
|
|
XMLHttpRequest::Constructor(const GlobalObject& aGlobal,
|
2013-08-23 05:17:09 +00:00
|
|
|
const MozXMLHttpRequestParameters& aParams,
|
2012-06-07 18:28:33 +00:00
|
|
|
ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2012-12-03 16:07:49 +00:00
|
|
|
JSContext* cx = aGlobal.GetContext();
|
|
|
|
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx);
|
2012-03-31 04:42:20 +00:00
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
nsRefPtr<XMLHttpRequest> xhr = new XMLHttpRequest(workerPrivate);
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2012-09-17 00:20:16 +00:00
|
|
|
if (workerPrivate->XHRParamsAllowed()) {
|
2013-10-16 17:20:51 +00:00
|
|
|
if (aParams.mMozSystem)
|
|
|
|
xhr->mMozAnon = true;
|
|
|
|
else
|
|
|
|
xhr->mMozAnon = aParams.mMozAnon;
|
2012-12-17 21:44:04 +00:00
|
|
|
xhr->mMozSystem = aParams.mMozSystem;
|
2012-09-17 00:20:16 +00:00
|
|
|
}
|
2012-06-07 18:28:33 +00:00
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
return xhr.forget();
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XMLHttpRequest::ReleaseProxy(ReleaseType aType)
|
|
|
|
{
|
|
|
|
// Can't assert that we're on the worker thread here because mWorkerPrivate
|
|
|
|
// may be gone.
|
|
|
|
|
|
|
|
if (mProxy) {
|
|
|
|
if (aType == XHRIsGoingAway) {
|
|
|
|
// We're in a GC finalizer, so we can't do a sync call here (and we don't
|
|
|
|
// need to).
|
|
|
|
nsRefPtr<AsyncTeardownRunnable> runnable =
|
|
|
|
new AsyncTeardownRunnable(mProxy);
|
2012-07-30 14:20:58 +00:00
|
|
|
mProxy = nullptr;
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2012-08-14 09:10:41 +00:00
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(runnable))) {
|
2012-03-31 04:42:20 +00:00
|
|
|
NS_ERROR("Failed to dispatch teardown runnable!");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This isn't necessary if the worker is going away or the XHR is going
|
|
|
|
// away.
|
|
|
|
if (aType == Default) {
|
|
|
|
// Don't let any more events run.
|
|
|
|
mProxy->mOuterEventStreamId++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to make a sync call here.
|
|
|
|
nsRefPtr<SyncTeardownRunnable> runnable =
|
|
|
|
new SyncTeardownRunnable(mWorkerPrivate, mProxy);
|
2012-07-30 14:20:58 +00:00
|
|
|
mProxy = nullptr;
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
if (!runnable->Dispatch(nullptr)) {
|
2012-03-31 04:42:20 +00:00
|
|
|
NS_ERROR("Failed to dispatch teardown runnable!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-06 01:15:11 +00:00
|
|
|
XMLHttpRequest::MaybePin(ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
if (mRooted) {
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
JSContext* cx = GetCurrentThreadJSContext();
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
if (!mWorkerPrivate->AddFeature(cx, this)) {
|
2014-01-28 17:51:44 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
NS_ADDREF_THIS();
|
|
|
|
|
|
|
|
mRooted = true;
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-06 01:15:11 +00:00
|
|
|
XMLHttpRequest::MaybeDispatchPrematureAbortEvents(ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(mProxy);
|
|
|
|
|
|
|
|
mStateData.mReadyState = 4;
|
|
|
|
|
|
|
|
if (mProxy->mSeenUploadLoadStart) {
|
|
|
|
MOZ_ASSERT(mUpload);
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
DispatchPrematureAbortEvent(mUpload, NS_LITERAL_STRING("abort"), true,
|
|
|
|
aRv);
|
2012-05-06 01:15:11 +00:00
|
|
|
if (aRv.Failed()) {
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
DispatchPrematureAbortEvent(mUpload, NS_LITERAL_STRING("loadend"), true,
|
|
|
|
aRv);
|
2012-05-06 01:15:11 +00:00
|
|
|
if (aRv.Failed()) {
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mProxy->mSeenUploadLoadStart = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mProxy->mSeenLoadStart) {
|
2013-11-05 14:16:26 +00:00
|
|
|
DispatchPrematureAbortEvent(this, NS_LITERAL_STRING("readystatechange"),
|
|
|
|
false, aRv);
|
2012-05-06 01:15:11 +00:00
|
|
|
if (aRv.Failed()) {
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
DispatchPrematureAbortEvent(this, NS_LITERAL_STRING("abort"), false, aRv);
|
2012-05-06 01:15:11 +00:00
|
|
|
if (aRv.Failed()) {
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
DispatchPrematureAbortEvent(this, NS_LITERAL_STRING("loadend"), false,
|
|
|
|
aRv);
|
2012-05-06 01:15:11 +00:00
|
|
|
if (aRv.Failed()) {
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mProxy->mSeenLoadStart = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-05 14:16:26 +00:00
|
|
|
XMLHttpRequest::DispatchPrematureAbortEvent(EventTarget* aTarget,
|
|
|
|
const nsAString& aEventType,
|
2012-05-06 01:15:11 +00:00
|
|
|
bool aUploadTarget,
|
|
|
|
ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(aTarget);
|
|
|
|
|
2012-07-25 20:49:24 +00:00
|
|
|
if (!mProxy) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
if (aEventType.EqualsLiteral("readystatechange")) {
|
|
|
|
NS_NewDOMEvent(getter_AddRefs(event), aTarget, nullptr, nullptr);
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
if (event) {
|
|
|
|
event->InitEvent(aEventType, false, false);
|
|
|
|
}
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-11-05 14:16:26 +00:00
|
|
|
NS_NewDOMProgressEvent(getter_AddRefs(event), aTarget, nullptr, nullptr);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMProgressEvent> progress = do_QueryInterface(event);
|
|
|
|
if (progress) {
|
|
|
|
if (aUploadTarget) {
|
|
|
|
progress->InitProgressEvent(aEventType, false, false,
|
|
|
|
mProxy->mLastUploadLengthComputable,
|
|
|
|
mProxy->mLastUploadLoaded,
|
|
|
|
mProxy->mLastUploadTotal);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
progress->InitProgressEvent(aEventType, false, false,
|
|
|
|
mProxy->mLastLengthComputable,
|
|
|
|
mProxy->mLastLoaded,
|
|
|
|
mProxy->mLastTotal);
|
|
|
|
}
|
|
|
|
}
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!event) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
|
|
aTarget->DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XMLHttpRequest::Unpin()
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-03-31 04:42:20 +00:00
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
MOZ_ASSERT(mRooted, "Mismatched calls to Unpin!");
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
JSContext* cx = GetCurrentThreadJSContext();
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
mWorkerPrivate->RemoveFeature(cx, this);
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
mRooted = false;
|
|
|
|
|
|
|
|
NS_RELEASE_THIS();
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
void
|
|
|
|
XMLHttpRequest::SendInternal(const nsAString& aStringBody,
|
2014-02-01 02:50:07 +00:00
|
|
|
JSAutoStructuredCloneBuffer&& aBody,
|
2012-03-31 04:42:20 +00:00
|
|
|
nsTArray<nsCOMPtr<nsISupports> >& aClonedObjects,
|
2012-05-06 01:15:11 +00:00
|
|
|
ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
bool hasUploadListeners = mUpload ? mUpload->HasListeners() : false;
|
|
|
|
|
|
|
|
MaybePin(aRv);
|
2012-05-06 01:15:11 +00:00
|
|
|
if (aRv.Failed()) {
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoUnpinXHR autoUnpin(this);
|
2012-12-21 20:14:47 +00:00
|
|
|
Maybe<AutoSyncLoopHolder> autoSyncLoop;
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
nsCOMPtr<nsIEventTarget> syncLoopTarget;
|
2012-12-21 20:14:47 +00:00
|
|
|
bool isSyncXHR = mProxy->mIsSyncXHR;
|
|
|
|
if (isSyncXHR) {
|
|
|
|
autoSyncLoop.construct(mWorkerPrivate);
|
2013-10-23 13:16:49 +00:00
|
|
|
syncLoopTarget = autoSyncLoop.ref().EventTarget();
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mProxy->mOuterChannelId++;
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
JSContext* cx = mWorkerPrivate->GetJSContext();
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
nsRefPtr<SendRunnable> runnable =
|
2014-02-01 02:50:07 +00:00
|
|
|
new SendRunnable(mWorkerPrivate, mProxy, aStringBody, Move(aBody),
|
2013-10-23 13:16:49 +00:00
|
|
|
aClonedObjects, syncLoopTarget, hasUploadListeners);
|
2012-03-31 04:42:20 +00:00
|
|
|
if (!runnable->Dispatch(cx)) {
|
2013-04-03 05:12:15 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-21 20:14:47 +00:00
|
|
|
if (!isSyncXHR) {
|
|
|
|
autoUnpin.Clear();
|
|
|
|
MOZ_ASSERT(autoSyncLoop.empty());
|
|
|
|
return;
|
|
|
|
}
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2012-12-21 20:14:47 +00:00
|
|
|
autoUnpin.Clear();
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
if (!autoSyncLoop.ref().Run()) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-01-18 18:05:38 +00:00
|
|
|
bool
|
2012-03-31 04:42:20 +00:00
|
|
|
XMLHttpRequest::Notify(JSContext* aCx, Status aStatus)
|
2012-01-18 18:05:38 +00:00
|
|
|
{
|
2012-03-31 04:42:20 +00:00
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-11-05 14:16:26 +00:00
|
|
|
MOZ_ASSERT(mWorkerPrivate->GetJSContext() == aCx);
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
if (aStatus >= Canceling && !mCanceled) {
|
|
|
|
mCanceled = true;
|
|
|
|
ReleaseProxy(WorkerIsGoingAway);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-01-18 18:05:38 +00:00
|
|
|
}
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
void
|
2013-06-13 05:20:10 +00:00
|
|
|
XMLHttpRequest::Open(const nsACString& aMethod, const nsAString& aUrl,
|
2012-05-30 03:45:18 +00:00
|
|
|
bool aAsync, const Optional<nsAString>& aUser,
|
|
|
|
const Optional<nsAString>& aPassword, ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mProxy) {
|
|
|
|
MaybeDispatchPrematureAbortEvents(aRv);
|
2012-05-06 01:15:11 +00:00
|
|
|
if (aRv.Failed()) {
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2012-09-17 00:20:16 +00:00
|
|
|
mProxy = new Proxy(this, mMozAnon, mMozSystem);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mProxy->mOuterEventStreamId++;
|
|
|
|
|
|
|
|
nsRefPtr<OpenRunnable> runnable =
|
2012-05-30 03:45:18 +00:00
|
|
|
new OpenRunnable(mWorkerPrivate, mProxy, aMethod, aUrl, aUser, aPassword,
|
2013-03-04 09:06:15 +00:00
|
|
|
mBackgroundRequest, mWithCredentials,
|
2012-05-30 03:45:18 +00:00
|
|
|
mTimeout);
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-03-31 04:42:20 +00:00
|
|
|
ReleaseProxy();
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mProxy->mIsSyncXHR = !aAsync;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-06-13 05:20:10 +00:00
|
|
|
XMLHttpRequest::SetRequestHeader(const nsACString& aHeader,
|
|
|
|
const nsACString& aValue, ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mProxy) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<SetRequestHeaderRunnable> runnable =
|
2013-06-13 05:20:10 +00:00
|
|
|
new SetRequestHeaderRunnable(mWorkerPrivate, mProxy, aHeader, aValue);
|
2013-11-05 14:16:26 +00:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-06 01:15:11 +00:00
|
|
|
XMLHttpRequest::SetTimeout(uint32_t aTimeout, ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTimeout = aTimeout;
|
|
|
|
|
|
|
|
if (!mProxy) {
|
|
|
|
// Open may not have been called yet, in which case we'll handle the
|
|
|
|
// timeout in OpenRunnable.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<SetTimeoutRunnable> runnable =
|
|
|
|
new SetTimeoutRunnable(mWorkerPrivate, mProxy, aTimeout);
|
2013-11-05 14:16:26 +00:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-06 01:15:11 +00:00
|
|
|
XMLHttpRequest::SetWithCredentials(bool aWithCredentials, ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mWithCredentials = aWithCredentials;
|
|
|
|
|
|
|
|
if (!mProxy) {
|
|
|
|
// Open may not have been called yet, in which case we'll handle the
|
|
|
|
// credentials in OpenRunnable.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<SetWithCredentialsRunnable> runnable =
|
|
|
|
new SetWithCredentialsRunnable(mWorkerPrivate, mProxy, aWithCredentials);
|
2013-11-05 14:16:26 +00:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XMLHttpRequest::SetMozBackgroundRequest(bool aBackgroundRequest,
|
2012-05-06 01:15:11 +00:00
|
|
|
ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBackgroundRequest = aBackgroundRequest;
|
|
|
|
|
|
|
|
if (!mProxy) {
|
|
|
|
// Open may not have been called yet, in which case we'll handle the
|
|
|
|
// background request in OpenRunnable.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<SetBackgroundRequestRunnable> runnable =
|
|
|
|
new SetBackgroundRequestRunnable(mWorkerPrivate, mProxy,
|
|
|
|
aBackgroundRequest);
|
2013-11-05 14:16:26 +00:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLHttpRequestUpload*
|
2012-05-06 01:15:11 +00:00
|
|
|
XMLHttpRequest::GetUpload(ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2013-10-28 14:04:47 +00:00
|
|
|
return nullptr;
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mUpload) {
|
2013-11-05 14:16:26 +00:00
|
|
|
mUpload = XMLHttpRequestUpload::Create(this);
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
if (!mUpload) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2013-10-28 14:04:47 +00:00
|
|
|
return nullptr;
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mUpload;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-06 01:15:11 +00:00
|
|
|
XMLHttpRequest::Send(ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mProxy) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to clone.
|
|
|
|
JSAutoStructuredCloneBuffer buffer;
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > clonedObjects;
|
|
|
|
|
2014-02-01 02:50:07 +00:00
|
|
|
SendInternal(NullString(), Move(buffer), clonedObjects, aRv);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-06 01:15:11 +00:00
|
|
|
XMLHttpRequest::Send(const nsAString& aBody, ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mProxy) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to clone.
|
|
|
|
JSAutoStructuredCloneBuffer buffer;
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > clonedObjects;
|
|
|
|
|
2014-02-01 02:50:07 +00:00
|
|
|
SendInternal(aBody, Move(buffer), clonedObjects, aRv);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-12-20 19:28:18 +00:00
|
|
|
XMLHttpRequest::Send(JS::Handle<JSObject*> aBody, ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2013-11-21 18:56:35 +00:00
|
|
|
JSContext* cx = mWorkerPrivate->GetJSContext();
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
MOZ_ASSERT(aBody);
|
2013-11-21 18:56:35 +00:00
|
|
|
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mProxy) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-23 00:18:32 +00:00
|
|
|
JS::Rooted<JS::Value> valToClone(cx);
|
2013-12-20 19:28:18 +00:00
|
|
|
if (JS_IsArrayBufferObject(aBody) || JS_IsArrayBufferViewObject(aBody) ||
|
|
|
|
file::GetDOMBlobFromJSObject(aBody)) {
|
|
|
|
valToClone.setObject(*aBody);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-12-20 19:28:18 +00:00
|
|
|
JS::Rooted<JS::Value> obj(cx, JS::ObjectValue(*aBody));
|
2013-11-16 12:31:36 +00:00
|
|
|
JSString* bodyStr = JS::ToString(cx, obj);
|
2012-03-31 04:42:20 +00:00
|
|
|
if (!bodyStr) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-10-23 00:18:32 +00:00
|
|
|
valToClone.setString(bodyStr);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSStructuredCloneCallbacks* callbacks =
|
|
|
|
mWorkerPrivate->IsChromeWorker() ?
|
|
|
|
ChromeWorkerStructuredCloneCallbacks(false) :
|
|
|
|
WorkerStructuredCloneCallbacks(false);
|
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > clonedObjects;
|
|
|
|
|
|
|
|
JSAutoStructuredCloneBuffer buffer;
|
|
|
|
if (!buffer.write(cx, valToClone, callbacks, &clonedObjects)) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-01 02:50:07 +00:00
|
|
|
SendInternal(EmptyString(), Move(buffer), clonedObjects, aRv);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 19:28:18 +00:00
|
|
|
void
|
|
|
|
XMLHttpRequest::Send(const ArrayBuffer& aBody, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> obj(mWorkerPrivate->GetJSContext(), aBody.Obj());
|
|
|
|
return Send(obj, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XMLHttpRequest::Send(const ArrayBufferView& aBody, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> obj(mWorkerPrivate->GetJSContext(), aBody.Obj());
|
|
|
|
return Send(obj, aRv);
|
|
|
|
}
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
void
|
2012-05-06 01:15:11 +00:00
|
|
|
XMLHttpRequest::SendAsBinary(const nsAString& aBody, ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
NS_NOTYETIMPLEMENTED("Implement me!");
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
return;
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-06 01:15:11 +00:00
|
|
|
XMLHttpRequest::Abort(ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mProxy) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MaybeDispatchPrematureAbortEvents(aRv);
|
2012-05-06 01:15:11 +00:00
|
|
|
if (aRv.Failed()) {
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mProxy->mOuterEventStreamId++;
|
|
|
|
|
|
|
|
nsRefPtr<AbortRunnable> runnable = new AbortRunnable(mWorkerPrivate, mProxy);
|
2013-11-05 14:16:26 +00:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-06-13 05:20:10 +00:00
|
|
|
XMLHttpRequest::GetResponseHeader(const nsACString& aHeader,
|
|
|
|
nsACString& aResponseHeader, ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mProxy) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-13 05:20:10 +00:00
|
|
|
nsCString responseHeader;
|
2012-03-31 04:42:20 +00:00
|
|
|
nsRefPtr<GetResponseHeaderRunnable> runnable =
|
2013-06-13 05:20:10 +00:00
|
|
|
new GetResponseHeaderRunnable(mWorkerPrivate, mProxy, aHeader,
|
|
|
|
responseHeader);
|
2013-11-05 14:16:26 +00:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-06-13 05:20:10 +00:00
|
|
|
aResponseHeader = responseHeader;
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-06-13 05:20:10 +00:00
|
|
|
XMLHttpRequest::GetAllResponseHeaders(nsACString& aResponseHeaders,
|
2012-05-06 01:15:11 +00:00
|
|
|
ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mProxy) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-13 05:20:10 +00:00
|
|
|
nsCString responseHeaders;
|
2012-03-31 04:42:20 +00:00
|
|
|
nsRefPtr<GetAllResponseHeadersRunnable> runnable =
|
|
|
|
new GetAllResponseHeadersRunnable(mWorkerPrivate, mProxy, responseHeaders);
|
2013-11-05 14:16:26 +00:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aResponseHeaders = responseHeaders;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-06 01:15:11 +00:00
|
|
|
XMLHttpRequest::OverrideMimeType(const nsAString& aMimeType, ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're supposed to throw if the state is not OPENED or HEADERS_RECEIVED. We
|
|
|
|
// can detect OPENED really easily but we can't detect HEADERS_RECEIVED in a
|
|
|
|
// non-racy way until the XHR state machine actually runs on this thread
|
|
|
|
// (bug 671047). For now we're going to let this work only if the Send()
|
|
|
|
// method has not been called.
|
|
|
|
if (!mProxy || SendInProgress()) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<OverrideMimeTypeRunnable> runnable =
|
|
|
|
new OverrideMimeTypeRunnable(mWorkerPrivate, mProxy, aMimeType);
|
2013-11-05 14:16:26 +00:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XMLHttpRequest::SetResponseType(XMLHttpRequestResponseType aResponseType,
|
2012-05-06 01:15:11 +00:00
|
|
|
ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mProxy || SendInProgress()) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// "document" is fine for the main thread but not for a worker. Short-circuit
|
|
|
|
// that here.
|
2013-05-06 19:28:13 +00:00
|
|
|
if (aResponseType == XMLHttpRequestResponseType::Document) {
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString responseType;
|
|
|
|
ConvertResponseTypeToString(aResponseType, responseType);
|
|
|
|
|
|
|
|
nsRefPtr<SetResponseTypeRunnable> runnable =
|
|
|
|
new SetResponseTypeRunnable(mWorkerPrivate, mProxy, responseType);
|
2013-11-05 14:16:26 +00:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 04:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString acceptedResponseTypeString;
|
|
|
|
runnable->GetResponseType(acceptedResponseTypeString);
|
|
|
|
|
|
|
|
mResponseType = ConvertStringToResponseType(acceptedResponseTypeString);
|
|
|
|
}
|
|
|
|
|
|
|
|
jsval
|
2012-05-31 18:16:48 +00:00
|
|
|
XMLHttpRequest::GetResponse(JSContext* /* unused */, ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
if (NS_SUCCEEDED(mStateData.mResponseTextResult) &&
|
|
|
|
JSVAL_IS_VOID(mStateData.mResponse)) {
|
|
|
|
MOZ_ASSERT(mStateData.mResponseText.Length());
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(mStateData.mResponseResult));
|
|
|
|
|
|
|
|
JSString* str =
|
2013-11-05 14:16:26 +00:00
|
|
|
JS_NewUCStringCopyN(mWorkerPrivate->GetJSContext(),
|
|
|
|
mStateData.mResponseText.get(),
|
2012-03-31 04:42:20 +00:00
|
|
|
mStateData.mResponseText.Length());
|
|
|
|
if (!str) {
|
2012-05-06 01:15:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2012-03-31 04:42:20 +00:00
|
|
|
return JSVAL_VOID;
|
|
|
|
}
|
|
|
|
|
|
|
|
mStateData.mResponse = STRING_TO_JSVAL(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
aRv = mStateData.mResponseResult;
|
|
|
|
return mStateData.mResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-06 01:15:11 +00:00
|
|
|
XMLHttpRequest::GetResponseText(nsAString& aResponseText, ErrorResult& aRv)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
aRv = mStateData.mResponseTextResult;
|
|
|
|
aResponseText = mStateData.mResponseText;
|
|
|
|
}
|
2013-11-05 14:16:26 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
XMLHttpRequest::UpdateState(const StateData& aStateData)
|
|
|
|
{
|
|
|
|
mStateData = aStateData;
|
|
|
|
if (JSVAL_IS_GCTHING(mStateData.mResponse)) {
|
|
|
|
mozilla::HoldJSObjects(this);
|
|
|
|
}
|
|
|
|
}
|