tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit cd5f760648eced05bd41edb619bc8cc6b412d1e0
parent 3039907ce143833ffefdf300ad0bc5560f9c418c
Author: Emilio Cobos Álvarez <emilio@crisal.io>
Date:   Fri, 17 Oct 2025 10:45:55 +0000

Bug 1994964 - Introduce PresShell::GetRootWidget(). r=tnikkel,layout-reviewers

Most callers of nsViewManager::GetRootWidget have a pres shell around
and don't need to care about views.

This is in preparation for bug 1994942, where we'll disconnect subdocs
from the view tree (and thus the view manager will not have the ability
to get a root widget).

Differential Revision: https://phabricator.services.mozilla.com/D269019

Diffstat:
Maccessible/android/SessionAccessibility.cpp | 7+------
Maccessible/base/nsCoreUtils.cpp | 4+---
Maccessible/generic/DocAccessible.cpp | 10+++-------
Maccessible/generic/LocalAccessible.cpp | 2+-
Maccessible/windows/msaa/MsaaAccessible.cpp | 17++++++++---------
Mdom/base/nsContentSink.cpp | 4+---
Mdom/base/nsFocusManager.cpp | 16+++++-----------
Mdom/base/nsGlobalWindowOuter.cpp | 21++++-----------------
Mdom/events/TouchEvent.cpp | 6++----
Mdom/ipc/BrowserParent.cpp | 2+-
Mdom/view-transitions/ViewTransition.cpp | 3+--
Mlayout/base/PresShell.cpp | 7+++++--
Mlayout/base/PresShell.h | 3+++
Mlayout/base/TouchManager.cpp | 10+++-------
Mlayout/base/nsLayoutUtils.cpp | 5++---
Mlayout/base/nsPresContext.cpp | 7+------
Mlayout/generic/nsIFrame.cpp | 16++++++----------
Mlayout/xul/nsXULPopupManager.cpp | 11+++++------
Mwidget/android/nsDragService.cpp | 7+------
Mwidget/gtk/nsDragService.cpp | 9++++-----
Mwidget/nsBaseDragService.cpp | 7++-----
21 files changed, 60 insertions(+), 114 deletions(-)

