2017-03-22 13:04:57 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
#include "AbortSignal.h"
|
2017-10-12 15:50:23 +03:00
|
|
|
|
2017-03-22 13:04:57 +03:00
|
|
|
#include "mozilla/dom/Event.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/dom/EventBinding.h"
|
2017-08-29 12:31:06 +03:00
|
|
|
#include "mozilla/dom/AbortSignalBinding.h"
|
2020-10-29 11:02:58 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2017-03-22 13:04:57 +03:00
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom {
|
2017-03-22 13:04:57 +03:00
|
|
|
|
2018-08-26 15:16:21 +03:00
|
|
|
// AbortSignalImpl
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
AbortSignalImpl::AbortSignalImpl(bool aAborted) : mAborted(aAborted) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-08-26 15:16:21 +03:00
|
|
|
bool AbortSignalImpl::Aborted() const { return mAborted; }
|
|
|
|
|
2020-11-03 02:50:12 +03:00
|
|
|
// https://dom.spec.whatwg.org/#abortsignal-signal-abort steps 1-4
|
2020-10-22 10:42:18 +03:00
|
|
|
void AbortSignalImpl::SignalAbort() {
|
2020-11-03 02:50:12 +03:00
|
|
|
// Step 1.
|
2018-08-26 15:16:21 +03:00
|
|
|
if (mAborted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-03 02:50:12 +03:00
|
|
|
// Step 2.
|
2018-08-26 15:16:21 +03:00
|
|
|
mAborted = true;
|
|
|
|
|
2020-11-03 02:50:12 +03:00
|
|
|
// Step 3.
|
|
|
|
// When there are multiple followers, the follower removal algorithm
|
|
|
|
// https://dom.spec.whatwg.org/#abortsignal-remove could be invoked in an
|
|
|
|
// earlier algorithm to remove a later algorithm, so |mFollowers| must be a
|
|
|
|
// |nsTObserverArray| to defend against mutation.
|
2020-09-22 01:11:19 +03:00
|
|
|
for (RefPtr<AbortFollower> follower : mFollowers.ForwardRange()) {
|
2020-11-03 02:50:12 +03:00
|
|
|
MOZ_ASSERT(follower->mFollowingSignal == this);
|
2020-10-22 10:42:18 +03:00
|
|
|
follower->RunAbortAlgorithm();
|
2018-08-26 15:16:21 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 02:50:12 +03:00
|
|
|
// Step 4.
|
|
|
|
// Clear follower->signal links, then clear signal->follower links.
|
|
|
|
for (AbortFollower* follower : mFollowers.ForwardRange()) {
|
|
|
|
follower->mFollowingSignal = nullptr;
|
2018-08-26 15:16:21 +03:00
|
|
|
}
|
2020-11-03 02:50:12 +03:00
|
|
|
mFollowers.Clear();
|
2018-08-26 15:16:21 +03:00
|
|
|
}
|
|
|
|
|
2020-10-29 11:02:58 +03:00
|
|
|
/* static */ void AbortSignalImpl::Traverse(
|
|
|
|
AbortSignalImpl* aSignal, nsCycleCollectionTraversalCallback& cb) {
|
|
|
|
// To be filled in shortly.
|
|
|
|
}
|
|
|
|
|
2018-08-26 15:16:21 +03:00
|
|
|
// AbortSignal
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(AbortSignal)
|
2017-03-22 13:04:57 +03:00
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(AbortSignal,
|
2017-03-22 13:04:57 +03:00
|
|
|
DOMEventTargetHelper)
|
2020-10-29 11:02:58 +03:00
|
|
|
AbortSignalImpl::Traverse(static_cast<AbortSignalImpl*>(tmp), cb);
|
|
|
|
AbortFollower::Traverse(static_cast<AbortFollower*>(tmp), cb);
|
2017-03-22 13:04:57 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(AbortSignal,
|
2017-03-22 13:04:57 +03:00
|
|
|
DOMEventTargetHelper)
|
2020-10-29 11:02:58 +03:00
|
|
|
AbortSignalImpl::Unlink(static_cast<AbortSignalImpl*>(tmp));
|
|
|
|
AbortFollower::Unlink(static_cast<AbortFollower*>(tmp));
|
2017-03-22 13:04:57 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AbortSignal)
|
2017-03-22 13:04:57 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(AbortSignal, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(AbortSignal, DOMEventTargetHelper)
|
2017-03-22 13:04:57 +03:00
|
|
|
|
2018-08-03 08:06:16 +03:00
|
|
|
AbortSignal::AbortSignal(nsIGlobalObject* aGlobalObject, bool aAborted)
|
|
|
|
: DOMEventTargetHelper(aGlobalObject), AbortSignalImpl(aAborted) {}
|
2017-03-22 13:06:08 +03:00
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
JSObject* AbortSignal::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return AbortSignal_Binding::Wrap(aCx, this, aGivenProto);
|
2017-03-22 13:04:57 +03:00
|
|
|
}
|
|
|
|
|
2021-03-15 23:46:53 +03:00
|
|
|
already_AddRefed<AbortSignal> AbortSignal::Abort(GlobalObject& aGlobal) {
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
RefPtr<AbortSignal> abortSignal = new AbortSignal(global, true);
|
|
|
|
return abortSignal.forget();
|
|
|
|
}
|
|
|
|
|
2020-11-03 02:50:12 +03:00
|
|
|
// https://dom.spec.whatwg.org/#abortsignal-signal-abort
|
2020-10-22 10:42:18 +03:00
|
|
|
void AbortSignal::SignalAbort() {
|
2020-11-03 02:50:12 +03:00
|
|
|
// Steps 1-4.
|
2020-10-22 10:42:18 +03:00
|
|
|
AbortSignalImpl::SignalAbort();
|
2017-03-22 13:05:10 +03:00
|
|
|
|
2020-11-03 02:50:12 +03:00
|
|
|
// Step 5.
|
2017-03-22 13:04:57 +03:00
|
|
|
EventInit init;
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
|
|
|
|
RefPtr<Event> event = Event::Constructor(this, u"abort"_ns, init);
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
2018-04-05 20:42:41 +03:00
|
|
|
DispatchEvent(*event);
|
2017-03-22 13:05:10 +03:00
|
|
|
}
|
|
|
|
|
2017-08-29 12:31:07 +03:00
|
|
|
// AbortFollower
|
2017-03-22 13:05:10 +03:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2017-08-29 12:31:07 +03:00
|
|
|
AbortFollower::~AbortFollower() { Unfollow(); }
|
2017-03-22 13:05:10 +03:00
|
|
|
|
2020-11-03 02:50:12 +03:00
|
|
|
// https://dom.spec.whatwg.org/#abortsignal-add
|
2018-08-26 15:16:21 +03:00
|
|
|
void AbortFollower::Follow(AbortSignalImpl* aSignal) {
|
2020-11-03 02:50:12 +03:00
|
|
|
// Step 1.
|
|
|
|
if (aSignal->mAborted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-22 13:05:10 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aSignal);
|
|
|
|
|
|
|
|
Unfollow();
|
|
|
|
|
2020-11-03 02:50:12 +03:00
|
|
|
// Step 2.
|
2017-03-22 13:05:10 +03:00
|
|
|
mFollowingSignal = aSignal;
|
2020-11-03 02:50:12 +03:00
|
|
|
MOZ_ASSERT(!aSignal->mFollowers.Contains(this));
|
|
|
|
aSignal->mFollowers.AppendElement(this);
|
2017-03-22 13:05:10 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 02:50:12 +03:00
|
|
|
// https://dom.spec.whatwg.org/#abortsignal-remove
|
2017-08-29 12:31:07 +03:00
|
|
|
void AbortFollower::Unfollow() {
|
2017-03-22 13:05:10 +03:00
|
|
|
if (mFollowingSignal) {
|
2020-11-03 02:50:12 +03:00
|
|
|
// |Unfollow| is called by cycle-collection unlink code that runs in no
|
|
|
|
// guaranteed order. So we can't, symmetric with |Follow| above, assert
|
|
|
|
// that |this| will be found in |mFollowingSignal->mFollowers|.
|
|
|
|
mFollowingSignal->mFollowers.RemoveElement(this);
|
2017-03-22 13:05:10 +03:00
|
|
|
mFollowingSignal = nullptr;
|
|
|
|
}
|
2017-03-22 13:04:57 +03:00
|
|
|
}
|
|
|
|
|
2017-08-29 12:31:07 +03:00
|
|
|
bool AbortFollower::IsFollowing() const { return !!mFollowingSignal; }
|
2017-08-29 12:31:06 +03:00
|
|
|
|
2020-10-29 11:02:58 +03:00
|
|
|
/* static */ void AbortFollower::Traverse(
|
|
|
|
AbortFollower* aFollower, nsCycleCollectionTraversalCallback& cb) {
|
|
|
|
ImplCycleCollectionTraverse(cb, aFollower->mFollowingSignal,
|
|
|
|
"mFollowingSignal", 0);
|
|
|
|
}
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom
|