From 29bca891d5ca243774a7355fc3a6a68903f0d596 Mon Sep 17 00:00:00 2001 From: Moonchild Date: Thu, 11 Jun 2020 12:43:17 +0000 Subject: Issue #1587 - Part 6: Move FetchController/Signal to its own dir Since it is specced separately from fetch. --- dom/abort/FetchController.cpp | 127 ++++++++++++++++ dom/abort/FetchController.h | 73 ++++++++++ dom/abort/FetchSignal.cpp | 151 +++++++++++++++++++ dom/abort/FetchSignal.h | 79 ++++++++++ dom/abort/moz.build | 26 ++++ dom/abort/tests/file_fetch_controller.html | 161 +++++++++++++++++++++ dom/abort/tests/mochitest.ini | 6 + dom/abort/tests/moz.build | 8 + dom/abort/tests/test_fetch_controller.html | 40 +++++ dom/abort/tests/worker_fetch_controller.js | 27 ++++ dom/fetch/FetchController.cpp | 127 ---------------- dom/fetch/FetchController.h | 73 ---------- dom/fetch/FetchSignal.cpp | 151 ------------------- dom/fetch/FetchSignal.h | 79 ---------- dom/fetch/moz.build | 4 - dom/moz.build | 1 + .../mochitest/fetch/file_fetch_controller.html | 161 --------------------- dom/tests/mochitest/fetch/mochitest.ini | 3 - .../mochitest/fetch/test_fetch_controller.html | 40 ----- .../mochitest/fetch/worker_fetch_controller.js | 27 ---- 20 files changed, 699 insertions(+), 665 deletions(-) create mode 100644 dom/abort/FetchController.cpp create mode 100644 dom/abort/FetchController.h create mode 100644 dom/abort/FetchSignal.cpp create mode 100644 dom/abort/FetchSignal.h create mode 100644 dom/abort/moz.build create mode 100644 dom/abort/tests/file_fetch_controller.html create mode 100644 dom/abort/tests/mochitest.ini create mode 100644 dom/abort/tests/moz.build create mode 100644 dom/abort/tests/test_fetch_controller.html create mode 100644 dom/abort/tests/worker_fetch_controller.js delete mode 100644 dom/fetch/FetchController.cpp delete mode 100644 dom/fetch/FetchController.h delete mode 100644 dom/fetch/FetchSignal.cpp delete mode 100644 dom/fetch/FetchSignal.h delete mode 100644 dom/tests/mochitest/fetch/file_fetch_controller.html delete mode 100644 dom/tests/mochitest/fetch/test_fetch_controller.html delete mode 100644 dom/tests/mochitest/fetch/worker_fetch_controller.js (limited to 'dom') diff --git a/dom/abort/FetchController.cpp b/dom/abort/FetchController.cpp new file mode 100644 index 000000000..2eb40b980 --- /dev/null +++ b/dom/abort/FetchController.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 "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 new file mode 100644 index 000000000..7a0132dca --- /dev/null +++ b/dom/abort/FetchController.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_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 new file mode 100644 index 000000000..07ad6b53d --- /dev/null +++ b/dom/abort/FetchSignal.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 "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 new file mode 100644 index 000000000..4970f03de --- /dev/null +++ b/dom/abort/FetchSignal.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_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 new file mode 100644 index 000000000..e7d8146e5 --- /dev/null +++ b/dom/abort/moz.build @@ -0,0 +1,26 @@ +# -*- 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") + +TEST_DIRS += ['tests'] + +EXPORTS.mozilla.dom += [ + 'FetchController.h', + 'FetchSignal.h', +] + +UNIFIED_SOURCES += [ + 'FetchController.cpp', + 'FetchSignal.cpp', +] + +LOCAL_INCLUDES += [ + '../workers', +] + +FINAL_LIBRARY = 'xul' diff --git a/dom/abort/tests/file_fetch_controller.html b/dom/abort/tests/file_fetch_controller.html new file mode 100644 index 000000000..e4137aac9 --- /dev/null +++ b/dom/abort/tests/file_fetch_controller.html @@ -0,0 +1,161 @@ + diff --git a/dom/abort/tests/mochitest.ini b/dom/abort/tests/mochitest.ini new file mode 100644 index 000000000..5ecc7048e --- /dev/null +++ b/dom/abort/tests/mochitest.ini @@ -0,0 +1,6 @@ +[DEFAULT] +support-files = + file_fetch_controller.html + worker_fetch_controller.js + +[test_fetch_controller.html] diff --git a/dom/abort/tests/moz.build b/dom/abort/tests/moz.build new file mode 100644 index 000000000..8e5cb5d71 --- /dev/null +++ b/dom/abort/tests/moz.build @@ -0,0 +1,8 @@ +# -*- 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'] + diff --git a/dom/abort/tests/test_fetch_controller.html b/dom/abort/tests/test_fetch_controller.html new file mode 100644 index 000000000..812fb9161 --- /dev/null +++ b/dom/abort/tests/test_fetch_controller.html @@ -0,0 +1,40 @@ + + + + + Test FetchController + + + + + + + + diff --git a/dom/abort/tests/worker_fetch_controller.js b/dom/abort/tests/worker_fetch_controller.js new file mode 100644 index 000000000..6b008fea8 --- /dev/null +++ b/dom/abort/tests/worker_fetch_controller.js @@ -0,0 +1,27 @@ +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/FetchController.cpp b/dom/fetch/FetchController.cpp deleted file mode 100644 index 2eb40b980..000000000 --- a/dom/fetch/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/fetch/FetchController.h b/dom/fetch/FetchController.h deleted file mode 100644 index 7a0132dca..000000000 --- a/dom/fetch/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/fetch/FetchSignal.cpp b/dom/fetch/FetchSignal.cpp deleted file mode 100644 index 07ad6b53d..000000000 --- a/dom/fetch/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/fetch/FetchSignal.h b/dom/fetch/FetchSignal.h deleted file mode 100644 index 4970f03de..000000000 --- a/dom/fetch/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/fetch/moz.build b/dom/fetch/moz.build index 82fd99173..e2b466428 100644 --- a/dom/fetch/moz.build +++ b/dom/fetch/moz.build @@ -7,11 +7,9 @@ EXPORTS.mozilla.dom += [ 'ChannelInfo.h', 'Fetch.h', - 'FetchController.h', 'FetchDriver.h', 'FetchIPCTypes.h', 'FetchObserver.h', - 'FetchSignal.h', 'FetchUtil.h', 'Headers.h', 'InternalHeaders.h', @@ -30,10 +28,8 @@ UNIFIED_SOURCES += [ SOURCES += [ 'ChannelInfo.cpp', - 'FetchController.cpp', 'FetchDriver.cpp', 'FetchObserver.cpp', - 'FetchSignal.cpp', 'FetchUtil.cpp', 'Headers.cpp', 'InternalHeaders.cpp', diff --git a/dom/moz.build b/dom/moz.build index 89c539b4b..8a958982f 100644 --- a/dom/moz.build +++ b/dom/moz.build @@ -37,6 +37,7 @@ interfaces = [ DIRS += ['interfaces/' + i for i in interfaces] DIRS += [ + 'abort', 'animation', 'apps', 'base', diff --git a/dom/tests/mochitest/fetch/file_fetch_controller.html b/dom/tests/mochitest/fetch/file_fetch_controller.html deleted file mode 100644 index e4137aac9..000000000 --- a/dom/tests/mochitest/fetch/file_fetch_controller.html +++ /dev/null @@ -1,161 +0,0 @@ - diff --git a/dom/tests/mochitest/fetch/mochitest.ini b/dom/tests/mochitest/fetch/mochitest.ini index 7493ede50..a9447d0d9 100644 --- a/dom/tests/mochitest/fetch/mochitest.ini +++ b/dom/tests/mochitest/fetch/mochitest.ini @@ -1,7 +1,6 @@ [DEFAULT] support-files = fetch_test_framework.js - file_fetch_controller.html test_fetch_basic.js test_fetch_basic_http.js test_fetch_cors.js @@ -23,7 +22,6 @@ support-files = empty.js^headers^ worker_temporaryFileBlob.js common_temporaryFileBlob.js - worker_fetch_controller.js !/dom/xhr/tests/file_XHR_binary1.bin !/dom/xhr/tests/file_XHR_binary1.bin^headers^ !/dom/xhr/tests/file_XHR_binary2.bin @@ -45,7 +43,6 @@ support-files = [test_fetch_basic_http.html] [test_fetch_basic_http_sw_reroute.html] [test_fetch_basic_http_sw_empty_reroute.html] -[test_fetch_controller.html] [test_fetch_cors.html] skip-if = toolkit == 'android' # Bug 1210282 [test_fetch_cors_sw_reroute.html] diff --git a/dom/tests/mochitest/fetch/test_fetch_controller.html b/dom/tests/mochitest/fetch/test_fetch_controller.html deleted file mode 100644 index 812fb9161..000000000 --- a/dom/tests/mochitest/fetch/test_fetch_controller.html +++ /dev/null @@ -1,40 +0,0 @@ - - - - - Test FetchController - - - - - - - - diff --git a/dom/tests/mochitest/fetch/worker_fetch_controller.js b/dom/tests/mochitest/fetch/worker_fetch_controller.js deleted file mode 100644 index 6b008fea8..000000000 --- a/dom/tests/mochitest/fetch/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](); -} -- cgit v1.2.3