Closed Bug 2088603 Opened 26 days ago Closed 4 days ago

Add Fluent support for DOM console messages and start migrating strings from dom.properties

Categories

(Core :: DOM: Core & HTML, task)

task

Tracking

()

RESOLVED FIXED
163 Branch
Tracking Status
firefox163 --- fixed

People

(Reporter: flod, Assigned: flod)

References

(Blocks 1 open bug)

Details

Attachments

(6 external files, 1 obsolete file)

dom.properties is one of the largest remaining legacy .properties files. This bug adds the infrastructure needed to report DOM console messages using Fluent, and migrates a first set of strings to validate it (ideally trying to cover edge cases).

Infrastructure:

  • Add a Fluent-based nsContentUtils::ReportToConsole, together with FormatFluentString and FormatMaybeFluentString. This mirrors the existing dom.properties support (FluentResourceFile / gFluentResourceFiles follow PropertiesFile / gPropertiesFiles, cached Localization objects follow sStringBundles).
  • Cache a separate en-US Localization for documents that spoof English, since web-exposed messages (e.g. deprecation reports) must not leak the UI locale.
  • Support Fluent messages in nsIGlobalObject::ReportToConsole, including from workers: ReportErrorToConsoleRunnable accepts either a PropertiesFile or a FluentResourceFile, and messages are formatted on the main thread.
  • Add SpecialPowers.formatFluentValue, so tests can compute expected messages (including bidi isolation marks) instead of hard-coding them.

Initial migrations (with a Fluent migration recipe so existing translations carry over):

  • A first sample of dom.properties strings into dom/dom.ftl; URLs are also passed as arguments instead of being hard-coded in translations, which has always been hickey.
  • PerformanceObserver warnings (reported from both windows and workers).
  • SVG reference document warnings, mapped from their enum through a table so further migrations only require adding rows instead of changing code.
  • A sample of deprecation warnings (dom/deprecations.ftl).
  • The slow script dialog (dom/dialogs.ftl) and the file input validation message (dom/form-validation.ftl).

Once this lands, remaining dom.properties strings can be migrated incrementally in follow-up bugs.

I tried to measure performance of Fluent vs. .properties locally (macOS, en-US), comparing validationMessage for a required file input with a required text input.

Time

  • First message from a Fluent file in a content process: 0.14–0.2 ms, mostly fixed setup. Each additional file: 0.06–0.14 ms, mostly parsing.
  • Five files loaded in one process: 0.42 ms (1 message each), 0.52 ms (all of dom.properties split into 5 files), 0.76 ms (5 × 275 messages).
  • After the first call there's no measurable difference from .properties (0.36 vs 0.32 µs per call), so frequently triggered warnings only pay the cold cost once per process.

Memory (change in heapAllocated)

  • About 11 KB per Localization, plus roughly 7–10× the .ftl size for the parsed resource, in each content process that uses that file.
  • All of dom.properties migrated into 5 files: about 360 KB per process that uses them all. dom.properties uses about 0.3 KB per process, thanks to the shared-memory string bundles.

Takeaways

  • The files in this bug would all be under 2 KB, so the cost is a few tens of KB, only in processes that trigger these messages.
  • We should avoid having a single large file like we have with dom.properties. Group messages by how often they're hit, so rarely used strings don't get loaded by most processes, while keeping in mind the ~11 KB fixed cost per file (avoid too many files).
  • Content processes have nothing like SharedStringBundle for Fluent.

Following Claude's suggestion, tried with DMD (heap profiler).

DMD build on macOS (en-US), one content process. It loads the three files in my WIP for this bug, then repeats with form-validation.ftl padded to 277 messages (+37.5 KB).

Allocation Files in this bug Padded Cost
Parsed AST (fluent_syntax) 8.7 KB 152.8 KB ~3.8× file size
File contents 3.8 KB 51.8 KB ~1.3× file size
Bundle entries (add_resource) 1.5 KB 34.9 KB ~120 B per message
Bundle cache (ChunkyVec) 24.1 KB 24.1 KB 8 KB per Localization
Localization / generator 1.2 KB 1.1 KB ~0.4 KB per Localization
Total 39.3 KB 264.6 KB

