From e97a29a6569fac73e81cf46a0e682ac926ea3456 Mon Sep 17 00:00:00 2001 From: Moonchild Date: Thu, 11 Jun 2020 23:20:52 +0000 Subject: Issue #1587 - Part 7: Rename FetchController to AbortController Also renames FetchSignal to AbortSignal. Includes renaming the various controlling prefs to enable. --- dom/abort/AbortController.cpp | 127 ++++++++++++++++ dom/abort/AbortController.h | 73 ++++++++++ dom/abort/AbortSignal.cpp | 151 +++++++++++++++++++ dom/abort/AbortSignal.h | 79 ++++++++++ dom/abort/FetchController.cpp | 127 ---------------- dom/abort/FetchController.h | 73 ---------- dom/abort/FetchSignal.cpp | 151 ------------------- dom/abort/FetchSignal.h | 79 ---------- dom/abort/moz.build | 8 +- dom/abort/tests/file_abort_controller.html | 161 +++++++++++++++++++++ dom/abort/tests/file_fetch_controller.html | 161 --------------------- dom/abort/tests/mochitest.ini | 4 +- dom/abort/tests/test_abort_controller.html | 40 +++++ dom/abort/tests/test_fetch_controller.html | 40 ----- dom/abort/tests/worker_abort_controller.js | 27 ++++ dom/abort/tests/worker_fetch_controller.js | 27 ---- dom/fetch/Fetch.cpp | 52 +++---- dom/fetch/FetchDriver.cpp | 2 +- dom/fetch/FetchDriver.h | 8 +- dom/fetch/FetchObserver.cpp | 2 +- dom/fetch/FetchObserver.h | 6 +- dom/fetch/Request.h | 2 +- dom/tests/mochitest/fetch/file_fetch_observer.html | 8 +- dom/webidl/AbortController.webidl | 15 ++ dom/webidl/AbortSignal.webidl | 13 ++ dom/webidl/FetchController.webidl | 15 -- dom/webidl/FetchSignal.webidl | 13 -- dom/webidl/Request.webidl | 4 +- dom/webidl/moz.build | 4 +- dom/workers/WorkerPrefs.h | 2 +- 30 files changed, 737 insertions(+), 737 deletions(-) create mode 100644 dom/abort/AbortController.cpp create mode 100644 dom/abort/AbortController.h create mode 100644 dom/abort/AbortSignal.cpp create mode 100644 dom/abort/AbortSignal.h delete mode 100644 dom/abort/FetchController.cpp delete mode 100644 dom/abort/FetchController.h delete mode 100644 dom/abort/FetchSignal.cpp delete mode 100644 dom/abort/FetchSignal.h create mode 100644 dom/abort/tests/file_abort_controller.html delete mode 100644 dom/abort/tests/file_fetch_controller.html create mode 100644 dom/abort/tests/test_abort_controller.html delete mode 100644 dom/abort/tests/test_fetch_controller.html create mode 100644 dom/abort/tests/worker_abort_controller.js delete mode 100644 dom/abort/tests/worker_fetch_controller.js create mode 100644 dom/webidl/AbortController.webidl create mode 100644 dom/webidl/AbortSignal.webidl delete mode 100644 dom/webidl/FetchController.webidl delete mode 100644 dom/webidl/FetchSignal.webidl diff --git a/dom/abort/AbortController.cpp b/dom/abort/AbortController.cpp new file mode 100644 index 000000000..4043b7bf3 --- /dev/null +++ b/dom/abort/AbortController.cpp @@ -0,0 +1,127 @@ +/* -*- 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 "mozilla/dom/AbortControllerBinding.h" +#include "WorkerPrivate.h" + +namespace mozilla { +namespace dom { + +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(AbortController, mGlobal, mSignal, + mFollowingSignal) + +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 */ bool +AbortController::IsEnabled(JSContext* aCx, JSObject* aGlobal) +{ + if (NS_IsMainThread()) { + return Preferences::GetBool("dom.abortController.enabled", false); + } + + using namespace workers; + + // Otherwise, check the pref via the WorkerPrivate + WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx); + if (!workerPrivate) { + return false; + } + + return workerPrivate->AbortControllerEnabled(); +} + +/* 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) +{} + +JSObject* +AbortController::WrapObject(JSContext* aCx, JS::Handle aGivenProto) +{ + return AbortControllerBinding::Wrap(aCx, this, aGivenProto); +} + +nsIGlobalObject* +AbortController::GetParentObject() const +{ + return mGlobal; +} + +AbortSignal* +AbortController::Signal() +{ + if (!mSignal) { + mSignal = new AbortSignal(this, mAborted); + } + + return mSignal; +} + +void +AbortController::Abort() +{ + if (mAborted) { + return; + } + + mAborted = true; + + if (mSignal) { + mSignal->Abort(); + } +} + +void +AbortController::Follow(AbortSignal& aSignal) +{ + AbortSignal::Follower::Follow(&aSignal); +} + +void +AbortController::Unfollow(AbortSignal& aSignal) +{ + if (mFollowingSignal != &aSignal) { + return; + } + + AbortSignal::Follower::Unfollow(); +} + +AbortSignal* +AbortController::Following() const +{ + return mFollowingSignal; +} + +void +AbortController::Aborted() +{ + Abort(); +} + +} // dom namespace +} // mozilla namespace diff --git a/dom/abort/AbortController.h b/dom/abort/AbortController.h new file mode 100644 index 000000000..9ce4253c9 --- /dev/null +++ b/dom/abort/AbortController.h @@ -0,0 +1,73 @@ +/* -*- 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 "mozilla/dom/AbortSignal.h" +#include "nsCycleCollectionParticipant.h" +#include "nsWrapperCache.h" +#include "mozilla/ErrorResult.h" +#include "nsIGlobalObject.h" + +namespace mozilla { +namespace dom { + +class AbortController final : public nsISupports + , public nsWrapperCache + , public AbortSignal::Follower +{ +public: + NS_DECL_CYCLE_COLLECTING_ISUPPORTS + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(AbortController) + + static bool + IsEnabled(JSContext* aCx, JSObject* aGlobal); + + 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(); + + void + Follow(AbortSignal& aSignal); + + void + Unfollow(AbortSignal& aSignal); + + AbortSignal* + Following() const; + + // AbortSignal::Follower + + void Aborted() override; + +private: + ~AbortController() = default; + + nsCOMPtr mGlobal; + RefPtr mSignal; + + bool mAborted; +}; + +} // dom namespace +} // mozilla namespace + +#endif // mozilla_dom_AbortController_h diff --git a/dom/abort/AbortSignal.cpp b/dom/abort/AbortSignal.cpp new file mode 100644 index 000000000..38b0a2492 --- /dev/null +++ b/dom/abort/AbortSignal.cpp @@ -0,0 +1,151 @@ +/* -*- 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 "AbortController.h" +#include "mozilla/dom/Event.h" +#include "mozilla/dom/AbortSignalBinding.h" + +namespace mozilla { +namespace dom { + +NS_IMPL_CYCLE_COLLECTION_CLASS(AbortSignal) + +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(AbortSignal, + DOMEventTargetHelper) + NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mController) +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END + +NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(AbortSignal, + DOMEventTargetHelper) + NS_IMPL_CYCLE_COLLECTION_UNLINK(mController) +NS_IMPL_CYCLE_COLLECTION_UNLINK_END + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(AbortSignal) +NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper) + +NS_IMPL_ADDREF_INHERITED(AbortSignal, DOMEventTargetHelper) +NS_IMPL_RELEASE_INHERITED(AbortSignal, DOMEventTargetHelper) + +AbortSignal::AbortSignal(AbortController* aController, + bool aAborted) + : DOMEventTargetHelper(aController->GetParentObject()) + , mController(aController) + , mAborted(aAborted) +{} + +AbortSignal::AbortSignal(bool aAborted) + : mAborted(aAborted) +{} + +JSObject* +AbortSignal::WrapObject(JSContext* aCx, JS::Handle aGivenProto) +{ + return AbortSignalBinding::Wrap(aCx, this, aGivenProto); +} + +bool +AbortSignal::Aborted() const +{ + return mAborted; +} + +void +AbortSignal::Abort() +{ + MOZ_ASSERT(!mAborted); + mAborted = true; + + // Let's inform the followers. + for (uint32_t i = 0; i < mFollowers.Length(); ++i) { + mFollowers[i]->Aborted(); + } + + EventInit init; + init.mBubbles = false; + init.mCancelable = false; + + // TODO which kind of event should we dispatch here? + + RefPtr event = + Event::Constructor(this, NS_LITERAL_STRING("abort"), init); + event->SetTrusted(true); + + bool dummy; + DispatchEvent(event, &dummy); +} + +void +AbortSignal::AddFollower(AbortSignal::Follower* aFollower) +{ + MOZ_DIAGNOSTIC_ASSERT(aFollower); + if (!mFollowers.Contains(aFollower)) { + mFollowers.AppendElement(aFollower); + } +} + +void +AbortSignal::RemoveFollower(AbortSignal::Follower* aFollower) +{ + MOZ_DIAGNOSTIC_ASSERT(aFollower); + mFollowers.RemoveElement(aFollower); +} + +bool +AbortSignal::CanAcceptFollower(AbortSignal::Follower* aFollower) const +{ + MOZ_DIAGNOSTIC_ASSERT(aFollower); + + if (!mController) { + return true; + } + + if (aFollower == mController) { + return false; + } + + AbortSignal* following = mController->Following(); + if (!following) { + return true; + } + + return following->CanAcceptFollower(aFollower); +} + +// AbortSignal::Follower +// ---------------------------------------------------------------------------- + +AbortSignal::Follower::~Follower() +{ + Unfollow(); +} + +void +AbortSignal::Follower::Follow(AbortSignal* aSignal) +{ + MOZ_DIAGNOSTIC_ASSERT(aSignal); + + if (!aSignal->CanAcceptFollower(this)) { + return; + } + + Unfollow(); + + mFollowingSignal = aSignal; + aSignal->AddFollower(this); +} + +void +AbortSignal::Follower::Unfollow() +{ + if (mFollowingSignal) { + mFollowingSignal->RemoveFollower(this); + mFollowingSignal = nullptr; + } +} + +} // dom namespace +} // mozilla namespace diff --git a/dom/abort/AbortSignal.h b/dom/abort/AbortSignal.h new file mode 100644 index 000000000..c5045dbd6 --- /dev/null +++ b/dom/abort/AbortSignal.h @@ -0,0 +1,79 @@ +/* -*- 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/DOMEventTargetHelper.h" + +namespace mozilla { +namespace dom { + +class AbortController; +class AbortSignal; + +class AbortSignal final : public DOMEventTargetHelper +{ +public: + // This class must be implemented by objects who want to follow a AbortSignal. + class Follower + { + public: + virtual void Aborted() = 0; + + protected: + virtual ~Follower(); + + void + Follow(AbortSignal* aSignal); + + void + Unfollow(); + + RefPtr mFollowingSignal; + }; + + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(AbortSignal, DOMEventTargetHelper) + + AbortSignal(AbortController* aController, bool aAborted); + explicit AbortSignal(bool aAborted); + + JSObject* + WrapObject(JSContext* aCx, JS::Handle aGivenProto) override; + + bool + Aborted() const; + + void + Abort(); + + IMPL_EVENT_HANDLER(abort); + + void + AddFollower(Follower* aFollower); + + void + RemoveFollower(Follower* aFollower); + + bool + CanAcceptFollower(Follower* aFollower) const; + +private: + ~AbortSignal() = default; + + RefPtr mController; + + // Raw pointers. Follower unregisters itself in the DTOR. + nsTArray mFollowers; + + bool mAborted; +}; + +} // dom namespace +} // mozilla namespace + +#endif // mozilla_dom_AbortSignal_h diff --git a/dom/abort/FetchController.cpp b/dom/abort/FetchController.cpp deleted file mode 100644 index 2eb40b980..000000000 --- a/dom/abort/FetchController.cpp +++ /dev/null @@ -1,127 +0,0 @@ -/* -*- 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 "FetchController.h" -#include "FetchSignal.h" -#include "mozilla/dom/FetchControllerBinding.h" -#include "WorkerPrivate.h" - -namespace mozilla { -namespace dom { - -NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(FetchController, mGlobal, mSignal, - mFollowingSignal) - -NS_IMPL_CYCLE_COLLECTING_ADDREF(FetchController) -NS_IMPL_CYCLE_COLLECTING_RELEASE(FetchController) - -NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FetchController) - NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY - NS_INTERFACE_MAP_ENTRY(nsISupports) -NS_INTERFACE_MAP_END - -/* static */ bool -FetchController::IsEnabled(JSContext* aCx, JSObject* aGlobal) -{ - if (NS_IsMainThread()) { - return Preferences::GetBool("dom.fetchController.enabled", false); - } - - using namespace workers; - - // Otherwise, check the pref via the WorkerPrivate - WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx); - if (!workerPrivate) { - return false; - } - - return workerPrivate->FetchControllerEnabled(); -} - -/* static */ already_AddRefed -FetchController::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv) -{ - nsCOMPtr global = do_QueryInterface(aGlobal.GetAsSupports()); - if (!global) { - aRv.Throw(NS_ERROR_FAILURE); - return nullptr; - } - - RefPtr fetchController = new FetchController(global); - return fetchController.forget(); -} - -FetchController::FetchController(nsIGlobalObject* aGlobal) - : mGlobal(aGlobal) - , mAborted(false) -{} - -JSObject* -FetchController::WrapObject(JSContext* aCx, JS::Handle aGivenProto) -{ - return FetchControllerBinding::Wrap(aCx, this, aGivenProto); -} - -nsIGlobalObject* -FetchController::GetParentObject() const -{ - return mGlobal; -} - -FetchSignal* -FetchController::Signal() -{ - if (!mSignal) { - mSignal = new FetchSignal(this, mAborted); - } - - return mSignal; -} - -void -FetchController::Abort() -{ - if (mAborted) { - return; - } - - mAborted = true; - - if (mSignal) { - mSignal->Abort(); - } -} - -void -FetchController::Follow(FetchSignal& aSignal) -{ - FetchSignal::Follower::Follow(&aSignal); -} - -void -FetchController::Unfollow(FetchSignal& aSignal) -{ - if (mFollowingSignal != &aSignal) { - return; - } - - FetchSignal::Follower::Unfollow(); -} - -FetchSignal* -FetchController::Following() const -{ - return mFollowingSignal; -} - -void -FetchController::Aborted() -{ - Abort(); -} - -} // dom namespace -} // mozilla namespace diff --git a/dom/abort/FetchController.h b/dom/abort/FetchController.h deleted file mode 100644 index 7a0132dca..000000000 --- a/dom/abort/FetchController.h +++ /dev/null @@ -1,73 +0,0 @@ -/* -*- 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_FetchController_h -#define mozilla_dom_FetchController_h - -#include "mozilla/dom/BindingDeclarations.h" -#include "mozilla/dom/FetchSignal.h" -#include "nsCycleCollectionParticipant.h" -#include "nsWrapperCache.h" -#include "mozilla/ErrorResult.h" -#include "nsIGlobalObject.h" - -namespace mozilla { -namespace dom { - -class FetchController final : public nsISupports - , public nsWrapperCache - , public FetchSignal::Follower -{ -public: - NS_DECL_CYCLE_COLLECTING_ISUPPORTS - NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(FetchController) - - static bool - IsEnabled(JSContext* aCx, JSObject* aGlobal); - - static already_AddRefed - Constructor(const GlobalObject& aGlobal, ErrorResult& aRv); - - explicit FetchController(nsIGlobalObject* aGlobal); - - JSObject* - WrapObject(JSContext* aCx, JS::Handle aGivenProto) override; - - nsIGlobalObject* - GetParentObject() const; - - FetchSignal* - Signal(); - - void - Abort(); - - void - Follow(FetchSignal& aSignal); - - void - Unfollow(FetchSignal& aSignal); - - FetchSignal* - Following() const; - - // FetchSignal::Follower - - void Aborted() override; - -private: - ~FetchController() = default; - - nsCOMPtr mGlobal; - RefPtr mSignal; - - bool mAborted; -}; - -} // dom namespace -} // mozilla namespace - -#endif // mozilla_dom_FetchController_h diff --git a/dom/abort/FetchSignal.cpp b/dom/abort/FetchSignal.cpp deleted file mode 100644 index 07ad6b53d..000000000 --- a/dom/abort/FetchSignal.cpp +++ /dev/null @@ -1,151 +0,0 @@ -/* -*- 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 "FetchSignal.h" -#include "FetchController.h" -#include "mozilla/dom/Event.h" -#include "mozilla/dom/FetchSignalBinding.h" - -namespace mozilla { -namespace dom { - -NS_IMPL_CYCLE_COLLECTION_CLASS(FetchSignal) - -NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(FetchSignal, - DOMEventTargetHelper) - NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mController) -NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END - -NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(FetchSignal, - DOMEventTargetHelper) - NS_IMPL_CYCLE_COLLECTION_UNLINK(mController) -NS_IMPL_CYCLE_COLLECTION_UNLINK_END - -NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FetchSignal) -NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper) - -NS_IMPL_ADDREF_INHERITED(FetchSignal, DOMEventTargetHelper) -NS_IMPL_RELEASE_INHERITED(FetchSignal, DOMEventTargetHelper) - -FetchSignal::FetchSignal(FetchController* aController, - bool aAborted) - : DOMEventTargetHelper(aController->GetParentObject()) - , mController(aController) - , mAborted(aAborted) -{} - -FetchSignal::FetchSignal(bool aAborted) - : mAborted(aAborted) -{} - -JSObject* -FetchSignal::WrapObject(JSContext* aCx, JS::Handle aGivenProto) -{ - return FetchSignalBinding::Wrap(aCx, this, aGivenProto); -} - -bool -FetchSignal::Aborted() const -{ - return mAborted; -} - -void -FetchSignal::Abort() -{ - MOZ_ASSERT(!mAborted); - mAborted = true; - - // Let's inform the followers. - for (uint32_t i = 0; i < mFollowers.Length(); ++i) { - mFollowers[i]->Aborted(); - } - - EventInit init; - init.mBubbles = false; - init.mCancelable = false; - - // TODO which kind of event should we dispatch here? - - RefPtr event = - Event::Constructor(this, NS_LITERAL_STRING("abort"), init); - event->SetTrusted(true); - - bool dummy; - DispatchEvent(event, &dummy); -} - -void -FetchSignal::AddFollower(FetchSignal::Follower* aFollower) -{ - MOZ_DIAGNOSTIC_ASSERT(aFollower); - if (!mFollowers.Contains(aFollower)) { - mFollowers.AppendElement(aFollower); - } -} - -void -FetchSignal::RemoveFollower(FetchSignal::Follower* aFollower) -{ - MOZ_DIAGNOSTIC_ASSERT(aFollower); - mFollowers.RemoveElement(aFollower); -} - -bool -FetchSignal::CanAcceptFollower(FetchSignal::Follower* aFollower) const -{ - MOZ_DIAGNOSTIC_ASSERT(aFollower); - - if (!mController) { - return true; - } - - if (aFollower == mController) { - return false; - } - - FetchSignal* following = mController->Following(); - if (!following) { - return true; - } - - return following->CanAcceptFollower(aFollower); -} - -// FetchSignal::Follower -// ---------------------------------------------------------------------------- - -FetchSignal::Follower::~Follower() -{ - Unfollow(); -} - -void -FetchSignal::Follower::Follow(FetchSignal* aSignal) -{ - MOZ_DIAGNOSTIC_ASSERT(aSignal); - - if (!aSignal->CanAcceptFollower(this)) { - return; - } - - Unfollow(); - - mFollowingSignal = aSignal; - aSignal->AddFollower(this); -} - -void -FetchSignal::Follower::Unfollow() -{ - if (mFollowingSignal) { - mFollowingSignal->RemoveFollower(this); - mFollowingSignal = nullptr; - } -} - -} // dom namespace -} // mozilla namespace diff --git a/dom/abort/FetchSignal.h b/dom/abort/FetchSignal.h deleted file mode 100644 index 4970f03de..000000000 --- a/dom/abort/FetchSignal.h +++ /dev/null @@ -1,79 +0,0 @@ -/* -*- 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_FetchSignal_h -#define mozilla_dom_FetchSignal_h - -#include "mozilla/DOMEventTargetHelper.h" - -namespace mozilla { -namespace dom { - -class FetchController; -class FetchSignal; - -class FetchSignal final : public DOMEventTargetHelper -{ -public: - // This class must be implemented by objects who want to follow a FetchSignal. - class Follower - { - public: - virtual void Aborted() = 0; - - protected: - virtual ~Follower(); - - void - Follow(FetchSignal* aSignal); - - void - Unfollow(); - - RefPtr mFollowingSignal; - }; - - NS_DECL_ISUPPORTS_INHERITED - NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(FetchSignal, DOMEventTargetHelper) - - FetchSignal(FetchController* aController, bool aAborted); - explicit FetchSignal(bool aAborted); - - JSObject* - WrapObject(JSContext* aCx, JS::Handle aGivenProto) override; - - bool - Aborted() const; - - void - Abort(); - - IMPL_EVENT_HANDLER(abort); - - void - AddFollower(Follower* aFollower); - - void - RemoveFollower(Follower* aFollower); - - bool - CanAcceptFollower(Follower* aFollower) const; - -private: - ~FetchSignal() = default; - - RefPtr mController; - - // Raw pointers. Follower unregisters itself in the DTOR. - nsTArray mFollowers; - - bool mAborted; -}; - -} // dom namespace -} // mozilla namespace - -#endif // mozilla_dom_FetchSignal_h diff --git a/dom/abort/moz.build b/dom/abort/moz.build index e7d8146e5..cb48ee15f 100644 --- a/dom/abort/moz.build +++ b/dom/abort/moz.build @@ -10,13 +10,13 @@ with Files("**"): TEST_DIRS += ['tests'] EXPORTS.mozilla.dom += [ - 'FetchController.h', - 'FetchSignal.h', + 'AbortController.h', + 'AbortSignal.h', ] UNIFIED_SOURCES += [ - 'FetchController.cpp', - 'FetchSignal.cpp', + 'AbortController.cpp', + 'AbortSignal.cpp', ] LOCAL_INCLUDES += [ diff --git a/dom/abort/tests/file_abort_controller.html b/dom/abort/tests/file_abort_controller.html new file mode 100644 index 000000000..e4137aac9 --- /dev/null +++ b/dom/abort/tests/file_abort_controller.html @@ -0,0 +1,161 @@ + diff --git a/dom/abort/tests/file_fetch_controller.html b/dom/abort/tests/file_fetch_controller.html deleted file mode 100644 index e4137aac9..000000000 --- a/dom/abort/tests/file_fetch_controller.html +++ /dev/null @@ -1,161 +0,0 @@ - diff --git a/dom/abort/tests/mochitest.ini b/dom/abort/tests/mochitest.ini index 5ecc7048e..c8cc95fda 100644 --- a/dom/abort/tests/mochitest.ini +++ b/dom/abort/tests/mochitest.ini @@ -1,6 +1,6 @@ [DEFAULT] support-files = - file_fetch_controller.html + file_abort_controller.html worker_fetch_controller.js -[test_fetch_controller.html] +[test_abort_controller.html] diff --git a/dom/abort/tests/test_abort_controller.html b/dom/abort/tests/test_abort_controller.html new file mode 100644 index 000000000..812fb9161 --- /dev/null +++ b/dom/abort/tests/test_abort_controller.html @@ -0,0 +1,40 @@ + + + + + Test FetchController + + + + + + + + diff --git a/dom/abort/tests/test_fetch_controller.html b/dom/abort/tests/test_fetch_controller.html deleted file mode 100644 index 812fb9161..000000000 --- a/dom/abort/tests/test_fetch_controller.html +++ /dev/null @@ -1,40 +0,0 @@ - - - - - Test FetchController - - - - - - - - diff --git a/dom/abort/tests/worker_abort_controller.js b/dom/abort/tests/worker_abort_controller.js new file mode 100644 index 000000000..6fd1c7888 --- /dev/null +++ b/dom/abort/tests/worker_abort_controller.js @@ -0,0 +1,27 @@ +function testWorkerAbortedFetch() { + var fc = new AbortController(); + fc.abort(); + + fetch('slow.sjs', { signal: fc.signal }).then(() => { + postMessage(false); + }, e => { + postMessage(e.name == "AbortError"); + }); +} + +function testWorkerFetchAndAbort() { + var fc = new AbortController(); + + var p = fetch('slow.sjs', { signal: fc.signal }); + fc.abort(); + + p.then(() => { + postMessage(false); + }, e => { + postMessage(e.name == "AbortError"); + }); +} + +onmessage = function(e) { + self[e.data](); +} diff --git a/dom/abort/tests/worker_fetch_controller.js b/dom/abort/tests/worker_fetch_controller.js deleted file mode 100644 index 6b008fea8..000000000 --- a/dom/abort/tests/worker_fetch_controller.js +++ /dev/null @@ -1,27 +0,0 @@ -function testWorkerAbortedFetch() { - var fc = new FetchController(); - fc.abort(); - - fetch('slow.sjs', { signal: fc.signal }).then(() => { - postMessage(false); - }, e => { - postMessage(e.name == "AbortError"); - }); -} - -function testWorkerFetchAndAbort() { - var fc = new FetchController(); - - var p = fetch('slow.sjs', { signal: fc.signal }); - fc.abort(); - - p.then(() => { - postMessage(false); - }, e => { - postMessage(e.name == "AbortError"); - }); -} - -onmessage = function(e) { - self[e.data](); -} diff --git a/dom/fetch/Fetch.cpp b/dom/fetch/Fetch.cpp index 04aa7fd91..4dbe2de0a 100644 --- a/dom/fetch/Fetch.cpp +++ b/dom/fetch/Fetch.cpp @@ -53,24 +53,24 @@ namespace dom { using namespace workers; -// This class helps the proxying of FetchSignal changes cross threads. -class FetchSignalProxy final : public FetchSignal::Follower +// This class helps the proxying of AbortSignal changes cross threads. +class AbortSignalProxy final : public AbortSignal::Follower { // This is created and released on the main-thread. - RefPtr mSignalMainThread; + RefPtr mSignalMainThread; - // This value is used only for the creation of FetchSignal on the + // This value is used only for the creation of AbortSignal on the // main-thread. They are not updated. const bool mAborted; - // This runnable propagates changes from the FetchSignal on workers to the - // FetchSignal on main-thread. - class FetchSignalProxyRunnable final : public Runnable + // This runnable propagates changes from the AbortSignal on workers to the + // AbortSignal on main-thread. + class AbortSignalProxyRunnable final : public Runnable { - RefPtr mProxy; + RefPtr mProxy; public: - explicit FetchSignalProxyRunnable(FetchSignalProxy* aProxy) + explicit AbortSignalProxyRunnable(AbortSignalProxy* aProxy) : mProxy(aProxy) {} @@ -78,16 +78,16 @@ class FetchSignalProxy final : public FetchSignal::Follower Run() override { MOZ_ASSERT(NS_IsMainThread()); - FetchSignal* signal = mProxy->GetOrCreateSignalForMainThread(); + AbortSignal* signal = mProxy->GetOrCreateSignalForMainThread(); signal->Abort(); return NS_OK; } }; public: - NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FetchSignalProxy) + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AbortSignalProxy) - explicit FetchSignalProxy(FetchSignal* aSignal) + explicit AbortSignalProxy(AbortSignal* aSignal) : mAborted(aSignal->Aborted()) { Follow(aSignal); @@ -96,17 +96,17 @@ public: void Aborted() override { - RefPtr runnable = - new FetchSignalProxyRunnable(this); + RefPtr runnable = + new AbortSignalProxyRunnable(this); NS_DispatchToMainThread(runnable); } - FetchSignal* + AbortSignal* GetOrCreateSignalForMainThread() { MOZ_ASSERT(NS_IsMainThread()); if (!mSignalMainThread) { - mSignalMainThread = new FetchSignal(mAborted); + mSignalMainThread = new AbortSignal(mAborted); } return mSignalMainThread; } @@ -118,7 +118,7 @@ public: } private: - ~FetchSignalProxy() + ~AbortSignalProxy() { NS_ReleaseOnMainThread(mSignalMainThread.forget()); } @@ -133,14 +133,14 @@ class WorkerFetchResolver final : public FetchDriverObserver friend class WorkerFetchResponseRunnable; RefPtr mPromiseProxy; - RefPtr mSignalProxy; + RefPtr mSignalProxy; RefPtr mFetchObserver; public: // Returns null if worker is shutting down. static already_AddRefed Create(workers::WorkerPrivate* aWorkerPrivate, Promise* aPromise, - FetchSignal* aSignal, FetchObserver* aObserver) + AbortSignal* aSignal, FetchObserver* aObserver) { MOZ_ASSERT(aWorkerPrivate); aWorkerPrivate->AssertIsOnWorkerThread(); @@ -150,9 +150,9 @@ public: return nullptr; } - RefPtr signalProxy; + RefPtr signalProxy; if (aSignal) { - signalProxy = new FetchSignalProxy(aSignal); + signalProxy = new AbortSignalProxy(aSignal); } RefPtr r = @@ -160,8 +160,8 @@ public: return r.forget(); } - FetchSignal* - GetFetchSignal() + AbortSignal* + GetAbortSignal() { MOZ_ASSERT(NS_IsMainThread()); @@ -183,7 +183,7 @@ public: private: WorkerFetchResolver(PromiseWorkerProxy* aProxy, - FetchSignalProxy* aSignalProxy, + AbortSignalProxy* aSignalProxy, FetchObserver* aObserver) : mPromiseProxy(aProxy) , mSignalProxy(aSignalProxy) @@ -287,7 +287,7 @@ public: fetch->SetWorkerScript(spec); } - RefPtr signal = mResolver->GetFetchSignal(); + RefPtr signal = mResolver->GetAbortSignal(); // ...but release it before calling Fetch, because mResolver's callback can // be called synchronously and they want the mutex, too. @@ -329,7 +329,7 @@ FetchRequest(nsIGlobalObject* aGlobal, const RequestOrUSVString& aInput, RefPtr r = request->GetInternalRequest(); - RefPtr signal; + RefPtr signal; if (aInit.mSignal.WasPassed()) { signal = &aInit.mSignal.Value(); // Let's FetchDriver to deal with an already aborted signal. diff --git a/dom/fetch/FetchDriver.cpp b/dom/fetch/FetchDriver.cpp index e8d726ce1..067e32db4 100644 --- a/dom/fetch/FetchDriver.cpp +++ b/dom/fetch/FetchDriver.cpp @@ -67,7 +67,7 @@ FetchDriver::~FetchDriver() } nsresult -FetchDriver::Fetch(FetchSignal* aSignal, FetchDriverObserver* aObserver) +FetchDriver::Fetch(AbortSignal* aSignal, FetchDriverObserver* aObserver) { workers::AssertIsOnMainThread(); #ifdef DEBUG diff --git a/dom/fetch/FetchDriver.h b/dom/fetch/FetchDriver.h index e942aa242..57dffa5a7 100644 --- a/dom/fetch/FetchDriver.h +++ b/dom/fetch/FetchDriver.h @@ -12,7 +12,7 @@ #include "nsIStreamListener.h" #include "nsIThreadRetargetableStreamListener.h" #include "mozilla/ConsoleReportCollector.h" -#include "mozilla/dom/FetchSignal.h" +#include "mozilla/dom/AbortSignal.h" #include "mozilla/dom/SRIMetadata.h" #include "mozilla/RefPtr.h" @@ -84,7 +84,7 @@ class FetchDriver final : public nsIStreamListener, public nsIChannelEventSink, public nsIInterfaceRequestor, public nsIThreadRetargetableStreamListener, - public FetchSignal::Follower + public AbortSignal::Follower { public: NS_DECL_ISUPPORTS @@ -98,7 +98,7 @@ public: nsIPrincipal* aPrincipal, nsILoadGroup* aLoadGroup); - nsresult Fetch(FetchSignal* aSignal, + nsresult Fetch(AbortSignal* aSignal, FetchDriverObserver* aObserver); void @@ -111,7 +111,7 @@ public: mWorkerScript = aWorkerScirpt; } - // FetchSignal::Follower + // AbortSignal::Follower void Aborted() override; diff --git a/dom/fetch/FetchObserver.cpp b/dom/fetch/FetchObserver.cpp index 982f0ad49..93d93773f 100644 --- a/dom/fetch/FetchObserver.cpp +++ b/dom/fetch/FetchObserver.cpp @@ -46,7 +46,7 @@ FetchObserver::IsEnabled(JSContext* aCx, JSObject* aGlobal) } FetchObserver::FetchObserver(nsIGlobalObject* aGlobal, - FetchSignal* aSignal) + AbortSignal* aSignal) : DOMEventTargetHelper(aGlobal) , mState(FetchState::Requesting) { diff --git a/dom/fetch/FetchObserver.h b/dom/fetch/FetchObserver.h index 45adf2ba1..5cd835b3d 100644 --- a/dom/fetch/FetchObserver.h +++ b/dom/fetch/FetchObserver.h @@ -9,13 +9,13 @@ #include "mozilla/DOMEventTargetHelper.h" #include "mozilla/dom/FetchObserverBinding.h" -#include "mozilla/dom/FetchSignal.h" +#include "mozilla/dom/AbortSignal.h" namespace mozilla { namespace dom { class FetchObserver final : public DOMEventTargetHelper - , public FetchSignal::Follower + , public AbortSignal::Follower { public: NS_DECL_ISUPPORTS_INHERITED @@ -24,7 +24,7 @@ public: static bool IsEnabled(JSContext* aCx, JSObject* aGlobal); - FetchObserver(nsIGlobalObject* aGlobal, FetchSignal* aSignal); + FetchObserver(nsIGlobalObject* aGlobal, AbortSignal* aSignal); JSObject* WrapObject(JSContext* aCx, JS::Handle aGivenProto) override; diff --git a/dom/fetch/Request.h b/dom/fetch/Request.h index 56a75e5af..f6fe9be7b 100644 --- a/dom/fetch/Request.h +++ b/dom/fetch/Request.h @@ -12,7 +12,7 @@ #include "nsWrapperCache.h" #include "mozilla/dom/Fetch.h" -#include "mozilla/dom/FetchSignal.h" +#include "mozilla/dom/AbortSignal.h" #include "mozilla/dom/InternalRequest.h" // Required here due to certain WebIDL enums/classes being declared in both // files. diff --git a/dom/tests/mochitest/fetch/file_fetch_observer.html b/dom/tests/mochitest/fetch/file_fetch_observer.html index a172a18dc..668e609ec 100644 --- a/dom/tests/mochitest/fetch/file_fetch_observer.html +++ b/dom/tests/mochitest/fetch/file_fetch_observer.html @@ -19,7 +19,7 @@ function testObserver() { } function testObserveAbort() { - var fc = new FetchController(); + var fc = new AbortController(); fetch('http://mochi.test:8888/tests/dom/tests/mochitest/fetch/slow.sjs', { signal: fc.signal, @@ -37,7 +37,7 @@ function testObserveAbort() { } function testObserveComplete() { - var fc = new FetchController(); + var fc = new AbortController(); fetch('http://mochi.test:8888/tests/dom/tests/mochitest/fetch/slow.sjs', { signal: fc.signal, @@ -54,7 +54,7 @@ function testObserveComplete() { } function testObserveErrored() { - var fc = new FetchController(); + var fc = new AbortController(); fetch('foo: bar', { signal: fc.signal, @@ -71,7 +71,7 @@ function testObserveErrored() { } function testObserveResponding() { - var fc = new FetchController(); + var fc = new AbortController(); fetch('http://mochi.test:8888/tests/dom/tests/mochitest/fetch/slow.sjs', { signal: fc.signal, diff --git a/dom/webidl/AbortController.webidl b/dom/webidl/AbortController.webidl new file mode 100644 index 000000000..f5d8f317b --- /dev/null +++ b/dom/webidl/AbortController.webidl @@ -0,0 +1,15 @@ +/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. + */ + +[Constructor(), Exposed=(Window,Worker), + Func="AbortController::IsEnabled"] +interface AbortController { + readonly attribute AbortSignal signal; + + void abort(); + void follow(AbortSignal signal); + void unfollow(AbortSignal signal); +}; diff --git a/dom/webidl/AbortSignal.webidl b/dom/webidl/AbortSignal.webidl new file mode 100644 index 000000000..b4b03bb7e --- /dev/null +++ b/dom/webidl/AbortSignal.webidl @@ -0,0 +1,13 @@ +/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. + */ + +[Exposed=(Window,Worker), + Func="AbortController::IsEnabled"] +interface AbortSignal : EventTarget { + readonly attribute boolean aborted; + + attribute EventHandler onabort; +}; diff --git a/dom/webidl/FetchController.webidl b/dom/webidl/FetchController.webidl deleted file mode 100644 index c5b1cc6da..000000000 --- a/dom/webidl/FetchController.webidl +++ /dev/null @@ -1,15 +0,0 @@ -/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. - */ - -[Constructor(), Exposed=(Window,Worker), - Func="FetchController::IsEnabled"] -interface FetchController { - readonly attribute FetchSignal signal; - - void abort(); - void follow(FetchSignal signal); - void unfollow(FetchSignal signal); -}; diff --git a/dom/webidl/FetchSignal.webidl b/dom/webidl/FetchSignal.webidl deleted file mode 100644 index 965355c20..000000000 --- a/dom/webidl/FetchSignal.webidl +++ /dev/null @@ -1,13 +0,0 @@ -/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. - */ - -[Exposed=(Window,Worker), - Func="FetchController::IsEnabled"] -interface FetchSignal : EventTarget { - readonly attribute boolean aborted; - - attribute EventHandler onabort; -}; diff --git a/dom/webidl/Request.webidl b/dom/webidl/Request.webidl index 8c6e33da3..fe6a63ec0 100644 --- a/dom/webidl/Request.webidl +++ b/dom/webidl/Request.webidl @@ -48,8 +48,8 @@ dictionary RequestInit { RequestRedirect redirect; DOMString integrity; - [Func="FetchController::IsEnabled"] - FetchSignal signal; + [Func="AbortController::IsEnabled"] + AbortSignal signal; [Func="FetchObserver::IsEnabled"] ObserverCallback observe; diff --git a/dom/webidl/moz.build b/dom/webidl/moz.build index 22075a443..b2dcf8d8d 100644 --- a/dom/webidl/moz.build +++ b/dom/webidl/moz.build @@ -17,6 +17,8 @@ PREPROCESSED_WEBIDL_FILES = [ ] WEBIDL_FILES = [ + 'AbortController.webidl', + 'AbortSignal.webidl', 'AbstractWorker.webidl', 'AnalyserNode.webidl', 'Animatable.webidl', @@ -141,10 +143,8 @@ WEBIDL_FILES = [ 'ExtendableMessageEvent.webidl', 'FakePluginTagInit.webidl', 'Fetch.webidl', - 'FetchController.webidl', 'FetchEvent.webidl', 'FetchObserver.webidl', - 'FetchSignal.webidl', 'File.webidl', 'FileList.webidl', 'FileMode.webidl', diff --git a/dom/workers/WorkerPrefs.h b/dom/workers/WorkerPrefs.h index 374f41ecf..b552d8956 100644 --- a/dom/workers/WorkerPrefs.h +++ b/dom/workers/WorkerPrefs.h @@ -39,7 +39,7 @@ WORKER_SIMPLE_PREF("dom.push.enabled", PushEnabled, PUSH_ENABLED) WORKER_SIMPLE_PREF("dom.requestcontext.enabled", RequestContextEnabled, REQUESTCONTEXT_ENABLED) WORKER_SIMPLE_PREF("gfx.offscreencanvas.enabled", OffscreenCanvasEnabled, OFFSCREENCANVAS_ENABLED) WORKER_SIMPLE_PREF("dom.webkitBlink.dirPicker.enabled", WebkitBlinkDirectoryPickerEnabled, DOM_WEBKITBLINK_DIRPICKER_WEBKITBLINK) -WORKER_SIMPLE_PREF("dom.fetchController.enabled", FetchControllerEnabled, FETCHCONTROLLER_ENABLED) +WORKER_SIMPLE_PREF("dom.abortController.enabled", AbortControllerEnabled, ABORTCONTROLLER_ENABLED) WORKER_SIMPLE_PREF("dom.fetchObserver.enabled", FetchObserverEnabled, FETCHOBSERVER_ENABLED) WORKER_PREF("dom.workers.latestJSVersion", JSVersionChanged) WORKER_PREF("intl.accept_languages", PrefLanguagesChanged) -- cgit v1.2.3