Bug 1712930 - Part 6: Get rid of NS_ERROR_DOM_INVALID_STATE_XHR_MUST_NOT_BE_SENDING; r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D116080
This commit is contained in:
Edgar Chen
2021-06-01 13:10:48 +00:00
parent 7d4546bf1a
commit 0b9af21b09
6 changed files with 92 additions and 85 deletions

View File

@@ -2819,51 +2819,51 @@ void XMLHttpRequestMainThread::Send(
NOT_CALLABLE_IN_SYNC_SEND_RV
if (aData.IsNull()) {
aRv = SendInternal(nullptr);
SendInternal(nullptr, false, aRv);
return;
}
if (aData.Value().IsDocument()) {
BodyExtractor<Document> body(&aData.Value().GetAsDocument());
aRv = SendInternal(&body, true);
SendInternal(&body, true, aRv);
return;
}
if (aData.Value().IsBlob()) {
BodyExtractor<const Blob> body(&aData.Value().GetAsBlob());
aRv = SendInternal(&body);
SendInternal(&body, false, aRv);
return;
}
if (aData.Value().IsArrayBuffer()) {
BodyExtractor<const ArrayBuffer> body(&aData.Value().GetAsArrayBuffer());
aRv = SendInternal(&body);
SendInternal(&body, false, aRv);
return;
}
if (aData.Value().IsArrayBufferView()) {
BodyExtractor<const ArrayBufferView> body(
&aData.Value().GetAsArrayBufferView());
aRv = SendInternal(&body);
SendInternal(&body, false, aRv);
return;
}
if (aData.Value().IsFormData()) {
BodyExtractor<const FormData> body(&aData.Value().GetAsFormData());
aRv = SendInternal(&body);
SendInternal(&body, false, aRv);
return;
}
if (aData.Value().IsURLSearchParams()) {
BodyExtractor<const URLSearchParams> body(
&aData.Value().GetAsURLSearchParams());
aRv = SendInternal(&body);
SendInternal(&body, false, aRv);
return;
}
if (aData.Value().IsUSVString()) {
BodyExtractor<const nsAString> body(&aData.Value().GetAsUSVString());
aRv = SendInternal(&body, true);
SendInternal(&body, true, aRv);
return;
}
}
@@ -2885,25 +2885,31 @@ nsresult XMLHttpRequestMainThread::MaybeSilentSendFailure(nsresult aRv) {
return NS_OK;
}
nsresult XMLHttpRequestMainThread::SendInternal(const BodyExtractorBase* aBody,
bool aBodyIsDocumentOrString) {
void XMLHttpRequestMainThread::SendInternal(const BodyExtractorBase* aBody,
bool aBodyIsDocumentOrString,
ErrorResult& aRv) {
MOZ_ASSERT(NS_IsMainThread());
NS_ENSURE_TRUE(mPrincipal, NS_ERROR_NOT_INITIALIZED);
if (!mPrincipal) {
aRv.Throw(NS_ERROR_NOT_INITIALIZED);
return;
}
// Step 1
if (mState != XMLHttpRequest_Binding::OPENED) {
return NS_ERROR_DOM_INVALID_STATE_XHR_MUST_BE_OPENED;
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_XHR_MUST_BE_OPENED);
return;
}
// Step 2
if (mFlagSend) {
return NS_ERROR_DOM_INVALID_STATE_XHR_MUST_NOT_BE_SENDING;
aRv.ThrowInvalidStateError("XMLHttpRequest must not be sending.");
return;
}
nsresult rv = CheckCurrentGlobalCorrectness();
if (NS_FAILED(rv)) {
return NS_ERROR_DOM_INVALID_STATE_XHR_HAS_INVALID_CONTEXT;
if (NS_FAILED(CheckCurrentGlobalCorrectness())) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_XHR_HAS_INVALID_CONTEXT);
return;
}
// If open() failed to create the channel, then throw a network error
@@ -2911,13 +2917,15 @@ nsresult XMLHttpRequestMainThread::SendInternal(const BodyExtractorBase* aBody,
// we have internal code relying on the channel being created in open().
if (!mChannel) {
mFlagSend = true; // so CloseRequestWithError sets us to DONE.
return MaybeSilentSendFailure(NS_ERROR_DOM_NETWORK_ERR);
aRv = MaybeSilentSendFailure(NS_ERROR_DOM_NETWORK_ERR);
return;
}
// non-GET requests aren't allowed for blob.
if (IsBlobURI(mRequestURL) && !mRequestMethod.EqualsLiteral("GET")) {
mFlagSend = true; // so CloseRequestWithError sets us to DONE.
return MaybeSilentSendFailure(NS_ERROR_DOM_NETWORK_ERR);
aRv = MaybeSilentSendFailure(NS_ERROR_DOM_NETWORK_ERR);
return;
}
// XXX We should probably send a warning to the JS console
@@ -2938,9 +2946,11 @@ nsresult XMLHttpRequestMainThread::SendInternal(const BodyExtractorBase* aBody,
nsAutoCString charset;
nsAutoCString defaultContentType;
uint64_t size_u64;
rv = aBody->GetAsStream(getter_AddRefs(uploadStream), &size_u64,
defaultContentType, charset);
NS_ENSURE_SUCCESS(rv, rv);
aRv = aBody->GetAsStream(getter_AddRefs(uploadStream), &size_u64,
defaultContentType, charset);
if (aRv.Failed()) {
return;
}
// make sure it fits within js MAX_SAFE_INTEGER
mUploadTotal =
@@ -2993,8 +3003,8 @@ nsresult XMLHttpRequestMainThread::SendInternal(const BodyExtractorBase* aBody,
nsCOMPtr<nsIURI> uri;
nsAutoCString scheme;
rv = mChannel->GetURI(getter_AddRefs(uri));
if (NS_SUCCEEDED(rv)) {
aRv = mChannel->GetURI(getter_AddRefs(uri));
if (!aRv.Failed()) {
uri->GetScheme(scheme);
if (scheme.LowerCaseEqualsLiteral("jar")) {
mIsMappedArrayBuffer = true;
@@ -3002,8 +3012,10 @@ nsresult XMLHttpRequestMainThread::SendInternal(const BodyExtractorBase* aBody,
}
}
rv = InitiateFetch(uploadStream.forget(), mUploadTotal, uploadContentType);
NS_ENSURE_SUCCESS(rv, rv);
aRv = InitiateFetch(uploadStream.forget(), mUploadTotal, uploadContentType);
if (aRv.Failed()) {
return;
}
// Start our timeout
mRequestSentTime = PR_Now();
@@ -3035,30 +3047,32 @@ nsresult XMLHttpRequestMainThread::SendInternal(const BodyExtractorBase* aBody,
SuspendEventDispatching();
StopProgressEventTimer();
auto scopeExit = MakeScopeExit([&] {
UnsuppressEventHandlingAndResume();
ResumeEventDispatching();
});
SyncTimeoutType syncTimeoutType = MaybeStartSyncTimeoutTimer();
if (syncTimeoutType == eErrorOrExpired) {
Abort();
rv = NS_ERROR_DOM_NETWORK_ERR;
aRv.Throw(NS_ERROR_DOM_NETWORK_ERR);
return;
}
if (NS_SUCCEEDED(rv)) {
nsAutoSyncOperation sync(mSuspendedDoc,
SyncOperationBehavior::eSuspendInput);
if (!SpinEventLoopUntil([&]() { return !mFlagSyncLooping; })) {
rv = NS_ERROR_UNEXPECTED;
}
// Time expired... We should throw.
if (syncTimeoutType == eTimerStarted && !mSyncTimeoutTimer) {
rv = NS_ERROR_DOM_NETWORK_ERR;
}
nsAutoSyncOperation sync(mSuspendedDoc,
SyncOperationBehavior::eSuspendInput);
if (!SpinEventLoopUntil([&]() { return !mFlagSyncLooping; })) {
CancelSyncTimeoutTimer();
aRv.Throw(NS_ERROR_UNEXPECTED);
return;
}
UnsuppressEventHandlingAndResume();
ResumeEventDispatching();
// Time expired... We should throw.
if (syncTimeoutType == eTimerStarted && !mSyncTimeoutTimer) {
CancelSyncTimeoutTimer();
aRv.Throw(NS_ERROR_DOM_NETWORK_ERR);
return;
}
} else {
// Now that we've successfully opened the channel, we can change state. Note
// that this needs to come after the AsyncOpen() and rv check, because this
@@ -3080,10 +3094,8 @@ nsresult XMLHttpRequestMainThread::SendInternal(const BodyExtractorBase* aBody,
}
if (!mChannel) {
return MaybeSilentSendFailure(NS_ERROR_DOM_NETWORK_ERR);
aRv = MaybeSilentSendFailure(NS_ERROR_DOM_NETWORK_ERR);
}
return rv;
}
// http://dvcs.w3.org/hg/xhr/raw-file/tip/Overview.html#dom-xmlhttprequest-setrequestheader
@@ -3100,7 +3112,7 @@ void XMLHttpRequestMainThread::SetRequestHeader(const nsACString& aName,
// Step 2
if (mFlagSend) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_XHR_MUST_NOT_BE_SENDING);
aRv.ThrowInvalidStateError("XMLHttpRequest must not be sending.");
return;
}
@@ -3230,26 +3242,26 @@ bool XMLHttpRequestMainThread::MozBackgroundRequest() const {
return mFlagBackgroundRequest;
}
nsresult XMLHttpRequestMainThread::SetMozBackgroundRequest(
bool aMozBackgroundRequest) {
void XMLHttpRequestMainThread::SetMozBackgroundRequestExternal(
bool aMozBackgroundRequest, ErrorResult& aRv) {
if (!IsSystemXHR()) {
return NS_ERROR_DOM_SECURITY_ERR;
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
return;
}
if (mState != XMLHttpRequest_Binding::UNSENT) {
// Can't change this while we're in the middle of something.
return NS_ERROR_DOM_INVALID_STATE_XHR_MUST_NOT_BE_SENDING;
aRv.ThrowInvalidStateError("XMLHttpRequest must not be sending.");
return;
}
mFlagBackgroundRequest = aMozBackgroundRequest;
return NS_OK;
}
void XMLHttpRequestMainThread::SetMozBackgroundRequest(
bool aMozBackgroundRequest, ErrorResult& aRv) {
// No errors for this webIDL method on main-thread.
SetMozBackgroundRequest(aMozBackgroundRequest);
SetMozBackgroundRequestExternal(aMozBackgroundRequest, IgnoreErrors());
}
void XMLHttpRequestMainThread::SetOriginStack(
@@ -3284,7 +3296,7 @@ void XMLHttpRequestMainThread::SetWithCredentials(bool aWithCredentials,
if ((mState != XMLHttpRequest_Binding::UNSENT &&
mState != XMLHttpRequest_Binding::OPENED) ||
mFlagSend || mIsAnon) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_XHR_MUST_NOT_BE_SENDING);
aRv.ThrowInvalidStateError("XMLHttpRequest must not be sending.");
return;
}