File: AbortSignal.cpp

package info (click to toggle)
firefox 147.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,484 kB
  • sloc: cpp: 7,607,246; javascript: 6,533,185; ansic: 3,775,227; python: 1,415,393; xml: 634,561; asm: 438,951; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (495 lines) | stat: -rw-r--r-- 17,048 bytes parent folder | download | duplicates (3)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/* -*- 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/RefPtr.h"
#include "mozilla/dom/AbortSignalBinding.h"
#include "mozilla/dom/DOMException.h"
#include "mozilla/dom/Event.h"
#include "mozilla/dom/EventBinding.h"
#include "mozilla/dom/TimeoutHandler.h"
#include "mozilla/dom/TimeoutManager.h"
#include "mozilla/dom/ToJSValue.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "nsCycleCollectionParticipant.h"
#include "nsGlobalWindowInner.h"
#include "nsPIDOMWindow.h"

namespace mozilla::dom {

// AbortSignalImpl
// ----------------------------------------------------------------------------

AbortSignalImpl::AbortSignalImpl(SignalAborted aAborted,
                                 JS::Handle<JS::Value> aReason)
    : mReason(aReason), mAborted(aAborted) {
  MOZ_ASSERT_IF(!mReason.isUndefined(), Aborted());
}

bool AbortSignalImpl::Aborted() const { return mAborted == SignalAborted::Yes; }

void AbortSignalImpl::GetReason(JSContext* aCx,
                                JS::MutableHandle<JS::Value> aReason) {
  if (!Aborted()) {
    return;
  }
  MaybeAssignAbortError(aCx);
  aReason.set(mReason);
}

JS::Value AbortSignalImpl::RawReason() const { return mReason.get(); }

// https://dom.spec.whatwg.org/#abortsignal-signal-abort
void AbortSignalImpl::SignalAbort(JS::Handle<JS::Value> aReason) {
  // Step 1: If signal is aborted, then return.
  if (Aborted()) {
    return;
  }

  // Step 2: Set signal’s abort reason to reason if it is given; otherwise to a
  // new "AbortError" DOMException.
  //
  // (But given AbortSignalImpl is supposed to run without JS context, the
  // DOMException creation is deferred to the getter.)
  SetAborted(aReason);

  // Step 3 - 6
  SignalAbortWithDependents();
}

void AbortSignalImpl::SignalAbortWithDependents() {
  // AbortSignalImpl cannot have dependents, so just run abort steps for itself.
  RunAbortSteps();
}

// https://dom.spec.whatwg.org/#run-the-abort-steps
// This skips event firing as AbortSignalImpl is not supposed to be exposed to
// JS. It's done instead in AbortSignal::RunAbortSteps.
void AbortSignalImpl::RunAbortSteps() {
  // Step 1: For each algorithm of signal’s abort algorithms: run algorithm.
  //
  // 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 2: Empty signal’s abort algorithms.
  UnlinkFollowers();
}

void AbortSignalImpl::SetAborted(JS::Handle<JS::Value> aReason) {
  mAborted = SignalAborted::Yes;
  mReason = aReason;
}

void AbortSignalImpl::Traverse(AbortSignalImpl* aSignal,
                               nsCycleCollectionTraversalCallback& cb) {
  ImplCycleCollectionTraverse(cb, aSignal->mFollowers, "mFollowers", 0);
}

void AbortSignalImpl::Unlink(AbortSignalImpl* aSignal) {
  aSignal->mReason.setUndefined();
  aSignal->UnlinkFollowers();
}

void AbortSignalImpl::MaybeAssignAbortError(JSContext* aCx) {
  MOZ_ASSERT(Aborted());
  if (!mReason.isUndefined()) {
    return;
  }

  JS::Rooted<JS::Value> exception(aCx);
  RefPtr<DOMException> dom = DOMException::Create(NS_ERROR_DOM_ABORT_ERR);

  if (NS_WARN_IF(!ToJSValue(aCx, dom, &exception))) {
    return;
  }

  mReason.set(exception);
}

void AbortSignalImpl::UnlinkFollowers() {
  // Manually unlink all followers before destructing the array, or otherwise
  // the array will be accessed by Unfollow() while being destructed.
  for (RefPtr<AbortFollower>& follower : mFollowers.ForwardRange()) {
    follower->mFollowingSignal = nullptr;
  }
  mFollowers.Clear();
}

// AbortSignal
// ----------------------------------------------------------------------------

NS_IMPL_CYCLE_COLLECTION_CLASS(AbortSignal)

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(AbortSignal,
                                                  DOMEventTargetHelper)
  AbortSignalImpl::Traverse(static_cast<AbortSignalImpl*>(tmp), cb);
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDependentSignals)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(AbortSignal,
                                                DOMEventTargetHelper)
  AbortSignalImpl::Unlink(static_cast<AbortSignalImpl*>(tmp));
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mDependentSignals)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AbortSignal)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)

NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(AbortSignal,
                                               DOMEventTargetHelper)
  NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mReason)
NS_IMPL_CYCLE_COLLECTION_TRACE_END

NS_IMPL_ADDREF_INHERITED(AbortSignal, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(AbortSignal, DOMEventTargetHelper)

already_AddRefed<AbortSignal> AbortSignal::Create(
    nsIGlobalObject* aGlobalObject, SignalAborted aAborted,
    JS::Handle<JS::Value> aReason) {
  RefPtr<AbortSignal> signal =
      new AbortSignal(aGlobalObject, aAborted, aReason);
  signal->Init();
  return signal.forget();
}

void AbortSignal::Init() {
  // Init is use to separate this HoldJSObjects call to avoid calling
  // it in the constructor.
  //
  // We can't call HoldJSObjects in the constructor because it'll
  // addref `this` before the vtable is set up properly, so the parent
  // type gets stored in the CC participant table. This is problematic
  // for classes that inherit AbortSignal.
  mozilla::HoldJSObjects(this);
}

AbortSignal::AbortSignal(nsIGlobalObject* aGlobalObject, SignalAborted aAborted,
                         JS::Handle<JS::Value> aReason)
    : DOMEventTargetHelper(aGlobalObject),
      AbortSignalImpl(aAborted, aReason),
      mDependent(false) {}

JSObject* AbortSignal::WrapObject(JSContext* aCx,
                                  JS::Handle<JSObject*> aGivenProto) {
  return AbortSignal_Binding::Wrap(aCx, this, aGivenProto);
}

already_AddRefed<AbortSignal> AbortSignal::Abort(
    GlobalObject& aGlobal, JS::Handle<JS::Value> aReason) {
  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());

  RefPtr<AbortSignal> abortSignal =
      AbortSignal::Create(global, SignalAborted::Yes, aReason);
  return abortSignal.forget();
}

class AbortSignalTimeoutHandler final : public TimeoutHandler {
 public:
  AbortSignalTimeoutHandler(JSContext* aCx, AbortSignal* aSignal)
      : TimeoutHandler(aCx), mSignal(aSignal) {}

  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_CLASS(AbortSignalTimeoutHandler)

  // https://dom.spec.whatwg.org/#dom-abortsignal-timeout
  // Step 3
  MOZ_CAN_RUN_SCRIPT bool Call(const char* /* unused */) override {
    AutoJSAPI jsapi;
    if (NS_WARN_IF(!jsapi.Init(mSignal->GetParentObject()))) {
      // (false is only for setInterval, see
      // nsGlobalWindowInner::RunTimeoutHandler)
      return true;
    }

    // Step 1. Queue a global task on the timer task source given global to
    // signal abort given signal and a new "TimeoutError" DOMException.
    JS::Rooted<JS::Value> exception(jsapi.cx());
    RefPtr<DOMException> dom = DOMException::Create(NS_ERROR_DOM_TIMEOUT_ERR);
    if (NS_WARN_IF(!ToJSValue(jsapi.cx(), dom, &exception))) {
      return true;
    }

    mSignal->SignalAbort(exception);
    return true;
  }

 private:
  ~AbortSignalTimeoutHandler() override = default;

  RefPtr<AbortSignal> mSignal;
};

