From e22c322da591f4ac83d4a4d9ac0fd3c65167149d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Fri, 17 Feb 2023 21:15:06 +0000 Subject: [PATCH] Bug 1812868 - Tweak RDM and theme setup. r=mstange This is in order to make nsPresContext::UseOverlayScrollbars() thread-safe for stylo usage. Document::GetBrowsingContext() does ref-counting of main-thread-only objects, and we don't want that. Similarly nsPresContext::Theme() mutates the pres context which is not something we can do from multiple threads. Reviewed in: https://phabricator.services.mozilla.com/D168148 --- docshell/base/BrowsingContext.cpp | 16 +---------- layout/base/nsPresContext.cpp | 44 ++++++++++++++++++++++--------- layout/base/nsPresContext.h | 19 +++++-------- 3 files changed, 39 insertions(+), 40 deletions(-) diff --git a/docshell/base/BrowsingContext.cpp b/docshell/base/BrowsingContext.cpp index 787f531b800e..b270badc02ec 100644 --- a/docshell/base/BrowsingContext.cpp +++ b/docshell/base/BrowsingContext.cpp @@ -2757,21 +2757,7 @@ void BrowsingContext::DidSet(FieldIndex, bool aOldValue) { if (GetInRDMPane() == aOldValue) { return; } - - PreOrderWalk([&](BrowsingContext* aContext) { - if (nsIDocShell* shell = aContext->GetDocShell()) { - if (nsPresContext* pc = shell->GetPresContext()) { - pc->RecomputeTheme(); - - // This is a bit of a lie, but this affects the overlay-scrollbars - // media query and it's the code-path that gets taken for regular system - // metrics changes via ThemeChanged(). - pc->MediaFeatureValuesChanged( - {MediaFeatureChangeReason::SystemMetricsChange}, - MediaFeatureChangePropagation::JustThisDocument); - } - } - }); + PresContextAffectingFieldChanged(); } bool BrowsingContext::CanSet(FieldIndex, diff --git a/layout/base/nsPresContext.cpp b/layout/base/nsPresContext.cpp index a4c267df9377..5ffe460f8725 100644 --- a/layout/base/nsPresContext.cpp +++ b/layout/base/nsPresContext.cpp @@ -278,6 +278,7 @@ nsPresContext::nsPresContext(dom::Document* aDocument, nsPresContextType aType) mFontFeatureValuesDirty(true), mFontPaletteValuesDirty(true), mIsVisual(false), + mInRDMPane(false), mHasWarnedAboutTooLargeDashedOrDottedRadius(false), mQuirkSheetAdded(false), mHadNonBlankPaint(false), @@ -858,6 +859,8 @@ void nsPresContext::AttachPresShell(mozilla::PresShell* aPresShell) { // have a presshell, and hence a document. GetUserPreferences(); + EnsureTheme(); + nsIURI* docURI = doc->GetDocumentURI(); if (IsDynamic() && docURI) { @@ -933,6 +936,8 @@ void nsPresContext::RecomputeBrowsingContextDependentData() { return browsingContext->GetEmbedderColorSchemes().mPreferred; }()); + SetInRDMPane(top->GetInRDMPane()); + if (doc == mDocument) { // Medium doesn't apply to resource documents, etc. RefPtr mediumToEmulate; @@ -1286,6 +1291,14 @@ void nsPresContext::UpdateEffectiveTextZoom() { MediaFeatureChangePropagation::JustThisDocument); } +void nsPresContext::SetInRDMPane(bool aInRDMPane) { + if (mInRDMPane == aInRDMPane) { + return; + } + mInRDMPane = aInRDMPane; + RecomputeTheme(); +} + float nsPresContext::GetDeviceFullZoom() { return mDeviceContext->GetFullZoom(); } @@ -1626,11 +1639,15 @@ void nsPresContext::RecordInteractionTime(InteractionType aType, } } -nsITheme* nsPresContext::EnsureTheme() { +nsITheme* nsPresContext::Theme() const { + MOZ_ASSERT(mTheme); + return mTheme; +} + +void nsPresContext::EnsureTheme() { MOZ_ASSERT(!mTheme); if (Document()->ShouldAvoidNativeTheme()) { - BrowsingContext* bc = Document()->GetBrowsingContext(); - if (bc && bc->Top()->InRDMPane()) { + if (mInRDMPane) { mTheme = do_GetRDMThemeDoNotUseDirectly(); } else { mTheme = do_GetBasicNativeThemeDoNotUseDirectly(); @@ -1639,7 +1656,6 @@ nsITheme* nsPresContext::EnsureTheme() { mTheme = do_GetNativeThemeDoNotUseDirectly(); } MOZ_RELEASE_ASSERT(mTheme); - return mTheme; } void nsPresContext::RecomputeTheme() { @@ -1651,17 +1667,21 @@ void nsPresContext::RecomputeTheme() { if (oldTheme == mTheme) { return; } - // Theme only affects layout information, not style, so we just need to - // reframe (as it affects whether we create scrollbar buttons for example). - RebuildAllStyleData(nsChangeHint_ReconstructFrame, RestyleHint{0}); + // Theme affects layout information (as it affects whether we create + // scrollbar buttons for example) and also style (affects the + // scrollbar-inline-size env var). + RebuildAllStyleData(nsChangeHint_ReconstructFrame, + RestyleHint::RecascadeSubtree()); + // This is a bit of a lie, but this affects the overlay-scrollbars + // media query and it's the code-path that gets taken for regular system + // metrics changes via ThemeChanged(). + MediaFeatureValuesChanged({MediaFeatureChangeReason::SystemMetricsChange}, + MediaFeatureChangePropagation::JustThisDocument); } bool nsPresContext::UseOverlayScrollbars() const { - if (LookAndFeel::GetInt(LookAndFeel::IntID::UseOverlayScrollbars)) { - return true; - } - BrowsingContext* bc = Document()->GetBrowsingContext(); - return bc && bc->Top()->InRDMPane(); + return LookAndFeel::GetInt(LookAndFeel::IntID::UseOverlayScrollbars) || + mInRDMPane; } void nsPresContext::ThemeChanged(widget::ThemeChangeKind aKind) { diff --git a/layout/base/nsPresContext.h b/layout/base/nsPresContext.h index d6a08a0d74b3..acbb76d9ab68 100644 --- a/layout/base/nsPresContext.h +++ b/layout/base/nsPresContext.h @@ -573,6 +573,7 @@ class nsPresContext : public nsISupports, public mozilla::SupportsWeakPtr { } void SetFullZoom(float aZoom); void SetOverrideDPPX(float); + void SetInRDMPane(bool aInRDMPane); public: float GetFullZoom() { return mFullZoom; } @@ -824,18 +825,7 @@ class nsPresContext : public nsISupports, public mozilla::SupportsWeakPtr { */ uint32_t GetBidi() const; - /* - * Obtain a native theme for rendering our widgets (both form controls and - * html) - * - * Guaranteed to return non-null. - */ - nsITheme* Theme() MOZ_NONNULL_RETURN { - if (MOZ_LIKELY(mTheme)) { - return mTheme; - } - return EnsureTheme(); - } + nsITheme* Theme() const MOZ_NONNULL_RETURN; void RecomputeTheme(); @@ -1352,6 +1342,9 @@ class nsPresContext : public nsISupports, public mozilla::SupportsWeakPtr { unsigned mIsVisual : 1; + // Are we in the RDM pane? + unsigned mInRDMPane : 1; + unsigned mHasWarnedAboutTooLargeDashedOrDottedRadius : 1; // Have we added quirk.css to the style set? @@ -1383,7 +1376,7 @@ class nsPresContext : public nsISupports, public mozilla::SupportsWeakPtr { void LastRelease(); - nsITheme* EnsureTheme(); + void EnsureTheme(); #ifdef DEBUG private: