/src/mozilla-central/layout/inspector/ServoStyleRuleMap.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | #include "mozilla/ServoStyleRuleMap.h" |
8 | | |
9 | | #include "mozilla/css/GroupRule.h" |
10 | | #include "mozilla/dom/CSSImportRule.h" |
11 | | #include "mozilla/dom/CSSRuleBinding.h" |
12 | | #include "mozilla/dom/CSSStyleRule.h" |
13 | | #include "mozilla/IntegerRange.h" |
14 | | #include "mozilla/ServoStyleSet.h" |
15 | | #include "mozilla/StyleSheetInlines.h" |
16 | | #include "nsDocument.h" |
17 | | #include "nsStyleSheetService.h" |
18 | | |
19 | | namespace mozilla { |
20 | | |
21 | | void |
22 | | ServoStyleRuleMap::EnsureTable(ServoStyleSet& aStyleSet) |
23 | 0 | { |
24 | 0 | if (!IsEmpty()) { |
25 | 0 | return; |
26 | 0 | } |
27 | 0 | aStyleSet.EnumerateStyleSheetArrays( |
28 | 0 | [this](const nsTArray<RefPtr<StyleSheet>>& aArray) { |
29 | 0 | for (auto& sheet : aArray) { |
30 | 0 | FillTableFromStyleSheet(*sheet); |
31 | 0 | } |
32 | 0 | }); |
33 | 0 | } |
34 | | |
35 | | void |
36 | | ServoStyleRuleMap::EnsureTable(nsXBLPrototypeResources& aXBLResources) |
37 | 0 | { |
38 | 0 | if (!IsEmpty() || !aXBLResources.GetServoStyles()) { |
39 | 0 | return; |
40 | 0 | } |
41 | 0 | for (auto index : IntegerRange(aXBLResources.SheetCount())) { |
42 | 0 | FillTableFromStyleSheet(*aXBLResources.StyleSheetAt(index)); |
43 | 0 | } |
44 | 0 | } |
45 | | |
46 | | void |
47 | | ServoStyleRuleMap::EnsureTable(ShadowRoot& aShadowRoot) |
48 | 0 | { |
49 | 0 | if (!IsEmpty()) { |
50 | 0 | return; |
51 | 0 | } |
52 | 0 | for (auto index : IntegerRange(aShadowRoot.SheetCount())) { |
53 | 0 | FillTableFromStyleSheet(*aShadowRoot.SheetAt(index)); |
54 | 0 | } |
55 | 0 | } |
56 | | |
57 | | void |
58 | | ServoStyleRuleMap::SheetAdded(StyleSheet& aStyleSheet) |
59 | 0 | { |
60 | 0 | if (!IsEmpty()) { |
61 | 0 | FillTableFromStyleSheet(aStyleSheet); |
62 | 0 | } |
63 | 0 | } |
64 | | |
65 | | void |
66 | | ServoStyleRuleMap::SheetRemoved(StyleSheet& aStyleSheet) |
67 | 0 | { |
68 | 0 | // Invalidate all data inside. This isn't strictly necessary since |
69 | 0 | // we should always get update from document before new queries come. |
70 | 0 | // But it is probably still safer if we try to avoid having invalid |
71 | 0 | // pointers inside. Also if the document keep adding and removing |
72 | 0 | // stylesheets, this would also prevent us from infinitely growing |
73 | 0 | // memory usage. |
74 | 0 | mTable.Clear(); |
75 | 0 | } |
76 | | |
77 | | void |
78 | | ServoStyleRuleMap::RuleAdded(StyleSheet& aStyleSheet, css::Rule& aStyleRule) |
79 | 0 | { |
80 | 0 | if (!IsEmpty()) { |
81 | 0 | FillTableFromRule(aStyleRule); |
82 | 0 | } |
83 | 0 | } |
84 | | |
85 | | void |
86 | | ServoStyleRuleMap::RuleRemoved(StyleSheet& aStyleSheet, |
87 | | css::Rule& aStyleRule) |
88 | 0 | { |
89 | 0 | if (IsEmpty()) { |
90 | 0 | return; |
91 | 0 | } |
92 | 0 | |
93 | 0 | switch (aStyleRule.Type()) { |
94 | 0 | case CSSRule_Binding::STYLE_RULE: { |
95 | 0 | auto& rule = static_cast<CSSStyleRule&>(aStyleRule); |
96 | 0 | mTable.Remove(rule.Raw()); |
97 | 0 | break; |
98 | 0 | } |
99 | 0 | case CSSRule_Binding::IMPORT_RULE: |
100 | 0 | case CSSRule_Binding::MEDIA_RULE: |
101 | 0 | case CSSRule_Binding::SUPPORTS_RULE: |
102 | 0 | case CSSRule_Binding::DOCUMENT_RULE: { |
103 | 0 | // See the comment in StyleSheetRemoved. |
104 | 0 | mTable.Clear(); |
105 | 0 | break; |
106 | 0 | } |
107 | 0 | case CSSRule_Binding::FONT_FACE_RULE: |
108 | 0 | case CSSRule_Binding::PAGE_RULE: |
109 | 0 | case CSSRule_Binding::KEYFRAMES_RULE: |
110 | 0 | case CSSRule_Binding::KEYFRAME_RULE: |
111 | 0 | case CSSRule_Binding::NAMESPACE_RULE: |
112 | 0 | case CSSRule_Binding::COUNTER_STYLE_RULE: |
113 | 0 | case CSSRule_Binding::FONT_FEATURE_VALUES_RULE: |
114 | 0 | break; |
115 | 0 | default: |
116 | 0 | MOZ_ASSERT_UNREACHABLE("Unhandled rule"); |
117 | 0 | } |
118 | 0 | } |
119 | | |
120 | | size_t |
121 | | ServoStyleRuleMap::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const |
122 | 0 | { |
123 | 0 | size_t n = aMallocSizeOf(this); |
124 | 0 | n += mTable.ShallowSizeOfExcludingThis(aMallocSizeOf); |
125 | 0 | return n; |
126 | 0 | } |
127 | | |
128 | | void |
129 | | ServoStyleRuleMap::FillTableFromRule(css::Rule& aRule) |
130 | 0 | { |
131 | 0 | switch (aRule.Type()) { |
132 | 0 | case CSSRule_Binding::STYLE_RULE: { |
133 | 0 | auto& rule = static_cast<CSSStyleRule&>(aRule); |
134 | 0 | mTable.Put(rule.Raw(), &rule); |
135 | 0 | break; |
136 | 0 | } |
137 | 0 | case CSSRule_Binding::MEDIA_RULE: |
138 | 0 | case CSSRule_Binding::SUPPORTS_RULE: |
139 | 0 | case CSSRule_Binding::DOCUMENT_RULE: { |
140 | 0 | auto& rule = static_cast<css::GroupRule&>(aRule); |
141 | 0 | auto ruleList = static_cast<ServoCSSRuleList*>(rule.CssRules()); |
142 | 0 | FillTableFromRuleList(*ruleList); |
143 | 0 | break; |
144 | 0 | } |
145 | 0 | case CSSRule_Binding::IMPORT_RULE: { |
146 | 0 | auto& rule = static_cast<CSSImportRule&>(aRule); |
147 | 0 | MOZ_ASSERT(aRule.GetStyleSheet()); |
148 | 0 | FillTableFromStyleSheet(*rule.GetStyleSheet()); |
149 | 0 | break; |
150 | 0 | } |
151 | 0 | } |
152 | 0 | } |
153 | | |
154 | | void |
155 | | ServoStyleRuleMap::FillTableFromRuleList(ServoCSSRuleList& aRuleList) |
156 | 0 | { |
157 | 0 | for (uint32_t i : IntegerRange(aRuleList.Length())) { |
158 | 0 | FillTableFromRule(*aRuleList.GetRule(i)); |
159 | 0 | } |
160 | 0 | } |
161 | | |
162 | | void |
163 | | ServoStyleRuleMap::FillTableFromStyleSheet(StyleSheet& aSheet) |
164 | 0 | { |
165 | 0 | if (aSheet.IsComplete()) { |
166 | 0 | FillTableFromRuleList(*aSheet.GetCssRulesInternal()); |
167 | 0 | } |
168 | 0 | } |
169 | | |
170 | | } // namespace mozilla |