The fixed font preference reports a change every time it is re-applied

#40260226 Area: Blink>Fonts CL 8669858 In Review

What the bug reported, and what is left of it

The report is from 2023: every bfcache back/forward navigation ran CSSFontSelector::FontCacheInvalidated() on Windows, because WebContentsImpl::DidFinishNavigation re-applies the same WebPreferences and the font family setters reported a change for unchanged values.

I probed that on current main first: re-applying identical preferences through WebView::ApplyWebPreferences() four times in a row produces invalidations only on the first apply. The list case the bug was filed for was fixed by b012decc740a9 ("Tweak font family setting logic", 2024), which caches the first available font per family list and compares against it.

One instance of the same bug survived that fix, on macOS.

Root cause

GenericFontFamilySettings::UpdateFixed() compared the incoming family against Fixed(), and Fixed() translates a stored "Osaka" to "Osaka-Mono" on macOS (crbug.com/40535332). The embedder passes "Osaka", or the list that resolves to it, and the comparison is against "Osaka-Mono", which it can never equal:

bool GenericFontFamilySettings::UpdateFixed(const AtomicString& family,
                                            UScriptCode script) {
  const AtomicString& old_family = Fixed(script);   // "Osaka-Mono"
  if (!ShouldUpdateFontFamily(old_family, family)) {  // never false
    return false;
  }

So DevToolsEmulator::SetFixedFontFamily() calls Settings::NotifyGenericFontFamilyChange(), which runs Page::SettingsChanged(kFontFamily) → StyleEngine::UpdateGenericFontFamilySettings() → CSSFontSelector::FontCacheInvalidated() → MarkFontsNeedUpdate() on every navigation, which is the stack in the bug.

This is the configured default, not an exotic setting: the macOS fixed font for Japanese is ,Osaka,BIZ UDGothic,Menlo in locale_settings_mac.grd.

The fix compares against the stored family (GenericFontFamilyForScript(fixed_font_family_map_, script)) instead. Fixed() keeps translating for its callers.

Per navigation

The preference value is identical on every navigation; only the name the setter compares against differs.

Red/green

GenericFontFamilySettingsTest.OsakaFixedFontNotChanged is guarded by BUILDFLAG(IS_MAC), so to see it fail on Linux I built with the IS_MAC guard around the Osaka translation removed:

second UpdateFixed("Osaka")
without the fix Actual: true, Expected: false
with the fixfalse, test passes

blink_platform_unittests --gtest_filter=*Font* and blink_unittests --gtest_filter=*Font*:*Settings* pass. The macOS bots run the real test; mac-rel and mac14-tests are on the CL.

What I dropped from the first draft

An earlier version compared whole GenericFontFamilySettings objects in CSSFontSelector::UpdateGenericFontFamilySettings() and skipped the invalidation when they matched. That is a backstop at the consumer, not a fix: the settings genuinely differ at the moment the selector is called, since the map holds the list on one apply and the resolved family after the next read. It also added an operator== to a platform type for a single call site. The setter is the thing that was wrong, so the fix is one line there.

Links

Issue 40260226 CL 8669858 standalone sampler.html