From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- dom/abort/AbortController.cpp | 80 ++++++ dom/abort/AbortController.h | 56 ++++ dom/abort/AbortFollower.h | 90 +++++++ dom/abort/AbortSignal.cpp | 327 +++++++++++++++++++++++ dom/abort/AbortSignal.h | 66 +++++ dom/abort/moz.build | 25 ++ dom/abort/tests/mochitest.ini | 10 + dom/abort/tests/moz.build | 9 + dom/abort/tests/slow.sjs | 13 + dom/abort/tests/test_abort_controller.html | 73 +++++ dom/abort/tests/test_abort_controller_fetch.html | 80 ++++++ dom/abort/tests/test_event_listener_leaks.html | 43 +++ dom/abort/tests/unit/test_abort.js | 8 + dom/abort/tests/unit/xpcshell.ini | 5 + dom/abort/tests/worker_abort_controller_fetch.js | 33 +++ 15 files changed, 918 insertions(+) create mode 100644 dom/abort/AbortController.cpp create mode 100644 dom/abort/AbortController.h create mode 100644 dom/abort/AbortFollower.h create mode 100644 dom/abort/AbortSignal.cpp create mode 100644 dom/abort/AbortSignal.h create mode 100644 dom/abort/moz.build create mode 100644 dom/abort/tests/mochitest.ini create mode 100644 dom/abort/tests/moz.build create mode 100644 dom/abort/tests/slow.sjs create mode 100644 dom/abort/tests/test_abort_controller.html create mode 100644 dom/abort/tests/test_abort_controller_fetch.html create mode 100644 dom/abort/tests/test_event_listener_leaks.html create mode 100644 dom/abort/tests/unit/test_abort.js create mode 100644 dom/abort/tests/unit/xpcshell.ini create mode 100644 dom/abort/tests/worker_abort_controller_fetch.js (limited to 'dom/abort') diff --git a/dom/abort/AbortController.cpp b/dom/abort/AbortController.cpp new file mode 100644 index 0000000000..9bd8202559 --- /dev/null +++ b/dom/abort/AbortController.cpp @@ -0,0 +1,80 @@ +/* -*- 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 "AbortController.h" +#include "AbortSignal.h" +#include "js/Value.h" +#include "mozilla/dom/AbortControllerBinding.h" +#include "mozilla/dom/BindingUtils.h" +#include "mozilla/dom/DOMException.h" +#include "mozilla/dom/WorkerPrivate.h" +#include "mozilla/HoldDropJSObjects.h" + +namespace mozilla::dom { + +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WITH_JS_MEMBERS(AbortController, + (mGlobal, mSignal), + (mReason)) + +NS_IMPL_CYCLE_COLLECTING_ADDREF(AbortController) +NS_IMPL_CYCLE_COLLECTING_RELEASE(AbortController) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AbortController) + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY + NS_INTERFACE_MAP_ENTRY(nsISupports) +NS_INTERFACE_MAP_END + +/* static */ +already_AddRefed AbortController::Constructor( + const GlobalObject& aGlobal, ErrorResult& aRv) { + nsCOMPtr global = do_QueryInterface(aGlobal.GetAsSupports()); + if (!global) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + + RefPtr abortController = new AbortController(global); + return abortController.forget(); +} + +AbortController::AbortController(nsIGlobalObject* aGlobal) + : mGlobal(aGlobal), mAborted(false), mReason(JS::UndefinedHandleValue) { + mozilla::HoldJSObjects(this); +} + +JSObject* AbortController::WrapObject(JSContext* aCx, + JS::Handle aGivenProto) { + return AbortController_Binding::Wrap(aCx, this, aGivenProto); +} + +nsIGlobalObject* AbortController::GetParentObject() const { return mGlobal; } + +AbortSignal* AbortController::Signal() { + if (!mSignal) { + JS::Rooted reason(RootingCx(), mReason); + mSignal = new AbortSignal(mGlobal, mAborted, reason); + } + + return mSignal; +} + +void AbortController::Abort(JSContext* aCx, JS::Handle aReason) { + if (mAborted) { + return; + } + + mAborted = true; + + if (mSignal) { + mSignal->SignalAbort(aReason); + } else { + mReason = aReason; + } +} + +AbortController::~AbortController() { mozilla::DropJSObjects(this); } + +} // namespace mozilla::dom diff --git a/dom/abort/AbortController.h b/dom/abort/AbortController.h new file mode 100644 index 0000000000..0af33453cf --- /dev/null +++ b/dom/abort/AbortController.h @@ -0,0 +1,56 @@ +/* -*- 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/. */ + +#ifndef mozilla_dom_AbortController_h +#define mozilla_dom_AbortController_h + +#include "mozilla/dom/BindingDeclarations.h" +#include "nsCOMPtr.h" +#include "nsCycleCollectionParticipant.h" +#include "nsWrapperCache.h" + +class nsIGlobalObject; + +namespace mozilla { +class ErrorResult; + +namespace dom { + +class AbortSignal; + +class AbortController : public nsISupports, public nsWrapperCache { + public: + NS_DECL_CYCLE_COLLECTING_ISUPPORTS + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(AbortController) + + static already_AddRefed Constructor( + const GlobalObject& aGlobal, ErrorResult& aRv); + + explicit AbortController(nsIGlobalObject* aGlobal); + + JSObject* WrapObject(JSContext* aCx, + JS::Handle aGivenProto) override; + + nsIGlobalObject* GetParentObject() const; + + AbortSignal* Signal(); + + void Abort(JSContext* aCx, JS::Handle aReason); + + protected: + virtual ~AbortController(); + + nsCOMPtr mGlobal; + RefPtr mSignal; + + bool mAborted; + JS::Heap mReason; +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_AbortController_h diff --git a/dom/abort/AbortFollower.h b/dom/abort/AbortFollower.h new file mode 100644 index 0000000000..9599960390 --- /dev/null +++ b/dom/abort/AbortFollower.h @@ -0,0 +1,90 @@ +/* -*- 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/. */ + +#ifndef mozilla_dom_AbortFollower_h +#define mozilla_dom_AbortFollower_h + +#include "jsapi.h" +#include "nsISupportsImpl.h" +#include "nsTObserverArray.h" +#include "mozilla/WeakPtr.h" + +namespace mozilla::dom { + +class AbortSignal; +class AbortSignalImpl; + +// This class must be implemented by objects who want to follow an +// AbortSignalImpl. +class AbortFollower : public nsISupports { + public: + virtual void RunAbortAlgorithm() = 0; + + // This adds strong reference to this follower on the signal, which means + // you'll need to call Unfollow() to prevent your object from living + // needlessly longer. + void Follow(AbortSignalImpl* aSignal); + + // Explicitly call this to let garbage collection happen sooner when the + // follower finished its work and cannot be aborted anymore. + void Unfollow(); + + bool IsFollowing() const; + + AbortSignalImpl* Signal() const { return mFollowingSignal; } + + protected: + virtual ~AbortFollower(); + + friend class AbortSignalImpl; + + WeakPtr mFollowingSignal; +}; + +class AbortSignalImpl : public nsISupports, public SupportsWeakPtr { + public: + explicit AbortSignalImpl(bool aAborted, JS::Handle aReason); + + bool Aborted() const; + + // Web IDL Layer + void GetReason(JSContext* aCx, JS::MutableHandle aReason); + // Helper for other DOM code + JS::Value RawReason() const; + + virtual void SignalAbort(JS::Handle aReason); + + protected: + // Subclasses of this class must call these Traverse and Unlink functions + // during corresponding cycle collection operations. + static void Traverse(AbortSignalImpl* aSignal, + nsCycleCollectionTraversalCallback& cb); + + static void Unlink(AbortSignalImpl* aSignal); + + virtual ~AbortSignalImpl() { UnlinkFollowers(); } + + JS::Heap mReason; + + private: + friend class AbortFollower; + + void MaybeAssignAbortError(JSContext* aCx); + + void UnlinkFollowers(); + + // Raw pointers. |AbortFollower::Follow| adds to this array, and + // |AbortFollower::Unfollow| (also called by the destructor) will remove + // from this array. Finally, calling |SignalAbort()| will (after running all + // abort algorithms) empty this and make all contained followers |Unfollow()|. + nsTObserverArray> mFollowers; + + bool mAborted; +}; + +} // namespace mozilla::dom + +#endif // mozilla_dom_AbortFollower_h diff --git a/dom/abort/AbortSignal.cpp b/dom/abort/AbortSignal.cpp new file mode 100644 index 0000000000..01d0ac947f --- /dev/null +++ b/dom/abort/AbortSignal.cpp @@ -0,0 +1,327 @@ +/* -*- 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/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 "mozilla/RefPtr.h" +#include "nsCycleCollectionParticipant.h" +#include "nsPIDOMWindow.h" + +namespace mozilla::dom { + +// AbortSignalImpl +// ---------------------------------------------------------------------------- + +AbortSignalImpl::AbortSignalImpl(bool aAborted, JS::Handle aReason) + : mReason(aReason), mAborted(aAborted) { + MOZ_ASSERT_IF(!mReason.isUndefined(), mAborted); +} + +bool AbortSignalImpl::Aborted() const { return mAborted; } + +void AbortSignalImpl::GetReason(JSContext* aCx, + JS::MutableHandle aReason) { + if (!mAborted) { + return; + } + MaybeAssignAbortError(aCx); + aReason.set(mReason); +} + +JS::Value AbortSignalImpl::RawReason() const { return mReason.get(); } + +// https://dom.spec.whatwg.org/#abortsignal-signal-abort steps 1-4 +void AbortSignalImpl::SignalAbort(JS::Handle aReason) { + // Step 1. + if (mAborted) { + return; + } + + // Step 2. + mAborted = true; + mReason = aReason; + + // 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& follower : mFollowers.ForwardRange()) { + MOZ_ASSERT(follower->mFollowingSignal == this); + follower->RunAbortAlgorithm(); + } + + // Step 4. + UnlinkFollowers(); +} + +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(mAborted); + if (!mReason.isUndefined()) { + return; + } + + JS::Rooted exception(aCx); + RefPtr 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& 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(tmp), cb); +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END + +NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(AbortSignal, + DOMEventTargetHelper) + AbortSignalImpl::Unlink(static_cast(tmp)); +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) + +AbortSignal::AbortSignal(nsIGlobalObject* aGlobalObject, bool aAborted, + JS::Handle aReason) + : DOMEventTargetHelper(aGlobalObject), AbortSignalImpl(aAborted, aReason) { + mozilla::HoldJSObjects(this); +} + +JSObject* AbortSignal::WrapObject(JSContext* aCx, + JS::Handle aGivenProto) { + return AbortSignal_Binding::Wrap(aCx, this, aGivenProto); +} + +already_AddRefed AbortSignal::Abort( + GlobalObject& aGlobal, JS::Handle aReason) { + nsCOMPtr global = do_QueryInterface(aGlobal.GetAsSupports()); + + RefPtr abortSignal = new AbortSignal(global, true, 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 exception(jsapi.cx()); + RefPtr 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 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 innerWindow = + do_QueryInterface(aGlobal.GetAsSupports()); + if (!innerWindow) { + aRv.ThrowInvalidStateError("Could not find window."); + return; + } + + int32_t handle; + nsresult rv = innerWindow->TimeoutManager().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::Timeout(GlobalObject& aGlobal, + uint64_t aMilliseconds, + ErrorResult& aRv) { + // Step 2. Let global be signal’s relevant global object. + nsCOMPtr global = do_QueryInterface(aGlobal.GetAsSupports()); + + // Step 1. Let signal be a new AbortSignal object. + RefPtr signal = + new AbortSignal(global, false, JS::UndefinedHandleValue); + + // Step 3. Run steps after a timeout given global, "AbortSignal-timeout", + // milliseconds, and the following step: ... + RefPtr handler = + new AbortSignalTimeoutHandler(aGlobal.Context(), signal); + + // Note: We only supports int32_t range intervals + int32_t timeout = + aMilliseconds > uint64_t(std::numeric_limits::max()) + ? std::numeric_limits::max() + : static_cast(aMilliseconds); + + SetTimeoutForGlobal(aGlobal, *handler, timeout, aRv); + if (aRv.Failed()) { + return nullptr; + } + + // Step 4. Return signal. + return signal.forget(); +} + +// https://dom.spec.whatwg.org/#dom-abortsignal-throwifaborted +void AbortSignal::ThrowIfAborted(JSContext* aCx, ErrorResult& aRv) { + aRv.MightThrowJSException(); + + if (Aborted()) { + JS::Rooted reason(aCx); + GetReason(aCx, &reason); + aRv.ThrowJSException(aCx, reason); + } +} + +// https://dom.spec.whatwg.org/#abortsignal-signal-abort +void AbortSignal::SignalAbort(JS::Handle aReason) { + // Step 1, in case "signal abort" algorithm is called directly + if (Aborted()) { + return; + } + + // Steps 1-4. + AbortSignalImpl::SignalAbort(aReason); + + // Step 5. + EventInit init; + init.mBubbles = false; + init.mCancelable = false; + + RefPtr event = Event::Constructor(this, u"abort"_ns, init); + event->SetTrusted(true); + + DispatchEvent(*event); +} + +void AbortSignal::RunAbortAlgorithm() { + JS::Rooted reason(RootingCx(), Signal()->RawReason()); + SignalAbort(reason); +} + +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->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; } + +} // namespace mozilla::dom diff --git a/dom/abort/AbortSignal.h b/dom/abort/AbortSignal.h new file mode 100644 index 0000000000..45c0390477 --- /dev/null +++ b/dom/abort/AbortSignal.h @@ -0,0 +1,66 @@ +/* -*- 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/. */ + +#ifndef mozilla_dom_AbortSignal_h +#define mozilla_dom_AbortSignal_h + +#include "mozilla/dom/AbortFollower.h" +#include "mozilla/DOMEventTargetHelper.h" + +namespace mozilla::dom { + +// AbortSignal the spec concept includes the concept of a child signal +// "following" a parent signal -- internally, adding abort steps to the parent +// signal that will then signal abort on the child signal -- to propagate +// signaling abort from one signal to another. See +// . +// +// This requires that AbortSignal also inherit from AbortFollower. +// +// This ability to follow isn't directly exposed in the DOM; as of this writing +// it appears only to be used internally in the Fetch API. It might be a good +// idea to split AbortSignal into an implementation that can follow, and an +// implementation that can't, to provide this complexity only when it's needed. +class AbortSignal : public DOMEventTargetHelper, + public AbortSignalImpl, + public AbortFollower { + public: + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(AbortSignal, + DOMEventTargetHelper) + + AbortSignal(nsIGlobalObject* aGlobalObject, bool aAborted, + JS::Handle aReason); + + JSObject* WrapObject(JSContext* aCx, + JS::Handle aGivenProto) override; + + IMPL_EVENT_HANDLER(abort); + + static already_AddRefed Abort(GlobalObject& aGlobal, + JS::Handle aReason); + + static already_AddRefed Timeout(GlobalObject& aGlobal, + uint64_t aMilliseconds, + ErrorResult& aRv); + + void ThrowIfAborted(JSContext* aCx, ErrorResult& aRv); + + // AbortSignalImpl + void SignalAbort(JS::Handle aReason) override; + + // AbortFollower + void RunAbortAlgorithm() override; + + virtual bool IsTaskSignal() const { return false; } + + protected: + ~AbortSignal(); +}; + +} // namespace mozilla::dom + +#endif // mozilla_dom_AbortSignal_h diff --git a/dom/abort/moz.build b/dom/abort/moz.build new file mode 100644 index 0000000000..eb5b25584a --- /dev/null +++ b/dom/abort/moz.build @@ -0,0 +1,25 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +with Files("**"): + BUG_COMPONENT = ("Core", "DOM: Core & HTML") + +TEST_DIRS += ["tests"] + +EXPORTS.mozilla.dom += [ + "AbortController.h", + "AbortFollower.h", + "AbortSignal.h", +] + +UNIFIED_SOURCES += [ + "AbortController.cpp", + "AbortSignal.cpp", +] + +include("/ipc/chromium/chromium-config.mozbuild") + +FINAL_LIBRARY = "xul" diff --git a/dom/abort/tests/mochitest.ini b/dom/abort/tests/mochitest.ini new file mode 100644 index 0000000000..2d708e8aa2 --- /dev/null +++ b/dom/abort/tests/mochitest.ini @@ -0,0 +1,10 @@ +[DEFAULT] +support-files = + worker_abort_controller_fetch.js + slow.sjs + !/dom/events/test/event_leak_utils.js + +[test_abort_controller.html] +[test_abort_controller_fetch.html] +[test_event_listener_leaks.html] +skip-if = (os == "win" && processor == "aarch64") #bug 1535784 diff --git a/dom/abort/tests/moz.build b/dom/abort/tests/moz.build new file mode 100644 index 0000000000..af8d7033c8 --- /dev/null +++ b/dom/abort/tests/moz.build @@ -0,0 +1,9 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +MOCHITEST_MANIFESTS += ["mochitest.ini"] + +XPCSHELL_TESTS_MANIFESTS += ["unit/xpcshell.ini"] diff --git a/dom/abort/tests/slow.sjs b/dom/abort/tests/slow.sjs new file mode 100644 index 0000000000..33a9c76b81 --- /dev/null +++ b/dom/abort/tests/slow.sjs @@ -0,0 +1,13 @@ +function handleRequest(request, response) { + response.processAsync(); + + let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); + timer.init( + function () { + response.write("Here the content. But slowly."); + response.finish(); + }, + 1000, + Ci.nsITimer.TYPE_ONE_SHOT + ); +} diff --git a/dom/abort/tests/test_abort_controller.html b/dom/abort/tests/test_abort_controller.html new file mode 100644 index 0000000000..a7181711d5 --- /dev/null +++ b/dom/abort/tests/test_abort_controller.html @@ -0,0 +1,73 @@ + + + + + Test AbortController + + + + + + + diff --git a/dom/abort/tests/test_abort_controller_fetch.html b/dom/abort/tests/test_abort_controller_fetch.html new file mode 100644 index 0000000000..2342ecda89 --- /dev/null +++ b/dom/abort/tests/test_abort_controller_fetch.html @@ -0,0 +1,80 @@ + + + + + Test AbortController in Fetch API + + + + + + + diff --git a/dom/abort/tests/test_event_listener_leaks.html b/dom/abort/tests/test_event_listener_leaks.html new file mode 100644 index 0000000000..f9684e2309 --- /dev/null +++ b/dom/abort/tests/test_event_listener_leaks.html @@ -0,0 +1,43 @@ + + + + + Bug 1450271 - Test AbortSignal event listener leak conditions + + + + + +

