Bug 1455256 - Port more components to WorkerRef - part 5 - XHR, r=asuth

This commit is contained in:
Andrea Marchesini 2018-06-18 16:37:21 -04:00
parent dbb6b928ed
commit 4ea7edd9be
2 changed files with 29 additions and 37 deletions

View File

@ -24,6 +24,7 @@
#include "mozilla/dom/URLSearchParams.h"
#include "mozilla/dom/WorkerScope.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/dom/WorkerRef.h"
#include "mozilla/dom/WorkerRunnable.h"
#include "mozilla/Telemetry.h"
#include "nsComponentManagerUtils.h"
@ -1527,10 +1528,14 @@ SendRunnable::RunOnMainThread(ErrorResult& aRv)
}
XMLHttpRequestWorker::XMLHttpRequestWorker(WorkerPrivate* aWorkerPrivate)
: WorkerHolder("XMLHttpRequestWorker"), mWorkerPrivate(aWorkerPrivate),
mResponseType(XMLHttpRequestResponseType::Text), mTimeout(0),
mRooted(false), mBackgroundRequest(false), mWithCredentials(false),
mCanceled(false), mMozAnon(false), mMozSystem(false)
: mWorkerPrivate(aWorkerPrivate)
, mResponseType(XMLHttpRequestResponseType::Text)
, mTimeout(0)
, mBackgroundRequest(false)
, mWithCredentials(false)
, mCanceled(false)
, mMozAnon(false)
, mMozSystem(false)
{
mWorkerPrivate->AssertIsOnWorkerThread();
@ -1543,7 +1548,7 @@ XMLHttpRequestWorker::~XMLHttpRequestWorker()
ReleaseProxy(XHRIsGoingAway);
MOZ_ASSERT(!mRooted);
MOZ_ASSERT(!mWorkerRef);
mozilla::DropJSObjects(this);
}
@ -1638,18 +1643,25 @@ XMLHttpRequestWorker::MaybePin(ErrorResult& aRv)
{
mWorkerPrivate->AssertIsOnWorkerThread();
if (mRooted) {
if (mWorkerRef) {
return;
}
if (!HoldWorker(mWorkerPrivate, Canceling)) {
RefPtr<XMLHttpRequestWorker> self = this;
mWorkerRef =
StrongWorkerRef::Create(mWorkerPrivate, "XMLHttpRequestWorker",
[self]() {
if (!self->mCanceled) {
self->mCanceled = true;
self->ReleaseProxy(WorkerIsGoingAway);
}
});
if (NS_WARN_IF(!mWorkerRef)) {
aRv.Throw(NS_ERROR_FAILURE);
return;
}
NS_ADDREF_THIS();
mRooted = true;
}
void
@ -1760,11 +1772,8 @@ XMLHttpRequestWorker::Unpin()
{
mWorkerPrivate->AssertIsOnWorkerThread();
MOZ_ASSERT(mRooted, "Mismatched calls to Unpin!");
ReleaseWorker();
mRooted = false;
MOZ_ASSERT(mWorkerRef, "Mismatched calls to Unpin!");
mWorkerRef = nullptr;
NS_RELEASE_THIS();
}
@ -1814,7 +1823,7 @@ XMLHttpRequestWorker::SendInternal(SendRunnable* aRunnable,
if (aRv.Failed()) {
// Dispatch() may have spun the event loop and we may have already unrooted.
// If so we don't want autoUnpin to try again.
if (!mRooted) {
if (!mWorkerRef) {
autoUnpin.Clear();
}
return;
@ -1841,19 +1850,6 @@ XMLHttpRequestWorker::SendInternal(SendRunnable* aRunnable,
}
}
bool
XMLHttpRequestWorker::Notify(WorkerStatus aStatus)
{
mWorkerPrivate->AssertIsOnWorkerThread();
if (aStatus >= Canceling && !mCanceled) {
mCanceled = true;
ReleaseProxy(WorkerIsGoingAway);
}
return true;
}
void
XMLHttpRequestWorker::Open(const nsACString& aMethod,
const nsAString& aUrl, bool aAsync,

View File

@ -10,18 +10,17 @@
#include "XMLHttpRequest.h"
#include "XMLHttpRequestString.h"
#include "mozilla/dom/TypedArray.h"
#include "mozilla/dom/WorkerHolder.h"
namespace mozilla {
namespace dom {
class Proxy;
class SendRunnable;
class DOMString;
class SendRunnable;
class StrongWorkerRef;
class WorkerPrivate;
class XMLHttpRequestWorker final : public XMLHttpRequest,
public WorkerHolder
class XMLHttpRequestWorker final : public XMLHttpRequest
{
public:
struct StateData
@ -49,13 +48,13 @@ public:
private:
RefPtr<XMLHttpRequestUpload> mUpload;
WorkerPrivate* mWorkerPrivate;
RefPtr<StrongWorkerRef> mWorkerRef;
RefPtr<Proxy> mProxy;
XMLHttpRequestResponseType mResponseType;
StateData mStateData;
uint32_t mTimeout;
bool mRooted;
bool mBackgroundRequest;
bool mWithCredentials;
bool mCanceled;
@ -77,9 +76,6 @@ public:
void
Unpin();
bool
Notify(WorkerStatus aStatus) override;
virtual uint16_t
ReadyState() const override
{
@ -266,7 +262,7 @@ public:
bool
SendInProgress() const
{
return mRooted;
return !!mWorkerRef;
}
private: