From c7992b86566115928a7434700f4028f92d7788a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Fri, 17 Apr 2020 22:55:03 +0000 Subject: [PATCH] Bug 1449522 - Cleanup some parent walks in StyleSheet code. r=nordzilla Differential Revision: https://phabricator.services.mozilla.com/D71410 --- layout/style/StyleSheet.cpp | 47 +++++++++++++++++-------------------- layout/style/StyleSheet.h | 19 ++++++++++----- 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/layout/style/StyleSheet.cpp b/layout/style/StyleSheet.cpp index 5d0c3063d3df..491d1db57c1c 100644 --- a/layout/style/StyleSheet.cpp +++ b/layout/style/StyleSheet.cpp @@ -159,28 +159,26 @@ Document* StyleSheet::GetAssociatedDocument() const { dom::DocumentOrShadowRoot* StyleSheet::GetAssociatedDocumentOrShadowRoot() const { - for (const auto* sheet = this; sheet; sheet = sheet->mParentSheet) { - if (sheet->mDocumentOrShadowRoot) { - return sheet->mDocumentOrShadowRoot; - } - if (sheet->IsConstructed()) { - return sheet->mConstructorDocument; - } + const StyleSheet& outer = OutermostSheet(); + if (outer.mDocumentOrShadowRoot) { + return outer.mDocumentOrShadowRoot; + } + if (outer.IsConstructed()) { + return outer.mConstructorDocument; } return nullptr; } Document* StyleSheet::GetKeptAliveByDocument() const { - for (const auto* sheet = this; sheet; sheet = sheet->mParentSheet) { - if (sheet->mDocumentOrShadowRoot) { - return sheet->mDocumentOrShadowRoot->AsNode().GetComposedDoc(); - } - if (sheet->IsConstructed()) { - for (DocumentOrShadowRoot* adopter : sheet->mAdopters) { - MOZ_ASSERT(adopter->AsNode().OwnerDoc() == sheet->mConstructorDocument); - if (adopter->AsNode().IsInComposedDoc()) { - return sheet->mConstructorDocument.get(); - } + const StyleSheet& outer = OutermostSheet(); + if (outer.mDocumentOrShadowRoot) { + return outer.mDocumentOrShadowRoot->AsNode().GetComposedDoc(); + } + if (outer.IsConstructed()) { + for (DocumentOrShadowRoot* adopter : outer.mAdopters) { + MOZ_ASSERT(adopter->AsNode().OwnerDoc() == outer.mConstructorDocument); + if (adopter->AsNode().IsInComposedDoc()) { + return outer.mConstructorDocument.get(); } } } @@ -306,15 +304,14 @@ void StyleSheet::ApplicableStateChanged(bool aApplicable) { } }; - for (const auto* sheet = this; sheet; sheet = sheet->mParentSheet) { - if (sheet->mDocumentOrShadowRoot) { - Notify(*sheet->mDocumentOrShadowRoot); - } + const StyleSheet& sheet = OutermostSheet(); + if (sheet.mDocumentOrShadowRoot) { + Notify(*sheet.mDocumentOrShadowRoot); + } - for (DocumentOrShadowRoot* adopter : sheet->mAdopters) { - MOZ_ASSERT(adopter, "adopters should never be null"); - Notify(*adopter); - } + for (DocumentOrShadowRoot* adopter : sheet.mAdopters) { + MOZ_ASSERT(adopter, "adopters should never be null"); + Notify(*adopter); } } diff --git a/layout/style/StyleSheet.h b/layout/style/StyleSheet.h index e5ab7a1491d9..7dd52f51e7a1 100644 --- a/layout/style/StyleSheet.h +++ b/layout/style/StyleSheet.h @@ -394,12 +394,7 @@ class StyleSheet final : public nsICSSLoaderObserver, public nsWrapperCache { // True if any of this sheet's ancestors were created through the // Constructable StyleSheets API bool SelfOrAncestorIsConstructed() const { - for (auto* sheet = this; sheet; sheet = sheet->mParentSheet) { - if (sheet->IsConstructed()) { - return true; - } - } - return false; + return OutermostSheet().IsConstructed(); } // Ture if the sheet's constructor document matches the given document @@ -473,6 +468,18 @@ class StyleSheet final : public nsICSSLoaderObserver, public nsWrapperCache { mState |= State::ModifiedRules | State::ModifiedRulesForDevtools; } + const StyleSheet& OutermostSheet() const { + auto* current = this; + while (current->mParentSheet) { + MOZ_ASSERT(!current->mDocumentOrShadowRoot, + "Shouldn't be set on child sheets"); + MOZ_ASSERT(!current->mConstructorDocument, + "Shouldn't be set on child sheets"); + current = current->mParentSheet; + } + return *current; + } + StyleSheetInfo& Inner() { MOZ_ASSERT(mInner); return *mInner;