NS_IMPL_CYCLE_COLLECTION(AbortSignalTimeoutHandler, mSignal)
NS_IMPL_CYCLE_COLLECTING_ADDREF(AbortSignalTimeoutHandler)
NS_IMPL_CYCLE_COLLECTING_RELEASE(AbortSignalTimeoutHandler)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AbortSignalTimeoutHandler)
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

static void SetTimeoutForGlobal(GlobalObject& aGlobal, TimeoutHandler& aHandler,
                                int32_t timeout, ErrorResult& aRv) {
  if (NS_IsMainThread()) {
    nsCOMPtr<nsPIDOMWindowInner> innerWindow =
        do_QueryInterface(aGlobal.GetAsSupports());
    if (!innerWindow) {
      aRv.ThrowInvalidStateError("Could not find window.");
      return;
    }

    int32_t handle;
    nsresult rv =
        nsGlobalWindowInner::Cast(innerWindow)
            ->GetTimeoutManager()
            ->SetTimeout(&aHandler, timeout, /* aIsInterval */ false,
                         Timeout::Reason::eAbortSignalTimeout, &handle);
    if (NS_FAILED(rv)) {
      aRv.Throw(rv);
      return;
    }
  } else {
    WorkerPrivate* workerPrivate =
        GetWorkerPrivateFromContext(aGlobal.Context());
    workerPrivate->SetTimeout(aGlobal.Context(), &aHandler, timeout,
                              /* aIsInterval */ false,
                              Timeout::Reason::eAbortSignalTimeout, aRv);
    if (aRv.Failed()) {
      return;
    }
  }
}

