summaryrefslogtreecommitdiffstats
path: root/dom/fetch
diff options
context:
space:
mode:
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;
};