Bug 1883861 - Part 1: Move visitMemoryBarrier into the common CodeGenerator file...
[gecko.git] / layout / style / PreferenceSheet.cpp
bloba2062098856b8fff671d623ff5a013914e2a506c
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "PreferenceSheet.h"
9 #include "ServoCSSParser.h"
10 #include "MainThreadUtils.h"
11 #include "mozilla/Encoding.h"
12 #include "mozilla/Preferences.h"
13 #include "mozilla/StaticPrefs_browser.h"
14 #include "mozilla/StaticPrefs_devtools.h"
15 #include "mozilla/StaticPrefs_layout.h"
16 #include "mozilla/StaticPrefs_widget.h"
17 #include "mozilla/StaticPrefs_ui.h"
18 #include "mozilla/Telemetry.h"
19 #include "mozilla/LookAndFeel.h"
20 #include "mozilla/ServoBindings.h"
21 #include "mozilla/dom/Document.h"
22 #include "nsContentUtils.h"
24 #define AVG2(a, b) (((a) + (b) + 1) >> 1)
26 namespace mozilla {
28 using dom::Document;
30 bool PreferenceSheet::sInitialized;
31 PreferenceSheet::Prefs PreferenceSheet::sContentPrefs;
32 PreferenceSheet::Prefs PreferenceSheet::sChromePrefs;
33 PreferenceSheet::Prefs PreferenceSheet::sPrintPrefs;
35 static void GetColor(const char* aPrefName, ColorScheme aColorScheme,
36 nscolor& aColor) {
37 nsAutoCString darkPrefName;
38 if (aColorScheme == ColorScheme::Dark) {
39 darkPrefName.Append(aPrefName);
40 darkPrefName.AppendLiteral(".dark");
41 aPrefName = darkPrefName.get();
44 nsAutoCString value;
45 Preferences::GetCString(aPrefName, value);
46 if (value.IsEmpty() || Encoding::UTF8ValidUpTo(value) != value.Length()) {
47 return;
49 nscolor result;
50 if (!ServoCSSParser::ComputeColor(nullptr, NS_RGB(0, 0, 0), value, &result)) {
51 return;
53 aColor = result;
56 auto PreferenceSheet::PrefsKindFor(const Document& aDoc) -> PrefsKind {
57 if (aDoc.IsInChromeDocShell()) {
58 return PrefsKind::Chrome;
61 if (aDoc.IsBeingUsedAsImage() && aDoc.ChromeRulesEnabled()) {
62 return PrefsKind::Chrome;
65 if (aDoc.IsStaticDocument()) {
66 return PrefsKind::Print;
69 return PrefsKind::Content;
72 static bool UseDocumentColors(bool aUseAcccessibilityTheme) {
73 switch (StaticPrefs::browser_display_document_color_use()) {
74 case 1:
75 return true;
76 case 2:
77 return false;
78 default:
79 return !aUseAcccessibilityTheme;
83 static bool UseStandinsForNativeColors() {
84 return nsContentUtils::ShouldResistFingerprinting(
85 "we want to have consistent colors across the browser if RFP is "
86 "enabled, so we check the global preference"
87 "not excluding chrome browsers or webpages, so we call the legacy "
88 "RFP function to prevent that",
89 RFPTarget::UseStandinsForNativeColors) ||
90 StaticPrefs::ui_use_standins_for_native_colors();
93 void PreferenceSheet::Prefs::LoadColors(bool aIsLight) {
94 auto& colors = aIsLight ? mLightColors : mDarkColors;
96 if (!aIsLight) {
97 // Initialize the dark-color-scheme foreground/background colors as being
98 // the reverse of these members' default values, for ~reasonable fallback if
99 // the user configures broken pref values.
100 std::swap(colors.mDefault, colors.mDefaultBackground);
103 const auto scheme = aIsLight ? ColorScheme::Light : ColorScheme::Dark;
105 // Link colors might be provided by the OS, but they might not be. If they are
106 // not, then fall back to the pref colors.
108 // In particular, we don't query active link color to the OS.
109 GetColor("browser.anchor_color", scheme, colors.mLink);
110 GetColor("browser.active_color", scheme, colors.mActiveLink);
111 GetColor("browser.visited_color", scheme, colors.mVisitedLink);
113 // Historically we've given more weight to the "use standins" setting than the
114 // "use system colors" one. In practice most users don't use standins because
115 // it's hidden behind prefs.
116 if (mUsePrefColors && !mUseStandins) {
117 GetColor("browser.display.background_color", scheme,
118 colors.mDefaultBackground);
119 GetColor("browser.display.foreground_color", scheme, colors.mDefault);
120 } else {
121 using ColorID = LookAndFeel::ColorID;
122 const auto standins = LookAndFeel::UseStandins(mUseStandins);
123 colors.mDefault = LookAndFeel::Color(ColorID::Windowtext, scheme, standins,
124 colors.mDefault);
125 colors.mDefaultBackground = LookAndFeel::Color(
126 ColorID::Window, scheme, standins, colors.mDefaultBackground);
127 colors.mLink = LookAndFeel::Color(ColorID::MozNativehyperlinktext, scheme,
128 standins, colors.mLink);
130 if (auto color = LookAndFeel::GetColor(
131 ColorID::MozNativevisitedhyperlinktext, scheme, standins)) {
132 // If the system provides a visited link color, we should use it.
133 colors.mVisitedLink = *color;
134 } else if (mUseAccessibilityTheme) {
135 // The fallback visited link color on HCM (if the system doesn't provide
136 // one) is produced by preserving the foreground's green and averaging the
137 // foreground and background for the red and blue. This is how IE and
138 // Edge do it too.
139 colors.mVisitedLink = NS_RGB(
140 AVG2(NS_GET_R(colors.mDefault), NS_GET_R(colors.mDefaultBackground)),
141 NS_GET_G(colors.mDefault),
142 AVG2(NS_GET_B(colors.mDefault), NS_GET_B(colors.mDefaultBackground)));
143 } else {
144 // Otherwise we keep the default visited link color
147 if (mUseAccessibilityTheme) {
148 colors.mActiveLink = colors.mLink;
152 // Wherever we got the default background color from, ensure it is opaque.
153 colors.mDefaultBackground =
154 NS_ComposeColors(NS_RGB(0xFF, 0xFF, 0xFF), colors.mDefaultBackground);
157 bool PreferenceSheet::Prefs::NonNativeThemeShouldBeHighContrast() const {
158 // We only do that if we are overriding the document colors. Otherwise it
159 // causes issues when pages only override some of the system colors,
160 // specially in dark themes mode.
161 return StaticPrefs::widget_non_native_theme_always_high_contrast() ||
162 !mUseDocumentColors;
165 auto PreferenceSheet::ColorSchemeSettingForChrome()
166 -> ChromeColorSchemeSetting {
167 switch (StaticPrefs::browser_theme_toolbar_theme()) {
168 case 0: // Dark
169 return ChromeColorSchemeSetting::Dark;
170 case 1: // Light
171 return ChromeColorSchemeSetting::Light;
172 default:
173 return ChromeColorSchemeSetting::System;
177 ColorScheme PreferenceSheet::ThemeDerivedColorSchemeForContent() {
178 switch (StaticPrefs::browser_theme_content_theme()) {
179 case 0: // Dark
180 return ColorScheme::Dark;
181 case 1: // Light
182 return ColorScheme::Light;
183 default:
184 return LookAndFeel::SystemColorScheme();
188 void PreferenceSheet::Prefs::Load(bool aIsChrome) {
189 *this = {};
191 mIsChrome = aIsChrome;
192 mUseAccessibilityTheme =
193 LookAndFeel::GetInt(LookAndFeel::IntID::UseAccessibilityTheme);
194 // Chrome documents always use system colors, not stand-ins, not forced, etc.
195 if (!aIsChrome) {
196 mUseDocumentColors = UseDocumentColors(mUseAccessibilityTheme);
197 mUsePrefColors = !StaticPrefs::browser_display_use_system_colors();
198 mUseStandins = UseStandinsForNativeColors();
201 LoadColors(true);
202 LoadColors(false);
204 // When forcing the pref colors, we need to forcibly use the light color-set,
205 // as those are the colors exposed to the user in the colors dialog.
206 mMustUseLightColorSet = mUsePrefColors && !mUseDocumentColors;
207 #ifdef XP_WIN
208 if (mUseAccessibilityTheme) {
209 // Windows overrides the light colors with the HCM colors when HCM is
210 // active, so make sure to always use the light system colors in that case,
211 // and also make sure that we always use the light color set for the same
212 // reason.
213 mMustUseLightSystemColors = mMustUseLightColorSet = true;
215 #endif
217 mColorScheme = [&] {
218 if (aIsChrome) {
219 switch (ColorSchemeSettingForChrome()) {
220 case ChromeColorSchemeSetting::Light:
221 return ColorScheme::Light;
222 case ChromeColorSchemeSetting::Dark:
223 return ColorScheme::Dark;
224 case ChromeColorSchemeSetting::System:
225 break;
227 return LookAndFeel::SystemColorScheme();
229 if (mMustUseLightColorSet) {
230 // When forcing colors in a way such as color-scheme isn't respected, we
231 // compute a preference based on the darkness of
232 // our background.
233 return LookAndFeel::IsDarkColor(mLightColors.mDefaultBackground)
234 ? ColorScheme::Dark
235 : ColorScheme::Light;
237 switch (StaticPrefs::layout_css_prefers_color_scheme_content_override()) {
238 case 0:
239 return ColorScheme::Dark;
240 case 1:
241 return ColorScheme::Light;
242 default:
243 return ThemeDerivedColorSchemeForContent();
245 }();
248 void PreferenceSheet::Initialize() {
249 MOZ_ASSERT(NS_IsMainThread());
250 MOZ_ASSERT(!sInitialized);
252 sInitialized = true;
254 sContentPrefs.Load(false);
255 sChromePrefs.Load(true);
256 sPrintPrefs = sContentPrefs;
258 // For printing, we always use a preferred-light color scheme.
259 sPrintPrefs.mColorScheme = ColorScheme::Light;
260 if (!sPrintPrefs.mUseDocumentColors) {
261 // When overriding document colors, we ignore the `color-scheme` property,
262 // but we still don't want to use the system colors (which might be dark,
263 // despite having made it into mLightColors), because it both wastes ink
264 // and it might interact poorly with the color adjustments we do while
265 // printing.
267 // So we override the light colors with our hardcoded default colors, and
268 // force the use of stand-ins.
269 sPrintPrefs.mLightColors = Prefs().mLightColors;
270 sPrintPrefs.mUseStandins = true;
274 nsAutoString useDocumentColorPref;
275 switch (StaticPrefs::browser_display_document_color_use()) {
276 case 1:
277 useDocumentColorPref.AssignLiteral("always");
278 break;
279 case 2:
280 useDocumentColorPref.AssignLiteral("never");
281 break;
282 default:
283 useDocumentColorPref.AssignLiteral("default");
284 break;
287 Telemetry::ScalarSet(Telemetry::ScalarID::A11Y_THEME, useDocumentColorPref,
288 sContentPrefs.mUseAccessibilityTheme);
289 if (!sContentPrefs.mUseDocumentColors) {
290 // If a user has chosen to override doc colors through OS HCM or our HCM,
291 // we should log the user's current foreground (text) color and background
292 // color. Note, the document color use pref is the inverse of the HCM
293 // dropdown option in preferences.
295 // Note that we only look at light colors because that's the color set we
296 // use when forcing colors (since color-scheme is ignored when colors are
297 // forced).
299 // The light color set is the one that potentially contains the Windows HCM
300 // theme color/background (if we're using system colors and the user is
301 // using a High Contrast theme), and also the colors that as of today we
302 // allow setting in about:preferences.
303 Telemetry::ScalarSet(Telemetry::ScalarID::A11Y_HCM_FOREGROUND,
304 sContentPrefs.mLightColors.mDefault);
305 Telemetry::ScalarSet(Telemetry::ScalarID::A11Y_HCM_BACKGROUND,
306 sContentPrefs.mLightColors.mDefaultBackground);
309 Telemetry::ScalarSet(Telemetry::ScalarID::A11Y_BACKPLATE,
310 StaticPrefs::browser_display_permit_backplate());
311 Telemetry::ScalarSet(Telemetry::ScalarID::A11Y_USE_SYSTEM_COLORS,
312 StaticPrefs::browser_display_use_system_colors());
313 Telemetry::ScalarSet(Telemetry::ScalarID::A11Y_ALWAYS_UNDERLINE_LINKS,
314 StaticPrefs::layout_css_always_underline_links());
317 bool PreferenceSheet::AffectedByPref(const nsACString& aPref) {
318 const char* prefNames[] = {
319 StaticPrefs::GetPrefName_privacy_resistFingerprinting(),
320 StaticPrefs::GetPrefName_ui_use_standins_for_native_colors(),
321 "browser.anchor_color",
322 "browser.active_color",
323 "browser.visited_color",
326 if (StringBeginsWith(aPref, "browser.display."_ns)) {
327 return true;
330 for (const char* pref : prefNames) {
331 if (aPref.Equals(pref)) {
332 return true;
336 return false;
339 } // namespace mozilla
341 #undef AVG2