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/. */
6 
7 /*
8  * methods for dealing with CSS properties and tables of the keyword
9  * values they accept
10  */
11 
12 #include "nsCSSProps.h"
13 
14 #include "mozilla/ArrayUtils.h"
15 #include "mozilla/Casting.h"
16 
17 #include "gfxPlatform.h"
18 #include "nsLayoutUtils.h"
19 #include "nsIWidget.h"
20 #include "nsStyleConsts.h"  // For system widget appearance types
21 
22 #include "mozilla/dom/Animation.h"
23 #include "mozilla/dom/AnimationEffectBinding.h"  // for PlaybackDirection
24 #include "mozilla/gfx/gfxVars.h"                 // for UseWebRender
25 #include "mozilla/gfx/gfxVarReceiver.h"
26 #include "mozilla/LookAndFeel.h"  // for system colors
27 
28 #include "nsString.h"
29 #include "nsStaticNameTable.h"
30 
31 #include "mozilla/Preferences.h"
32 #include "mozilla/StaticPrefs_layout.h"
33 
34 using namespace mozilla;
35 
36 static int32_t gPropertyTableRefCount;
37 static nsStaticCaseInsensitiveNameTable* gFontDescTable;
38 static nsStaticCaseInsensitiveNameTable* gCounterDescTable;
39 static nsTHashMap<nsCStringHashKey, nsCSSPropertyID>* gPropertyIDLNameTable;
40 
41 static const char* const kCSSRawFontDescs[] = {
42 #define CSS_FONT_DESC(name_, method_) #name_,
43 #include "nsCSSFontDescList.h"
44 #undef CSS_FONT_DESC
45 };
46 
47 static const char* const kCSSRawCounterDescs[] = {
48 #define CSS_COUNTER_DESC(name_, method_) #name_,
49 #include "nsCSSCounterDescList.h"
50 #undef CSS_COUNTER_DESC
51 };
52 
CreateStaticTable(const char * const aRawTable[],int32_t aLength)53 static nsStaticCaseInsensitiveNameTable* CreateStaticTable(
54     const char* const aRawTable[], int32_t aLength) {
55   auto table = new nsStaticCaseInsensitiveNameTable(aRawTable, aLength);
56 #ifdef DEBUG
57   // Partially verify the entries.
58   for (int32_t index = 0; index < aLength; ++index) {
59     nsAutoCString temp(aRawTable[index]);
60     MOZ_ASSERT(-1 == temp.FindChar('_'),
61                "underscore char in case insensitive name table");
62   }
63 #endif
64   return table;
65 }
66 
RecomputeEnabledState(const char * aPref,void *)67 void nsCSSProps::RecomputeEnabledState(const char* aPref, void*) {
68   MOZ_RELEASE_ASSERT(NS_IsMainThread());
69   DebugOnly<bool> foundPref = false;
70   for (const PropertyPref* pref = kPropertyPrefTable;
71        pref->mPropID != eCSSProperty_UNKNOWN; pref++) {
72     if (!aPref || !strcmp(aPref, pref->mPref)) {
73       foundPref = true;
74 #ifdef FUZZING
75       gPropertyEnabled[pref->mPropID] = true;
76 #else
77       gPropertyEnabled[pref->mPropID] = Preferences::GetBool(pref->mPref);
78 #endif
79       if (pref->mPropID == eCSSProperty_backdrop_filter) {
80         gPropertyEnabled[pref->mPropID] &=
81             gfx::gfxVars::GetUseWebRenderOrDefault();
82       }
83     }
84   }
85   MOZ_ASSERT(foundPref);
86 }
87 
AddRefTable(void)88 void nsCSSProps::AddRefTable(void) {
89   if (0 == gPropertyTableRefCount++) {
90     MOZ_ASSERT(!gFontDescTable, "pre existing array!");
91     MOZ_ASSERT(!gCounterDescTable, "pre existing array!");
92     MOZ_ASSERT(!gPropertyIDLNameTable, "pre existing array!");
93 
94     gFontDescTable = CreateStaticTable(kCSSRawFontDescs, eCSSFontDesc_COUNT);
95     gCounterDescTable =
96         CreateStaticTable(kCSSRawCounterDescs, eCSSCounterDesc_COUNT);
97 
98     gPropertyIDLNameTable = new nsTHashMap<nsCStringHashKey, nsCSSPropertyID>;
99     for (nsCSSPropertyID p = nsCSSPropertyID(0);
100          size_t(p) < ArrayLength(kIDLNameTable); p = nsCSSPropertyID(p + 1)) {
101       if (kIDLNameTable[p]) {
102         gPropertyIDLNameTable->InsertOrUpdate(
103             nsDependentCString(kIDLNameTable[p]), p);
104       }
105     }
106 
107     static bool prefObserversInited = false;
108     if (!prefObserversInited) {
109       prefObserversInited = true;
110       for (const PropertyPref* pref = kPropertyPrefTable;
111            pref->mPropID != eCSSProperty_UNKNOWN; pref++) {
112         // https://bugzilla.mozilla.org/show_bug.cgi?id=1472523
113         // We need to use nsCString instead of substring because the preference
114         // callback code stores them. Using AssignLiteral prevents any
115         // unnecessary allocations.
116         nsCString prefName;
117         prefName.AssignLiteral(pref->mPref, strlen(pref->mPref));
118         Preferences::RegisterCallback(nsCSSProps::RecomputeEnabledState,
119                                       prefName);
120       }
121       RecomputeEnabledState(/* aPrefName = */ nullptr);
122     }
123   }
124 }
125 
ReleaseTable(void)126 void nsCSSProps::ReleaseTable(void) {
127   if (0 == --gPropertyTableRefCount) {
128     delete gFontDescTable;
129     gFontDescTable = nullptr;
130 
131     delete gCounterDescTable;
132     gCounterDescTable = nullptr;
133 
134     delete gPropertyIDLNameTable;
135     gPropertyIDLNameTable = nullptr;
136   }
137 }
138 
139 /* static */
IsCustomPropertyName(const nsACString & aProperty)140 bool nsCSSProps::IsCustomPropertyName(const nsACString& aProperty) {
141   return aProperty.Length() >= CSS_CUSTOM_NAME_PREFIX_LENGTH &&
142          StringBeginsWith(aProperty, "--"_ns);
143 }
144 
LookupPropertyByIDLName(const nsACString & aPropertyIDLName,EnabledState aEnabled)145 nsCSSPropertyID nsCSSProps::LookupPropertyByIDLName(
146     const nsACString& aPropertyIDLName, EnabledState aEnabled) {
147   MOZ_ASSERT(gPropertyIDLNameTable, "no lookup table, needs addref");
148   nsCSSPropertyID res;
149   if (!gPropertyIDLNameTable->Get(aPropertyIDLName, &res)) {
150     return eCSSProperty_UNKNOWN;
151   }
152   MOZ_ASSERT(res < eCSSProperty_COUNT);
153   if (!IsEnabled(res, aEnabled)) {
154     return eCSSProperty_UNKNOWN;
155   }
156   return res;
157 }
158 
LookupFontDesc(const nsACString & aFontDesc)159 nsCSSFontDesc nsCSSProps::LookupFontDesc(const nsACString& aFontDesc) {
160   MOZ_ASSERT(gFontDescTable, "no lookup table, needs addref");
161   nsCSSFontDesc which = nsCSSFontDesc(gFontDescTable->Lookup(aFontDesc));
162 
163   if (which == eCSSFontDesc_Display &&
164       !StaticPrefs::layout_css_font_display_enabled()) {
165     which = eCSSFontDesc_UNKNOWN;
166   }
167   return which;
168 }
169 
GetStringValue(nsCSSFontDesc aFontDescID)170 const nsCString& nsCSSProps::GetStringValue(nsCSSFontDesc aFontDescID) {
171   MOZ_ASSERT(gFontDescTable, "no lookup table, needs addref");
172   if (gFontDescTable) {
173     return gFontDescTable->GetStringValue(int32_t(aFontDescID));
174   } else {
175     static nsDependentCString sNullStr("");
176     return sNullStr;
177   }
178 }
179 
GetStringValue(nsCSSCounterDesc aCounterDesc)180 const nsCString& nsCSSProps::GetStringValue(nsCSSCounterDesc aCounterDesc) {
181   MOZ_ASSERT(gCounterDescTable, "no lookup table, needs addref");
182   if (gCounterDescTable) {
183     return gCounterDescTable->GetStringValue(int32_t(aCounterDesc));
184   } else {
185     static nsDependentCString sNullStr("");
186     return sNullStr;
187   }
188 }
189 const CSSPropFlags nsCSSProps::kFlagsTable[eCSSProperty_COUNT] = {
190 #define CSS_PROP_LONGHAND(name_, id_, method_, flags_, ...) flags_,
191 #define CSS_PROP_SHORTHAND(name_, id_, method_, flags_, ...) flags_,
192 #include "mozilla/ServoCSSPropList.h"
193 #undef CSS_PROP_SHORTHAND
194 #undef CSS_PROP_LONGHAND
195 };
196 
197 /* static */
198 bool nsCSSProps::gPropertyEnabled[eCSSProperty_COUNT_with_aliases] = {
199 // If the property has any "ENABLED_IN" flag set, it is disabled by
200 // default. Note that, if a property has pref, whatever its default
201 // value is, it will later be changed in nsCSSProps::AddRefTable().
202 // If the property has "ENABLED_IN" flags but doesn't have a pref,
203 // it is an internal property which is disabled elsewhere.
204 #define IS_ENABLED_BY_DEFAULT(flags_) \
205   (!((flags_) & (CSSPropFlags::EnabledMask | CSSPropFlags::Inaccessible)))
206 
207 #define CSS_PROP_LONGHAND(name_, id_, method_, flags_, ...) \
208   IS_ENABLED_BY_DEFAULT(flags_),
209 #define CSS_PROP_SHORTHAND(name_, id_, method_, flags_, ...) \
210   IS_ENABLED_BY_DEFAULT(flags_),
211 #define CSS_PROP_ALIAS(...) true,
212 #include "mozilla/ServoCSSPropList.h"
213 #undef CSS_PROP_ALIAS
214 #undef CSS_PROP_SHORTHAND
215 #undef CSS_PROP_LONGHAND
216 
217 #undef IS_ENABLED_BY_DEFAULT
218 };
219 
220 /**
221  * A singleton class to register as a receiver for gfxVars.
222  * Updates the state of backdrop-filter's pref if the gfx
223  * WebRender var changes state.
224  */
225 class nsCSSPropsGfxVarReceiver final : public gfx::gfxVarReceiver {
226   constexpr nsCSSPropsGfxVarReceiver() = default;
227 
228   // WebRender's last known enabled state.
229   static bool sLastKnownUseWebRender;
230   static nsCSSPropsGfxVarReceiver sInstance;
231 
232  public:
GetInstance()233   static gfx::gfxVarReceiver& GetInstance() { return sInstance; }
234 
OnVarChanged(const gfx::GfxVarUpdate &)235   void OnVarChanged(const gfx::GfxVarUpdate&) override {
236     bool enabled = gfxVars::UseWebRender();
237     if (sLastKnownUseWebRender != enabled) {
238       sLastKnownUseWebRender = enabled;
239       nsCSSProps::RecomputeEnabledState("layout.css.backdrop-filter.enabled");
240     }
241   }
242 };
243 
244 /* static */
245 nsCSSPropsGfxVarReceiver nsCSSPropsGfxVarReceiver::sInstance =
246     nsCSSPropsGfxVarReceiver();
247 
248 /* static */
249 bool nsCSSPropsGfxVarReceiver::sLastKnownUseWebRender = false;
250 
251 /* static */
GfxVarReceiver()252 gfx::gfxVarReceiver& nsCSSProps::GfxVarReceiver() {
253   return nsCSSPropsGfxVarReceiver::GetInstance();
254 }
255 
256 #include "nsCSSPropsGenerated.inc"
257