Roughly 9 KB fixed per Localization + ~6× the .ftl size, in each content process that uses the file. Parsed resources stay cached in L10nFileSource until the process exits.

  • Most of the fixed cost is unused: fluent-fallback's bundle cache uses a ChunkyVec whose first chunk has room for 32 bundles (DEFAULT_CAPACITY), but a Localization normally needs one or two. A smaller first chunk would save ~7 KB per Localization across all of Firefox.
  • The AST and the entry map are ~80% of the per-byte cost, which lazy per-message parsing would avoid.

Will file a separate bug for the first part, the second is a much larger change.

See Also: → 2088830

Add Fluent-based nsContentUtils::ReportToConsole, FormatFluentString and
FormatMaybeFluentString, and migrate a few dom.properties strings to
dom/dom.ftl. URLs are now passed as arguments instead of being hard-coded in
translations.

The new code mirrors the existing dom.properties support in nsContentUtils:
FluentResourceFile and gFluentResourceFiles follow PropertiesFile and
gPropertiesFiles, the cached Localization objects follow sStringBundles
(including the cleanup in Shutdown()), and FormatFluentStringRunnable follows
FormatLocalizedStringRunnable to format messages for workers on the main
thread. Since a Localization's language is fixed when it's created, a separate
en-US instance is cached for documents that spoof English, as
txMozillaXSLTProcessor does.

Add SpecialPowers.formatFluentValue, so that tests can compute the expected
messages, including bidi isolation marks, instead of hard-coding them.

Migrate the PerformanceObserver warnings, which are reported from both windows
and workers.

ReportErrorToConsoleRunnable now accepts either a PropertiesFile or a
FluentResourceFile, instead of adding a separate runnable for Fluent messages.

Warnings identified by an enum are mapped to Fluent messages with a table, so
that migrating more of them only requires adding rows.

Deprecation reports are exposed to web content, so they use en-US when English
is spoofed. They can be reported from workers, which relies on the console
relay added in part 2.

This is preserving the current awkward & used to define the accesskey.

Moving this to an accesskey attribute would require adding C++ code to read the Fluent attribute,
a complex Fluent migration, and code to mark the character in the label.

This feels unnecessary given this is the only string still using this format for accesskeys,
and this dialog should only be relevant for non e10s builds (so hopefully short-lived).

AI review highlighted an issue with BiDi markers (U+2068 and U+2069) that Fluent automatically adds around variables. That's a problem with DevTools, using a regex to make links active, and possibly for users copying and pasting error messages.

