summaryrefslogtreecommitdiffstats
path: root/dom/fetch
diff options
context:
space:
mode:
authorMoonchild <moonchild@palemoon.org>2020-06-10 21:33:28 +0000
committerwolfbeast <mcwerewolf@wolfbeast.com>2020-06-13 11:51:30 +0200
commit9e2da53a02356244c5dc0a3e7b7ec916740d3d51 (patch)
tree098aa27ce9fbcc0371e4f1bb52fac0f5541d7702 /dom/fetch
parent2b41c85019bd5fd7e62556b5d5bf6ace2a6d6963 (diff)
downloadUXP-9e2da53a02356244c5dc0a3e7b7ec916740d3d51.tar
UXP-9e2da53a02356244c5dc0a3e7b7ec916740d3d51.tar.gz
UXP-9e2da53a02356244c5dc0a3e7b7ec916740d3d51.tar.lz
UXP-9e2da53a02356244c5dc0a3e7b7ec916740d3d51.tar.xz
UXP-9e2da53a02356244c5dc0a3e7b7ec916740d3d51.zip
Issue #1587 - Part 2: Implement controller follow/unfollow
Diffstat (limited to 'dom/fetch')
-rw-r--r--dom/fetch/FetchController.cpp23
-rw-r--r--dom/fetch/FetchController.h11
-rw-r--r--dom/fetch/FetchSignal.cpp77
-rw-r--r--dom/fetch/FetchSignal.h31
4 files changed, 134 insertions, 8 deletions
diff --git a/dom/fetch/FetchController.cpp b/dom/fetch/FetchController.cpp
index b3d8a4d9c..2eb40b980 100644
--- a/dom/fetch/FetchController.cpp
+++ b/dom/fetch/FetchController.cpp
@@ -12,7 +12,8 @@
namespace mozilla {
namespace dom {
-NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(FetchController, mGlobal, mSignal)
+NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(FetchController, mGlobal, mSignal,
+ mFollowingSignal)
NS_IMPL_CYCLE_COLLECTING_ADDREF(FetchController)
NS_IMPL_CYCLE_COLLECTING_RELEASE(FetchController)
@@ -97,13 +98,29 @@ FetchController::Abort()
void
FetchController::Follow(FetchSignal& aSignal)
{
- // TODO
+ FetchSignal::Follower::Follow(&aSignal);
}
void
FetchController::Unfollow(FetchSignal& aSignal)
{
- // TODO
+ if (mFollowingSignal != &aSignal) {
+ return;
+ }
+
+ FetchSignal::Follower::Unfollow();
+}
+
+FetchSignal*
+FetchController::Following() const
+{
+ return mFollowingSignal;
+}
+
+void
+FetchController::Aborted()
+{
+ Abort();
}
} // dom namespace
diff --git a/dom/fetch/FetchController.h b/dom/fetch/FetchController.h
index 854c6f974..7a0132dca 100644
--- a/dom/fetch/FetchController.h
+++ b/dom/fetch/FetchController.h
@@ -8,6 +8,7 @@
#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"
@@ -16,10 +17,9 @@
namespace mozilla {
namespace dom {
-class FetchSignal;
-
class FetchController final : public nsISupports
, public nsWrapperCache
+ , public FetchSignal::Follower
{
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
@@ -51,6 +51,13 @@ public:
void
Unfollow(FetchSignal& aSignal);
+ FetchSignal*
+ Following() const;
+
+ // FetchSignal::Follower
+
+ void Aborted() override;
+
private:
~FetchController() = default;
diff --git a/dom/fetch/FetchSignal.cpp b/dom/fetch/FetchSignal.cpp
index 4395dbcf2..1924263e8 100644
--- a/dom/fetch/FetchSignal.cpp
+++ b/dom/fetch/FetchSignal.cpp
@@ -16,12 +16,12 @@ 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(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(mController)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FetchSignal)
@@ -55,6 +55,11 @@ 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;
@@ -65,7 +70,73 @@ FetchSignal::Abort()
Event::Constructor(this, NS_LITERAL_STRING("abort"), init);
event->SetTrusted(true);
- DispatchDOMEvent(nullptr, event, nullptr, nullptr);
+ 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 (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
diff --git a/dom/fetch/FetchSignal.h b/dom/fetch/FetchSignal.h
index 5bb16b834..5d2f13c68 100644
--- a/dom/fetch/FetchSignal.h
+++ b/dom/fetch/FetchSignal.h
@@ -13,10 +13,29 @@ 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)
@@ -33,11 +52,23 @@ public:
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;
};