2019-03-07 00:34:30 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "PreferenceSheet.h"
|
|
|
|
|
|
|
|
#include "ServoCSSParser.h"
|
|
|
|
#include "MainThreadUtils.h"
|
2020-01-08 04:21:30 +03:00
|
|
|
#include "mozilla/Encoding.h"
|
2019-07-26 04:10:23 +03:00
|
|
|
#include "mozilla/StaticPrefs_browser.h"
|
2019-08-28 16:09:36 +03:00
|
|
|
#include "mozilla/StaticPrefs_devtools.h"
|
2019-04-17 01:32:53 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2019-03-07 00:34:30 +03:00
|
|
|
#include "mozilla/LookAndFeel.h"
|
|
|
|
#include "mozilla/dom/Document.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
2019-07-11 00:46:21 +03:00
|
|
|
#define AVG2(a, b) (((a) + (b) + 1) >> 1)
|
|
|
|
|
2019-03-07 00:34:30 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2019-11-30 14:57:10 +03:00
|
|
|
using dom::Document;
|
|
|
|
|
2019-03-07 00:34:30 +03:00
|
|
|
bool PreferenceSheet::sInitialized;
|
|
|
|
PreferenceSheet::Prefs PreferenceSheet::sContentPrefs;
|
|
|
|
PreferenceSheet::Prefs PreferenceSheet::sChromePrefs;
|
|
|
|
|
|
|
|
static void GetColor(const char* aPrefName, nscolor& aColor) {
|
2020-01-08 04:21:30 +03:00
|
|
|
nsAutoCString value;
|
|
|
|
Preferences::GetCString(aPrefName, value);
|
|
|
|
if (value.IsEmpty() || Encoding::UTF8ValidUpTo(value) != value.Length()) {
|
2019-03-07 00:34:30 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
nscolor result;
|
|
|
|
if (!ServoCSSParser::ComputeColor(nullptr, NS_RGB(0, 0, 0), value, &result)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
aColor = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PreferenceSheet::ShouldUseChromePrefs(const Document& aDoc) {
|
2019-08-28 16:09:36 +03:00
|
|
|
// DevTools documents run in a content frame but should temporarily use
|
|
|
|
// chrome preferences, in particular to avoid applying High Contrast mode
|
|
|
|
// colors. See Bug 1575766.
|
|
|
|
if (aDoc.IsDevToolsDocument() &&
|
|
|
|
StaticPrefs::devtools_toolbox_force_chrome_prefs()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aDoc.IsInChromeDocShell()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aDoc.IsBeingUsedAsImage() && aDoc.IsDocumentURISchemeChrome()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2019-03-07 00:34:30 +03:00
|
|
|
}
|
|
|
|
|
2019-03-07 00:36:12 +03:00
|
|
|
static bool UseAccessibilityTheme(bool aIsChrome) {
|
2019-03-07 00:34:30 +03:00
|
|
|
return !aIsChrome &&
|
|
|
|
!!LookAndFeel::GetInt(LookAndFeel::eIntID_UseAccessibilityTheme, 0);
|
|
|
|
}
|
|
|
|
|
2019-11-15 16:39:08 +03:00
|
|
|
static bool UseDocumentColors(bool aIsChrome, bool aUseAcccessibilityTheme) {
|
|
|
|
switch (StaticPrefs::browser_display_document_color_use()) {
|
|
|
|
case 1:
|
|
|
|
return true;
|
|
|
|
case 2:
|
|
|
|
return aIsChrome;
|
|
|
|
default:
|
|
|
|
return !aUseAcccessibilityTheme;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 00:34:30 +03:00
|
|
|
void PreferenceSheet::Prefs::Load(bool aIsChrome) {
|
|
|
|
*this = {};
|
|
|
|
|
2019-03-07 00:36:12 +03:00
|
|
|
mIsChrome = aIsChrome;
|
|
|
|
mUseAccessibilityTheme = UseAccessibilityTheme(aIsChrome);
|
2019-03-07 00:34:30 +03:00
|
|
|
mUnderlineLinks = StaticPrefs::browser_underline_anchors();
|
|
|
|
|
|
|
|
mUseFocusColors = StaticPrefs::browser_display_use_focus_colors();
|
|
|
|
mFocusRingWidth = StaticPrefs::browser_display_focus_ring_width();
|
|
|
|
mFocusRingStyle = StaticPrefs::browser_display_focus_ring_style();
|
|
|
|
mFocusRingOnAnything = StaticPrefs::browser_display_focus_ring_on_anything();
|
|
|
|
|
2019-03-07 00:36:12 +03:00
|
|
|
const bool usePrefColors = !aIsChrome && !mUseAccessibilityTheme &&
|
2019-03-07 00:34:30 +03:00
|
|
|
!StaticPrefs::browser_display_use_system_colors();
|
|
|
|
|
|
|
|
if (nsContentUtils::UseStandinsForNativeColors()) {
|
|
|
|
mDefaultColor = LookAndFeel::GetColorUsingStandins(
|
2019-05-26 16:10:00 +03:00
|
|
|
LookAndFeel::ColorID::Windowtext, mDefaultColor);
|
2019-03-07 00:34:30 +03:00
|
|
|
mDefaultBackgroundColor = LookAndFeel::GetColorUsingStandins(
|
2019-05-26 16:10:00 +03:00
|
|
|
LookAndFeel::ColorID::Window, mDefaultBackgroundColor);
|
2019-07-11 00:46:21 +03:00
|
|
|
mLinkColor = LookAndFeel::GetColorUsingStandins(
|
|
|
|
LookAndFeel::ColorID::MozNativehyperlinktext, mLinkColor);
|
2019-03-07 00:34:30 +03:00
|
|
|
} else if (usePrefColors) {
|
|
|
|
GetColor("browser.display.background_color", mDefaultBackgroundColor);
|
|
|
|
GetColor("browser.display.foreground_color", mDefaultColor);
|
2019-07-11 00:46:21 +03:00
|
|
|
GetColor("browser.anchor_color", mLinkColor);
|
2019-03-07 00:34:30 +03:00
|
|
|
} else {
|
|
|
|
mDefaultColor = LookAndFeel::GetColor(
|
2019-05-26 16:10:00 +03:00
|
|
|
LookAndFeel::ColorID::WindowForeground, mDefaultColor);
|
2019-03-07 00:34:30 +03:00
|
|
|
mDefaultBackgroundColor = LookAndFeel::GetColor(
|
2019-05-26 16:10:00 +03:00
|
|
|
LookAndFeel::ColorID::WindowBackground, mDefaultBackgroundColor);
|
2019-07-11 00:46:21 +03:00
|
|
|
mLinkColor = LookAndFeel::GetColor(
|
|
|
|
LookAndFeel::ColorID::MozNativehyperlinktext, mLinkColor);
|
2019-03-07 00:34:30 +03:00
|
|
|
}
|
|
|
|
|
2019-07-11 00:46:21 +03:00
|
|
|
if (mUseAccessibilityTheme && !nsContentUtils::UseStandinsForNativeColors()) {
|
|
|
|
mActiveLinkColor = mLinkColor;
|
|
|
|
// Visited link color is produced by preserving the foreground's green
|
|
|
|
// and averaging the foreground and background for the red and blue.
|
|
|
|
// This is how IE and Edge do it too.
|
|
|
|
mVisitedLinkColor = NS_RGB(
|
|
|
|
AVG2(NS_GET_R(mDefaultColor), NS_GET_R(mDefaultBackgroundColor)),
|
|
|
|
NS_GET_G(mDefaultColor),
|
|
|
|
AVG2(NS_GET_B(mDefaultColor), NS_GET_B(mDefaultBackgroundColor)));
|
|
|
|
} else {
|
|
|
|
GetColor("browser.active_color", mActiveLinkColor);
|
|
|
|
GetColor("browser.visited_color", mVisitedLinkColor);
|
|
|
|
}
|
2019-03-07 00:34:30 +03:00
|
|
|
|
|
|
|
GetColor("browser.display.focus_text_color", mFocusTextColor);
|
|
|
|
GetColor("browser.display.focus_background_color", mFocusBackgroundColor);
|
|
|
|
|
|
|
|
// Wherever we got the default background color from, ensure it is
|
|
|
|
// opaque.
|
|
|
|
mDefaultBackgroundColor =
|
|
|
|
NS_ComposeColors(NS_RGB(0xFF, 0xFF, 0xFF), mDefaultBackgroundColor);
|
2019-11-15 16:39:08 +03:00
|
|
|
mUseDocumentColors = UseDocumentColors(aIsChrome, mUseAccessibilityTheme);
|
2019-03-07 00:34:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PreferenceSheet::Initialize() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!sInitialized);
|
|
|
|
|
|
|
|
sInitialized = true;
|
|
|
|
|
|
|
|
sContentPrefs.Load(false);
|
|
|
|
sChromePrefs.Load(true);
|
2019-04-17 01:32:53 +03:00
|
|
|
|
|
|
|
nsAutoString useDocumentColorPref;
|
|
|
|
switch (StaticPrefs::browser_display_document_color_use()) {
|
|
|
|
case 1:
|
|
|
|
useDocumentColorPref.AssignLiteral("always");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
useDocumentColorPref.AssignLiteral("never");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
useDocumentColorPref.AssignLiteral("default");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Telemetry::ScalarSet(Telemetry::ScalarID::A11Y_THEME, useDocumentColorPref,
|
|
|
|
UseAccessibilityTheme(false));
|
2019-10-04 19:44:49 +03:00
|
|
|
|
|
|
|
Telemetry::ScalarSet(Telemetry::ScalarID::A11Y_BACKPLATE,
|
|
|
|
StaticPrefs::browser_display_permit_backplate());
|
2019-03-07 00:34:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
2019-07-11 00:46:21 +03:00
|
|
|
|
|
|
|
#undef AVG2
|