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)
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
,
37 nsAutoCString darkPrefName
;
38 if (aColorScheme
== ColorScheme::Dark
) {
39 darkPrefName
.Append(aPrefName
);
40 darkPrefName
.AppendLiteral(".dark");
41 aPrefName
= darkPrefName
.get();
45 Preferences::GetCString(aPrefName
, value
);
46 if (value
.IsEmpty() || Encoding::UTF8ValidUpTo(value
) != value
.Length()) {
50 if (!ServoCSSParser::ComputeColor(nullptr, NS_RGB(0, 0, 0), value
, &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()) {
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
;
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
);
121 using ColorID
= LookAndFeel::ColorID
;
122 const auto standins
= LookAndFeel::UseStandins(mUseStandins
);
123 colors
.mDefault
= LookAndFeel::Color(ColorID::Windowtext
, scheme
, standins
,
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
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
)));
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() ||
165 auto PreferenceSheet::ColorSchemeSettingForChrome()
166 -> ChromeColorSchemeSetting
{
167 switch (StaticPrefs::browser_theme_toolbar_theme()) {
169 return ChromeColorSchemeSetting::Dark
;
171 return ChromeColorSchemeSetting::Light
;
173 return ChromeColorSchemeSetting::System
;
177 ColorScheme
PreferenceSheet::ThemeDerivedColorSchemeForContent() {
178 switch (StaticPrefs::browser_theme_content_theme()) {
180 return ColorScheme::Dark
;
182 return ColorScheme::Light
;
184 return LookAndFeel::SystemColorScheme();
188 void PreferenceSheet::Prefs::Load(bool aIsChrome
) {
191 mIsChrome
= aIsChrome
;
192 mUseAccessibilityTheme
=
193 LookAndFeel::GetInt(LookAndFeel::IntID::UseAccessibilityTheme
);
194 // Chrome documents always use system colors, not stand-ins, not forced, etc.
196 mUseDocumentColors
= UseDocumentColors(mUseAccessibilityTheme
);
197 mUsePrefColors
= !StaticPrefs::browser_display_use_system_colors();
198 mUseStandins
= UseStandinsForNativeColors();
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
;
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
213 mMustUseLightSystemColors
= mMustUseLightColorSet
= true;
219 switch (ColorSchemeSettingForChrome()) {
220 case ChromeColorSchemeSetting::Light
:
221 return ColorScheme::Light
;
222 case ChromeColorSchemeSetting::Dark
:
223 return ColorScheme::Dark
;
224 case ChromeColorSchemeSetting::System
:
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
233 return LookAndFeel::IsDarkColor(mLightColors
.mDefaultBackground
)
235 : ColorScheme::Light
;
237 switch (StaticPrefs::layout_css_prefers_color_scheme_content_override()) {
239 return ColorScheme::Dark
;
241 return ColorScheme::Light
;
243 return ThemeDerivedColorSchemeForContent();
248 void PreferenceSheet::Initialize() {
249 MOZ_ASSERT(NS_IsMainThread());
250 MOZ_ASSERT(!sInitialized
);
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
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()) {
277 useDocumentColorPref
.AssignLiteral("always");
280 useDocumentColorPref
.AssignLiteral("never");
283 useDocumentColorPref
.AssignLiteral("default");
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
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
)) {
330 for (const char* pref
: prefNames
) {
331 if (aPref
.Equals(pref
)) {
339 } // namespace mozilla