Bug 1733465 part 2: Modernize nsViewManager::GetRootWidget() to use already_AddRefed return value, instead of requiring getter_AddRefs at callsite. r=tnikkel

This patch shouldn't change behavior at all.

This patch replaces a manual NS_ADDREF call with typesafe code that manages the
reference count for us.  This reduces repeated boilerplate code, in the
implementation as well as the callsites.

Differential Revision: https://phabricator.services.mozilla.com/D127179
This commit is contained in:
Daniel Holbert 2021-10-01 20:38:09 +00:00
parent bfb72ec419
commit ed93bd5007
13 changed files with 36 additions and 47 deletions

View File

@ -182,8 +182,7 @@ RefPtr<SessionAccessibility> SessionAccessibility::GetInstanceFor(
return nullptr; return nullptr;
} }
nsCOMPtr<nsIWidget> rootWidget; nsCOMPtr<nsIWidget> rootWidget = vm->GetRootWidget();
vm->GetRootWidget(getter_AddRefs(rootWidget));
// `rootWidget` can be one of several types. Here we make sure it is an // `rootWidget` can be one of several types. Here we make sure it is an
// android nsWindow. // android nsWindow.
if (RefPtr<nsWindow> window = nsWindow::From(rootWidget)) { if (RefPtr<nsWindow> window = nsWindow::From(rootWidget)) {

View File

@ -932,8 +932,7 @@ void* DocAccessible::GetNativeWindow() const {
nsViewManager* vm = mPresShell->GetViewManager(); nsViewManager* vm = mPresShell->GetViewManager();
if (!vm) return nullptr; if (!vm) return nullptr;
nsCOMPtr<nsIWidget> widget; nsCOMPtr<nsIWidget> widget = vm->GetRootWidget();
vm->GetRootWidget(getter_AddRefs(widget));
if (widget) return widget->GetNativeData(NS_NATIVE_WINDOW); if (widget) return widget->GetNativeData(NS_NATIVE_WINDOW);
return nullptr; return nullptr;

View File

@ -251,8 +251,7 @@ HWND MsaaAccessible::GetHWNDFor(Accessible* aAccessible) {
nsIWidget* widget = frame->GetNearestWidget(); nsIWidget* widget = frame->GetNearestWidget();
if (widget && widget->IsVisible()) { if (widget && widget->IsVisible()) {
if (nsViewManager* vm = document->PresShellPtr()->GetViewManager()) { if (nsViewManager* vm = document->PresShellPtr()->GetViewManager()) {
nsCOMPtr<nsIWidget> rootWidget; nsCOMPtr<nsIWidget> rootWidget = vm->GetRootWidget();
vm->GetRootWidget(getter_AddRefs(rootWidget));
// Make sure the accessible belongs to popup. If not then use // Make sure the accessible belongs to popup. If not then use
// document HWND (which might be different from root widget in the // document HWND (which might be different from root widget in the
// case of window emulation). // case of window emulation).

View File

@ -1032,8 +1032,7 @@ nsresult nsContentSink::DidProcessATokenImpl() {
(mDeflectedCount % StaticPrefs::content_sink_event_probe_rate()) == 0) { (mDeflectedCount % StaticPrefs::content_sink_event_probe_rate()) == 0) {
nsViewManager* vm = presShell->GetViewManager(); nsViewManager* vm = presShell->GetViewManager();
NS_ENSURE_TRUE(vm, NS_ERROR_FAILURE); NS_ENSURE_TRUE(vm, NS_ERROR_FAILURE);
nsCOMPtr<nsIWidget> widget; nsCOMPtr<nsIWidget> widget = vm->GetRootWidget();
vm->GetRootWidget(getter_AddRefs(widget));
mHasPendingEvent = widget && widget->HasPendingInputEvent(); mHasPendingEvent = widget && widget->HasPendingInputEvent();
} }

View File

@ -1370,8 +1370,7 @@ void nsFocusManager::EnsureCurrentWidgetFocused(CallerType aCallerType) {
if (!vm) { if (!vm) {
return; return;
} }
nsCOMPtr<nsIWidget> widget; nsCOMPtr<nsIWidget> widget = vm->GetRootWidget();
vm->GetRootWidget(getter_AddRefs(widget));
if (!widget) { if (!widget) {
return; return;
} }
@ -2581,8 +2580,7 @@ void nsFocusManager::Focus(
if (aAdjustWidget && !sTestMode) { if (aAdjustWidget && !sTestMode) {
if (nsViewManager* vm = presShell->GetViewManager()) { if (nsViewManager* vm = presShell->GetViewManager()) {
nsCOMPtr<nsIWidget> widget; nsCOMPtr<nsIWidget> widget = vm->GetRootWidget();
vm->GetRootWidget(getter_AddRefs(widget));
if (widget) if (widget)
widget->SetFocus(nsIWidget::Raise::No, aFlags & FLAG_NONSYSTEMCALLER widget->SetFocus(nsIWidget::Raise::No, aFlags & FLAG_NONSYSTEMCALLER
? CallerType::NonSystem ? CallerType::NonSystem
@ -2966,8 +2964,7 @@ void nsFocusManager::RaiseWindow(nsPIDOMWindowOuter* aWindow,
} }
if (nsViewManager* vm = presShell->GetViewManager()) { if (nsViewManager* vm = presShell->GetViewManager()) {
nsCOMPtr<nsIWidget> widget; nsCOMPtr<nsIWidget> widget = vm->GetRootWidget();
vm->GetRootWidget(getter_AddRefs(widget));
if (widget) { if (widget) {
widget->SetFocus(nsIWidget::Raise::Yes, aCallerType); widget->SetFocus(nsIWidget::Raise::Yes, aCallerType);
} }

View File

@ -349,10 +349,7 @@ already_AddRefed<nsPIDOMWindowOuter> BrowserParent::GetParentWindowOuter() {
already_AddRefed<nsIWidget> BrowserParent::GetTopLevelWidget() { already_AddRefed<nsIWidget> BrowserParent::GetTopLevelWidget() {
if (RefPtr<Element> element = mFrameElement) { if (RefPtr<Element> element = mFrameElement) {
if (PresShell* presShell = element->OwnerDoc()->GetPresShell()) { if (PresShell* presShell = element->OwnerDoc()->GetPresShell()) {
nsViewManager* vm = presShell->GetViewManager(); return presShell->GetViewManager()->GetRootWidget();
nsCOMPtr<nsIWidget> widget;
vm->GetRootWidget(getter_AddRefs(widget));
return widget.forget();
} }
} }
return nullptr; return nullptr;

View File

@ -8837,9 +8837,8 @@ bool PresShell::EventHandler::AdjustContextMenuKeyEvent(
nsRootPresContext* rootPC = GetPresContext()->GetRootPresContext(); nsRootPresContext* rootPC = GetPresContext()->GetRootPresContext();
aMouseEvent->mRefPoint = LayoutDeviceIntPoint(0, 0); aMouseEvent->mRefPoint = LayoutDeviceIntPoint(0, 0);
if (rootPC) { if (rootPC) {
rootPC->PresShell()->GetViewManager()->GetRootWidget( aMouseEvent->mWidget =
getter_AddRefs(aMouseEvent->mWidget)); rootPC->PresShell()->GetViewManager()->GetRootWidget();
if (aMouseEvent->mWidget) { if (aMouseEvent->mWidget) {
// default the refpoint to the topleft of our document // default the refpoint to the topleft of our document
nsPoint offset(0, 0); nsPoint offset(0, 0);

View File

@ -1081,8 +1081,14 @@ nsIWidget* nsPresContext::GetRootWidget() const {
if (!vm) { if (!vm) {
return nullptr; return nullptr;
} }
nsCOMPtr<nsIWidget> widget;
vm->GetRootWidget(getter_AddRefs(widget)); // XXXdholbert REVIEW NOTE: It's kind of sketchy that we're returning a raw
// pointer to a refcounted object here, when we've got an owning reference
// which we release just as we return the raw pointer. Plus, it's wasteful
// to be incurring an AddRef/Release operation before the object actually
// even gets used (and potentially AddRef'ed again) by the caller. I'll be
// cleaning this up to address these issues in the next patch in this series.
nsCOMPtr<nsIWidget> widget = vm->GetRootWidget();
return widget.get(); return widget.get();
} }

View File

@ -2337,8 +2337,8 @@ already_AddRefed<ComputedStyle> nsIFrame::ComputeSelectionStyle(
template <typename SizeOrMaxSize> template <typename SizeOrMaxSize>
static inline bool IsIntrinsicKeyword(const SizeOrMaxSize& aSize) { static inline bool IsIntrinsicKeyword(const SizeOrMaxSize& aSize) {
// All keywords other than auto/none/-moz-available depend on intrinsic sizes. // All keywords other than auto/none/-moz-available depend on intrinsic sizes.
return aSize.IsMaxContent() || aSize.IsMinContent() || return aSize.IsMaxContent() || aSize.IsMinContent() || aSize.IsFitContent() ||
aSize.IsFitContent() || aSize.IsFitContentFunction(); aSize.IsFitContentFunction();
} }
bool nsIFrame::CanBeDynamicReflowRoot() const { bool nsIFrame::CanBeDynamicReflowRoot() const {
@ -6973,9 +6973,8 @@ nsRect nsIFrame::GetScreenRectInAppUnits() const {
rootScreenPos.x = NS_round(parentScale * rootPt.x); rootScreenPos.x = NS_round(parentScale * rootPt.x);
rootScreenPos.y = NS_round(parentScale * rootPt.y); rootScreenPos.y = NS_round(parentScale * rootPt.y);
} else { } else {
nsCOMPtr<nsIWidget> rootWidget; nsCOMPtr<nsIWidget> rootWidget =
presContext->PresShell()->GetViewManager()->GetRootWidget( presContext->PresShell()->GetViewManager()->GetRootWidget();
getter_AddRefs(rootWidget));
if (rootWidget) { if (rootWidget) {
LayoutDeviceIntPoint rootDevPx = rootWidget->WidgetToScreenOffset(); LayoutDeviceIntPoint rootDevPx = rootWidget->WidgetToScreenOffset();
rootScreenPos.x = presContext->DevPixelsToAppUnits(rootDevPx.x); rootScreenPos.x = presContext->DevPixelsToAppUnits(rootDevPx.x);

View File

@ -1527,8 +1527,8 @@ nsEventStatus nsXULPopupManager::FirePopupShowingEvent(
// coordinates are relative to the root widget // coordinates are relative to the root widget
nsPresContext* rootPresContext = aPresContext->GetRootPresContext(); nsPresContext* rootPresContext = aPresContext->GetRootPresContext();
if (rootPresContext) { if (rootPresContext) {
rootPresContext->PresShell()->GetViewManager()->GetRootWidget( event.mWidget =
getter_AddRefs(event.mWidget)); rootPresContext->PresShell()->GetViewManager()->GetRootWidget();
} else { } else {
event.mWidget = nullptr; event.mWidget = nullptr;
} }

View File

@ -903,21 +903,16 @@ void nsViewManager::DecrementDisableRefreshCount() {
NS_ASSERTION(mRefreshDisableCount >= 0, "Invalid refresh disable count!"); NS_ASSERTION(mRefreshDisableCount >= 0, "Invalid refresh disable count!");
} }
void nsViewManager::GetRootWidget(nsIWidget** aWidget) { already_AddRefed<nsIWidget> nsViewManager::GetRootWidget() {
if (!mRootView) { nsCOMPtr<nsIWidget> rootWidget;
*aWidget = nullptr; if (mRootView) {
return; if (mRootView->HasWidget()) {
rootWidget = mRootView->GetWidget();
} else if (mRootView->GetParent()) {
rootWidget = mRootView->GetParent()->GetViewManager()->GetRootWidget();
}
} }
if (mRootView->HasWidget()) { return rootWidget.forget();
*aWidget = mRootView->GetWidget();
NS_ADDREF(*aWidget);
return;
}
if (mRootView->GetParent()) {
mRootView->GetParent()->GetViewManager()->GetRootWidget(aWidget);
return;
}
*aWidget = nullptr;
} }
LayoutDeviceIntRect nsViewManager::ViewToWidget(nsView* aView, LayoutDeviceIntRect nsViewManager::ViewToWidget(nsView* aView,

View File

@ -14,6 +14,7 @@
#include "nsDeviceContext.h" #include "nsDeviceContext.h"
#include "nsTArray.h" #include "nsTArray.h"
#include "mozilla/Attributes.h" #include "mozilla/Attributes.h"
#include "mozilla/AlreadyAddRefed.h"
#include "mozilla/EventForwards.h" #include "mozilla/EventForwards.h"
class nsIWidget; class nsIWidget;
@ -281,7 +282,7 @@ class nsViewManager final {
* Retrieve the widget at the root of the nearest enclosing * Retrieve the widget at the root of the nearest enclosing
* view manager whose root view has a widget. * view manager whose root view has a widget.
*/ */
void GetRootWidget(nsIWidget** aWidget); already_AddRefed<nsIWidget> GetRootWidget();
/** /**
* Indicate whether the viewmanager is currently painting * Indicate whether the viewmanager is currently painting

View File

@ -293,8 +293,7 @@ static GtkWindow* GetGtkWindow(dom::Document* aDocument) {
RefPtr<nsViewManager> vm = presShell->GetViewManager(); RefPtr<nsViewManager> vm = presShell->GetViewManager();
if (!vm) return nullptr; if (!vm) return nullptr;
nsCOMPtr<nsIWidget> widget; nsCOMPtr<nsIWidget> widget = vm->GetRootWidget();
vm->GetRootWidget(getter_AddRefs(widget));
if (!widget) return nullptr; if (!widget) return nullptr;
GtkWidget* gtkWidget = GtkWidget* gtkWidget =