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_layout.h"
15 #include "mozilla/StaticPrefs_widget.h"
16 #include "mozilla/StaticPrefs_ui.h"
17 #include "mozilla/glean/GleanMetrics.h"
18 #include "mozilla/LookAndFeel.h"
19 #include "mozilla/ServoBindings.h"
20 #include "mozilla/dom/Document.h"
21 #include "nsContentUtils.h"
23 #define AVG2(a, b) (((a) + (b) + 1) >> 1)
29 bool PreferenceSheet::sInitialized
;
30 PreferenceSheet::Prefs
PreferenceSheet::sContentPrefs
;
31 PreferenceSheet::Prefs
PreferenceSheet::sChromePrefs
;
32 PreferenceSheet::Prefs
PreferenceSheet::sPrintPrefs
;
34 static void GetColor(const char* aPrefName
, ColorScheme aColorScheme
,
36 nsAutoCString darkPrefName
;
37 if (aColorScheme
== ColorScheme::Dark
) {
38 darkPrefName
.Append(aPrefName
);
39 darkPrefName
.AppendLiteral(".dark");
40 aPrefName
= darkPrefName
.get();
44 Preferences::GetCString(aPrefName
, value
);
45 if (value
.IsEmpty() || Encoding::UTF8ValidUpTo(value
) != value
.Length()) {
49 if (!ServoCSSParser::ComputeColor(nullptr, NS_RGB(0, 0, 0), value
, &result
)) {
55 auto PreferenceSheet::PrefsKindFor(const Document
& aDoc
) -> PrefsKind
{
56 if (aDoc
.IsInChromeDocShell()) {
57 return PrefsKind::Chrome
;
60 if (aDoc
.IsBeingUsedAsImage() && aDoc
.ChromeRulesEnabled()) {
61 return PrefsKind::Chrome
;
64 if (aDoc
.IsStaticDocument()) {
65 return PrefsKind::Print
;
68 return PrefsKind::Content
;
71 static bool UseDocumentColors(bool aUseAcccessibilityTheme
) {
72 switch (StaticPrefs::browser_display_document_color_use()) {
78 return !aUseAcccessibilityTheme
;
82 static bool UseStandinsForNativeColors() {
83 return nsContentUtils::ShouldResistFingerprinting(
84 "we want to have consistent colors across the browser if RFP is "
85 "enabled, so we check the global preference"
86 "not excluding chrome browsers or webpages, so we call the legacy "
87 "RFP function to prevent that",
88 RFPTarget::UseStandinsForNativeColors
) ||
89 StaticPrefs::ui_use_standins_for_native_colors();
92 void PreferenceSheet::Prefs::LoadColors(bool aIsLight
) {
93 auto& colors
= aIsLight
? mLightColors
: mDarkColors
;
96 // Initialize the dark-color-scheme foreground/background colors as being
97 // the reverse of these members' default values, for ~reasonable fallback if
98 // the user configures broken pref values.
99 std::swap(colors
.mDefault
, colors
.mDefaultBackground
);
102 const auto scheme
= aIsLight
? ColorScheme::Light
: ColorScheme::Dark
;
104 // Link colors might be provided by the OS, but they might not be. If they are
105 // not, then fall back to the pref colors.
107 // In particular, we don't query active link color to the OS.
108 GetColor("browser.anchor_color", scheme
, colors
.mLink
);
109 GetColor("browser.active_color", scheme
, colors
.mActiveLink
);
110 GetColor("browser.visited_color", scheme
, colors
.mVisitedLink
);
112 // Historically we've given more weight to the "use standins" setting than the
113 // "use system colors" one. In practice most users don't use standins because
114 // it's hidden behind prefs.
115 if (mUsePrefColors
&& !mUseStandins
) {
116 GetColor("browser.display.background_color", scheme
,
117 colors
.mDefaultBackground
);
118 GetColor("browser.display.foreground_color", scheme
, colors
.mDefault
);
120 using ColorID
= LookAndFeel::ColorID
;
121 const auto standins
= LookAndFeel::UseStandins(mUseStandins
);
122 colors
.mDefault
= LookAndFeel::Color(ColorID::Windowtext
, scheme
, standins
,
124 colors
.mDefaultBackground
= LookAndFeel::Color(
125 ColorID::Window
, scheme
, standins
, colors
.mDefaultBackground
);
126 colors
.mLink
= LookAndFeel::Color(ColorID::MozNativehyperlinktext
, scheme
,
127 standins
, colors
.mLink
);
129 if (auto color
= LookAndFeel::GetColor(
130 ColorID::MozNativevisitedhyperlinktext
, scheme
, standins
)) {
131 // If the system provides a visited link color, we should use it.
132 colors
.mVisitedLink
= *color
;
133 } else if (mUseAccessibilityTheme
) {
134 // The fallback visited link color on HCM (if the system doesn't provide
135 // one) is produced by preserving the foreground's green and averaging the
136 // foreground and background for the red and blue. This is how IE and
138 colors
.mVisitedLink
= NS_RGB(
139 AVG2(NS_GET_R(colors
.mDefault
), NS_GET_R(colors
.mDefaultBackground
)),
140 NS_GET_G(colors
.mDefault
),
141 AVG2(NS_GET_B(colors
.mDefault
), NS_GET_B(colors
.mDefaultBackground
)));
143 // Otherwise we keep the default visited link color
146 if (mUseAccessibilityTheme
) {
147 colors
.mActiveLink
= colors
.mLink
;
151 // Wherever we got the default background color from, ensure it is opaque.
152 colors
.mDefaultBackground
=
153 NS_ComposeColors(NS_RGB(0xFF, 0xFF, 0xFF), colors
.mDefaultBackground
);
156 bool PreferenceSheet::Prefs::NonNativeThemeShouldBeHighContrast() const {
157 // We only do that if we are overriding the document colors. Otherwise it
158 // causes issues when pages only override some of the system colors,
159 // specially in dark themes mode.
160 return StaticPrefs::widget_non_native_theme_always_high_contrast() ||
164 auto PreferenceSheet::ColorSchemeSettingForChrome()
165 -> ChromeColorSchemeSetting
{
166 switch (StaticPrefs::browser_theme_toolbar_theme()) {
168 return ChromeColorSchemeSetting::Dark
;
170 return ChromeColorSchemeSetting::Light
;
172 return ChromeColorSchemeSetting::System
;
176 ColorScheme
PreferenceSheet::ThemeDerivedColorSchemeForContent() {
177 switch (StaticPrefs::browser_theme_content_theme()) {
179 return ColorScheme::Dark
;
181 return ColorScheme::Light
;
183 return LookAndFeel::SystemColorScheme();
187 void PreferenceSheet::Prefs::Load(bool aIsChrome
) {
190 mIsChrome
= aIsChrome
;
191 mUseAccessibilityTheme
=
192 LookAndFeel::GetInt(LookAndFeel::IntID::UseAccessibilityTheme
);
193 // Chrome documents always use system colors, not stand-ins, not forced, etc.
195 mUseDocumentColors
= UseDocumentColors(mUseAccessibilityTheme
);
196 mUsePrefColors
= !StaticPrefs::browser_display_use_system_colors();
197 mUseStandins
= UseStandinsForNativeColors();
203 // When forcing the pref colors, we need to forcibly use the light color-set,
204 // as those are the colors exposed to the user in the colors dialog.
205 mMustUseLightColorSet
= mUsePrefColors
&& !mUseDocumentColors
;
207 if (mUseAccessibilityTheme
&& (mIsChrome
|| !mUseDocumentColors
)) {
208 // Windows overrides the light colors with the HCM colors when HCM is
209 // active, so make sure to always use the light system colors in that case,
210 // and also make sure that we always use the light color set for the same
212 mMustUseLightSystemColors
= mMustUseLightColorSet
= true;
218 switch (ColorSchemeSettingForChrome()) {
219 case ChromeColorSchemeSetting::Light
:
220 return ColorScheme::Light
;
221 case ChromeColorSchemeSetting::Dark
:
222 return ColorScheme::Dark
;
223 case ChromeColorSchemeSetting::System
:
226 return LookAndFeel::SystemColorScheme();
228 if (mMustUseLightColorSet
) {
229 // When forcing colors in a way such as color-scheme isn't respected, we
230 // compute a preference based on the darkness of
232 return LookAndFeel::IsDarkColor(mLightColors
.mDefaultBackground
)
234 : ColorScheme::Light
;
236 switch (StaticPrefs::layout_css_prefers_color_scheme_content_override()) {
238 return ColorScheme::Dark
;
240 return ColorScheme::Light
;
242 return ThemeDerivedColorSchemeForContent();
247 void PreferenceSheet::Initialize() {
248 MOZ_ASSERT(NS_IsMainThread());
249 MOZ_ASSERT(!sInitialized
);
253 sContentPrefs
.Load(false);
254 sChromePrefs
.Load(true);
255 sPrintPrefs
= sContentPrefs
;
257 // For printing, we always use a preferred-light color scheme.
258 sPrintPrefs
.mColorScheme
= ColorScheme::Light
;
259 if (!sPrintPrefs
.mUseDocumentColors
) {
260 // When overriding document colors, we ignore the `color-scheme` property,
261 // but we still don't want to use the system colors (which might be dark,
262 // despite having made it into mLightColors), because it both wastes ink
263 // and it might interact poorly with the color adjustments we do while
266 // So we override the light colors with our hardcoded default colors, and
267 // force the use of stand-ins.
268 sPrintPrefs
.mLightColors
= Prefs().mLightColors
;
269 sPrintPrefs
.mUseStandins
= true;
273 // Telemetry for these preferences is only collected on the parent process.
274 if (!XRE_IsParentProcess()) {
278 glean::a11y::ThemeLabel gleanLabel
;
279 switch (StaticPrefs::browser_display_document_color_use()) {
281 gleanLabel
= glean::a11y::ThemeLabel::eAlways
;
284 gleanLabel
= glean::a11y::ThemeLabel::eNever
;
287 gleanLabel
= glean::a11y::ThemeLabel::eDefault
;
291 glean::a11y::theme
.EnumGet(gleanLabel
)
292 .Set(sContentPrefs
.mUseAccessibilityTheme
);
293 if (!sContentPrefs
.mUseDocumentColors
) {
294 // If a user has chosen to override doc colors through OS HCM or our HCM,
295 // we should log the user's current foreground (text) color and background
296 // color. Note, the document color use pref is the inverse of the HCM
297 // dropdown option in preferences.
299 // Note that we only look at light colors because that's the color set we
300 // use when forcing colors (since color-scheme is ignored when colors are
303 // The light color set is the one that potentially contains the Windows HCM
304 // theme color/background (if we're using system colors and the user is
305 // using a High Contrast theme), and also the colors that as of today we
306 // allow setting in about:preferences.
307 glean::a11y::hcm_foreground
.Set(sContentPrefs
.mLightColors
.mDefault
);
308 glean::a11y::hcm_background
.Set(
309 sContentPrefs
.mLightColors
.mDefaultBackground
);
312 glean::a11y::backplate
.Set(StaticPrefs::browser_display_permit_backplate());
313 glean::a11y::use_system_colors
.Set(
314 StaticPrefs::browser_display_use_system_colors());
315 glean::a11y::always_underline_links
.Set(
316 StaticPrefs::layout_css_always_underline_links());
319 bool PreferenceSheet::AffectedByPref(const nsACString
& aPref
) {
320 const char* prefNames
[] = {
321 StaticPrefs::GetPrefName_privacy_resistFingerprinting(),
322 StaticPrefs::GetPrefName_ui_use_standins_for_native_colors(),
323 "browser.anchor_color",
324 "browser.active_color",
325 "browser.visited_color",
328 if (StringBeginsWith(aPref
, "browser.display."_ns
)) {
332 for (const char* pref
: prefNames
) {
333 if (aPref
.Equals(pref
)) {
341 } // namespace mozilla