1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
/* -*- 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/. */
#include "AbortSignal.h"
#include "mozilla/dom/Event.h"
#include "mozilla/dom/EventBinding.h"
#include "mozilla/dom/AbortSignalBinding.h"
#include "mozilla/RefPtr.h"
namespace mozilla::dom {
// AbortSignalImpl
// ----------------------------------------------------------------------------
AbortSignalImpl::AbortSignalImpl(bool aAborted) : mAborted(aAborted) {}
bool AbortSignalImpl::Aborted() const { return mAborted; }
// https://dom.spec.whatwg.org/#abortsignal-signal-abort steps 1-4
void AbortSignalImpl::SignalAbort() {
// Step 1.
if (mAborted) {
return;
}
// Step 2.
mAborted = true;
// 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.
for (RefPtr<AbortFollower> follower : mFollowers.ForwardRange()) {
MOZ_ASSERT(follower->mFollowingSignal == this);
follower->RunAbortAlgorithm();
}
// Step 4.
// Clear follower->signal links, then clear signal->follower links.
for (AbortFollower* follower : mFollowers.ForwardRange()) {
follower->mFollowingSignal = nullptr;
}
mFollowers.Clear();
}
/* static */ void AbortSignalImpl::Traverse(
AbortSignalImpl* aSignal, nsCycleCollectionTraversalCallback& cb) {
// To be filled in shortly.
}
// AbortSignal
// ----------------------------------------------------------------------------
NS_IMPL_CYCLE_COLLECTION_CLASS(AbortSignal)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(AbortSignal,
DOMEventTargetHelper)
AbortSignalImpl::Traverse(static_cast<AbortSignalImpl*>(tmp), cb);
AbortFollower::Traverse(static_cast<AbortFollower*>(tmp), cb);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(AbortSignal,
DOMEventTargetHelper)
AbortSignalImpl::Unlink(static_cast<AbortSignalImpl*>(tmp));
AbortFollower::Unlink(static_cast<AbortFollower*>(tmp));
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AbortSignal)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
NS_IMPL_ADDREF_INHERITED(AbortSignal, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(AbortSignal, DOMEventTargetHelper)
AbortSignal::AbortSignal(nsIGlobalObject* aGlobalObject, bool aAborted)
: DOMEventTargetHelper(aGlobalObject), AbortSignalImpl(aAborted) {}
JSObject* AbortSignal::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return AbortSignal_Binding::Wrap(aCx, this, aGivenProto);
}
// https://dom.spec.whatwg.org/#abortsignal-signal-abort
void AbortSignal::SignalAbort() {
// Steps 1-4.
AbortSignalImpl::SignalAbort();
// Step 5.
EventInit init;
init.mBubbles = false;
init.mCancelable = false;
RefPtr<Event> event = Event::Constructor(this, u"abort"_ns, init);
event->SetTrusted(true);
DispatchEvent(*event);
}
// AbortFollower
// ----------------------------------------------------------------------------
AbortFollower::~AbortFollower() { Unfollow(); }
// https://dom.spec.whatwg.org/#abortsignal-add
void AbortFollower::Follow(AbortSignalImpl* aSignal) {
// Step 1.
if (aSignal->mAborted) {
return;
}
MOZ_DIAGNOSTIC_ASSERT(aSignal);
Unfollow();
// Step 2.
mFollowingSignal = aSignal;
MOZ_ASSERT(!aSignal->mFollowers.Contains(this));
aSignal->mFollowers.AppendElement(this);
}
// https://dom.spec.whatwg.org/#abortsignal-remove
void AbortFollower::Unfollow() {
if (mFollowingSignal) {
// |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);
mFollowingSignal = nullptr;
}
}
bool AbortFollower::IsFollowing() const { return !!mFollowingSignal; }
/* static */ void AbortFollower::Traverse(
AbortFollower* aFollower, nsCycleCollectionTraversalCallback& cb) {
ImplCycleCollectionTraverse(cb, aFollower->mFollowingSignal,
"mFollowingSignal", 0);
}
} // namespace mozilla::dom
|