From cfb8b32810b67de75d1b24b701baa9bea1a3b3d3 Mon Sep 17 00:00:00 2001 From: Daniel Holbert Date: Wed, 15 May 2024 07:29:20 +0000 Subject: [PATCH] Bug 1896711 part 3: Use MakeRefPtr/MakeAndAddRef to reduce boilerplate, in layout/base. r=TYLin Also remove some unnecessary null-checks for objects that we're allocating infallibly. Differential Revision: https://phabricator.services.mozilla.com/D210389 --- layout/base/AccessibleCaretManager.cpp | 2 +- layout/base/GeometryUtils.cpp | 11 ++++------ layout/base/PresShell.cpp | 9 +++------ layout/base/nsDocumentViewer.cpp | 28 +++++++++++--------------- layout/base/nsLayoutHistoryState.cpp | 3 +-- layout/base/nsLayoutUtils.cpp | 2 +- layout/base/nsPresContext.cpp | 2 +- layout/base/nsStyleSheetService.cpp | 2 +- 8 files changed, 24 insertions(+), 35 deletions(-) diff --git a/layout/base/AccessibleCaretManager.cpp b/layout/base/AccessibleCaretManager.cpp index 7c125cf023cc..a7e9c4314f5e 100644 --- a/layout/base/AccessibleCaretManager.cpp +++ b/layout/base/AccessibleCaretManager.cpp @@ -1436,7 +1436,7 @@ void AccessibleCaretManager::DispatchCaretStateChangedEvent( commonAncestorNode = sel->GetFrameSelection()->GetAncestorLimiter(); } - RefPtr domRect = new DOMRect(ToSupports(doc)); + auto domRect = MakeRefPtr(ToSupports(doc)); nsRect rect = nsLayoutUtils::GetSelectionBoundingRect(sel); nsIFrame* commonAncestorFrame = nullptr; diff --git a/layout/base/GeometryUtils.cpp b/layout/base/GeometryUtils.cpp index dac899f7558b..0ec6ee3eb37c 100644 --- a/layout/base/GeometryUtils.cpp +++ b/layout/base/GeometryUtils.cpp @@ -455,8 +455,7 @@ already_AddRefed ConvertQuadFromNode( if (aRv.Failed()) { return nullptr; } - RefPtr result = new DOMQuad(aTo->GetParentObject().mObject, points); - return result.forget(); + return MakeAndAddRef(aTo->GetParentObject().mObject, points); } already_AddRefed ConvertRectFromNode( @@ -473,8 +472,7 @@ already_AddRefed ConvertRectFromNode( if (aRv.Failed()) { return nullptr; } - RefPtr result = new DOMQuad(aTo->GetParentObject().mObject, points); - return result.forget(); + return MakeAndAddRef(aTo->GetParentObject().mObject, points); } already_AddRefed ConvertPointFromNode( @@ -490,9 +488,8 @@ already_AddRefed ConvertPointFromNode( if (aRv.Failed()) { return nullptr; } - RefPtr result = - new DOMPoint(aTo->GetParentObject().mObject, point.x, point.y); - return result.forget(); + return MakeAndAddRef(aTo->GetParentObject().mObject, point.x, + point.y); } } // namespace mozilla diff --git a/layout/base/PresShell.cpp b/layout/base/PresShell.cpp index 9104431ca02e..a3a36c7473aa 100644 --- a/layout/base/PresShell.cpp +++ b/layout/base/PresShell.cpp @@ -5700,8 +5700,7 @@ void PresShell::SynthesizeMouseMove(bool aFromScroll) { } if (!mSynthMouseMoveEvent.IsPending()) { - RefPtr ev = - new nsSynthMouseMoveEvent(this, aFromScroll); + auto ev = MakeRefPtr(this, aFromScroll); GetPresContext()->RefreshDriver()->AddRefreshObserver( ev, FlushType::Display, "Synthetic mouse move event"); @@ -10500,11 +10499,10 @@ bool PresShell::VerifyIncrementalReflow() { } // Create a presentation context to view the new frame tree - RefPtr cx = new nsRootPresContext( + auto cx = MakeRefPtr( mDocument, mPresContext->IsPaginated() ? nsPresContext::eContext_PrintPreview : nsPresContext::eContext_Galley); - NS_ENSURE_TRUE(cx, false); nsDeviceContext* dc = mPresContext->DeviceContext(); nsresult rv = cx->Init(dc); @@ -10516,8 +10514,7 @@ bool PresShell::VerifyIncrementalReflow() { nsIWidget* parentWidget = rootView->GetWidget(); // Create a new view manager. - RefPtr vm = new nsViewManager(); - NS_ENSURE_TRUE(vm, false); + auto vm = MakeRefPtr(); rv = vm->Init(dc); NS_ENSURE_SUCCESS(rv, false); diff --git a/layout/base/nsDocumentViewer.cpp b/layout/base/nsDocumentViewer.cpp index da0835e416cb..c0b5f218b536 100644 --- a/layout/base/nsDocumentViewer.cpp +++ b/layout/base/nsDocumentViewer.cpp @@ -483,8 +483,7 @@ class nsDocumentShownDispatcher : public Runnable { //------------------------------------------------------------------ already_AddRefed NS_NewDocumentViewer() { - RefPtr viewer = new nsDocumentViewer(); - return viewer.forget(); + return MakeAndAddRef(); } void nsDocumentViewer::PrepareToStartLoad() { @@ -1254,8 +1253,7 @@ nsDocumentViewer::DispatchBeforeUnload() { // Now, fire an BeforeUnload event to the document and see if it's ok // to unload... nsPresContext* presContext = mDocument->GetPresContext(); - RefPtr event = - new BeforeUnloadEvent(mDocument, presContext, nullptr); + auto event = MakeRefPtr(mDocument, presContext, nullptr); event->InitEvent(u"beforeunload"_ns, false, true); // Dispatching to |window|, but using |document| as the target. @@ -2094,8 +2092,7 @@ nsDocumentViewer::Show() { // Notify observers that a new page has been shown. This will get run // from the event loop after we actually draw the page. - RefPtr event = - new nsDocumentShownDispatcher(document); + auto event = MakeRefPtr(document); document->Dispatch(event.forget()); return NS_OK; @@ -2888,10 +2885,10 @@ nsDocumentViewer::Print(nsIPrintSettings* aPrintSettings, // earlier in this function. // TODO(dholbert) Do we need to bother with this stack-owned local RefPtr? // (Is there an edge case where it's needed to keep the nsPrintJob alive?) - RefPtr printJob = - new nsPrintJob(*this, *mContainer, *mDocument, - float(AppUnitsPerCSSInch()) / - float(mDeviceContext->AppUnitsPerDevPixel())); + auto printJob = + MakeRefPtr(*this, *mContainer, *mDocument, + float(AppUnitsPerCSSInch()) / + float(mDeviceContext->AppUnitsPerDevPixel())); mPrintJob = printJob; nsresult rv = printJob->Print(*mDocument, aPrintSettings, aRemotePrintJob, @@ -2931,10 +2928,10 @@ nsDocumentViewer::PrintPreview(nsIPrintSettings* aPrintSettings, // in this function. // TODO(dholbert) Do we need to bother with this stack-owned local RefPtr? // (Is there an edge case where it's needed to keep the nsPrintJob alive?) - RefPtr printJob = - new nsPrintJob(*this, *mContainer, *doc, - float(AppUnitsPerCSSInch()) / - float(mDeviceContext->AppUnitsPerDevPixel())); + auto printJob = + MakeRefPtr(*this, *mContainer, *doc, + float(AppUnitsPerCSSInch()) / + float(mDeviceContext->AppUnitsPerDevPixel())); mPrintJob = printJob; nsresult rv = printJob->PrintPreview( @@ -3373,8 +3370,7 @@ NS_IMETHODIMP nsDocumentViewer::SetPrintSettingsForSubdocument( return NS_ERROR_NOT_AVAILABLE; } - RefPtr devspec = - new nsDeviceContextSpecProxy(aRemotePrintJob); + auto devspec = MakeRefPtr(aRemotePrintJob); nsresult rv = devspec->Init(aPrintSettings, /* aIsPrintPreview = */ true); NS_ENSURE_SUCCESS(rv, rv); diff --git a/layout/base/nsLayoutHistoryState.cpp b/layout/base/nsLayoutHistoryState.cpp index f79545fb3b3e..b0f5275af808 100644 --- a/layout/base/nsLayoutHistoryState.cpp +++ b/layout/base/nsLayoutHistoryState.cpp @@ -34,8 +34,7 @@ class nsLayoutHistoryState final : public nsILayoutHistoryState, }; already_AddRefed NS_NewLayoutHistoryState() { - RefPtr state = new nsLayoutHistoryState(); - return state.forget(); + return MakeAndAddRef(); } NS_IMPL_ISUPPORTS(nsLayoutHistoryState, nsILayoutHistoryState, diff --git a/layout/base/nsLayoutUtils.cpp b/layout/base/nsLayoutUtils.cpp index 24cfbc2e3abd..14e80c57e887 100644 --- a/layout/base/nsLayoutUtils.cpp +++ b/layout/base/nsLayoutUtils.cpp @@ -3652,7 +3652,7 @@ nsLayoutUtils::RectListBuilder::RectListBuilder(DOMRectList* aList) : mRectList(aList) {} void nsLayoutUtils::RectListBuilder::AddRect(const nsRect& aRect) { - RefPtr rect = new DOMRect(mRectList); + auto rect = MakeRefPtr(mRectList); rect->SetLayoutRect(aRect); mRectList->Append(std::move(rect)); diff --git a/layout/base/nsPresContext.cpp b/layout/base/nsPresContext.cpp index d19141b576f1..0c6bd0fbd6b7 100644 --- a/layout/base/nsPresContext.cpp +++ b/layout/base/nsPresContext.cpp @@ -2830,7 +2830,7 @@ void nsPresContext::NotifyContentfulPaint() { MOZ_ASSERT(!nowTime.IsNull(), "Most recent refresh timestamp should exist since we are in " "a refresh driver tick"); - RefPtr paintTiming = new PerformancePaintTiming( + auto paintTiming = MakeRefPtr( perf, u"first-contentful-paint"_ns, nowTime); perf->SetFCPTimingEntry(paintTiming); diff --git a/layout/base/nsStyleSheetService.cpp b/layout/base/nsStyleSheetService.cpp index 061e03fd9d6c..1c1a58f5548c 100644 --- a/layout/base/nsStyleSheetService.cpp +++ b/layout/base/nsStyleSheetService.cpp @@ -155,7 +155,7 @@ nsresult nsStyleSheetService::LoadAndRegisterSheetInternal( return NS_ERROR_INVALID_ARG; } - RefPtr loader = new css::Loader; + auto loader = MakeRefPtr(); auto result = loader->LoadSheetSync(aSheetURI, parsingMode, css::Loader::UseSystemPrincipal::Yes); if (result.isErr()) {