1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
/* -*- 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/. */
/* representation of CSSRuleList for stylo */
#include "mozilla/ServoCSSRuleList.h"
#include "mozilla/dom/CSSCounterStyleRule.h"
#include "mozilla/dom/CSSFontFaceRule.h"
#include "mozilla/dom/CSSFontFeatureValuesRule.h"
#include "mozilla/dom/CSSFontPaletteValuesRule.h"
#include "mozilla/dom/CSSImportRule.h"
#include "mozilla/dom/CSSLayerBlockRule.h"
#include "mozilla/dom/CSSLayerStatementRule.h"
#include "mozilla/dom/CSSKeyframesRule.h"
#include "mozilla/dom/CSSContainerRule.h"
#include "mozilla/dom/CSSMediaRule.h"
#include "mozilla/dom/CSSMozDocumentRule.h"
#include "mozilla/dom/CSSNamespaceRule.h"
#include "mozilla/dom/CSSPageRule.h"
#include "mozilla/dom/CSSPropertyRule.h"
#include "mozilla/dom/CSSStyleRule.h"
#include "mozilla/dom/CSSSupportsRule.h"
#include "mozilla/IntegerRange.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/StyleSheet.h"
#include "mozilla/dom/Document.h"
using namespace mozilla::dom;
namespace mozilla {
ServoCSSRuleList::ServoCSSRuleList(
already_AddRefed<StyleLockedCssRules> aRawRules, StyleSheet* aSheet,
css::GroupRule* aParentRule)
: mStyleSheet(aSheet), mParentRule(aParentRule), mRawRules(aRawRules) {
ResetRules();
}
// QueryInterface implementation for ServoCSSRuleList
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ServoCSSRuleList)
NS_INTERFACE_MAP_END_INHERITING(dom::CSSRuleList)
NS_IMPL_ADDREF_INHERITED(ServoCSSRuleList, dom::CSSRuleList)
NS_IMPL_RELEASE_INHERITED(ServoCSSRuleList, dom::CSSRuleList)
NS_IMPL_CYCLE_COLLECTION_CLASS(ServoCSSRuleList)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ServoCSSRuleList)
tmp->DropAllRules();
NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(dom::CSSRuleList)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ServoCSSRuleList,
dom::CSSRuleList)
tmp->EnumerateInstantiatedRules([&](css::Rule* aRule, uint32_t) {
if (!aRule->IsCCLeaf()) {
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mRules[i]");
cb.NoteXPCOMChild(aRule);
}
});
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
css::Rule* ServoCSSRuleList::GetRule(uint32_t aIndex) {
uintptr_t rule = mRules[aIndex];
if (rule <= kMaxRuleType) {
RefPtr<css::Rule> ruleObj = nullptr;
switch (StyleCssRuleType(rule)) {
#define CASE_RULE_WITH_PREFIX(const_, prefix_, name_) \
case StyleCssRuleType::const_: { \
uint32_t line = 0, column = 0; \
RefPtr<Style##prefix_##const_##Rule> raw = \
Servo_CssRules_Get##const_##RuleAt(mRawRules, aIndex, &line, &column) \
.Consume(); \
MOZ_ASSERT(raw); \
ruleObj = new CSS##name_##Rule(raw.forget(), mStyleSheet, mParentRule, \
line, column); \
MOZ_ASSERT(ruleObj->Type() == StyleCssRuleType(rule)); \
break; \
}
#define CASE_RULE_LOCKED(const_, name_) \
CASE_RULE_WITH_PREFIX(const_, Locked, name_)
#define CASE_RULE_UNLOCKED(const_, name_) CASE_RULE_WITH_PREFIX(const_, , name_)
CASE_RULE_LOCKED(Style, Style)
CASE_RULE_LOCKED(Keyframes, Keyframes)
CASE_RULE_UNLOCKED(Media, Media)
CASE_RULE_UNLOCKED(Namespace, Namespace)
CASE_RULE_LOCKED(Page, Page)
CASE_RULE_UNLOCKED(Property, Property)
CASE_RULE_UNLOCKED(Supports, Supports)
CASE_RULE_UNLOCKED(Document, MozDocument)
CASE_RULE_LOCKED(Import, Import)
CASE_RULE_UNLOCKED(FontFeatureValues, FontFeatureValues)
CASE_RULE_UNLOCKED(FontPaletteValues, FontPaletteValues)
CASE_RULE_LOCKED(FontFace, FontFace)
CASE_RULE_LOCKED(CounterStyle, CounterStyle)
CASE_RULE_UNLOCKED(LayerBlock, LayerBlock)
CASE_RULE_UNLOCKED(LayerStatement, LayerStatement)
CASE_RULE_UNLOCKED(Container, Container)
#undef CASE_RULE_LOCKED
#undef CASE_RULE_UNLOCKED
#undef CASE_RULE_WITH_PREFIX
case StyleCssRuleType::Keyframe:
MOZ_ASSERT_UNREACHABLE("keyframe rule cannot be here");
return nullptr;
case StyleCssRuleType::Margin:
// Margin rules not implemented yet, see bug 1864737
return nullptr;
}
rule = CastToUint(ruleObj.forget().take());
mRules[aIndex] = rule;
}
return CastToPtr(rule);
}
css::Rule* ServoCSSRuleList::IndexedGetter(uint32_t aIndex, bool& aFound) {
if (aIndex >= mRules.Length()) {
aFound = false;
return nullptr;
}
aFound = true;
return GetRule(aIndex);
}
template <typename Func>
void ServoCSSRuleList::EnumerateInstantiatedRules(Func aCallback) {
uint32_t index = 0;
for (uintptr_t rule : mRules) {
if (rule > kMaxRuleType) {
aCallback(CastToPtr(rule), index);
}
index++;
}
}
static void DropRule(already_AddRefed<css::Rule> aRule) {
RefPtr<css::Rule> rule = aRule;
rule->DropReferences();
}
void ServoCSSRuleList::ResetRules() {
// DropRule could reenter here via the cycle collector.
auto rules = std::move(mRules);
for (uintptr_t rule : rules) {
if (rule > kMaxRuleType) {
DropRule(already_AddRefed<css::Rule>(CastToPtr(rule)));
}
}
MOZ_ASSERT(mRules.IsEmpty());
if (mRawRules) {
Servo_CssRules_ListTypes(mRawRules, &mRules);
}
}
void ServoCSSRuleList::DropAllRules() {
mStyleSheet = nullptr;
mParentRule = nullptr;
mRawRules = nullptr;
ResetRules();
}
void ServoCSSRuleList::DropSheetReference() {
// If mStyleSheet is not set on this rule list, then it is not set on any of
// its instantiated rules either. To avoid O(N^2) beavhior in the depth of
// group rule nesting, which can happen if we are unlinked starting from the
// deepest nested group rule, skip recursing into the rule list if we know we
// don't need to.
if (!mStyleSheet) {
return;
}
mStyleSheet = nullptr;
EnumerateInstantiatedRules(
[](css::Rule* rule, uint32_t) { rule->DropSheetReference(); });
}
void ServoCSSRuleList::DropParentRuleReference() {
mParentRule = nullptr;
EnumerateInstantiatedRules(
[](css::Rule* rule, uint32_t) { rule->DropParentRuleReference(); });
}
nsresult ServoCSSRuleList::InsertRule(const nsACString& aRule,
uint32_t aIndex) {
MOZ_ASSERT(mStyleSheet,
"Caller must ensure that "
"the list is not unlinked from stylesheet");
if (!mRawRules || IsReadOnly()) {
return NS_OK;
}
mStyleSheet->WillDirty();
css::Loader* loader = nullptr;
auto allowImportRules = mStyleSheet->SelfOrAncestorIsConstructed()
? StyleAllowImportRules::No
: StyleAllowImportRules::Yes;
// TODO(emilio, bug 1535456): Should probably always be able to get a handle
// to some loader if we're parsing an @import rule, but which one?
//
// StyleSheet::ReparseSheet just mints a new loader, but that'd be wrong in
// this case I think, since such a load will bypass CSP checks.
if (Document* doc = mStyleSheet->GetAssociatedDocument()) {
loader = doc->CSSLoader();
}
StyleCssRuleType type;
uint32_t containingTypes = 0;
for (css::Rule* rule = mParentRule; rule; rule = rule->GetParentRule()) {
containingTypes |= (1 << uint32_t(rule->Type()));
}
nsresult rv = Servo_CssRules_InsertRule(
mRawRules, mStyleSheet->RawContents(), &aRule, aIndex, containingTypes,
loader, allowImportRules, mStyleSheet, &type);
NS_ENSURE_SUCCESS(rv, rv);
mRules.InsertElementAt(aIndex, uintptr_t(type));
return rv;
}
nsresult ServoCSSRuleList::DeleteRule(uint32_t aIndex) {
if (!mRawRules || IsReadOnly()) {
return NS_OK;
}
nsresult rv = Servo_CssRules_DeleteRule(mRawRules, aIndex);
if (!NS_FAILED(rv)) {
uintptr_t rule = mRules[aIndex];
mRules.RemoveElementAt(aIndex);
if (rule > kMaxRuleType) {
DropRule(already_AddRefed<css::Rule>(CastToPtr(rule)));
}
}
return rv;
}
void ServoCSSRuleList::SetRawContents(RefPtr<StyleLockedCssRules> aNewRules,
bool aFromClone) {
mRawRules = std::move(aNewRules);
if (!aFromClone) {
ResetRules();
return;
}
EnumerateInstantiatedRules([&](css::Rule* aRule, uint32_t aIndex) {
#define RULE_CASE_WITH_PREFIX(constant_, prefix_, type_) \
case StyleCssRuleType::constant_: { \
uint32_t line = 0, column = 0; \
RefPtr<Style##prefix_##constant_##Rule> raw = \
Servo_CssRules_Get##constant_##RuleAt(mRawRules, aIndex, &line, \
&column) \
.Consume(); \
static_cast<dom::CSS##type_##Rule*>(aRule)->SetRawAfterClone( \
std::move(raw)); \
break; \
}
#define RULE_CASE_LOCKED(constant_, type_) \
RULE_CASE_WITH_PREFIX(constant_, Locked, type_)
#define RULE_CASE_UNLOCKED(constant_, type_) \
RULE_CASE_WITH_PREFIX(constant_, , type_)
switch (aRule->Type()) {
RULE_CASE_LOCKED(Style, Style)
RULE_CASE_LOCKED(Keyframes, Keyframes)
RULE_CASE_UNLOCKED(Media, Media)
RULE_CASE_UNLOCKED(Namespace, Namespace)
RULE_CASE_LOCKED(Page, Page)
RULE_CASE_UNLOCKED(Property, Property)
RULE_CASE_UNLOCKED(Supports, Supports)
RULE_CASE_UNLOCKED(Document, MozDocument)
RULE_CASE_LOCKED(Import, Import)
RULE_CASE_UNLOCKED(FontFeatureValues, FontFeatureValues)
RULE_CASE_UNLOCKED(FontPaletteValues, FontPaletteValues)
RULE_CASE_LOCKED(FontFace, FontFace)
RULE_CASE_LOCKED(CounterStyle, CounterStyle)
RULE_CASE_UNLOCKED(LayerBlock, LayerBlock)
RULE_CASE_UNLOCKED(LayerStatement, LayerStatement)
RULE_CASE_UNLOCKED(Container, Container)
case StyleCssRuleType::Keyframe:
MOZ_ASSERT_UNREACHABLE("keyframe rule cannot be here");
break;
case StyleCssRuleType::Margin:
// Margin rules not implemented yet, see bug 1864737
break;
}
#undef RULE_CASE_WITH_PREFIX
#undef RULE_CASE_LOCKED
#undef RULE_CASE_UNLOCKED
});
}
ServoCSSRuleList::~ServoCSSRuleList() {
MOZ_ASSERT(!mStyleSheet, "Backpointer should have been cleared");
MOZ_ASSERT(!mParentRule, "Backpointer should have been cleared");
DropAllRules();
}
bool ServoCSSRuleList::IsReadOnly() const {
MOZ_ASSERT(!mStyleSheet || !mParentRule ||
mStyleSheet->IsReadOnly() == mParentRule->IsReadOnly(),
"a parent rule should be read only iff the owning sheet is "
"read only");
return mStyleSheet && mStyleSheet->IsReadOnly();
}
} // namespace mozilla
|