Skip to content

Commit

Permalink
Use more smart pointers in StyleScope.h
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=261983

Reviewed by Darin Adler.

* Source/WebCore/css/CSSStyleSheet.h:
* Source/WebCore/css/StyleSheetContents.cpp:
(WebCore::StyleSheetContents::StyleSheetContents):
* Source/WebCore/css/StyleSheetContents.h:
* Source/WebCore/style/StyleInvalidator.cpp:
(WebCore::Style::shouldDirtyAllStyle):
(WebCore::Style::Invalidator::Invalidator):
(WebCore::Style::m_dirtiesAllStyle):
* Source/WebCore/style/StyleInvalidator.h:
* Source/WebCore/style/StyleScope.cpp:
(WebCore::Style::Scope::analyzeStyleSheetChange):
* Source/WebCore/style/StyleScope.h:

Canonical link: https://commits.webkit.org/268375@main
  • Loading branch information
cdumez committed Sep 24, 2023
1 parent 4e5391c commit 207c7c9
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Source/WebCore/css/CSSStyleSheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ void CSSStyleSheet::didMutateRules(RuleMutationType mutationType, WhetherContent
ASSERT(m_contents->hasOneClient());

forEachStyleScope([&](Style::Scope& scope) {
if ((mutationType == RuleInsertion || mutationType == RuleReplace) && !contentsWereClonedForMutation && !scope.activeStyleSheetsContains(this)) {
if ((mutationType == RuleInsertion || mutationType == RuleReplace) && !contentsWereClonedForMutation && !scope.activeStyleSheetsContains(*this)) {
if (insertedKeyframesRule) {
if (auto* resolver = scope.resolverIfExists())
resolver->addKeyframeStyle(*insertedKeyframesRule);
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/css/CSSStyleSheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace Style {
class Scope;
}

class CSSStyleSheet final : public StyleSheet {
class CSSStyleSheet final : public StyleSheet, public CanMakeCheckedPtr {
public:
struct Init {
String baseURL;
Expand Down
5 changes: 1 addition & 4 deletions Source/WebCore/css/StyleSheetContents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ StyleSheetContents::StyleSheetContents(StyleRuleImport* ownerRule, const String&
}

StyleSheetContents::StyleSheetContents(const StyleSheetContents& o)
: RefCounted<StyleSheetContents>()
, CanMakeWeakPtr<StyleSheetContents>()
, m_ownerRule(nullptr)
, m_originalURL(o.m_originalURL)
: m_originalURL(o.m_originalURL)
, m_encodingFromCharsetRule(o.m_encodingFromCharsetRule)
, m_layerRulesBeforeImportRules(o.m_layerRulesBeforeImportRules.size())
, m_importRules(o.m_importRules.size())
Expand Down
6 changes: 3 additions & 3 deletions Source/WebCore/css/StyleSheetContents.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
#pragma once

#include "CSSParserContext.h"
#include <wtf/CheckedRef.h>
#include <wtf/Function.h>
#include <wtf/HashMap.h>
#include <wtf/RefCounted.h>
#include <wtf/URL.h>
#include <wtf/Vector.h>
#include <wtf/WeakPtr.h>
#include <wtf/text/AtomStringHash.h>

namespace WebCore {
Expand All @@ -45,7 +45,7 @@ class StyleRuleNamespace;

enum class CachePolicy : uint8_t;

class StyleSheetContents final : public RefCounted<StyleSheetContents>, public CanMakeWeakPtr<StyleSheetContents> {
class StyleSheetContents final : public RefCounted<StyleSheetContents>, public CanMakeCheckedPtr {
public:
static Ref<StyleSheetContents> create(const CSSParserContext& context = CSSParserContext(HTMLStandardMode))
{
Expand Down Expand Up @@ -161,7 +161,7 @@ class StyleSheetContents final : public RefCounted<StyleSheetContents>, public C

void clearCharsetRule();

StyleRuleImport* m_ownerRule;
StyleRuleImport* m_ownerRule { nullptr };

String m_originalURL;

Expand Down
8 changes: 4 additions & 4 deletions Source/WebCore/style/StyleInvalidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ static bool shouldDirtyAllStyle(const StyleSheetContents& sheet)
return false;
}

static bool shouldDirtyAllStyle(const Vector<StyleSheetContents*>& sheets)
static bool shouldDirtyAllStyle(const Vector<CheckedRef<StyleSheetContents>>& sheets)
{
for (auto& sheet : sheets) {
if (shouldDirtyAllStyle(*sheet))
if (shouldDirtyAllStyle(sheet))
return true;
}
return false;
}

Invalidator::Invalidator(const Vector<StyleSheetContents*>& sheets, const MQ::MediaQueryEvaluator& mediaQueryEvaluator)
Invalidator::Invalidator(const Vector<CheckedRef<StyleSheetContents>>& sheets, const MQ::MediaQueryEvaluator& mediaQueryEvaluator)
: m_ownedRuleSet(RuleSet::create())
, m_ruleSets({ m_ownedRuleSet })
, m_dirtiesAllStyle(shouldDirtyAllStyle(sheets))
Expand All @@ -99,7 +99,7 @@ Invalidator::Invalidator(const Vector<StyleSheetContents*>& sheets, const MQ::Me
RuleSetBuilder ruleSetBuilder(*m_ownedRuleSet, mediaQueryEvaluator, nullptr, RuleSetBuilder::ShrinkToFit::Disable);

for (auto& sheet : sheets)
ruleSetBuilder.addRulesFromSheet(*sheet);
ruleSetBuilder.addRulesFromSheet(sheet);

m_ruleInformation = collectRuleInformation();
}
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/style/StyleInvalidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct SelectorMatchingState;

class Invalidator {
public:
Invalidator(const Vector<StyleSheetContents*>&, const MQ::MediaQueryEvaluator&);
Invalidator(const Vector<CheckedRef<StyleSheetContents>>&, const MQ::MediaQueryEvaluator&);
Invalidator(const InvalidationRuleSetVector&);

~Invalidator();
Expand Down
12 changes: 6 additions & 6 deletions Source/WebCore/style/StyleScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,14 +480,14 @@ Scope::StyleSheetChange Scope::analyzeStyleSheetChange(const Vector<RefPtr<CSSSt
if (newStylesheetCount < oldStylesheetCount)
return { ResolverUpdateType::Reconstruct };

Vector<StyleSheetContents*> addedSheets;
Vector<CheckedRef<StyleSheetContents>> addedSheets;
unsigned newIndex = 0;
for (unsigned oldIndex = 0; oldIndex < oldStylesheetCount; ++oldIndex) {
if (newIndex >= newStylesheetCount)
return { ResolverUpdateType::Reconstruct };

while (m_activeStyleSheets[oldIndex] != newStylesheets[newIndex]) {
addedSheets.append(&newStylesheets[newIndex]->contents());
addedSheets.append(newStylesheets[newIndex]->contents());
++newIndex;
if (newIndex == newStylesheetCount)
return { ResolverUpdateType::Reconstruct };
Expand All @@ -496,7 +496,7 @@ Scope::StyleSheetChange Scope::analyzeStyleSheetChange(const Vector<RefPtr<CSSSt
}
bool hasInsertions = !addedSheets.isEmpty();
while (newIndex < newStylesheetCount) {
addedSheets.append(&newStylesheets[newIndex]->contents());
addedSheets.append(newStylesheets[newIndex]->contents());
++newIndex;
}

Expand Down Expand Up @@ -644,14 +644,14 @@ const Vector<RefPtr<CSSStyleSheet>> Scope::activeStyleSheetsForInspector()
return result;
}

bool Scope::activeStyleSheetsContains(const CSSStyleSheet* sheet) const
bool Scope::activeStyleSheetsContains(const CSSStyleSheet& sheet) const
{
if (m_activeStyleSheets.isEmpty())
return false;

if (m_weakCopyOfActiveStyleSheetListForFastLookup.isEmpty()) {
for (auto& activeStyleSheet : m_activeStyleSheets)
m_weakCopyOfActiveStyleSheetListForFastLookup.add(activeStyleSheet.get());
m_weakCopyOfActiveStyleSheetListForFastLookup.add(*activeStyleSheet);
}
return m_weakCopyOfActiveStyleSheetListForFastLookup.contains(sheet);
}
Expand Down Expand Up @@ -930,7 +930,7 @@ bool Scope::updateQueryContainerState(QueryContainerUpdateContext& context)
auto it = previousStates.find(*containerElement);
bool changed = it == previousStates.end() || sizeChanged(it->value);
// Protect against unstable layout by invalidating only once per container.
if (changed && context.invalidatedContainers.add(containerElement).isNewEntry)
if (changed && context.invalidatedContainers.add(*containerElement).isNewEntry)
containersToInvalidate.append(containerElement);
m_queryContainerStates.add(*containerElement, size);
}
Expand Down
8 changes: 4 additions & 4 deletions Source/WebCore/style/StyleScope.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Scope : public CanMakeWeakPtr<Scope> {
bool usesStyleBasedEditability() const { return m_usesStyleBasedEditability; }
bool usesHasPseudoClass() const { return m_usesHasPseudoClass; }

bool activeStyleSheetsContains(const CSSStyleSheet*) const;
bool activeStyleSheetsContains(const CSSStyleSheet&) const;

void evaluateMediaQueriesForViewportChange();
void evaluateMediaQueriesForAccessibilitySettingsChange();
Expand Down Expand Up @@ -139,7 +139,7 @@ class Scope : public CanMakeWeakPtr<Scope> {
static Scope* forOrdinal(Element&, ScopeOrdinal);

struct QueryContainerUpdateContext {
HashSet<Element*> invalidatedContainers;
HashSet<CheckedRef<Element>> invalidatedContainers;
};
bool updateQueryContainerState(QueryContainerUpdateContext&);

Expand Down Expand Up @@ -179,7 +179,7 @@ class Scope : public CanMakeWeakPtr<Scope> {
};
struct StyleSheetChange {
ResolverUpdateType resolverUpdateType;
Vector<StyleSheetContents*> addedSheets { };
Vector<CheckedRef<StyleSheetContents>> addedSheets { };
};
StyleSheetChange analyzeStyleSheetChange(const Vector<RefPtr<CSSStyleSheet>>& newStylesheets);
void invalidateStyleAfterStyleSheetChange(const StyleSheetChange&);
Expand Down Expand Up @@ -210,7 +210,7 @@ class Scope : public CanMakeWeakPtr<Scope> {

Timer m_pendingUpdateTimer;

mutable HashSet<const CSSStyleSheet*> m_weakCopyOfActiveStyleSheetListForFastLookup;
mutable HashSet<CheckedRef<const CSSStyleSheet>> m_weakCopyOfActiveStyleSheetListForFastLookup;

// Track the currently loading top-level stylesheets needed for rendering.
// Sheets loaded using the @import directive are not included in this count.
Expand Down

0 comments on commit 207c7c9

Please sign in to comment.