summaryrefslogtreecommitdiff
path: root/dom/fetch
diff options
context:
space:
mode:
Diffstat (limited to 'dom/fetch')
-rw-r--r--dom/fetch/FetchController.cpp110
-rw-r--r--dom/fetch/FetchController.h66
-rw-r--r--dom/fetch/FetchSignal.cpp72
-rw-r--r--dom/fetch/FetchSignal.h47
-rw-r--r--dom/fetch/Request.h1
-rw-r--r--dom/fetch/moz.build4
6 files changed, 300 insertions, 0 deletions
diff --git a/dom/fetch/FetchController.cpp b/dom/fetch/FetchController.cpp
new file mode 100644
index 0000000000..b3d8a4d9c5
--- /dev/null
+++ b/dom/fetch/FetchController.cpp
@@ -0,0 +1,110 @@
+/* -*- 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)
+
+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)
+{
+ // TODO
+}
+
+void
+FetchController::Unfollow(FetchSignal& aSignal)
+{
+ // TODO
+}
+
+} // dom namespace
+} // mozilla namespace
diff --git a/dom/fetch/FetchController.h b/dom/fetch/FetchController.h
new file mode 100644
index 0000000000..854c6f9743
--- /dev/null
+++ b/dom/fetch/FetchController.h
@@ -0,0 +1,66 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_dom_FetchController_h
+#define mozilla_dom_FetchController_h
+
+#include "mozilla/dom/BindingDeclarations.h"
+#include "nsCycleCollectionParticipant.h"
+#include "nsWrapperCache.h"
+#include "mozilla/ErrorResult.h"
+#include "nsIGlobalObject.h"
+
+namespace mozilla {
+namespace dom {
+
+class FetchSignal;
+
+class FetchController final : public nsISupports
+ , public nsWrapperCache
+{
+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);
+
+private:
+ ~FetchController() = default;
+
+ nsCOMPtr<nsIGlobalObject> mGlobal;
+ RefPtr<FetchSignal> mSignal;
+
+ bool mAborted;
+};
+
+} // dom namespace
+} // mozilla namespace
+
+#endif // mozilla_dom_FetchController_h
diff --git a/dom/fetch/FetchSignal.cpp b/dom/fetch/FetchSignal.cpp
new file mode 100644
index 0000000000..4395dbcf2f
--- /dev/null
+++ b/dom/fetch/FetchSignal.cpp
@@ -0,0 +1,72 @@
+/* -*- 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)
+{}
+
+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;
+
+ 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);
+
+ DispatchDOMEvent(nullptr, event, nullptr, nullptr);
+}
+
+} // dom namespace
+} // mozilla namespace
diff --git a/dom/fetch/FetchSignal.h b/dom/fetch/FetchSignal.h
new file mode 100644
index 0000000000..5bb16b8348
--- /dev/null
+++ b/dom/fetch/FetchSignal.h
@@ -0,0 +1,47 @@
+/* -*- 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 final : public DOMEventTargetHelper
+{
+public:
+ NS_DECL_ISUPPORTS_INHERITED
+ NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(FetchSignal, DOMEventTargetHelper)
+
+ FetchSignal(FetchController* aController, bool aAborted);
+
+ JSObject*
+ WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
+
+ bool
+ Aborted() const;
+
+ void
+ Abort();
+
+ IMPL_EVENT_HANDLER(abort);
+
+private:
+ ~FetchSignal() = default;
+
+ RefPtr<FetchController> mController;
+
+ bool mAborted;
+};
+
+} // dom namespace
+} // mozilla namespace
+
+#endif // mozilla_dom_FetchSignal_h
diff --git a/dom/fetch/Request.h b/dom/fetch/Request.h
index d33c748122..56a75e5af5 100644
--- a/dom/fetch/Request.h
+++ b/dom/fetch/Request.h
@@ -12,6 +12,7 @@
#include "nsWrapperCache.h"
#include "mozilla/dom/Fetch.h"
+#include "mozilla/dom/FetchSignal.h"
#include "mozilla/dom/InternalRequest.h"
// Required here due to certain WebIDL enums/classes being declared in both
// files.
diff --git a/dom/fetch/moz.build b/dom/fetch/moz.build
index be820ab570..757f857f28 100644
--- a/dom/fetch/moz.build
+++ b/dom/fetch/moz.build
@@ -7,8 +7,10 @@
EXPORTS.mozilla.dom += [
'ChannelInfo.h',
'Fetch.h',
+ 'FetchController.h',
'FetchDriver.h',
'FetchIPCTypes.h',
+ 'FetchSignal.h',
'FetchUtil.h',
'Headers.h',
'InternalHeaders.h',
@@ -27,7 +29,9 @@ UNIFIED_SOURCES += [
SOURCES += [
'ChannelInfo.cpp',
+ 'FetchController.cpp',
'FetchDriver.cpp',
+ 'FetchSignal.cpp',
'FetchUtil.cpp',
'Headers.cpp',
'InternalHeaders.cpp',