Issue #1587 - Part 5: Hook FetchObserver up to the Fetch API

This commit is contained in:
Moonchild 2020-06-11 08:51:07 +00:00 committed by Roy Tam
commit 3d2ca85edd
11 changed files with 399 additions and 75 deletions

View file

@ -6,6 +6,7 @@
#include "FetchObserver.h"
#include "WorkerPrivate.h"
#include "mozilla/dom/Event.h"
namespace mozilla {
namespace dom {
@ -45,10 +46,14 @@ FetchObserver::IsEnabled(JSContext* aCx, JSObject* aGlobal)
}
FetchObserver::FetchObserver(nsIGlobalObject* aGlobal,
FetchState aState)
FetchSignal* aSignal)
: DOMEventTargetHelper(aGlobal)
, mState(aState)
{}
, mState(FetchState::Requesting)
{
if (aSignal) {
Follow(aSignal);
}
}
JSObject*
FetchObserver::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
@ -62,5 +67,51 @@ FetchObserver::State() const
return mState;
}
void
FetchObserver::Aborted()
{
SetState(FetchState::Aborted);
}
void
FetchObserver::SetState(FetchState aState)
{
MOZ_ASSERT(mState < aState);
if (mState == FetchState::Aborted ||
mState == FetchState::Errored ||
mState == FetchState::Complete) {
// We are already in a final state.
return;
}
// We cannot pass from Requesting to Complete directly.
if (mState == FetchState::Requesting &&
aState == FetchState::Complete) {
SetState(FetchState::Responding);
}
mState = aState;
if (mState == FetchState::Aborted ||
mState == FetchState::Errored ||
mState == FetchState::Complete) {
Unfollow();
}
EventInit init;
init.mBubbles = false;
init.mCancelable = false;
// TODO which kind of event should we dispatch here?
RefPtr<Event> event =
Event::Constructor(this, NS_LITERAL_STRING("statechange"), init);
event->SetTrusted(true);
bool dummy;
DispatchEvent(event, &dummy);
}
} // dom namespace
} // mozilla namespace