2011-07-17 23:09:13 +04:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2012-03-31 08:42:20 +04: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 23:09:13 +04:00
|
|
|
|
|
|
|
#include "XMLHttpRequest.h"
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
#include "nsIDOMEvent.h"
|
|
|
|
#include "nsIDOMEventListener.h"
|
|
|
|
#include "nsIRunnable.h"
|
|
|
|
#include "nsIVariant.h"
|
|
|
|
#include "nsIXMLHttpRequest.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
|
2012-01-14 21:43:00 +04:00
|
|
|
#include "jsfriendapi.h"
|
2013-10-23 17:16:49 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
|
|
|
#include "mozilla/dom/Exceptions.h"
|
2014-06-04 02:52:36 +04:00
|
|
|
#include "mozilla/dom/ProgressEvent.h"
|
2013-10-23 17:16:49 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2012-03-31 08:42:20 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
#include "File.h"
|
|
|
|
#include "RuntimeService.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "WorkerPrivate.h"
|
2013-10-23 17:16:49 +04:00
|
|
|
#include "WorkerRunnable.h"
|
2012-03-31 08:42:20 +04:00
|
|
|
#include "XMLHttpRequestUpload.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-09-20 11:55:36 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2012-11-10 19:45:52 +04:00
|
|
|
using namespace mozilla::dom;
|
2012-03-31 08:42:20 +04:00
|
|
|
USING_WORKERS_NAMESPACE
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
// XXX Need to figure this out...
|
|
|
|
#define UNCATCHABLE_EXCEPTION NS_ERROR_OUT_OF_MEMORY
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04: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 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
BEGIN_WORKERS_NAMESPACE
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-06-15 06:31:55 +04:00
|
|
|
class Proxy MOZ_FINAL : public nsIDOMEventListener
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
public:
|
|
|
|
// Read on multiple threads.
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
XMLHttpRequest* mXMLHttpRequestPrivate;
|
|
|
|
|
2012-09-17 04:20:16 +04:00
|
|
|
// XHR Params:
|
|
|
|
bool mMozAnon;
|
|
|
|
bool mMozSystem;
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
// Only touched on the main thread.
|
|
|
|
nsRefPtr<nsXMLHttpRequest> mXHR;
|
|
|
|
nsCOMPtr<nsIXMLHttpRequestUpload> mXHRUpload;
|
2013-10-23 17:16:49 +04:00
|
|
|
nsCOMPtr<nsIEventTarget> mSyncLoopTarget;
|
|
|
|
nsCOMPtr<nsIEventTarget> mSyncEventResponseTarget;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mInnerEventStreamId;
|
|
|
|
uint32_t mInnerChannelId;
|
|
|
|
uint32_t mOutstandingSendCount;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
// Only touched on the worker thread.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mOuterEventStreamId;
|
|
|
|
uint32_t mOuterChannelId;
|
|
|
|
uint64_t mLastLoaded;
|
|
|
|
uint64_t mLastTotal;
|
|
|
|
uint64_t mLastUploadLoaded;
|
|
|
|
uint64_t mLastUploadTotal;
|
2012-03-31 08:42:20 +04:00
|
|
|
bool mIsSyncXHR;
|
|
|
|
bool mLastLengthComputable;
|
|
|
|
bool mLastUploadLengthComputable;
|
|
|
|
bool mSeenLoadStart;
|
|
|
|
bool mSeenUploadLoadStart;
|
|
|
|
|
|
|
|
// Only touched on the main thread.
|
|
|
|
bool mUploadEventListenersAttached;
|
|
|
|
bool mMainThreadSeenLoadStart;
|
|
|
|
bool mInOpen;
|
2014-06-06 15:17:57 +04:00
|
|
|
bool mArrayBufferResponseWasTransferred;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
public:
|
2012-09-17 04:20:16 +04:00
|
|
|
Proxy(XMLHttpRequest* aXHRPrivate, bool aMozAnon, bool aMozSystem)
|
2012-07-30 18:20:58 +04:00
|
|
|
: mWorkerPrivate(nullptr), mXMLHttpRequestPrivate(aXHRPrivate),
|
2012-09-17 04:20:16 +04:00
|
|
|
mMozAnon(aMozAnon), mMozSystem(aMozSystem),
|
2012-03-31 08:42:20 +04: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),
|
2014-06-06 15:17:57 +04:00
|
|
|
mInOpen(false), mArrayBufferResponseWasTransferred(false)
|
2012-03-31 08:42:20 +04:00
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIDOMEVENTLISTENER
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
bool
|
2013-10-23 17:16:49 +04:00
|
|
|
Init();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
void
|
|
|
|
Teardown();
|
|
|
|
|
|
|
|
bool
|
|
|
|
AddRemoveEventListeners(bool aUpload, bool aAdd);
|
|
|
|
|
|
|
|
void
|
|
|
|
Reset()
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
AssertIsOnMainThread();
|
2011-11-04 06:51:33 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
if (mUploadEventListenersAttached) {
|
|
|
|
AddRemoveEventListeners(true, false);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
already_AddRefed<nsIEventTarget>
|
|
|
|
GetEventTarget()
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
AssertIsOnMainThread();
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> target = mSyncEventResponseTarget ?
|
|
|
|
mSyncEventResponseTarget :
|
|
|
|
mSyncLoopTarget;
|
|
|
|
return target.forget();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~Proxy()
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(!mXHR);
|
|
|
|
MOZ_ASSERT(!mXHRUpload);
|
|
|
|
MOZ_ASSERT(!mOutstandingSendCount);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
};
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
END_WORKERS_NAMESPACE
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
namespace {
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
inline void
|
2012-05-03 08:35:38 +04:00
|
|
|
ConvertResponseTypeToString(XMLHttpRequestResponseType aType,
|
|
|
|
nsString& aString)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
using namespace
|
2012-05-03 08:35:38 +04:00
|
|
|
mozilla::dom::XMLHttpRequestResponseTypeValues;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
size_t index = static_cast<size_t>(aType);
|
|
|
|
MOZ_ASSERT(index < ArrayLength(strings), "Codegen gave us a bad value!");
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
aString.AssignASCII(strings[index].value, strings[index].length);
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
inline XMLHttpRequestResponseType
|
|
|
|
ConvertStringToResponseType(const nsAString& aString)
|
|
|
|
{
|
|
|
|
using namespace
|
2012-05-03 08:35:38 +04:00
|
|
|
mozilla::dom::XMLHttpRequestResponseTypeValues;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04: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 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-20 11:36:40 +04:00
|
|
|
MOZ_CRASH("Don't know anything about this response type!");
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04: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 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
2013-10-23 17:16:49 +04: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 08:42:20 +04: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 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
static_assert(MOZ_ARRAY_LENGTH(sEventStrings) == STRING_COUNT,
|
|
|
|
"Bad string count!");
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class MainThreadProxyRunnable : public MainThreadWorkerSyncRunnable
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
nsRefPtr<Proxy> mProxy;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MainThreadProxyRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy)
|
|
|
|
: MainThreadWorkerSyncRunnable(aWorkerPrivate, aProxy->GetEventTarget()),
|
2012-03-31 08:42:20 +04:00
|
|
|
mProxy(aProxy)
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aProxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~MainThreadProxyRunnable()
|
2012-03-31 08:42:20 +04:00
|
|
|
{ }
|
|
|
|
};
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class XHRUnpinRunnable MOZ_FINAL : public MainThreadWorkerControlRunnable
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
XMLHttpRequest* mXMLHttpRequestPrivate;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
public:
|
|
|
|
XHRUnpinRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
XMLHttpRequest* aXHRPrivate)
|
2013-10-23 17:16:49 +04:00
|
|
|
: MainThreadWorkerControlRunnable(aWorkerPrivate),
|
2012-03-31 08:42:20 +04:00
|
|
|
mXMLHttpRequestPrivate(aXHRPrivate)
|
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(aXHRPrivate);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~XHRUnpinRunnable()
|
|
|
|
{ }
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
2014-06-06 15:12:15 +04:00
|
|
|
if (mXMLHttpRequestPrivate->SendInProgress()) {
|
|
|
|
mXMLHttpRequestPrivate->Unpin();
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
};
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class AsyncTeardownRunnable MOZ_FINAL : public nsRunnable
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
nsRefPtr<Proxy> mProxy;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
public:
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit AsyncTeardownRunnable(Proxy* aProxy)
|
2013-10-23 17:16:49 +04:00
|
|
|
: mProxy(aProxy)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(aProxy);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
private:
|
|
|
|
~AsyncTeardownRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() MOZ_OVERRIDE
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
AssertIsOnMainThread();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
mProxy->Teardown();
|
2012-07-30 18:20:58 +04:00
|
|
|
mProxy = nullptr;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
};
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class LoadStartDetectionRunnable MOZ_FINAL : public nsRunnable,
|
2012-06-15 06:31:55 +04:00
|
|
|
public nsIDOMEventListener
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
nsRefPtr<Proxy> mProxy;
|
|
|
|
nsRefPtr<nsXMLHttpRequest> mXHR;
|
|
|
|
XMLHttpRequest* mXMLHttpRequestPrivate;
|
|
|
|
nsString mEventType;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mChannelId;
|
2013-10-23 17:16:49 +04:00
|
|
|
bool mReceivedLoadStart;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class ProxyCompleteRunnable MOZ_FINAL : public MainThreadProxyRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
XMLHttpRequest* mXMLHttpRequestPrivate;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mChannelId;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
ProxyCompleteRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
2012-08-22 19:56:38 +04:00
|
|
|
XMLHttpRequest* aXHRPrivate, uint32_t aChannelId)
|
2013-10-23 17:16:49 +04:00
|
|
|
: MainThreadProxyRunnable(aWorkerPrivate, aProxy),
|
2012-03-31 08:42:20 +04:00
|
|
|
mXMLHttpRequestPrivate(aXHRPrivate), mChannelId(aChannelId)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~ProxyCompleteRunnable()
|
|
|
|
{ }
|
2011-11-08 05:01:29 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) MOZ_OVERRIDE
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
if (mChannelId != mProxy->mOuterChannelId) {
|
|
|
|
// Threads raced, this event is now obsolete.
|
|
|
|
return true;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
if (mSyncLoopTarget) {
|
|
|
|
aWorkerPrivate->StopSyncLoop(mSyncLoopTarget, true);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2014-06-06 15:12:15 +04:00
|
|
|
if (mXMLHttpRequestPrivate->SendInProgress()) {
|
|
|
|
mXMLHttpRequestPrivate->Unpin();
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
return true;
|
|
|
|
}
|
2013-10-23 17:16:49 +04: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 08:42:20 +04:00
|
|
|
};
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
public:
|
|
|
|
LoadStartDetectionRunnable(Proxy* aProxy, XMLHttpRequest* aXHRPrivate)
|
|
|
|
: mWorkerPrivate(aProxy->mWorkerPrivate), mProxy(aProxy), mXHR(aProxy->mXHR),
|
2013-10-23 17:16:49 +04:00
|
|
|
mXMLHttpRequestPrivate(aXHRPrivate), mChannelId(mProxy->mInnerChannelId),
|
|
|
|
mReceivedLoadStart(false)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
mEventType.AssignWithConversion(sEventStrings[STRING_loadstart]);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
NS_DECL_NSIDOMEVENTLISTENER
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
bool
|
|
|
|
RegisterAndDispatch()
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
AssertIsOnMainThread();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
if (NS_FAILED(mXHR->AddEventListener(mEventType, this, false, false, 2))) {
|
|
|
|
NS_WARNING("Failed to add event listener!");
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
return NS_SUCCEEDED(NS_DispatchToCurrentThread(this));
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~LoadStartDetectionRunnable()
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
};
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class EventRunnable MOZ_FINAL : public MainThreadProxyRunnable
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
nsString mType;
|
|
|
|
nsString mResponseType;
|
|
|
|
JSAutoStructuredCloneBuffer mResponseBuffer;
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > mClonedObjects;
|
2013-07-23 13:58:27 +04:00
|
|
|
JS::Heap<JS::Value> mResponse;
|
2012-03-31 08:42:20 +04:00
|
|
|
nsString mResponseText;
|
2014-05-17 04:24:37 +04:00
|
|
|
nsString mResponseURL;
|
2013-06-13 09:20:10 +04:00
|
|
|
nsCString mStatusText;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t mLoaded;
|
|
|
|
uint64_t mTotal;
|
|
|
|
uint32_t mEventStreamId;
|
|
|
|
uint32_t mStatus;
|
|
|
|
uint16_t mReadyState;
|
2012-03-31 08:42:20 +04:00
|
|
|
bool mUploadEvent;
|
|
|
|
bool mProgressEvent;
|
|
|
|
bool mLengthComputable;
|
2014-06-06 15:17:57 +04:00
|
|
|
bool mUseCachedArrayBufferResponse;
|
2012-03-31 08:42:20 +04:00
|
|
|
nsresult mResponseTextResult;
|
|
|
|
nsresult mStatusResult;
|
|
|
|
nsresult mResponseResult;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
public:
|
2013-07-23 13:58:27 +04:00
|
|
|
class StateDataAutoRooter : private JS::CustomAutoRooter
|
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
XMLHttpRequest::StateData* mStateData;
|
|
|
|
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
|
|
|
|
2013-07-23 13:58:27 +04:00
|
|
|
public:
|
|
|
|
explicit StateDataAutoRooter(JSContext* aCx, XMLHttpRequest::StateData* aData
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2014-03-28 20:01:50 +04:00
|
|
|
: CustomAutoRooter(aCx), mStateData(aData)
|
2013-07-23 13:58:27 +04:00
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual void trace(JSTracer* aTrc)
|
|
|
|
{
|
2014-08-13 13:05:22 +04:00
|
|
|
JS_CallValueTracer(aTrc, &mStateData->mResponse,
|
|
|
|
"XMLHttpRequest::StateData::mResponse");
|
2013-07-23 13:58:27 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-23 17:16:49 +04: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),
|
2014-06-06 15:17:57 +04:00
|
|
|
mLengthComputable(aLengthComputable), mUseCachedArrayBufferResponse(false),
|
|
|
|
mResponseTextResult(NS_OK), mStatusResult(NS_OK), mResponseResult(NS_OK)
|
2013-10-23 17:16:49 +04:00
|
|
|
{ }
|
2013-11-05 18:16:26 +04:00
|
|
|
|
2013-10-23 17:16:49 +04: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),
|
2014-06-06 15:17:57 +04:00
|
|
|
mUseCachedArrayBufferResponse(false), mResponseTextResult(NS_OK),
|
|
|
|
mStatusResult(NS_OK), mResponseResult(NS_OK)
|
2013-10-23 17:16:49 +04:00
|
|
|
{ }
|
2013-11-05 18:16:26 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~EventRunnable()
|
|
|
|
{ }
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual bool
|
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate) MOZ_OVERRIDE;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) MOZ_OVERRIDE;
|
2012-03-31 08:42:20 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class WorkerThreadProxySyncRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
nsRefPtr<Proxy> mProxy;
|
2013-10-23 17:16:49 +04:00
|
|
|
nsCOMPtr<nsIEventTarget> mSyncLoopTarget;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
private:
|
2013-10-23 17:16:49 +04:00
|
|
|
class ResponseRunnable MOZ_FINAL: public MainThreadStopSyncLoopRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
nsRefPtr<Proxy> mProxy;
|
2012-04-25 03:50:00 +04:00
|
|
|
nsresult mErrorCode;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
ResponseRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
2013-10-23 17:16:49 +04:00
|
|
|
nsresult aErrorCode)
|
|
|
|
: MainThreadStopSyncLoopRunnable(aWorkerPrivate, aProxy->GetEventTarget(),
|
|
|
|
NS_SUCCEEDED(aErrorCode)),
|
|
|
|
mProxy(aProxy), mErrorCode(aErrorCode)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(aProxy);
|
2011-12-02 01:30:28 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~ResponseRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
MaybeSetException(JSContext* aCx) MOZ_OVERRIDE
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(NS_FAILED(mErrorCode));
|
|
|
|
|
2014-01-11 04:37:47 +04:00
|
|
|
Throw(aCx, mErrorCode);
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
};
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
public:
|
|
|
|
WorkerThreadProxySyncRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy)
|
2013-10-23 17:16:49 +04:00
|
|
|
: mWorkerPrivate(aWorkerPrivate), mProxy(aProxy)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
MOZ_ASSERT(aProxy);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2011-08-13 23:43:51 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
bool
|
|
|
|
Dispatch(JSContext* aCx)
|
2011-08-13 23:43:51 +04:00
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2012-12-22 00:14:47 +04:00
|
|
|
AutoSyncLoopHolder syncLoop(mWorkerPrivate);
|
2013-10-23 17:16:49 +04:00
|
|
|
mSyncLoopTarget = syncLoop.EventTarget();
|
2011-08-13 23:43:51 +04:00
|
|
|
|
2014-05-23 23:53:17 +04:00
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(this))) {
|
2012-03-31 08:42:20 +04:00
|
|
|
JS_ReportError(aCx, "Failed to dispatch to main thread!");
|
2011-08-13 23:43:51 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
return syncLoop.Run();
|
2011-08-13 23:43:51 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
protected:
|
|
|
|
virtual ~WorkerThreadProxySyncRunnable()
|
|
|
|
{ }
|
|
|
|
|
2012-04-25 03:50:00 +04:00
|
|
|
virtual nsresult
|
2012-03-31 08:42:20 +04:00
|
|
|
MainThreadRun() = 0;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
NS_DECL_NSIRUNNABLE
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class SyncTeardownRunnable MOZ_FINAL : public WorkerThreadProxySyncRunnable
|
2011-11-04 06:51:33 +04:00
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
public:
|
|
|
|
SyncTeardownRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy)
|
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy)
|
2013-10-23 17:16:49 +04:00
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
~SyncTeardownRunnable()
|
|
|
|
{ }
|
2011-11-04 06:51:33 +04:00
|
|
|
|
2012-04-25 03:50:00 +04:00
|
|
|
virtual nsresult
|
2013-10-23 17:16:49 +04:00
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mProxy->Teardown();
|
2014-05-29 09:07:38 +04:00
|
|
|
MOZ_ASSERT(!mProxy->mSyncLoopTarget);
|
2012-03-31 08:42:20 +04:00
|
|
|
return NS_OK;
|
2011-11-04 06:51:33 +04:00
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
};
|
2011-11-04 06:51:33 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class SetBackgroundRequestRunnable MOZ_FINAL :
|
|
|
|
public WorkerThreadProxySyncRunnable
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
bool mValue;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
public:
|
|
|
|
SetBackgroundRequestRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
|
|
bool aValue)
|
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mValue(aValue)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~SetBackgroundRequestRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2012-04-25 03:50:00 +04:00
|
|
|
return mProxy->mXHR->SetMozBackgroundRequest(mValue);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class SetWithCredentialsRunnable MOZ_FINAL :
|
|
|
|
public WorkerThreadProxySyncRunnable
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
bool mValue;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SetWithCredentialsRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
|
|
bool aValue)
|
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mValue(aValue)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~SetWithCredentialsRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2012-04-25 03:50:00 +04:00
|
|
|
return mProxy->mXHR->SetWithCredentials(mValue);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class SetResponseTypeRunnable MOZ_FINAL : public WorkerThreadProxySyncRunnable
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
nsString mResponseType;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SetResponseTypeRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
|
|
const nsAString& aResponseType)
|
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy),
|
|
|
|
mResponseType(aResponseType)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
void
|
|
|
|
GetResponseType(nsAString& aResponseType)
|
|
|
|
{
|
|
|
|
aResponseType.Assign(mResponseType);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~SetResponseTypeRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
nsresult rv = mProxy->mXHR->SetResponseType(mResponseType);
|
|
|
|
mResponseType.Truncate();
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = mProxy->mXHR->GetResponseType(mResponseType);
|
|
|
|
}
|
2012-04-25 03:50:00 +04:00
|
|
|
return rv;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class SetTimeoutRunnable MOZ_FINAL : public WorkerThreadProxySyncRunnable
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mTimeout;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
SetTimeoutRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aTimeout)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mTimeout(aTimeout)
|
2012-03-31 08:42:20 +04:00
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~SetTimeoutRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2012-04-25 03:50:00 +04:00
|
|
|
return mProxy->mXHR->SetTimeout(mTimeout);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class AbortRunnable MOZ_FINAL : public WorkerThreadProxySyncRunnable
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
AbortRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy)
|
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~AbortRunnable()
|
|
|
|
{ }
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE;
|
2012-03-31 08:42:20 +04:00
|
|
|
};
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class GetAllResponseHeadersRunnable MOZ_FINAL :
|
|
|
|
public WorkerThreadProxySyncRunnable
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2013-06-13 09:20:10 +04:00
|
|
|
nsCString& mResponseHeaders;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
GetAllResponseHeadersRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
2013-06-13 09:20:10 +04:00
|
|
|
nsCString& aResponseHeaders)
|
2012-03-31 08:42:20 +04:00
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy),
|
|
|
|
mResponseHeaders(aResponseHeaders)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~GetAllResponseHeadersRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mProxy->mXHR->GetAllResponseHeaders(mResponseHeaders);
|
2012-04-25 03:50:00 +04:00
|
|
|
return NS_OK;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class GetResponseHeaderRunnable MOZ_FINAL : public WorkerThreadProxySyncRunnable
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
const nsCString mHeader;
|
|
|
|
nsCString& mValue;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GetResponseHeaderRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
2013-06-13 09:20:10 +04:00
|
|
|
const nsACString& aHeader, nsCString& aValue)
|
2012-03-31 08:42:20 +04:00
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mHeader(aHeader),
|
|
|
|
mValue(aValue)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~GetResponseHeaderRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2012-04-25 03:50:00 +04:00
|
|
|
return mProxy->mXHR->GetResponseHeader(mHeader, mValue);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class OpenRunnable MOZ_FINAL : public WorkerThreadProxySyncRunnable
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2013-06-13 09:20:10 +04:00
|
|
|
nsCString mMethod;
|
2012-05-30 07:45:18 +04:00
|
|
|
nsString mURL;
|
|
|
|
Optional<nsAString> mUser;
|
|
|
|
nsString mUserStr;
|
|
|
|
Optional<nsAString> mPassword;
|
|
|
|
nsString mPasswordStr;
|
2012-03-31 08:42:20 +04:00
|
|
|
bool mBackgroundRequest;
|
|
|
|
bool mWithCredentials;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mTimeout;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
OpenRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
2013-06-13 09:20:10 +04:00
|
|
|
const nsACString& aMethod, const nsAString& aURL,
|
2012-05-30 07:45:18 +04:00
|
|
|
const Optional<nsAString>& aUser,
|
|
|
|
const Optional<nsAString>& aPassword,
|
2013-03-04 13:06:15 +04:00
|
|
|
bool aBackgroundRequest, bool aWithCredentials,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aTimeout)
|
2012-03-31 08:42:20 +04:00
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mMethod(aMethod),
|
2013-10-23 17:16:49 +04:00
|
|
|
mURL(aURL), mBackgroundRequest(aBackgroundRequest),
|
|
|
|
mWithCredentials(aWithCredentials), mTimeout(aTimeout)
|
2012-05-30 07:45:18 +04:00
|
|
|
{
|
|
|
|
if (aUser.WasPassed()) {
|
|
|
|
mUserStr = aUser.Value();
|
|
|
|
mUser = &mUserStr;
|
|
|
|
}
|
|
|
|
if (aPassword.WasPassed()) {
|
|
|
|
mPasswordStr = aPassword.Value();
|
|
|
|
mPassword = &mPasswordStr;
|
|
|
|
}
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~OpenRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
WorkerPrivate* oldWorker = mProxy->mWorkerPrivate;
|
|
|
|
mProxy->mWorkerPrivate = mWorkerPrivate;
|
|
|
|
|
2012-04-25 03:50:00 +04:00
|
|
|
nsresult rv = MainThreadRunInternal();
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
mProxy->mWorkerPrivate = oldWorker;
|
2012-04-25 03:50:00 +04:00
|
|
|
return rv;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2012-04-25 03:50:00 +04:00
|
|
|
nsresult
|
2013-10-23 17:16:49 +04:00
|
|
|
MainThreadRunInternal();
|
2012-03-31 08:42:20 +04:00
|
|
|
};
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class SendRunnable MOZ_FINAL : public WorkerThreadProxySyncRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
nsString mStringBody;
|
|
|
|
JSAutoStructuredCloneBuffer mBody;
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > mClonedObjects;
|
2013-10-23 17:16:49 +04:00
|
|
|
nsCOMPtr<nsIEventTarget> mSyncLoopTarget;
|
2012-03-31 08:42:20 +04:00
|
|
|
bool mHasUploadListeners;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SendRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
2014-02-01 06:50:07 +04:00
|
|
|
const nsAString& aStringBody, JSAutoStructuredCloneBuffer&& aBody,
|
|
|
|
nsTArray<nsCOMPtr<nsISupports>>& aClonedObjects,
|
2013-10-23 17:16:49 +04:00
|
|
|
nsIEventTarget* aSyncLoopTarget, bool aHasUploadListeners)
|
2014-02-01 06:50:07 +04:00
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy)
|
|
|
|
, mStringBody(aStringBody)
|
|
|
|
, mBody(Move(aBody))
|
|
|
|
, mSyncLoopTarget(aSyncLoopTarget)
|
|
|
|
, mHasUploadListeners(aHasUploadListeners)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mClonedObjects.SwapElements(aClonedObjects);
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~SendRunnable()
|
|
|
|
{ }
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE;
|
2012-03-31 08:42:20 +04:00
|
|
|
};
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class SetRequestHeaderRunnable MOZ_FINAL : public WorkerThreadProxySyncRunnable
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
nsCString mHeader;
|
|
|
|
nsCString mValue;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SetRequestHeaderRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
2013-06-13 09:20:10 +04:00
|
|
|
const nsACString& aHeader, const nsACString& aValue)
|
2012-03-31 08:42:20 +04:00
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mHeader(aHeader),
|
|
|
|
mValue(aValue)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~SetRequestHeaderRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2012-04-25 03:50:00 +04:00
|
|
|
return mProxy->mXHR->SetRequestHeader(mHeader, mValue);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class OverrideMimeTypeRunnable MOZ_FINAL : public WorkerThreadProxySyncRunnable
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
nsString mMimeType;
|
|
|
|
|
|
|
|
public:
|
|
|
|
OverrideMimeTypeRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
|
|
const nsAString& aMimeType)
|
|
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mMimeType(aMimeType)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~OverrideMimeTypeRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mProxy->mXHR->OverrideMimeType(mMimeType);
|
2012-04-25 03:50:00 +04:00
|
|
|
return NS_OK;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class AutoUnpinXHR
|
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
XMLHttpRequest* mXMLHttpRequestPrivate;
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
public:
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit AutoUnpinXHR(XMLHttpRequest* aXMLHttpRequestPrivate)
|
2012-03-31 08:42:20 +04:00
|
|
|
: mXMLHttpRequestPrivate(aXMLHttpRequestPrivate)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aXMLHttpRequestPrivate);
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoUnpinXHR()
|
|
|
|
{
|
|
|
|
if (mXMLHttpRequestPrivate) {
|
|
|
|
mXMLHttpRequestPrivate->Unpin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
2013-10-28 18:04:47 +04:00
|
|
|
mXMLHttpRequestPrivate = nullptr;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2013-10-23 17:16:49 +04: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 08:42:20 +04: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 18:20:58 +04:00
|
|
|
if (!runnable->Dispatch(nullptr)) {
|
2012-03-31 08:42:20 +04:00
|
|
|
NS_RUNTIMEABORT("We're going to hang at shutdown anyways.");
|
|
|
|
}
|
|
|
|
|
2014-05-29 09:07:38 +04:00
|
|
|
if (mSyncLoopTarget) {
|
|
|
|
// We have an unclosed sync loop. Fix that now.
|
|
|
|
nsRefPtr<MainThreadStopSyncLoopRunnable> runnable =
|
|
|
|
new MainThreadStopSyncLoopRunnable(mWorkerPrivate,
|
|
|
|
mSyncLoopTarget.forget(),
|
|
|
|
false);
|
|
|
|
if (!runnable->Dispatch(nullptr)) {
|
|
|
|
NS_RUNTIMEABORT("We're going to hang at shutdown anyways.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mWorkerPrivate = nullptr;
|
2012-03-31 08:42:20 +04:00
|
|
|
mOutstandingSendCount = 0;
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mXHRUpload = nullptr;
|
|
|
|
mXHR = nullptr;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
2014-05-29 09:07:38 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(!mWorkerPrivate);
|
|
|
|
MOZ_ASSERT(!mSyncLoopTarget);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-03-31 08:42:20 +04: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 19:56:38 +04:00
|
|
|
uint32_t lastEventType = aUpload ? STRING_LAST_EVENTTARGET : STRING_LAST_XHR;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
nsAutoString eventType;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index <= lastEventType; index++) {
|
2012-03-31 08:42:20 +04: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;
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(Proxy, nsIDOMEventListener)
|
2012-03-31 08:42:20 +04: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);
|
2014-06-04 02:52:36 +04:00
|
|
|
ProgressEvent* progressEvent = aEvent->InternalDOMEvent()->AsProgressEvent();
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
nsRefPtr<EventRunnable> runnable;
|
|
|
|
|
|
|
|
if (mInOpen && type.EqualsASCII(sEventStrings[STRING_readystatechange])) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t readyState = 0;
|
2012-03-31 08:42:20 +04:00
|
|
|
if (NS_SUCCEEDED(mXHR->GetReadyState(&readyState)) &&
|
|
|
|
readyState == nsIXMLHttpRequest::OPENED) {
|
|
|
|
mInnerEventStreamId++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (progressEvent) {
|
2014-06-04 02:52:36 +04:00
|
|
|
runnable = new EventRunnable(this, !!uploadTarget, type,
|
|
|
|
progressEvent->LengthComputable(),
|
|
|
|
progressEvent->Loaded(),
|
|
|
|
progressEvent->Total());
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
runnable = new EventRunnable(this, !!uploadTarget, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-05-06 17:04:17 +04:00
|
|
|
AutoSafeJSContext cx;
|
2013-04-18 19:36:04 +04:00
|
|
|
JSAutoRequest ar(cx);
|
2012-03-31 08:42:20 +04: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-30 00:32:50 +04:00
|
|
|
if (!runnable->RegisterAndDispatch()) {
|
2012-03-31 08:42:20 +04:00
|
|
|
NS_WARNING("Failed to dispatch LoadStartDetectionRunnable!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(WorkerThreadProxySyncRunnable, nsRunnable)
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(AsyncTeardownRunnable, nsRunnable)
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(LoadStartDetectionRunnable, nsRunnable,
|
|
|
|
nsIDOMEventListener)
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
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;
|
2014-05-29 09:07:02 +04:00
|
|
|
mProxy->mSyncLoopTarget = nullptr;
|
2013-10-23 17:16:49 +04:00
|
|
|
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 21:39:36 +04:00
|
|
|
mResponseResult = xhr->GetResponse(aCx, &response);
|
2013-10-23 17:16:49 +04:00
|
|
|
if (NS_SUCCEEDED(mResponseResult)) {
|
2014-04-29 03:41:16 +04:00
|
|
|
if (!response.isGCThing()) {
|
2013-10-23 17:16:49 +04:00
|
|
|
mResponse = response;
|
2014-06-06 15:17:57 +04:00
|
|
|
} else {
|
|
|
|
bool doClone = true;
|
|
|
|
JS::Rooted<JS::Value> transferable(aCx);
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, response.isObjectOrNull() ?
|
|
|
|
response.toObjectOrNull() : nullptr);
|
|
|
|
if (obj && JS_IsArrayBufferObject(obj)) {
|
|
|
|
// Use cached response if the arraybuffer has been transfered.
|
|
|
|
if (mProxy->mArrayBufferResponseWasTransferred) {
|
|
|
|
MOZ_ASSERT(JS_IsNeuteredArrayBufferObject(obj));
|
|
|
|
mUseCachedArrayBufferResponse = true;
|
|
|
|
doClone = false;
|
|
|
|
} else {
|
2014-06-11 07:37:47 +04:00
|
|
|
MOZ_ASSERT(!JS_IsNeuteredArrayBufferObject(obj));
|
2014-06-06 15:17:57 +04:00
|
|
|
JS::AutoValueArray<1> argv(aCx);
|
|
|
|
argv[0].set(response);
|
|
|
|
obj = JS_NewArrayObject(aCx, argv);
|
|
|
|
if (obj) {
|
|
|
|
transferable.setObject(*obj);
|
2014-06-11 07:37:47 +04:00
|
|
|
// Only cache the response when the readyState is DONE.
|
|
|
|
if (xhr->ReadyState() == nsIXMLHttpRequest::DONE) {
|
|
|
|
mProxy->mArrayBufferResponseWasTransferred = true;
|
|
|
|
}
|
2014-06-06 15:17:57 +04:00
|
|
|
} else {
|
|
|
|
mResponseResult = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
doClone = false;
|
|
|
|
}
|
|
|
|
}
|
2014-06-06 14:08:49 +04:00
|
|
|
}
|
2014-06-06 15:17:57 +04:00
|
|
|
|
|
|
|
if (doClone) {
|
|
|
|
// Anything subject to GC must be cloned.
|
|
|
|
JSStructuredCloneCallbacks* callbacks =
|
|
|
|
aWorkerPrivate->IsChromeWorker() ?
|
2014-09-25 18:12:06 +04:00
|
|
|
workers::ChromeWorkerStructuredCloneCallbacks(true) :
|
|
|
|
workers::WorkerStructuredCloneCallbacks(true);
|
2014-06-06 15:17:57 +04:00
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > clonedObjects;
|
|
|
|
|
|
|
|
if (mResponseBuffer.write(aCx, response, transferable, callbacks,
|
|
|
|
&clonedObjects)) {
|
|
|
|
mClonedObjects.SwapElements(clonedObjects);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Failed to clone response!");
|
|
|
|
mResponseResult = NS_ERROR_DOM_DATA_CLONE_ERR;
|
|
|
|
mProxy->mArrayBufferResponseWasTransferred = false;
|
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mStatusResult = xhr->GetStatus(&mStatus);
|
|
|
|
|
|
|
|
xhr->GetStatusText(mStatusText);
|
|
|
|
|
|
|
|
mReadyState = xhr->ReadyState();
|
|
|
|
|
2014-05-17 04:24:37 +04:00
|
|
|
xhr->GetResponseURL(mResponseURL);
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
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)) {
|
2014-04-28 06:32:05 +04:00
|
|
|
MOZ_ASSERT(mResponse.isUndefined() || mResponse.isNull());
|
2013-10-23 17:16:49 +04:00
|
|
|
state->mResponseResult = mResponseTextResult;
|
|
|
|
state->mResponse = mResponse;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state->mResponseResult = mResponseResult;
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(mResponseResult)) {
|
|
|
|
if (mResponseBuffer.data()) {
|
2014-04-28 06:32:05 +04:00
|
|
|
MOZ_ASSERT(mResponse.isUndefined());
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2014-02-01 06:50:07 +04:00
|
|
|
JSAutoStructuredCloneBuffer responseBuffer(Move(mResponseBuffer));
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
JSStructuredCloneCallbacks* callbacks =
|
|
|
|
aWorkerPrivate->IsChromeWorker() ?
|
2014-09-25 18:12:06 +04:00
|
|
|
workers::ChromeWorkerStructuredCloneCallbacks(false) :
|
|
|
|
workers::WorkerStructuredCloneCallbacks(false);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2014-05-17 04:24:37 +04:00
|
|
|
state->mResponseURL = mResponseURL;
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
XMLHttpRequest* xhr = mProxy->mXMLHttpRequestPrivate;
|
2014-06-06 15:17:57 +04:00
|
|
|
xhr->UpdateState(*state, mUseCachedArrayBufferResponse);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
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) {
|
2014-06-04 02:52:36 +04:00
|
|
|
ProgressEventInit init;
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mLengthComputable = mLengthComputable;
|
|
|
|
init.mLoaded = mLoaded;
|
|
|
|
init.mTotal = mTotal;
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2014-06-04 02:52:36 +04:00
|
|
|
event = ProgressEvent::Constructor(target, mType, init);
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
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() ?
|
2014-09-25 18:12:06 +04:00
|
|
|
workers::ChromeWorkerStructuredCloneCallbacks(true) :
|
|
|
|
workers::WorkerStructuredCloneCallbacks(true);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
JS::Rooted<JS::Value> body(cx);
|
|
|
|
if (mBody.read(cx, &body, callbacks, &mClonedObjects)) {
|
2014-01-09 21:39:36 +04:00
|
|
|
if (NS_FAILED(xpc->JSValToVariant(cx, body, getter_AddRefs(variant)))) {
|
2013-10-23 17:16:49 +04: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!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 15:17:57 +04:00
|
|
|
mProxy->mArrayBufferResponseWasTransferred = false;
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
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 18:16:26 +04:00
|
|
|
XMLHttpRequest::XMLHttpRequest(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate),
|
2013-05-06 23:28:13 +04:00
|
|
|
mResponseType(XMLHttpRequestResponseType::Text), mTimeout(0),
|
2013-11-05 18:16:26 +04:00
|
|
|
mRooted(false), mBackgroundRequest(false), mWithCredentials(false),
|
|
|
|
mCanceled(false), mMozAnon(false), mMozSystem(false)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-11-05 18:16:26 +04:00
|
|
|
|
|
|
|
SetIsDOMBinding();
|
2014-08-07 02:23:33 +04:00
|
|
|
|
|
|
|
mozilla::HoldJSObjects(this);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
XMLHttpRequest::~XMLHttpRequest()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
ReleaseProxy(XHRIsGoingAway);
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mRooted);
|
|
|
|
|
|
|
|
mozilla::DropJSObjects(this);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04: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)
|
2014-02-21 02:35:47 +04:00
|
|
|
tmp->mStateData.mResponse.setUndefined();
|
2013-11-05 18:16:26 +04:00
|
|
|
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*
|
2014-04-09 02:27:18 +04:00
|
|
|
XMLHttpRequest::WrapObject(JSContext* aCx)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-09 02:27:17 +04:00
|
|
|
return XMLHttpRequestBinding_workers::Wrap(aCx, this);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2013-11-05 18:16:26 +04:00
|
|
|
already_AddRefed<XMLHttpRequest>
|
2013-08-23 09:17:08 +04:00
|
|
|
XMLHttpRequest::Constructor(const GlobalObject& aGlobal,
|
2013-08-23 09:17:09 +04:00
|
|
|
const MozXMLHttpRequestParameters& aParams,
|
2012-06-07 22:28:33 +04:00
|
|
|
ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2014-06-16 22:08:00 +04:00
|
|
|
JSContext* cx = aGlobal.Context();
|
2012-12-03 20:07:49 +04:00
|
|
|
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx);
|
2012-03-31 08:42:20 +04:00
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
nsRefPtr<XMLHttpRequest> xhr = new XMLHttpRequest(workerPrivate);
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2012-09-17 04:20:16 +04:00
|
|
|
if (workerPrivate->XHRParamsAllowed()) {
|
2013-10-16 21:20:51 +04:00
|
|
|
if (aParams.mMozSystem)
|
|
|
|
xhr->mMozAnon = true;
|
|
|
|
else
|
|
|
|
xhr->mMozAnon = aParams.mMozAnon;
|
2012-12-18 01:44:04 +04:00
|
|
|
xhr->mMozSystem = aParams.mMozSystem;
|
2012-09-17 04:20:16 +04:00
|
|
|
}
|
2012-06-07 22:28:33 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
return xhr.forget();
|
2012-03-31 08:42:20 +04: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 18:20:58 +04:00
|
|
|
mProxy = nullptr;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2012-08-14 13:10:41 +04:00
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(runnable))) {
|
2012-03-31 08:42:20 +04: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 18:20:58 +04:00
|
|
|
mProxy = nullptr;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
if (!runnable->Dispatch(nullptr)) {
|
2012-03-31 08:42:20 +04:00
|
|
|
NS_ERROR("Failed to dispatch teardown runnable!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-06 05:15:11 +04:00
|
|
|
XMLHttpRequest::MaybePin(ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
if (mRooted) {
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
JSContext* cx = GetCurrentThreadJSContext();
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
if (!mWorkerPrivate->AddFeature(cx, this)) {
|
2014-01-28 21:51:44 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
NS_ADDREF_THIS();
|
|
|
|
|
|
|
|
mRooted = true;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-06 05:15:11 +04:00
|
|
|
XMLHttpRequest::MaybeDispatchPrematureAbortEvents(ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(mProxy);
|
|
|
|
|
2014-06-06 15:12:12 +04:00
|
|
|
// Only send readystatechange event when state changed.
|
|
|
|
bool isStateChanged = false;
|
|
|
|
if (mStateData.mReadyState != 4) {
|
|
|
|
isStateChanged = true;
|
|
|
|
mStateData.mReadyState = 4;
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
if (mProxy->mSeenUploadLoadStart) {
|
|
|
|
MOZ_ASSERT(mUpload);
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
DispatchPrematureAbortEvent(mUpload, NS_LITERAL_STRING("abort"), true,
|
|
|
|
aRv);
|
2012-05-06 05:15:11 +04:00
|
|
|
if (aRv.Failed()) {
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
DispatchPrematureAbortEvent(mUpload, NS_LITERAL_STRING("loadend"), true,
|
|
|
|
aRv);
|
2012-05-06 05:15:11 +04:00
|
|
|
if (aRv.Failed()) {
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mProxy->mSeenUploadLoadStart = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mProxy->mSeenLoadStart) {
|
2014-06-06 15:12:12 +04:00
|
|
|
if (isStateChanged) {
|
|
|
|
DispatchPrematureAbortEvent(this, NS_LITERAL_STRING("readystatechange"),
|
|
|
|
false, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
DispatchPrematureAbortEvent(this, NS_LITERAL_STRING("abort"), false, aRv);
|
2012-05-06 05:15:11 +04:00
|
|
|
if (aRv.Failed()) {
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
DispatchPrematureAbortEvent(this, NS_LITERAL_STRING("loadend"), false,
|
|
|
|
aRv);
|
2012-05-06 05:15:11 +04:00
|
|
|
if (aRv.Failed()) {
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mProxy->mSeenLoadStart = false;
|
|
|
|
}
|
2014-06-06 15:12:15 +04:00
|
|
|
|
|
|
|
if (mRooted) {
|
|
|
|
Unpin();
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-05 18:16:26 +04:00
|
|
|
XMLHttpRequest::DispatchPrematureAbortEvent(EventTarget* aTarget,
|
|
|
|
const nsAString& aEventType,
|
2012-05-06 05:15:11 +04:00
|
|
|
bool aUploadTarget,
|
|
|
|
ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(aTarget);
|
|
|
|
|
2012-07-26 00:49:24 +04:00
|
|
|
if (!mProxy) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
if (aEventType.EqualsLiteral("readystatechange")) {
|
|
|
|
NS_NewDOMEvent(getter_AddRefs(event), aTarget, nullptr, nullptr);
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
if (event) {
|
|
|
|
event->InitEvent(aEventType, false, false);
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
else {
|
2014-06-04 02:52:36 +04:00
|
|
|
ProgressEventInit init;
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
if (aUploadTarget) {
|
|
|
|
init.mLengthComputable = mProxy->mLastUploadLengthComputable;
|
|
|
|
init.mLoaded = mProxy->mLastUploadLoaded;
|
|
|
|
init.mTotal = mProxy->mLastUploadTotal;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
init.mLengthComputable = mProxy->mLastLengthComputable;
|
|
|
|
init.mLoaded = mProxy->mLastLoaded;
|
|
|
|
init.mTotal = mProxy->mLastTotal;
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
2014-06-04 02:52:36 +04:00
|
|
|
event = ProgressEvent::Constructor(aTarget, aEventType, init);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!event) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
|
|
aTarget->DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XMLHttpRequest::Unpin()
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
MOZ_ASSERT(mRooted, "Mismatched calls to Unpin!");
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
JSContext* cx = GetCurrentThreadJSContext();
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
mWorkerPrivate->RemoveFeature(cx, this);
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
mRooted = false;
|
|
|
|
|
|
|
|
NS_RELEASE_THIS();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
void
|
|
|
|
XMLHttpRequest::SendInternal(const nsAString& aStringBody,
|
2014-02-01 06:50:07 +04:00
|
|
|
JSAutoStructuredCloneBuffer&& aBody,
|
2012-03-31 08:42:20 +04:00
|
|
|
nsTArray<nsCOMPtr<nsISupports> >& aClonedObjects,
|
2012-05-06 05:15:11 +04:00
|
|
|
ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
bool hasUploadListeners = mUpload ? mUpload->HasListeners() : false;
|
|
|
|
|
|
|
|
MaybePin(aRv);
|
2012-05-06 05:15:11 +04:00
|
|
|
if (aRv.Failed()) {
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoUnpinXHR autoUnpin(this);
|
2012-12-22 00:14:47 +04:00
|
|
|
Maybe<AutoSyncLoopHolder> autoSyncLoop;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
nsCOMPtr<nsIEventTarget> syncLoopTarget;
|
2012-12-22 00:14:47 +04:00
|
|
|
bool isSyncXHR = mProxy->mIsSyncXHR;
|
|
|
|
if (isSyncXHR) {
|
2014-08-14 02:39:41 +04:00
|
|
|
autoSyncLoop.emplace(mWorkerPrivate);
|
|
|
|
syncLoopTarget = autoSyncLoop->EventTarget();
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mProxy->mOuterChannelId++;
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
JSContext* cx = mWorkerPrivate->GetJSContext();
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
nsRefPtr<SendRunnable> runnable =
|
2014-02-01 06:50:07 +04:00
|
|
|
new SendRunnable(mWorkerPrivate, mProxy, aStringBody, Move(aBody),
|
2013-10-23 17:16:49 +04:00
|
|
|
aClonedObjects, syncLoopTarget, hasUploadListeners);
|
2012-03-31 08:42:20 +04:00
|
|
|
if (!runnable->Dispatch(cx)) {
|
2013-04-03 09:12:15 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-22 00:14:47 +04:00
|
|
|
if (!isSyncXHR) {
|
|
|
|
autoUnpin.Clear();
|
2014-08-14 02:39:41 +04:00
|
|
|
MOZ_ASSERT(!autoSyncLoop);
|
2012-12-22 00:14:47 +04:00
|
|
|
return;
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2012-12-22 00:14:47 +04:00
|
|
|
autoUnpin.Clear();
|
|
|
|
|
2014-08-14 02:39:41 +04:00
|
|
|
if (!autoSyncLoop->Run()) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-01-18 22:05:38 +04:00
|
|
|
bool
|
2012-03-31 08:42:20 +04:00
|
|
|
XMLHttpRequest::Notify(JSContext* aCx, Status aStatus)
|
2012-01-18 22:05:38 +04:00
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-11-05 18:16:26 +04:00
|
|
|
MOZ_ASSERT(mWorkerPrivate->GetJSContext() == aCx);
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
if (aStatus >= Canceling && !mCanceled) {
|
|
|
|
mCanceled = true;
|
|
|
|
ReleaseProxy(WorkerIsGoingAway);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-01-18 22:05:38 +04:00
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
void
|
2013-06-13 09:20:10 +04:00
|
|
|
XMLHttpRequest::Open(const nsACString& aMethod, const nsAString& aUrl,
|
2012-05-30 07:45:18 +04:00
|
|
|
bool aAsync, const Optional<nsAString>& aUser,
|
|
|
|
const Optional<nsAString>& aPassword, ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mProxy) {
|
|
|
|
MaybeDispatchPrematureAbortEvents(aRv);
|
2012-05-06 05:15:11 +04:00
|
|
|
if (aRv.Failed()) {
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2012-09-17 04:20:16 +04:00
|
|
|
mProxy = new Proxy(this, mMozAnon, mMozSystem);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mProxy->mOuterEventStreamId++;
|
|
|
|
|
|
|
|
nsRefPtr<OpenRunnable> runnable =
|
2012-05-30 07:45:18 +04:00
|
|
|
new OpenRunnable(mWorkerPrivate, mProxy, aMethod, aUrl, aUser, aPassword,
|
2013-03-04 13:06:15 +04:00
|
|
|
mBackgroundRequest, mWithCredentials,
|
2012-05-30 07:45:18 +04:00
|
|
|
mTimeout);
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-03-31 08:42:20 +04:00
|
|
|
ReleaseProxy();
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mProxy->mIsSyncXHR = !aAsync;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-06-13 09:20:10 +04:00
|
|
|
XMLHttpRequest::SetRequestHeader(const nsACString& aHeader,
|
|
|
|
const nsACString& aValue, ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mProxy) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<SetRequestHeaderRunnable> runnable =
|
2013-06-13 09:20:10 +04:00
|
|
|
new SetRequestHeaderRunnable(mWorkerPrivate, mProxy, aHeader, aValue);
|
2013-11-05 18:16:26 +04:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-06 05:15:11 +04:00
|
|
|
XMLHttpRequest::SetTimeout(uint32_t aTimeout, ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 08:42:20 +04: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 18:16:26 +04:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-06 05:15:11 +04:00
|
|
|
XMLHttpRequest::SetWithCredentials(bool aWithCredentials, ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 08:42:20 +04: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 18:16:26 +04:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XMLHttpRequest::SetMozBackgroundRequest(bool aBackgroundRequest,
|
2012-05-06 05:15:11 +04:00
|
|
|
ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 08:42:20 +04: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 18:16:26 +04:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLHttpRequestUpload*
|
2012-05-06 05:15:11 +04:00
|
|
|
XMLHttpRequest::GetUpload(ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2013-10-28 18:04:47 +04:00
|
|
|
return nullptr;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mUpload) {
|
2013-11-05 18:16:26 +04:00
|
|
|
mUpload = XMLHttpRequestUpload::Create(this);
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
if (!mUpload) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2013-10-28 18:04:47 +04:00
|
|
|
return nullptr;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mUpload;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-06 05:15:11 +04:00
|
|
|
XMLHttpRequest::Send(ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mProxy) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to clone.
|
|
|
|
JSAutoStructuredCloneBuffer buffer;
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > clonedObjects;
|
|
|
|
|
2014-02-01 06:50:07 +04:00
|
|
|
SendInternal(NullString(), Move(buffer), clonedObjects, aRv);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-06 05:15:11 +04:00
|
|
|
XMLHttpRequest::Send(const nsAString& aBody, ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mProxy) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to clone.
|
|
|
|
JSAutoStructuredCloneBuffer buffer;
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > clonedObjects;
|
|
|
|
|
2014-02-01 06:50:07 +04:00
|
|
|
SendInternal(aBody, Move(buffer), clonedObjects, aRv);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-12-20 23:28:18 +04:00
|
|
|
XMLHttpRequest::Send(JS::Handle<JSObject*> aBody, ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2013-11-21 22:56:35 +04:00
|
|
|
JSContext* cx = mWorkerPrivate->GetJSContext();
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
MOZ_ASSERT(aBody);
|
2013-11-21 22:56:35 +04:00
|
|
|
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mProxy) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-23 04:18:32 +04:00
|
|
|
JS::Rooted<JS::Value> valToClone(cx);
|
2013-12-20 23:28:18 +04:00
|
|
|
if (JS_IsArrayBufferObject(aBody) || JS_IsArrayBufferViewObject(aBody) ||
|
|
|
|
file::GetDOMBlobFromJSObject(aBody)) {
|
|
|
|
valToClone.setObject(*aBody);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
else {
|
2013-12-20 23:28:18 +04:00
|
|
|
JS::Rooted<JS::Value> obj(cx, JS::ObjectValue(*aBody));
|
2013-11-16 16:31:36 +04:00
|
|
|
JSString* bodyStr = JS::ToString(cx, obj);
|
2012-03-31 08:42:20 +04:00
|
|
|
if (!bodyStr) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-10-23 04:18:32 +04:00
|
|
|
valToClone.setString(bodyStr);
|
2012-03-31 08:42:20 +04: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 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-01 06:50:07 +04:00
|
|
|
SendInternal(EmptyString(), Move(buffer), clonedObjects, aRv);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2013-12-20 23:28:18 +04: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 08:42:20 +04:00
|
|
|
void
|
2012-05-06 05:15:11 +04:00
|
|
|
XMLHttpRequest::SendAsBinary(const nsAString& aBody, ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
NS_NOTYETIMPLEMENTED("Implement me!");
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
return;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-06 05:15:11 +04:00
|
|
|
XMLHttpRequest::Abort(ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mProxy) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MaybeDispatchPrematureAbortEvents(aRv);
|
2012-05-06 05:15:11 +04:00
|
|
|
if (aRv.Failed()) {
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mProxy->mOuterEventStreamId++;
|
|
|
|
|
|
|
|
nsRefPtr<AbortRunnable> runnable = new AbortRunnable(mWorkerPrivate, mProxy);
|
2013-11-05 18:16:26 +04:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-06-13 09:20:10 +04:00
|
|
|
XMLHttpRequest::GetResponseHeader(const nsACString& aHeader,
|
|
|
|
nsACString& aResponseHeader, ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mProxy) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-13 09:20:10 +04:00
|
|
|
nsCString responseHeader;
|
2012-03-31 08:42:20 +04:00
|
|
|
nsRefPtr<GetResponseHeaderRunnable> runnable =
|
2013-06-13 09:20:10 +04:00
|
|
|
new GetResponseHeaderRunnable(mWorkerPrivate, mProxy, aHeader,
|
|
|
|
responseHeader);
|
2013-11-05 18:16:26 +04:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-06-13 09:20:10 +04:00
|
|
|
aResponseHeader = responseHeader;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-06-13 09:20:10 +04:00
|
|
|
XMLHttpRequest::GetAllResponseHeaders(nsACString& aResponseHeaders,
|
2012-05-06 05:15:11 +04:00
|
|
|
ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mProxy) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-13 09:20:10 +04:00
|
|
|
nsCString responseHeaders;
|
2012-03-31 08:42:20 +04:00
|
|
|
nsRefPtr<GetAllResponseHeadersRunnable> runnable =
|
|
|
|
new GetAllResponseHeadersRunnable(mWorkerPrivate, mProxy, responseHeaders);
|
2013-11-05 18:16:26 +04:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aResponseHeaders = responseHeaders;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-06 05:15:11 +04:00
|
|
|
XMLHttpRequest::OverrideMimeType(const nsAString& aMimeType, ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 08:42:20 +04: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 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<OverrideMimeTypeRunnable> runnable =
|
|
|
|
new OverrideMimeTypeRunnable(mWorkerPrivate, mProxy, aMimeType);
|
2013-11-05 18:16:26 +04:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XMLHttpRequest::SetResponseType(XMLHttpRequestResponseType aResponseType,
|
2012-05-06 05:15:11 +04:00
|
|
|
ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCanceled) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(UNCATCHABLE_EXCEPTION);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mProxy || SendInProgress()) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// "document" is fine for the main thread but not for a worker. Short-circuit
|
|
|
|
// that here.
|
2013-05-06 23:28:13 +04:00
|
|
|
if (aResponseType == XMLHttpRequestResponseType::Document) {
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString responseType;
|
|
|
|
ConvertResponseTypeToString(aResponseType, responseType);
|
|
|
|
|
|
|
|
nsRefPtr<SetResponseTypeRunnable> runnable =
|
|
|
|
new SetResponseTypeRunnable(mWorkerPrivate, mProxy, responseType);
|
2013-11-05 18:16:26 +04:00
|
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString acceptedResponseTypeString;
|
|
|
|
runnable->GetResponseType(acceptedResponseTypeString);
|
|
|
|
|
|
|
|
mResponseType = ConvertStringToResponseType(acceptedResponseTypeString);
|
|
|
|
}
|
|
|
|
|
2014-06-12 00:26:52 +04:00
|
|
|
void
|
|
|
|
XMLHttpRequest::GetResponse(JSContext* /* unused */,
|
|
|
|
JS::MutableHandle<JS::Value> aResponse,
|
|
|
|
ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
if (NS_SUCCEEDED(mStateData.mResponseTextResult) &&
|
2014-04-28 06:32:05 +04:00
|
|
|
mStateData.mResponse.isUndefined()) {
|
2012-03-31 08:42:20 +04:00
|
|
|
MOZ_ASSERT(mStateData.mResponseText.Length());
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(mStateData.mResponseResult));
|
|
|
|
|
|
|
|
JSString* str =
|
2013-11-05 18:16:26 +04:00
|
|
|
JS_NewUCStringCopyN(mWorkerPrivate->GetJSContext(),
|
|
|
|
mStateData.mResponseText.get(),
|
2012-03-31 08:42:20 +04:00
|
|
|
mStateData.mResponseText.Length());
|
|
|
|
if (!str) {
|
2012-05-06 05:15:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2014-06-12 00:26:52 +04:00
|
|
|
return;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mStateData.mResponse = STRING_TO_JSVAL(str);
|
|
|
|
}
|
|
|
|
|
2014-06-12 00:26:52 +04:00
|
|
|
JS::ExposeValueToActiveJS(mStateData.mResponse);
|
2012-03-31 08:42:20 +04:00
|
|
|
aRv = mStateData.mResponseResult;
|
2014-06-12 00:26:52 +04:00
|
|
|
aResponse.set(mStateData.mResponse);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-06 05:15:11 +04:00
|
|
|
XMLHttpRequest::GetResponseText(nsAString& aResponseText, ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
aRv = mStateData.mResponseTextResult;
|
|
|
|
aResponseText = mStateData.mResponseText;
|
|
|
|
}
|
2013-11-05 18:16:26 +04:00
|
|
|
|
|
|
|
void
|
2014-06-06 15:17:57 +04:00
|
|
|
XMLHttpRequest::UpdateState(const StateData& aStateData,
|
|
|
|
bool aUseCachedArrayBufferResponse)
|
2013-11-05 18:16:26 +04:00
|
|
|
{
|
2014-06-06 15:17:57 +04:00
|
|
|
if (aUseCachedArrayBufferResponse) {
|
|
|
|
MOZ_ASSERT(JS_IsArrayBufferObject(mStateData.mResponse.toObjectOrNull()));
|
|
|
|
JS::Rooted<JS::Value> response(mWorkerPrivate->GetJSContext(),
|
|
|
|
mStateData.mResponse);
|
|
|
|
mStateData = aStateData;
|
|
|
|
mStateData.mResponse = response;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mStateData = aStateData;
|
|
|
|
}
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|