Bug 1882660 [wpt PR 44844] - IDB: Consolidate previously extended WPT files into...
[gecko.git] / dom / abort / AbortController.cpp
blob9bd8202559bcc4b57f9210ebef93b21fe54e97d1
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 "AbortController.h"
8 #include "AbortSignal.h"
9 #include "js/Value.h"
10 #include "mozilla/dom/AbortControllerBinding.h"
11 #include "mozilla/dom/BindingUtils.h"
12 #include "mozilla/dom/DOMException.h"
13 #include "mozilla/dom/WorkerPrivate.h"
14 #include "mozilla/HoldDropJSObjects.h"
16 namespace mozilla::dom {
18 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WITH_JS_MEMBERS(AbortController,
19 (mGlobal, mSignal),
20 (mReason))
22 NS_IMPL_CYCLE_COLLECTING_ADDREF(AbortController)
23 NS_IMPL_CYCLE_COLLECTING_RELEASE(AbortController)
25 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AbortController)
26 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
27 NS_INTERFACE_MAP_ENTRY(nsISupports)
28 NS_INTERFACE_MAP_END
30 /* static */
31 already_AddRefed<AbortController> AbortController::Constructor(
32 const GlobalObject& aGlobal, ErrorResult& aRv) {
33 nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
34 if (!global) {
35 aRv.Throw(NS_ERROR_FAILURE);
36 return nullptr;
39 RefPtr<AbortController> abortController = new AbortController(global);
40 return abortController.forget();
43 AbortController::AbortController(nsIGlobalObject* aGlobal)
44 : mGlobal(aGlobal), mAborted(false), mReason(JS::UndefinedHandleValue) {
45 mozilla::HoldJSObjects(this);
48 JSObject* AbortController::WrapObject(JSContext* aCx,
49 JS::Handle<JSObject*> aGivenProto) {
50 return AbortController_Binding::Wrap(aCx, this, aGivenProto);
53 nsIGlobalObject* AbortController::GetParentObject() const { return mGlobal; }
55 AbortSignal* AbortController::Signal() {
56 if (!mSignal) {
57 JS::Rooted<JS::Value> reason(RootingCx(), mReason);
58 mSignal = new AbortSignal(mGlobal, mAborted, reason);
61 return mSignal;
64 void AbortController::Abort(JSContext* aCx, JS::Handle<JS::Value> aReason) {
65 if (mAborted) {
66 return;
69 mAborted = true;
71 if (mSignal) {
72 mSignal->SignalAbort(aReason);
73 } else {
74 mReason = aReason;
78 AbortController::~AbortController() { mozilla::DropJSObjects(this); }
80 } // namespace mozilla::dom