From 30c673699131aed0ea35abbf7b1d4154065cc20b Mon Sep 17 00:00:00 2001 From: Jan-Niklas Jaeschke Date: Tue, 7 Mar 2023 08:22:06 +0000 Subject: [PATCH] Bug 1820286: Removed unnecessary strong references in `Highlight`. r=masayuki Due to the sections being guarded by an `AutoFrameSelectionBatcher`, no strong references are needed. Thus, `MOZ_KnownLive()` can be used to suppress warnings. Differential Revision: https://phabricator.services.mozilla.com/D171707 --- dom/base/Highlight.cpp | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/dom/base/Highlight.cpp b/dom/base/Highlight.cpp index 7e6a7e0e135f..71190ac08be5 100644 --- a/dom/base/Highlight.cpp +++ b/dom/base/Highlight.cpp @@ -90,12 +90,13 @@ already_AddRefed Highlight::CreateHighlightSelection( selection->SetHighlightName(aHighlightName); AutoFrameSelectionBatcher selectionBatcher(__FUNCTION__); selectionBatcher.AddFrameSelection(aFrameSelection); - // NOLINTNEXTLINE(performance-for-range-copy) - for (const RefPtr range : mRanges) { + for (const RefPtr& range : mRanges) { if (range->GetComposedDocOfContainers() == aFrameSelection->GetPresShell()->GetDocument()) { - selection->AddHighlightRangeAndSelectFramesAndNotifyListeners(*range, - aRv); + // since this is run in a context guarded by a selection batcher, + // no strong reference is needed to keep `range` alive. + selection->AddHighlightRangeAndSelectFramesAndNotifyListeners( + MOZ_KnownLive(*range), aRv); } } return selection.forget(); @@ -110,12 +111,14 @@ void Highlight::Add(AbstractRange& aRange, ErrorResult& aRv) { mRanges.AppendElement(&aRange); AutoFrameSelectionBatcher selectionBatcher(__FUNCTION__, mHighlightRegistries.Count()); - for (const RefPtr registry : + for (const RefPtr& registry : mHighlightRegistries.Keys()) { auto frameSelection = registry->GetFrameSelection(); selectionBatcher.AddFrameSelection(frameSelection); - - registry->MaybeAddRangeToHighlightSelection(aRange, *this, aRv); + // since this is run in a context guarded by a selection batcher, + // no strong reference is needed to keep `registry` alive. + MOZ_KnownLive(registry)->MaybeAddRangeToHighlightSelection(aRange, *this, + aRv); if (aRv.Failed()) { return; } @@ -134,8 +137,8 @@ void Highlight::Clear(ErrorResult& aRv) { mHighlightRegistries.Keys()) { auto frameSelection = registry->GetFrameSelection(); selectionBatcher.AddFrameSelection(frameSelection); - // Because of the selection batcher, this call does *not* run script. - // MOZ_KnownLive() is needed regardless. + // since this is run in a context guarded by a selection batcher, + // no strong reference is needed to keep `registry` alive. MOZ_KnownLive(registry)->RemoveHighlightSelection(*this); } } @@ -151,8 +154,8 @@ bool Highlight::Delete(AbstractRange& aRange, ErrorResult& aRv) { mHighlightRegistries.Keys()) { auto frameSelection = registry->GetFrameSelection(); selectionBatcher.AddFrameSelection(frameSelection); - // Because of the selection batcher, this call does *not* run script. - // MOZ_KnownLive() is needed regardless. + // since this is run in a context guarded by a selection batcher, + // no strong reference is needed to keep `registry` alive. MOZ_KnownLive(registry)->MaybeRemoveRangeFromHighlightSelection(aRange, *this); }