Bug 1330876 - Listen to change event of preference "ui.use_standins_for_native_colors" and update default foreground and background colors draft
authorChung-Sheng Fu <cfu@mozilla.com>
Wed, 07 Jun 2017 18:11:44 +0800
changeset 590864 82e7d2e1e5724904a4c8e3834248b3ef619f7826
parent 579879 2c783a7b6d05b4b2b417bc5f21b7e40cbf3df077
child 592366 12c66dde33e08e088f5d2e80904361bd558a6fc5
child 592383 3b857574add35f068a014a5575c6abaf769cf782
push id62857
push userbmo:cfu@mozilla.com
push dateThu, 08 Jun 2017 07:45:32 +0000
bugs1330876
milestone55.0a1
Bug 1330876 - Listen to change event of preference "ui.use_standins_for_native_colors" and update default foreground and background colors MozReview-Commit-ID: BVFbGQVVNkf
dom/canvas/test/test_bug232227.html
layout/base/nsPresContext.cpp
--- a/dom/canvas/test/test_bug232227.html
+++ b/dom/canvas/test/test_bug232227.html
@@ -62,30 +62,30 @@ function beginTest() {
 		[ "-moz-Field", 0xFF, 0xFF, 0xFF ],
 		[ "-moz-FieldText", 0x00, 0x00, 0x00 ],
 		[ "-moz-MenuHover", 0x33, 0x99, 0xFF ],
 		[ "-moz-MenuHoverText", 0x00, 0x00, 0x00 ],
 		[ "-moz-MenubarText", 0x00, 0x00, 0x00 ],
     [ "-moz-MenubarHoverText", 0x00, 0x00, 0x00 ],
 		[ "-moz-NativeHyperlinkText", 0x00, 0x66, 0xCC ],
 		[ "-moz-OddTreeRow", 0xFF, 0xFF, 0xFF ],
-		[ "-moz-html-CellHighlight", 0x33, 0x99, 0xFF ],
-		[ "-moz-html-CellHighlightText", 0xFF, 0xFF, 0xFF ],
 		[ "-moz-mac-chrome-active", 0xB2, 0xB2, 0xB2 ],
 		[ "-moz-mac-chrome-inactive", 0xE1, 0xE1, 0xE1 ],
 		[ "-moz-mac-focusring", 0x60, 0x9D, 0xD7 ],
 		[ "-moz-mac-menuselect", 0x38, 0x75, 0xD7 ],
 		[ "-moz-mac-menushadow", 0xA3, 0xA3, 0xA3 ],
 		[ "-moz-mac-menutextdisable", 0x88, 0x88, 0x88 ],
 		[ "-moz-mac-menutextselect", 0xFF, 0xFF, 0xFF ],
 		[ "-moz-mac-DisabledToolbarText", 0x3F, 0x3F, 0x3F ],
 		[ "-moz-mac-AlternatePrimaryHighlight", 0x3F, 0x3F, 0x3F ],
 		[ "-moz-mac-SecondaryHighlight", 0xD4, 0xD4, 0xD4 ],
 		[ "-moz-win-MediaText", 0xFF, 0xFF, 0xFF ],
 		[ "-moz-win-CommunicationsText", 0xFF, 0xFF, 0xFF ],
+		[ "-moz-html-CellHighlight", 0x33, 0x99, 0xFF ],
+		[ "-moz-html-CellHighlightText", 0xFF, 0xFF, 0xFF ],
 
 		// These five are configured via Tools -> Options -> Content -> Colors.
 		//"-moz-ActiveHyperlinkText",
 		//"-moz-HyperLinkText",
 		//"-moz-VisitedHyperlinkText",
 		//"-moz-default-background-color",
 		//"-moz-default-color",
 	];
--- a/layout/base/nsPresContext.cpp
+++ b/layout/base/nsPresContext.cpp
@@ -95,16 +95,20 @@
 #include "mozilla/dom/URL.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 using namespace mozilla::layers;
 
 uint8_t gNotifySubDocInvalidationData;
 
