Bug 1700051: part 26) Correct typo in comment of `mozInlineSpellWordUtil::BuildSoftTe...
[gecko.git] / dom / fetch / FetchObserver.cpp
blob7bdf6b2db84746b652052e42c78e27c75ec95dc1
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 AbortFollower::Traverse(static_cast<AbortFollower*>(tmp), cb);
18 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
20 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(FetchObserver,
21 DOMEventTargetHelper)
22 AbortFollower::Unlink(static_cast<AbortFollower*>(tmp));
23 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
25 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FetchObserver)
26 NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
28 NS_IMPL_ADDREF_INHERITED(FetchObserver, DOMEventTargetHelper)
29 NS_IMPL_RELEASE_INHERITED(FetchObserver, DOMEventTargetHelper)
31 FetchObserver::FetchObserver(nsIGlobalObject* aGlobal,
32 AbortSignalImpl* aSignalImpl)
33 : DOMEventTargetHelper(aGlobal), mState(FetchState::Requesting) {
34 if (aSignalImpl) {
35 Follow(aSignalImpl);
39 JSObject* FetchObserver::WrapObject(JSContext* aCx,
40 JS::Handle<JSObject*> aGivenProto) {
41 return FetchObserver_Binding::Wrap(aCx, this, aGivenProto);
44 FetchState FetchObserver::State() const { return mState; }
46 void FetchObserver::RunAbortAlgorithm() { SetState(FetchState::Aborted); }
48 void FetchObserver::SetState(FetchState aState) {
49 MOZ_ASSERT(mState < aState);
51 if (mState == FetchState::Aborted || mState == FetchState::Errored ||
52 mState == FetchState::Complete) {
53 // We are already in a final state.
54 return;
57 // We cannot pass from Requesting to Complete directly.
58 if (mState == FetchState::Requesting && aState == FetchState::Complete) {
59 SetState(FetchState::Responding);
62 mState = aState;
64 if (mState == FetchState::Aborted || mState == FetchState::Errored ||
65 mState == FetchState::Complete) {
66 Unfollow();
69 EventInit init;
70 init.mBubbles = false;
71 init.mCancelable = false;
73 // TODO which kind of event should we dispatch here?
75 RefPtr<Event> event = Event::Constructor(this, u"statechange"_ns, init);
76 event->SetTrusted(true);
78 DispatchEvent(*event);
81 } // namespace mozilla::dom