summaryrefslogtreecommitdiffstats
path: root/dom/abort
diff options
context:
space:
mode:
authorMoonchild <moonchild@palemoon.org>2020-07-10 18:46:10 +0000
committerMoonchild <moonchild@palemoon.org>2020-07-10 18:46:10 +0000
commitf1b51be787c11090c8d9b2ec73255df7a67c7eb7 (patch)
tree9c6d35ce6f19e0fef3c47c7e2c152394854cf217 /dom/abort
parent2deaddfca28508ac1a634eb6088a1da8e571ec6e (diff)
parent82faff19e1761797b7a75f9221f0709c5a38bfe6 (diff)
downloadUXP-f1b51be787c11090c8d9b2ec73255df7a67c7eb7.tar
UXP-f1b51be787c11090c8d9b2ec73255df7a67c7eb7.tar.gz
UXP-f1b51be787c11090c8d9b2ec73255df7a67c7eb7.tar.lz
UXP-f1b51be787c11090c8d9b2ec73255df7a67c7eb7.tar.xz
UXP-f1b51be787c11090c8d9b2ec73255df7a67c7eb7.zip
Merge branch 'redwood' into releaseRELBASE_20200711
Diffstat (limited to 'dom/abort')
-rw-r--r--dom/abort/AbortController.cpp98
-rw-r--r--dom/abort/AbortController.h59
-rw-r--r--dom/abort/AbortSignal.cpp124
-rw-r--r--dom/abort/AbortSignal.h76
-rw-r--r--dom/abort/moz.build26
-rw-r--r--dom/abort/tests/file_abort_controller.html113
-rw-r--r--dom/abort/tests/mochitest.ini6
-rw-r--r--dom/abort/tests/moz.build8
-rw-r--r--dom/abort/tests/test_abort_controller.html40
-rw-r--r--dom/abort/tests/worker_abort_controller.js27
10 files changed, 577 insertions, 0 deletions
diff --git a/dom/abort/AbortController.cpp b/dom/abort/AbortController.cpp
new file mode 100644
index 000000000..bd8159e7b
--- /dev/null
+++ b/dom/abort/AbortController.cpp
@@ -0,0 +1,98 @@
+/* -*- 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)
+
+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>
+AbortController::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
+{
+ nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
+ if (!global) {
+ aRv.Throw(NS_ERROR_FAILURE);
+ return nullptr;
+ }
+
+ RefPtr<AbortController> abortController = new AbortController(global);
+ return abortController.forget();
+}
+
+AbortController::AbortController(nsIGlobalObject* aGlobal)
+ : mGlobal(aGlobal)
+ , mAborted(false)
+{}
+
+JSObject*
+AbortController::WrapObject(JSContext* aCx, JS::Handle<JSObject*> 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();
+ }
+}
+
+} // dom namespace
+} // mozilla namespace
diff --git a/dom/abort/AbortController.h b/dom/abort/AbortController.h
new file mode 100644
index 000000000..0b99dc49c
--- /dev/null
+++ b/dom/abort/AbortController.h
@@ -0,0 +1,59 @@
+/* -*- 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:
+ NS_DECL_CYCLE_COLLECTING_ISUPPORTS
+ NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(AbortController)
+
+ static bool
+ IsEnabled(JSContext* aCx, JSObject* aGlobal);
+
+ static already_AddRefed<AbortController>
+ Constructor(const GlobalObject& aGlobal, ErrorResult& aRv);
+
+ explicit AbortController(nsIGlobalObject* aGlobal);
+
+ JSObject*
+ WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
+
+ nsIGlobalObject*
+ GetParentObject() const;
+
+ AbortSignal*
+ Signal();
+
+ void
+ Abort();
+
+private:
+ ~AbortController() = default;
+
+ nsCOMPtr<nsIGlobalObject> mGlobal;
+ RefPtr<AbortSignal> 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..20f36d2ab
--- /dev/null
+++ b/dom/abort/AbortSignal.cpp
@@ -0,0 +1,124 @@
+/* -*- 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<JSObject*> 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;
+
+ RefPtr<Event> 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);
+}
+
+// AbortSignal::Follower
+// ----------------------------------------------------------------------------
+
+AbortSignal::Follower::~Follower()
+{
+ Unfollow();
+}
+
+void
+AbortSignal::Follower::Follow(AbortSignal* aSignal)
+{
+ MOZ_DIAGNOSTIC_ASSERT(aSignal);
+
+ 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..35e582942
--- /dev/null
+++ b/dom/abort/AbortSignal.h
@@ -0,0 +1,76 @@
+/* -*- 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<AbortSignal> 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<JSObject*> aGivenProto) override;
+
+ bool
+ Aborted() const;
+
+ void
+ Abort();
+
+ IMPL_EVENT_HANDLER(abort);
+
+ void
+ AddFollower(Follower* aFollower);
+
+ void
+ RemoveFollower(Follower* aFollower);
+
+private:
+ ~AbortSignal() = default;
+
+ RefPtr<AbortController> mController;
+
+ // Raw pointers. Follower unregisters itself in the DTOR.
+ nsTArray<Follower*> mFollowers;
+
+ bool mAborted;
+};
+
+} // dom namespace
+} // mozilla namespace
+
+#endif // mozilla_dom_AbortSignal_h
diff --git a/dom/abort/moz.build b/dom/abort/moz.build
new file mode 100644
index 000000000..cb48ee15f
--- /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 += [
+ 'AbortController.h',
+ 'AbortSignal.h',
+]
+
+UNIFIED_SOURCES += [
+ 'AbortController.cpp',
+ 'AbortSignal.cpp',
+]
+
+LOCAL_INCLUDES += [
+ '../workers',
+]
+
+FINAL_LIBRARY = 'xul'
diff --git a/dom/abort/tests/file_abort_controller.html b/dom/abort/tests/file_abort_controller.html
new file mode 100644
index 000000000..3a15fa346
--- /dev/null
+++ b/dom/abort/tests/file_abort_controller.html
@@ -0,0 +1,113 @@
+<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 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,
+
+ // 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..c8cc95fda
--- /dev/null
+++ b/dom/abort/tests/mochitest.ini
@@ -0,0 +1,6 @@
+[DEFAULT]
+support-files =
+ file_abort_controller.html
+ worker_fetch_controller.js
+
+[test_abort_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_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 @@
+<!--
+ 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_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]();
+}