diff --git a/accessible/windows/msaa/MsaaAccessible.cpp b/accessible/windows/msaa/MsaaAccessible.cpp index 3a88bc53cee6..640161e7ea97 100644 --- a/accessible/windows/msaa/MsaaAccessible.cpp +++ b/accessible/windows/msaa/MsaaAccessible.cpp @@ -251,8 +251,7 @@ HWND MsaaAccessible::GetHWNDFor(Accessible* aAccessible) { nsIWidget* widget = frame->GetNearestWidget(); if (widget && widget->IsVisible()) { if (nsViewManager* vm = document->PresShellPtr()->GetViewManager()) { - nsCOMPtr rootWidget; - vm->GetRootWidget(getter_AddRefs(rootWidget)); + nsCOMPtr 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). diff --git a/dom/events/IMEStateManager.cpp b/dom/events/IMEStateManager.cpp index 0624276906aa..7b5efe72f8f4 100644 --- a/dom/events/IMEStateManager.cpp +++ b/dom/events/IMEStateManager.cpp @@ -767,8 +767,12 @@ void IMEStateManager::OnClickInEditor(nsPresContext* aPresContext, nsCOMPtr widget(sWidget); - MOZ_ASSERT(!sPresContext->GetTextInputHandlingWidget() || - sPresContext->GetTextInputHandlingWidget() == widget); +#ifdef DEBUG + { + nsCOMPtr tihWidget = sPresContext->GetTextInputHandlingWidget(); + MOZ_ASSERT(!tihWidget || tihWidget == widget); + } +#endif // DEBUG if (!aMouseEvent->IsTrusted()) { MOZ_LOG(sISMLog, LogLevel::Debug, @@ -1001,8 +1005,12 @@ void IMEStateManager::UpdateIMEState(const IMEState& aNewIMEState, OwningNonNull widget(*sWidget); - MOZ_ASSERT(!sPresContext->GetTextInputHandlingWidget() || - sPresContext->GetTextInputHandlingWidget() == widget); +#ifdef DEBUG + { + nsCOMPtr tihWidget = sPresContext->GetTextInputHandlingWidget(); + MOZ_ASSERT(!tihWidget || tihWidget == widget); + } +#endif // DEBUG // TODO: Investigate if we could put off to initialize IMEContentObserver // later because a lot of callers need to be marked as @@ -1215,8 +1223,12 @@ void IMEStateManager::SetInputContextForChildProcess( nsCOMPtr widget(sWidget); - MOZ_ASSERT(!sPresContext->GetTextInputHandlingWidget() || - sPresContext->GetTextInputHandlingWidget() == widget); +#ifdef DEBUG + { + nsCOMPtr tihWidget = sPresContext->GetTextInputHandlingWidget(); + MOZ_ASSERT(!tihWidget || tihWidget == widget); + } +#endif // DEBUG MOZ_ASSERT(aInputContext.mOrigin == InputContext::ORIGIN_CONTENT); sActiveChildInputContext = aInputContext; @@ -1883,7 +1895,7 @@ nsresult IMEStateManager::NotifyIME(IMEMessage aMessage, return NS_ERROR_INVALID_ARG; } - nsIWidget* widget = aPresContext->GetTextInputHandlingWidget(); + nsCOMPtr widget = aPresContext->GetTextInputHandlingWidget(); if (NS_WARN_IF(!widget)) { MOZ_LOG(sISMLog, LogLevel::Error, (" NotifyIME(), FAILED due to no widget for the " @@ -2015,7 +2027,12 @@ void IMEStateManager::CreateIMEContentObserver(EditorBase& aEditorBase) { return; } - MOZ_ASSERT(sPresContext->GetTextInputHandlingWidget() == widget); +#ifdef DEBUG + { + nsCOMPtr tihWidget = sPresContext->GetTextInputHandlingWidget(); + MOZ_ASSERT(tihWidget == widget); + } +#endif // DEBUG MOZ_LOG(sISMLog, LogLevel::Debug, (" CreateIMEContentObserver() is creating an " diff --git a/dom/events/TextComposition.h b/dom/events/TextComposition.h index c8c7e30eb9ed..9005556f1b30 100644 --- a/dom/events/TextComposition.h +++ b/dom/events/TextComposition.h @@ -13,6 +13,7 @@ #include "nsTArray.h" #include "nsThreadUtils.h" #include "nsPresContext.h" +#include "mozilla/AlreadyAddRefed.h" #include "mozilla/Attributes.h" #include "mozilla/EventForwards.h" #include "mozilla/RangeBoundary.h" @@ -75,7 +76,7 @@ class TextComposition final { // error due to inaccessible Release() method. TextRangeArray* GetRanges() const { return mRanges; } // Returns the widget which is proper to call NotifyIME(). - nsIWidget* GetWidget() const { + already_AddRefed GetWidget() const { return mPresContext ? mPresContext->GetRootWidget() : nullptr; } // Returns the tab parent which has this composition in its remote process. diff --git a/dom/events/TouchEvent.cpp b/dom/events/TouchEvent.cpp index 587f410c557b..c132f99f1702 100644 --- a/dom/events/TouchEvent.cpp +++ b/dom/events/TouchEvent.cpp @@ -242,8 +242,11 @@ bool TouchEvent::PrefEnabled(nsIDocShell* aDocShell) { // APZ might be disabled on this particular widget, in which case // TouchEvent support will also be disabled. Try to detect that. RefPtr pc = aDocShell->GetPresContext(); - if (pc && pc->GetRootWidget()) { - enabled &= pc->GetRootWidget()->AsyncPanZoomEnabled(); + if (pc) { + nsCOMPtr widget = pc->GetRootWidget(); + if (widget) { + enabled &= widget->AsyncPanZoomEnabled(); + } } } #endif diff --git a/layout/base/nsPresContext.cpp b/layout/base/nsPresContext.cpp index e470194da2f9..db2a891c9e9f 100644 --- a/layout/base/nsPresContext.cpp +++ b/layout/base/nsPresContext.cpp @@ -1075,21 +1075,14 @@ nsIWidget* nsPresContext::GetNearestWidget(nsPoint* aOffset) { return rootView->GetNearestWidget(aOffset); } -nsIWidget* nsPresContext::GetRootWidget() const { +already_AddRefed nsPresContext::GetRootWidget() const { NS_ENSURE_TRUE(mPresShell, nullptr); nsViewManager* vm = mPresShell->GetViewManager(); if (!vm) { return nullptr; } - // 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 widget = vm->GetRootWidget(); - return widget.get(); + return vm->GetRootWidget(); } // We may want to replace this with something faster, maybe caching the root diff --git a/layout/base/nsPresContext.h b/layout/base/nsPresContext.h index b737ce426fad..886e3814af12 100644 --- a/layout/base/nsPresContext.h +++ b/layout/base/nsPresContext.h @@ -232,13 +232,13 @@ class nsPresContext : public nsISupports, public mozilla::SupportsWeakPtr { /** * Returns the root widget for this. */ - nsIWidget* GetRootWidget() const; + already_AddRefed GetRootWidget() const; /** * Returns the widget which may have native focus and handles text input * like keyboard input, IME, etc. */ - nsIWidget* GetTextInputHandlingWidget() const { + already_AddRefed GetTextInputHandlingWidget() const { // Currently, root widget for each PresContext handles text input. return GetRootWidget(); } @@ -1100,6 +1100,7 @@ class nsPresContext : public nsISupports, public mozilla::SupportsWeakPtr { void UpdateCharSet(NotNull aCharSet); void DoForceReflowForFontInfoUpdateFromStyle(); + public: // Used by the PresShell to force a reflow when some aspect of font info // has been updated, potentially affecting font selection and layout. diff --git a/layout/base/nsRefreshDriver.cpp b/layout/base/nsRefreshDriver.cpp index 91d781c6ce19..ef46e8842b6e 100644 --- a/layout/base/nsRefreshDriver.cpp +++ b/layout/base/nsRefreshDriver.cpp @@ -993,7 +993,7 @@ void nsRefreshDriver::CreateVsyncRefreshTimer() { if (!mOwnTimer) { // If available, we fetch the widget-specific vsync source. nsPresContext* pc = GetPresContext(); - nsIWidget* widget = pc->GetRootWidget(); + nsCOMPtr widget = pc->GetRootWidget(); if (widget) { if (RefPtr localVsyncSource = widget->GetVsyncSource()) { @@ -2514,7 +2514,7 @@ void nsRefreshDriver::Tick(VsyncId aId, TimeStamp aNowTime, // Forward our composition payloads to the layer manager. if (!mCompositionPayloads.IsEmpty()) { - nsIWidget* widget = mPresContext->GetRootWidget(); + nsCOMPtr widget = mPresContext->GetRootWidget(); WindowRenderer* renderer = widget ? widget->GetWindowRenderer() : nullptr; if (renderer && renderer->AsWebRender()) { renderer->AsWebRender()->RegisterPayloads(mCompositionPayloads); diff --git a/layout/xul/nsXULPopupManager.cpp b/layout/xul/nsXULPopupManager.cpp index 4096ac760f2f..8a3865b6edb4 100644 --- a/layout/xul/nsXULPopupManager.cpp +++ b/layout/xul/nsXULPopupManager.cpp @@ -966,7 +966,7 @@ void nsXULPopupManager::ShowTooltipAtScreen(nsIContent* aPopup, // coordinates are relative to the root widget nsPresContext* rootPresContext = pc->GetRootPresContext(); if (rootPresContext) { - nsIWidget* rootWidget = rootPresContext->GetRootWidget(); + nsCOMPtr rootWidget = rootPresContext->GetRootWidget(); if (rootWidget) { mousePoint -= rootWidget->WidgetToScreenOffset(); } diff --git a/widget/gtk/nsNativeThemeGTK.cpp b/widget/gtk/nsNativeThemeGTK.cpp index 2c321f0d3679..28a841c32f55 100644 --- a/widget/gtk/nsNativeThemeGTK.cpp +++ b/widget/gtk/nsNativeThemeGTK.cpp @@ -68,7 +68,7 @@ static inline gint GetMonitorScaleFactor(nsPresContext* aPresContext) { // the real monitor scale cannot go under 1. double scale = StaticPrefs::layout_css_devPixelsPerPx(); if (scale <= 0) { - if (nsIWidget* rootWidget = aPresContext->GetRootWidget()) { + if (nsCOMPtr rootWidget = aPresContext->GetRootWidget()) { // We need to use GetDefaultScale() despite it returns monitor scale // factor multiplied by font scale factor because it is the only scale // updated in nsPuppetWidget. diff --git a/widget/nsNativeBasicTheme.cpp b/widget/nsNativeBasicTheme.cpp index 6b149e0ef152..9cd81780e37c 100644 --- a/widget/nsNativeBasicTheme.cpp +++ b/widget/nsNativeBasicTheme.cpp @@ -360,7 +360,7 @@ static std::pair SystemColorPair( auto nsNativeBasicTheme::GetDPIRatioForScrollbarPart(nsPresContext* aPc) -> DPIRatio { if (auto* rootPc = aPc->GetRootPresContext()) { - if (auto* widget = rootPc->GetRootWidget()) { + if (nsCOMPtr widget = rootPc->GetRootWidget()) { return widget->GetDefaultScale(); } } diff --git a/widget/windows/nsNativeThemeWin.cpp b/widget/windows/nsNativeThemeWin.cpp index ca98ab3afeef..152177eb8f50 100644 --- a/widget/windows/nsNativeThemeWin.cpp +++ b/widget/windows/nsNativeThemeWin.cpp @@ -1462,7 +1462,7 @@ static bool AssumeThemePartAndStateAreTransparent(int32_t aPart, static inline double GetThemeDpiScaleFactor(nsPresContext* aPresContext) { if (WinUtils::IsPerMonitorDPIAware() || StaticPrefs::layout_css_devPixelsPerPx() > 0.0) { - nsIWidget* rootWidget = aPresContext->GetRootWidget(); + nsCOMPtr rootWidget = aPresContext->GetRootWidget(); if (rootWidget) { double systemScale = WinUtils::SystemScaleFactor(); return rootWidget->GetDefaultScale().scale / systemScale; @@ -2007,7 +2007,7 @@ bool nsNativeThemeWin::GetWidgetPadding(nsDeviceContext* aContext, // the border padding. This should be addressed in nsWindow, // but currently can't be, see UpdateNonClientMargins. if (aAppearance == StyleAppearance::MozWindowTitlebarMaximized) { - nsIWidget* rootWidget = nullptr; + nsCOMPtr rootWidget; if (WinUtils::HasSystemMetricsForDpi()) { rootWidget = aFrame->PresContext()->GetRootWidget(); }