diff options
Diffstat (limited to 'dom/abort')
-rw-r--r-- | dom/abort/FetchController.cpp | 127 | ||||
-rw-r--r-- | dom/abort/FetchController.h | 73 | ||||
-rw-r--r-- | dom/abort/FetchSignal.cpp | 151 | ||||
-rw-r--r-- | dom/abort/FetchSignal.h | 79 | ||||
-rw-r--r-- | dom/abort/moz.build | 26 | ||||
-rw-r--r-- | dom/abort/tests/file_fetch_controller.html | 161 | ||||
-rw-r--r-- | dom/abort/tests/mochitest.ini | 6 | ||||
-rw-r--r-- | dom/abort/tests/moz.build | 8 | ||||
-rw-r--r-- | dom/abort/tests/test_fetch_controller.html | 40 | ||||
-rw-r--r-- | dom/abort/tests/worker_fetch_controller.js | 27 |
10 files changed, 698 insertions, 0 deletions
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> +FetchController::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv) +{ + nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports()); + if (!global) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + + RefPtr<FetchController> fetchController = new FetchController(global); + return fetchController.forget(); +} + +FetchController::FetchController(nsIGlobalObject* aGlobal) + : mGlobal(aGlobal) + , mAborted(false) +{} + +JSObject* +FetchController::WrapObject(JSContext* aCx, JS::Handle<JSObject*> 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<FetchController> + Constructor(const GlobalObject& aGlobal, ErrorResult& aRv); + + explicit FetchController(nsIGlobalObject* aGlobal); + + JSObject* + WrapObject(JSContext* aCx, JS::Handle<JSObject*> 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<nsIGlobalObject> mGlobal; + RefPtr<FetchSignal> 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<JSObject*> 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 = + 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<FetchSignal> 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<JSObject*> 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<FetchController> mController; + + // Raw pointers. Follower unregisters itself in the DTOR. + nsTArray<Follower*> 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 @@ +<script> +function ok(a, msg) { + parent.postMessage({ type: "check", status: !!a, message: msg }, "*"); +} + +function is(a, b, msg) { + ok(a === b, msg); +} + +function testWebIDL() { + ok("FetchController" in self, "We have a FetchController prototype"); + ok("FetchSignal" in self, "We have a FetchSignal prototype"); + + var fc = new FetchController(); + ok(!!fc, "FetchController can be created"); + ok(fc instanceof FetchController, "FetchController is a FetchController"); + + ok(!!fc.signal, "FetchController has a signal"); + ok(fc.signal instanceof FetchSignal, "fetchSignal is a FetchSignal"); + is(fc.signal.aborted, false, "By default FetchSignal.aborted is false"); + next(); +} + +function testUpdateData() { + var fc = new FetchController(); + + is(fc.signal.aborted, false, "By default FetchSignal.aborted is false"); + + fc.abort(); + is(fc.signal.aborted, true, "Signal is aborted"); + + next(); +} + +function testFollowingOurself() { + // Let's follow ourself + var fc = new FetchController(); + fc.follow(fc.signal); + + fc.abort(); + is(fc.signal.aborted, true, "Signal is aborted"); + + next(); +} + +function testFollowingOther() { + // Let's follow another one + var fc1 = new FetchController(); + var fc2 = new FetchController(); + fc1.follow(fc2.signal); + + fc2.abort(); + + is(fc1.signal.aborted, true, "Signal is aborted"); + is(fc2.signal.aborted, true, "Signal is aborted"); + + next(); +} + +function testFollowingLoop() { + // fc1 -> fc2 -> fc3 -> fc1 + var fc1 = new FetchController(); + var fc2 = new FetchController(); + var fc3 = new FetchController(); + fc1.follow(fc2.signal); + fc2.follow(fc3.signal); + fc3.follow(fc1.signal); + + fc3.abort(); + + is(fc1.signal.aborted, true, "Signal is aborted"); + is(fc2.signal.aborted, true, "Signal is aborted"); + is(fc3.signal.aborted, true, "Signal is aborted"); + + next(); +} + +function testAbortEvent() { + var fc = new FetchController(); + fc.signal.onabort = function(e) { + is(e.type, "abort", "Abort received"); + next(); + } + fc.abort(); +} + +function testAbortedFetch() { + var fc = new FetchController(); + fc.abort(); + + fetch('slow.sjs', { signal: fc.signal }).then(() => { + ok(false, "Fetch should not return a resolved promise"); + }, e => { + is(e.name, "AbortError", "We have an abort error"); + }).then(next); +} + +function testFetchAndAbort() { + var fc = new FetchController(); + + var p = fetch('slow.sjs', { signal: fc.signal }); + fc.abort(); + + p.then(() => { + ok(false, "Fetch should not return a resolved promise"); + }, e => { + is(e.name, "AbortError", "We have an abort error"); + }).then(next); +} + +function testWorkerAbortedFetch() { + var w = new Worker('worker_fetch_controller.js'); + w.onmessage = function(e) { + ok(e.data, "Abort + Fetch works in workers"); + next(); + } + w.postMessage('testWorkerAbortedFetch'); +} + +function testWorkerFetchAndAbort() { + var w = new Worker('worker_fetch_controller.js'); + w.onmessage = function(e) { + ok(e.data, "Abort + Fetch works in workers"); + next(); + } + w.postMessage('testWorkerFetchAndAbort'); +} + +var steps = [ + // Simple stuff + testWebIDL, + testUpdateData, + + // Following algorithm + testFollowingOurself, + testFollowingOther, + testFollowingLoop, + + // Event propagation + testAbortEvent, + + // fetch + signaling + testAbortedFetch, + testFetchAndAbort, + testWorkerAbortedFetch, + testWorkerFetchAndAbort, +]; + +function next() { + if (!steps.length) { + parent.postMessage({ type: "finish" }, "*"); + return; + } + + var step = steps.shift(); + step(); +} + +next(); + +</script> 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 @@ +<!-- + Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ +--> +<!DOCTYPE HTML> +<html> +<head> + <title>Test FetchController</title> + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> +</head> +<body> +<script class="testbody" type="text/javascript"> + +SpecialPowers.pushPrefEnv({"set": [["dom.fetchController.enabled", true ]]}, () => { + let ifr = document.createElement('iframe'); + ifr.src = "file_fetch_controller.html"; + document.body.appendChild(ifr); + + onmessage = function(e) { + if (e.data.type == "finish") { + SimpleTest.finish(); + return; + } + + if (e.data.type == "check") { + ok(e.data.status, e.data.message); + return; + } + + ok(false, "Something when wrong."); + } +}); + +SimpleTest.waitForExplicitFinish(); + +</script> +</body> +</html> + 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](); +} |