Bug 1882465 - Update .hg-annotate-ignore-revs and .git-blame-ignore-revs to reflect...
[gecko.git] / dom / fetch / FetchObserver.cpp
blob0c9d1b9f7aabf17c0bf4de065cabad817b7c730f
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "FetchObserver.h"
8 #include "mozilla/dom/Event.h"
9 #include "mozilla/dom/EventBinding.h"
11 namespace mozilla::dom {
13 NS_IMPL_CYCLE_COLLECTION_CLASS(FetchObserver)
15 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(FetchObserver,
16 DOMEventTargetHelper)
17 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
19 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(FetchObserver,
20 DOMEventTargetHelper)
21 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
23 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FetchObserver)
24 NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
26 NS_IMPL_ADDREF_INHERITED(FetchObserver, DOMEventTargetHelper)
27 NS_IMPL_RELEASE_INHERITED(FetchObserver, DOMEventTargetHelper)
29 FetchObserver::FetchObserver(nsIGlobalObject* aGlobal,
30 AbortSignalImpl* aSignalImpl)
31 : DOMEventTargetHelper(aGlobal), mState(FetchState::Requesting) {
32 if (aSignalImpl) {
33 Follow(aSignalImpl);
37 JSObject* FetchObserver::WrapObject(JSContext* aCx,
38 JS::Handle<JSObject*> aGivenProto) {
39 return FetchObserver_Binding::Wrap(aCx, this, aGivenProto);
42 FetchState FetchObserver::State() const { return mState; }
44 void FetchObserver::RunAbortAlgorithm() { SetState(FetchState::Aborted); }
46 void FetchObserver::SetState(FetchState aState) {
47 MOZ_ASSERT(mState < aState);
49 if (mState == FetchState::Aborted || mState == FetchState::Errored ||
50 mState == FetchState::Complete) {
51 // We are already in a final state.
52 return;
55 // We cannot pass from Requesting to Complete directly.
56 if (mState == FetchState::Requesting && aState == FetchState::Complete) {
57 SetState(FetchState::Responding);
60 mState = aState;
62 if (mState == FetchState::Aborted || mState == FetchState::Errored ||
63 mState == FetchState::Complete) {
64 Unfollow();
67 EventInit init;
68 init.mBubbles = false;
69 init.mCancelable = false;
71 // TODO which kind of event should we dispatch here?
73 RefPtr<Event> event = Event::Constructor(this, u"statechange"_ns, init);
74 event->SetTrusted(true);
76 DispatchEvent(*event);
79 } // namespace mozilla::dom