The two options on the table are:

  • Create a second instance of L10nRegistry with BiDi isolation disabled (that's a built-in setting). Requires Rust and C++, although the code changes should be contained.
  • Strip BiDi markers from the strings, and accept possible issues (it's not possible to differentiate the ones inserted by localizers from those introduced by Fluent). Turns our that both PushNotifications and CrashReportes are already doing that (example).

I tried searching for an error message with and without markers, it definitely impacts results on GitHub (no results with markers), Bing (half the results). Based on this small sample, I think we need to remove them.

Bing: Ignoring unsupported entryTypes message (about half the results)

GitHub issues: exact phrase "Ignoring unsupported entryTypes: paint"

I ended up writing a patch for L10nRegistry and a test, and discovered that the bot's finding was incorrect.

BiDi markers were put behind a pref in bug 1439018, and that pref is false in Gecko
https://searchfox.org/firefox-main/rev/fa1e90ea670c4234c61b65a05dc02032b9bcde03/intl/l10n/rust/fluent-ffi/src/bundle.rs#150-155

Duplicate of this bug: 1760277
Attachment #9674771 - Attachment description: WIP: Bug 2088603 - (part 1) Add Fluent support to nsContentUtils::ReportToConsole and migrate the getElementById, document.write and EditContext warnings. r?#dom-core-reviewers!,jgraham! → WIP: Bug 2088603 - (part 1) Add Fluent support to nsContentUtils::ReportToConsole and migrate first warnings. r?#dom-core-reviewers!,eemeli!
Attachment #9674772 - Attachment description: WIP: Bug 2088603 - (part 2) Support Fluent messages in nsIGlobalObject::ReportToConsole for windows and workers, and migrate the PerformanceObserver warnings. r?#dom-core-reviewers! → WIP: Bug 2088603 - (part 2) Support Fluent messages in nsIGlobalObject::ReportToConsole for windows and workers, migrate PerformanceObserver warnings. r?#dom-core-reviewers!
Attachment #9674774 - Attachment description: WIP: Bug 2088603 - (part 4) Add Fluent support for deprecation warnings and migrate the captureEvents, releaseEvents, mozGetAll and MathML mo accent ones. r?#dom-core-reviewers! → WIP: Bug 2088603 - (part 4) Add Fluent support for deprecation warnings and migrate the captureEvents, releaseEvents, mozGetAll and MathML mo accent ones. r?#dom-core-reviewers!,eemeli!
Attachment #9674775 - Attachment description: WIP: Bug 2088603 - (part 5) Migrate the unresponsive script dialog and the missing file validation message to Fluent. r?#dom-core-reviewers! → WIP: Bug 2088603 - (part 5) Migrate the unresponsive script dialog to Fluent. r?#dom-core-reviewers!

The messages for invalid week and month values were looked up with keys that
don't exist in dom.properties (FormValidationInvalidWeek and
FormValidationInvalidMonth instead of FormValidationInvalidDateWeek and
FormValidationInvalidDateMonth), so these inputs had no message. They now
use the migrated Fluent messages.

The text lengths are passed as numbers, so that the messages can select
plural forms and format the numbers for the locale.

To avoid selecting a plural form for two numbers in the same message, the
text length messages only select one for the length limit, and show the
current length as "(current length: N)".

Blocks: 2095010
Attachment #9674773 - Attachment description: WIP: Bug 2088603 - (part 3) Migrate the SVG reference loop and reference chain length warnings to Fluent. r?#dom-core-reviewers! → Bug 2088603 - (part 3) Migrate the SVG reference loop and reference chain length warnings to Fluent. r?#dom-core-reviewers!
Attachment #9674772 - Attachment description: WIP: Bug 2088603 - (part 2) Support Fluent messages in nsIGlobalObject::ReportToConsole for windows and workers, migrate PerformanceObserver warnings. r?#dom-core-reviewers! → Bug 2088603 - (part 2) Support Fluent messages in nsIGlobalObject::ReportToConsole for windows and workers, migrate PerformanceObserver warnings. r?#dom-core-reviewers!
Attachment #9674771 - Attachment description: WIP: Bug 2088603 - (part 1) Add Fluent support to nsContentUtils::ReportToConsole and migrate first warnings. r?#dom-core-reviewers!,eemeli! → Bug 2088603 - (part 1) Add Fluent support to nsContentUtils::ReportToConsole and migrate first warnings. r?#dom-core-reviewers!,eemeli!
Attachment #9674774 - Attachment description: WIP: Bug 2088603 - (part 4) Add Fluent support for deprecation warnings and migrate the captureEvents, releaseEvents, mozGetAll and MathML mo accent ones. r?#dom-core-reviewers!,eemeli! → Bug 2088603 - (part 4) Add Fluent support for deprecation warnings and migrate the captureEvents, releaseEvents, mozGetAll and MathML mo accent ones. r?#dom-core-reviewers!,eemeli!
Attachment #9674775 - Attachment description: WIP: Bug 2088603 - (part 5) Migrate the unresponsive script dialog to Fluent. r?#dom-core-reviewers! → Bug 2088603 - (part 5) Migrate the unresponsive script dialog to Fluent. r?#dom-core-reviewers!
Attachment #9683298 - Attachment description: WIP: Bug 2088603 - (part 6) Migrate the form validation messages to Fluent, using plural forms for the text length ones. r?#dom-core-reviewers! → Bug 2088603 - (part 6) Migrate the form validation messages to Fluent, using plural forms for the text length ones. r?#dom-core-reviewers!
Depends on: 2095034

Note: This rearranges "spoof English"-sensitive code.

(In reply to Henri Sivonen (:hsivonen) from comment #13)

Note: This rearranges "spoof English"-sensitive code.

Ideally this should be a no-op for that.

just a quick question: in Tor Browser we force users to restart the browser if they change app language. I know FF does this if the direction changes. We do this in TB regardless of direction change because some items e.g. MediaDocument.properties, dom.properties (to name some) are not updated live and require a restart. Would changing to Fluent resolve this?

The .properties strings used by DOM code are cached in nsContentUtils for the lifetime of the process, so they don't pick up a language change. The Fluent replacement uses a Localization object that listens for intl:app-locales-changed and reloads its bundles, including in content processes, so messages migrated to Fluent should switch to the new language without a restart.

This bug only migrates a small part of dom.properties, but I'm hoping that migrating the remaining strings becomes a mechanical process.
MediaDocument.properties will keep requiring a restart until they're migrated too. appstrings.properties is even more complicated (see bug 1790187).

Pushed by flodolo@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/46b6b0b90f90 https://hg.mozilla.org/integration/autoland/rev/61067d2baa45 (part 1) Add Fluent support to nsContentUtils::ReportToConsole and migrate first warnings. r=eemeli,dom-core-reviewers,fluent-reviewers,geckoview-reviewers,hsivonen,bolsson,tthibaud https://github.com/mozilla-firefox/firefox/commit/2e8df4ead5e1 https://hg.mozilla.org/integration/autoland/rev/0bd10a7867ee (part 2) Support Fluent messages in nsIGlobalObject::ReportToConsole for windows and workers, migrate PerformanceObserver warnings. r=dom-core-reviewers,fluent-reviewers,dom-worker-reviewers,hsivonen,bolsson,edenchuang https://github.com/mozilla-firefox/firefox/commit/dede564857cd https://hg.mozilla.org/integration/autoland/rev/6a51e00b0ac5 (part 3) Migrate the SVG reference loop and reference chain length warnings to Fluent. r=dom-core-reviewers,fluent-reviewers,hsivonen,bolsson https://github.com/mozilla-firefox/firefox/commit/0cb57bb8dcef https://hg.mozilla.org/integration/autoland/rev/5802827a10f7 (part 4) Add Fluent support for deprecation warnings and migrate the captureEvents, releaseEvents, mozGetAll and MathML mo accent ones. r=eemeli,dom-core-reviewers,fluent-reviewers,hsivonen,bolsson https://github.com/mozilla-firefox/firefox/commit/444c3b88d3eb https://hg.mozilla.org/integration/autoland/rev/9985bd81d2ec (part 5) Migrate the unresponsive script dialog to Fluent. r=dom-core-reviewers,fluent-reviewers,hsivonen,bolsson https://github.com/mozilla-firefox/firefox/commit/6a1979795b6a https://hg.mozilla.org/integration/autoland/rev/ca6078f43794 (part 6) Migrate the form validation messages to Fluent, using plural forms for the text length ones. r=dom-core-reviewers,fluent-reviewers,hsivonen,bolsson,eemeli
Pushed by agoloman@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/b2bff9b6f2d6 https://hg.mozilla.org/integration/autoland/rev/e7f420cc8264 Revert "Bug 2088603 - (part 6) Migrate the form validation messages to Fluent, using plural forms for the text length ones. r=dom-core-reviewers,fluent-reviewers,hsivonen,bolsson,eemeli" for causing mochitest failures @test_deprecated_fluent.html.

Backed out for causing mochitest failures @test_deprecated_fluent.html.

Flags: needinfo?(francesco.lodolo)

Lovely, test fails only with --enable-xorigin-tests, it didn't fail locally or in the many try runs I've made.

Fixing and checking if other tests might be affected before pushing again.

Flags: needinfo?(francesco.lodolo)
Pushed by nfay@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/b9c2822428fa https://hg.mozilla.org/mozilla-central/rev/271c0cf88615 Revert "Bug 2088603 - (part 6) Migrate the form validation messages to Fluent, using plural forms for the text length ones. r=dom-core-reviewers,fluent-reviewers,hsivonen,bolsson,eemeli" for causing mochitest failures @test_deprecated_fluent.html.
Status: RESOLVED → REOPENED
Resolution: FIXED → ---
Target Milestone: 163 Branch → ---
Pushed by flodolo@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/078fac72a8ef https://hg.mozilla.org/integration/autoland/rev/e5cf1a613235 (part 1) Add Fluent support to nsContentUtils::ReportToConsole and migrate first warnings. r=eemeli,dom-core-reviewers,fluent-reviewers,geckoview-reviewers,hsivonen,bolsson,tthibaud https://github.com/mozilla-firefox/firefox/commit/e354ae52a1c6 https://hg.mozilla.org/integration/autoland/rev/fe5a2c8cc886 (part 2) Support Fluent messages in nsIGlobalObject::ReportToConsole for windows and workers, migrate PerformanceObserver warnings. r=dom-core-reviewers,fluent-reviewers,dom-worker-reviewers,hsivonen,bolsson,edenchuang https://github.com/mozilla-firefox/firefox/commit/c60294eb86ae https://hg.mozilla.org/integration/autoland/rev/311487d687ec (part 3) Migrate the SVG reference loop and reference chain length warnings to Fluent. r=dom-core-reviewers,fluent-reviewers,hsivonen,bolsson https://github.com/mozilla-firefox/firefox/commit/a5db340db9ad https://hg.mozilla.org/integration/autoland/rev/663a06a3aff4 (part 4) Add Fluent support for deprecation warnings and migrate the captureEvents, releaseEvents, mozGetAll and MathML mo accent ones. r=eemeli,dom-core-reviewers,fluent-reviewers,hsivonen,bolsson https://github.com/mozilla-firefox/firefox/commit/c8a510663988 https://hg.mozilla.org/integration/autoland/rev/ceb9541542af (part 5) Migrate the unresponsive script dialog to Fluent. r=dom-core-reviewers,fluent-reviewers,hsivonen,bolsson https://github.com/mozilla-firefox/firefox/commit/eee04bf3c5d5 https://hg.mozilla.org/integration/autoland/rev/473b81eadf38 (part 6) Migrate the form validation messages to Fluent, using plural forms for the text length ones. r=dom-core-reviewers,fluent-reviewers,hsivonen,bolsson,eemeli

This reverts commit 6a1979795b6a5f0c0016c857303b566fcb8f5e34.

Revert "Bug 2088603 - (part 5) Migrate the unresponsive script dialog to Fluent. r=dom-core-reviewers,fluent-reviewers,hsivonen,bolsson"

This reverts commit 444c3b88d3eb578ccda303f95f38257a2c66013c.

Revert "Bug 2088603 - (part 4) Add Fluent support for deprecation warnings and migrate the captureEvents, releaseEvents, mozGetAll and MathML mo accent ones. r=eemeli,dom-core-reviewers,fluent-reviewers,hsivonen,bolsson"

This reverts commit 0cb57bb8dcefda027b61ef417f9131ae830454dc.

Revert "Bug 2088603 - (part 3) Migrate the SVG reference loop and reference chain length warnings to Fluent. r=dom-core-reviewers,fluent-reviewers,hsivonen,bolsson"

This reverts commit dede564857cdf1195e78a1aa5109e5d08eaefd47.

Revert "Bug 2088603 - (part 2) Support Fluent messages in nsIGlobalObject::ReportToConsole for windows and workers, migrate PerformanceObserver warnings. r=dom-core-reviewers,fluent-reviewers,dom-worker-reviewers,hsivonen,bolsson,edenchuang"

This reverts commit 2e8df4ead5e12c19b7afb9cc4a4d717b42543d50.

Revert "Bug 2088603 - (part 1) Add Fluent support to nsContentUtils::ReportToConsole and migrate first warnings. r=eemeli,dom-core-reviewers,fluent-reviewers,geckoview-reviewers,hsivonen,bolsson,tthibaud"

This reverts commit 46b6b0b90f9055854849eddd5e09f659021dffa6.

Attachment #9691829 - Attachment is obsolete: true
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: