Ticket #18777: 0001-Bug-6786-Do-not-expose-system-colors-to-CSS-or-canva.patch

File 0001-Bug-6786-Do-not-expose-system-colors-to-CSS-or-canva.patch, 7.0 KB (added by mcs, 4 years ago)

rebased patch

  • layout/base/nsLayoutUtils.cpp

    From 2628d71cb7faf8c30dc893c67b9bbc5f94805098 Mon Sep 17 00:00:00 2001
    From: Kathy Brade <brade@pearlcrescent.com>
    Date: Wed, 13 Apr 2016 10:50:38 -0400
    Subject: [PATCH] Bug #6786: Do not expose system colors to CSS or canvas.
    
    Restore the portion of this fix that was not upstreamed as part of
    https://bugzilla.mozilla.org/show_bug.cgi?id=232227: use properly
    contrasting colors if the desktop theme specifies white on black
    for text colors (see bug 7920). These color choices are not exposed
    to content.
    ---
     layout/base/nsLayoutUtils.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++
     layout/base/nsLayoutUtils.h   |  4 ++++
     layout/generic/nsFrame.cpp    |  6 +++++-
     widget/LookAndFeel.h          |  3 +++
     widget/nsXPLookAndFeel.cpp    | 41 +++++++++++++++++++++++++++++++++++
     5 files changed, 103 insertions(+), 1 deletion(-)
    
    diff --git a/layout/base/nsLayoutUtils.cpp b/layout/base/nsLayoutUtils.cpp
    index 9c4d5e5..4fb2131 100644
    a b ShouldDarkenColors(nsPresContext* aPresContext) 
    54655465nscolor
    54665466nsLayoutUtils::GetColor(nsIFrame* aFrame, nsCSSProperty aProperty)
    54675467{
     5468  if (aProperty == eCSSProperty_color)
     5469  {
     5470    nscolor nativeColor = NS_RGB(0, 0, 0);
     5471    if (GetNativeTextColor(aFrame, nativeColor))
     5472      return nativeColor;
     5473  }
     5474
    54685475  nscolor color = aFrame->GetVisitedDependentColor(aProperty);
    54695476  if (ShouldDarkenColors(aFrame->PresContext())) {
    54705477    color = DarkenColor(color);
    54715478  }
     5479
    54725480  return color;
    54735481}
    54745482
     5483bool
     5484nsLayoutUtils::GetNativeTextColor(nsIFrame* aFrame, nscolor& aColor)
     5485{
     5486  nsPresContext *presContext = aFrame->PresContext();
     5487  if (!presContext->IsChrome()) {
     5488    // If native appearance was used to draw the background of the containing
     5489    // frame, return a contrasting native foreground color instead of the
     5490    // color from the element's style.  This avoids a problem where black
     5491    // text was displayed on a black background when a Windows theme such as
     5492    // "High Contrast Black" was used.  The background is drawn inside
     5493    // nsNativeThemeWin::ClassicDrawWidgetBackground().
     5494    //
     5495    // Because both the background color and this foreground color are used
     5496    // directly without exposing the colors via CSS computed styles, the
     5497    // native colors are not leaked to content.
     5498    nsIFrame* bgFrame =
     5499                    nsCSSRendering::FindNonTransparentBackgroundFrame(aFrame);
     5500    if (bgFrame) {
     5501      const nsStyleDisplay* displayData = bgFrame->StyleDisplay();
     5502      uint8_t widgetType = displayData->mAppearance;
     5503      nsITheme *theme = presContext->GetTheme();
     5504      if (theme && widgetType && theme->ThemeSupportsWidget(presContext,
     5505                                                            bgFrame,
     5506                                                            widgetType)) {
     5507        bool isDisabled = false;
     5508        nsIContent* frameContent = bgFrame->GetContent();
     5509        if (frameContent && frameContent->IsElement()) {
     5510          EventStates es = frameContent->AsElement()->State();
     5511          isDisabled = es.HasState(NS_EVENT_STATE_DISABLED);
     5512        }
     5513
     5514        if (NS_SUCCEEDED(LookAndFeel::GetColorForNativeAppearance(widgetType,
     5515                                                       isDisabled, &aColor))) {
     5516            return true;
     5517        }
     5518      }
     5519    }
     5520  }
     5521
     5522  return false;
     5523}
     5524
    54755525gfxFloat
    54765526nsLayoutUtils::GetSnappedBaselineY(nsIFrame* aFrame, gfxContext* aContext,
    54775527                                   nscoord aY, nscoord aAscent)
  • layout/base/nsLayoutUtils.h

    diff --git a/layout/base/nsLayoutUtils.h b/layout/base/nsLayoutUtils.h
    index 89a38bd..62ad4f7 100644
    a b public: 
    15631563  // Get a suitable foreground color for painting aProperty for aFrame.
    15641564  static nscolor GetColor(nsIFrame* aFrame, nsCSSProperty aProperty);
    15651565
     1566  // Get the native text color if appropriate.  If false is returned, callers
     1567  // should fallback to the CSS color.
     1568  static bool GetNativeTextColor(nsIFrame* aFrame, nscolor& aColor);
     1569
    15661570  // Get a baseline y position in app units that is snapped to device pixels.
    15671571  static gfxFloat GetSnappedBaselineY(nsIFrame* aFrame, gfxContext* aContext,
    15681572                                      nscoord aY, nscoord aAscent);
  • layout/generic/nsFrame.cpp

    diff --git a/layout/generic/nsFrame.cpp b/layout/generic/nsFrame.cpp
    index dc2a3e1..94c5c77 100644
    a b nsIFrame::DisplayCaret(nsDisplayListBuilder* aBuilder, 
    15801580nscolor
    15811581nsIFrame::GetCaretColorAt(int32_t aOffset)
    15821582{
    1583   // Use text color.
     1583  nscolor color = NS_RGB(0, 0, 0);
     1584  if (nsLayoutUtils::GetNativeTextColor(this, color))
     1585    return color;
     1586
     1587  // Use CSS text color.
    15841588  return StyleColor()->mColor;
    15851589}
    15861590
  • widget/LookAndFeel.h

    diff --git a/widget/LookAndFeel.h b/widget/LookAndFeel.h
    index ac2a991..0a43b76 100644
    a b public: 
    535535  static nsresult GetColor(ColorID aID, bool aUseStandinsForNativeColors,
    536536                           nscolor* aResult);
    537537
     538  static nsresult GetColorForNativeAppearance(uint8_t aWidgetType,
     539                                          bool aIsDisabled, nscolor* aResult);
     540
    538541  /**
    539542   * GetInt() and GetFloat() return a int or float value for aID.  The result
    540543   * might be distance, time, some flags or a int value which has particular
  • widget/nsXPLookAndFeel.cpp

    diff --git a/widget/nsXPLookAndFeel.cpp b/widget/nsXPLookAndFeel.cpp
    index 7144e8e..f9b6cae 100644
    a b  
    1111#include "nsLookAndFeel.h"
    1212#include "nsCRT.h"
    1313#include "nsFont.h"
     14#include "nsThemeConstants.h"
    1415#include "mozilla/dom/ContentChild.h"
    1516#include "mozilla/Preferences.h"
    1617#include "mozilla/gfx/2D.h"
    LookAndFeel::GetColor(ColorID aID, bool aUseStandinsForNativeColors, 
    906907
    907908// static
    908909nsresult
     910LookAndFeel::GetColorForNativeAppearance(uint8_t aWidgetType, bool aIsDisabled,
     911                                         nscolor* aResult)
     912{
     913  NS_ENSURE_ARG_POINTER(aResult);
     914
     915  ColorID colorID = eColorID_LAST_COLOR;
     916  switch (aWidgetType) {
     917    case NS_THEME_TEXTFIELD:
     918    case NS_THEME_TEXTFIELD_MULTILINE:
     919    case NS_THEME_LISTBOX:
     920    case NS_THEME_DROPDOWN:
     921    case NS_THEME_DROPDOWN_TEXTFIELD:
     922    case NS_THEME_TREEVIEW:
     923      colorID = (aIsDisabled) ? eColorID_graytext : eColorID__moz_fieldtext;
     924      break;
     925
     926    case NS_THEME_TOOLTIP:
     927      colorID = eColorID_infotext;
     928      break;
     929
     930    case NS_THEME_BUTTON:
     931    case NS_THEME_GROUPBOX:
     932    case NS_THEME_PROGRESSBAR:
     933    case NS_THEME_PROGRESSBAR_VERTICAL:
     934    case NS_THEME_TAB_PANEL:
     935    case NS_THEME_STATUSBAR:
     936    case NS_THEME_STATUSBAR_RESIZER_PANEL:
     937      colorID = (aIsDisabled) ? eColorID_graytext : eColorID_buttontext;
     938      break;
     939  }
     940
     941  if (LookAndFeel::eColorID_LAST_COLOR == colorID)
     942    return NS_ERROR_FAILURE;
     943
     944  *aResult = NS_RGB(0, 0, 0);
     945  return nsLookAndFeel::GetInstance()->NativeGetColor(colorID, *aResult);
     946}
     947
     948// static
     949nsresult
    909950LookAndFeel::GetInt(IntID aID, int32_t* aResult)
    910951{
    911952  return nsLookAndFeel::GetInstance()->GetIntImpl(aID, *aResult);