diff --git a/dom/messagechannel/MessagePortParent.h b/dom/messagechannel/MessagePortParent.h index 4cbebe29b..23195c3a7 100644 --- a/dom/messagechannel/MessagePortParent.h +++ b/dom/messagechannel/MessagePortParent.h @@ -5,6 +5,7 @@ #ifndef mozilla_dom_MessagePortParent_h #define mozilla_dom_MessagePortParent_h +#include "mozilla/WeakPtr.h" #include "mozilla/dom/PMessagePortParent.h" namespace mozilla { @@ -12,7 +13,8 @@ namespace dom { class MessagePortService; -class MessagePortParent final : public PMessagePortParent +class MessagePortParent final : public PMessagePortParent, + public SupportsWeakPtr { public: explicit MessagePortParent(const nsID& aUUID); @@ -40,6 +42,8 @@ public: const nsID& aDestinationUUID, const uint32_t& aSequenceID); + MOZ_DECLARE_WEAKREFERENCE_TYPENAME(MessagePortParent) + private: virtual bool RecvPostMessages(nsTArray&& aMessages) override; diff --git a/dom/messagechannel/MessagePortService.cpp b/dom/messagechannel/MessagePortService.cpp index c76d9382b..85d0d42e9 100644 --- a/dom/messagechannel/MessagePortService.cpp +++ b/dom/messagechannel/MessagePortService.cpp @@ -9,6 +9,7 @@ #include "mozilla/ipc/BackgroundParent.h" #include "mozilla/StaticPtr.h" #include "mozilla/Unused.h" +#include "mozilla/WeakPtr.h" #include "nsTArray.h" using mozilla::ipc::AssertIsOnBackgroundThread; @@ -59,7 +60,7 @@ public: { uint32_t mSequenceID; // MessagePortParent keeps the service alive, and we don't want a cycle. - MessagePortParent* mParent; + WeakPtr mParent; }; FallibleTArray mNextParents; @@ -275,9 +276,13 @@ MessagePortService::CloseAll(const nsID& aUUID, bool aForced) data->mParent->Close(); } - for (const MessagePortServiceData::NextParent& parent : data->mNextParents) { - parent.mParent->CloseAndDelete(); + for (const MessagePortServiceData::NextParent& nextParent : data->mNextParents) { + MessagePortParent* const parent = nextParent.mParent; + if (parent) { + parent->CloseAndDelete(); + } } + data->mNextParents.Clear(); nsID destinationUUID = data->mDestinationUUID;