+ + + + diff --git a/dom/abort/tests/unit/test_abort.js b/dom/abort/tests/unit/test_abort.js new file mode 100644 index 0000000000..c1586443cb --- /dev/null +++ b/dom/abort/tests/unit/test_abort.js @@ -0,0 +1,8 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +function run_test() { + let ac = new AbortController(); + Assert.ok(ac instanceof AbortController); + Assert.ok(ac.signal instanceof AbortSignal); +} diff --git a/dom/abort/tests/unit/xpcshell.ini b/dom/abort/tests/unit/xpcshell.ini new file mode 100644 index 0000000000..219daa771a --- /dev/null +++ b/dom/abort/tests/unit/xpcshell.ini @@ -0,0 +1,5 @@ +[DEFAULT] +head = +support-files = + +[test_abort.js] diff --git a/dom/abort/tests/worker_abort_controller_fetch.js b/dom/abort/tests/worker_abort_controller_fetch.js new file mode 100644 index 0000000000..571d9ffc7e --- /dev/null +++ b/dom/abort/tests/worker_abort_controller_fetch.js @@ -0,0 +1,33 @@ +function testWorkerAbortedFetch() { + var ac = new AbortController(); + ac.abort(); + + fetch("slow.sjs", { signal: ac.signal }).then( + () => { + postMessage(false); + }, + e => { + postMessage(e.name == "AbortError"); + } + ); +} + +function testWorkerFetchAndAbort() { + var ac = new AbortController(); + + var p = fetch("slow.sjs", { signal: ac.signal }); + ac.abort(); + + p.then( + () => { + postMessage(false); + }, + e => { + postMessage(e.name == "AbortError"); + } + ); +} + +self.onmessage = function (e) { + self[e.data](); +}; -- cgit v1.2.3