// https://dom.spec.whatwg.org/#dom-abortsignal-timeout
already_AddRefed<AbortSignal> AbortSignal::Timeout(GlobalObject& aGlobal,
                                                   uint64_t aMilliseconds,
                                                   ErrorResult& aRv) {
  // Step 2. Let global be signal’s relevant global object.
  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());

  // Step 1. Let signal be a new AbortSignal object.
  RefPtr<AbortSignal> signal =
      AbortSignal::Create(global, SignalAborted::No, JS::UndefinedHandleValue);

  // Step 3. Run steps after a timeout given global, "AbortSignal-timeout",
  // milliseconds, and the following step: ...
  RefPtr<TimeoutHandler> handler =
      new AbortSignalTimeoutHandler(aGlobal.Context(), signal);

  // Note: We only supports int32_t range intervals
  int32_t timeout =
      aMilliseconds > uint64_t(std::numeric_limits<int32_t>::max())
          ? std::numeric_limits<int32_t>::max()
          : static_cast<int32_t>(aMilliseconds);

  SetTimeoutForGlobal(aGlobal, *handler, timeout, aRv);
  if (aRv.Failed()) {
    return nullptr;
  }

  // Step 4. Return signal.
  return signal.forget();
}

// https://dom.spec.whatwg.org/#create-a-dependent-abort-signal
already_AddRefed<AbortSignal> AbortSignal::Any(
    GlobalObject& aGlobal,
    const Sequence<OwningNonNull<AbortSignal>>& aSignals) {
  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
  return Any(global, aSignals, [](nsIGlobalObject* aGlobal) {
    return AbortSignal::Create(aGlobal, SignalAborted::No,
                               JS::UndefinedHandleValue);
  });
}

already_AddRefed<AbortSignal> AbortSignal::Any(
    nsIGlobalObject* aGlobal,
    const Span<const OwningNonNull<AbortSignal>>& aSignals,
    FunctionRef<already_AddRefed<AbortSignal>(nsIGlobalObject* aGlobal)>
        aCreateResultSignal) {
  // Step 1. Let resultSignal be a new object implementing AbortSignal using
  // realm
  RefPtr<AbortSignal> resultSignal = aCreateResultSignal(aGlobal);

  if (!aSignals.IsEmpty()) {
    // (Prepare for step 2 which uses the reason of this. Cannot use
    // RawReason because that can cause constructing new DOMException for each
    // dependent signal instead of sharing the single one.)
    AutoJSAPI jsapi;
    if (!jsapi.Init(aGlobal)) {
      return nullptr;
    }
    JSContext* cx = jsapi.cx();

    // Step 2. For each signal of signals: if signal is aborted, then set
    // resultSignal's abort reason to signal's abort reason and return
    // resultSignal.
    for (const auto& signal : aSignals) {
      if (signal->Aborted()) {
        JS::Rooted<JS::Value> reason(cx);
        signal->GetReason(cx, &reason);
        resultSignal->SetAborted(reason);
        return resultSignal.forget();
      }
    }
  }

  // Step 3. Set resultSignal's dependent to true
  resultSignal->mDependent = true;

  // Step 4. For each signal of signals
  for (const auto& signal : aSignals) {
    if (!signal->Dependent()) {
      // Step 4.1. If signal is not dependent, make resultSignal dependent on it
      resultSignal->MakeDependentOn(signal);
    } else {
      // Step 4.2. Otherwise, make resultSignal dependent on its source signals
      for (const auto& sourceSignal : signal->mSourceSignals) {
        if (!sourceSignal) {
          // Bug 1908466, sourceSignal might have been garbage collected.
          // As signal is not aborted, sourceSignal also wasn't.
          // Thus do not depend on it, as it cannot be aborted anymore.
          continue;
        }
        MOZ_ASSERT(!sourceSignal->Aborted() && !sourceSignal->Dependent());
        resultSignal->MakeDependentOn(sourceSignal);
      }
    }
  }

  // Step 5. Return resultSignal.
  return resultSignal.forget();
}

