Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Bug 1311798 - Align XMLHttpRequest.abort() with the spec. r=baku
Browse files Browse the repository at this point in the history
  • Loading branch information
wisniewskit committed Nov 21, 2016
1 parent 3e66cf3 commit 8c7a1f1
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 26 deletions.
69 changes: 67 additions & 2 deletions dom/xhr/XMLHttpRequestMainThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1064,10 +1064,75 @@ XMLHttpRequestMainThread::CloseRequestWithError(const ProgressEventType aType)
}

void
XMLHttpRequestMainThread::Abort(ErrorResult& arv)
XMLHttpRequestMainThread::RequestErrorSteps(const ProgressEventType aEventType,
const nsresult aOptionalException,
ErrorResult& aRv)
{
// Step 1
mState = State::done;

StopProgressEventTimer();

// Step 2
mFlagSend = false;

// Step 3
ResetResponse();

// If we're in the destructor, don't risk dispatching an event.
if (mFlagDeleted) {
mFlagSyncLooping = false;
return;
}

// Step 4
if (mFlagSynchronous && NS_FAILED(aOptionalException)) {
aRv.Throw(aOptionalException);
return;
}

// Step 5
FireReadystatechangeEvent();

// Step 6
if (mUpload && !mUploadComplete) {

// Step 6-1
mUploadComplete = true;

// Step 6-2
if (mFlagHadUploadListenersOnSend) {

// Steps 6-3, 6-4 (loadend is fired for us)
DispatchProgressEvent(mUpload, aEventType, 0, -1);
}
}

// Steps 7 and 8 (loadend is fired for us)
DispatchProgressEvent(this, aEventType, 0, -1);
}

void
XMLHttpRequestMainThread::Abort(ErrorResult& aRv)
{
mFlagAborted = true;
CloseRequestWithError(ProgressEventType::abort);

// Step 1
CloseRequest();

// Step 2
if ((mState == State::opened && mFlagSend) ||
mState == State::headers_received ||
mState == State::loading) {
RequestErrorSteps(ProgressEventType::abort, NS_OK, aRv);
}

// Step 3
if (mState == State::done) {
ChangeState(State::unsent, false); // no ReadystateChange event
}

mFlagSyncLooping = false;
}

NS_IMETHODIMP
Expand Down
5 changes: 5 additions & 0 deletions dom/xhr/XMLHttpRequestMainThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,11 @@ class XMLHttpRequestMainThread final : public XMLHttpRequest,
aRv = SendInternal(&body);
}

void
RequestErrorSteps(const ProgressEventType aEventType,
const nsresult aOptionalException,
ErrorResult& aRv);

void
Abort() {
ErrorResult rv;
Expand Down
7 changes: 6 additions & 1 deletion dom/xhr/XMLHttpRequestWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1669,7 +1669,9 @@ XMLHttpRequestWorker::MaybeDispatchPrematureAbortEvents(ErrorResult& aRv)

// Only send readystatechange event when state changed.
bool isStateChanged = false;
if (mStateData.mReadyState != 4) {
if ((mStateData.mReadyState == 1 && mStateData.mFlagSend) ||
mStateData.mReadyState == 2 ||
mStateData.mReadyState == 3) {
isStateChanged = true;
mStateData.mReadyState = 4;
}
Expand Down Expand Up @@ -1811,6 +1813,8 @@ XMLHttpRequestWorker::SendInternal(SendRunnable* aRunnable,
aRunnable->SetSyncLoopTarget(syncLoopTarget);
aRunnable->SetHaveUploadListeners(hasUploadListeners);

mStateData.mFlagSend = true;

aRunnable->Dispatch(aRv);
if (aRv.Failed()) {
// Dispatch() may have spun the event loop and we may have already unrooted.
Expand All @@ -1837,6 +1841,7 @@ XMLHttpRequestWorker::SendInternal(SendRunnable* aRunnable,
if (!autoSyncLoop->Run() && !aRv.Failed()) {
aRv.Throw(NS_ERROR_FAILURE);
}
mStateData.mFlagSend = false;
}

bool
Expand Down
7 changes: 4 additions & 3 deletions dom/xhr/XMLHttpRequestWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ class XMLHttpRequestWorker final : public XMLHttpRequest,
uint32_t mStatus;
nsCString mStatusText;
uint16_t mReadyState;
bool mFlagSend;
JS::Heap<JS::Value> mResponse;
nsresult mResponseTextResult;
nsresult mStatusResult;
nsresult mResponseResult;

StateData()
: mStatus(0), mReadyState(0), mResponse(JS::UndefinedValue()),
mResponseTextResult(NS_OK), mStatusResult(NS_OK),
mResponseResult(NS_OK)
: mStatus(0), mReadyState(0), mFlagSend(false),
mResponse(JS::UndefinedValue()), mResponseTextResult(NS_OK),
mStatusResult(NS_OK), mResponseResult(NS_OK)
{ }

void trace(JSTracer* trc);
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 8c7a1f1

Please sign in to comment.