+// This preference was first introduced in Bug 232227, in order to prevent
+// system colors from being exposed to CSS or canvas.
+constexpr char kUseStandinsForNativeColors[] = "ui.use_standins_for_native_colors";
+
 /**
  * Layer UserData for ContainerLayers that want to be notified
  * of local invalidations of them and their descendant layers.
  * Pass a callback to ComputeDifferences to have these called.
  */
 class ContainerLayerPresContext : public LayerUserData {
 public:
   nsPresContext* mPresContext;
@@ -375,16 +379,19 @@ nsPresContext::Destroy()
                                   "layout.css.devPixelsPerPx",
                                   this);
   Preferences::UnregisterCallback(nsPresContext::PrefChangedCallback,
                                   "nglayout.debug.paint_flashing",
                                   this);
   Preferences::UnregisterCallback(nsPresContext::PrefChangedCallback,
                                   "nglayout.debug.paint_flashing_chrome",
                                   this);
+  Preferences::UnregisterCallback(nsPresContext::PrefChangedCallback,
+                                  kUseStandinsForNativeColors,
+                                  this);
 
   mRefreshDriver = nullptr;
 }
 
 nsPresContext::~nsPresContext()
 {
   NS_PRECONDITION(!mShell, "Presshell forgot to clear our mShell pointer");
   DetachShell();
@@ -462,21 +469,27 @@ nsPresContext::GetDocumentColorPreferenc
   // but in some reference tests, that is not the case.
   gfxPrefs::GetSingleton();
 
   int32_t useAccessibilityTheme = 0;
   bool usePrefColors = true;
   bool isChromeDocShell = false;
   static int32_t sDocumentColorsSetting;
   static bool sDocumentColorsSettingPrefCached = false;
+  static bool sUseStandinsForNativeColors = false;
   if (!sDocumentColorsSettingPrefCached) {
     sDocumentColorsSettingPrefCached = true;
     Preferences::AddIntVarCache(&sDocumentColorsSetting,
                                 "browser.display.document_color_use",
                                 0);
+
+    // The preference "ui.use_standins_for_native_colors" also affects
+    // default foreground and background colors.
+    Preferences::AddBoolVarCache(&sUseStandinsForNativeColors,
+                                 kUseStandinsForNativeColors);
   }
 
   nsIDocument* doc = mDocument->GetDisplayDocument();
   if (doc && doc->GetDocShell()) {
     isChromeDocShell = nsIDocShellTreeItem::typeChrome ==
                        doc->GetDocShell()->ItemType();
   } else {
     nsCOMPtr<nsIDocShellTreeItem> docShell(mContainer);
@@ -495,17 +508,28 @@ nsPresContext::GetDocumentColorPreferenc
       LookAndFeel::GetInt(LookAndFeel::eIntID_UseAccessibilityTheme, 0);
     usePrefColors = !useAccessibilityTheme;
   }
   if (usePrefColors) {
     usePrefColors =
       !Preferences::GetBool("browser.display.use_system_colors", false);
   }
 
-  if (usePrefColors) {
+  if (sUseStandinsForNativeColors) {
+    // Once the preference "ui.use_standins_for_native_colors" is enabled,
+    // use fixed color values instead of prefered colors and system colors.
+    if (NS_FAILED(LookAndFeel::GetColor(
+        LookAndFeel::eColorID_windowtext, true, &mDefaultColor))) {
+      mDefaultColor = NS_RGB(0x00, 0x00, 0x00);
+    }
+    if (NS_FAILED(LookAndFeel::GetColor(
+        LookAndFeel::eColorID_window, true, &mBackgroundColor))) {
+      mBackgroundColor = NS_RGB(0xff, 0xff, 0xff);
+    }
+  } else if (usePrefColors) {
     nsAdoptingString colorStr =
       Preferences::GetString("browser.display.foreground_color");
 
     if (!colorStr.IsEmpty()) {
       mDefaultColor = MakeColorPref(colorStr);
     }
 
     colorStr = Preferences::GetString("browser.display.background_color");
@@ -917,16 +941,19 @@ nsPresContext::Init(nsDeviceContext* aDe
                                 "layout.css.devPixelsPerPx",
                                 this);
   Preferences::RegisterCallback(nsPresContext::PrefChangedCallback,
                                 "nglayout.debug.paint_flashing",
                                 this);
   Preferences::RegisterCallback(nsPresContext::PrefChangedCallback,
                                 "nglayout.debug.paint_flashing_chrome",
                                 this);
+  Preferences::RegisterCallback(nsPresContext::PrefChangedCallback,
+                                kUseStandinsForNativeColors,
+                                this);
 
   nsresult rv = mEventManager->Init();
   NS_ENSURE_SUCCESS(rv, rv);
 
   mEventManager->SetPresContext(this);
 
 #ifdef RESTYLE_LOGGING
   mRestyleLoggingEnabled = GeckoRestyleManager::RestyleLoggingInitiallyEnabled();