Bug 1921551 - React to sync sign in flow correctly r=android-reviewers,matt-tighe
[gecko.git] / layout / style / Rule.h
blob80222b2e4b0058ce6238c1815126b3943a0b177b
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 "mozilla/WeakPtr.h"
17 #include "nsISupports.h"
18 #include "nsWrapperCache.h"
20 template <class T>
21 struct already_AddRefed;
23 namespace mozilla {
25 enum class StyleCssRuleType : uint8_t;
27 namespace css {
28 class GroupRule;
30 class Rule : public nsISupports, public nsWrapperCache, public SupportsWeakPtr {
31 protected:
32 Rule(StyleSheet* aSheet, Rule* aParentRule, uint32_t aLineNumber,
33 uint32_t aColumnNumber)
34 : mSheet(aSheet),
35 mParentRule(aParentRule),
36 mLineNumber(aLineNumber),
37 mColumnNumber(aColumnNumber) {
38 #ifdef DEBUG
39 AssertParentRuleType();
40 #endif
43 #ifdef DEBUG
44 void AssertParentRuleType();
45 #endif
47 Rule(const Rule& aCopy)
48 : mSheet(aCopy.mSheet),
49 mParentRule(aCopy.mParentRule),
50 mLineNumber(aCopy.mLineNumber),
51 mColumnNumber(aCopy.mColumnNumber) {}
53 virtual ~Rule() = default;
55 public:
56 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
57 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_WRAPPERCACHE_CLASS(Rule)
58 // Return true if this rule is known to be a cycle collection leaf, in the
59 // sense that it doesn't have any outgoing owning edges.
60 virtual bool IsCCLeaf() const MOZ_MUST_OVERRIDE;
62 virtual bool IsGroupRule() const { return false; }
64 #ifdef DEBUG
65 virtual void List(FILE* out = stdout, int32_t aIndent = 0) const = 0;
66 #endif
68 StyleSheet* GetStyleSheet() const { return mSheet; }
70 // Clear the mSheet pointer on this rule and descendants.
71 virtual void DropSheetReference();
73 // Clear the mParentRule pointer on this rule.
74 void DropParentRuleReference() { mParentRule = nullptr; }
76 void DropReferences() {
77 DropSheetReference();
78 DropParentRuleReference();
81 uint32_t GetLineNumber() const { return mLineNumber; }
82 uint32_t GetColumnNumber() const { return mColumnNumber; }
84 // Whether this a rule in a read only style sheet.
85 bool IsReadOnly() const;
87 // Whether this rule is an @import rule that hasn't loaded yet (and thus
88 // doesn't affect the style of the page).
89 bool IsIncompleteImportRule() const;
91 // This is pure virtual because all of Rule's data members are non-owning and
92 // thus measured elsewhere.
93 virtual size_t SizeOfIncludingThis(MallocSizeOf) const MOZ_MUST_OVERRIDE = 0;
95 virtual StyleCssRuleType Type() const = 0;
97 // WebIDL interface
98 uint16_t TypeForBindings() const {
99 auto type = uint16_t(Type());
100 // Per https://drafts.csswg.org/cssom/#dom-cssrule-type for constants > 15
101 // we return 0.
102 return type > 15 ? 0 : type;
104 virtual void GetCssText(nsACString& aCssText) const = 0;
105 void SetCssText(const nsACString& aCssText);
106 Rule* GetParentRule() const;
107 StyleSheet* GetParentStyleSheet() const { return GetStyleSheet(); }
108 nsINode* GetAssociatedDocumentOrShadowRoot() const {
109 if (!mSheet) {
110 return nullptr;
112 auto* associated = mSheet->GetAssociatedDocumentOrShadowRoot();
113 return associated ? &associated->AsNode() : nullptr;
115 nsISupports* GetParentObject() const { return mSheet; }
117 struct ContainingRuleState {
118 uint32_t mContainingTypes = 0;
119 Maybe<StyleCssRuleType> mParseRelativeType;
121 static ContainingRuleState From(Rule* aRule) {
122 return aRule ? aRule->GetContainingRuleStateForParsing()
123 : ContainingRuleState();
126 ContainingRuleState GetContainingRuleStateForParsing() const;
128 protected:
129 // True if we're known-live for cycle collection purposes.
130 bool IsKnownLive() const;
132 // Hook subclasses can use to properly unlink the nsWrapperCache of
133 // their declarations.
134 void UnlinkDeclarationWrapper(nsWrapperCache& aDecl);
136 // mSheet should only ever be null when we create a synthetic CSSFontFaceRule
137 // for an InspectorFontFace.
139 // mSheet and mParentRule will be cleared when they are detached from the
140 // parent object, either because the rule is removed or the parent is
141 // destroyed.
142 StyleSheet* MOZ_NON_OWNING_REF mSheet;
143 Rule* MOZ_NON_OWNING_REF mParentRule;
145 // Keep the same type so that MSVC packs them.
146 uint32_t mLineNumber;
147 uint32_t mColumnNumber;
150 } // namespace css
151 } // namespace mozilla
153 #endif /* mozilla_css_Rule_h___ */