Bug 1848004 - Refactor event type code in XMLHttpRequest code; r=sunil,necko-reviewers,valentin

Differential Revision: https://phabricator.services.mozilla.com/D185919
This commit is contained in:
Thomas Wisniewski 2024-02-12 23:34:21 +00:00
Родитель 96d44ea498
Коммит 034bb2a89c
5 изменённых файлов: 167 добавлений и 159 удалений

Просмотреть файл

@ -7,6 +7,7 @@
#ifndef mozilla_dom_XMLHttpRequest_h
#define mozilla_dom_XMLHttpRequest_h
#include <iterator> // std::begin, std::end
#include "mozilla/Attributes.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/XMLHttpRequestEventTarget.h"
@ -24,6 +25,87 @@ class XMLHttpRequestUpload;
class XMLHttpRequest : public XMLHttpRequestEventTarget {
public:
struct EventType {
const char* cStr;
const char16_t* str;
EventType(const char* name, const char16_t* uname)
: cStr(name), str(uname) {}
operator const nsDependentString() const { return nsDependentString(str); }
friend bool operator==(const EventType& a, const EventType& b) {
return !strcmp(a.cStr, b.cStr);
}
friend bool operator!=(const EventType& a, const EventType& b) {
return strcmp(a.cStr, b.cStr);
}
friend bool operator==(const nsAString& a, const EventType& b) {
return a.Equals(b.str);
}
friend bool operator!=(const nsAString& a, const EventType& b) {
return !a.Equals(b.str);
}
inline bool operator==(const nsString& b) const { return b.Equals(str); }
inline bool operator!=(const nsString& b) const { return !b.Equals(str); }
};
struct ProgressEventType : public EventType {
ProgressEventType(const char* name, const char16_t* uname)
: EventType(name, uname) {}
};
struct ErrorProgressEventType : public ProgressEventType {
const nsresult errorCode;
ErrorProgressEventType(const char* name, const char16_t* uname,
const nsresult code)
: ProgressEventType(name, uname), errorCode(code) {}
};
#define DECL_EVENT(NAME) \
static inline const EventType NAME = EventType(#NAME, u## #NAME);
#define DECL_PROGRESSEVENT(NAME) \
static inline const ProgressEventType NAME = \
ProgressEventType(#NAME, u## #NAME);
#define DECL_ERRORPROGRESSEVENT(NAME, ERR) \
static inline const ErrorProgressEventType NAME = \
ErrorProgressEventType(#NAME, u## #NAME, ERR);
struct Events {
DECL_EVENT(readystatechange);
DECL_PROGRESSEVENT(loadstart);
DECL_PROGRESSEVENT(progress);
DECL_ERRORPROGRESSEVENT(error, NS_ERROR_DOM_NETWORK_ERR);
DECL_ERRORPROGRESSEVENT(abort, NS_ERROR_DOM_ABORT_ERR);
DECL_ERRORPROGRESSEVENT(timeout, NS_ERROR_DOM_TIMEOUT_ERR);
DECL_PROGRESSEVENT(load);
DECL_PROGRESSEVENT(loadend);
static inline const EventType* All[]{
&readystatechange, &loadstart, &progress, &error, &abort,
&timeout, &load, &loadend};
static inline const EventType* ProgressEvents[]{
&loadstart, &progress, &error, &abort, &timeout, &load, &loadend};
static inline const EventType* Find(const nsString& name) {
for (const EventType* type : Events::All) {
if (*type == name) {
return type;
}
}
return nullptr;
}
};
static already_AddRefed<XMLHttpRequest> Constructor(
const GlobalObject& aGlobal, const MozXMLHttpRequestParameters& aParams,
ErrorResult& aRv);

Просмотреть файл

@ -119,6 +119,9 @@ using namespace mozilla::net;
namespace mozilla::dom {
using EventType = XMLHttpRequest::EventType;
using Events = XMLHttpRequest::Events;
// Maximum size that we'll grow an ArrayBuffer instead of doubling,
// once doubling reaches this threshold
const uint32_t XML_HTTP_REQUEST_ARRAYBUFFER_MAX_GROWTH = 32 * 1024 * 1024;
@ -130,14 +133,6 @@ const int32_t XML_HTTP_REQUEST_MAX_CONTENT_LENGTH_PREALLOCATE =
1 * 1024 * 1024 * 1024LL;
namespace {
const nsLiteralString ProgressEventTypeStrings[] = {
u"loadstart"_ns, u"progress"_ns, u"error"_ns, u"abort"_ns,
u"timeout"_ns, u"load"_ns, u"loadend"_ns};
static_assert(MOZ_ARRAY_LENGTH(ProgressEventTypeStrings) ==
size_t(XMLHttpRequestMainThread::ProgressEventType::ENUM_MAX),
"Mismatched lengths for ProgressEventTypeStrings and "
"ProgressEventType enums");
const nsString kLiteralString_readystatechange = u"readystatechange"_ns;
const nsString kLiteralString_xmlhttprequest = u"xmlhttprequest"_ns;
const nsString kLiteralString_DOMContentLoaded = u"DOMContentLoaded"_ns;
@ -1019,21 +1014,12 @@ void XMLHttpRequestMainThread::CloseRequest(nsresult detail) {
}
void XMLHttpRequestMainThread::CloseRequestWithError(
const ProgressEventType aType) {
const ErrorProgressEventType& aType) {
DEBUG_WORKERREFS;
MOZ_LOG(
gXMLHttpRequestLog, LogLevel::Debug,
("%p CloseRequestWithError(%hhu)", this, static_cast<uint8_t>(aType)));
nsresult detail = NS_ERROR_DOM_UNKNOWN_ERR;
if (aType == ProgressEventType::abort) {
detail = NS_ERROR_DOM_ABORT_ERR;
} else if (aType == ProgressEventType::error) {
detail = NS_ERROR_DOM_NETWORK_ERR;
} else if (aType == ProgressEventType::timeout) {
detail = NS_ERROR_DOM_TIMEOUT_ERR;
}
MOZ_LOG(gXMLHttpRequestLog, LogLevel::Debug,
("%p CloseRequestWithError(%s)", this, aType.cStr));
CloseRequest(detail);
CloseRequest(aType.errorCode);
ResetResponse();
@ -1071,8 +1057,7 @@ void XMLHttpRequestMainThread::RequestErrorSteps(
const ProgressEventType aEventType, const nsresult aOptionalException,
ErrorResult& aRv) {
MOZ_LOG(gXMLHttpRequestLog, LogLevel::Debug,
("%p RequestErrorSteps(%hhu,0x%" PRIx32 ")", this,
static_cast<uint8_t>(aEventType),
("%p RequestErrorSteps(%s,0x%" PRIx32 ")", this, aEventType.cStr,
static_cast<uint32_t>(aOptionalException)));
// Cancel our timers first before setting our state to done, so we don't
@ -1139,7 +1124,7 @@ void XMLHttpRequestMainThread::AbortInternal(ErrorResult& aRv) {
if ((mState == XMLHttpRequest_Binding::OPENED && mFlagSend) ||
mState == XMLHttpRequest_Binding::HEADERS_RECEIVED ||
mState == XMLHttpRequest_Binding::LOADING) {
RequestErrorSteps(ProgressEventType::abort, NS_ERROR_DOM_ABORT_ERR, aRv);
RequestErrorSteps(Events::abort, NS_ERROR_DOM_ABORT_ERR, aRv);
}
// Step 3
@ -1384,7 +1369,7 @@ nsresult XMLHttpRequestMainThread::FireReadystatechangeEvent() {
}
void XMLHttpRequestMainThread::DispatchProgressEvent(
DOMEventTargetHelper* aTarget, const ProgressEventType aType,
DOMEventTargetHelper* aTarget, const ProgressEventType& aType,
int64_t aLoaded, int64_t aTotal) {
DEBUG_WORKERREFS;
NS_ASSERTION(aTarget, "null target");
@ -1397,8 +1382,7 @@ void XMLHttpRequestMainThread::DispatchProgressEvent(
// If blocked by CORS, zero-out the stats on progress events
// and never fire "progress" or "load" events at all.
if (IsDeniedCrossSiteCORSRequest()) {
if (aType == ProgressEventType::progress ||
aType == ProgressEventType::load) {
if (aType == Events::progress || aType == Events::load) {
return;
}
aLoaded = 0;
@ -1412,28 +1396,22 @@ void XMLHttpRequestMainThread::DispatchProgressEvent(
init.mLoaded = aLoaded;
init.mTotal = (aTotal == -1) ? 0 : aTotal;
const nsAString& typeString = ProgressEventTypeStrings[(uint8_t)aType];
RefPtr<ProgressEvent> event =
ProgressEvent::Constructor(aTarget, typeString, init);
ProgressEvent::Constructor(aTarget, aType, init);
event->SetTrusted(true);
if (MOZ_LOG_TEST(gXMLHttpRequestLog, LogLevel::Debug)) {
nsAutoString type;
event->GetType(type);
MOZ_LOG(gXMLHttpRequestLog, LogLevel::Debug,
("firing %s event (%u,%u,%" PRIu64 ",%" PRIu64 ")",
NS_ConvertUTF16toUTF8(type).get(), aTarget == mUpload,
aTotal != -1, aLoaded, (aTotal == -1) ? 0 : aTotal));
}
MOZ_LOG(
gXMLHttpRequestLog, LogLevel::Debug,
("firing %s event (%u,%u,%" PRIu64 ",%" PRIu64 ")", aType.cStr,
aTarget == mUpload, aTotal != -1, aLoaded, (aTotal == -1) ? 0 : aTotal));
DispatchOrStoreEvent(aTarget, event);
// If we're sending a load, error, timeout or abort event, then
// also dispatch the subsequent loadend event.
if (aType == ProgressEventType::load || aType == ProgressEventType::error ||
aType == ProgressEventType::timeout ||
aType == ProgressEventType::abort) {
DispatchProgressEvent(aTarget, ProgressEventType::loadend, aLoaded, aTotal);
if (aType == Events::load || aType == Events::error ||
aType == Events::timeout || aType == Events::abort) {
DispatchProgressEvent(aTarget, Events::loadend, aLoaded, aTotal);
}
}
@ -1934,7 +1912,7 @@ XMLHttpRequestMainThread::OnDataAvailable(nsIRequest* request,
if (mState == XMLHttpRequest_Binding::HEADERS_RECEIVED) {
ChangeState(XMLHttpRequest_Binding::LOADING);
if (!mFlagSynchronous) {
DispatchProgressEvent(this, ProgressEventType::progress, mLoadTransferred,
DispatchProgressEvent(this, Events::progress, mLoadTransferred,
mLoadTotal);
}
mProgressSinceLastProgressEvent = false;
@ -2002,14 +1980,13 @@ XMLHttpRequestMainThread::OnStartRequest(nsIRequest* request) {
mUploadTransferred = mUploadTotal;
if (mProgressSinceLastProgressEvent) {
DispatchProgressEvent(mUpload, ProgressEventType::progress,
mUploadTransferred, mUploadTotal);
DispatchProgressEvent(mUpload, Events::progress, mUploadTransferred,
mUploadTotal);
mProgressSinceLastProgressEvent = false;
}
mUploadComplete = true;
DispatchProgressEvent(mUpload, ProgressEventType::load, mUploadTotal,
mUploadTotal);
DispatchProgressEvent(mUpload, Events::load, mUploadTotal, mUploadTotal);
}
mFlagParseBody = true;
@ -2264,7 +2241,7 @@ XMLHttpRequestMainThread::OnStopRequest(nsIRequest* request, nsresult status) {
if (status == NS_BINDING_ABORTED) {
mFlagParseBody = false;
IgnoredErrorResult rv;
RequestErrorSteps(ProgressEventType::abort, NS_ERROR_DOM_ABORT_ERR, rv);
RequestErrorSteps(Events::abort, NS_ERROR_DOM_ABORT_ERR, rv);
ChangeState(XMLHttpRequest_Binding::UNSENT, false);
return NS_OK;
}
@ -2377,7 +2354,7 @@ XMLHttpRequestMainThread::OnStopRequest(nsIRequest* request, nsresult status) {
static_cast<uint32_t>(status)));
IgnoredErrorResult rv;
mFlagParseBody = false;
RequestErrorSteps(ProgressEventType::error, NS_ERROR_DOM_NETWORK_ERR, rv);
RequestErrorSteps(Events::error, NS_ERROR_DOM_NETWORK_ERR, rv);
// RequestErrorSteps will not call ChangeStateToDone for sync XHRs, so we
// do so here to ensure progress events are sent and our state is sane.
if (mFlagSynchronous) {
@ -2506,8 +2483,7 @@ void XMLHttpRequestMainThread::ChangeStateToDoneInternal() {
// will have not sent a progress event yet, so one must be sent here).
if (!mFlagSynchronous &&
(!mLoadTransferred || mProgressSinceLastProgressEvent)) {
DispatchProgressEvent(this, ProgressEventType::progress, mLoadTransferred,
mLoadTotal);
DispatchProgressEvent(this, Events::progress, mLoadTransferred, mLoadTotal);
mProgressSinceLastProgressEvent = false;
}
@ -2528,16 +2504,15 @@ void XMLHttpRequestMainThread::ChangeStateToDoneInternal() {
// Per spec, if we failed in the upload phase, fire a final error
// and loadend events for the upload after readystatechange=4/done.
if (!mFlagSynchronous && mUpload && !mUploadComplete) {
DispatchProgressEvent(mUpload, ProgressEventType::error, 0, -1);
DispatchProgressEvent(mUpload, Events::error, 0, -1);
}
// Per spec, fire download's load/error and loadend events after
// readystatechange=4/done (and of course all upload events).
if (mErrorLoad != ErrorType::eOK) {
DispatchProgressEvent(this, ProgressEventType::error, 0, -1);
DispatchProgressEvent(this, Events::error, 0, -1);
} else {
DispatchProgressEvent(this, ProgressEventType::load, mLoadTransferred,
mLoadTotal);
DispatchProgressEvent(this, Events::load, mLoadTransferred, mLoadTotal);
}
if (mErrorLoad != ErrorType::eOK) {
@ -3055,10 +3030,9 @@ nsresult XMLHttpRequestMainThread::MaybeSilentSendFailure(nsresult aRv) {
// Defer the actual sending of async events just in case listeners
// are attached after the send() method is called.
Unused << NS_WARN_IF(
NS_FAILED(DispatchToMainThread(NewRunnableMethod<ProgressEventType>(
NS_FAILED(DispatchToMainThread(NewRunnableMethod<ErrorProgressEventType>(
"dom::XMLHttpRequestMainThread::CloseRequestWithError", this,
&XMLHttpRequestMainThread::CloseRequestWithError,
ProgressEventType::error))));
&XMLHttpRequestMainThread::CloseRequestWithError, Events::error))));
return NS_OK;
}
@ -3278,10 +3252,9 @@ void XMLHttpRequestMainThread::SendInternal(const BodyExtractorBase* aBody,
StartProgressEventTimer();
}
// Dispatch loadstart events
DispatchProgressEvent(this, ProgressEventType::loadstart, 0, -1);
DispatchProgressEvent(this, Events::loadstart, 0, -1);
if (mUpload && !mUploadComplete) {
DispatchProgressEvent(mUpload, ProgressEventType::loadstart, 0,
mUploadTotal);
DispatchProgressEvent(mUpload, Events::loadstart, 0, mUploadTotal);
}
}
@ -3730,7 +3703,7 @@ void XMLHttpRequestMainThread::HandleTimeoutCallback() {
}
mFlagTimedOut = true;
CloseRequestWithError(ProgressEventType::timeout);
CloseRequestWithError(Events::timeout);
}
void XMLHttpRequestMainThread::CancelTimeoutTimer() {
@ -3780,13 +3753,12 @@ void XMLHttpRequestMainThread::HandleProgressTimerCallback() {
if (InUploadPhase()) {
if (mUpload && !mUploadComplete && mFlagHadUploadListenersOnSend) {
DispatchProgressEvent(mUpload, ProgressEventType::progress,
mUploadTransferred, mUploadTotal);
DispatchProgressEvent(mUpload, Events::progress, mUploadTransferred,
mUploadTotal);
}
} else {
FireReadystatechangeEvent();
DispatchProgressEvent(this, ProgressEventType::progress, mLoadTransferred,
mLoadTotal);
DispatchProgressEvent(this, Events::progress, mLoadTransferred, mLoadTotal);
}
mProgressSinceLastProgressEvent = false;

Просмотреть файл

@ -194,17 +194,6 @@ class XMLHttpRequestMainThread final : public XMLHttpRequest,
friend class XMLHttpRequestDoneNotifier;
public:
enum class ProgressEventType : uint8_t {
loadstart,
progress,
error,
abort,
timeout,
load,
loadend,
ENUM_MAX
};
// Make sure that any additions done to ErrorType enum are also mirrored in
// XHR_ERROR_TYPE enum of TelemetrySend.sys.mjs.
enum class ErrorType : uint16_t {
@ -432,7 +421,7 @@ class XMLHttpRequestMainThread final : public XMLHttpRequest,
// doesn't bubble.
nsresult FireReadystatechangeEvent();
void DispatchProgressEvent(DOMEventTargetHelper* aTarget,
const ProgressEventType aType, int64_t aLoaded,
const ProgressEventType& aType, int64_t aLoaded,
int64_t aTotal);
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(
@ -742,7 +731,7 @@ class XMLHttpRequestMainThread final : public XMLHttpRequest,
*
* @param aType The progress event type.
*/
void CloseRequestWithError(const ProgressEventType aType);
void CloseRequestWithError(const ErrorProgressEventType& aType);
nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
nsCOMPtr<nsIChannel> mNewRedirectChannel;

Просмотреть файл

@ -46,6 +46,9 @@ extern mozilla::LazyLogModule gXMLHttpRequestLog;
namespace mozilla::dom {
using EventType = XMLHttpRequest::EventType;
using Events = XMLHttpRequest::Events;
/**
* XMLHttpRequest in workers
*
@ -279,42 +282,6 @@ class SendRunnable final : public WorkerThreadProxySyncRunnable {
namespace {
enum {
STRING_abort = 0,
STRING_error,
STRING_load,
STRING_loadstart,
STRING_progress,
STRING_timeout,
STRING_loadend,
STRING_readystatechange,
STRING_COUNT,
STRING_LAST_XHR = STRING_readystatechange,
STRING_LAST_EVENTTARGET = STRING_loadend
};
static_assert(STRING_LAST_XHR >= STRING_LAST_EVENTTARGET, "Bad string setup!");
static_assert(STRING_LAST_XHR == STRING_COUNT - 1, "Bad string setup!");
const char* const sEventStrings[] = {
// XMLHttpRequestEventTarget event types, supported by both XHR and Upload.
"abort",
"error",
"load",
"loadstart",
"progress",
"timeout",
"loadend",
// XMLHttpRequest event types, supported only by XHR.
"readystatechange",
};
static_assert(MOZ_ARRAY_LENGTH(sEventStrings) == STRING_COUNT,
"Bad string count!");
class MainThreadProxyRunnable : public MainThreadWorkerSyncRunnable {
protected:
RefPtr<Proxy> mProxy;
@ -403,7 +370,6 @@ class LoadStartDetectionRunnable final : public Runnable,
mChannelId(mProxy->mInnerChannelId),
mReceivedLoadStart(false) {
AssertIsOnMainThread();
mEventType.AssignASCII(sEventStrings[STRING_loadstart]);
}
NS_DECL_ISUPPORTS_INHERITED
@ -413,7 +379,8 @@ class LoadStartDetectionRunnable final : public Runnable,
bool RegisterAndDispatch() {
AssertIsOnMainThread();
if (NS_FAILED(mXHR->AddEventListener(mEventType, this, false, false))) {
if (NS_FAILED(
mXHR->AddEventListener(Events::loadstart, this, false, false))) {
NS_WARNING("Failed to add event listener!");
return false;
}
@ -426,7 +393,7 @@ class LoadStartDetectionRunnable final : public Runnable,
};
class EventRunnable final : public MainThreadProxyRunnable {
nsString mType;
const EventType& mType;
UniquePtr<XMLHttpRequestWorker::ResponseData> mResponseData;
nsString mResponseURL;
nsCString mStatusText;
@ -446,7 +413,7 @@ class EventRunnable final : public MainThreadProxyRunnable {
JS::PersistentRooted<JSObject*> mScopeObj;
public:
EventRunnable(Proxy* aProxy, bool aUploadEvent, const nsString& aType,
EventRunnable(Proxy* aProxy, bool aUploadEvent, const EventType& aType,
bool aLengthComputable, uint64_t aLoaded, uint64_t aTotal,
JS::Handle<JSObject*> aScopeObj)
: MainThreadProxyRunnable(aProxy->mWorkerPrivate, aProxy),
@ -464,7 +431,7 @@ class EventRunnable final : public MainThreadProxyRunnable {
mErrorDetail(NS_OK),
mScopeObj(RootingCx(), aScopeObj) {}
EventRunnable(Proxy* aProxy, bool aUploadEvent, const nsString& aType,
EventRunnable(Proxy* aProxy, bool aUploadEvent, const EventType& aType,
JS::Handle<JSObject*> aScopeObj)
: MainThreadProxyRunnable(aProxy->mWorkerPrivate, aProxy),
mType(aType),
@ -837,17 +804,16 @@ bool Proxy::AddRemoveEventListeners(bool aUpload, bool aAdd) {
: static_cast<XMLHttpRequestEventTarget*>(mXHR.get());
MOZ_ASSERT(targetHelper, "This should never fail!");
uint32_t lastEventType = aUpload ? STRING_LAST_EVENTTARGET : STRING_LAST_XHR;
nsAutoString eventType;
for (uint32_t index = 0; index <= lastEventType; index++) {
eventType = NS_ConvertASCIItoUTF16(sEventStrings[index]);
for (const EventType* type : Events::All) {
if (aUpload && *type == Events::readystatechange) {
continue;
}
if (aAdd) {
if (NS_FAILED(targetHelper->AddEventListener(eventType, this, false))) {
if (NS_FAILED(targetHelper->AddEventListener(*type, this, false))) {
return false;
}
} else {
targetHelper->RemoveEventListener(eventType, this, false);
targetHelper->RemoveEventListener(*type, this, false);
}
}
@ -872,13 +838,16 @@ Proxy::HandleEvent(Event* aEvent) {
return NS_OK;
}
nsAutoString type;
aEvent->GetType(type);
nsAutoString _type;
aEvent->GetType(_type);
const EventType* typePtr = Events::Find(_type);
MOZ_DIAGNOSTIC_ASSERT(typePtr, "Shouldn't get non-XMLHttpRequest events");
const EventType& type = *typePtr;
bool isUploadTarget = mXHR != aEvent->GetTarget();
ProgressEvent* progressEvent = aEvent->AsProgressEvent();
if (mInOpen && type.EqualsASCII(sEventStrings[STRING_readystatechange])) {
if (mInOpen && type == Events::readystatechange) {
if (mXHR->ReadyState() == 1) {
mInnerEventStreamId++;
}
@ -901,7 +870,7 @@ Proxy::HandleEvent(Event* aEvent) {
RefPtr<EventRunnable> runnable;
if (progressEvent) {
if (!mIsSyncXHR || !type.EqualsASCII(sEventStrings[STRING_progress])) {
if (!mIsSyncXHR || type != Events::progress) {
runnable = new EventRunnable(
this, isUploadTarget, type, progressEvent->LengthComputable(),
progressEvent->Loaded(), progressEvent->Total(), scope);
@ -916,10 +885,9 @@ Proxy::HandleEvent(Event* aEvent) {
}
if (!isUploadTarget) {
if (type.EqualsASCII(sEventStrings[STRING_loadstart])) {
if (type == Events::loadstart) {
mMainThreadSeenLoadStart = true;
} else if (mMainThreadSeenLoadStart &&
type.EqualsASCII(sEventStrings[STRING_loadend])) {
} else if (mMainThreadSeenLoadStart && type == Events::loadend) {
mMainThreadSeenLoadStart = false;
RefPtr<LoadStartDetectionRunnable> runnable =
@ -940,7 +908,7 @@ NS_IMETHODIMP
LoadStartDetectionRunnable::Run() {
AssertIsOnMainThread();
mXHR->RemoveEventListener(mEventType, this, false);
mXHR->RemoveEventListener(Events::loadstart, this, false);
if (!mReceivedLoadStart) {
if (mProxy->mOutstandingSendCount > 1) {
@ -971,7 +939,7 @@ LoadStartDetectionRunnable::HandleEvent(Event* aEvent) {
{
nsAutoString type;
aEvent->GetType(type);
MOZ_ASSERT(type == mEventType);
MOZ_ASSERT(type == Events::loadstart);
}
#endif
@ -1000,7 +968,7 @@ bool EventRunnable::PreDispatch(WorkerPrivate* /* unused */) {
XMLHttpRequestResponseType type = xhr->ResponseType();
// We want to take the result data only if this is available.
if (mType.EqualsASCII(sEventStrings[STRING_readystatechange])) {
if (mType == Events::readystatechange) {
switch (type) {
case XMLHttpRequestResponseType::_empty:
case XMLHttpRequestResponseType::Text: {
@ -1058,17 +1026,17 @@ bool EventRunnable::WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) {
return true;
}
if (mType.EqualsASCII(sEventStrings[STRING_loadend])) {
if (mType == Events::loadend) {
mProxy->mLastErrorDetailAtLoadend = mErrorDetail;
}
if (mType.EqualsASCII(sEventStrings[STRING_loadstart])) {
if (mType == Events::loadstart) {
if (mUploadEvent) {
mProxy->mSeenUploadLoadStart = true;
} else {
mProxy->mSeenLoadStart = true;
}
} else if (mType.EqualsASCII(sEventStrings[STRING_loadend])) {
} else if (mType == Events::loadend) {
if (mUploadEvent) {
mProxy->mSeenUploadLoadStart = false;
if (mProxy->mDispatchPrematureAbortEventToUpload) {
@ -1082,7 +1050,7 @@ bool EventRunnable::WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) {
return true;
}
}
} else if (mType.EqualsASCII(sEventStrings[STRING_abort])) {
} else if (mType == Events::abort) {
if ((mUploadEvent && mProxy->mDispatchPrematureAbortEventToUpload) ||
(!mUploadEvent && mProxy->mDispatchPrematureAbortEvent)) {
// We've already dispatched premature abort events.
@ -1116,10 +1084,9 @@ bool EventRunnable::WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) {
state->mResponseURL = mResponseURL;
XMLHttpRequestWorker* xhr = mProxy->mXMLHttpRequestPrivate;
xhr->UpdateState(std::move(state),
mType.EqualsASCII(sEventStrings[STRING_readystatechange])
? std::move(mResponseData)
: nullptr);
xhr->UpdateState(std::move(state), mType == Events::readystatechange
? std::move(mResponseData)
: nullptr);
if (mUploadEvent && !xhr->GetUploadObjectNoCreate()) {
return true;
@ -1518,12 +1485,12 @@ void XMLHttpRequestWorker::MaybeDispatchPrematureAbortEvents(ErrorResult& aRv) {
if (mProxy->mSeenUploadLoadStart) {
MOZ_ASSERT(mUpload);
DispatchPrematureAbortEvent(mUpload, u"abort"_ns, true, aRv);
FireEvent(mUpload, Events::abort, true, aRv);
if (aRv.Failed()) {
return;
}
DispatchPrematureAbortEvent(mUpload, u"loadend"_ns, true, aRv);
FireEvent(mUpload, Events::loadend, true, aRv);
if (aRv.Failed()) {
return;
}
@ -1540,18 +1507,18 @@ void XMLHttpRequestWorker::MaybeDispatchPrematureAbortEvents(ErrorResult& aRv) {
if (mProxy->mSeenLoadStart) {
if (isStateChanged) {
DispatchPrematureAbortEvent(this, u"readystatechange"_ns, false, aRv);
FireEvent(this, Events::readystatechange, false, aRv);
if (aRv.Failed()) {
return;
}
}
DispatchPrematureAbortEvent(this, u"abort"_ns, false, aRv);
FireEvent(this, Events::abort, false, aRv);
if (aRv.Failed()) {
return;
}
DispatchPrematureAbortEvent(this, u"loadend"_ns, false, aRv);
FireEvent(this, Events::loadend, false, aRv);
if (aRv.Failed()) {
return;
}
@ -1567,9 +1534,9 @@ void XMLHttpRequestWorker::MaybeDispatchPrematureAbortEvents(ErrorResult& aRv) {
}
}
void XMLHttpRequestWorker::DispatchPrematureAbortEvent(
EventTarget* aTarget, const nsAString& aEventType, bool aUploadTarget,
ErrorResult& aRv) {
void XMLHttpRequestWorker::FireEvent(EventTarget* aTarget,
const EventType& aEventType,
bool aUploadTarget, ErrorResult& aRv) {
mWorkerPrivate->AssertIsOnWorkerThread();
MOZ_ASSERT(aTarget);
@ -1579,12 +1546,11 @@ void XMLHttpRequestWorker::DispatchPrematureAbortEvent(
}
RefPtr<Event> event;
if (aEventType.EqualsLiteral("readystatechange")) {
if (aEventType == Events::readystatechange) {
event = NS_NewDOMEvent(aTarget, nullptr, nullptr);
event->InitEvent(aEventType, false, false);
} else {
if (mProxy->mIsSyncXHR &&
aEventType.EqualsASCII(sEventStrings[STRING_progress])) {
if (mProxy->mIsSyncXHR && aEventType == Events::progress) {
return;
}
@ -1612,7 +1578,7 @@ void XMLHttpRequestWorker::DispatchPrematureAbortEvent(
MOZ_LOG(gXMLHttpRequestLog, LogLevel::Debug,
("%p firing %s pre-abort event (%u,%u,%" PRIu64 ",%" PRIu64, this,
NS_ConvertUTF16toUTF8(aEventType).get(), aUploadTarget,
aEventType.cStr, aUploadTarget,
aUploadTarget ? mProxy->mLastUploadLengthComputable
: mProxy->mLastLengthComputable,
aUploadTarget ? mProxy->mLastUploadLoaded : mProxy->mLastLoaded,

Просмотреть файл

@ -241,9 +241,8 @@ class XMLHttpRequestWorker final : public SupportsWeakPtr,
void MaybeDispatchPrematureAbortEvents(ErrorResult& aRv);
void DispatchPrematureAbortEvent(EventTarget* aTarget,
const nsAString& aEventType,
bool aUploadTarget, ErrorResult& aRv);
void FireEvent(EventTarget* aTarget, const EventType& aEventType,
bool aUploadTarget, ErrorResult& aRv);
void Send(JSContext* aCx, JS::Handle<JSObject*> aBody, ErrorResult& aRv);