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 /* representation of CSSRuleList for stylo */
8
9 #include "mozilla/ServoCSSRuleList.h"
10
11 #include "mozilla/IntegerRange.h"
12 #include "mozilla/ServoBindings.h"
13 #include "mozilla/ServoDocumentRule.h"
14 #include "mozilla/ServoImportRule.h"
15 #include "mozilla/ServoFontFeatureValuesRule.h"
16 #include "mozilla/ServoKeyframesRule.h"
17 #include "mozilla/ServoMediaRule.h"
18 #include "mozilla/ServoNamespaceRule.h"
19 #include "mozilla/ServoPageRule.h"
20 #include "mozilla/ServoStyleRule.h"
21 #include "mozilla/ServoStyleSheet.h"
22 #include "mozilla/ServoSupportsRule.h"
23 #include "nsCSSCounterStyleRule.h"
24 #include "nsCSSFontFaceRule.h"
25
26 using namespace mozilla::dom;
27
28 namespace mozilla {
29
ServoCSSRuleList(already_AddRefed<ServoCssRules> aRawRules,ServoStyleSheet * aDirectOwnerStyleSheet)30 ServoCSSRuleList::ServoCSSRuleList(already_AddRefed<ServoCssRules> aRawRules,
31 ServoStyleSheet* aDirectOwnerStyleSheet)
32 : mStyleSheet(aDirectOwnerStyleSheet), mRawRules(aRawRules) {
33 Servo_CssRules_ListTypes(mRawRules, &mRules);
34 }
35
36 // QueryInterface implementation for ServoCSSRuleList
37 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ServoCSSRuleList)
38 NS_INTERFACE_MAP_END_INHERITING(dom::CSSRuleList)
39
40 NS_IMPL_ADDREF_INHERITED(ServoCSSRuleList, dom::CSSRuleList)
41 NS_IMPL_RELEASE_INHERITED(ServoCSSRuleList, dom::CSSRuleList)
42
43 NS_IMPL_CYCLE_COLLECTION_CLASS(ServoCSSRuleList)
44
45 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ServoCSSRuleList)
46 tmp->DropAllRules();
47 NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(dom::CSSRuleList)
48 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ServoCSSRuleList,
49 dom::CSSRuleList)
__anon0f6ab9ce0102(css::Rule* aRule) 50 tmp->EnumerateInstantiatedRules([&](css::Rule* aRule) {
51 if (!aRule->IsCCLeaf()) {
52 NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mRules[i]");
53 cb.NoteXPCOMChild(aRule);
54 // Note about @font-face and @counter-style rule again, since
55 // there is an indirect owning edge through Servo's struct that
56 // FontFaceRule / CounterStyleRule in Servo owns a Gecko
57 // nsCSSFontFaceRule / nsCSSCounterStyleRule object.
58 auto type = aRule->Type();
59 if (type == CSSRuleBinding::FONT_FACE_RULE ||
60 type == CSSRuleBinding::COUNTER_STYLE_RULE) {
61 NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mRawRules[i]");
62 cb.NoteXPCOMChild(aRule);
63 }
64 }
65 });
66 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
67
SetParentRule(css::GroupRule * aParentRule)68 void ServoCSSRuleList::SetParentRule(css::GroupRule* aParentRule) {
69 mParentRule = aParentRule;
70 EnumerateInstantiatedRules(
71 [aParentRule](css::Rule* rule) { rule->SetParentRule(aParentRule); });
72 }
73
SetStyleSheet(StyleSheet * aStyleSheet)74 void ServoCSSRuleList::SetStyleSheet(StyleSheet* aStyleSheet) {
75 mStyleSheet = aStyleSheet ? aStyleSheet->AsServo() : nullptr;
76 EnumerateInstantiatedRules(
77 [this](css::Rule* rule) { rule->SetStyleSheet(mStyleSheet); });
78 }
79
GetRule(uint32_t aIndex)80 css::Rule* ServoCSSRuleList::GetRule(uint32_t aIndex) {
81 uintptr_t rule = mRules[aIndex];
82 if (rule <= kMaxRuleType) {
83 RefPtr<css::Rule> ruleObj = nullptr;
84 switch (rule) {
85 #define CASE_RULE(const_, name_) \
86 case CSSRuleBinding::const_##_RULE: { \
87 uint32_t line = 0, column = 0; \
88 RefPtr<RawServo##name_##Rule> rule = \
89 Servo_CssRules_Get##name_##RuleAt(mRawRules, aIndex, &line, &column) \
90 .Consume(); \
91 MOZ_ASSERT(rule); \
92 ruleObj = new Servo##name_##Rule(rule.forget(), line, column); \
93 break; \
94 }
95 CASE_RULE(STYLE, Style)
96 CASE_RULE(KEYFRAMES, Keyframes)
97 CASE_RULE(MEDIA, Media)
98 CASE_RULE(NAMESPACE, Namespace)
99 CASE_RULE(PAGE, Page)
100 CASE_RULE(SUPPORTS, Supports)
101 CASE_RULE(DOCUMENT, Document)
102 CASE_RULE(IMPORT, Import)
103 CASE_RULE(FONT_FEATURE_VALUES, FontFeatureValues)
104 #undef CASE_RULE
105 // For @font-face and @counter-style rules, the function returns
106 // a borrowed Gecko rule object directly, so we don't need to
107 // create anything here. But we still need to have the style sheet
108 // and parent rule set properly.
109 case CSSRuleBinding::FONT_FACE_RULE: {
110 ruleObj = Servo_CssRules_GetFontFaceRuleAt(mRawRules, aIndex);
111 break;
112 }
113 case CSSRuleBinding::COUNTER_STYLE_RULE: {
114 ruleObj = Servo_CssRules_GetCounterStyleRuleAt(mRawRules, aIndex);
115 break;
116 }
117 case CSSRuleBinding::KEYFRAME_RULE:
118 MOZ_ASSERT_UNREACHABLE("keyframe rule cannot be here");
119 return nullptr;
120 default:
121 NS_WARNING("stylo: not implemented yet");
122 return nullptr;
123 }
124 ruleObj->SetStyleSheet(mStyleSheet);
125 ruleObj->SetParentRule(mParentRule);
126 rule = CastToUint(ruleObj.forget().take());
127 mRules[aIndex] = rule;
128 }
129 return CastToPtr(rule);
130 }
131
IndexedGetter(uint32_t aIndex,bool & aFound)132 css::Rule* ServoCSSRuleList::IndexedGetter(uint32_t aIndex, bool& aFound) {
133 if (aIndex >= mRules.Length()) {
134 aFound = false;
135 return nullptr;
136 }
137 aFound = true;
138 return GetRule(aIndex);
139 }
140
141 template <typename Func>
EnumerateInstantiatedRules(Func aCallback)142 void ServoCSSRuleList::EnumerateInstantiatedRules(Func aCallback) {
143 for (uintptr_t rule : mRules) {
144 if (rule > kMaxRuleType) {
145 aCallback(CastToPtr(rule));
146 }
147 }
148 }
149
DropRule(already_AddRefed<css::Rule> aRule)150 static void DropRule(already_AddRefed<css::Rule> aRule) {
151 RefPtr<css::Rule> rule = aRule;
152 rule->SetStyleSheet(nullptr);
153 rule->SetParentRule(nullptr);
154 }
155
DropAllRules()156 void ServoCSSRuleList::DropAllRules() {
157 EnumerateInstantiatedRules(
158 [](css::Rule* rule) { DropRule(already_AddRefed<css::Rule>(rule)); });
159 mRules.Clear();
160 mRawRules = nullptr;
161 }
162
DropReference()163 void ServoCSSRuleList::DropReference() {
164 mStyleSheet = nullptr;
165 mParentRule = nullptr;
166 DropAllRules();
167 }
168
InsertRule(const nsAString & aRule,uint32_t aIndex)169 nsresult ServoCSSRuleList::InsertRule(const nsAString& aRule, uint32_t aIndex) {
170 MOZ_ASSERT(mStyleSheet,
171 "Caller must ensure that "
172 "the list is not unlinked from stylesheet");
173 NS_ConvertUTF16toUTF8 rule(aRule);
174 bool nested = !!mParentRule;
175 css::Loader* loader = nullptr;
176 if (nsIDocument* doc = mStyleSheet->GetAssociatedDocument()) {
177 loader = doc->CSSLoader();
178 }
179 uint16_t type;
180 nsresult rv =
181 Servo_CssRules_InsertRule(mRawRules, mStyleSheet->RawContents(), &rule,
182 aIndex, nested, loader, mStyleSheet, &type);
183 if (NS_FAILED(rv)) {
184 return rv;
185 }
186 mRules.InsertElementAt(aIndex, type);
187 return rv;
188 }
189
DeleteRule(uint32_t aIndex)190 nsresult ServoCSSRuleList::DeleteRule(uint32_t aIndex) {
191 nsresult rv = Servo_CssRules_DeleteRule(mRawRules, aIndex);
192 if (!NS_FAILED(rv)) {
193 uintptr_t rule = mRules[aIndex];
194 if (rule > kMaxRuleType) {
195 DropRule(already_AddRefed<css::Rule>(CastToPtr(rule)));
196 }
197 mRules.RemoveElementAt(aIndex);
198 }
199 return rv;
200 }
201
GetDOMCSSRuleType(uint32_t aIndex) const202 uint16_t ServoCSSRuleList::GetDOMCSSRuleType(uint32_t aIndex) const {
203 uintptr_t rule = mRules[aIndex];
204 if (rule <= kMaxRuleType) {
205 return rule;
206 }
207 return CastToPtr(rule)->Type();
208 }
209
~ServoCSSRuleList()210 ServoCSSRuleList::~ServoCSSRuleList() {
211 MOZ_ASSERT(!mStyleSheet, "Backpointer should have been cleared");
212 MOZ_ASSERT(!mParentRule, "Backpointer should have been cleared");
213 DropAllRules();
214 }
215
216 } // namespace mozilla
217