Bug 1717873 - Print documents shouldn't use system colors when in hcm. r=morgan
authorEmilio Cobos Álvarez <emilio@crisal.io>
Sat, 24 Jul 2021 21:10:44 +0000
changeset 586583 7b4b180bf39349c1b5b3da7a3c708f23b4740db2
parent 586582 dde0d94e689a04c12f0b386069465688f775caf0
child 586584 cdd6ccd2d2426e1551d31b316086409326f14aa0
push id38641
push userabutkovits@mozilla.com
push dateSun, 25 Jul 2021 21:23:34 +0000
treeherdermozilla-central@3881437c0949 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmorgan
bugs1717873
milestone92.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1717873 - Print documents shouldn't use system colors when in hcm. r=morgan Instead, use default colors. Testing this properly requires writing test infrastructure for paged tests with "print backgrounds" settings turned off, not sure if worth it. Depends on D120679 Differential Revision: https://phabricator.services.mozilla.com/D120680
layout/style/PreferenceSheet.cpp
layout/style/PreferenceSheet.h
widget/nsXPLookAndFeel.cpp
--- a/layout/style/PreferenceSheet.cpp
+++ b/layout/style/PreferenceSheet.cpp
@@ -22,48 +22,53 @@
 
 namespace mozilla {
 
 using dom::Document;
 
 bool PreferenceSheet::sInitialized;
 PreferenceSheet::Prefs PreferenceSheet::sContentPrefs;
 PreferenceSheet::Prefs PreferenceSheet::sChromePrefs;
+PreferenceSheet::Prefs PreferenceSheet::sPrintPrefs;
 
 static void GetColor(const char* aPrefName, nscolor& aColor) {
   nsAutoCString value;
   Preferences::GetCString(aPrefName, value);
   if (value.IsEmpty() || Encoding::UTF8ValidUpTo(value) != value.Length()) {
     return;
   }
   nscolor result;
   if (!ServoCSSParser::ComputeColor(nullptr, NS_RGB(0, 0, 0), value, &result)) {
     return;
   }
   aColor = result;
 }
 
-bool PreferenceSheet::ShouldUseChromePrefs(const Document& aDoc) {
+auto PreferenceSheet::PrefsKindFor(const Document& aDoc) -> PrefsKind {
   // 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;
+    return PrefsKind::Chrome;
   }
 
   if (aDoc.IsInChromeDocShell()) {
-    return true;
+    return PrefsKind::Chrome;
   }
 
   if (aDoc.IsBeingUsedAsImage() && aDoc.IsDocumentURISchemeChrome()) {
-    return true;
+    return PrefsKind::Chrome;
   }
 
-  return false;
+  if (aDoc.IsStaticDocument()) {
+    return PrefsKind::Print;
+  }
+
+  return PrefsKind::Content;
 }
 
 static bool UseAccessibilityTheme(bool aIsChrome) {
   return !aIsChrome &&
          !!LookAndFeel::GetInt(LookAndFeel::IntID::UseAccessibilityTheme, 0);
 }
 
 static bool UseDocumentColors(bool aIsChrome, bool aUseAcccessibilityTheme) {
@@ -137,16 +142,20 @@ void PreferenceSheet::Prefs::Load(bool a
 void PreferenceSheet::Initialize() {
   MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(!sInitialized);
 
   sInitialized = true;
 
   sContentPrefs.Load(false);
   sChromePrefs.Load(true);
+  sPrintPrefs = sContentPrefs;
+  if (!sPrintPrefs.mUseDocumentColors) {
+    sPrintPrefs.mColors = Prefs().mColors;
+  }
 
   nsAutoString useDocumentColorPref;
   switch (StaticPrefs::browser_display_document_color_use()) {
     case 1:
       useDocumentColorPref.AssignLiteral("always");
       break;
     case 2:
       useDocumentColorPref.AssignLiteral("never");
--- a/layout/style/PreferenceSheet.h
+++ b/layout/style/PreferenceSheet.h
@@ -64,24 +64,49 @@ struct PreferenceSheet {
     return sContentPrefs;
   }
 
   static Prefs& ChromePrefs() {
     MOZ_ASSERT(sInitialized);
     return sChromePrefs;
   }
 
-  static bool ShouldUseChromePrefs(const dom::Document&);
+  static Prefs& PrintPrefs() {
+    MOZ_ASSERT(sInitialized);
+    return sPrintPrefs;
+  }
+
+  enum class PrefsKind {
+    Chrome,
+    Print,
+    Content,
+  };
+
+  static PrefsKind PrefsKindFor(const dom::Document&);
+
+  static bool ShouldUseChromePrefs(const dom::Document& aDocument) {
+    return PrefsKindFor(aDocument) == PrefsKind::Chrome;
+  }
+
   static const Prefs& PrefsFor(const dom::Document& aDocument) {
-    return ShouldUseChromePrefs(aDocument) ? ChromePrefs() : ContentPrefs();
+    switch (PrefsKindFor(aDocument)) {
+      case PrefsKind::Chrome:
+        return ChromePrefs();
+      case PrefsKind::Print:
+        return PrintPrefs();
+      case PrefsKind::Content:
+        break;
+    }
+    return ContentPrefs();
   }
 
  private:
   static bool sInitialized;
+  static Prefs sChromePrefs;
+  static Prefs sPrintPrefs;
   static Prefs sContentPrefs;
-  static Prefs sChromePrefs;
 
   static void Initialize();
 };
 
 }  // namespace mozilla
 
 #endif
--- a/widget/nsXPLookAndFeel.cpp
+++ b/widget/nsXPLookAndFeel.cpp
@@ -1038,20 +1038,28 @@ static bool ColorIsCSSAccessible(LookAnd
       break;
   }
 
   return true;
 }
 
 LookAndFeel::UseStandins LookAndFeel::ShouldUseStandins(
     const dom::Document& aDoc, ColorID aId) {
-  return UseStandins(
-      ShouldUseStandinsForNativeColorForNonNativeTheme(aDoc, aId) ||
-      (nsContentUtils::UseStandinsForNativeColors() &&
-       !nsContentUtils::IsChromeDoc(&aDoc) && ColorIsCSSAccessible(aId)));
+  if (ShouldUseStandinsForNativeColorForNonNativeTheme(aDoc, aId)) {
+    return UseStandins::Yes;
+  }
+  if (nsContentUtils::UseStandinsForNativeColors() &&
+      !nsContentUtils::IsChromeDoc(&aDoc) && ColorIsCSSAccessible(aId)) {
+    return UseStandins::Yes;
+  }
+  if (aDoc.IsStaticDocument() &&
+      !PreferenceSheet::ContentPrefs().mUseDocumentColors) {
+    return UseStandins::Yes;
+  }
+  return UseStandins::No;
 }
 
 Maybe<nscolor> LookAndFeel::GetColor(ColorID aId, const dom::Document& aDoc) {
   return GetColor(aId, ColorSchemeForDocument(aDoc),
                   ShouldUseStandins(aDoc, aId));
 }
 
 Maybe<nscolor> LookAndFeel::GetColor(ColorID aId, const nsIFrame* aFrame) {