diff --git a/accessible/android/SessionAccessibility.cpp b/accessible/android/SessionAccessibility.cpp @@ -388,12 +388,7 @@ RefPtr<SessionAccessibility> SessionAccessibility::GetInstanceFor( return nullptr; } - nsViewManager* vm = aPresShell->GetViewManager(); - if (!vm) { - return nullptr; - } - - nsCOMPtr<nsIWidget> rootWidget = vm->GetRootWidget(); + nsCOMPtr<nsIWidget> rootWidget = aPresShell->GetRootWidget(); // `rootWidget` can be one of several types. Here we make sure it is an // android nsWindow. if (RefPtr<nsWindow> window = nsWindow::From(rootWidget)) { diff --git a/accessible/base/nsCoreUtils.cpp b/accessible/base/nsCoreUtils.cpp @@ -107,9 +107,7 @@ void nsCoreUtils::DispatchClickEvent(XULTreeElement* aTree, int32_t aRowIndex, nsIFrame* rootFrame = presShell->GetRootFrame(); nsPoint offset; - nsCOMPtr<nsIWidget> rootWidget = - rootFrame->GetView()->GetNearestWidget(&offset); - + nsCOMPtr<nsIWidget> rootWidget = rootFrame->GetNearestWidget(offset); RefPtr<nsPresContext> presContext = presShell->GetPresContext(); int32_t cnvdX = presContext->CSSPixelsToDevPixels(tcX + int32_t(rect.x) + 1) + diff --git a/accessible/generic/DocAccessible.cpp b/accessible/generic/DocAccessible.cpp @@ -1184,13 +1184,9 @@ void* DocAccessible::GetNativeWindow() const { if (!mPresShell) { return nullptr; } - - nsViewManager* vm = mPresShell->GetViewManager(); - if (!vm) return nullptr; - - nsCOMPtr<nsIWidget> widget = vm->GetRootWidget(); - if (widget) return widget->GetNativeData(NS_NATIVE_WINDOW); - + if (nsIWidget* widget = mPresShell->GetRootWidget()) { + return widget->GetNativeData(NS_NATIVE_WINDOW); + } return nullptr; } diff --git a/accessible/generic/LocalAccessible.cpp b/accessible/generic/LocalAccessible.cpp @@ -497,7 +497,7 @@ LocalAccessible* LocalAccessible::LocalChildAtPoint( nsIFrame* startFrame = rootFrame; // Check whether the point is at popup content. - nsIWidget* rootWidget = rootFrame->GetView()->GetNearestWidget(nullptr); + nsIWidget* rootWidget = rootFrame->GetNearestWidget(); NS_ENSURE_TRUE(rootWidget, nullptr); LayoutDeviceIntRect rootRect = rootWidget->GetScreenBounds(); diff --git a/accessible/windows/msaa/MsaaAccessible.cpp b/accessible/windows/msaa/MsaaAccessible.cpp @@ -179,17 +179,16 @@ HWND MsaaAccessible::GetHWNDFor(Accessible* aAccessible) { // Popup lives in own windows, use its HWND until the popup window is // hidden to make old JAWS versions work with collapsed comboboxes (see // discussion in bug 379678). - nsIFrame* frame = localAcc->GetFrame(); - if (frame) { + if (nsIFrame* frame = localAcc->GetFrame()) { nsIWidget* widget = frame->GetNearestWidget(); if (widget && widget->IsVisible()) { - if (nsViewManager* vm = document->PresShellPtr()->GetViewManager()) { - nsCOMPtr<nsIWidget> rootWidget = vm->GetRootWidget(); - // Make sure the accessible belongs to popup. If not then use - // document HWND (which might be different from root widget in the - // case of window emulation). - if (rootWidget != widget) - return static_cast<HWND>(widget->GetNativeData(NS_NATIVE_WINDOW)); + nsCOMPtr<nsIWidget> rootWidget = + document->PresShellPtr()->GetRootWidget(); + // Make sure the accessible belongs to popup. If not then use + // document HWND (which might be different from root widget in the + // case of window emulation). + if (rootWidget != widget) { + return static_cast<HWND>(widget->GetNativeData(NS_NATIVE_WINDOW)); } } } diff --git a/dom/base/nsContentSink.cpp b/dom/base/nsContentSink.cpp @@ -774,9 +774,7 @@ nsresult nsContentSink::DidProcessATokenImpl() { if (StaticPrefs::content_sink_pending_event_mode() != 0 && !mHasPendingEvent && (mDeflectedCount % StaticPrefs::content_sink_event_probe_rate()) == 0) { - nsViewManager* vm = presShell->GetViewManager(); - NS_ENSURE_TRUE(vm, NS_ERROR_FAILURE); - nsCOMPtr<nsIWidget> widget = vm->GetRootWidget(); + nsIWidget* widget = presShell->GetRootWidget(); mHasPendingEvent = widget && widget->HasPendingInputEvent(); } diff --git a/dom/base/nsFocusManager.cpp b/dom/base/nsFocusManager.cpp @@ -1489,11 +1489,7 @@ void nsFocusManager::EnsureCurrentWidgetFocused(CallerType aCallerType) { if (!presShell) { return; } - nsViewManager* vm = presShell->GetViewManager(); - if (!vm) { - return; - } - nsCOMPtr<nsIWidget> widget = vm->GetRootWidget(); + nsCOMPtr<nsIWidget> widget = presShell->GetRootWidget(); if (!widget) { return; } @@ -2723,12 +2719,10 @@ void nsFocusManager::Focus( SetFocusedWindowInternal(aWindow, aActionId); if (aAdjustWidget && !sTestMode) { - if (nsViewManager* vm = presShell->GetViewManager()) { - if (nsCOMPtr<nsIWidget> widget = vm->GetRootWidget()) { - widget->SetFocus(nsIWidget::Raise::No, aFlags & FLAG_NONSYSTEMCALLER - ? CallerType::NonSystem - : CallerType::System); - } + if (nsCOMPtr<nsIWidget> widget = presShell->GetRootWidget()) { + widget->SetFocus(nsIWidget::Raise::No, aFlags & FLAG_NONSYSTEMCALLER + ? CallerType::NonSystem + : CallerType::System); } } diff --git a/dom/base/nsGlobalWindowOuter.cpp b/dom/base/nsGlobalWindowOuter.cpp @@ -3889,7 +3889,7 @@ nsIWidget* nsGlobalWindowOuter::GetNearestWidget() const { if (!rootFrame) { return nullptr; } - return rootFrame->GetView()->GetNearestWidget(nullptr); + return rootFrame->GetNearestWidget(); } void nsGlobalWindowOuter::SetFullscreenOuter(bool aFullscreen, @@ -6139,8 +6139,7 @@ nsGlobalWindowOuter* nsGlobalWindowOuter::EnterModalState() { do_GetService("@mozilla.org/widget/dragservice;1"); if (ds && topWin->GetDocShell()) { if (PresShell* presShell = topWin->GetDocShell()->GetPresShell()) { - if (nsViewManager* vm = presShell->GetViewManager()) { - RefPtr<nsIWidget> widget = vm->GetRootWidget(); + if (RefPtr<nsIWidget> widget = presShell->GetRootWidget()) { if (nsCOMPtr<nsIDragSession> session = ds->GetCurrentSession(widget)) { session->EndDragSession(true, 0); } @@ -7163,26 +7162,14 @@ void nsGlobalWindowOuter::SetCursorOuter(const nsACString& aCursor, } if (presContext) { - // Need root widget. PresShell* presShell = mDocShell->GetPresShell(); if (!presShell) { aError.Throw(NS_ERROR_FAILURE); return; } - nsViewManager* vm = presShell->GetViewManager(); - if (!vm) { - aError.Throw(NS_ERROR_FAILURE); - return; - } - - nsView* rootView = vm->GetRootView(); - if (!rootView) { - aError.Throw(NS_ERROR_FAILURE); - return; - } - - nsIWidget* widget = rootView->GetNearestWidget(nullptr); + // Need root widget. + nsIWidget* widget = presShell->GetRootWidget(); if (!widget) { aError.Throw(NS_ERROR_FAILURE); return; diff --git a/dom/events/TouchEvent.cpp b/dom/events/TouchEvent.cpp @@ -248,10 +248,8 @@ bool TouchEvent::PrefEnabled(nsIDocShell* aDocShell) { if (enabled && aDocShell) { // APZ might be disabled on this particular widget, in which case // TouchEvent support will also be disabled. Try to detect that. - RefPtr<nsPresContext> pc = aDocShell->GetPresContext(); - if (pc) { - nsCOMPtr<nsIWidget> widget = pc->GetRootWidget(); - if (widget) { + if (RefPtr<nsPresContext> pc = aDocShell->GetPresContext()) { + if (nsCOMPtr<nsIWidget> widget = pc->GetRootWidget()) { enabled &= widget->AsyncPanZoomEnabled(); } } diff --git a/dom/ipc/BrowserParent.cpp b/dom/ipc/BrowserParent.cpp @@ -463,7 +463,7 @@ already_AddRefed<nsPIDOMWindowOuter> BrowserParent::GetParentWindowOuter() { already_AddRefed<nsIWidget> BrowserParent::GetTopLevelWidget() { if (RefPtr<Element> element = mFrameElement) { if (PresShell* presShell = element->OwnerDoc()->GetPresShell()) { - return do_AddRef(presShell->GetViewManager()->GetRootWidget()); + return do_AddRef(presShell->GetRootWidget()); } } return nullptr; diff --git a/dom/view-transitions/ViewTransition.cpp b/dom/view-transitions/ViewTransition.cpp @@ -1354,8 +1354,7 @@ Maybe<SkipTransitionReason> ViewTransition::CaptureOldState() { ->GetPresShell()) { // Build a display list and send it to WR in order to perform the // capturing of old content. - RefPtr<nsViewManager> vm = ps->GetViewManager(); - if (RefPtr widget = vm->GetRootWidget()) { + if (RefPtr widget = ps->GetRootWidget()) { VT_LOG("ViewTransitions::CaptureOldState(), requesting composite"); ps->PaintAndRequestComposite(ps->GetRootFrame(), widget->GetWindowRenderer(), diff --git a/layout/base/PresShell.cpp b/layout/base/PresShell.cpp @@ -9975,8 +9975,7 @@ bool PresShell::EventHandler::AdjustContextMenuKeyEvent( nsRootPresContext* rootPC = GetPresContext()->GetRootPresContext(); aMouseEvent->mRefPoint = LayoutDeviceIntPoint(0, 0); if (rootPC) { - aMouseEvent->mWidget = - rootPC->PresShell()->GetViewManager()->GetRootWidget(); + aMouseEvent->mWidget = rootPC->PresShell()->GetRootWidget(); if (aMouseEvent->mWidget) { // default the refpoint to the topleft of our document nsPoint offset(0, 0); @@ -12158,6 +12157,10 @@ void PresShell::UpdateImageLockingState() { } } +nsIWidget* PresShell::GetRootWidget() const { + return mViewManager ? mViewManager->GetRootWidget() : nullptr; +} + PresShell* PresShell::GetRootPresShell() const { if (mPresContext) { nsPresContext* rootPresContext = mPresContext->GetRootPresContext(); diff --git a/layout/base/PresShell.h b/layout/base/PresShell.h @@ -453,6 +453,9 @@ class PresShell final : public nsStubDocumentObserver, */ nsIFrame* GetRootFrame() const { return mFrameConstructor->GetRootFrame(); } + // Return the closest root widget (widget owned by a root frame). + nsIWidget* GetRootWidget() const; + /** * Get root scroll container frame from the frame constructor. */ diff --git a/layout/base/TouchManager.cpp b/layout/base/TouchManager.cpp @@ -74,13 +74,9 @@ void TouchManager::EvictTouchPoint(RefPtr<Touch>& aTouch, if (node) { Document* doc = node->GetComposedDoc(); if (doc && (!aLimitToDocument || aLimitToDocument == doc)) { - PresShell* presShell = doc->GetPresShell(); - if (presShell) { - nsIFrame* frame = presShell->GetRootFrame(); - if (frame) { - nsCOMPtr<nsIWidget> widget = - frame->GetView()->GetNearestWidget(nullptr); - if (widget) { + if (PresShell* presShell = doc->GetPresShell()) { + if (nsIFrame* frame = presShell->GetRootFrame()) { + if (nsCOMPtr<nsIWidget> widget = frame->GetNearestWidget()) { WidgetTouchEvent event(true, eTouchEnd, widget); event.mTouches.AppendElement(aTouch); nsEventStatus status; diff --git a/layout/base/nsLayoutUtils.cpp b/layout/base/nsLayoutUtils.cpp @@ -1499,10 +1499,9 @@ nsPoint GetEventCoordinatesRelativeTo(nsIWidget* aWidget, return nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE); } - nsView* view = frame->GetView(); - if (view) { + if (nsView* view = frame->GetView()) { nsIWidget* frameWidget = view->GetWidget(); - if (frameWidget && frameWidget == aWidget) { + if (frameWidget == aWidget) { // Special case this cause it happens a lot. // This also fixes bug 664707, events in the extra-special case of select // dropdown popups that are transformed. diff --git a/layout/base/nsPresContext.cpp b/layout/base/nsPresContext.cpp @@ -1181,12 +1181,7 @@ nsIWidget* nsPresContext::GetNearestWidget(nsPoint* aOffset) { } nsIWidget* nsPresContext::GetRootWidget() const { - NS_ENSURE_TRUE(mPresShell, nullptr); - nsViewManager* vm = mPresShell->GetViewManager(); - if (!vm) { - return nullptr; - } - return vm->GetRootWidget(); + return mPresShell ? mPresShell->GetRootWidget() : nullptr; } // We may want to replace this with something faster, maybe caching the root diff --git a/layout/generic/nsIFrame.cpp b/layout/generic/nsIFrame.cpp @@ -7903,14 +7903,10 @@ nsRect nsIFrame::GetScreenRectInAppUnits() const { parentScreenRectAppUnits.TopLeft() + rootFrameOffsetInParent; rootScreenPos.x = NS_round(parentScale * rootPt.x); rootScreenPos.y = NS_round(parentScale * rootPt.y); - } else { - nsCOMPtr<nsIWidget> rootWidget = - presContext->PresShell()->GetViewManager()->GetRootWidget(); - if (rootWidget) { - LayoutDeviceIntPoint rootDevPx = rootWidget->WidgetToScreenOffset(); - rootScreenPos.x = presContext->DevPixelsToAppUnits(rootDevPx.x); - rootScreenPos.y = presContext->DevPixelsToAppUnits(rootDevPx.y); - } + } else if (nsCOMPtr<nsIWidget> rootWidget = presContext->GetRootWidget()) { + LayoutDeviceIntPoint rootDevPx = rootWidget->WidgetToScreenOffset(); + rootScreenPos.x = presContext->DevPixelsToAppUnits(rootDevPx.x); + rootScreenPos.y = presContext->DevPixelsToAppUnits(rootDevPx.y); } return nsRect(rootScreenPos + GetOffsetTo(rootFrame), GetSize()); @@ -7939,7 +7935,7 @@ void nsIFrame::GetOffsetFromView(nsPoint& aOffset, nsView** aView) const { nsIWidget* nsIFrame::GetNearestWidget() const { if (!HasAnyStateBits(NS_FRAME_IN_POPUP)) { - return PresShell()->GetViewManager()->GetRootWidget(); + return PresContext()->GetRootWidget(); } nsPoint unused; return GetNearestWidget(unused); @@ -7975,7 +7971,7 @@ nsIWidget* nsIFrame::GetNearestWidget(nsPoint& aOffset) const { break; } } while (true); - return PresShell()->GetViewManager()->GetRootWidget(); + return PresContext()->GetRootWidget(); } Matrix4x4Flagged nsIFrame::GetTransformMatrix(ViewportType aViewportType, diff --git a/layout/xul/nsXULPopupManager.cpp b/layout/xul/nsXULPopupManager.cpp @@ -225,12 +225,12 @@ UniquePtr<nsMenuChainItem> nsMenuChainItem::Detach() { } void nsXULPopupManager::AddMenuChainItem(UniquePtr<nsMenuChainItem> aItem) { - PopupType popupType = aItem->Frame()->GetPopupType(); + auto* frame = aItem->Frame(); + PopupType popupType = frame->GetPopupType(); if (StaticPrefs::layout_cursor_disable_for_popups() && popupType != PopupType::Tooltip) { - if (nsPresContext* rootPC = - aItem->Frame()->PresContext()->GetRootPresContext()) { - if (nsCOMPtr<nsIWidget> rootWidget = rootPC->GetRootWidget()) { + if (auto* rootPc = frame->PresContext()->GetRootPresContext()) { + if (nsCOMPtr<nsIWidget> rootWidget = rootPc->GetRootWidget()) { rootWidget->SetCustomCursorAllowed(false); } } @@ -1751,8 +1751,7 @@ nsEventStatus nsXULPopupManager::FirePopupShowingEvent( // coordinates are relative to the root widget nsPresContext* rootPresContext = aPresContext->GetRootPresContext(); if (rootPresContext) { - event.mWidget = - rootPresContext->PresShell()->GetViewManager()->GetRootWidget(); + event.mWidget = rootPresContext->GetRootWidget(); } else { event.mWidget = nullptr; } diff --git a/widget/android/nsDragService.cpp b/widget/android/nsDragService.cpp @@ -52,12 +52,7 @@ static nsWindow* GetWindow(dom::Document* aDocument) { return nullptr; } - RefPtr<nsViewManager> vm = presShell->GetViewManager(); - if (!vm) { - return nullptr; - } - - nsCOMPtr<nsIWidget> widget = vm->GetRootWidget(); + nsCOMPtr<nsIWidget> widget = presShell->GetRootWidget(); if (!widget) { return nullptr; } diff --git a/widget/gtk/nsDragService.cpp b/widget/gtk/nsDragService.cpp @@ -730,11 +730,10 @@ static GtkWindow* GetGtkWindow(dom::Document* aDocument) { return nullptr; } - RefPtr<nsViewManager> vm = presShell->GetViewManager(); - if (!vm) return nullptr; - - nsCOMPtr<nsIWidget> widget = vm->GetRootWidget(); - if (!widget) return nullptr; + nsCOMPtr<nsIWidget> widget = presShell->GetRootWidget(); + if (!widget) { + return nullptr; + } GtkWidget* gtkWidget = static_cast<nsWindow*>(widget.get())->GetGtkWidget(); if (!gtkWidget) return nullptr; diff --git a/widget/nsBaseDragService.cpp b/widget/nsBaseDragService.cpp @@ -1351,8 +1351,7 @@ void nsBaseDragSession::TakeSessionBrowserListFromService() { /* static */ nsIWidget* nsBaseDragService::GetWidgetFromWidgetProvider( nsISupports* aWidgetProvider) { - nsCOMPtr<nsIWidget> widget = do_QueryObject(aWidgetProvider); - if (widget) { + if (nsCOMPtr<nsIWidget> widget = do_QueryObject(aWidgetProvider)) { return widget; } @@ -1374,9 +1373,7 @@ nsIWidget* nsBaseDragService::GetWidgetFromWidgetProvider( NS_ENSURE_TRUE(docShell, nullptr); PresShell* presShell = docShell->GetPresShell(); NS_ENSURE_TRUE(presShell, nullptr); - nsViewManager* vm = presShell->GetViewManager(); - NS_ENSURE_TRUE(vm, nullptr); - return vm->GetRootWidget(); + return presShell->GetRootWidget(); } NS_IMETHODIMP