diff options
author | Moonchild <moonchild@palemoon.org> | 2020-06-11 12:43:17 +0000 |
---|---|---|
committer | wolfbeast <mcwerewolf@wolfbeast.com> | 2020-06-13 11:52:17 +0200 |
commit | 29bca891d5ca243774a7355fc3a6a68903f0d596 (patch) | |
tree | ec5a36a57a179b811642c0ee5636ffd3005adc67 /dom/abort/FetchController.cpp | |
parent | bb7060582934f087b71702d1e83f762d5fd6f0af (diff) | |
download | UXP-29bca891d5ca243774a7355fc3a6a68903f0d596.tar UXP-29bca891d5ca243774a7355fc3a6a68903f0d596.tar.gz UXP-29bca891d5ca243774a7355fc3a6a68903f0d596.tar.lz UXP-29bca891d5ca243774a7355fc3a6a68903f0d596.tar.xz UXP-29bca891d5ca243774a7355fc3a6a68903f0d596.zip |
Issue #1587 - Part 6: Move FetchController/Signal to its own dir
Since it is specced separately from fetch.
Diffstat (limited to 'dom/abort/FetchController.cpp')
-rw-r--r-- | dom/abort/FetchController.cpp | 127 |
1 files changed, 127 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 |