void AbortSignal::MakeDependentOn(AbortSignal* aSignal) {
  MOZ_ASSERT(mDependent);
  MOZ_ASSERT(aSignal);
  // append only if not already contained in list
  // https://infra.spec.whatwg.org/#set-append
  if (!mSourceSignals.Contains(aSignal)) {
    mSourceSignals.AppendElement(aSignal);
  }
  if (!aSignal->mDependentSignals.Contains(this)) {
    aSignal->mDependentSignals.AppendElement(this);
  }
}

// https://dom.spec.whatwg.org/#dom-abortsignal-throwifaborted
void AbortSignal::ThrowIfAborted(JSContext* aCx, ErrorResult& aRv) {
  aRv.MightThrowJSException();

  if (Aborted()) {
    JS::Rooted<JS::Value> reason(aCx);
    GetReason(aCx, &reason);
    aRv.ThrowJSException(aCx, reason);
  }
}

// Step 3 - 6 of https://dom.spec.whatwg.org/#abortsignal-signal-abort
void AbortSignal::SignalAbortWithDependents() {
  // Step 3: Let dependentSignalsToAbort be a new list.
  nsTArray<RefPtr<AbortSignal>> dependentSignalsToAbort;

  // mDependentSignals can go away after this function.
  nsTArray<RefPtr<AbortSignal>> dependentSignals = std::move(mDependentSignals);

  if (!dependentSignals.IsEmpty()) {
    // (Prepare for step 4.1.1 which uses the reason of this. Cannot use
    // RawReason because that can cause constructing new DOMException for each
    // dependent signal instead of sharing the single one.)
    AutoJSAPI jsapi;
    if (!jsapi.Init(GetParentObject())) {
      return;
    }
    JSContext* cx = jsapi.cx();
    JS::Rooted<JS::Value> reason(cx);
    GetReason(cx, &reason);

    // Step 4. For each dependentSignal of signal’s dependent signals:
    for (const auto& dependentSignal : dependentSignals) {
      MOZ_ASSERT(dependentSignal->mSourceSignals.Contains(this));
      // Step 4.1: If dependentSignal is not aborted, then:
      if (!dependentSignal->Aborted()) {
        // Step 4.1.1: Set dependentSignal’s abort reason to signal’s abort
        // reason.
        dependentSignal->SetAborted(reason);
        // Step 4.1.2: Append dependentSignal to dependentSignalsToAbort.
        dependentSignalsToAbort.AppendElement(dependentSignal);
      }
    }
  }

  // Step 5: Run the abort steps for signal.
  RunAbortSteps();

  // Step 6: For each dependentSignal of dependentSignalsToAbort, run the abort
  // steps for dependentSignal.
  for (const auto& dependentSignal : dependentSignalsToAbort) {
    dependentSignal->RunAbortSteps();
  }
}

// https://dom.spec.whatwg.org/#run-the-abort-steps
void AbortSignal::RunAbortSteps() {
  // Step 1 - 2:
  AbortSignalImpl::RunAbortSteps();

  // Step 3. Fire an event named abort at this signal.
  EventInit init;
  init.mBubbles = false;
  init.mCancelable = false;

  RefPtr<Event> event = Event::Constructor(this, u"abort"_ns, init);
  event->SetTrusted(true);

  DispatchEvent(*event);
}

bool AbortSignal::Dependent() const { return mDependent; }

AbortSignal::~AbortSignal() { mozilla::DropJSObjects(this); }

// AbortFollower
// ----------------------------------------------------------------------------

AbortFollower::~AbortFollower() { Unfollow(); }

// https://dom.spec.whatwg.org/#abortsignal-add
void AbortFollower::Follow(AbortSignalImpl* aSignal) {
  // Step 1.
  if (aSignal->Aborted()) {
    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; }

}  // namespace mozilla::dom