FetchObserver.cpp (2512B)
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/. */ 6 7 #include "FetchObserver.h" 8 9 #include "mozilla/dom/Event.h" 10 #include "mozilla/dom/EventBinding.h" 11 12 namespace mozilla::dom { 13 14 NS_IMPL_CYCLE_COLLECTION_CLASS(FetchObserver) 15 16 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(FetchObserver, 17 DOMEventTargetHelper) 18 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END 19 20 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(FetchObserver, 21 DOMEventTargetHelper) 22 NS_IMPL_CYCLE_COLLECTION_UNLINK_END 23 24 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FetchObserver) 25 NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper) 26 27 NS_IMPL_ADDREF_INHERITED(FetchObserver, DOMEventTargetHelper) 28 NS_IMPL_RELEASE_INHERITED(FetchObserver, DOMEventTargetHelper) 29 30 FetchObserver::FetchObserver(nsIGlobalObject* aGlobal, 31 AbortSignalImpl* aSignalImpl) 32 : DOMEventTargetHelper(aGlobal), mState(FetchState::Requesting) { 33 if (aSignalImpl) { 34 Follow(aSignalImpl); 35 } 36 } 37 38 JSObject* FetchObserver::WrapObject(JSContext* aCx, 39 JS::Handle<JSObject*> aGivenProto) { 40 return FetchObserver_Binding::Wrap(aCx, this, aGivenProto); 41 } 42 43 FetchState FetchObserver::State() const { return mState; } 44 45 void FetchObserver::RunAbortAlgorithm() { SetState(FetchState::Aborted); } 46 47 void FetchObserver::SetState(FetchState aState) { 48 MOZ_ASSERT(mState < aState); 49 50 if (mState == FetchState::Aborted || mState == FetchState::Errored || 51 mState == FetchState::Complete) { 52 // We are already in a final state. 53 return; 54 } 55 56 // We cannot pass from Requesting to Complete directly. 57 if (mState == FetchState::Requesting && aState == FetchState::Complete) { 58 SetState(FetchState::Responding); 59 } 60 61 mState = aState; 62 63 if (mState == FetchState::Aborted || mState == FetchState::Errored || 64 mState == FetchState::Complete) { 65 Unfollow(); 66 } 67 68 EventInit init; 69 init.mBubbles = false; 70 init.mCancelable = false; 71 72 // TODO which kind of event should we dispatch here? 73 74 RefPtr<Event> event = Event::Constructor(this, u"statechange"_ns, init); 75 event->SetTrusted(true); 76 77 DispatchEvent(*event); 78 } 79 80 } // namespace mozilla::dom