Bug 1642579 [wpt PR 23909] - [COOP access reporting] Preliminary WPT tests., a=testonly
[gecko.git] / layout / style / Rule.h
blobfe4055b8772c084dc2813c0366499936ea3657d2
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/. */
7 /* base class for all rule types in a CSS style sheet */
9 #ifndef mozilla_css_Rule_h___
10 #define mozilla_css_Rule_h___
12 #include "mozilla/dom/CSSRuleBinding.h"
13 #include "mozilla/dom/DocumentOrShadowRoot.h"
14 #include "mozilla/StyleSheet.h"
15 #include "mozilla/MemoryReporting.h"
16 #include "nsISupports.h"
17 #include "nsWrapperCache.h"
19 struct nsRuleData;
20 template <class T>
21 struct already_AddRefed;
22 class nsHTMLCSSStyleSheet;
24 namespace mozilla {
25 namespace css {
26 class GroupRule;
28 class Rule : public nsISupports, public nsWrapperCache {
29 protected:
30 Rule(StyleSheet* aSheet, Rule* aParentRule, uint32_t aLineNumber,
31 uint32_t aColumnNumber)
32 : mSheet(aSheet),
33 mParentRule(aParentRule),
34 mLineNumber(aLineNumber),
35 mColumnNumber(aColumnNumber) {
36 #ifdef DEBUG
37 // Would be nice to check that this->Type() is KEYFRAME_RULE when
38 // mParentRule->Tye() is KEYFRAMES_RULE, but we can't call
39 // this->Type() here since it's virtual.
40 if (mParentRule) {
41 int16_t type = mParentRule->Type();
42 MOZ_ASSERT(type == dom::CSSRule_Binding::MEDIA_RULE ||
43 type == dom::CSSRule_Binding::DOCUMENT_RULE ||
44 type == dom::CSSRule_Binding::SUPPORTS_RULE ||
45 type == dom::CSSRule_Binding::KEYFRAMES_RULE);
47 #endif
50 Rule(const Rule& aCopy)
51 : mSheet(aCopy.mSheet),
52 mParentRule(aCopy.mParentRule),
53 mLineNumber(aCopy.mLineNumber),
54 mColumnNumber(aCopy.mColumnNumber) {}
56 virtual ~Rule() = default;
58 public:
59 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
60 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(Rule)
61 // Return true if this rule is known to be a cycle collection leaf, in the
62 // sense that it doesn't have any outgoing owning edges.
63 virtual bool IsCCLeaf() const MOZ_MUST_OVERRIDE;
65 #ifdef DEBUG
66 virtual void List(FILE* out = stdout, int32_t aIndent = 0) const = 0;
67 #endif
69 StyleSheet* GetStyleSheet() const { return mSheet; }
71 // Clear the mSheet pointer on this rule and descendants.
72 virtual void DropSheetReference();
74 // Clear the mParentRule pointer on this rule.
75 void DropParentRuleReference() { mParentRule = nullptr; }
77 void DropReferences() {
78 DropSheetReference();
79 DropParentRuleReference();
82 uint32_t GetLineNumber() const { return mLineNumber; }
83 uint32_t GetColumnNumber() const { return mColumnNumber; }
85 // Whether this a rule in a read only style sheet.
86 bool IsReadOnly() const;
88 // Whether this rule is an @import rule that hasn't loaded yet (and thus
89 // doesn't affect the style of the page).
90 bool IsIncompleteImportRule() const;
92 // This is pure virtual because all of Rule's data members are non-owning and
93 // thus measured elsewhere.
94 virtual size_t SizeOfIncludingThis(MallocSizeOf) const MOZ_MUST_OVERRIDE = 0;
96 // WebIDL interface
97 virtual uint16_t Type() const = 0;
98 virtual void GetCssText(nsAString& aCssText) const = 0;
99 void SetCssText(const nsAString& aCssText);
100 Rule* GetParentRule() const;
101 StyleSheet* GetParentStyleSheet() const { return GetStyleSheet(); }
102 nsINode* GetParentObject() const {
103 if (!mSheet) {
104 return nullptr;
106 auto* associated = mSheet->GetAssociatedDocumentOrShadowRoot();
107 return associated ? &associated->AsNode() : nullptr;
110 protected:
111 // True if we're known-live for cycle collection purposes.
112 bool IsKnownLive() const;
114 // Hook subclasses can use to properly unlink the nsWrapperCache of
115 // their declarations.
116 void UnlinkDeclarationWrapper(nsWrapperCache& aDecl);
118 // mSheet should only ever be null when we create a synthetic CSSFontFaceRule
119 // for an InspectorFontFace.
121 // mSheet and mParentRule will be cleared when they are detached from the
122 // parent object, either because the rule is removed or the parent is
123 // destroyed.
124 StyleSheet* MOZ_NON_OWNING_REF mSheet;
125 Rule* MOZ_NON_OWNING_REF mParentRule;
127 // Keep the same type so that MSVC packs them.
128 uint32_t mLineNumber;
129 uint32_t mColumnNumber;
132 } // namespace css
133 } // namespace mozilla
135 #endif /* mozilla_css_Rule_h___ */