Commit 5535ec91 authored by Alison Maher's avatar Alison Maher Committed by Chromium LUCI CQ

Delay loading the forced colors stylesheet

Due to memory regressions caused by loading the forced colors
stylesheet when the ForcedColors flag is enabled, delay loading
the stylesheet until forced colors is triggered for the first time.

Bug: 1167339
Change-Id: I6de0f02442dc5f1b23776daf2883fe4793293548
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2633971
Commit-Queue: Alison Maher <almaher@microsoft.com>
Reviewed-by: default avatarRune Lillesveen <futhark@chromium.org>
Cr-Commit-Position: refs/heads/master@{#845231}
parent 563ef4b9
......@@ -89,20 +89,15 @@ static StyleSheetContents* ParseUASheet(const String& str) {
CSSDefaultStyleSheets::CSSDefaultStyleSheets()
: media_controls_style_sheet_loader_(nullptr) {
// Strict-mode rules.
String forced_colors_style_sheet =
RuntimeEnabledFeatures::ForcedColorsEnabled()
? UncompressResourceAsASCIIString(IDR_UASTYLE_THEME_FORCED_COLORS_CSS)
: String();
// Predefined @counter-style rules
String predefined_counter_styles_sheet =
RuntimeEnabledFeatures::CSSAtRuleCounterStyleEnabled()
? UncompressResourceAsASCIIString(
IDR_UASTYLE_PREDEFINED_COUNTER_STYLES_CSS)
: String();
// Strict-mode rules.
String default_rules = UncompressResourceAsASCIIString(IDR_UASTYLE_HTML_CSS) +
LayoutTheme::GetTheme().ExtraDefaultStyleSheet() +
forced_colors_style_sheet +
predefined_counter_styles_sheet;
default_style_sheet_ = ParseUASheet(default_rules);
......@@ -121,13 +116,11 @@ CSSDefaultStyleSheets::CSSDefaultStyleSheets()
default_svg_style_->CompactRulesIfNeeded();
default_quirks_style_->CompactRulesIfNeeded();
default_print_style_->CompactRulesIfNeeded();
default_forced_color_style_->CompactRulesIfNeeded();
DCHECK(default_style_->UniversalRules()->IsEmpty());
DCHECK(default_mathml_style_->UniversalRules()->IsEmpty());
DCHECK(default_svg_style_->UniversalRules()->IsEmpty());
DCHECK(default_quirks_style_->UniversalRules()->IsEmpty());
DCHECK(default_print_style_->UniversalRules()->IsEmpty());
DCHECK(default_forced_color_style_->UniversalRules()->IsEmpty());
#endif
}
......@@ -140,6 +133,7 @@ void CSSDefaultStyleSheets::PrepareForLeakDetection() {
mathml_style_sheet_.Clear();
media_controls_style_sheet_.Clear();
text_track_style_sheet_.Clear();
forced_colors_style_sheet_.Clear();
fullscreen_style_sheet_.Clear();
webxr_overlay_style_sheet_.Clear();
marker_style_sheet_.Clear();
......@@ -160,15 +154,13 @@ void CSSDefaultStyleSheets::InitializeDefaultStyles() {
default_svg_style_ = MakeGarbageCollected<RuleSet>();
default_quirks_style_ = MakeGarbageCollected<RuleSet>();
default_print_style_ = MakeGarbageCollected<RuleSet>();
default_forced_color_style_ = MakeGarbageCollected<RuleSet>();
default_media_controls_style_ = MakeGarbageCollected<RuleSet>();
default_forced_color_style_.Clear();
default_pseudo_element_style_.Clear();
default_style_->AddRulesFromSheet(DefaultStyleSheet(), ScreenEval());
default_quirks_style_->AddRulesFromSheet(QuirksStyleSheet(), ScreenEval());
default_print_style_->AddRulesFromSheet(DefaultStyleSheet(), PrintEval());
default_forced_color_style_->AddRulesFromSheet(DefaultStyleSheet(),
ForcedColorsEval());
}
RuleSet* CSSDefaultStyleSheets::DefaultViewSourceStyle() {
......@@ -226,8 +218,10 @@ bool CSSDefaultStyleSheets::EnsureDefaultStyleSheetsForElement(
ParseUASheet(UncompressResourceAsASCIIString(IDR_UASTYLE_SVG_CSS));
default_svg_style_->AddRulesFromSheet(SvgStyleSheet(), ScreenEval());
default_print_style_->AddRulesFromSheet(SvgStyleSheet(), PrintEval());
default_forced_color_style_->AddRulesFromSheet(SvgStyleSheet(),
ForcedColorsEval());
if (default_forced_color_style_) {
default_forced_color_style_->AddRulesFromSheet(SvgStyleSheet(),
ForcedColorsEval());
}
changed_default_style = true;
}
......@@ -253,8 +247,10 @@ bool CSSDefaultStyleSheets::EnsureDefaultStyleSheetsForElement(
ScreenEval());
default_print_style_->AddRulesFromSheet(MediaControlsStyleSheet(),
PrintEval());
default_forced_color_style_->AddRulesFromSheet(MediaControlsStyleSheet(),
ForcedColorsEval());
if (default_forced_color_style_) {
default_forced_color_style_->AddRulesFromSheet(MediaControlsStyleSheet(),
ForcedColorsEval());
}
changed_default_style = true;
}
......@@ -331,8 +327,10 @@ bool CSSDefaultStyleSheets::EnsureDefaultStyleSheetForXrOverlay() {
default_style_->AddRulesFromSheet(webxr_overlay_style_sheet_, ScreenEval());
default_print_style_->AddRulesFromSheet(webxr_overlay_style_sheet_,
PrintEval());
default_forced_color_style_->AddRulesFromSheet(webxr_overlay_style_sheet_,
ForcedColorsEval());
if (default_forced_color_style_) {
default_forced_color_style_->AddRulesFromSheet(webxr_overlay_style_sheet_,
ForcedColorsEval());
}
return true;
}
......@@ -349,6 +347,38 @@ void CSSDefaultStyleSheets::EnsureDefaultStyleSheetForFullscreen() {
ScreenEval());
}
bool CSSDefaultStyleSheets::EnsureDefaultStyleSheetForForcedColors() {
if (forced_colors_style_sheet_)
return false;
String forced_colors_rules =
RuntimeEnabledFeatures::ForcedColorsEnabled()
? UncompressResourceAsASCIIString(IDR_UASTYLE_THEME_FORCED_COLORS_CSS)
: String();
forced_colors_style_sheet_ = ParseUASheet(forced_colors_rules);
if (!default_forced_color_style_)
default_forced_color_style_ = MakeGarbageCollected<RuleSet>();
default_forced_color_style_->AddRulesFromSheet(DefaultStyleSheet(),
ForcedColorsEval());
default_forced_color_style_->AddRulesFromSheet(ForcedColorsStyleSheet(),
ForcedColorsEval());
if (svg_style_sheet_) {
default_forced_color_style_->AddRulesFromSheet(SvgStyleSheet(),
ForcedColorsEval());
}
if (media_controls_style_sheet_) {
default_forced_color_style_->AddRulesFromSheet(MediaControlsStyleSheet(),
ForcedColorsEval());
}
if (webxr_overlay_style_sheet_) {
default_forced_color_style_->AddRulesFromSheet(webxr_overlay_style_sheet_,
ForcedColorsEval());
}
return true;
}
void CSSDefaultStyleSheets::CollectFeaturesTo(const Document& document,
RuleFeatureSet& features) {
if (DefaultStyle())
......@@ -380,6 +410,7 @@ void CSSDefaultStyleSheets::Trace(Visitor* visitor) const {
visitor->Trace(mathml_style_sheet_);
visitor->Trace(media_controls_style_sheet_);
visitor->Trace(text_track_style_sheet_);
visitor->Trace(forced_colors_style_sheet_);
visitor->Trace(fullscreen_style_sheet_);
visitor->Trace(webxr_overlay_style_sheet_);
visitor->Trace(marker_style_sheet_);
......
......@@ -51,6 +51,7 @@ class CSSDefaultStyleSheets final
bool EnsureDefaultStyleSheetsForPseudoElement(PseudoId);
bool EnsureDefaultStyleSheetForXrOverlay();
void EnsureDefaultStyleSheetForFullscreen();
bool EnsureDefaultStyleSheetForForcedColors();
RuleSet* DefaultStyle() { return default_style_.Get(); }
RuleSet* DefaultMathMLStyle() { return default_mathml_style_.Get(); }
......@@ -83,6 +84,9 @@ class CSSDefaultStyleSheets final
return fullscreen_style_sheet_.Get();
}
StyleSheetContents* MarkerStyleSheet() { return marker_style_sheet_.Get(); }
StyleSheetContents* ForcedColorsStyleSheet() {
return forced_colors_style_sheet_.Get();
}
CORE_EXPORT void PrepareForLeakDetection();
......@@ -131,6 +135,7 @@ class CSSDefaultStyleSheets final
Member<StyleSheetContents> fullscreen_style_sheet_;
Member<StyleSheetContents> webxr_overlay_style_sheet_;
Member<StyleSheetContents> marker_style_sheet_;
Member<StyleSheetContents> forced_colors_style_sheet_;
std::unique_ptr<UAStyleSheetLoader> media_controls_style_sheet_loader_;
};
......
......@@ -1377,6 +1377,17 @@ void StyleEngine::EnsureUAStyleForPseudoElement(PseudoId pseudo_id) {
}
}
void StyleEngine::EnsureUAStyleForForcedColors() {
DCHECK(!IsHTMLImport());
DCHECK(global_rule_set_);
if (CSSDefaultStyleSheets::Instance()
.EnsureDefaultStyleSheetForForcedColors()) {
global_rule_set_->MarkDirty();
if (GetDocument().IsActive())
UpdateActiveStyle();
}
}
bool StyleEngine::HasRulesForId(const AtomicString& id) const {
DCHECK(!IsHTMLImport());
DCHECK(global_rule_set_);
......
......@@ -294,6 +294,7 @@ class CORE_EXPORT StyleEngine final : public GarbageCollected<StyleEngine>,
void EnsureUAStyleForXrOverlay();
void EnsureUAStyleForElement(const Element&);
void EnsureUAStyleForPseudoElement(PseudoId);
void EnsureUAStyleForForcedColors();
void PlatformColorsChanged();
......
......@@ -878,13 +878,13 @@ Document::Document(const DocumentInit& initializer,
lifecycle_.AdvanceTo(DocumentLifecycle::kInactive);
UpdateForcedColors();
// Since CSSFontSelector requires Document::fetcher_ and StyleEngine owns
// CSSFontSelector, need to initialize |style_engine_| after initializing
// |fetcher_|.
style_engine_ = MakeGarbageCollected<StyleEngine>(*this);
UpdateForcedColors();
// The parent's parser should be suspended together with all the other
// objects, else this new Document would have a new ExecutionContext which
// suspended state would not match the one from the parent, and could start
......@@ -8383,6 +8383,8 @@ void Document::UpdateForcedColors() {
? web_theme_engine->GetForcedColors()
: ForcedColors::kNone;
in_forced_colors_mode_ = forced_colors != ForcedColors::kNone;
if (in_forced_colors_mode_)
GetStyleEngine().EnsureUAStyleForForcedColors();
}
bool Document::InForcedColorsMode() const {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment