From 0833d699e378cfe3c5fc8b843990b7f5f7f8d68e Mon Sep 17 00:00:00 2001 From: Olli Pettay Date: Fri, 8 Sep 2023 14:51:48 +0000 Subject: [PATCH] Bug 1843477, non-virtual AddRef/Release for EventTarget, r=mccr8 And convert some nsCOMPtr to RefPtr Depends on D187147 Differential Revision: https://phabricator.services.mozilla.com/D187148 --- dom/base/Attr.h | 3 ++- dom/base/Document.h | 3 ++- dom/base/nsGlobalWindowInner.h | 3 ++- dom/base/nsGlobalWindowOuter.h | 3 ++- dom/base/nsIContent.h | 3 ++- dom/base/nsWindowRoot.h | 3 ++- dom/events/DOMEventTargetHelper.h | 3 ++- dom/events/EventDispatcher.cpp | 10 ++++----- dom/events/EventListenerManager.cpp | 2 +- dom/events/EventTarget.cpp | 34 +++++++++++++++++++++++++++++ dom/events/EventTarget.h | 19 ++++++++++++++++ xpcom/base/nsISupportsImpl.h | 6 ----- 12 files changed, 73 insertions(+), 19 deletions(-) diff --git a/dom/base/Attr.h b/dom/base/Attr.h index c921221fe473..311f7aa597b0 100644 --- a/dom/base/Attr.h +++ b/dom/base/Attr.h @@ -34,7 +34,8 @@ class Attr final : public nsINode { Attr(nsDOMAttributeMap* aAttrMap, already_AddRefed&& aNodeInfo, const nsAString& aValue); - NS_DECL_CYCLE_COLLECTING_ISUPPORTS_FINAL_DELETECYCLECOLLECTABLE + NS_DECL_ISUPPORTS_INHERITED + NS_IMETHOD_(void) DeleteCycleCollectable(void) final; NS_DECL_DOMARENA_DESTROY diff --git a/dom/base/Document.h b/dom/base/Document.h index 10e25e69b83b..31bac347b0e3 100644 --- a/dom/base/Document.h +++ b/dom/base/Document.h @@ -562,7 +562,8 @@ class Document : public nsINode, NS_DECLARE_STATIC_IID_ACCESSOR(NS_IDOCUMENT_IID) - NS_DECL_CYCLE_COLLECTING_ISUPPORTS + NS_DECL_ISUPPORTS_INHERITED + NS_IMETHOD_(void) DeleteCycleCollectable() override; NS_DECL_ADDSIZEOFEXCLUDINGTHIS diff --git a/dom/base/nsGlobalWindowInner.h b/dom/base/nsGlobalWindowInner.h index d44f1a5dfd33..583c286e9bdf 100644 --- a/dom/base/nsGlobalWindowInner.h +++ b/dom/base/nsGlobalWindowInner.h @@ -217,7 +217,8 @@ class nsGlobalWindowInner final : public mozilla::dom::EventTarget, mozilla::dom::WindowGlobalChild* aActor); // nsISupports - NS_DECL_CYCLE_COLLECTING_ISUPPORTS + NS_DECL_ISUPPORTS_INHERITED + NS_IMETHOD_(void) DeleteCycleCollectable() override; // nsWrapperCache virtual JSObject* WrapObject(JSContext* cx, diff --git a/dom/base/nsGlobalWindowOuter.h b/dom/base/nsGlobalWindowOuter.h index 8a891ca19a56..361ffe4b6ada 100644 --- a/dom/base/nsGlobalWindowOuter.h +++ b/dom/base/nsGlobalWindowOuter.h @@ -208,7 +208,8 @@ class nsGlobalWindowOuter final : public mozilla::dom::EventTarget, void ReallyCloseWindow(); // nsISupports - NS_DECL_CYCLE_COLLECTING_ISUPPORTS + NS_DECL_ISUPPORTS_INHERITED + NS_IMETHOD_(void) DeleteCycleCollectable() override; // nsWrapperCache virtual JSObject* WrapObject(JSContext* cx, diff --git a/dom/base/nsIContent.h b/dom/base/nsIContent.h index b28fdcb582f1..9683deadccb4 100644 --- a/dom/base/nsIContent.h +++ b/dom/base/nsIContent.h @@ -65,7 +65,8 @@ class nsIContent : public nsINode { NS_DECLARE_STATIC_IID_ACCESSOR(NS_ICONTENT_IID) - NS_DECL_CYCLE_COLLECTING_ISUPPORTS_FINAL_DELETECYCLECOLLECTABLE + NS_DECL_ISUPPORTS_INHERITED + NS_IMETHOD_(void) DeleteCycleCollectable(void) final; NS_DECL_CYCLE_COLLECTION_CLASS(nsIContent) diff --git a/dom/base/nsWindowRoot.h b/dom/base/nsWindowRoot.h index 7fb56acd1893..821095d2c988 100644 --- a/dom/base/nsWindowRoot.h +++ b/dom/base/nsWindowRoot.h @@ -21,7 +21,8 @@ class nsWindowRoot final : public nsPIWindowRoot { public: explicit nsWindowRoot(nsPIDOMWindowOuter* aWindow); - NS_DECL_CYCLE_COLLECTING_ISUPPORTS + NS_DECL_ISUPPORTS_INHERITED + NS_IMETHOD_(void) DeleteCycleCollectable() override; mozilla::EventListenerManager* GetExistingListenerManager() const override; mozilla::EventListenerManager* GetOrCreateListenerManager() override; diff --git a/dom/events/DOMEventTargetHelper.h b/dom/events/DOMEventTargetHelper.h index 622f482a1677..63d97c3b9470 100644 --- a/dom/events/DOMEventTargetHelper.h +++ b/dom/events/DOMEventTargetHelper.h @@ -55,7 +55,8 @@ class DOMEventTargetHelper : public dom::EventTarget, explicit DOMEventTargetHelper(nsIGlobalObject* aGlobalObject); explicit DOMEventTargetHelper(DOMEventTargetHelper* aOther); - NS_DECL_CYCLE_COLLECTING_ISUPPORTS + NS_DECL_ISUPPORTS_INHERITED + NS_IMETHOD_(void) DeleteCycleCollectable() override; NS_DECL_CYCLE_COLLECTION_SKIPPABLE_WRAPPERCACHE_CLASS(DOMEventTargetHelper) virtual EventListenerManager* GetExistingListenerManager() const override; diff --git a/dom/events/EventDispatcher.cpp b/dom/events/EventDispatcher.cpp index dd4247ee44f2..7e1336bd153f 100644 --- a/dom/events/EventDispatcher.cpp +++ b/dom/events/EventDispatcher.cpp @@ -374,8 +374,8 @@ class EventTargetChainItem { MOZ_CAN_RUN_SCRIPT void PostHandleEvent(EventChainPostVisitor& aVisitor); private: - const nsCOMPtr mTarget; - nsCOMPtr mRetargetedRelatedTarget; + const RefPtr mTarget; + RefPtr mRetargetedRelatedTarget; Maybe>> mRetargetedTouchTargets; Maybe>> mInitialTargetTouches; @@ -410,7 +410,7 @@ class EventTargetChainItem { uint16_t mItemFlags; nsCOMPtr mItemData; // Event retargeting must happen whenever mNewTarget is non-null. - nsCOMPtr mNewTarget; + RefPtr mNewTarget; // Cache mTarget's event listener manager. RefPtr mManager; @@ -830,7 +830,7 @@ nsresult EventDispatcher::Dispatch(EventTarget* aTarget, NS_ENSURE_TRUE(!nsContentUtils::IsInStableOrMetaStableState(), NS_ERROR_DOM_INVALID_STATE_ERR); - nsCOMPtr target(aTarget); + RefPtr target(aTarget); RefPtr eventTimingEntry; // Similar to PerformancePaintTiming, we don't need to @@ -845,7 +845,7 @@ nsresult EventDispatcher::Dispatch(EventTarget* aTarget, if (aEvent->mFlags.mRetargetToNonNativeAnonymous) { nsIContent* content = nsIContent::FromEventTargetOrNull(target); if (content && content->IsInNativeAnonymousSubtree()) { - nsCOMPtr newTarget = + RefPtr newTarget = content->FindFirstNonChromeOnlyAccessContent(); NS_ENSURE_STATE(newTarget); diff --git a/dom/events/EventListenerManager.cpp b/dom/events/EventListenerManager.cpp index 1ace55992c77..03a34e4f3827 100644 --- a/dom/events/EventListenerManager.cpp +++ b/dom/events/EventListenerManager.cpp @@ -1868,7 +1868,7 @@ bool EventListenerManager::HasListeners() const { nsresult EventListenerManager::GetListenerInfo( nsTArray>& aList) { - nsCOMPtr target = mTarget; + RefPtr target = mTarget; NS_ENSURE_STATE(target); aList.Clear(); for (const auto& entry : mListenerMap.mEntries) { diff --git a/dom/events/EventTarget.cpp b/dom/events/EventTarget.cpp index 5502de5bcea6..20ec7c41bec1 100644 --- a/dom/events/EventTarget.cpp +++ b/dom/events/EventTarget.cpp @@ -18,6 +18,40 @@ namespace mozilla::dom { +#ifndef NS_BUILD_REFCNT_LOGGING +MozExternalRefCountType EventTarget::NonVirtualAddRef() { + return mRefCnt.incr(this); +} + +MozExternalRefCountType EventTarget::NonVirtualRelease() { + if (mRefCnt.get() == 1) { + return Release(); + } + return mRefCnt.decr(this); +} +#endif + +NS_IMETHODIMP_(MozExternalRefCountType) EventTarget::AddRef() { + MOZ_ASSERT_UNREACHABLE("EventTarget::AddRef should not be called"); + return 0; +} + +NS_IMETHODIMP_(MozExternalRefCountType) EventTarget::Release() { + MOZ_ASSERT_UNREACHABLE("EventTarget::Release should not be called"); + return 0; +} + +NS_IMETHODIMP EventTarget::QueryInterface(REFNSIID aIID, void** aInstancePtr) { + MOZ_ASSERT_UNREACHABLE("EventTarget::QueryInterface should not be called"); + *aInstancePtr = nullptr; + return NS_ERROR_FAILURE; +} + +NS_IMETHODIMP_(void) EventTarget::DeleteCycleCollectable() { + MOZ_ASSERT_UNREACHABLE( + "EventTarget::DeleteCycleCollectable should not be called"); +} + /* static */ already_AddRefed EventTarget::Constructor( const GlobalObject& aGlobal, ErrorResult& aRv) { diff --git a/dom/events/EventTarget.h b/dom/events/EventTarget.h index 058ce06cbcd0..db76936f86ab 100644 --- a/dom/events/EventTarget.h +++ b/dom/events/EventTarget.h @@ -52,6 +52,13 @@ class EventTarget : public nsISupports, public nsWrapperCache { public: NS_DECLARE_STATIC_IID_ACCESSOR(NS_EVENTTARGET_IID) + NS_DECL_CYCLE_COLLECTING_ISUPPORTS + +#ifndef NS_BUILD_REFCNT_LOGGING + MozExternalRefCountType NonVirtualAddRef(); + MozExternalRefCountType NonVirtualRelease(); +#endif + // WebIDL API static already_AddRefed Constructor(const GlobalObject& aGlobal, ErrorResult& aRv); @@ -427,6 +434,18 @@ NS_DEFINE_STATIC_IID_ACCESSOR(EventTarget, NS_EVENTTARGET_IID) NS_IMPL_FROMEVENTTARGET_HELPER_WITH_GETTER_INNER(_class, _getter) } // namespace dom + +#ifndef NS_BUILD_REFCNT_LOGGING +template <> +class RefPtrTraits { + public: + static void Release(dom::EventTarget* aTarget) { + aTarget->NonVirtualRelease(); + } + static void AddRef(dom::EventTarget* aTarget) { aTarget->NonVirtualAddRef(); } +}; +#endif + } // namespace mozilla #endif // mozilla_dom_EventTarget_h_ diff --git a/xpcom/base/nsISupportsImpl.h b/xpcom/base/nsISupportsImpl.h index 29c53330672f..2bbe72775b74 100644 --- a/xpcom/base/nsISupportsImpl.h +++ b/xpcom/base/nsISupportsImpl.h @@ -459,12 +459,6 @@ class InterfaceNeedsThreadSafeRefCnt : public std::false_type {}; \ public: -#define NS_DECL_CYCLE_COLLECTING_ISUPPORTS_FINAL_DELETECYCLECOLLECTABLE \ - NS_DECL_CYCLE_COLLECTING_ISUPPORTS_META(override) \ - NS_IMETHOD_(void) DeleteCycleCollectable(void) final; \ - \ - public: - #define NS_DECL_CYCLE_COLLECTING_ISUPPORTS_META(...) \ public: \ NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) __VA_ARGS__; \